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