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

Revision 23133, 13.0 KB checked in by yomoro, 11 years ago (diff)

#2275 PEAR更新 r23125 の対応でエラーが出ていたため、LC_Page_Upgrade_Download.phpも修正。(HTTP_Request2に対応)

  • 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        $response = $objReq->send();
133
134        // リクエストチェック
135        $objLog->log('* http request check start');
136        if (PEAR::isError($objReq)) {
137            $objJson->setError(OSTORE_E_C_HTTP_REQ);
138            $objJson->display();
139            $objLog->error(OSTORE_E_C_HTTP_REQ, $objReq);
140
141            return;
142        }
143
144        // レスポンスチェック
145        $objLog->log('* http response check start');
146        if ($response->getStatus() !== 200) {
147            $objJson->setError(OSTORE_E_C_HTTP_RESP);
148            $objJson->display();
149            $objLog->error(OSTORE_E_C_HTTP_RESP, $objReq);
150
151            return;
152        }
153
154        $body = $response->getBody();
155        $objRet = $objJson->decode($body);
156
157        // JSONデータのチェック
158        $objLog->log('* json data check start');
159        if (empty($objRet)) {
160            $objJson->setError(OSTORE_E_C_FAILED_JSON_PARSE);
161            $objJson->display();
162            $objLog->error(OSTORE_E_C_FAILED_JSON_PARSE, $objReq);
163
164            return;
165        }
166
167        // ダウンロードデータの保存
168        if ($objRet->status === OSTORE_STATUS_SUCCESS) {
169            $objLog->log('* save file start');
170            $time = time();
171            $dir  = DATA_REALDIR . 'downloads/tmp/';
172            $filename = $time . '.tar.gz';
173
174            $data = base64_decode($objRet->data->dl_file);
175
176            $objLog->log("* open ${filename} start");
177            if ($fp = @fopen($dir . $filename, 'w')) {
178                @fwrite($fp, $data);
179                @fclose($fp);
180            } else {
181                $objJson->setError(OSTORE_E_C_PERMISSION);
182                $objJson->display();
183                $objLog->error(OSTORE_E_C_PERMISSION, $dir . $filename);
184
185                return;
186            }
187
188            // ダウンロードアーカイブを展開する
189            $exract_dir = $dir . $time;
190            $objLog->log("* mkdir ${exract_dir} start");
191            if (!@mkdir($exract_dir)) {
192                $objJson->setError(OSTORE_E_C_PERMISSION);
193                $objJson->display();
194                $objLog->error(OSTORE_E_C_PERMISSION, $exract_dir);
195
196                return;
197            }
198
199            $objLog->log("* extract ${dir}${filename} start");
200            $tar = new Archive_Tar($dir . $filename);
201            $tar->extract($exract_dir);
202
203            $objLog->log('* copy batch start');
204            @include_once CLASS_REALDIR . 'batch/SC_Batch_Update.php';
205            $objBatch = new SC_Batch_Update();
206            $arrCopyLog = $objBatch->execute($exract_dir);
207
208            $objLog->log('* copy batch check start');
209            if (count($arrCopyLog['err']) > 0) {
210                $objJson->setError(OSTORE_E_C_BATCH_ERR);
211                $objJson->display();
212                $objLog->error(OSTORE_E_C_BATCH_ERR, $arrCopyLog);
213                $this->registerUpdateLog($arrCopyLog, $objRet->data);
214                $this->updateMdlTable($objRet->data);
215
216                return;
217            }
218
219            // dtb_module_update_logの更新
220            $objLog->log('* insert dtb_module_update start');
221            $this->registerUpdateLog($arrCopyLog, $objRet->data);
222
223            // dtb_moduleの更新
224            $objLog->log('* insert/update dtb_module start');
225            $this->updateMdlTable($objRet->data);
226
227            // DB更新ファイルの読み込み、実行
228            $objLog->log('* file execute start');
229            $this->fileExecute($objRet->data->product_code);
230
231            // 配信サーバーへ通知
232            $objLog->log('* notify to lockon server start');
233            $objReq = $this->notifyDownload($mode, $response->getCookies());
234
235            $objLog->log('* dl commit result:' . serialize($objReq));
236
237            $productData = $objRet->data;
238            $productData->dl_file = '';
239            $objJson->setSUCCESS($productData, 'インストール/アップデートに成功しました。');
240            $objJson->display();
241            $objLog->end();
242
243            return;
244        } else {
245            // 配信サーバー側でエラーを補足
246            echo $body;
247            $objLog->error($objRet->errcode, $objReq);
248
249            return;
250        }
251    }
252
253    public function initParam()
254    {
255        $this->objForm = new SC_FormParam_Ex();
256        $this->objForm->addParam(
257            'product_id', 'product_id', INT_LEN, '', array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK')
258        );
259        $this->objForm->setParam($_POST);
260    }
261
262    /**
263     * dtb_moduleを更新する
264     *
265     * @param object $objRet
266     */
267    public function updateMdlTable($objRet)
268    {
269        $table = 'dtb_module';
270        $where = 'module_id = ?';
271        $objQuery =& SC_Query_Ex::getSingletonInstance();
272
273        $exists = $objQuery->exists($table, $where, array($objRet->product_id));
274        if ($exists) {
275            $arrUpdate = array(
276                'module_code' => $objRet->product_code,
277                'module_name' => $objRet->product_name,
278                'update_date' => 'CURRENT_TIMESTAMP'
279            );
280            $objQuery->update($table, $arrUpdate ,$where, array($objRet->product_id));
281        } else {
282            $arrInsert = array(
283                'module_id'   => $objRet->product_id,
284                'module_code' => $objRet->product_code,
285                'module_name' => $objRet->product_name,
286                'auto_update_flg' => '0',
287                'create_date'     => 'CURRENT_TIMESTAMP',
288                'update_date' => 'CURRENT_TIMESTAMP'
289            );
290            $objQuery->insert($table, $arrInsert);
291        }
292    }
293
294    /**
295     * 配信サーバーへダウンロード完了を通知する.
296     *
297     * FIXME エラーコード追加
298     * @param array #arrCookies Cookie配列
299     * @return
300     */
301    public function notifyDownload($mode, $arrCookies)
302    {
303        $arrPOSTParams = array(
304            'eccube_url' => HTTP_URL
305        );
306        $objReq = $this->request($mode . '_commit', $arrPOSTParams, $arrCookies);
307
308        return $objReq;
309    }
310
311    /**
312     * アクセスチェック
313     *
314     * @return boolean
315     */
316    public function isValidAccess($mode)
317    {
318        $objLog = new LC_Upgrade_Helper_Log;
319        switch ($mode) {
320        // モジュールダウンロード
321        case 'download':
322            if ($this->isLoggedInAdminPage() === true) {
323                $objLog->log('* admin login ok');
324
325                return true;
326            }
327            break;
328        // 自動アップロード最新ファイル取得
329        case 'patch_download':
330        // モジュール自動アップロード
331        case 'auto_update':
332            $objForm = new SC_FormParam;
333            $objForm->addParam('public_key', 'public_key', MTEXT_LEN, '', array('EXIST_CHECK', 'ALNUM_CHECK', 'MAX_LENGTH_CHECK'));
334            $objForm->addParam('sha1_key', 'sha1_key', MTEXT_LEN, '', array('EXIST_CHECK', 'ALNUM_CHECK', 'MAX_LENGTH_CHECK'));
335            $objForm->setParam($_POST);
336
337            $objLog->log('* param check start');
338            $arrErr = $objForm->checkError();
339            if ($arrErr) {
340                $objLog->log('* invalid param ' . print_r($arrErr, true));
341
342                return false;
343            }
344
345            $objLog->log('* public_key check start');
346            $public_key = $this->getPublicKey();
347            if (empty($public_key)) {
348                $objLog->log('* public_key not found');
349
350                return false;
351            }
352
353            $sha1_key = $objForm->getValue('sha1_key');
354            $public_key_sha1 = $objForm->getValue('public_key');
355
356            $objLog->log('* ip check start');
357            if ($this->isValidIP()
358            && $public_key_sha1 === sha1($public_key . $sha1_key)) {
359                $objLog->log('* auto update login ok');
360
361                return true;
362            }
363            break;
364        default:
365            $objLog->log('* mode invalid ' . $mode);
366
367            return false;
368        }
369
370        return false;
371    }
372
373    public function registerUpdateLog($arrLog, $objRet)
374    {
375        $objQuery =& SC_Query_Ex::getSingletonInstance();
376        $arrInsert = array(
377            'log_id'      => $objQuery->nextVal('dtb_module_update_logs_log_id'),
378            'module_id'   => $objRet->product_id,
379            'buckup_path' => $arrLog['buckup_path'],
380            'error_flg'   => count($arrLog['err']),
381            'error'       => implode("\n", $arrLog['err']),
382            'ok'          => implode("\n", $arrLog['ok']),
383            'update_date' => 'CURRENT_TIMESTAMP',
384            'create_date' => 'CURRENT_TIMESTAMP',
385        );
386        $objQuery->insert('dtb_module_update_logs', $arrInsert);
387    }
388
389    /**
390     * DB更新ファイルの読み込み、実行
391     *
392     * パッチ側でupdate.phpを用意する.
393     * 他の変数・関数とかぶらないよう、
394     * LC_Update_Updater::execute()で処理を実行する.
395     */
396    public function fileExecute($productCode)
397    {
398        $file = DATA_REALDIR . 'downloads/update/' . $productCode . '_update.php';
399        if (file_exists($file)) {
400            @include_once $file;
401            if (class_exists('LC_Update_Updater')) {
402                $update = new LC_Update_Updater;
403                $update->execute();
404            }
405        }
406    }
407}
Note: See TracBrowser for help on using the repository browser.