* @copyright 2015 PrestaShow.pl * @license http://PrestaShow.pl/license */ class PShowUpdateController extends PShowAdminController { public $default_action = 'index'; public $filepath = null; public function ajaxProcess() { if (!defined('PS_ADMIN_DIR')) { define('PS_ADMIN_DIR', true); } if (Tools::isSubmit('getNewestVersion')) { die(Tools::jsonEncode(PShowUpdate::getInstance($this->filepath)->getNewestVersionNumber())); } } public function __construct() { $reflection = new ReflectionClass($this); $this->filepath = $reflection->getFileName(); require_once dirname($this->filepath) . "/../../config.php"; parent::__construct(); $this->controller_displayName = $this->l('Module update'); $this->action_displayName = $this->l('Update informations'); $smarty = Context::getContext()->smarty; $displayModuleVersion = PShowUpdate::getInstance($this->filepath)->formatVersionToDisplay( PShowUpdate::getInstance($this->filepath)->getModuleVersionNumber() ); $displayNewestVersion = PShowUpdate::getInstance($this->filepath)->formatVersionToDisplay( PShowUpdate::getInstance($this->filepath)->getNewestVersionNumber() ); $changelog_url = 'http://git.layersshow.com/Prestashow/changelog/raw/master/' . PShowUpdate::getInstance($this->filepath)->getModuleName() . '.md'; $changelog = Tools::file_get_contents($changelog_url); $changelog_path = PShowUpdate::getInstance($this->filepath)->getModulePath() . 'changelog.md'; if (!$changelog) { if (file_exists($changelog_path)) { $changelog = Tools::file_get_contents($changelog_path); } else { $changelog = $this->l('Changelog not found :('); } } $smarty->assign('changelog', $changelog); $smarty->assign('PShowUpdateInstance', PShowUpdate::getInstance($this->filepath)); $smarty->assign('ModuleVersionNumber', $displayModuleVersion); $smarty->assign('NewestVersionNumber', $displayNewestVersion); $smarty->assign('compareModuleAndNewestVersion', PShowUpdate::getInstance($this->filepath)->compareModuleAndNewestVersion()); } public function getCHMOD() { return Tools::substr(sprintf('%o', fileperms($this->filepath)), -4); } public function indexAction() { $this->action_displayName = $this->l('Update informations'); if ($this->getCHMOD() !== "0777") { $this->updateChmod(); if ($this->getCHMOD() !== "0777") { $this->alerts[] = array('danger', 'To update module you must set CHMOD 777 for all module files'); return; } } } public function updateChmod($path = null) { if ($path === null) { $path = PShowUpdate::getInstance($this->filepath)->getModulePath(); } $files = glob($path); foreach ($files as $file) { if (is_dir($file)) { $this->updateChmod($file . "/*"); } @chmod($file, 0777); } } public function updateAction() { $this->action = 'index'; $this->action_displayName = $this->l('Update informations'); $this->updateChmod(); $from_ver = PShowUpdate::getInstance($this->filepath)->getModuleVersionNumber(); if (PShowUpdate::getInstance($this->filepath)->compareModuleAndNewestVersion() && !Tools::getValue('force')) { return false; } $backup = PShowUpdate::getInstance($this->filepath)->makeModuleBackup(); if ($backup !== true) { $this->alerts[] = array('warning', $this->l('Module backup error: ') . $backup); return false; } else { $this->alerts[] = array('success', $this->l('Module backup completed')); } PShowUpdate::getInstance($this->filepath)->clearTmpDir(); $download = PShowUpdate::getInstance($this->filepath)->downloadUpdate(); if ($download !== true) { $this->alerts[] = array('warning', $this->l('Download error: ') . $download); return false; } else { $this->alerts[] = array('success', $this->l('Update downloaded')); } if (!PShowUpdate::getInstance($this->filepath)->extractUpdate()) { $this->alerts[] = array('warning', $this->l('Update package extract error')); return false; } else { $this->alerts[] = array('success', $this->l('Update package extracted')); } if (!PShowUpdate::getInstance($this->filepath)->moveUpdateToModule()) { $this->alerts[] = array('warning', $this->l('Module update error')); return false; } else { $this->alerts[] = array('success', $this->l('Module updated')); } Tools::redirectAdmin('index.php?controller=' . PShowUpdate::getInstance($this->filepath)->getModuleName() . '' . 'Update&token=' . $this->token . '&page=execupdate&from_version=' . $from_ver); } public function execupdateAction() { $this->action = 'index'; $this->action_displayName = $this->l('Update informations'); PShowUpdate::getInstance($this->filepath)->execUpdate(Tools::getValue('from_version')); PShowUpdate::getInstance($this->filepath)->clearTmpDir(); $this->alerts[] = array('success', $this->l('Module updated. In case of problems, reinstall the module.')); $this->indexAction(); } }