PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Upgrader.php

https://bitbucket.org/enurkov/prestashop
PHP | 292 lines | 200 code | 21 blank | 71 comment | 49 complexity | 813401793a737ce427f0b7443e0fca02 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class UpgraderCore
  27. {
  28. const DEFAULT_CHECK_VERSION_DELAY_HOURS = 24;
  29. public $rss_version_link = 'http://api.prestashop.com/xml/upgrader.xml';
  30. public $rss_md5file_link_dir = 'http://api.prestashop.com/xml/md5/';
  31. /**
  32. * @var boolean contains true if last version is not installed
  33. */
  34. protected $need_upgrade = false;
  35. protected $changed_files = array();
  36. protected $missing_files = array();
  37. public $version_name;
  38. public $version_num;
  39. public $version_is_modified = null;
  40. /**
  41. * @var string contains hte url where to download the file
  42. */
  43. public $link;
  44. public $autoupgrade;
  45. public $autoupgrade_module;
  46. public $autoupgrade_last_version;
  47. public $autoupgrade_module_link;
  48. public $changelog;
  49. public $md5;
  50. public function __construct($autoload = false)
  51. {
  52. if ($autoload)
  53. {
  54. $this->loadFromConfig();
  55. // checkPSVersion to get need_upgrade
  56. $this->checkPSVersion();
  57. }
  58. }
  59. public function __get($var)
  60. {
  61. if ($var == 'need_upgrade')
  62. return $this->isLastVersion();
  63. }
  64. /**
  65. * downloadLast download the last version of PrestaShop and save it in $dest/$filename
  66. *
  67. * @param string $dest directory where to save the file
  68. * @param string $filename new filename
  69. * @return boolean
  70. *
  71. * @TODO ftp if copy is not possible (safe_mode for example)
  72. */
  73. public function downloadLast($dest, $filename = 'prestashop.zip')
  74. {
  75. if (empty($this->link))
  76. $this->checkPSVersion();
  77. $destPath = realpath($dest).DIRECTORY_SEPARATOR.$filename;
  78. if (@copy($this->link, $destPath))
  79. return true;
  80. else
  81. return false;
  82. }
  83. public function isLastVersion()
  84. {
  85. if (empty($this->link))
  86. $this->checkPSVersion();
  87. return $this->need_upgrade;
  88. }
  89. /**
  90. * checkPSVersion ask to prestashop.com if there is a new version. return an array if yes, false otherwise
  91. *
  92. * @return mixed
  93. */
  94. public function checkPSVersion($force = false)
  95. {
  96. if (class_exists('Configuration'))
  97. $last_check = Configuration::get('PS_LAST_VERSION_CHECK');
  98. else
  99. $last_check = 0;
  100. // if we use the autoupgrade process, we will never refresh it
  101. // except if no check has been done before
  102. if ($force || ($last_check < time() - (3600 * Upgrader::DEFAULT_CHECK_VERSION_DELAY_HOURS)))
  103. {
  104. libxml_set_streams_context(@stream_context_create(array('http' => array('timeout' => 3))));
  105. if ($feed = @simplexml_load_file($this->rss_version_link))
  106. {
  107. $this->version_name = (string)$feed->version->name;
  108. $this->version_num = (string)$feed->version->num;
  109. $this->link = (string)$feed->download->link;
  110. $this->md5 = (string)$feed->download->md5;
  111. $this->changelog = (string)$feed->download->changelog;
  112. $this->autoupgrade = (int)$feed->autoupgrade;
  113. $this->autoupgrade_module = (int)$feed->autoupgrade_module;
  114. $this->autoupgrade_last_version = (string)$feed->autoupgrade_last_version;
  115. $this->autoupgrade_module_link = (string)$feed->autoupgrade_module_link;
  116. $this->desc = (string)$feed->desc;
  117. $config_last_version = array(
  118. 'name' => $this->version_name,
  119. 'num' => $this->version_num,
  120. 'link' => $this->link,
  121. 'md5' => $this->md5,
  122. 'autoupgrade' => $this->autoupgrade,
  123. 'autoupgrade_module' => $this->autoupgrade_module,
  124. 'autoupgrade_last_version' => $this->autoupgrade_last_version,
  125. 'autoupgrade_module_link' => $this->autoupgrade_module_link,
  126. 'changelog' => $this->changelog,
  127. 'desc' => $this->desc
  128. );
  129. if (class_exists('Configuration'))
  130. {
  131. Configuration::updateValue('PS_LAST_VERSION', serialize($config_last_version));
  132. Configuration::updateValue('PS_LAST_VERSION_CHECK', time());
  133. }
  134. }
  135. }
  136. else
  137. $this->loadFromConfig();
  138. // retro-compatibility :
  139. // return array(name,link) if you don't use the last version
  140. // false otherwise
  141. if (version_compare(_PS_VERSION_, $this->version_num, '<'))
  142. {
  143. $this->need_upgrade = true;
  144. return array('name' => $this->version_name, 'link' => $this->link);
  145. }
  146. else
  147. return false;
  148. }
  149. /**
  150. * load the last version informations stocked in base
  151. *
  152. * @return $this
  153. */
  154. public function loadFromConfig()
  155. {
  156. $last_version_check = Tools::unSerialize(Configuration::get('PS_LAST_VERSION'));
  157. if($last_version_check)
  158. {
  159. if (isset($last_version_check['name']))
  160. $this->version_name = $last_version_check['name'];
  161. if (isset($last_version_check['num']))
  162. $this->version_num = $last_version_check['num'];
  163. if (isset($last_version_check['link']))
  164. $this->link = $last_version_check['link'];
  165. if (isset($last_version_check['autoupgrade']))
  166. $this->autoupgrade = $last_version_check['autoupgrade'];
  167. if (isset($last_version_check['autoupgrade_module']))
  168. $this->autoupgrade_module = $last_version_check['autoupgrade_module'];
  169. if (isset($last_version_check['autoupgrade_last_version']))
  170. $this->autoupgrade_last_version = $last_version_check['autoupgrade_last_version'];
  171. if (isset($last_version_check['autoupgrade_module_link']))
  172. $this->autoupgrade_module_link = $last_version_check['autoupgrade_module_link'];
  173. if (isset($last_version_check['md5']))
  174. $this->md5 = $last_version_check['md5'];
  175. if (isset($last_version_check['desc']))
  176. $this->desc = $last_version_check['desc'];
  177. if (isset($last_version_check['changelog']))
  178. $this->changelog = $last_version_check['changelog'];
  179. }
  180. return $this;
  181. }
  182. /**
  183. * return an array of files
  184. * that the md5file does not match to the original md5file (provided by $rss_md5file_link_dir )
  185. * @return void
  186. */
  187. public function getChangedFilesList()
  188. {
  189. if (is_array($this->changed_files) && count($this->changed_files) == 0)
  190. {
  191. libxml_set_streams_context(@stream_context_create(array('http' => array('timeout' => 3))));
  192. $checksum = @simplexml_load_file($this->rss_md5file_link_dir._PS_VERSION_.'.xml');
  193. if ($checksum == false)
  194. {
  195. $this->changed_files = false;
  196. }
  197. else
  198. $this->browseXmlAndCompare($checksum->ps_root_dir[0]);
  199. }
  200. return $this->changed_files;
  201. }
  202. /** populate $this->changed_files with $path
  203. * in sub arrays mail, translation and core items
  204. * @param string $path filepath to add, relative to _PS_ROOT_DIR_
  205. */
  206. protected function addChangedFile($path)
  207. {
  208. $this->version_is_modified = true;
  209. if (strpos($path, 'mails/') !== false)
  210. $this->changed_files['mail'][] = $path;
  211. else if (
  212. strpos($path, '/en.php') !== false
  213. || strpos($path, '/fr.php') !== false
  214. || strpos($path, '/es.php') !== false
  215. || strpos($path, '/it.php') !== false
  216. || strpos($path, '/de.php') !== false
  217. || strpos($path, 'translations/') !== false
  218. )
  219. $this->changed_files['translation'][] = $path;
  220. else
  221. $this->changed_files['core'][] = $path;
  222. }
  223. /** populate $this->missing_files with $path
  224. * @param string $path filepath to add, relative to _PS_ROOT_DIR_
  225. */
  226. protected function addMissingFile($path)
  227. {
  228. $this->version_is_modified = true;
  229. $this->missing_files[] = $path;
  230. }
  231. protected function browseXmlAndCompare($node, &$current_path = array(), $level = 1)
  232. {
  233. foreach ($node as $key => $child)
  234. {
  235. if (is_object($child) && $child->getName() == 'dir')
  236. {
  237. $current_path[$level] = (string)$child['name'];
  238. $this->browseXmlAndCompare($child, $current_path, $level + 1);
  239. }
  240. else if (is_object($child) && $child->getName() == 'md5file')
  241. {
  242. // We will store only relative path.
  243. // absolute path is only used for file_exists and compare
  244. $relative_path = '';
  245. for ($i = 1; $i < $level; $i++)
  246. $relative_path .= $current_path[$i].'/';
  247. $relative_path .= (string)$child['name'];
  248. $fullpath = _PS_ROOT_DIR_.DIRECTORY_SEPARATOR . $relative_path;
  249. $fullpath = str_replace('ps_root_dir', _PS_ROOT_DIR_, $fullpath);
  250. // replace default admin dir by current one
  251. $fullpath = str_replace(_PS_ROOT_DIR_.'/admin', _PS_ADMIN_DIR_, $fullpath);
  252. if (!file_exists($fullpath))
  253. $this->addMissingFile($relative_path);
  254. else if (!$this->compareChecksum($fullpath, (string)$child))
  255. $this->addChangedFile($relative_path);
  256. // else, file is original (and ok)
  257. }
  258. }
  259. }
  260. protected function compareChecksum($path, $original_sum)
  261. {
  262. if (md5_file($path) == $original_sum)
  263. return true;
  264. return false;
  265. }
  266. public function isAuthenticPrestashopVersion()
  267. {
  268. $this->getChangedFilesList();
  269. return !$this->version_is_modified;
  270. }
  271. }