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