* @copyright PrestaShop * @license http://www.opensource.org/licenses/osl-3.0.php Open-source licence 3.0 * @version 1.3 * */ include_once(PS_ADMIN_DIR.'/../classes/AdminTab.php'); class AdminModules extends AdminTab { /** @var array map with $_GET keywords and their callback */ private $map = array( 'install' => 'install', 'uninstall' => 'uninstall', 'configure' => 'getContent' ); public function postProcess() { global $currentIndex, $cookie; if (Tools::isSubmit('all_module_send')) { if (Tools::getValue('all_module')) Configuration::updateValue('PS_SHOW_ALL_MODULES', 0); else Configuration::updateValue('PS_SHOW_ALL_MODULES', 1); } /* Automatically copy a module from external URL and unarchive it in the appropriated directory */ if (Tools::isSubmit('active')) { if ($this->tabAccess['edit'] === '1') { $module = Module::getInstanceByName(strval(Tools::getValue('module_name'))); if (Validate::isLoadedObject($module)) { Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'module` SET `active`= 1 WHERE `name` = \''.pSQL(Tools::getValue('module_name')).'\''); Tools::redirectAdmin($currentIndex.'&conf=5'.'&token='.$this->token); } else $this->_errors[] = Tools::displayError('Cannot load module object'); } else $this->_errors[] = Tools::displayError('You do not have permission to add anything here.'); } elseif (Tools::isSubmit('desactive')) { if ($this->tabAccess['edit'] === '1') { $module = Module::getInstanceByName(Tools::getValue('module_name')); if (Validate::isLoadedObject($module)) { Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'module` SET `active`= 0 WHERE `name` = \''.pSQL(Tools::getValue('module_name')).'\''); Tools::redirectAdmin($currentIndex.'&conf=5'.'&token='.$this->token); } else $this->_errors[] = Tools::displayError('Cannot load module object'); } else $this->_errors[] = Tools::displayError('You do not have permission to add anything here.'); } if (Tools::isSubmit('submitDownload')) { if ($this->tabAccess['add'] === '1') { if (Validate::isModuleUrl($url = Tools::getValue('url'), $this->_errors)) { if (!@copy($url, _PS_MODULE_DIR_.basename($url))) $this->_errors[] = Tools::displayError('404 Module not found'); else $this->extractArchive(_PS_MODULE_DIR_.basename($url)); } } else $this->_errors[] = Tools::displayError('You do not have permission to add anything here.'); } if (Tools::isSubmit('submitDownload2')) { if ($this->tabAccess['add'] === '1') { if (!isset($_FILES['file']['tmp_name']) OR empty($_FILES['file']['tmp_name'])) $this->_errors[] = $this->l('no file selected'); elseif (substr($_FILES['file']['name'], -4) != '.tar' AND substr($_FILES['file']['name'], -4) != '.zip' AND substr($_FILES['file']['name'], -4) != '.tgz' AND substr($_FILES['file']['name'], -7) != '.tar.gz') $this->_errors[] = Tools::displayError('unknown archive type'); elseif (!@copy($_FILES['file']['tmp_name'], _PS_MODULE_DIR_.$_FILES['file']['name'])) $this->_errors[] = Tools::displayError('an error occured while copying archive to module directory'); else $this->extractArchive(_PS_MODULE_DIR_.$_FILES['file']['name']); } else $this->_errors[] = Tools::displayError('You do not have permission to add anything here.'); } /* Call appropriate module callback */ else { $return = false; foreach ($this->map as $key => $method) { $modules = Tools::getValue($key); if (strpos($modules, '|')) $modules = explode('|', $modules); else $modules = empty($modules) ? false : array($modules); $module_errors = array(); if ($modules) foreach ($modules AS $name) { if (!($module = @Module::getInstanceByName(urldecode($name)))) $this->_errors[] = $this->l('module not found'); elseif ($key == 'install' AND $this->tabAccess['add'] !== '1') $this->_errors[] = Tools::displayError('You do not have permission to add anything here.'); elseif ($key == 'uninstall' AND $this->tabAccess['delete'] !== '1') $this->_errors[] = Tools::displayError('You do not have permission to delete here.'); elseif ($key == 'configure' AND $this->tabAccess['edit'] !== '1') $this->_errors[] = Tools::displayError('You do not have permission to edit anything here.'); elseif (($echo = $module->{$method}()) AND ($key == 'configure')) { echo '

'.$this->l('Back to modules list').'


'.$echo.'

'.$this->l('Back to modules list').'

