| 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 'utils/LC_Utils_Upgrade.php'; |
|---|
| 27 | require_once 'utils/LC_Utils_Upgrade_Log.php'; |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * 自動アップデートを行う. |
|---|
| 31 | * |
|---|
| 32 | * TODO 要リファクタリング |
|---|
| 33 | * |
|---|
| 34 | * @package Page |
|---|
| 35 | * @author LOCKON CO.,LTD. |
|---|
| 36 | * @version $Id$ |
|---|
| 37 | */ |
|---|
| 38 | class LC_Page_Upgrade_AutoUpdate extends LC_Page_Upgrade_Base { |
|---|
| 39 | |
|---|
| 40 | // }}} |
|---|
| 41 | // {{{ functions |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Page を初期化する. |
|---|
| 45 | * |
|---|
| 46 | * @return void |
|---|
| 47 | */ |
|---|
| 48 | function init() { |
|---|
| 49 | $this->objJson = new Services_Json(); |
|---|
| 50 | $this->objLog = new LC_Utils_Upgrade_Log('Auto Update'); |
|---|
| 51 | |
|---|
| 52 | $this->objForm = new SC_FormParam(); |
|---|
| 53 | $this->objForm->addParam('product_id', 'product_id', INT_LEN, '', array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK')); |
|---|
| 54 | $this->objForm->setParam($_POST); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * Page のプロセス. |
|---|
| 59 | * |
|---|
| 60 | * @return void |
|---|
| 61 | */ |
|---|
| 62 | function process() { |
|---|
| 63 | $this->objLog->start(); |
|---|
| 64 | |
|---|
| 65 | // IPチェック |
|---|
| 66 | $this->objLog->log('* ip check start'); |
|---|
| 67 | if (LC_Utils_Upgrade::isValidIP() !== true) { |
|---|
| 68 | $arrErr = array( |
|---|
| 69 | 'status' => OWNERSSTORE_STATUS_ERROR, |
|---|
| 70 | 'errcode' => OWNERSSTORE_ERR_AU_INVALID_IP |
|---|
| 71 | ); |
|---|
| 72 | echo $this->objJson->encode($arrErr); |
|---|
| 73 | $this->objLog->errLog($arrErr['errcode'], $_SERVER['REMOTE_ADDR']); |
|---|
| 74 | exit; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | // パラメーチェック |
|---|
| 78 | $this->objLog->log('* post parameter check start'); |
|---|
| 79 | if ($this->objForm->checkError()) { |
|---|
| 80 | $arrErr = array( |
|---|
| 81 | 'status' => OWNERSSTORE_STATUS_ERROER, |
|---|
| 82 | 'errcode' => OWNERSSTORE_ERR_AU_POST_PARAM |
|---|
| 83 | ); |
|---|
| 84 | echo $this->objJson->encode($arrErr); |
|---|
| 85 | $this->objLog->errLog($arrErr['errcode'], $_POST); |
|---|
| 86 | exit; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | // 自動アップデート設定の判定 |
|---|
| 90 | $this->objLog->log('* auto update settings check start'); |
|---|
| 91 | if ($this->autoUpdateEnable() !== true) { |
|---|
| 92 | $arrErr = array( |
|---|
| 93 | 'status' => OWNERSSTORE_STATUS_ERROER, |
|---|
| 94 | 'errcode' => OWNERSSTORE_ERR_AU_NO_UPDATE, |
|---|
| 95 | ); |
|---|
| 96 | echo $this->objJson->encode($arrErr); |
|---|
| 97 | $this->objLog->errLog($arrErr['errcode']); |
|---|
| 98 | exit; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | // ダウンロードリクエストを開始 |
|---|
| 102 | $this->objLog->log('* http request start'); |
|---|
| 103 | $objReq = LC_Utils_Upgrade::request( |
|---|
| 104 | 'download', |
|---|
| 105 | array('product_id' => $this->objForm->getValue('product_id')) |
|---|
| 106 | ); |
|---|
| 107 | |
|---|
| 108 | // リクエストのエラーチェック |
|---|
| 109 | $this->objLog->log('* http request check start'); |
|---|
| 110 | if (PEAR::isError($objReq)) { |
|---|
| 111 | $arrErr = array( |
|---|
| 112 | 'status' => OWNERSSTORE_STATUS_ERROR, |
|---|
| 113 | 'errcode' => OWNERSSTORE_ERR_AU_HTTP_REQ, |
|---|
| 114 | ); |
|---|
| 115 | echo $this->objJson->encode($arrErr); |
|---|
| 116 | $this->objLog->errLog($arrErr['errcode'], $objReq); |
|---|
| 117 | exit; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | // レスポンスの検証 |
|---|
| 121 | $this->objLog->log('* http response check start'); |
|---|
| 122 | if ($objReq->getResponseCode() !== 200) { |
|---|
| 123 | $arrErr = array( |
|---|
| 124 | 'status' => OWNERSSTORE_STATUS_ERROR, |
|---|
| 125 | 'errcode' => OWNERSSTORE_ERR_AU_HTTP_RESP_CODE, |
|---|
| 126 | ); |
|---|
| 127 | echo $this->objJson->encode($arrErr); |
|---|
| 128 | $this->objLog->errLog($arrErr['errcode'], $objReq); |
|---|
| 129 | exit; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | // JSONデータの検証 |
|---|
| 133 | $body = $objReq->getResponseBody(); |
|---|
| 134 | $objRet = $this->objJson->decode($body); |
|---|
| 135 | |
|---|
| 136 | $this->objLog->log('* json data check start'); |
|---|
| 137 | if (empty($objRet)) { |
|---|
| 138 | $arrErr = array( |
|---|
| 139 | 'status' => OWNERSSTORE_STATUS_ERROR, |
|---|
| 140 | 'errcode' => OWNERSSTORE_ERR_AU_INVALID_JSON_DATA, |
|---|
| 141 | ); |
|---|
| 142 | echo $this->objJson->encode($arrErr); |
|---|
| 143 | $this->objLog->errLog($arrErr['errcode'], $objReq); |
|---|
| 144 | exit; |
|---|
| 145 | } |
|---|
| 146 | // ダウンロードデータの保存 |
|---|
| 147 | if ($objRet->status === OWNERSSTORE_STATUS_SUCCESS) { |
|---|
| 148 | $this->objLog->log('* save file start'); |
|---|
| 149 | $time = time(); |
|---|
| 150 | $dir = DATA_PATH . 'downloads/tmp/'; |
|---|
| 151 | $filename = $time . '.tar.gz'; |
|---|
| 152 | |
|---|
| 153 | $data = base64_decode($objRet->body); |
|---|
| 154 | |
|---|
| 155 | if ($fp = fopen($dir . $filename, "w")) { |
|---|
| 156 | fwrite($fp, $data); |
|---|
| 157 | fclose($fp); |
|---|
| 158 | } else { |
|---|
| 159 | $arrErr = array( |
|---|
| 160 | 'status' => OWNERSSTORE_STATUS_ERROR, |
|---|
| 161 | 'errcode' => OWNERSSTORE_ERR_AU_FILE_WRITE, |
|---|
| 162 | ); |
|---|
| 163 | echo $this->objJson->encode($arrErr); |
|---|
| 164 | $this->objLog->errLog($arrErr['errcode'], $dir . $filename); |
|---|
| 165 | exit; |
|---|
| 166 | } |
|---|
| 167 | // ダウンロードアーカイブを展開する |
|---|
| 168 | $exract_dir = $dir . $time; |
|---|
| 169 | if (!@mkdir($exract_dir)) { |
|---|
| 170 | $arrErr = array( |
|---|
| 171 | 'status' => OWNERSSTORE_STATUS_ERROR, |
|---|
| 172 | 'errcode' => OWNERSSTORE_ERR_AU_MKDIR, |
|---|
| 173 | ); |
|---|
| 174 | echo $this->objJson->encode($arrErr); |
|---|
| 175 | $this->objLog->errLog($arrErr['errcode'], $exract_dir); |
|---|
| 176 | exit; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | $tar = new Archive_Tar($dir . $filename); |
|---|
| 180 | $tar->extract($exract_dir); |
|---|
| 181 | |
|---|
| 182 | include_once CLASS_PATH . 'batch/SC_Batch_Update.php'; |
|---|
| 183 | $objBatch = new SC_Batch_Update(); |
|---|
| 184 | $arrCopyLog = $objBatch->execute($exract_dir); |
|---|
| 185 | |
|---|
| 186 | // テーブルの更新 |
|---|
| 187 | $this->updateMdlTable($objRet->product_data); |
|---|
| 188 | // サーバへ通知 |
|---|
| 189 | $this->notifyDownload($objReq->getResponseCookies()); |
|---|
| 190 | |
|---|
| 191 | echo $this->objJson->encode(array('status' => OWNERSSTORE_STATUS_SUCCESS)); |
|---|
| 192 | $this->objLog->log('* file save ok'); |
|---|
| 193 | exit; |
|---|
| 194 | } else { |
|---|
| 195 | echo $body; |
|---|
| 196 | $this->objLog->errLog($objRet->errcode, array($objRet, $objReq)); |
|---|
| 197 | exit; |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | /** |
|---|
| 202 | * デストラクタ |
|---|
| 203 | * |
|---|
| 204 | * @return void |
|---|
| 205 | */ |
|---|
| 206 | function destroy() { |
|---|
| 207 | $this->objLog->end(); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| 211 | * dtb_moduleを更新する |
|---|
| 212 | * |
|---|
| 213 | * @param object $objRet |
|---|
| 214 | */ |
|---|
| 215 | function updateMdlTable($objRet) { |
|---|
| 216 | $table = 'dtb_module'; |
|---|
| 217 | $where = 'module_id = ?'; |
|---|
| 218 | $objQuery = new SC_Query; |
|---|
| 219 | |
|---|
| 220 | $count = $objQuery->count($table, $where, array($objRet->product_id)); |
|---|
| 221 | if ($count) { |
|---|
| 222 | $arrUpdate = array( |
|---|
| 223 | 'module_name' => $objRet->product_code, |
|---|
| 224 | 'update_date' => 'NOW()' |
|---|
| 225 | ); |
|---|
| 226 | $objQuery->update($table, $arrUpdate ,$where, array($objRet->product_id)); |
|---|
| 227 | } else { |
|---|
| 228 | $arrInsert = array( |
|---|
| 229 | 'module_id' => $objRet->product_id, |
|---|
| 230 | 'module_name' => $objRet->product_code, |
|---|
| 231 | 'auto_update_flg' => '0', |
|---|
| 232 | 'create_date' => 'NOW()', |
|---|
| 233 | 'update_date' => 'NOW()' |
|---|
| 234 | ); |
|---|
| 235 | $objQuery->insert($table, $arrInsert); |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | /** |
|---|
| 240 | * 配信サーバへダウンロード完了を通知する. |
|---|
| 241 | * |
|---|
| 242 | * FIXME エラーコード追加 |
|---|
| 243 | * @param array #arrCookies Cookie配列 |
|---|
| 244 | * @retrun |
|---|
| 245 | */ |
|---|
| 246 | function notifyDownload($arrCookies) { |
|---|
| 247 | $objReq = LC_Utils_Upgrade::request('download_log', array(), $arrCookies); |
|---|
| 248 | |
|---|
| 249 | return true; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | /** |
|---|
| 253 | * 自動アップデートが有効かどうかを判定する. |
|---|
| 254 | * |
|---|
| 255 | * @return boolean |
|---|
| 256 | */ |
|---|
| 257 | function autoUpdateEnable() { |
|---|
| 258 | $product_id = $this->objForm->getValue('product_id'); |
|---|
| 259 | |
|---|
| 260 | $where = 'module_id = ?'; |
|---|
| 261 | $objQuery = new SC_Query(); |
|---|
| 262 | $arrRet = $objQuery->select('auto_update_flg', 'dtb_module', $where, array($product_id)); |
|---|
| 263 | |
|---|
| 264 | if (isset($arrRet[0]['auto_update_flg']) |
|---|
| 265 | && $arrRet[0]['auto_update_flg'] === '1') { |
|---|
| 266 | |
|---|
| 267 | return true; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | return false; |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | ?> |
|---|