PageRenderTime 65ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/admin/AdminModulesController.php

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

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