OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxfilechecker.php
Go to the documentation of this file.
1 <?php
2 
9 class oxFileChecker {
10 
16  protected $_blError = false;
17 
23  protected $_sErrorMessage = null;
24 
30  public $_sWebServiceUrl = 'http://oxchkversion.oxid-esales.com/webService.php';
31 
37  protected $_oCurlHandler = null;
38 
44  protected $_sEdition = "";
45 
51  protected $_sVersion = "";
52 
58  protected $_sRevision = "";
59 
65  protected $_sBaseDirectory = '';
66 
67 
73  protected $_blListAllFiles = false;
74 
80  public function setBaseDirectory( $sDir )
81  {
82  if ( !empty( $sDir ) ) {
83  $this->_sBaseDirectory = $sDir;
84  }
85  }
86 
92  public function getBaseDirectory()
93  {
95  }
96 
102  public function setVersion( $sVersion )
103  {
104  if ( !empty( $sVersion ) ) {
105  $this->_sVersion = $sVersion;
106  }
107  }
108 
114  public function getVersion()
115  {
116  return $this->_sVersion;
117  }
118 
124  public function setEdition( $sEdition )
125  {
126  if ( !empty( $sEdition ) ) {
127  $this->_sEdition = $sEdition;
128  }
129  }
130 
136  public function getEdition()
137  {
138  return $this->_sEdition;
139  }
140 
146  public function setRevision( $sRevision )
147  {
148  if ( !empty( $sRevision ) ) {
149  $this->_sRevision = $sRevision;
150  }
151  }
152 
158  public function getRevision()
159  {
160  return $this->_sRevision;
161  }
162 
168  public function setWebServiceUrl( $sUrl )
169  {
170  if ( !empty( $sUrl ) ) {
171  $this->_sWebServiceUrl = $sUrl;
172  }
173  }
174 
180  public function getWebServiceUrl()
181  {
182  return $this->_sWebServiceUrl;
183  }
184 
190  public function hasError()
191  {
192  return $this->_blError;
193  }
194 
200  public function getErrorMessage()
201  {
202  return $this->_sErrorMessage;
203  }
204 
210  public function init()
211  {
212  $this->_oCurlHandler = oxNew( "oxCurl" );
213 
214  if ( !$this->checkSystemRequirements() ) {
215  $this->_blError = true;
216  $this->_sErrorMessage .= "Error: requirements are not met.";
217  return false;
218  }
219 
220  return true;
221  }
222 
223 
224 
230  public function checkSystemRequirements()
231  {
232  return $this->_isWebServiceOnline() && $this->_isShopVersionIsKnown();
233  }
234 
239  protected function _isWebServiceOnline()
240  {
241  $oXML = null;
242  $aParams = array(
243  'job' => 'ping',
244  );
245 
246  $this->_oCurlHandler->setUrl( $this->_sWebServiceUrl );
247  $this->_oCurlHandler->setMethod("GET");
248  $this->_oCurlHandler->setOption("CURLOPT_CONNECTTIMEOUT", 30);
249  $this->_oCurlHandler->setParameters( $aParams );
250  $sXML = $this->_oCurlHandler->execute();
251 
252  if (empty( $sXML ) ) {
253  $this->_blError = true;
254  $this->_sErrorMessage = oxRegistry::getLang()->translateString( 'OXDIAG_ERRORMESSAGEWEBSERVICEISNOTREACHABLE' );
255  }
256 
257  try {
258  $oXML = new SimpleXMLElement( $sXML );
259  } catch (Exception $ex) {
260  $this->_blError = true;
261  $this->_sErrorMessage .= oxRegistry::getLang()->translateString( 'OXDIAG_ERRORMESSAGEWEBSERVICERETURNEDNOXML' );
262  }
263 
264  if (!is_object( $oXML ) ) {
265  $this->_blError = true;
266  $this->_sErrorMessage .= oxRegistry::getLang()->translateString( 'OXDIAG_ERRORMESSAGEVERSIONDOESNOTEXIST' );
267  }
268 
269  return !$this->_blError;
270  }
271 
272 
277  protected function _isShopVersionIsKnown()
278  {
279  $aParams = array(
280  'job' => 'existsversion',
281  'ver' => $this->getVersion(),
282  'rev' => $this->getRevision(),
283  'edi' => $this->getEdition(),
284  );
285 
286  $sURL = $this->_sWebServiceUrl . "?" . http_build_query( $aParams );
287 
288  if ( $sXML = @file_get_contents( $sURL ) ) {
289  $oXML = new SimpleXMLElement( $sXML );
290  if ( is_object( $oXML ) ) {
291  if ( $oXML->exists == 1) {
292  return true;
293  }
294  }
295  }
296 
297  $this->_blError = true;
298  $sError = sprintf( oxRegistry::getLang()->translateString( 'OXDIAG_ERRORMESSAGEVERSIONDOESNOTEXIST' ),
299  $this->getEdition(), $this->getVersion(), $this->getRevision() );
300 
301  $this->_sErrorMessage .= $sError;
302 
303  return false;
304  }
305 
313  public function checkFile( $sFile )
314  {
315  $aResult = array();
316 
317  if ($this->_oCurlHandler == null) {
318  return $aResult;
319  }
320 
321  if ( !file_exists( $this->_sBaseDirectory . $sFile ) ) {
322  return $aResult;
323  }
324 
325  $sMD5 = md5_file ( $this->_sBaseDirectory . $sFile );
326 
327  usleep( 10 );
328  $oXML = $this->_getFileVersion( $sMD5, $sFile );
329  $sColor = "blue";
330  $blOk = true;
331  $sMessage = oxRegistry::getLang()->translateString( 'OXDIAG_ERRORVERSIONCOMPARE' );
332 
333  if (is_object( $oXML ) ) {
334 
335  if ( $oXML->res == 'OK' ) {
336  // If recognized, still can be source or snapshot
337  $aMatch = array();
338 
339  if (preg_match ('/(SOURCE|SNAPSHOT)/', $oXML->pkg, $aMatch ) ) {
340  $blOk = false;
341  $sMessage = 'SOURCE|SNAPSHOT';
342  $sColor = 'red';
343  } else {
344  $sMessage = oxRegistry::getLang()->translateString( 'OXDIAG_OK' );
345  $sColor = "green";
346  }
347  } elseif ( $oXML->res == 'VERSIONMISMATCH' ) {
348  $sMessage = oxRegistry::getLang()->translateString( 'OXDIAG_VERSION_MISMATCH' );
349  $sColor = 'red';
350  $blOk = false;
351  } elseif ( $oXML->res == 'MODIFIED' ) {
352  $sMessage = oxRegistry::getLang()->translateString( 'OXDIAG_MODIFIED' );
353  $sColor = 'red';
354  $blOk = false;
355  } elseif ( $oXML->res == 'OBSOLETE' ) {
356  $sMessage = oxRegistry::getLang()->translateString( 'OXDIAG_OBSOLETE' );
357  $sColor = 'red';
358  $blOk = false;
359  } elseif ( $oXML->res == 'UNKNOWN' ) {
360  $sMessage = oxRegistry::getLang()->translateString( 'OXDIAG_UNKNOWN' );
361  $sColor = "green";
362  }
363  }
364 
365  if ( $sMessage ) {
366  $aResult = array(
367  "result" => strval( $oXML->res),
368  "ok" => $blOk,
369  "file" => $sFile,
370  "color" => $sColor,
371  "message" => $sMessage
372  );
373  }
374 
375  return $aResult;
376  }
377 
386  protected function _getFileVersion( $sMD5, $sFile)
387  {
388  $aParams = array(
389  'job' => 'md5check',
390  'ver' => $this->getVersion(),
391  'rev' => $this->getRevision(),
392  'edi' => $this->getEdition(),
393  'fil' => $sFile,
394  'md5' => $sMD5,
395  );
396 
397  $this->_oCurlHandler->setUrl( $this->_sWebServiceUrl );
398  $this->_oCurlHandler->setMethod("GET");
399  $this->_oCurlHandler->setOption("CURLOPT_CONNECTTIMEOUT", 30);
400  $this->_oCurlHandler->setParameters( $aParams );
401  $sXML = $this->_oCurlHandler->execute();
402  $oXML = null;
403  try {
404  $oXML = new SimpleXMLElement( $sXML );
405  } catch (Exception $ex) {
406  $oXML = null;
407  }
408  return $oXML;
409  }
410 
411 }