'; } elseif($echo) $return = ($method == 'install' ? 12 : 13); elseif ($echo === false) $module_errors[] = $name; if ($key != 'configure' AND isset($_GET['bpay'])) Tools::redirectAdmin('index.php?tab=AdminPayment&token='.Tools::getAdminToken('AdminPayment'.intval(Tab::getIdFromClassName('AdminPayment')).intval($cookie->id_employee))); } if (sizeof($module_errors)) { echo '
'.$this->l('The following module(s) were not installed successfully:').'
'; } } if ($return) Tools::redirectAdmin($currentIndex.'&conf='.$return.'&token='.$this->token); } } function extractArchive($file) { global $currentIndex; $success = false; if (substr($file, -4) == '.zip') { if (class_exists('ZipArchive', false)) { $zip = new ZipArchive(); if ($zip->open($file) === true AND $zip->extractTo(_PS_MODULE_DIR_) AND $zip->close()) $success = true; else $this->_errors[] = Tools::displayError('error while extracting module (file may be corrupted)'); } else $this->_errors[] = Tools::displayError('zip is not installed on your server. Ask your host for further information.'); } else { $archive = new Archive_Tar($file); if ($archive->extract(_PS_MODULE_DIR_)) $success = true; else $this->_errors[] = Tools::displayError('error while extracting module (file may be corrupted)'); } @unlink($file); if ($success) Tools::redirectAdmin($currentIndex.'&conf=8'.'&token='.$this->token); } public function display() { if (!isset($_GET['configure']) OR sizeof($this->_errors)) $this->displayList(); } public function displayJavascript() { global $currentIndex; echo ' '; } public function displayList() { global $currentIndex, $cookie; $serialModules = ''; $modules = Module::getModulesOnDisk(); foreach ($modules AS $module) $serialModules .= $module->name.' '.$module->version.'-'.($module->active ? 'a' : 'i')."\n"; $serialModules = urlencode($serialModules); $this->displayJavascript(); $linkToSettings = 'index.php?tab=AdminPreferences&token='.Tools::getAdminToken('AdminPreferences'.intval(Tab::getIdFromClassName('AdminPreferences')).intval($cookie->id_employee)); echo ''.$this->l('Add a new module').' '.$this->l('Add a new module').''; if (Configuration::get('PRESTASTORE_LIVE') AND @ini_get('allow_url_fopen')) echo ' '.$this->l('PrestaStore').' ('.$this->l('disable').')'; echo '
 
'.$this->l('Add a new module').' '.$this->l('Add a new module').'

'.$this->l('The module must be either a zip file or a tarball.').'


'.$this->l('Download the module directly from a website.').'


'.$this->l('Upload the module from your computer.').'

'; if (Configuration::get('PRESTASTORE_LIVE')) echo ' '; echo '
 
'; /* Scan modules directories and load modules classes */ $warnings = array(); $orderModule = array(); $irow = 0; foreach ($modules AS $module) $orderModule[(isset($module->tab) AND !empty($module->tab)) ? $module->tab : $this->l('Not specified')][] = $module; asort($orderModule); foreach ($orderModule AS $tabModule) foreach ($tabModule AS $module) if ($module->active AND $module->warning) $this->displayWarning(''.$module->displayName.' - '.stripslashes(pSQL($module->warning))); $nameCountryDefault = Country::getNameById($cookie->id_lang, Configuration::get('PS_COUNTRY_DEFAULT')); echo '
('.$this->l('Current country:').' '.$nameCountryDefault.')
'; $showAllModules = Configuration::get('PS_SHOW_ALL_MODULES'); $isoCountryDefault = Country::getIsoById(Configuration::get('PS_COUNTRY_DEFAULT')); echo '
'; /* Browse modules by tab type */ foreach ($orderModule AS $tab => $tabModule) { echo '
'.$tab.' - '.sizeof($tabModule).' '.((sizeof($tabModule) > 1) ? $this->l('modules') : $this->l('module')).'
'; /* Display modules for each tab type */ foreach ($tabModule as $module) { if ($showAllModules || (!$showAllModules && ((isset($module->limited_countries) && in_array(strtolower($isoCountryDefault), $module->limited_countries)) || !isset($module->limited_countries)))) { if ($module->id) { $img = ''.$this->l('Module enabled').''; if ($module->warning) $img = ''.$this->l('Module installed but with warnings').''; if (!$module->active) $img = ''.$this->l('Module disabled').''; } else $img = ''.$this->l('Module not installed').''; echo ' '; } } echo '
'.stripslashes($module->displayName).''.($module->version ? ' v'.$module->version.(strpos($module->version, '.') !== false ? '' : '.0') : '').'
'.$module->description.'
'.(($module->id AND method_exists($module, 'getContent')) ? '>> '.$this->l('Configure').'' : '').' '; if ($module->id) echo ''; echo $img; if ($module->id) ''; echo ' '.((!$module->id) ? '' : '').' confirmUninstall) ? 'rel="false"' : 'rel="'.addslashes($module->confirmUninstall).'"').' />
'; } echo '

'.$this->l('Icon legend').'
  '.$this->l('Module not installed').'
  '.$this->l('Module installed and enabled').'
  '.$this->l('Module installed but disabled').'
  '.$this->l('Module installed but some warnings').'
 
'; } } ?>