oxfilechecker.php

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