Changeset 16624


Ignore:
Timestamp:
2007/10/30 20:56:00 (16 years ago)
Author:
adachi
Message:

リファクタリング

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/feature-module-update/data/class/pages/upgrade/LC_Page_Upgrade_AutoUpdate.php

    r16582 r16624  
    2424// {{{ requires 
    2525require_once CLASS_PATH . 'pages/upgrade/LC_Page_Upgrade_Base.php'; 
     26require_once 'utils/LC_Utils_Upgrade.php'; 
     27require_once 'utils/LC_Utils_Upgrade_Log.php'; 
    2628 
    2729/** 
     
    3638class LC_Page_Upgrade_AutoUpdate extends LC_Page_Upgrade_Base { 
    3739 
    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  
    4740    // }}} 
    4841    // {{{ functions 
     
    5548    function init() { 
    5649        $this->objJson = new Services_Json(); 
    57         $rhis->objReq  = new HTTP_Request(); 
     50        $this->objLog  = new LC_Utils_Upgrade_Log('Auto Update'); 
     51 
    5852        $this->objForm = new SC_FormParam(); 
    5953        $this->objForm->addParam('product_id', 'product_id', INT_LEN, '', array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK')); 
     
    6761     */ 
    6862    function process() { 
    69         $errFormat = '* error! code:%s / debug:%s'; 
    70  
    71         GC_Utils::gfPrintLog('###Auto Update Start###'); 
     63        $this->objLog->start(); 
    7264 
    7365        // IPチェック 
    74         GC_Utils::gfPrintLog('* ip check start'); 
    75         if ($this->isValidIP() !== true) { 
    76             $arrErr = array( 
    77                 'status'  => OWNERSSTORE_STATUS_ERROR, 
    78                 'errcode' => '' 
    79             ); 
    80             echo $this->objJson->encode($arrErr); 
    81             GC_Utils::gfPrintLog( 
    82                 sprintf($errFormat, $arrErr['errcode'], '') 
    83             ); 
     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']); 
    8474            exit; 
    8575        } 
    8676 
    8777        // パラメーチェック 
    88         GC_Utils::gfPrintLog('* post parameter check start'); 
     78        $this->objLog->log('* post parameter check start'); 
    8979        if ($this->objForm->checkError()) { 
    9080            $arrErr = array( 
    9181                'status'  => OWNERSSTORE_STATUS_ERROER, 
    92                 'errcode' => '', 
    93             ); 
    94             echo $this->objJson->encode($arrErr); 
    95             GC_Utils::gfPrintLog( 
    96                 sprintf($errFormat, $arrErr['errcode'], serialize($_POST)) 
    97             ); 
     82                'errcode' => OWNERSSTORE_ERR_AU_POST_PARAM 
     83            ); 
     84            echo $this->objJson->encode($arrErr); 
     85            $this->objLog->errLog($arrErr['errcode'], $_POST); 
    9886            exit; 
    9987        } 
    10088 
    10189        // 自動アップデート設定の判定 
    102         GC_Utils::gfPrintLog('* auto update settings check start'); 
     90        $this->objLog->log('* auto update settings check start'); 
    10391        if ($this->autoUpdateEnable() !== true) { 
    10492            $arrErr = array( 
    10593                'status'  => OWNERSSTORE_STATUS_ERROER, 
    106                 'errcode' => '', 
    107             ); 
    108             echo $this->objJson->encode($arrErr); 
    109             GC_Utils::gfPrintLog( 
    110                 sprintf($errFormat, $arrErr['errcode'], serialize($product_id)) 
    111             ); 
     94                'errcode' => OWNERSSTORE_ERR_AU_NO_UPDATE, 
     95            ); 
     96            echo $this->objJson->encode($arrErr); 
     97            $this->objLog->errLog($arrErr['errcode']); 
    11298            exit; 
    11399        } 
    114100 
    115101        // ダウンロードリクエストを開始 
    116         GC_Utils::gfPrintLog('* http request start'); 
    117         $resp = $this->request( 
     102        $this->objLog->log('* http request start'); 
     103        $objReq = LC_Utils_Upgrade::request( 
    118104            'download', 
    119105            array('product_id' => $this->objForm->getValue('product_id')) 
     
    121107 
    122108        // リクエストのエラーチェック 
    123         GC_Utils::gfPrintLog('* http response check start'); 
    124         if (PEAR::isError($resp)) { 
    125             $arrErr = array( 
    126                 'status'  => OWNERSSTORE_STATUS_ERROR, 
    127                 'errcode' => '', 
    128             ); 
    129             echo $this->objJson->encode($arrErr); 
    130             GC_Utils::gfPrintLog( 
    131                 sprintf($errFormat, $arrErr['errcode'], serialize($resp)) 
    132             ); 
     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); 
    133129            exit; 
    134130        } 
    135131 
    136132        // JSONデータの検証 
    137         $jsonData = $resp->getResponseBody(); 
    138         $objRet   = $this->objJson->decode($resp->getResponseBody($jsonData)); 
    139         GC_Utils::gfPrintLog('* json data check start'); 
     133        $body = $objReq->getResponseBody(); 
     134        $objRet = $this->objJson->decode($body); 
     135 
     136        $this->objLog->log('* json data check start'); 
    140137        if (empty($objRet)) { 
    141138            $arrErr = array( 
    142139                'status'  => OWNERSSTORE_STATUS_ERROR, 
    143                 'errcode' => '', 
    144             ); 
    145             echo $this->objJson->encode($arrErr); 
    146             GC_Utils::gfPrintLog( 
    147                 sprintf($errFormat, $arrErr['errcode'], serialize($resp)) 
    148             ); 
     140                'errcode' => OWNERSSTORE_ERR_AU_INVALID_JSON_DATA, 
     141            ); 
     142            echo $this->objJson->encode($arrErr); 
     143            $this->objLog->errLog($arrErr['errcode'], $objReq); 
    149144            exit; 
    150145        } 
    151146        // ダウンロードデータの保存 
    152147        if ($objRet->status === OWNERSSTORE_STATUS_SUCCESS) { 
    153             GC_Utils::gfPrintLog('* save file start'); 
     148            $this->objLog->log('* save file start'); 
    154149            $time = time(); 
    155150            $dir  = DATA_PATH . 'downloads/tmp/'; 
     
    164159                $arrErr = array( 
    165160                    'status'  => OWNERSSTORE_STATUS_ERROR, 
    166                     'errcode' => '', 
     161                    'errcode' => OWNERSSTORE_ERR_AU_FILE_WRITE, 
    167162                ); 
    168163                echo $this->objJson->encode($arrErr); 
    169                 GC_Utils::gfPrintLog( 
    170                     sprintf($errFormat, $arrErr['errcode'], serialize($dir . $filename)) 
    171                 ); 
     164                $this->objLog->errLog($arrErr['errcode'], $dir . $filename); 
    172165                exit; 
    173166            } 
     
    177170                $arrErr = array( 
    178171                    'status'  => OWNERSSTORE_STATUS_ERROR, 
    179                     'errcode' => '', 
     172                    'errcode' => OWNERSSTORE_ERR_AU_MKDIR, 
    180173                ); 
    181174                echo $this->objJson->encode($arrErr); 
    182                 GC_Utils::gfPrintLog( 
    183                     sprintf($errFormat, $arrErr['errcode'], serialize($exract_dir)) 
    184                 ); 
     175                $this->objLog->errLog($arrErr['errcode'], $exract_dir); 
    185176                exit; 
    186177            } 
     
    193184            $arrCopyLog = $objBatch->execute($exract_dir); 
    194185 
    195             $this->notifyDownload($resp->getResponseCookies(), $objRet->product_data); 
    196186            // テーブルの更新 
    197             // $this->updateMdlTable($objRet); 
     187            $this->updateMdlTable($objRet->product_data); 
     188            // サーバへ通知 
     189            $this->notifyDownload($objReq->getResponseCookies()); 
    198190 
    199191            echo $this->objJson->encode(array('status'  => OWNERSSTORE_STATUS_SUCCESS)); 
    200             GC_Utils::gfPrintLog('* file save ok'); 
     192            $this->objLog->log('* file save ok'); 
    201193            exit; 
    202194        } else { 
    203             echo $jsonData; 
    204             GC_Utils::gfPrintLog( 
    205                 sprintf($errFormat, $objRet->errcode, serialize(array($resp, $objRet))) 
    206             ); 
     195            echo $body; 
     196            $this->objLog->errLog($objRet->errcode, array($objRet, $objReq)); 
    207197            exit; 
    208198        } 
     
    215205     */ 
    216206    function destroy() { 
    217         GC_Utils::gfPrintLog('###Auto Update End###'); 
     207        $this->objLog->end(); 
    218208    } 
    219209 
     
    225215    function updateMdlTable($objRet) { 
    226216        $table = 'dtb_module'; 
     217        $where = 'module_id = ?'; 
    227218        $objQuery = new SC_Query; 
    228219 
    229         $count = $objQuery->count($objRet, 'module_id=?', array($objRet->product_id)); 
     220        $count = $objQuery->count($table, $where, array($objRet->product_id)); 
    230221        if ($count) { 
    231             $arrUpdate = array(); 
    232             $objQuery->update($table, $arrUpdate); 
     222            $arrUpdate = array( 
     223                'module_name' => $objRet->name, 
     224                'update_date' => 'NOW()' 
     225            ); 
     226            $objQuery->update($table, $arrUpdate ,$where, array($objRet->product_id)); 
    233227        } else { 
    234             $arrInsert = array(); 
     228            $arrInsert = array( 
     229                'module_id' => $objRet->product_id, 
     230                'module_name' => $objRet->name, 
     231                'auto_update_flg' => '0', 
     232                'create_date'     => 'NOW()', 
     233                'update_date' => 'NOW()' 
     234            ); 
    235235            $objQuery->insert($table, $arrInsert); 
    236236        } 
     
    245245     */ 
    246246    function notifyDownload($arrCookies) { 
    247         $objReq = new HTTP_Request(); 
    248         $objReq->setUrl('http://cube-shopaccount/upgrade/index.php'); 
    249         $objReq->setMethod('POST'); 
    250         $objReq->addPostData('mode', 'download_log'); 
    251  
    252         // Cookie追加 
    253         foreach ($arrCookies as $cookie) { 
    254             $objReq->addCookie($cookie['name'], $cookie['value']); 
    255         } 
    256  
    257         $e = $objReq->sendRequest(); 
    258     } 
    259  
    260     /** 
    261      * ロックオンからのアクセスかどうかを確認する. 
    262      * 
    263      * @return boolesan 
    264      */ 
    265     function isValidIP() { 
    266         if (isset($_SERVER['REMOTE_ADDR']) 
    267         && $_SERVER['REMOTE_ADDR'] == '127.0.0.1') { 
    268  
    269             return true; 
    270         } 
    271  
    272         return false; 
     247        $objReq = LC_Utils_Upgrade::request('download_log', array(), $arrCookies); 
     248 
     249        return true; 
    273250    } 
    274251 
     
    281258        $product_id = $this->objForm->getValue('product_id'); 
    282259 
    283         $where = 'product_id = ?'; 
     260        $where = 'module_id = ?'; 
    284261        $objQuery = new SC_Query(); 
    285262        $arrRet = $objQuery->select('auto_update_flg', 'dtb_module', $where, array($product_id)); 
Note: See TracChangeset for help on using the changeset viewer.