PageRenderTime 61ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/admin/AdminModulesController.php

https://gitlab.com/mtellezgalindo/PrestaShop
PHP | 1619 lines | 1362 code | 139 blank | 118 comment | 337 complexity | 18a15d29f3928b516007176b847ec7ea MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. * 2007-2014 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-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class AdminModulesControllerCore extends AdminController
  27. {
  28. private $_modules_ad = array(
  29. 'blockcart' => array('cartabandonmentpro'),
  30. //'bloctopmenu' => array('advancedtopmenu'),
  31. 'blocklayered' => array('pm_advancedsearch4')
  32. );
  33. /*
  34. ** @var array map with $_GET keywords and their callback
  35. */
  36. protected $map = array(
  37. 'check' => 'check',
  38. 'install' => 'install',
  39. 'uninstall' => 'uninstall',
  40. 'configure' => 'getContent',
  41. 'update' => 'update',
  42. 'delete' => 'delete',
  43. 'checkAndUpdate' => 'checkAndUpdate'
  44. );
  45. protected $list_modules_categories = array();
  46. protected $list_partners_modules = array();
  47. protected $list_natives_modules = array();
  48. protected $nb_modules_total = 0;
  49. protected $nb_modules_installed = 0;
  50. protected $nb_modules_activated = 0;
  51. protected $serial_modules = '';
  52. protected $modules_authors = array();
  53. protected $id_employee;
  54. protected $iso_default_country;
  55. protected $filter_configuration = array();
  56. protected $xml_modules_list = 'api.prestashop.com/xml/modules_list_16.xml';
  57. /**
  58. * Admin Modules Controller Constructor
  59. * Init list modules categories
  60. * Load id employee
  61. * Load filter configuration
  62. * Load cache file
  63. */
  64. public function __construct()
  65. {
  66. $this->bootstrap = true;
  67. parent::__construct();
  68. register_shutdown_function('displayFatalError');
  69. // Set the modules categories
  70. $this->list_modules_categories['administration']['name'] = $this->l('Administration');
  71. $this->list_modules_categories['advertising_marketing']['name'] = $this->l('Advertising and Marketing');
  72. $this->list_modules_categories['analytics_stats']['name'] = $this->l('Analytics and Stats');
  73. $this->list_modules_categories['billing_invoicing']['name'] = $this->l('Taxes & Invoicing');
  74. $this->list_modules_categories['checkout']['name'] = $this->l('Checkout');
  75. $this->list_modules_categories['content_management']['name'] = $this->l('Content Management');
  76. $this->list_modules_categories['export']['name'] = $this->l('Export');
  77. $this->list_modules_categories['emailing']['name'] = $this->l('Emailing');
  78. $this->list_modules_categories['front_office_features']['name'] = $this->l('Front Office Features');
  79. $this->list_modules_categories['i18n_localization']['name'] = $this->l('Internationalization and Localization');
  80. $this->list_modules_categories['merchandizing']['name'] = $this->l('Merchandising');
  81. $this->list_modules_categories['migration_tools']['name'] = $this->l('Migration Tools');
  82. $this->list_modules_categories['payments_gateways']['name'] = $this->l('Payments and Gateways');
  83. $this->list_modules_categories['payment_security']['name'] = $this->l('Site certification & Fraud prevention');
  84. $this->list_modules_categories['pricing_promotion']['name'] = $this->l('Pricing and Promotion');
  85. $this->list_modules_categories['quick_bulk_update']['name'] = $this->l('Quick / Bulk update');
  86. /* $this->list_modules_categories['search_filter']['name'] = $this->l('Search and Filter'); */
  87. $this->list_modules_categories['seo']['name'] = $this->l('SEO');
  88. $this->list_modules_categories['shipping_logistics']['name'] = $this->l('Shipping and Logistics');
  89. $this->list_modules_categories['slideshows']['name'] = $this->l('Slideshows');
  90. $this->list_modules_categories['smart_shopping']['name'] = $this->l('Comparison site & Feed management');
  91. $this->list_modules_categories['market_place']['name'] = $this->l('Marketplace');
  92. $this->list_modules_categories['others']['name'] = $this->l('Other Modules');
  93. $this->list_modules_categories['mobile']['name'] = $this->l('Mobile');
  94. $this->list_modules_categories['dashboard']['name'] = $this->l('Dashboard');
  95. $this->list_modules_categories['i18n_localization']['name'] = $this->l('Internationalization & Localization');
  96. $this->list_modules_categories['emailing']['name'] = $this->l('Emailing & SMS');
  97. $this->list_modules_categories['social_networks']['name'] = $this->l('Social Networks');
  98. uasort($this->list_modules_categories, array($this, 'checkCategoriesNames'));
  99. // Set Id Employee, Iso Default Country and Filter Configuration
  100. $this->id_employee = (int)$this->context->employee->id;
  101. $this->iso_default_country = $this->context->country->iso_code;
  102. $this->filter_configuration = Configuration::getMultiple(array(
  103. 'PS_SHOW_TYPE_MODULES_'.(int)$this->id_employee,
  104. 'PS_SHOW_COUNTRY_MODULES_'.(int)$this->id_employee,
  105. 'PS_SHOW_INSTALLED_MODULES_'.(int)$this->id_employee,
  106. 'PS_SHOW_ENABLED_MODULES_'.(int)$this->id_employee,
  107. 'PS_SHOW_CAT_MODULES_'.(int)$this->id_employee,
  108. ));
  109. // Load cache file modules list (natives and partners modules)
  110. $xmlModules = false;
  111. if (file_exists(_PS_ROOT_DIR_.Module::CACHE_FILE_MODULES_LIST))
  112. $xmlModules = @simplexml_load_file(_PS_ROOT_DIR_.Module::CACHE_FILE_MODULES_LIST);
  113. if ($xmlModules)
  114. foreach ($xmlModules->children() as $xmlModule)
  115. foreach ($xmlModule->children() as $module)
  116. foreach ($module->attributes() as $key => $value)
  117. {
  118. if ($xmlModule->attributes() == 'native' && $key == 'name')
  119. $this->list_natives_modules[] = (string)$value;
  120. if ($xmlModule->attributes() == 'partner' && $key == 'name')
  121. $this->list_partners_modules[] = (string)$value;
  122. }
  123. }
  124. public function checkCategoriesNames($a, $b)
  125. {
  126. if ($a['name'] === $this->l('Other Modules'))
  127. return true;
  128. return (bool)($a['name'] > $b['name']);
  129. }
  130. public function setMedia()
  131. {
  132. parent::setMedia();
  133. $this->addJqueryPlugin(array('autocomplete', 'fancybox', 'tablefilter'));
  134. if ($this->context->mode == Context::MODE_HOST && Tools::isSubmit('addnewmodule'))
  135. $this->addJS(_PS_JS_DIR_.'admin/addons.js');
  136. }
  137. public function ajaxProcessRefreshModuleList($force_reload_cache = false)
  138. {
  139. // Refresh modules_list.xml every week
  140. if (!$this->isFresh(Module::CACHE_FILE_MODULES_LIST, 86400) || $force_reload_cache)
  141. {
  142. if ($this->refresh(Module::CACHE_FILE_MODULES_LIST, 'https://'.$this->xml_modules_list))
  143. $this->status = 'refresh';
  144. elseif ($this->refresh(Module::CACHE_FILE_MODULES_LIST, 'http://'.$this->xml_modules_list))
  145. $this->status = 'refresh';
  146. else
  147. $this->status = 'error';
  148. }
  149. else
  150. $this->status = 'cache';
  151. // If logged to Addons Webservices, refresh default country native modules list every day
  152. if ($this->status != 'error')
  153. {
  154. if (!$this->isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, 86400) || $force_reload_cache)
  155. {
  156. if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, Tools::addonsRequest('native')))
  157. $this->status = 'refresh';
  158. else
  159. $this->status = 'error';
  160. }
  161. else
  162. $this->status = 'cache';
  163. if (!$this->isFresh(Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, 86400) || $force_reload_cache)
  164. {
  165. if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, Tools::addonsRequest('must-have')))
  166. $this->status = 'refresh';
  167. else
  168. $this->status = 'error';
  169. }
  170. else
  171. $this->status = 'cache';
  172. }
  173. // If logged to Addons Webservices, refresh customer modules list every day
  174. if ($this->logged_on_addons && $this->status != 'error')
  175. {
  176. if (!$this->isFresh(Module::CACHE_FILE_CUSTOMER_MODULES_LIST, 60) || $force_reload_cache)
  177. {
  178. if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_CUSTOMER_MODULES_LIST, Tools::addonsRequest('customer')))
  179. $this->status = 'refresh';
  180. else
  181. $this->status = 'error';
  182. }
  183. else
  184. $this->status = 'cache';
  185. }
  186. }
  187. public function displayAjaxRefreshModuleList()
  188. {
  189. echo Tools::jsonEncode(array('status' => $this->status));
  190. }
  191. public function ajaxProcessLogOnAddonsWebservices()
  192. {
  193. $content = Tools::addonsRequest('check_customer', array('username_addons' => pSQL(trim(Tools::getValue('username_addons'))), 'password_addons' => pSQL(trim(Tools::getValue('password_addons')))));
  194. $xml = @simplexml_load_string($content, null, LIBXML_NOCDATA);
  195. if (!$xml)
  196. die('KO');
  197. $result = strtoupper((string)$xml->success);
  198. if (!in_array($result, array('OK', 'KO')))
  199. die ('KO');
  200. if ($result == 'OK')
  201. {
  202. Tools::clearXMLCache();
  203. Configuration::updateValue('PS_LOGGED_ON_ADDONS', 1);
  204. $this->context->cookie->username_addons = pSQL(trim(Tools::getValue('username_addons')));
  205. $this->context->cookie->password_addons = pSQL(trim(Tools::getValue('password_addons')));
  206. $this->context->cookie->is_contributor = (int)$xml->is_contributor;
  207. $this->context->cookie->write();
  208. }
  209. die($result);
  210. }
  211. public function ajaxProcessLogOutAddonsWebservices()
  212. {
  213. $this->context->cookie->username_addons = '';
  214. $this->context->cookie->password_addons = '';
  215. $this->context->cookie->is_contributor = 0;
  216. $this->context->cookie->write();
  217. die('OK');
  218. }
  219. public function ajaxProcessReloadModulesList()
  220. {
  221. if (Tools::getValue('filterCategory'))
  222. Configuration::updateValue('PS_SHOW_CAT_MODULES_'.(int)$this->id_employee, Tools::getValue('filterCategory'));
  223. if (Tools::getValue('unfilterCategory'))
  224. Configuration::updateValue('PS_SHOW_CAT_MODULES_'.(int)$this->id_employee, '');
  225. $this->initContent();
  226. $this->smartyOutputContent('controllers/modules/list.tpl');
  227. exit;
  228. }
  229. public function ajaxProcessGetTabModulesList()
  230. {
  231. $tab_modules_list = Tools::getValue('tab_modules_list');
  232. $back = Tools::getValue('back_tab_modules_list');
  233. if ($back)
  234. $back .= '&tab_modules_open=1';
  235. $modules_list = array('installed' =>array(), 'not_installed' => array());
  236. if ($tab_modules_list)
  237. {
  238. $tab_modules_list = explode(',', $tab_modules_list);
  239. $modules_list_unsort = $this->getModulesByInstallation($tab_modules_list);
  240. }
  241. $installed = $uninstalled = array();
  242. foreach ($tab_modules_list as $key => $value) {
  243. $continue = 0;
  244. foreach ($modules_list_unsort['installed'] as $mod_in)
  245. if ($mod_in->name == $value)
  246. {
  247. $continue = 1;
  248. $installed[] = $mod_in;
  249. }
  250. if ($continue)
  251. continue;
  252. foreach ($modules_list_unsort['not_installed'] as $mod_in)
  253. if ($mod_in->name == $value)
  254. $uninstalled[] = $mod_in;
  255. }
  256. $modules_list_sort = array(
  257. 'installed' => $installed,
  258. 'not_installed' => $uninstalled
  259. );
  260. $this->context->smarty->assign(array(
  261. 'tab_modules_list' => $modules_list_sort,
  262. 'admin_module_favorites_view' => $this->context->link->getAdminLink('AdminModules').'&select=favorites',
  263. ));
  264. $this->smartyOutputContent('controllers/modules/tab_modules_list.tpl');
  265. exit;
  266. }
  267. public function ajaxProcessSetFilter()
  268. {
  269. $this->setFilterModules(Tools::getValue('module_type'), Tools::getValue('country_module_value'), Tools::getValue('module_install'), Tools::getValue('module_status'));
  270. die('OK');
  271. }
  272. public function ajaxProcessSaveFavoritePreferences()
  273. {
  274. $action = Tools::getValue('action_pref');
  275. $value = Tools::getValue('value_pref');
  276. $module = Tools::getValue('module_pref');
  277. $id_module_preference = (int)Db::getInstance()->getValue('SELECT `id_module_preference` FROM `'._DB_PREFIX_.'module_preference` WHERE `id_employee` = '.(int)$this->id_employee.' AND `module` = \''.pSQL($module).'\'');
  278. if ($id_module_preference > 0)
  279. {
  280. if ($action == 'i')
  281. $update = array('interest' => ($value == '' ? null : (int)$value));
  282. if ($action == 'f')
  283. $update = array('favorite' => ($value == '' ? null : (int)$value));
  284. Db::getInstance()->update('module_preference', $update, '`id_employee` = '.(int)$this->id_employee.' AND `module` = \''.pSQL($module).'\'', 0, true);
  285. }
  286. else
  287. {
  288. $insert = array('id_employee' => (int)$this->id_employee, 'module' => pSQL($module), 'interest' => null, 'favorite' => null);
  289. if ($action == 'i')
  290. $insert['interest'] = ($value == '' ? null : (int)$value);
  291. if ($action == 'f')
  292. $insert['favorite'] = ($value == '' ? null : (int)$value);
  293. Db::getInstance()->insert('module_preference', $insert, true);
  294. }
  295. die('OK');
  296. }
  297. public function ajaxProcessSaveTabModulePreferences()
  298. {
  299. $values = Tools::getValue('value_pref');
  300. $module = Tools::getValue('module_pref');
  301. if (Validate::isModuleName($module))
  302. {
  303. Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'tab_module_preference` WHERE `id_employee` = '.(int)$this->id_employee.' AND `module` = \''.pSQL($module).'\'');
  304. if (is_array($values) && count($values))
  305. foreach($values as $value)
  306. Db::getInstance()->execute('
  307. INSERT INTO `'._DB_PREFIX_.'tab_module_preference` (`id_tab_module_preference`, `id_employee`, `id_tab`, `module`)
  308. VALUES (NULL, '.(int)$this->id_employee.', '.(int)$value.', \''.pSQL($module).'\');');
  309. }
  310. die('OK');
  311. }
  312. /*
  313. ** Get current URL
  314. **
  315. ** @param array $remove List of keys to remove from URL
  316. ** @return string
  317. */
  318. protected function getCurrentUrl($remove = array())
  319. {
  320. $url = $_SERVER['REQUEST_URI'];
  321. if (!$remove)
  322. return $url;
  323. if (!is_array($remove))
  324. $remove = array($remove);
  325. $url = preg_replace('#(?<=&|\?)('.implode('|', $remove).')=.*?(&|$)#i', '', $url);
  326. $len = strlen($url);
  327. if ($url[$len - 1] == '&')
  328. $url = substr($url, 0, $len - 1);
  329. return $url;
  330. }
  331. protected function extractArchive($file, $redirect = true)
  332. {
  333. $zip_folders = array();
  334. $tmp_folder = _PS_MODULE_DIR_.md5(time());
  335. $success = false;
  336. if (substr($file, -4) == '.zip')
  337. {
  338. if (Tools::ZipExtract($file, $tmp_folder))
  339. {
  340. $zip_folders = scandir($tmp_folder);
  341. if (Tools::ZipExtract($file, _PS_MODULE_DIR_))
  342. $success = true;
  343. }
  344. }
  345. else
  346. {
  347. require_once(_PS_TOOL_DIR_.'tar/Archive_Tar.php');
  348. $archive = new Archive_Tar($file);
  349. if ($archive->extract($tmp_folder))
  350. {
  351. $zip_folders = scandir($tmp_folder);
  352. if ($archive->extract(_PS_MODULE_DIR_))
  353. $success = true;
  354. }
  355. }
  356. if (!$success)
  357. $this->errors[] = Tools::displayError('There was an error while extracting the module (file may be corrupted).');
  358. else
  359. {
  360. //check if it's a real module
  361. foreach($zip_folders as $folder)
  362. if (!in_array($folder, array('.', '..', '.svn', '.git', '__MACOSX')) && !Module::getInstanceByName($folder))
  363. {
  364. $this->errors[] = sprintf(Tools::displayError('The module %1$s that you uploaded is not a valid module.'), $folder);
  365. $this->recursiveDeleteOnDisk(_PS_MODULE_DIR_.$folder);
  366. }
  367. }
  368. @unlink($file);
  369. $this->recursiveDeleteOnDisk($tmp_folder);
  370. if ($success && $redirect)
  371. Tools::redirectAdmin(self::$currentIndex.'&conf=8&anchor='.ucfirst($folder).'&token='.$this->token);
  372. return $success;
  373. }
  374. protected function recursiveDeleteOnDisk($dir)
  375. {
  376. if (strpos(realpath($dir), realpath(_PS_MODULE_DIR_)) === false)
  377. return;
  378. if (is_dir($dir))
  379. {
  380. $objects = scandir($dir);
  381. foreach ($objects as $object)
  382. if ($object != '.' && $object != '..')
  383. {
  384. if (filetype($dir.'/'.$object) == 'dir')
  385. $this->recursiveDeleteOnDisk($dir.'/'.$object);
  386. else
  387. unlink($dir.'/'.$object);
  388. }
  389. reset($objects);
  390. rmdir($dir);
  391. }
  392. }
  393. /*
  394. ** Filter Configuration Methods
  395. ** Set and reset filter configuration
  396. */
  397. protected function setFilterModules($module_type, $country_module_value, $module_install, $module_status)
  398. {
  399. Configuration::updateValue('PS_SHOW_TYPE_MODULES_'.(int)$this->id_employee, $module_type);
  400. Configuration::updateValue('PS_SHOW_COUNTRY_MODULES_'.(int)$this->id_employee, $country_module_value);
  401. Configuration::updateValue('PS_SHOW_INSTALLED_MODULES_'.(int)$this->id_employee, $module_install);
  402. Configuration::updateValue('PS_SHOW_ENABLED_MODULES_'.(int)$this->id_employee, $module_status);
  403. }
  404. protected function resetFilterModules()
  405. {
  406. Configuration::updateValue('PS_SHOW_TYPE_MODULES_'.(int)$this->id_employee, 'allModules');
  407. Configuration::updateValue('PS_SHOW_COUNTRY_MODULES_'.(int)$this->id_employee, 0);
  408. Configuration::updateValue('PS_SHOW_INSTALLED_MODULES_'.(int)$this->id_employee, 'installedUninstalled');
  409. Configuration::updateValue('PS_SHOW_ENABLED_MODULES_'.(int)$this->id_employee, 'enabledDisabled');
  410. Configuration::updateValue('PS_SHOW_CAT_MODULES_'.(int)$this->id_employee, '');
  411. }
  412. /*
  413. ** Post Process Filter
  414. **
  415. */
  416. public function postProcessFilterModules()
  417. {
  418. $this->setFilterModules(Tools::getValue('module_type'), Tools::getValue('country_module_value'), Tools::getValue('module_install'), Tools::getValue('module_status'));
  419. Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token);
  420. }
  421. public function postProcessResetFilterModules()
  422. {
  423. $this->resetFilterModules();
  424. Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token);
  425. }
  426. public function postProcessFilterCategory()
  427. {
  428. // Save configuration and redirect employee
  429. Configuration::updateValue('PS_SHOW_CAT_MODULES_'.(int)$this->id_employee, Tools::getValue('filterCategory'));
  430. Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token);
  431. }
  432. public function postProcessUnfilterCategory()
  433. {
  434. // Save configuration and redirect employee
  435. Configuration::updateValue('PS_SHOW_CAT_MODULES_'.(int)$this->id_employee, '');
  436. Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token);
  437. }
  438. /*
  439. ** Post Process Module CallBack
  440. **
  441. */
  442. public function postProcessReset()
  443. {
  444. if ($this->tabAccess['edit'] === '1')
  445. {
  446. $module = Module::getInstanceByName(Tools::getValue('module_name'));
  447. if (Validate::isLoadedObject($module))
  448. {
  449. if (!$module->getPermission('configure'))
  450. $this->errors[] = Tools::displayError('You do not have the permission to use this module.');
  451. else
  452. {
  453. if (Tools::getValue('keep_data') == '1' && method_exists($module, 'reset'))
  454. {
  455. if ($module->reset())
  456. Tools::redirectAdmin(self::$currentIndex.'&conf=21'.'&token='.$this->token.'&tab_module='.$module->tab.'&module_name='.$module->name.'&anchor='.ucfirst($module->name));
  457. else
  458. $this->errors[] = Tools::displayError('Cannot reset this module.');
  459. }
  460. else
  461. {
  462. if ($module->uninstall())
  463. if ($module->install())
  464. Tools::redirectAdmin(self::$currentIndex.'&conf=21'.'&token='.$this->token.'&tab_module='.$module->tab.'&module_name='.$module->name.'&anchor='.ucfirst($module->name));
  465. else
  466. $this->errors[] = Tools::displayError('Cannot install this module.');
  467. else
  468. $this->errors[] = Tools::displayError('Cannot uninstall this module.');
  469. }
  470. }
  471. }
  472. else
  473. $this->errors[] = Tools::displayError('Cannot load the module\'s object.');
  474. if (($errors = $module->getErrors()) && is_array($errors))
  475. $this->errors = array_merge($this->errors, $errors);
  476. }
  477. else
  478. $this->errors[] = Tools::displayError('You do not have permission to add this.');
  479. }
  480. public function postProcessDownload()
  481. {
  482. // PrestaShop demo mode
  483. if (_PS_MODE_DEMO_ || ($this->context->mode == Context::MODE_HOST))
  484. {
  485. $this->errors[] = Tools::displayError('This functionality has been disabled.');
  486. return;
  487. }
  488. // Try to upload and unarchive the module
  489. if ($this->tabAccess['add'] === '1')
  490. {
  491. // UPLOAD_ERR_OK: 0
  492. // UPLOAD_ERR_INI_SIZE: 1
  493. // UPLOAD_ERR_FORM_SIZE: 2
  494. // UPLOAD_ERR_NO_TMP_DIR: 6
  495. // UPLOAD_ERR_CANT_WRITE: 7
  496. // UPLOAD_ERR_EXTENSION: 8
  497. // UPLOAD_ERR_PARTIAL: 3
  498. if (isset($_FILES['file']['error']) && $_FILES['file']['error'] != UPLOAD_ERR_OK)
  499. switch($_FILES['file']['error']) {
  500. case UPLOAD_ERR_INI_SIZE:
  501. case UPLOAD_ERR_FORM_SIZE:
  502. $this->errors[] = sprintf($this->l('File too large (limit of %s bytes).'), Tools::getMaxUploadSize());
  503. break;
  504. case UPLOAD_ERR_PARTIAL:
  505. $this->errors[] = $this->l('File upload was not completed.');
  506. break;
  507. case UPLOAD_ERR_NO_FILE:
  508. $this->errors[] = $this->l('No file was uploaded.');
  509. break;
  510. default:
  511. $this->errors[] = sprintf($this->l('Internal error #%s'), $_FILES['newfile']['error']);
  512. break;
  513. }
  514. elseif (!isset($_FILES['file']['tmp_name']) || empty($_FILES['file']['tmp_name']))
  515. $this->errors[] = $this->l('No file has been selected');
  516. elseif (substr($_FILES['file']['name'], -4) != '.tar' && substr($_FILES['file']['name'], -4) != '.zip'
  517. && substr($_FILES['file']['name'], -4) != '.tgz' && substr($_FILES['file']['name'], -7) != '.tar.gz')
  518. $this->errors[] = Tools::displayError('Unknown archive type.');
  519. elseif (!move_uploaded_file($_FILES['file']['tmp_name'], _PS_MODULE_DIR_.$_FILES['file']['name']))
  520. $this->errors[] = Tools::displayError('An error occurred while copying archive to the module directory.');
  521. else
  522. $this->extractArchive(_PS_MODULE_DIR_.$_FILES['file']['name']);
  523. }
  524. else
  525. $this->errors[] = Tools::displayError('You do not have permission to add this.');
  526. }
  527. public function postProcessEnable()
  528. {
  529. if ($this->tabAccess['edit'] === '1')
  530. {
  531. $module = Module::getInstanceByName(Tools::getValue('module_name'));
  532. if (Validate::isLoadedObject($module))
  533. {
  534. if (!$module->getPermission('configure'))
  535. $this->errors[] = Tools::displayError('You do not have the permission to use this module.');
  536. else
  537. {
  538. if (Tools::getValue('enable'))
  539. $module->enable();
  540. else
  541. $module->disable();
  542. Tools::redirectAdmin($this->getCurrentUrl('enable'));
  543. }
  544. }
  545. else
  546. $this->errors[] = Tools::displayError('Cannot load the module\'s object.');
  547. }
  548. else
  549. $this->errors[] = Tools::displayError('You do not have permission to add this.');
  550. }
  551. public function postProcessEnable_Device()
  552. {
  553. if ($this->tabAccess['edit'] === '1')
  554. {
  555. $module = Module::getInstanceByName(Tools::getValue('module_name'));
  556. if (Validate::isLoadedObject($module))
  557. {
  558. if (!$module->getPermission('configure'))
  559. $this->errors[] = Tools::displayError('You do not have the permission to use this module.');
  560. else
  561. {
  562. $module->enableDevice((int)Tools::getValue('enable_device'));
  563. Tools::redirectAdmin($this->getCurrentUrl('enable_device'));
  564. }
  565. }
  566. else
  567. $this->errors[] = Tools::displayError('Cannot load the module\'s object.');
  568. }
  569. else
  570. $this->errors[] = Tools::displayError('You do not have permission to add this.');
  571. }
  572. public function postProcessDisable_Device()
  573. {
  574. if ($this->tabAccess['edit'] === '1')
  575. {
  576. $module = Module::getInstanceByName(Tools::getValue('module_name'));
  577. if (Validate::isLoadedObject($module))
  578. {
  579. if (!$module->getPermission('configure'))
  580. $this->errors[] = Tools::displayError('You do not have the permission to use this module.');
  581. else
  582. {
  583. $module->disableDevice((int)Tools::getValue('disable_device'));
  584. Tools::redirectAdmin($this->getCurrentUrl('disable_device'));
  585. }
  586. }
  587. else
  588. $this->errors[] = Tools::displayError('Cannot load the module\'s object.');
  589. }
  590. else
  591. $this->errors[] = Tools::displayError('You do not have permission to add this.');
  592. }
  593. public function postProcessDelete()
  594. {
  595. // PrestaShop demo mode
  596. if (_PS_MODE_DEMO_)
  597. {
  598. $this->errors[] = Tools::displayError('This functionality has been disabled.');
  599. return;
  600. }
  601. if ($this->tabAccess['delete'] === '1')
  602. {
  603. if (Tools::getValue('module_name') != '')
  604. {
  605. $module = Module::getInstanceByName(Tools::getValue('module_name'));
  606. if (Validate::isLoadedObject($module) && !$module->getPermission('configure'))
  607. $this->errors[] = Tools::displayError('You do not have the permission to use this module.');
  608. else
  609. {
  610. // Uninstall the module before deleting the files, but do not block the process if uninstall returns false
  611. if (Module::isInstalled($module->name))
  612. $module->uninstall();
  613. $moduleDir = _PS_MODULE_DIR_.str_replace(array('.', '/', '\\'), array('', '', ''), Tools::getValue('module_name'));
  614. $this->recursiveDeleteOnDisk($moduleDir);
  615. if (!file_exists($moduleDir))
  616. Tools::redirectAdmin(self::$currentIndex.'&conf=22&token='.$this->token.'&tab_module='.Tools::getValue('tab_module').'&module_name='.Tools::getValue('module_name'));
  617. else
  618. $this->errors[] = Tools::displayError('Sorry, the module cannot be deleted. Please check if you have the right permissions on this folder.');
  619. }
  620. }
  621. }
  622. else
  623. $this->errors[] = Tools::displayError('You do not have permission to delete this.');
  624. }
  625. public function postProcessCallback()
  626. {
  627. $return = false;
  628. $installed_modules = array();
  629. foreach ($this->map as $key => $method)
  630. {
  631. if (!Tools::getValue($key))
  632. continue;
  633. // PrestaShop demo mode
  634. if (_PS_MODE_DEMO_)
  635. {
  636. $this->errors[] = Tools::displayError('This functionality has been disabled.');
  637. return;
  638. }
  639. if ($key == 'check')
  640. $this->ajaxProcessRefreshModuleList(true);
  641. elseif ($key == 'checkAndUpdate')
  642. {
  643. $modules = array();
  644. $this->ajaxProcessRefreshModuleList(true);
  645. $modules_on_disk = Module::getModulesOnDisk(true, $this->logged_on_addons, $this->id_employee);
  646. // Browse modules list
  647. foreach ($modules_on_disk as $km => $module_on_disk)
  648. {
  649. if (!Tools::getValue('module_name') && isset($module_on_disk->version_addons) && $module_on_disk->version_addons)
  650. $modules[] = $module_on_disk->name;
  651. }
  652. if (!Tools::getValue('module_name'))
  653. $modules_list_save = implode('|', $modules);
  654. }
  655. elseif (($modules = Tools::getValue($key)) && $key != 'checkAndUpdate')
  656. {
  657. if (strpos($modules, '|'))
  658. {
  659. $modules_list_save = $modules;
  660. $modules = explode('|', $modules);
  661. }
  662. if (!is_array($modules))
  663. $modules = (array)$modules;
  664. }
  665. $module_upgraded = array();
  666. $module_errors = array();
  667. if (isset($modules))
  668. foreach ($modules as $name)
  669. {
  670. $module_to_update = array();
  671. $module_to_update[$name] = null;
  672. $full_report = null;
  673. // If Addons module, download and unzip it before installing it
  674. if (!file_exists(_PS_MODULE_DIR_.$name.'/'.$name.'.php') || $key == 'update')
  675. {
  676. $filesList = array(
  677. array('type' => 'addonsNative', 'file' => Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, 'loggedOnAddons' => 0),
  678. array('type' => 'addonsBought', 'file' => Module::CACHE_FILE_CUSTOMER_MODULES_LIST, 'loggedOnAddons' => 1),
  679. );
  680. foreach ($filesList as $f)
  681. {
  682. if (file_exists(_PS_ROOT_DIR_.$f['file']))
  683. {
  684. $file = $f['file'];
  685. $content = Tools::file_get_contents(_PS_ROOT_DIR_.$file);
  686. if ($xml = @simplexml_load_string($content, null, LIBXML_NOCDATA))
  687. foreach ($xml->module as $modaddons)
  688. {
  689. if ($name == $modaddons->name)
  690. {
  691. $module_to_update[$name]['id'] = $modaddons->id;
  692. $module_to_update[$name]['displayName'] = $modaddons->displayName;
  693. $module_to_update[$name]['need_loggedOnAddons'] = $f['loggedOnAddons'];
  694. }
  695. }
  696. }
  697. }
  698. foreach ($module_to_update as $name => $attr)
  699. {
  700. if ((is_null($attr) && $this->logged_on_addons == 0) || ($attr['need_loggedOnAddons'] == 1 && $this->logged_on_addons == 0))
  701. $this->errors[] = sprintf(Tools::displayError('You need to be logged in to your PrestaShop Addons account in order to update the %s module. %s'), '<strong>'.$name.'</strong>', '<a href="#" class="addons_connect" data-toggle="modal" data-target="#modal_addons_connect" title="Addons">'.$this->l('Click here to log in.').'</a>');
  702. elseif (!is_null($attr['id']))
  703. {
  704. $download_ok = false;
  705. if ($attr['need_loggedOnAddons'] == 0 && file_put_contents(_PS_MODULE_DIR_.$name.'.zip', Tools::addonsRequest('module', array('id_module' => pSQL($attr['id'])))))
  706. $download_ok = true;
  707. elseif ($attr['need_loggedOnAddons'] == 1 && $this->logged_on_addons && file_put_contents(_PS_MODULE_DIR_.$name.'.zip', Tools::addonsRequest('module', array('id_module' => pSQL($attr['id']), 'username_addons' => pSQL(trim($this->context->cookie->username_addons)), 'password_addons' => pSQL(trim($this->context->cookie->password_addons))))))
  708. $download_ok = true;
  709. if (!$download_ok)
  710. $this->errors[] = sprintf(Tools::displayError("Module %s cannot be upgraded: Error while downloading the latest version."), '<strong>'.$attr['displayName'].'</strong>');
  711. elseif (!$this->extractArchive(_PS_MODULE_DIR_.$name.'.zip', false))
  712. $this->errors[] = sprintf(Tools::displayError("Module %s cannot be upgraded: Error while extracting the latest version"), '<strong>'.$attr['displayName'].'</strong>');
  713. else
  714. $module_upgraded[] = $name;
  715. }
  716. else
  717. $this->errors[] = sprintf(Tools::displayError("You do not have the rights to update the %s module. Please make sure you are logged in to the PrestaShop Addons account that purchased the module."), '<strong>'.$name.'</strong>');
  718. }
  719. }
  720. if (count($this->errors))
  721. continue;
  722. // Check potential error
  723. if (!($module = Module::getInstanceByName(urldecode($name))))
  724. $this->errors[] = $this->l('Module not found');
  725. elseif (($this->context->mode >= Context::MODE_HOST_CONTRIB) && in_array($module->name, Module::$hosted_modules_blacklist))
  726. $this->errors[] = Tools::displayError('You do not have permission to access this module.');
  727. elseif ($key == 'install' && $this->tabAccess['add'] !== '1')
  728. $this->errors[] = Tools::displayError('You do not have permission to install this module.');
  729. elseif ($key == 'install' && ($this->context->mode == Context::MODE_HOST) && !Module::isModuleTrusted($module->name))
  730. $this->errors[] = Tools::displayError('You do not have permission to install this module.');
  731. elseif ($key == 'delete' && ($this->tabAccess['delete'] !== '1' || !$module->getPermission('configure')))
  732. $this->errors[] = Tools::displayError('You do not have permission to delete this module.');
  733. elseif ($key == 'configure' && ($this->tabAccess['edit'] !== '1' || !$module->getPermission('configure') || !Module::isInstalled(urldecode($name))))
  734. $this->errors[] = Tools::displayError('You do not have permission to configure this module.');
  735. elseif ($key == 'install' && Module::isInstalled($module->name))
  736. $this->errors[] = Tools::displayError('This module is already installed:').' '.$module->name;
  737. elseif ($key == 'uninstall' && !Module::isInstalled($module->name))
  738. $this->errors[] = Tools::displayError('This module has already been uninstalled:').' '.$module->name;
  739. elseif ($key == 'update' && !Module::isInstalled($module->name))
  740. $this->errors[] = Tools::displayError('This module needs to be installed in order to be updated:').' '.$module->name;
  741. else
  742. {
  743. // If we install a module, force temporary global context for multishop
  744. if (Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_ALL && $method != 'getContent')
  745. {
  746. $shop_id = (int)Context::getContext()->shop->id;
  747. Context::getContext()->tmpOldShop = clone(Context::getContext()->shop);
  748. if ($shop_id)
  749. Context::getContext()->shop = new Shop($shop_id);
  750. }
  751. //retrocompatibility
  752. if (Tools::getValue('controller') != '')
  753. $_POST['tab'] = Tools::safeOutput(Tools::getValue('controller'));
  754. $echo = '';
  755. if ($key != 'update' && $key != 'checkAndUpdate')
  756. {
  757. // We check if method of module exists
  758. if (!method_exists($module, $method))
  759. throw new PrestaShopException('Method of module cannot be found');
  760. if ($key == 'uninstall' && !Module::getPermissionStatic($module->id, 'uninstall'))
  761. $this->errors[] = Tools::displayError('You do not have permission to uninstall this module.');
  762. if (count($this->errors))
  763. continue;
  764. // Get the return value of current method
  765. $echo = $module->{$method}();
  766. // After a successful install of a single module that has a configuration method, to the configuration page
  767. if ($key == 'install' && $echo === true && strpos(Tools::getValue('install'), '|') === false && method_exists($module, 'getContent'))
  768. Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token.'&configure='.$module->name.'&conf=12');
  769. }
  770. // If the method called is "configure" (getContent method), we show the html code of configure page
  771. if ($key == 'configure' && Module::isInstalled($module->name))
  772. {
  773. $this->bootstrap = (isset($module->bootstrap) && $module->bootstrap);
  774. if (isset($module->multishop_context))
  775. $this->multishop_context = $module->multishop_context;
  776. $back_link = self::$currentIndex.'&token='.$this->token.'&tab_module='.$module->tab.'&module_name='.$module->name;
  777. $hook_link = 'index.php?tab=AdminModulesPositions&token='.Tools::getAdminTokenLite('AdminModulesPositions').'&show_modules='.(int)$module->id;
  778. $trad_link = 'index.php?tab=AdminTranslations&token='.Tools::getAdminTokenLite('AdminTranslations').'&type=modules&lang=';
  779. $disable_link = $this->context->link->getAdminLink('AdminModules').'&module_name='.$module->name.'&enable=0&tab_module='.$module->tab;
  780. $uninstall_link = $this->context->link->getAdminLink('AdminModules').'&module_name='.$module->name.'&uninstall='.$module->name.'&tab_module='.$module->tab;
  781. $reset_link = $this->context->link->getAdminLink('AdminModules').'&module_name='.$module->name.'&reset&tab_module='.$module->tab;
  782. $update_link = $this->context->link->getAdminLink('AdminModules').'&checkAndUpdate=1&module_name='.$module->name;
  783. $is_reset_ready = false;
  784. if (method_exists($module, 'reset'))
  785. $is_reset_ready = true;
  786. $this->context->smarty->assign(
  787. array(
  788. 'module_name' => $module->name,
  789. 'module_display_name' => $module->displayName,
  790. 'back_link' => $back_link,
  791. 'module_hook_link' => $hook_link,
  792. 'module_disable_link' => $disable_link,
  793. 'module_uninstall_link' => $uninstall_link,
  794. 'module_reset_link' => $reset_link,
  795. 'module_update_link' => $update_link,
  796. 'trad_link' => $trad_link,
  797. 'module_languages' => Language::getLanguages(false),
  798. 'theme_language_dir' => _THEME_LANG_DIR_,
  799. 'page_header_toolbar_title' => $this->page_header_toolbar_title,
  800. 'page_header_toolbar_btn' => $this->page_header_toolbar_btn,
  801. 'add_permission' => $this->tabAccess['add'],
  802. 'is_reset_ready' => $is_reset_ready,
  803. )
  804. );
  805. // Display checkbox in toolbar if multishop
  806. if (Shop::isFeatureActive())
  807. {
  808. if (Shop::getContext() == Shop::CONTEXT_SHOP)
  809. $shop_context = 'shop <strong>'.$this->context->shop->name.'</strong>';
  810. elseif (Shop::getContext() == Shop::CONTEXT_GROUP)
  811. {
  812. $shop_group = new ShopGroup((int)Shop::getContextShopGroupID());
  813. $shop_context = 'all shops of group shop <strong>'.$shop_group->name.'</strong>';
  814. }
  815. else
  816. $shop_context = 'all shops';
  817. $this->context->smarty->assign(array(
  818. 'module' => $module,
  819. 'display_multishop_checkbox' => true,
  820. 'current_url' => $this->getCurrentUrl('enable'),
  821. 'shop_context' => $shop_context,
  822. ));
  823. }
  824. $this->context->smarty->assign(array(
  825. 'shop_list' => Helper::renderShopList(),
  826. 'is_multishop' => Shop::isFeatureActive(),
  827. 'multishop_context' => Shop::CONTEXT_ALL | Shop::CONTEXT_GROUP | Shop::CONTEXT_SHOP
  828. ));
  829. if (Shop::isFeatureActive() && isset(Context::getContext()->tmpOldShop))
  830. {
  831. Context::getContext()->shop = clone(Context::getContext()->tmpOldShop);
  832. unset(Context::getContext()->tmpOldShop);
  833. }
  834. // Display module configuration
  835. $header = $this->context->smarty->fetch('controllers/modules/configure.tpl');
  836. $configuration_bar = $this->context->smarty->fetch('controllers/modules/configuration_bar.tpl');
  837. $output = $header.$echo;
  838. if (isset($this->_modules_ad[$module->name]))
  839. {
  840. $ad_modules = $this->getModulesByInstallation($this->_modules_ad[$module->name]);
  841. foreach ($ad_modules['not_installed'] as $key => &$module)
  842. {
  843. if (isset($module->addons_buy_url))
  844. $module->addons_buy_url = str_replace('utm_source=v1trunk_api', 'utm_source=back-office', $module->addons_buy_url)
  845. .'&utm_medium=related-modules&utm_campaign=back-office-'.strtoupper($this->context->language->iso_code)
  846. .'&utm_content='.(($this->context->mode >= Context::MODE_HOST_CONTRIB) ? 'cloud' : 'download');
  847. if (isset($module->description_full) && trim($module->description_full) != '')
  848. $module->show_quick_view = true;
  849. }
  850. $this->context->smarty->assign(array(
  851. 'ad_modules' => $ad_modules,
  852. 'currentIndex' => self::$currentIndex
  853. ));
  854. $ad_bar = $this->context->smarty->fetch('controllers/modules/ad_bar.tpl');
  855. $output .= $ad_bar;
  856. }
  857. $this->context->smarty->assign('module_content', $output.$configuration_bar );
  858. }
  859. elseif ($echo === true)
  860. {
  861. $return = 13;
  862. if ($method == 'install')
  863. {
  864. $return = 12;
  865. $installed_modules[] = $module->id;
  866. }
  867. }
  868. elseif ($echo === false)
  869. $module_errors[] = array('name' => $name, 'message' => $module->getErrors());
  870. if (Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_ALL && isset(Context::getContext()->tmpOldShop))
  871. {
  872. Context::getContext()->shop = clone(Context::getContext()->tmpOldShop);
  873. unset(Context::getContext()->tmpOldShop);
  874. }
  875. }
  876. if ($key != 'configure' && Tools::getIsset('bpay'))
  877. Tools::redirectAdmin('index.php?tab=AdminPayment&token='.Tools::getAdminToken('AdminPayment'.(int)(Tab::getIdFromClassName('AdminPayment')).(int)$this->id_employee));
  878. }
  879. if (count($module_errors))
  880. {
  881. // If error during module installation, no redirection
  882. $html_error = $this->generateHtmlMessage($module_errors);
  883. if ($key == 'uninstall' )
  884. $this->errors[] = sprintf(Tools::displayError('The following module(s) could not be uninstalled properly: %s'), $html_error);
  885. else
  886. $this->errors[] = sprintf(Tools::displayError('The following module(s) could not be installed properly: %s'), $html_error);
  887. $this->context->smarty->assign('error_module', 'true');
  888. }
  889. }
  890. if ($return)
  891. {
  892. $params = (count($installed_modules)) ? '&installed_modules='.implode('|', $installed_modules) : '';
  893. // If redirect parameter is present and module installed with success, we redirect on configuration module page
  894. if (Tools::getValue('redirect') == 'config' && Tools::getValue('module_name') != '' && $return == '12' && Module::isInstalled(pSQL(Tools::getValue('module_name'))))
  895. Tools::redirectAdmin('index.php?controller=adminmodules&configure='.Tools::getValue('module_name').'&token='.Tools::getValue('token').'&module_name='.Tools::getValue('module_name').$params);
  896. Tools::redirectAdmin(self::$currentIndex.'&conf='.$return.'&token='.$this->token.'&tab_module='.$module->tab.'&module_name='.$module->name.'&anchor='.ucfirst($module->name).(isset($modules_list_save) ? '&modules_list='.$modules_list_save : '').$params);
  897. }
  898. if (Tools::getValue('update') || Tools::getValue('checkAndUpdate'))
  899. {
  900. $updated = '&updated=1';
  901. if (Tools::getValue('checkAndUpdate'))
  902. {
  903. $updated = '&check=1';
  904. if (Tools::getValue('module_name'))
  905. {
  906. $module = Module::getInstanceByName(Tools::getValue('module_name'));
  907. if (!Validate::isLoadedObject($module))
  908. unset($module);
  909. }
  910. }
  911. $module_upgraded = implode('|', $module_upgraded);
  912. if (isset($module_upgraded) && $module_upgraded != '')
  913. Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token.'&updated=1&module_name='.$module_upgraded);
  914. elseif (isset($modules_list_save))
  915. Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token.'&updated=1&module_name='.$modules_list_save);
  916. elseif (isset($module))
  917. Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token.$updated.'&tab_module='.$module->tab.'&module_name='.$module->name.'&anchor='.ucfirst($module->name).(isset($modules_list_save) ? '&modules_list='.$modules_list_save : ''));
  918. }
  919. }
  920. protected function getModulesByInstallation($tab_modules_list = null)
  921. {
  922. $all_modules = Module::getModulesOnDisk(true, $this->logged_on_addons, $this->id_employee);
  923. $all_unik_modules = array();
  924. $modules_list = array('installed' =>array(), 'not_installed' => array());
  925. foreach ($all_modules as $mod)
  926. if (!isset($all_unik_modules[$mod->name]))
  927. $all_unik_modules[$mod->name] = $mod;
  928. $all_modules = $all_unik_modules;
  929. foreach($all_modules as $module)
  930. {
  931. if (!isset($tab_modules_list) || in_array($module->name, $tab_modules_list))
  932. {
  933. $perm = true;
  934. if ($module->id)
  935. $perm &= Module::getPermissionStatic($module->id, 'configure');
  936. else
  937. {
  938. $id_admin_module = Tab::getIdFromClassName('AdminModules');
  939. $access = Profile::getProfileAccess($this->context->employee->id_profile, $id_admin_module);
  940. if (!$access['edit'])
  941. $perm &= false;
  942. }
  943. if (in_array($module->name, $this->list_partners_modules))
  944. $module->type = 'addonsPartner';
  945. if ($perm)
  946. {
  947. $this->fillModuleData($module, 'array');
  948. if ($module->id)
  949. $modules_list['installed'][] = $module;
  950. else
  951. $modules_list['not_installed'][] = $module;
  952. }
  953. }
  954. }
  955. return $modules_list;
  956. }
  957. public function postProcess()
  958. {
  959. // Parent Post Process
  960. parent::postProcess();
  961. // Get the list of installed module ans prepare it for ajax call.
  962. if (($list = Tools::getValue('installed_modules')))
  963. Context::getContext()->smarty->assign('installed_modules', Tools::jsonEncode(explode('|', $list)));
  964. // If redirect parameter is present and module already installed, we redirect on configuration module page
  965. if (Tools::getValue('redirect') == 'config' && Tools::getValue('module_name') != '' && Module::isInstalled(pSQL(Tools::getValue('module_name'))))
  966. Tools::redirectAdmin('index.php?controller=adminmodules&configure='.Tools::getValue('module_name').'&token='.Tools::getValue('token').'&module_name='.Tools::getValue('module_name'));
  967. // Execute filter or callback methods
  968. $filterMethods = array('filterModules', 'resetFilterModules', 'filterCategory', 'unfilterCategory');
  969. $callbackMethods = array('reset', 'download', 'enable', 'delete', 'enable_device', 'disable_device');
  970. $postProcessMethodsList = array_merge((array)$filterMethods, (array)$callbackMethods);
  971. foreach ($postProcessMethodsList as $ppm)
  972. if (Tools::isSubmit($ppm))
  973. {
  974. $ppm = 'postProcess'.ucfirst($ppm);
  975. if (method_exists($this, $ppm))
  976. $ppmReturn = $this->$ppm();
  977. }
  978. // Call appropriate module callback
  979. if (!isset($ppmReturn))
  980. $this->postProcessCallback();
  981. if ($back = Tools::getValue('back'))
  982. Tools::redirectAdmin($back);
  983. }
  984. /**
  985. * Generate html errors for a module process
  986. *
  987. * @param $module_errors
  988. * @return string
  989. */
  990. protected function generateHtmlMessage($module_errors)
  991. {
  992. $html_error = '';
  993. if (count($module_errors))
  994. {
  995. $html_error = '<ul>';
  996. foreach ($module_errors as $module_error)
  997. {
  998. $html_error_description = '';
  999. if (count($module_error['message']) > 0)
  1000. foreach ($module_error['message'] as $e)
  1001. $html_error_description .= '<br />&nbsp;&nbsp;&nbsp;&nbsp;'.$e;
  1002. $html_error .= '<li><b>'.$module_error['name'].'</b> : '.$html_error_description.'</li>';
  1003. }
  1004. $html_error .= '</ul>';
  1005. }
  1006. return $html_error;
  1007. }
  1008. public function initModulesList(&$modules)
  1009. {
  1010. foreach ($modules as $k => $module)
  1011. {
  1012. // Check add permissions, if add permissions not set, addons modules and uninstalled modules will not be displayed
  1013. if ($this->tabAccess['add'] !== '1' && isset($module->type) && ($module->type != 'addonsNative' || $module->type != 'addonsBought'))
  1014. unset($modules[$k]);
  1015. elseif ($this->tabAccess['add'] !== '1' && (!isset($module->id) || $module->id < 1))
  1016. unset($modules[$k]);
  1017. elseif ($module->id && !Module::getPermissionStatic($module->id, 'view') && !Module::getPermissionStatic($module->id, 'configure'))
  1018. unset($modules[$k]);
  1019. else
  1020. {
  1021. // Init serial and modules author list
  1022. if (!in_array($module->name, $this->list_natives_modules))
  1023. $this->serial_modules .= $module->name.' '.$module->version.'-'.($module->active ? 'a' : 'i')."\n";
  1024. $module_author = $module->author;
  1025. if (!empty($module_author) && ($module_author != ''))
  1026. $this->modules_authors[strtolower($module_author)] = 'notselected';
  1027. }
  1028. }
  1029. $this->serial_modules = urlencode($this->serial_modules);
  1030. }
  1031. public function makeModulesStats($module)
  1032. {
  1033. // Count Installed Modules
  1034. if (isset($module->id) && $module->id > 0)
  1035. $this->nb_modules_installed++;
  1036. // Count Activated Modules
  1037. if (isset($module->id) && $module->id > 0 && $module->active > 0)
  1038. $this->nb_modules_activated++;
  1039. // Count Modules By Category
  1040. if (isset($this->list_modules_categories[$module->tab]['nb']))
  1041. $this->list_modules_categories[$module->tab]['nb']++;
  1042. else
  1043. $this->list_modules_categories['others']['nb']++;
  1044. }
  1045. public function isModuleFiltered($module)
  1046. {
  1047. // If action on module, we display it
  1048. if (Tools::getValue('module_name') != '' && Tools::getValue('module_name') == $module->name)
  1049. return false;
  1050. // Filter on module name
  1051. $filter_name = Tools::getValue('filtername');
  1052. if (!empty($filter_name))
  1053. {
  1054. if (stristr($module->name, $filter_name) === false && stristr($module->displayName, $filter_name) === false && stristr($module->description, $filter_name) === false)
  1055. return true;
  1056. return false;
  1057. }
  1058. // Filter on interest
  1059. if ($module->interest !== '')
  1060. {
  1061. if ($module->interest === '0')
  1062. return true;
  1063. }
  1064. elseif ((int)Db::getInstance()->getValue('SELECT `id_module_preference` FROM `'._DB_PREFIX_.'module_preference` WHERE `module` = \''.pSQL($module->name).'\' AND `id_employee` = '.(int)$this->id_employee.' AND `interest` = 0') > 0)
  1065. return true;
  1066. // Filter on favorites
  1067. if (Configuration::get('PS_SHOW_CAT_MODULES_'.(int)$this->id_employee) == 'favorites')
  1068. {
  1069. if ((int)Db::getInstance()->getValue('SELECT `id_module_preference` FROM `'._DB_PREFIX_.'module_preference` WHERE `module` = \''.pSQL($module->name).'\' AND `id_employee` = '.(int)$this->id_employee.' AND `favorite` = 1 AND (`interest` = 1 OR `interest` IS NULL)') < 1)
  1070. return true;
  1071. }
  1072. else
  1073. {
  1074. // Handle "others" category
  1075. if (!isset($this->list_modules_categories[$module->tab]))
  1076. $module->tab = 'others';
  1077. // Filter on module category
  1078. $categoryFiltered = array();
  1079. $filterCategories = explode('|', Configuration::get('PS_SHOW_CAT_MODULES_'.(int)$this->id_employee));
  1080. if (count($filterCategories) > 0)
  1081. foreach ($filterCategories as $fc)
  1082. if (!empty($fc))
  1083. $categoryFiltered[$fc] = 1;
  1084. if (count($categoryFiltered) > 0 && !isset($categoryFiltered[$module->tab]))
  1085. return true;
  1086. }
  1087. // Filter on module type and author
  1088. $show_type_modules = $this->filter_configuration['PS_SHOW_TYPE_MODULES_'.(int)$this->id_employee];
  1089. if ($show_type_modules == 'nativeModules' && !in_array($module->name, $this->list_natives_modules))
  1090. return true;
  1091. elseif ($show_type_modules == 'partnerModules' && !in_array($module->name, $this->list_partners_modules))
  1092. return true;
  1093. elseif ($show_type_modules == 'addonsModules' && (!isset($module->type) || $module->type != 'addonsBought'))
  1094. return true;
  1095. elseif ($show_type_modules == 'mustHaveModules' && (!isset($module->type) || $module->type != 'addonsMustHave'))
  1096. return true;
  1097. elseif ($show_type_modules == 'otherModules' && (in_array($module->name, $this->list_partners_modules) || in_array($module->name, $this->list_natives_modules)))
  1098. return true;
  1099. elseif (strpos($show_type_modules, 'authorModules[') !== false)
  1100. {
  1101. // setting selected author in authors set
  1102. $author_selected = substr(str_replace(array('authorModules[', "\'"), array('', "'"), $show_type_modules), 0, -1);
  1103. $this->modules_authors[$author_selected] = 'selected';
  1104. if (empty($module->author) || strtolower($module->author) != $author_selected)
  1105. return true;
  1106. }
  1107. // Filter on install status
  1108. $show_installed_modules = $this->filter_configuration['PS_SHOW_INSTALLED_MODULES_'.(int)$this->id_employee];
  1109. if ($show_installed_modules == 'installed' && !$module->id)
  1110. return true;
  1111. if ($show_installed_modules == 'uninstalled' && $module->id)
  1112. return true;
  1113. // Filter on active status
  1114. $show_enabled_modules = $this->filter_configuration['PS_SHOW_ENABLED_MODULES_'.(int)$this->id_employee];
  1115. if ($show_enabled_modules == 'enabled' && !$module->active)
  1116. return true;
  1117. if ($show_enabled_modules == 'disabled' && $module->active)
  1118. return true;
  1119. // Filter on country
  1120. $show_country_modules = $this->filter_configuration['PS_SHOW_COUNTRY_MODULES_'.(int)$this->id_employee];
  1121. if ($show_country_modules && (isset($module->limited_countries) && !empty($module->limit

Large files files are truncated, but you can click here to view the full file