source: branches/feature-module-update/data/class/pages/upgrade/LC_Page_Upgrade_Download.php @ 16880

Revision 16880, 11.4 KB checked in by naka, 15 years ago (diff)

スペルミス修正

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24// {{{ requires
25require_once CLASS_PATH . 'pages/upgrade/LC_Page_Upgrade_Base.php';
26require_once DATA_PATH . 'module/Tar.php';
27
28/**
29 * オーナーズストアからダウンロードデータを取得する.
30 *
31 * TODO 要リファクタリング
32 *
33 * @package Page
34 * @author LOCKON CO.,LTD.
35 * @version $Id$
36 */
37class LC_Page_Upgrade_Download extends LC_Page_Upgrade_Base {
38
39    // }}}
40    // {{{ functions
41
42    /**
43     * Page を初期化する.
44     *
45     * @return void
46     */
47    function init() {
48        parent::init();
49    }
50
51    /**
52     * Page のプロセス.
53     *
54     * @return void
55     */
56    function process($mode) {
57        $objLog  = new LC_Upgrade_Helper_Log;
58        $objLog->start($mode);
59
60        $objJson = new LC_Upgrade_Helper_Json;
61
62        // アクセスチェック
63        $objLog->log('* auth start');
64        if ($this->isValidAccess($mode) !== true) {
65            // TODO
66            $objJson->setError(OSTORE_E_C_INVALID_ACCESS);
67            $objJson->display();
68            $objLog->error(OSTORE_E_C_INVALID_ACCESS);
69            return;
70        }
71
72        // パラメーチェック
73        $this->initParam();
74        $objLog->log('* post param check start');
75        if ($this->objForm->checkError()) {
76            $objJson->setError(OSTORE_E_C_INVALID_PARAM);
77            $objJson->display();
78            $objLog->error(OSTORE_E_C_INVALID_PARAM, $_POST);
79            return;
80        }
81
82        $objLog->log('* auto update check start');
83        if ($mode == 'auto_update'
84        && $this->autoUpdateEnable($this->objForm->getValue('product_id')) !== true) {
85            $objJson->setError(OSTORE_E_C_AUTOUP_DISABLE);
86            $objJson->display();
87            $objLog->error(OSTORE_E_C_AUTOUP_DISABLE, $_POST);
88            return;
89        }
90
91        // TODO CSRF対策
92
93        // 認証キーの取得
94        $public_key = $this->getPublicKey();
95        $sha1_key = $this->createSeed();
96
97        // 認証キーチェック
98        $objLog->log('* public key check start');
99        if (empty($public_key)) {
100            $objJson->setError(OSTORE_E_C_NO_KEY);
101            $objJson->display();
102            $objLog->error(OSTORE_E_C_NO_KEY);
103            return;
104        }
105
106        // リクエストを開始
107        $objLog->log('* http request start');
108        $arrPostData = array(
109            'eccube_url' => SITE_URL,
110            'public_key' => sha1($public_key . $sha1_key),
111            'sha1_key'   => $sha1_key,
112            'product_id' => $this->objForm->getValue('product_id')
113        );
114        $objReq = $this->request($mode, $arrPostData);
115
116        // リクエストチェック
117        $objLog->log('* http request check start');
118        if (PEAR::isError($objReq)) {
119            $objJson->setError(OSTORE_E_C_HTTP_REQ);
120            $objJson->display();
121            $objLogerr(OSTORE_E_C_HTTP_REQ, $objReq);
122            return;
123        }
124
125        // レスポンスチェック
126        $objLog->log('* http response check start');
127        if ($objReq->getResponseCode() !== 200) {
128            $objJson->setError(OSTORE_E_C_HTTP_RESP);
129            $objJson->display();
130            $objLog->error(OSTORE_E_C_HTTP_RESP, $objReq);
131            return;
132        }
133
134        $body = $objReq->getResponseBody();
135        $objRet = $objJson->decode($body);
136
137        // JSONデータのチェック
138        $objLog->log('* json data check start');
139        if (empty($objRet)) {
140            $objJson->setError(OSTORE_E_C_FAILED_JSON_PARSE);
141            $objJson->display();
142            $objLog->error(OSTORE_E_C_FAILED_JSON_PARSE, $objReq);
143            return;
144        }
145
146        // ダウンロードデータの保存
147        if ($objRet->status === OSTORE_STATUS_SUCCESS) {
148            $objLog->log('* save file start');
149            $time = time();
150            $dir  = DATA_PATH . 'downloads/tmp/';
151            $filename = $time . '.tar.gz';
152
153            $data = base64_decode($objRet->data->dl_file);
154
155            $objLog->log("* open ${filename} start");
156            if ($fp = @fopen($dir . $filename, "w")) {
157                @fwrite($fp, $data);
158                @fclose($fp);
159            } else {
160                $objJson->setError(OSTORE_E_C_PERMISSION);
161                $objJson->display();
162                $objLog->error(OSTORE_E_C_PERMISSION, $dir . $filename);
163                return;
164            }
165
166            // ダウンロードアーカイブを展開する
167            $exract_dir = $dir . $time;
168            $objLog->log("* mkdir ${exract_dir} start");
169            if (!@mkdir($exract_dir)) {
170                $objJson->setError(OSTORE_E_C_PERMISSION);
171                $objJson->display();
172                $objLog->error(OSTORE_E_C_PERMISSION, $exract_dir);
173                return;
174            }
175
176            $objLog->log("* extract ${dir}${filename} start");
177            $tar = new Archive_Tar($dir . $filename);
178            $tar->extract($exract_dir);
179
180            $objLog->log("* copy batch start");
181            @include_once CLASS_PATH . 'batch/SC_Batch_Update.php';
182            $objBatch = new SC_Batch_Update();
183            $arrCopyLog = $objBatch->execute($exract_dir);
184
185            $objLog->log("* copy batch check start");
186            if (count($arrCopyLog['err']) > 0) {
187                $objJson->setError(OSTORE_E_C_PERMISSION);
188                $objJson->display();
189                $objLog->error(OSTORE_E_C_PERMISSION, $arrCopyLog);
190                $this->registerUpdateLog($arrCopyLog, $objRet->data);
191                return;
192            }
193
194            // dtb_module_update_logの更新
195            $objLog->log("* insert dtb_module_update start");
196            $this->registerUpdateLog($arrCopyLog, $objRet->data);
197
198            // dtb_moduleの更新
199            $objLog->log("* insert/update dtb_module start");
200            $this->updateMdlTable($objRet->data);
201
202            // 配信サーバへ通知
203            $objLog->log("* notify to lockon server start");
204            $objReq = $this->notifyDownload($mode, $objReq->getResponseCookies());
205
206            $objLog->log('* dl commit result:' . serialize($objReq));
207
208            $objJson->setSUCCESS(array(), 'インストール/アップデートに成功しました。');
209            $objJson->display();
210            $objLog->end();
211            return;
212        } else {
213            // 配信サーバ側でエラーを補足
214            echo $body;
215            $objLog->error($objRet->errcode, $objReq);
216            return;
217        }
218    }
219
220    /**
221     * デストラクタ
222     *
223     * @return void
224     */
225    function destroy() {
226        parent::destroy();
227    }
228
229    function initParam() {
230        $this->objForm = new SC_FormParam();
231        $this->objForm->addParam(
232            'product_id', 'product_id', INT_LEN, '', array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK')
233        );
234        $this->objForm->setParam($_POST);
235    }
236
237    /**
238     * dtb_moduleを更新する
239     *
240     * @param object $objRet
241     */
242    function updateMdlTable($objRet) {
243        $table = 'dtb_module';
244        $where = 'module_id = ?';
245        $objQuery = new SC_Query;
246
247        $count = $objQuery->count($table, $where, array($objRet->product_id));
248        if ($count) {
249            $arrUpdate = array(
250                'module_code' => $objRet->product_code,
251                'module_name' => $objRet->product_name,
252                'update_date' => 'NOW()'
253            );
254            $objQuery->update($table, $arrUpdate ,$where, array($objRet->product_id));
255        } else {
256            $arrInsert = array(
257                'module_id'   => $objRet->product_id,
258                'module_code' => $objRet->product_code,
259                'module_name' => $objRet->product_name,
260                'auto_update_flg' => '0',
261                'create_date'     => 'NOW()',
262                'update_date' => 'NOW()'
263            );
264            $objQuery->insert($table, $arrInsert);
265        }
266    }
267
268    /**
269     * 配信サーバへダウンロード完了を通知する.
270     *
271     * FIXME エラーコード追加
272     * @param array #arrCookies Cookie配列
273     * @retrun
274     */
275    function notifyDownload($mode, $arrCookies) {
276        $arrPOSTParams = array(
277            'eccube_url' => SITE_URL
278        );
279        $objReq = $this->request($mode . '_commit', $arrPOSTParams, $arrCookies);
280        return $objReq;
281    }
282
283    /**
284     * アクセスチェック
285     *
286     * @return boolean
287     */
288    function isValidAccess($mode) {
289        $objLog = new LC_Upgrade_Helper_Log;
290        switch ($mode) {
291        // モジュールダウンロード
292        case 'download':
293            if ($this->isLoggedInAdminPage() === true) {
294                $objLog->log('* admin login ok');
295                return true;
296            }
297            break;
298        // 自動アップロード最新ファイル取得
299        case 'patch_download':
300        // モジュール自動アップロード
301        case 'auto_update':
302            $objForm = new SC_FormParam;
303            $objForm->addParam('public_key', 'public_key', MTEXT_LEN, '', array('EXIST_CHECK', 'ALNUM_CHECK', 'MAX_LENGTH_CHECK'));
304            $objForm->addParam('sha1_key', 'sha1_key', MTEXT_LEN, '', array('EXIST_CHECK', 'ALNUM_CHECK', 'MAX_LENGTH_CHECK'));
305            $objForm->setParam($_POST);
306
307            $objLog->log('* param check start');
308            $arrErr = $objForm->checkError();
309            if ($arrErr) {
310                $objLog->log('* invalid param ' . print_r($arrErr, true));               
311                return false;
312            }
313
314            $objLog->log('* public_key check start');
315            $public_key = $this->getPublicKey();
316            if (empty($public_key)) {
317                $objLog->log('* public_key not found');
318                return false;
319            }
320
321            $sha1_key = $objForm->getValue('sha1_key');
322            $public_key_sha1 = $objForm->getValue('public_key');
323
324            $objLog->log('* ip check start');
325            if ($this->isValidIP()
326            && $public_key_sha1 === sha1($public_key . $sha1_key)) {
327                $objLog->log('* auto update login ok');
328                return true;
329            }
330            break;
331        default:
332            $objLog->log('* mode invalid ' . $mode);
333            return false;
334        }
335        return false;
336    }
337
338    function registerUpdateLog($arrLog, $objRet) {
339        $arrInsert = array(
340            'module_id'   => $objRet->product_id,
341            'buckup_path' => $arrLog['buckup_path'],
342            'error_flg'   => count($arrLog['err']),
343            'error'       => implode("\n", $arrLog['err']),
344            'ok'          => implode("\n", $arrLog['ok']),
345            'update_date' => 'NOW()',
346            'create_date' => 'NOW()'
347        );
348        $objQuery = new SC_Query;
349        $objQuery->insert('dtb_module_update_logs', $arrInsert);
350    }
351}
352?>
Note: See TracBrowser for help on using the repository browser.