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