[16459] | 1 | <?php |
---|
| 2 | /* |
---|
[16582] | 3 | * This file is part of EC-CUBE |
---|
| 4 | * |
---|
[22206] | 5 | * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved. |
---|
[16459] | 6 | * |
---|
| 7 | * http://www.lockon.co.jp/ |
---|
[16582] | 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. |
---|
[16459] | 22 | */ |
---|
| 23 | |
---|
[19805] | 24 | require_once CLASS_REALDIR . 'pages/upgrade/LC_Page_Upgrade_Base.php'; |
---|
[16623] | 25 | |
---|
[16459] | 26 | /** |
---|
[16623] | 27 | * オーナーズストアからダウンロードデータを取得する. |
---|
[16459] | 28 | * |
---|
| 29 | * TODO 要リファクタリング |
---|
| 30 | * |
---|
| 31 | * @package Page |
---|
| 32 | * @author LOCKON CO.,LTD. |
---|
| 33 | * @version $Id$ |
---|
| 34 | */ |
---|
[22856] | 35 | class LC_Page_Upgrade_Download extends LC_Page_Upgrade_Base |
---|
[22567] | 36 | { |
---|
[16459] | 37 | /** |
---|
| 38 | * Page を初期化する. |
---|
| 39 | * |
---|
| 40 | * @return void |
---|
| 41 | */ |
---|
[23124] | 42 | public function init() |
---|
[22567] | 43 | { |
---|
[16839] | 44 | parent::init(); |
---|
[16464] | 45 | } |
---|
| 46 | |
---|
| 47 | /** |
---|
[16459] | 48 | * Page のプロセス. |
---|
| 49 | * |
---|
| 50 | * @return void |
---|
| 51 | */ |
---|
[23124] | 52 | public function process($mode) |
---|
[22567] | 53 | { |
---|
[16839] | 54 | $objLog = new LC_Upgrade_Helper_Log; |
---|
| 55 | $objLog->start($mode); |
---|
[16459] | 56 | |
---|
[16839] | 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); |
---|
[23124] | 66 | |
---|
[16839] | 67 | return; |
---|
[16459] | 68 | } |
---|
| 69 | |
---|
| 70 | // パラメーチェック |
---|
[16839] | 71 | $this->initParam(); |
---|
| 72 | $objLog->log('* post param check start'); |
---|
[16881] | 73 | $arrErr = $this->objForm->checkError(); |
---|
| 74 | if ($arrErr) { |
---|
[16839] | 75 | $objJson->setError(OSTORE_E_C_INVALID_PARAM); |
---|
| 76 | $objJson->display(); |
---|
| 77 | $objLog->error(OSTORE_E_C_INVALID_PARAM, $_POST); |
---|
[16881] | 78 | $objLog->log('* post param check error ' . print_r($arrErr, true)); |
---|
[23124] | 79 | |
---|
[16839] | 80 | return; |
---|
[16459] | 81 | } |
---|
| 82 | |
---|
[16873] | 83 | $objLog->log('* auto update check start'); |
---|
[16839] | 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(); |
---|
[16873] | 88 | $objLog->error(OSTORE_E_C_AUTOUP_DISABLE, $_POST); |
---|
[23124] | 89 | |
---|
[16839] | 90 | return; |
---|
| 91 | } |
---|
[16459] | 92 | |
---|
[16839] | 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); |
---|
[23124] | 105 | |
---|
[16839] | 106 | return; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | // リクエストを開始 |
---|
| 110 | $objLog->log('* http request start'); |
---|
[16893] | 111 | |
---|
[21441] | 112 | switch ($mode) { |
---|
[21526] | 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; |
---|
[16881] | 129 | } |
---|
[16893] | 130 | |
---|
[16873] | 131 | $objReq = $this->request($mode, $arrPostData); |
---|
[16459] | 132 | |
---|
[16839] | 133 | // リクエストチェック |
---|
| 134 | $objLog->log('* http request check start'); |
---|
[16623] | 135 | if (PEAR::isError($objReq)) { |
---|
[16839] | 136 | $objJson->setError(OSTORE_E_C_HTTP_REQ); |
---|
| 137 | $objJson->display(); |
---|
[16912] | 138 | $objLog->error(OSTORE_E_C_HTTP_REQ, $objReq); |
---|
[23124] | 139 | |
---|
[16839] | 140 | return; |
---|
[16623] | 141 | } |
---|
| 142 | |
---|
[16839] | 143 | // レスポンスチェック |
---|
| 144 | $objLog->log('* http response check start'); |
---|
[23141] | 145 | if ($objReq->getResponseCode() !== 200) { |
---|
[16839] | 146 | $objJson->setError(OSTORE_E_C_HTTP_RESP); |
---|
| 147 | $objJson->display(); |
---|
| 148 | $objLog->error(OSTORE_E_C_HTTP_RESP, $objReq); |
---|
[23124] | 149 | |
---|
[16839] | 150 | return; |
---|
[16459] | 151 | } |
---|
| 152 | |
---|
[23141] | 153 | $body = $objReq->getResponseBody(); |
---|
[16839] | 154 | $objRet = $objJson->decode($body); |
---|
[16623] | 155 | |
---|
[16839] | 156 | // JSONデータのチェック |
---|
[16880] | 157 | $objLog->log('* json data check start'); |
---|
[16464] | 158 | if (empty($objRet)) { |
---|
[16839] | 159 | $objJson->setError(OSTORE_E_C_FAILED_JSON_PARSE); |
---|
| 160 | $objJson->display(); |
---|
| 161 | $objLog->error(OSTORE_E_C_FAILED_JSON_PARSE, $objReq); |
---|
[23124] | 162 | |
---|
[16839] | 163 | return; |
---|
[16464] | 164 | } |
---|
[16839] | 165 | |
---|
[16464] | 166 | // ダウンロードデータの保存 |
---|
[16839] | 167 | if ($objRet->status === OSTORE_STATUS_SUCCESS) { |
---|
| 168 | $objLog->log('* save file start'); |
---|
[16459] | 169 | $time = time(); |
---|
[19805] | 170 | $dir = DATA_REALDIR . 'downloads/tmp/'; |
---|
[16459] | 171 | $filename = $time . '.tar.gz'; |
---|
| 172 | |
---|
[16866] | 173 | $data = base64_decode($objRet->data->dl_file); |
---|
[16459] | 174 | |
---|
[16839] | 175 | $objLog->log("* open ${filename} start"); |
---|
[20538] | 176 | if ($fp = @fopen($dir . $filename, 'w')) { |
---|
[16862] | 177 | @fwrite($fp, $data); |
---|
| 178 | @fclose($fp); |
---|
[16459] | 179 | } else { |
---|
[16862] | 180 | $objJson->setError(OSTORE_E_C_PERMISSION); |
---|
[16839] | 181 | $objJson->display(); |
---|
[16862] | 182 | $objLog->error(OSTORE_E_C_PERMISSION, $dir . $filename); |
---|
[23124] | 183 | |
---|
[16839] | 184 | return; |
---|
[16459] | 185 | } |
---|
[16623] | 186 | |
---|
[16459] | 187 | // ダウンロードアーカイブを展開する |
---|
| 188 | $exract_dir = $dir . $time; |
---|
[16839] | 189 | $objLog->log("* mkdir ${exract_dir} start"); |
---|
[16459] | 190 | if (!@mkdir($exract_dir)) { |
---|
[16862] | 191 | $objJson->setError(OSTORE_E_C_PERMISSION); |
---|
[16839] | 192 | $objJson->display(); |
---|
[16862] | 193 | $objLog->error(OSTORE_E_C_PERMISSION, $exract_dir); |
---|
[23124] | 194 | |
---|
[16839] | 195 | return; |
---|
[16459] | 196 | } |
---|
| 197 | |
---|
[16839] | 198 | $objLog->log("* extract ${dir}${filename} start"); |
---|
[16459] | 199 | $tar = new Archive_Tar($dir . $filename); |
---|
| 200 | $tar->extract($exract_dir); |
---|
| 201 | |
---|
[21514] | 202 | $objLog->log('* copy batch start'); |
---|
[19805] | 203 | @include_once CLASS_REALDIR . 'batch/SC_Batch_Update.php'; |
---|
[16459] | 204 | $objBatch = new SC_Batch_Update(); |
---|
| 205 | $arrCopyLog = $objBatch->execute($exract_dir); |
---|
| 206 | |
---|
[21514] | 207 | $objLog->log('* copy batch check start'); |
---|
[16862] | 208 | if (count($arrCopyLog['err']) > 0) { |
---|
[16893] | 209 | $objJson->setError(OSTORE_E_C_BATCH_ERR); |
---|
[16862] | 210 | $objJson->display(); |
---|
[16893] | 211 | $objLog->error(OSTORE_E_C_BATCH_ERR, $arrCopyLog); |
---|
[16862] | 212 | $this->registerUpdateLog($arrCopyLog, $objRet->data); |
---|
[16893] | 213 | $this->updateMdlTable($objRet->data); |
---|
[23124] | 214 | |
---|
[16862] | 215 | return; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | // dtb_module_update_logの更新 |
---|
[21514] | 219 | $objLog->log('* insert dtb_module_update start'); |
---|
[16862] | 220 | $this->registerUpdateLog($arrCopyLog, $objRet->data); |
---|
| 221 | |
---|
[17193] | 222 | // dtb_moduleの更新 |
---|
[21514] | 223 | $objLog->log('* insert/update dtb_module start'); |
---|
[17193] | 224 | $this->updateMdlTable($objRet->data); |
---|
[16893] | 225 | |
---|
[17193] | 226 | // DB更新ファイルの読み込み、実行 |
---|
[21514] | 227 | $objLog->log('* file execute start'); |
---|
[17193] | 228 | $this->fileExecute($objRet->data->product_code); |
---|
| 229 | |
---|
[20970] | 230 | // 配信サーバーへ通知 |
---|
[21514] | 231 | $objLog->log('* notify to lockon server start'); |
---|
[23141] | 232 | $objReq = $this->notifyDownload($mode, $objReq->getResponseCookies()); |
---|
[16623] | 233 | |
---|
[16862] | 234 | $objLog->log('* dl commit result:' . serialize($objReq)); |
---|
[16840] | 235 | |
---|
[16932] | 236 | $productData = $objRet->data; |
---|
| 237 | $productData->dl_file = ''; |
---|
| 238 | $objJson->setSUCCESS($productData, 'インストール/アップデートに成功しました。'); |
---|
[16839] | 239 | $objJson->display(); |
---|
| 240 | $objLog->end(); |
---|
[23124] | 241 | |
---|
[16839] | 242 | return; |
---|
[16459] | 243 | } else { |
---|
[20970] | 244 | // 配信サーバー側でエラーを補足 |
---|
[16623] | 245 | echo $body; |
---|
[16839] | 246 | $objLog->error($objRet->errcode, $objReq); |
---|
[23124] | 247 | |
---|
[16839] | 248 | return; |
---|
[16459] | 249 | } |
---|
| 250 | } |
---|
| 251 | |
---|
[23124] | 252 | public function initParam() |
---|
[22567] | 253 | { |
---|
[20501] | 254 | $this->objForm = new SC_FormParam_Ex(); |
---|
[16839] | 255 | $this->objForm->addParam( |
---|
| 256 | 'product_id', 'product_id', INT_LEN, '', array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK') |
---|
| 257 | ); |
---|
| 258 | $this->objForm->setParam($_POST); |
---|
| 259 | } |
---|
| 260 | |
---|
[16459] | 261 | /** |
---|
| 262 | * dtb_moduleを更新する |
---|
| 263 | * |
---|
| 264 | * @param object $objRet |
---|
| 265 | */ |
---|
[23124] | 266 | public function updateMdlTable($objRet) |
---|
[22567] | 267 | { |
---|
[16459] | 268 | $table = 'dtb_module'; |
---|
[16623] | 269 | $where = 'module_id = ?'; |
---|
[21750] | 270 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[16459] | 271 | |
---|
[21376] | 272 | $exists = $objQuery->exists($table, $where, array($objRet->product_id)); |
---|
| 273 | if ($exists) { |
---|
[16623] | 274 | $arrUpdate = array( |
---|
[16863] | 275 | 'module_code' => $objRet->product_code, |
---|
| 276 | 'module_name' => $objRet->product_name, |
---|
[21185] | 277 | 'update_date' => 'CURRENT_TIMESTAMP' |
---|
[16623] | 278 | ); |
---|
| 279 | $objQuery->update($table, $arrUpdate ,$where, array($objRet->product_id)); |
---|
[16459] | 280 | } else { |
---|
[16623] | 281 | $arrInsert = array( |
---|
[16863] | 282 | 'module_id' => $objRet->product_id, |
---|
| 283 | 'module_code' => $objRet->product_code, |
---|
| 284 | 'module_name' => $objRet->product_name, |
---|
[16623] | 285 | 'auto_update_flg' => '0', |
---|
[21185] | 286 | 'create_date' => 'CURRENT_TIMESTAMP', |
---|
| 287 | 'update_date' => 'CURRENT_TIMESTAMP' |
---|
[16623] | 288 | ); |
---|
[16459] | 289 | $objQuery->insert($table, $arrInsert); |
---|
| 290 | } |
---|
| 291 | } |
---|
[16483] | 292 | |
---|
| 293 | /** |
---|
[20970] | 294 | * 配信サーバーへダウンロード完了を通知する. |
---|
[16483] | 295 | * |
---|
[16485] | 296 | * FIXME エラーコード追加 |
---|
[16483] | 297 | * @param array #arrCookies Cookie配列 |
---|
[18639] | 298 | * @return |
---|
[16483] | 299 | */ |
---|
[23124] | 300 | public function notifyDownload($mode, $arrCookies) |
---|
[22567] | 301 | { |
---|
[16873] | 302 | $arrPOSTParams = array( |
---|
[19802] | 303 | 'eccube_url' => HTTP_URL |
---|
[16873] | 304 | ); |
---|
| 305 | $objReq = $this->request($mode . '_commit', $arrPOSTParams, $arrCookies); |
---|
[22856] | 306 | |
---|
[16840] | 307 | return $objReq; |
---|
[16483] | 308 | } |
---|
[16839] | 309 | |
---|
| 310 | /** |
---|
| 311 | * アクセスチェック |
---|
| 312 | * |
---|
| 313 | * @return boolean |
---|
| 314 | */ |
---|
[23124] | 315 | public function isValidAccess($mode) |
---|
[22567] | 316 | { |
---|
[16839] | 317 | $objLog = new LC_Upgrade_Helper_Log; |
---|
| 318 | switch ($mode) { |
---|
[16879] | 319 | // モジュールダウンロード |
---|
[16839] | 320 | case 'download': |
---|
| 321 | if ($this->isLoggedInAdminPage() === true) { |
---|
| 322 | $objLog->log('* admin login ok'); |
---|
[23124] | 323 | |
---|
[16839] | 324 | return true; |
---|
| 325 | } |
---|
| 326 | break; |
---|
[16879] | 327 | // 自動アップロード最新ファイル取得 |
---|
| 328 | case 'patch_download': |
---|
| 329 | // モジュール自動アップロード |
---|
[16839] | 330 | case 'auto_update': |
---|
| 331 | $objForm = new SC_FormParam; |
---|
| 332 | $objForm->addParam('public_key', 'public_key', MTEXT_LEN, '', array('EXIST_CHECK', 'ALNUM_CHECK', 'MAX_LENGTH_CHECK')); |
---|
| 333 | $objForm->addParam('sha1_key', 'sha1_key', MTEXT_LEN, '', array('EXIST_CHECK', 'ALNUM_CHECK', 'MAX_LENGTH_CHECK')); |
---|
| 334 | $objForm->setParam($_POST); |
---|
| 335 | |
---|
[16872] | 336 | $objLog->log('* param check start'); |
---|
[16879] | 337 | $arrErr = $objForm->checkError(); |
---|
| 338 | if ($arrErr) { |
---|
[16893] | 339 | $objLog->log('* invalid param ' . print_r($arrErr, true)); |
---|
[23124] | 340 | |
---|
[16839] | 341 | return false; |
---|
| 342 | } |
---|
| 343 | |
---|
[16872] | 344 | $objLog->log('* public_key check start'); |
---|
[16839] | 345 | $public_key = $this->getPublicKey(); |
---|
| 346 | if (empty($public_key)) { |
---|
| 347 | $objLog->log('* public_key not found'); |
---|
[23124] | 348 | |
---|
[16839] | 349 | return false; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | $sha1_key = $objForm->getValue('sha1_key'); |
---|
| 353 | $public_key_sha1 = $objForm->getValue('public_key'); |
---|
| 354 | |
---|
[16872] | 355 | $objLog->log('* ip check start'); |
---|
[23475] | 356 | if ($public_key_sha1 === sha1($public_key . $sha1_key)) { |
---|
[16839] | 357 | $objLog->log('* auto update login ok'); |
---|
[23124] | 358 | |
---|
[16839] | 359 | return true; |
---|
| 360 | } |
---|
| 361 | break; |
---|
| 362 | default: |
---|
| 363 | $objLog->log('* mode invalid ' . $mode); |
---|
[23124] | 364 | |
---|
[16839] | 365 | return false; |
---|
| 366 | } |
---|
[22856] | 367 | |
---|
[16839] | 368 | return false; |
---|
| 369 | } |
---|
[16862] | 370 | |
---|
[23124] | 371 | public function registerUpdateLog($arrLog, $objRet) |
---|
[22567] | 372 | { |
---|
[21750] | 373 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[16862] | 374 | $arrInsert = array( |
---|
[21527] | 375 | 'log_id' => $objQuery->nextVal('dtb_module_update_logs_log_id'), |
---|
[16862] | 376 | 'module_id' => $objRet->product_id, |
---|
| 377 | 'buckup_path' => $arrLog['buckup_path'], |
---|
| 378 | 'error_flg' => count($arrLog['err']), |
---|
| 379 | 'error' => implode("\n", $arrLog['err']), |
---|
| 380 | 'ok' => implode("\n", $arrLog['ok']), |
---|
[21185] | 381 | 'update_date' => 'CURRENT_TIMESTAMP', |
---|
[21527] | 382 | 'create_date' => 'CURRENT_TIMESTAMP', |
---|
[16862] | 383 | ); |
---|
| 384 | $objQuery->insert('dtb_module_update_logs', $arrInsert); |
---|
| 385 | } |
---|
[17193] | 386 | |
---|
| 387 | /** |
---|
| 388 | * DB更新ファイルの読み込み、実行 |
---|
| 389 | * |
---|
| 390 | * パッチ側でupdate.phpを用意する. |
---|
| 391 | * 他の変数・関数とかぶらないよう、 |
---|
| 392 | * LC_Update_Updater::execute()で処理を実行する. |
---|
| 393 | */ |
---|
[23124] | 394 | public function fileExecute($productCode) |
---|
[22567] | 395 | { |
---|
[19805] | 396 | $file = DATA_REALDIR . 'downloads/update/' . $productCode . '_update.php'; |
---|
[17193] | 397 | if (file_exists($file)) { |
---|
| 398 | @include_once $file; |
---|
| 399 | if (class_exists('LC_Update_Updater')) { |
---|
| 400 | $update = new LC_Update_Updater; |
---|
| 401 | $update->execute(); |
---|
| 402 | } |
---|
| 403 | } |
---|
| 404 | } |
---|
[16459] | 405 | } |
---|