oxfilechecker.php

Go to the documentation of this file.
00001 <?php
00002 
00009 class oxFileChecker
00010 {
00011 
00017     protected $_blError = false;
00018 
00024     protected $_sErrorMessage = null;
00025 
00031     public $_sWebServiceUrl = 'http://oxchkversion.oxid-esales.com/webService.php';
00032 
00038     protected $_oCurlHandler = null;
00039 
00045     protected $_sEdition = "";
00046 
00052     protected $_sVersion = "";
00053 
00059     protected $_sRevision = "";
00060 
00066     protected $_sBaseDirectory = '';
00067 
00068 
00074     protected $_blListAllFiles = false;
00075 
00081     public function setBaseDirectory($sDir)
00082     {
00083         if (!empty($sDir)) {
00084             $this->_sBaseDirectory = $sDir;
00085         }
00086     }
00087 
00093     public function getBaseDirectory()
00094     {
00095         return $this->_sBaseDirectory;
00096     }
00097 
00103     public function setVersion($sVersion)
00104     {
00105         if (!empty($sVersion)) {
00106             $this->_sVersion = $sVersion;
00107         }
00108     }
00109 
00115     public function getVersion()
00116     {
00117         return $this->_sVersion;
00118     }
00119 
00125     public function setEdition($sEdition)
00126     {
00127         if (!empty($sEdition)) {
00128             $this->_sEdition = $sEdition;
00129         }
00130     }
00131 
00137     public function getEdition()
00138     {
00139         return $this->_sEdition;
00140     }
00141 
00147     public function setRevision($sRevision)
00148     {
00149         if (!empty($sRevision)) {
00150             $this->_sRevision = $sRevision;
00151         }
00152     }
00153 
00159     public function getRevision()
00160     {
00161         return $this->_sRevision;
00162     }
00163 
00169     public function setWebServiceUrl($sUrl)
00170     {
00171         if (!empty($sUrl)) {
00172             $this->_sWebServiceUrl = $sUrl;
00173         }
00174     }
00175 
00181     public function getWebServiceUrl()
00182     {
00183         return $this->_sWebServiceUrl;
00184     }
00185 
00191     public function hasError()
00192     {
00193         return $this->_blError;
00194     }
00195 
00201     public function getErrorMessage()
00202     {
00203         return $this->_sErrorMessage;
00204     }
00205 
00211     public function init()
00212     {
00213         $this->_oCurlHandler = oxNew("oxCurl");
00214 
00215         if (!$this->checkSystemRequirements()) {
00216             $this->_blError = true;
00217             $this->_sErrorMessage .= "Error: requirements are not met.";
00218 
00219             return false;
00220         }
00221 
00222         return true;
00223     }
00224 
00225 
00231     public function checkSystemRequirements()
00232     {
00233         return $this->_isWebServiceOnline() && $this->_isShopVersionIsKnown();
00234     }
00235 
00241     protected function _isWebServiceOnline()
00242     {
00243         $oXML = null;
00244         $aParams = array(
00245             'job' => 'ping',
00246         );
00247 
00248         $this->_oCurlHandler->setUrl($this->_sWebServiceUrl);
00249         $this->_oCurlHandler->setMethod("GET");
00250         $this->_oCurlHandler->setOption("CURLOPT_CONNECTTIMEOUT", 30);
00251         $this->_oCurlHandler->setParameters($aParams);
00252         $sXML = $this->_oCurlHandler->execute();
00253 
00254         if (empty($sXML)) {
00255             $this->_blError = true;
00256             $this->_sErrorMessage = oxRegistry::getLang()->translateString('OXDIAG_ERRORMESSAGEWEBSERVICEISNOTREACHABLE');
00257         }
00258 
00259         try {
00260             $oXML = new SimpleXMLElement($sXML);
00261         } catch (Exception $ex) {
00262             $this->_blError = true;
00263             $this->_sErrorMessage .= oxRegistry::getLang()->translateString('OXDIAG_ERRORMESSAGEWEBSERVICERETURNEDNOXML');
00264         }
00265 
00266         if (!is_object($oXML)) {
00267             $this->_blError = true;
00268             $this->_sErrorMessage .= oxRegistry::getLang()->translateString('OXDIAG_ERRORMESSAGEVERSIONDOESNOTEXIST');
00269         }
00270 
00271         return !$this->_blError;
00272     }
00273 
00274 
00280     protected function _isShopVersionIsKnown()
00281     {
00282         $aParams = array(
00283             'job' => 'existsversion',
00284             'ver' => $this->getVersion(),
00285             'rev' => $this->getRevision(),
00286             'edi' => $this->getEdition(),
00287         );
00288 
00289         $sURL = $this->_sWebServiceUrl . "?" . http_build_query($aParams);
00290 
00291         if ($sXML = @file_get_contents($sURL)) {
00292             $oXML = new SimpleXMLElement($sXML);
00293             if (is_object($oXML)) {
00294                 if ($oXML->exists == 1) {
00295                     return true;
00296                 }
00297             }
00298         }
00299 
00300         $this->_blError = true;
00301         $sError = sprintf(
00302             oxRegistry::getLang()->translateString('OXDIAG_ERRORMESSAGEVERSIONDOESNOTEXIST'),
00303             $this->getEdition(), $this->getVersion(), $this->getRevision()
00304         );
00305 
00306         $this->_sErrorMessage .= $sError;
00307 
00308         return false;
00309     }
00310 
00319     public function checkFile($sFile)
00320     {
00321         $aResult = array();
00322 
00323         if ($this->_oCurlHandler == null) {
00324             return $aResult;
00325         }
00326 
00327         if (!file_exists($this->_sBaseDirectory . $sFile)) {
00328             return $aResult;
00329         }
00330 
00331         $sMD5 = md5_file($this->_sBaseDirectory . $sFile);
00332 
00333         usleep(10);
00334         $oXML = $this->_getFileVersion($sMD5, $sFile);
00335         $sColor = "blue";
00336         $blOk = true;
00337         $sMessage = oxRegistry::getLang()->translateString('OXDIAG_ERRORVERSIONCOMPARE');
00338 
00339         if (is_object($oXML)) {
00340 
00341             if ($oXML->res == 'OK') {
00342                 // If recognized, still can be source or snapshot
00343                 $aMatch = array();
00344 
00345                 if (preg_match('/(SOURCE|SNAPSHOT)/', $oXML->pkg, $aMatch)) {
00346                     $blOk = false;
00347                     $sMessage = 'SOURCE|SNAPSHOT';
00348                     $sColor = 'red';
00349                 } else {
00350                     $sMessage = oxRegistry::getLang()->translateString('OXDIAG_OK');
00351                     $sColor = "green";
00352                 }
00353             } elseif ($oXML->res == 'VERSIONMISMATCH') {
00354                 $sMessage = oxRegistry::getLang()->translateString('OXDIAG_VERSION_MISMATCH');
00355                 $sColor = 'red';
00356                 $blOk = false;
00357             } elseif ($oXML->res == 'MODIFIED') {
00358                 $sMessage = oxRegistry::getLang()->translateString('OXDIAG_MODIFIED');
00359                 $sColor = 'red';
00360                 $blOk = false;
00361             } elseif ($oXML->res == 'OBSOLETE') {
00362                 $sMessage = oxRegistry::getLang()->translateString('OXDIAG_OBSOLETE');
00363                 $sColor = 'red';
00364                 $blOk = false;
00365             } elseif ($oXML->res == 'UNKNOWN') {
00366                 $sMessage = oxRegistry::getLang()->translateString('OXDIAG_UNKNOWN');
00367                 $sColor = "green";
00368             }
00369         }
00370 
00371         if ($sMessage) {
00372             $aResult = array(
00373                 "result"  => strval($oXML->res),
00374                 "ok"      => $blOk,
00375                 "file"    => $sFile,
00376                 "color"   => $sColor,
00377                 "message" => $sMessage
00378             );
00379         }
00380 
00381         return $aResult;
00382     }
00383 
00392     protected function _getFileVersion($sMD5, $sFile)
00393     {
00394         $aParams = array(
00395             'job' => 'md5check',
00396             'ver' => $this->getVersion(),
00397             'rev' => $this->getRevision(),
00398             'edi' => $this->getEdition(),
00399             'fil' => $sFile,
00400             'md5' => $sMD5,
00401         );
00402 
00403         $this->_oCurlHandler->setUrl($this->_sWebServiceUrl);
00404         $this->_oCurlHandler->setMethod("GET");
00405         $this->_oCurlHandler->setOption("CURLOPT_CONNECTTIMEOUT", 30);
00406         $this->_oCurlHandler->setParameters($aParams);
00407         $sXML = $this->_oCurlHandler->execute();
00408         $oXML = null;
00409         try {
00410             $oXML = new SimpleXMLElement($sXML);
00411         } catch (Exception $ex) {
00412             $oXML = null;
00413         }
00414 
00415         return $oXML;
00416     }
00417 }