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

Revision 22567, 13.1 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

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