PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Upgrader.php

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