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

Revision 16582, 10.0 KB checked in by nanasess, 16 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';
26error_reporting(E_ALL);
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    /** SC_Sessionオブジェクト */
39    var $objSession = null;
40    /** Services_Jsonオブジェクト */
41    var $objJson = null;
42    /** HTTP_Requestオブジェクト */
43    var $objReq = null;
44    /** SC_FromParamオブジェクト */
45    var $objForm = null;
46
47    // }}}
48    // {{{ functions
49
50    /**
51     * Page を初期化する.
52     *
53     * @return void
54     */
55    function init() {
56        $this->objSess = new SC_Session();
57        $this->objJson = new Services_Json();
58        $rhis->objReq  = new HTTP_Request();
59        $this->objForm = new SC_FormParam();
60        $this->objForm->addParam(
61            'product_id', 'product_id', INT_LEN, '',
62            array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK')
63        );
64        $this->objForm->setParam($_POST);
65    }
66
67    /**
68     * 使用してません
69     * こんな感じで書けたら楽かな...
70     */
71    function _process() {
72        $result = $this->_try();
73        if ($e = $this->_catch($result)) {
74            GC_Utils::gfPrintLog(sprintf($e->log_format, $e->stacktrace));
75            $this->_throw($e->json);
76            exit;
77        }
78        echo $result;
79    }
80
81    /**
82     * Page のプロセス.
83     *
84     * @return void
85     */
86    function process() {
87        $errFormat = '* error! code:%s / debug:%s';
88
89        GC_Utils::gfPrintLog('###Download Start###');
90
91        // 管理画面ログインチェック
92        GC_Utils::gfPrintLog('* admin auth start');
93        if ($this->objSess->isSuccess() !== SUCCESS) {
94            $arrErr = array(
95                'status'  => OWNERSSTORE_STATUS_ERROR,
96                'errcode' => OWNERSSTORE_ERR_DL_ADMIN_AUTH,
97                'body' => '管理画面にログインしていません'
98            );
99            echo $this->objJson->encode($arrErr);
100            GC_Utils::gfPrintLog(
101                sprintf($errFormat, $arrErr['errcode'], serialize($_SESSION))
102            );
103            exit;
104        }
105
106        // パラメーチェック
107        GC_Utils::gfPrintLog('* post parameter check start');
108        if ($this->objForm->checkError()) {
109            $arrErr = array(
110                'status'  => OWNERSSTORE_STATUS_ERROER,
111                'errcode' => OWNERSSTORE_ERR_DL_POST_PARAM,
112                'body' => '配信サーバとの通信中にエラーが発生しました。エラーコード:' . OWNERSSTORE_ERR_DL_POST_PARAM
113            );
114            echo $this->objJson->encode($arrErr);
115            GC_Utils::gfPrintLog(
116                sprintf($errFormat, $arrErr['errcode'], serialize($_POST))
117            );
118            exit;
119        }
120
121        // TODO CSRF対策が必須
122
123        // ダウンロードリクエストを開始
124        GC_Utils::gfPrintLog('* http request start');
125        $resp = $this->request(
126            'download',
127            array('product_id' => $this->objForm->getValue('product_id'))
128        );
129
130        // リクエストのエラーチェック
131        GC_Utils::gfPrintLog('* http response check start');
132        if (PEAR::isError($resp)) {
133            $arrErr = array(
134                'status'  => OWNERSSTORE_STATUS_ERROR,
135                'errcode' => OWNERSSTORE_ERR_DL_HTTP_REQ,
136                'body' => '配信サーバとの通信中にエラーが発生しました。エラーコード:' . OWNERSSTORE_ERR_DL_HTTP_REQ
137            );
138            echo $this->objJson->encode($arrErr);
139            GC_Utils::gfPrintLog(
140                sprintf($errFormat, $arrErr['errcode'], serialize($resp))
141            );
142            exit;
143        }
144
145        // JSONデータの検証
146        $jsonData = $resp->getResponseBody();
147        $objRet   = $this->objJson->decode($resp->getResponseBody($jsonData));
148        GC_Utils::gfPrintLog('* json data check start');
149        if (empty($objRet)) {
150            $arrErr = array(
151                'status'  => OWNERSSTORE_STATUS_ERROR,
152                'errcode' => OWNERSSTORE_ERR_DL_INVALID_JSON_DATA,
153                'body' => '配信サーバとの通信中にエラーが発生しました。エラーコード:' . OWNERSSTORE_ERR_DL_INVALID_JSON_DATA
154            );
155            echo $this->objJson->encode($arrErr);
156            GC_Utils::gfPrintLog(
157                sprintf($errFormat, $arrErr['errcode'], serialize($resp))
158            );
159            exit;
160        }
161        // ダウンロードデータの保存
162        if ($objRet->status === OWNERSSTORE_STATUS_SUCCESS) {
163            GC_Utils::gfPrintLog('* save file start');
164            $time = time();
165            $dir  = DATA_PATH . 'downloads/tmp/';
166            $filename = $time . '.tar.gz';
167
168            $data = base64_decode($objRet->body);
169
170            if ($fp = fopen($dir . $filename, "w")) {
171                fwrite($fp, $data);
172                fclose($fp);
173            } else {
174                $arrErr = array(
175                    'status'  => OWNERSSTORE_STATUS_ERROR,
176                    'errcode' => OWNERSSTORE_ERR_DL_FILE_WRITE,
177                    'body' => '配信サーバとの通信中にエラーが発生しました。エラーコード:' . OWNERSSTORE_ERR_DL_FILE_WRITE
178                );
179                echo $this->objJson->encode($arrErr);
180                GC_Utils::gfPrintLog(
181                    sprintf($errFormat, $arrErr['errcode'], serialize($dir . $filename))
182                );
183                exit;
184            }
185            // ダウンロードアーカイブを展開する
186            $exract_dir = $dir . $time;
187            if (!@mkdir($exract_dir)) {
188                $arrErr = array(
189                    'status'  => OWNERSSTORE_STATUS_ERROR,
190                    'errcode' => OWNERSSTORE_ERR_DL_MKDIR,
191                    'body' => '配信サーバとの通信中にエラーが発生しました。エラーコード:' . OWNERSSTORE_ERR_DL_MKDIR
192                );
193                echo $this->objJson->encode($arrErr);
194                GC_Utils::gfPrintLog(
195                    sprintf($errFormat, $arrErr['errcode'], serialize($exract_dir))
196                );
197                exit;
198            }
199
200            $tar = new Archive_Tar($dir . $filename);
201            $tar->extract($exract_dir);
202
203            include_once CLASS_PATH . 'batch/SC_Batch_Update.php';
204            $objBatch = new SC_Batch_Update();
205            $arrCopyLog = $objBatch->execute($exract_dir);
206
207            $this->notifyDownload($resp->getResponseCookies(), $objRet->product_data);
208            // テーブルの更新
209            // $this->updateMdlTable($objRet);
210
211            $arrParam = array(
212                'status'  => OWNERSSTORE_STATUS_SUCCESS,
213                'body' => 'インストール/アップデートに成功しました!'
214            );
215            echo $this->objJson->encode($arrParam);
216            GC_Utils::gfPrintLog('* file save ok');
217            exit;
218        } else {
219            echo $jsonData;
220            GC_Utils::gfPrintLog(
221                sprintf($errFormat, $objRet->errcode, serialize(array($resp, $objRet)))
222            );
223            exit;
224        }
225    }
226
227    /**
228     * デストラクタ
229     *
230     * @return void
231     */
232    function destroy() {
233        GC_Utils::gfPrintLog('###Download End###');
234    }
235
236    /**
237     * dtb_moduleを更新する
238     *
239     * @param object $objRet
240     */
241    function updateMdlTable($objRet) {
242        $table = 'dtb_module';
243        $objQuery = new SC_Query;
244
245        $count = $objQuery->count($objRet, 'module_id=?', array($objRet->product_id));
246        if ($count) {
247            $arrUpdate = array();
248            $objQuery->update($table, $arrUpdate);
249        } else {
250            $arrInsert = array();
251            $objQuery->insert($table, $arrInsert);
252        }
253    }
254
255    /**
256     * 配信サーバへダウンロード完了を通知する.
257     *
258     * FIXME エラーコード追加
259     * @param array #arrCookies Cookie配列
260     * @retrun
261     */
262    function notifyDownload($arrCookies) {
263        $objReq = new HTTP_Request();
264        $objReq->setUrl('http://cube-shopaccount/upgrade/index.php');
265        $objReq->setMethod('POST');
266        $objReq->addPostData('mode', 'download_log');
267
268        // Cookie追加
269        foreach ($arrCookies as $cookie) {
270            $objReq->addCookie($cookie['name'], $cookie['value']);
271        }
272
273        $e = $objReq->sendRequest();
274        if (PEAR::isError($e)) {
275            $arrErr = array(
276                'status'  => OWNERSSTORE_STATUS_ERROR,
277                'errcode' => 999,
278                'body'    => '配信サーバとの通信中にエラーが発生しました。エラーコード:' . 999
279            );
280            return $arrErr;
281        }
282
283        if ($objReq->getResponseCode() !== 200) {
284            $arrErr = array(
285                'status'  => OWNERSSTORE_STATUS_ERROR,
286                'errcode' => 999,
287                'body'    => '配信サーバとの通信中にエラーが発生しました。エラーコード:' . 999
288            );
289            return $arrErr;
290        }
291        echo $objReq->getResponseBody();
292        // TODO STATUSチェック
293        return true;
294    }
295}
296?>
Note: See TracBrowser for help on using the repository browser.