* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision$ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ class UpgraderCore{ const DEFAULT_CHECK_VERSION_DELAY_HOURS = 24; public $rss_version_link = 'http://www.prestashop.com/xml/version.xml'; public $rss_md5file_link_dir = 'http://www.prestashop.com/xml/md5/'; /** * link contains hte url where to download the file * * @var string */ private $needUpgrade = false; private $noRefresh = false; private $changedFiles = array(); private $missingFiles = array(); private $versionIsModified = false; public $version_name; public $version_num; public $link; public $autoupgrade; public $autoupgrade_module; public $changelog; public $md5; public function __get($var) { if($var == 'needUpgrade') return $this->isLastVersion(); } /** * we need to checkPSVersion when we use that class * @param boolean noRefresh if true, checkPSVersion will not refresh its information * @return object Upgrader */ public function __construct($noRefresh = false) { $this->noRefresh = (bool)$noRefresh; } /** * downloadLast download the last version of PrestaShop and save it in $dest/$filename * * @param string $dest directory where to save the file * @param string $filename new filename * @return boolean * * @TODO ftp if copy is not possible (safe_mode for example) */ public function downloadLast($dest, $filename = 'prestashop.zip') { if (empty($this->link)) $this->checkPSVersion(); if (@copy($this->link, realpath($dest).DIRECTORY_SEPARATOR.$filename)) return true; else return false; } public function isLastVersion() { if (empty($this->link)) $this->checkPSVersion(); return $this->needUpgrade; } /** * checkPSVersion ask to prestashop.com if there is a new version. return an array if yes, false otherwise * * @return mixed */ public function checkPSVersion($force = false) { if (empty($this->link)) { $lastCheck = Configuration::get('PS_LAST_VERSION_CHECK'); // if we use the autoupgrade process, we will never refresh it // except if no check has been done before if ($force OR ($lastCheck < time() - (3600 * Upgrader::DEFAULT_CHECK_VERSION_DELAY_HOURS)) ) { libxml_set_streams_context(stream_context_create(array('http' => array('timeout' => 3)))); if ($feed = @simplexml_load_file($this->rss_version_link)) { $this->version_name = (string)$feed->version->name; $this->version_num = (string)$feed->version->num; $this->link = (string)$feed->download->link; $this->md5 = (string)$feed->download->md5; $this->changelog = (string)$feed->download->changelog; $this->autoupgrade = (int)$feed->autoupgrade; $this->autoupgrade_module = (int)$feed->autoupgrade_module; $this->desc = (string)$feed->desc ; $configLastVersion = array( 'name' => $this->version_name, 'num' => $this->version_num, 'link' => $this->link, 'md5' => $this->md5, 'autoupgrade' => $this->autoupgrade, 'autoupgrade_module' => $this->autoupgrade_module, 'changelog' => $this->changelog, 'desc' => $this->desc ); Configuration::updateValue('PS_LAST_VERSION',serialize($configLastVersion)); Configuration::updateValue('PS_LAST_VERSION_CHECK',time()); } } else { $lastVersionCheck = @unserialize(Configuration::get('PS_LAST_VERSION')); $this->version_name = $lastVersionCheck['name']; $this->version_num = $lastVersionCheck['num']; $this->link = $lastVersionCheck['link']; $this->autoupgrade = $lastVersionCheck['autoupgrade']; $this->autoupgrade_module = $lastVersionCheck['autoupgrade_module']; $this->md5 = $lastVersionCheck['md5']; $this->desc = $lastVersionCheck['desc']; $this->changelog = $lastVersionCheck['changelog']; } } // retro-compatibility : // return array(name,link) if you don't use the last version // false otherwise if (version_compare(_PS_VERSION_, $this->version_num, '<')) { $this->needUpgrade = true; return array('name' => $this->version_name, 'link' => $this->link); } else return false; } public function getChangedFilesList() { $checksum = simplexml_load_file($this->rss_md5file_link_dir._PS_VERSION_.'.xml'); if ($checksum === false) throw new Exception('No checksum xml file for your Prestashop version'); $this->browseXmlAndCompare($checksum); return $this->changedFiles; } protected function addChangedFile($path) { $this->versionIsModified = true; $this->changedFiles[] = $path; } protected function addMissingFile($path) { $this->versionIsModified = true; $this->missingFiles[] = $path; } protected function browseXmlAndCompare($node, &$currentPath = array(), $level = 0) { foreach ($node as $key => $child) { if (is_object($child) && $child->getName() != 'md5file') { $level += 1; $currentPath[$level] = $child->getName(); $this->browseXmlAndCompare($child, $currentPath, $level); $level -= 1; } else { $path = ''; for ($i=1;$i<=$level;$i++) $path .= $currentPath[$i].'/'; $path .= (string)$child->attributes()->name; $path = str_replace('ps_root_dir',_PS_ROOT_DIR_,$path); if(!file_exists($path)) $this->addMissingFile($path); else if (!$this->compareChecksum($path, (string)$child->attributes()->sum)) $this->addChangedFile($path); } } } protected function compareChecksum($path, $originalSum) { if (md5_file($path) == $originalSum) return true; return false; } public function isAuthenticPrestashopVersion() { return !$this->versionIsModified; } }