source: branches/version-2_13-dev/data/class/pages/upgrade/LC_Page_Upgrade_Download.php @ 23503

Revision 23503, 12.9 KB checked in by shutta, 10 years ago (diff)

#2448 typo修正・ソース整形・ソースコメントの改善 for 2.13.3

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2013 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
24require_once CLASS_REALDIR . 'pages/upgrade/LC_Page_Upgrade_Base.php';
25
26/**
27 * オーナーズストアからダウンロードデータを取得する.
28 *
29 * TODO 要リファクタリング
30 *
31 * @package Page
32 * @author LOCKON CO.,LTD.
33 * @version $Id$
34 */
35class LC_Page_Upgrade_Download extends LC_Page_Upgrade_Base
36{
37    /**
38     * Page を初期化する.
39     *
40     * @return void
41     */
42    public function init()
43    {
44        parent::init();
45    }
46
47    /**
48     * Page のプロセス.
49     *
50     * @return void
51     */
52    public function process($mode)
53    {
54        $objLog  = new LC_Upgrade_Helper_Log;
55        $objLog->start($mode);
56
57        $objJson = new LC_Upgrade_Helper_Json;
58
59        // アクセスチェック
60        $objLog->log('* auth start');
61        if ($this->isValidAccess($mode) !== true) {
62            // TODO
63            $objJson->setError(OSTORE_E_C_INVALID_ACCESS);
64            $objJson->display();
65            $objLog->error(OSTORE_E_C_INVALID_ACCESS);
66
67            return;
68        }
69
70        // パラメーチェック
71        $this->initParam();
72        $objLog->log('* post param check start');
73        $arrErr = $this->objForm->checkError();
74        if ($arrErr) {
75            $objJson->setError(OSTORE_E_C_INVALID_PARAM);
76            $objJson->display();
77            $objLog->error(OSTORE_E_C_INVALID_PARAM, $_POST);
78            $objLog->log('* post param check error ' . print_r($arrErr, true));
79
80            return;
81        }
82
83        $objLog->log('* auto update check start');
84        if ($mode == 'auto_update'
85        && $this->autoUpdateEnable($this->objForm->getValue('product_id')) !== true) {
86            $objJson->setError(OSTORE_E_C_AUTOUP_DISABLE);
87            $objJson->display();
88            $objLog->error(OSTORE_E_C_AUTOUP_DISABLE, $_POST);
89
90            return;
91        }
92
93        // TODO CSRF対策
94
95        // 認証キーの取得
96        $public_key = $this->getPublicKey();
97        $sha1_key = $this->createSeed();
98
99        // 認証キーチェック
100        $objLog->log('* public key check start');
101        if (empty($public_key)) {
102            $objJson->setError(OSTORE_E_C_NO_KEY);
103            $objJson->display();
104            $objLog->error(OSTORE_E_C_NO_KEY);
105
106            return;
107        }
108
109        // リクエストを開始
110        $objLog->log('* http request start');
111
112        switch ($mode) {
113            case 'patch_download':
114                $arrPostData = array(
115                    'eccube_url' => HTTP_URL,
116                    'public_key' => sha1($public_key . $sha1_key),
117                    'sha1_key'   => $sha1_key,
118                    'patch_code' => 'latest'
119                );
120                break;
121            default:
122                $arrPostData = array(
123                    'eccube_url' => HTTP_URL,
124                    'public_key' => sha1($public_key . $sha1_key),
125                    'sha1_key'   => $sha1_key,
126                    'product_id' => $this->objForm->getValue('product_id')
127                );
128                break;
129        }
130
131        $objReq = $this->request($mode, $arrPostData);
132
133        // リクエストチェック
134        $objLog->log('* http request check start');
135        if (PEAR::isError($objReq)) {
136            $objJson->setError(OSTORE_E_C_HTTP_REQ);
137            $objJson->display();
138            $objLog->error(OSTORE_E_C_HTTP_REQ, $objReq);
139
140            return;
141        }
142
143        // レスポンスチェック
144        $objLog->log('* http response check start');
145        if ($objReq->getResponseCode() !== 200) {
146            $objJson->setError(OSTORE_E_C_HTTP_RESP);
147            $objJson->display();
148            $objLog->error(OSTORE_E_C_HTTP_RESP, $objReq);
149
150            return;
151        }
152
153        $body = $objReq->getResponseBody();
154        $objRet = $objJson->decode($body);
155
156        // JSONデータのチェック
157        $objLog->log('* json data check start');
158        if (empty($objRet)) {
159            $objJson->setError(OSTORE_E_C_FAILED_JSON_PARSE);
160            $objJson->display();
161            $objLog->error(OSTORE_E_C_FAILED_JSON_PARSE, $objReq);
162
163            return;
164        }
165
166        // ダウンロードデータの保存
167        if ($objRet->status === OSTORE_STATUS_SUCCESS) {
168            $objLog->log('* save file start');
169            $time = time();
170            $dir  = DATA_REALDIR . 'downloads/tmp/';
171            $filename = $time . '.tar.gz';
172
173            $data = base64_decode($objRet->data->dl_file);
174
175            $objLog->log("* open ${filename} start");
176            if ($fp = @fopen($dir . $filename, 'w')) {
177                @fwrite($fp, $data);
178                @fclose($fp);
179            } else {
180                $objJson->setError(OSTORE_E_C_PERMISSION);
181                $objJson->display();
182                $objLog->error(OSTORE_E_C_PERMISSION, $dir . $filename);
183
184                return;
185            }
186
187            // ダウンロードアーカイブを展開する
188            $exract_dir = $dir . $time;
189            $objLog->log("* mkdir ${exract_dir} start");
190            if (!@mkdir($exract_dir)) {
191                $objJson->setError(OSTORE_E_C_PERMISSION);
192                $objJson->display();
193                $objLog->error(OSTORE_E_C_PERMISSION, $exract_dir);
194
195                return;
196            }
197
198            $objLog->log("* extract ${dir}${filename} start");
199            $tar = new Archive_Tar($dir . $filename);
200            $tar->extract($exract_dir);
201
202            $objLog->log('* copy batch start');
203            @include_once CLASS_REALDIR . 'batch/SC_Batch_Update.php';
204            $objBatch = new SC_Batch_Update();
205            $arrCopyLog = $objBatch->execute($exract_dir);
206
207            $objLog->log('* copy batch check start');
208            if (count($arrCopyLog['err']) > 0) {
209                $objJson->setError(OSTORE_E_C_BATCH_ERR);
210                $objJson->display();
211                $objLog->error(OSTORE_E_C_BATCH_ERR, $arrCopyLog);
212                $this->registerUpdateLog($arrCopyLog, $objRet->data);
213                $this->updateMdlTable($objRet->data);
214
215                return;
216            }
217
218            // dtb_module_update_logの更新
219            $objLog->log('* insert dtb_module_update start');
220            $this->registerUpdateLog($arrCopyLog, $objRet->data);
221
222            // dtb_moduleの更新
223            $objLog->log('* insert/update dtb_module start');
224            $this->updateMdlTable($objRet->data);
225
226            // DB更新ファイルの読み込み、実行
227            $objLog->log('* file execute start');
228            $this->fileExecute($objRet->data->product_code);
229
230            // 配信サーバーへ通知
231            $objLog->log('* notify to lockon server start');
232            $objReq = $this->notifyDownload($mode, $objReq->getResponseCookies());
233
234            $objLog->log('* dl commit result:' . serialize($objReq));
235
236            $productData = $objRet->data;
237            $productData->dl_file = '';
238            $objJson->setSUCCESS($productData, 'インストール/アップデートに成功しました。');
239            $objJson->display();
240            $objLog->end();
241
242            return;
243        } else {
244            // 配信サーバー側でエラーを補足
245            echo $body;
246            $objLog->error($objRet->errcode, $objReq);
247
248            return;
249        }
250    }
251
252    public function initParam()
253    {
254        $this->objForm = new SC_FormParam_Ex();
255        $this->objForm->addParam(
256            'product_id', 'product_id', INT_LEN, '', array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK')
257        );
258        $this->objForm->setParam($_POST);
259    }
260
261    /**
262     * dtb_moduleを更新する
263     *
264     * @param object $objRet
265     */
266    public function updateMdlTable($objRet)
267    {
268        $table = 'dtb_module';
269        $where = 'module_id = ?';
270        $objQuery =& SC_Query_Ex::getSingletonInstance();
271
272        $exists = $objQuery->exists($table, $where, array($objRet->product_id));
273        if ($exists) {
274            $arrUpdate = array(
275                'module_code' => $objRet->product_code,
276                'module_name' => $objRet->product_name,
277                'update_date' => 'CURRENT_TIMESTAMP'
278            );
279            $objQuery->update($table, $arrUpdate, $where, array($objRet->product_id));
280        } else {
281            $arrInsert = array(
282                'module_id'   => $objRet->product_id,
283                'module_code' => $objRet->product_code,
284                'module_name' => $objRet->product_name,
285                'auto_update_flg' => '0',
286                'create_date'     => 'CURRENT_TIMESTAMP',
287                'update_date' => 'CURRENT_TIMESTAMP'
288            );
289            $objQuery->insert($table, $arrInsert);
290        }
291    }
292
293    /**
294     * 配信サーバーへダウンロード完了を通知する.
295     *
296     * FIXME エラーコード追加
297     * @param array #arrCookies Cookie配列
298     * @return
299     */
300    public function notifyDownload($mode, $arrCookies)
301    {
302        $arrPOSTParams = array(
303            'eccube_url' => HTTP_URL
304        );
305        $objReq = $this->request($mode . '_commit', $arrPOSTParams, $arrCookies);
306
307        return $objReq;
308    }
309
310    /**
311     * アクセスチェック
312     *
313     * @return boolean
314     */
315    public function isValidAccess($mode)
316    {
317        $objLog = new LC_Upgrade_Helper_Log;
318        switch ($mode) {
319        // モジュールダウンロード
320        case 'download':
321            if ($this->isLoggedInAdminPage() === true) {
322                $objLog->log('* admin login ok');
323
324                return true;
325            }
326            break;
327        // 自動アップロード最新ファイル取得
328        case 'patch_download':
329        // モジュール自動アップロード
330        case 'auto_update':
331            $objForm = new SC_FormParam;
332            $objForm->addParam('public_key', 'public_key', MTEXT_LEN, '', array('EXIST_CHECK', 'ALNUM_CHECK', 'MAX_LENGTH_CHECK'));
333            $objForm->addParam('sha1_key', 'sha1_key', MTEXT_LEN, '', array('EXIST_CHECK', 'ALNUM_CHECK', 'MAX_LENGTH_CHECK'));
334            $objForm->setParam($_POST);
335
336            $objLog->log('* param check start');
337            $arrErr = $objForm->checkError();
338            if ($arrErr) {
339                $objLog->log('* invalid param ' . print_r($arrErr, true));
340
341                return false;
342            }
343
344            $objLog->log('* public_key check start');
345            $public_key = $this->getPublicKey();
346            if (empty($public_key)) {
347                $objLog->log('* public_key not found');
348
349                return false;
350            }
351
352            $sha1_key = $objForm->getValue('sha1_key');
353            $public_key_sha1 = $objForm->getValue('public_key');
354
355            $objLog->log('* ip check start');
356            if ($public_key_sha1 === sha1($public_key . $sha1_key)) {
357                $objLog->log('* auto update login ok');
358
359                return true;
360            }
361            break;
362        default:
363            $objLog->log('* mode invalid ' . $mode);
364
365            return false;
366        }
367
368        return false;
369    }
370
371    public function registerUpdateLog($arrLog, $objRet)
372    {
373        $objQuery =& SC_Query_Ex::getSingletonInstance();
374        $arrInsert = array(
375            'log_id'      => $objQuery->nextVal('dtb_module_update_logs_log_id'),
376            'module_id'   => $objRet->product_id,
377            'buckup_path' => $arrLog['buckup_path'],
378            'error_flg'   => count($arrLog['err']),
379            'error'       => implode("\n", $arrLog['err']),
380            'ok'          => implode("\n", $arrLog['ok']),
381            'update_date' => 'CURRENT_TIMESTAMP',
382            'create_date' => 'CURRENT_TIMESTAMP',
383        );
384        $objQuery->insert('dtb_module_update_logs', $arrInsert);
385    }
386
387    /**
388     * DB更新ファイルの読み込み、実行
389     *
390     * パッチ側でupdate.phpを用意する.
391     * 他の変数・関数とかぶらないよう、
392     * LC_Update_Updater::execute()で処理を実行する.
393     */
394    public function fileExecute($productCode)
395    {
396        $file = DATA_REALDIR . 'downloads/update/' . $productCode . '_update.php';
397        if (file_exists($file)) {
398            @include_once $file;
399            if (class_exists('LC_Update_Updater')) {
400                $update = new LC_Update_Updater;
401                $update->execute();
402            }
403        }
404    }
405}
Note: See TracBrowser for help on using the repository browser.