* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 9790 $ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ include_once(_PS_ADMIN_DIR_.'/../tools/tar/Archive_Tar.php'); class AdminModulesControllerCore extends AdminController { /** @var array map with $_GET keywords and their callback */ private $map = array( 'install' => 'install', 'uninstall' => 'uninstall', 'configure' => 'getContent', 'delete' => 'delete' ); private $list_modules_categories = array(); private $list_partners_modules = array(); private $list_natives_modules = array(); private $cache_file_modules_list = '/config/modules_list.xml'; private $xml_modules_list = 'http://www.prestashop.com/xml/modules_list.xml'; private $nb_modules_total = 0; private $nb_modules_installed = 0; private $nb_modules_activated = 0; private $serial_modules = ''; private $modules_authors = array(); public function __construct() { parent::__construct(); // Set the modules categories $this->list_modules_categories['administration']['name'] = $this->l('Administration'); $this->list_modules_categories['advertising_marketing']['name'] = $this->l('Advertising & Marketing'); $this->list_modules_categories['analytics_stats']['name'] = $this->l('Analytics & Stats'); $this->list_modules_categories['billing_invoicing']['name'] = $this->l('Billing & Invoicing'); $this->list_modules_categories['checkout']['name'] = $this->l('Checkout'); $this->list_modules_categories['content_management']['name'] = $this->l('Content Management'); $this->list_modules_categories['export']['name'] = $this->l('Export'); $this->list_modules_categories['front_office_features']['name'] = $this->l('Front Office Features'); $this->list_modules_categories['i18n_localization']['name'] = $this->l('I18n & Localization'); $this->list_modules_categories['merchandizing']['name'] = $this->l('Merchandizing'); $this->list_modules_categories['migration_tools']['name'] = $this->l('Migration Tools'); $this->list_modules_categories['payments_gateways']['name'] = $this->l('Payments & Gateways'); $this->list_modules_categories['payment_security']['name'] = $this->l('Payment Security'); $this->list_modules_categories['pricing_promotion']['name'] = $this->l('Pricing & Promotion'); $this->list_modules_categories['quick_bulk_update']['name'] = $this->l('Quick / Bulk update'); $this->list_modules_categories['search_filter']['name'] = $this->l('Search & Filter'); $this->list_modules_categories['seo']['name'] = $this->l('SEO'); $this->list_modules_categories['shipping_logistics']['name'] = $this->l('Shipping & Logistics'); $this->list_modules_categories['slideshows']['name'] = $this->l('Slideshows'); $this->list_modules_categories['smart_shopping']['name'] = $this->l('Smart Shopping'); $this->list_modules_categories['market_place']['name'] = $this->l('Market Place'); $this->list_modules_categories['social_networks']['name'] = $this->l('Social Networks'); $this->list_modules_categories['others']['name'] = $this->l('Other Modules'); // Load cache file modules list (natives and partners modules) if (file_exists(_PS_ROOT_DIR_.$this->cache_file_modules_list)) $xmlModules = @simplexml_load_file(_PS_ROOT_DIR_.$this->cache_file_modules_list); else $xmlModules = false; if ($xmlModules) { foreach($xmlModules->children() as $xmlModule) { if ($xmlModule->attributes() == 'native') foreach($xmlModule->children() as $module) foreach($module->attributes() as $key => $value) if ($key == 'name') $this->list_natives_modules[] = (string)$value; if ($xmlModule->attributes() == 'partner') foreach ($xmlModule->children() as $module) foreach ($module->attributes() as $key => $value) if ($key == 'name') $this->list_partners_modules[] = (string)$value; } } } /** if modules_list.xml is outdated, * this function will re-upload it from prestashop.com * * @return null */ public function ajaxProcessRefreshModuleList() { //refresh modules_list.xml every week if (!$this->isFresh()) { if ($this->refresh()) $this->status = 'refresh'; else $this->status = 'error'; } else $this->status = 'cache'; } public function isFresh($timeout = 604800000) { if (file_exists(_PS_ROOT_DIR_ . $this->_moduleCacheFile)) return ((time() - filemtime(_PS_ROOT_DIR_ . $this->_moduleCacheFile)) < $timeout); else return false; } public function refresh() { return file_put_contents(_PS_ROOT_DIR_.$this->_moduleCacheFile, Tools::file_get_contents($this->xml_modules_list)); } public function displayAjaxRefreshModuleList() { echo Tools::jsonEncode(array('status' => $this->status)); } private function setFilterModules($module_type, $country_module_value, $module_install, $module_status) { $this->context = Context::getContext(); Configuration::updateValue('PS_SHOW_TYPE_MODULES_'.(int)$this->context->employee->id, $module_type); Configuration::updateValue('PS_SHOW_COUNTRY_MODULES_'.(int)$this->context->employee->id, $country_module_value); Configuration::updateValue('PS_SHOW_INSTALLED_MODULES_'.(int)$this->context->employee->id, $module_install); Configuration::updateValue('PS_SHOW_ENABLED_MODULES_'.(int)$this->context->employee->id, $module_status); } private function resetFilterModules() { $this->context = Context::getContext(); Configuration::updateValue('PS_SHOW_TYPE_MODULES_'.(int)$this->context->employee->id, 'allModules'); Configuration::updateValue('PS_SHOW_COUNTRY_MODULES_'.(int)$this->context->employee->id, 0); Configuration::updateValue('PS_SHOW_INSTALLED_MODULES_'.(int)$this->context->employee->id, 'installedUninstalled'); Configuration::updateValue('PS_SHOW_ENABLED_MODULES_'.(int)$this->context->employee->id, 'enabledDisabled'); } /** * Get current URL * * @param array $remove List of keys to remove from URL * @return string */ protected function getCurrentUrl($remove = array()) { $url = $_SERVER['REQUEST_URI']; if (!$remove) return $url; if (!is_array($remove)) $remove = array($remove); $url = preg_replace('#(?<=&|\?)(' . implode('|', $remove) . ')=.*?(&|$)#i', '', $url); $len = strlen($url); if ($url[$len - 1] == '&') $url = substr($url, 0, $len - 1); return $url; } private function _getSubmitedModuleAuthor($value) { $value = str_replace('authorModules[', '', $value); $value = str_replace("\'", "'", $value); $value = substr($value, 0, -1); return $value; } public function postProcess() { $id_employee = (int)$this->context->employee->id; $filter_conf = Configuration::getMultiple(array( 'PS_SHOW_TYPE_MODULES_'.$id_employee, 'PS_SHOW_COUNTRY_MODULES_'.$id_employee, 'PS_SHOW_INSTALLED_MODULES_'.$id_employee, 'PS_SHOW_ENABLED_MODULES_'.$id_employee )); if (Tools::isSubmit('desactive') && isset($filter_conf['PS_SHOW_ENABLED_MODULES_'.$id_employee]) && $filter_conf['PS_SHOW_ENABLED_MODULES_'.$id_employee] != 'enabledDisabled') $this->setFilterModules($filter_conf['PS_SHOW_TYPE_MODULES_'.$id_employee], $filter_conf['PS_SHOW_COUNTRY_MODULES_'.$id_employee], $filter_conf['PS_SHOW_INSTALLED_MODULES_'.$id_employee], 'disabled'); if (Tools::isSubmit('active') && isset($filter_conf['PS_SHOW_ENABLED_MODULES_'.$id_employee]) && $filter_conf['PS_SHOW_ENABLED_MODULES_'.$id_employee] != 'enabledDisabled') $this->setFilterModules($filter_conf['PS_SHOW_TYPE_MODULES_'.$id_employee], $filter_conf['PS_SHOW_COUNTRY_MODULES_'.$id_employee], $filter_conf['PS_SHOW_INSTALLED_MODULES_'.$id_employee], 'enabled'); if (Tools::isSubmit('uninstall') && isset($filter_conf['PS_SHOW_INSTALLED_MODULES_'.$id_employee]) && $filter_conf['PS_SHOW_INSTALLED_MODULES_'.$id_employee] != 'installedUninstalled') $this->setFilterModules($filter_conf['PS_SHOW_TYPE_MODULES_'.$id_employee], $filter_conf['PS_SHOW_COUNTRY_MODULES_'.$id_employee], 'unistalled', $filter_conf['PS_SHOW_ENABLED_MODULES_'.$id_employee]); if (Tools::isSubmit('install') && isset($filter_conf['PS_SHOW_INSTALLED_MODULES_'.$id_employee]) && $filter_conf['PS_SHOW_INSTALLED_MODULES_'.$id_employee] != 'installedUninstalled') $this->setFilterModules($filter_conf['PS_SHOW_TYPE_MODULES_'.$id_employee], $filter_conf['PS_SHOW_COUNTRY_MODULES_'.$id_employee], 'installed', $filter_conf['PS_SHOW_ENABLED_MODULES_'.$id_employee]); if (Tools::getValue('filterCategory') != '') { $newFilterCategory = Tools::getValue('filterCategory'); $filterCategories = explode('|', Configuration::get('PS_SHOW_CAT_MODULES_'.(int)$this->context->employee->id)); // Check if category is not already filtered foreach ($filterCategories as $fc) if ($fc == $newFilterCategory) $newFilterCategory = ''; // Add the new filter if ($newFilterCategory != '') $filterCategories[] = $newFilterCategory; $filterCategories = implode('|', $filterCategories); Configuration::updateValue('PS_SHOW_CAT_MODULES_'.(int)$this->context->employee->id, $filterCategories); Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token); } if (Tools::getValue('unfilterCategory') != '') { $unfilterCategory = Tools::getValue('unfilterCategory'); $filterCategories = explode('|', Configuration::get('PS_SHOW_CAT_MODULES_'.(int)$this->context->employee->id)); // Remove the category foreach ($filterCategories as $k => $fc) if ($fc == $unfilterCategory) unset($filterCategories[$k]); $filterCategories = implode('|', $filterCategories); Configuration::updateValue('PS_SHOW_CAT_MODULES_'.(int)$this->context->employee->id, $filterCategories); Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token); } if (Tools::isSubmit('filterModules')) { $this->setFilterModules(Tools::getValue('module_type'), Tools::getValue('country_module_value'), Tools::getValue('module_install'), Tools::getValue('module_status')); Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token); } elseif (Tools::isSubmit('resetFilterModules')) { $this->resetFilterModules(); Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token); } if (Tools::isSubmit('active')) { if ($this->tabAccess['edit'] === '1') { $module = Module::getInstanceByName(Tools::getValue('module_name')); if (Validate::isLoadedObject($module)) { if (!$module->getPermission('configure')) $this->_errors[] = Tools::displayError('You do not have the permission to use this module'); else { $module->enable(); Tools::redirectAdmin(self::$currentIndex.'&conf=5&token='.$this->token.'&tab_module='.$module->tab.'&module_name='.$module->name); } } else $this->_errors[] = Tools::displayError('Cannot load module object'); } else $this->_errors[] = Tools::displayError('You do not have permission to add here.'); } elseif (Tools::isSubmit('desactive')) { if ($this->tabAccess['edit'] === '1') { $module = Module::getInstanceByName(Tools::getValue('module_name')); if (Validate::isLoadedObject($module)) { if (!$module->getPermission('configure')) $this->_errors[] = Tools::displayError('You do not have the permission to use this module'); else { $module->disable(); Tools::redirectAdmin(self::$currentIndex.'&conf=5&token='.$this->token.'&tab_module='.$module->tab.'&module_name='.$module->name); } } else $this->_errors[] = Tools::displayError('Cannot load module object'); } else $this->_errors[] = Tools::displayError('You do not have permission to add here.'); } elseif (Tools::isSubmit('reset')) { if ($this->tabAccess['edit'] === '1') { $module = Module::getInstanceByName(Tools::getValue('module_name')); if (Validate::isLoadedObject($module)) { if (!$module->getPermission('configure')) $this->_errors[] = Tools::displayError('You do not have the permission to use this module'); else { if ($module->uninstall()) if ($module->install()) Tools::redirectAdmin(self::$currentIndex.'&conf=21'.'&token='.$this->token.'&tab_module='.$module->tab.'&module_name='.$module->name); else $this->_errors[] = Tools::displayError('Cannot install module'); else $this->_errors[] = Tools::displayError('Cannot uninstall module'); } } else $this->_errors[] = Tools::displayError('Cannot load module object'); } else $this->_errors[] = Tools::displayError('You do not have permission to add here.'); } /* Automatically copy a module from external URL and unarchive it in the appropriated directory */ if (Tools::isSubmit('submitDownload2')) { /* PrestaShop demo mode */ if (_PS_MODE_DEMO_) { $this->_errors[] = Tools::displayError('This functionnality has been disabled.'); return; } /* PrestaShop demo mode*/ 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 occurred 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 here.'); } // Enable / disable module if (Tools::getValue('enable') !== false) { if ($this->tabAccess['edit'] === '1') { $module = Module::getInstanceByName(Tools::getValue('module_name')); if (Validate::isLoadedObject($module)) { if (!$module->getPermission('configure')) $this->_errors[] = Tools::displayError('You do not have the permission to use this module'); else { if (Tools::getValue('enable')) $module->enable(); else $module->disable(); Tools::redirectAdmin($this->getCurrentUrl('enable')); } } else $this->_errors[] = Tools::displayError('Cannot load module object'); } else $this->_errors[] = Tools::displayError('You do not have permission to add here.'); } if (Tools::isSubmit('deleteModule')) { if ($this->tabAccess['delete'] === '1') { if (Tools::getValue('module_name') != '') { $module = Module::getInstanceByName(Tools::getValue('module_name')); if (Validate::isLoadedObject($module) AND !$module->getPermission('configure')) $this->_errors[] = Tools::displayError('You do not have the permission to use this module'); else { $moduleDir = _PS_MODULE_DIR_.str_replace(array('.', '/', '\\'), array('', '', ''), Tools::getValue('module_name')); $this->recursiveDeleteOnDisk($moduleDir); Tools::redirectAdmin(self::$currentIndex.'&conf=22&token='.$this->token.'&tab_module='.Tools::getValue('tab_module').'&module_name='.Tools::getValue('module_name')); } } } else $this->_errors[] = Tools::displayError('You do not have permission to delete 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 install a module.'); elseif ($key == 'uninstall' AND ($this->tabAccess['delete'] !== '1' OR !$module->getPermission('configure'))) $this->_errors[] = Tools::displayError('You do not have permission to delete this module.'); elseif ($key == 'configure' AND ($this->tabAccess['edit'] !== '1' OR !$module->getPermission('configure'))) $this->_errors[] = Tools::displayError('You do not have permission to configure this module.'); elseif ($key == 'install' AND Module::isInstalled($module->name)) $this->_errors[] = Tools::displayError('This module is already installed:').' '.$module->name; elseif ($key == 'uninstall' AND !Module::isInstalled($module->name)) $this->_errors[] = Tools::displayError('This module is already uninstalled:').' '.$module->name; else { // If we install a module, force temporary global context for multishop if (Shop::isFeatureActive() && Context::shop() != Shop::CONTEXT_ALL) { Context::getContext()->tmpOldShop = clone(Context::getContext()->shop); Context::getContext()->shop = new Shop(); Configuration::updateValue('RSS_FEED_TITLE', 'lol'); } if (((method_exists($module, $method) && ($echo = $module->{$method}())) || ($echo = ' ')) AND $key == 'configure' AND Module::isInstalled($module->name)) { $backlink = self::$currentIndex.'&token='.$this->token.'&tab_module='.$module->tab.'&module_name='.$module->name; $hooklink = 'index.php?tab=AdminModulesPositions&token='.Tools::getAdminTokenLite('AdminModulesPositions').'&show_modules='.(int)$module->id; $tradlink = 'index.php?tab=AdminTranslations&token='.Tools::getAdminTokenLite('AdminTranslations').'&type=modules&lang='; $toolbar = '
| '.$this->l('Module').' '.$module->name.' | '.$this->l('Back').' | '.$this->l('Manage hooks').' | '.$this->l('Manage translations:').' ';
foreach (Language::getLanguages(false) AS $language)
$toolbar .= ' ';
$toolbar .= '
|
|---|---|---|---|
| active) ? 'checked="checked"' : '').' '.$activateOnclick.' /> '.$this->l('Activate module for').' '; if ($this->context->shop->getContextType() == Shop::CONTEXT_SHOP) $toolbar .= 'shop '.$this->context->shop->name.''; elseif ($this->context->shop->getContextType() == Shop::CONTEXT_GROUP) $toolbar .= 'all shops of group shop '.$this->context->shop->getGroup()->name.''; else $toolbar .= 'all shops'; $toolbar .= ' | |||