PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/admin444/tabs/AdminModules.php

http://marocmall.googlecode.com/
PHP | 377 lines | 341 code | 20 blank | 16 comment | 50 complexity | 7553c63c68e346cc542452918d44ed08 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Modules tab for admin panel, AdminModules.php
  4. * @category admin
  5. *
  6. * @author PrestaShop <support@prestashop.com>
  7. * @copyright PrestaShop
  8. * @license http://www.opensource.org/licenses/osl-3.0.php Open-source licence 3.0
  9. * @version 1.3
  10. *
  11. */
  12. include_once(PS_ADMIN_DIR.'/../classes/AdminTab.php');
  13. class AdminModules extends AdminTab
  14. {
  15. /** @var array map with $_GET keywords and their callback */
  16. private $map = array(
  17. 'install' => 'install',
  18. 'uninstall' => 'uninstall',
  19. 'configure' => 'getContent'
  20. );
  21. public function postProcess()
  22. {
  23. global $currentIndex, $cookie;
  24. if (Tools::isSubmit('all_module_send'))
  25. {
  26. if (Tools::getValue('all_module'))
  27. Configuration::updateValue('PS_SHOW_ALL_MODULES', 0);
  28. else
  29. Configuration::updateValue('PS_SHOW_ALL_MODULES', 1);
  30. }
  31. /* Automatically copy a module from external URL and unarchive it in the appropriated directory */
  32. if (Tools::isSubmit('active'))
  33. {
  34. if ($this->tabAccess['edit'] === '1')
  35. {
  36. $module = Module::getInstanceByName(strval(Tools::getValue('module_name')));
  37. if (Validate::isLoadedObject($module))
  38. {
  39. Db::getInstance()->Execute('
  40. UPDATE `'._DB_PREFIX_.'module`
  41. SET `active`= 1
  42. WHERE `name` = \''.pSQL(Tools::getValue('module_name')).'\'');
  43. Tools::redirectAdmin($currentIndex.'&conf=5'.'&token='.$this->token);
  44. } else
  45. $this->_errors[] = Tools::displayError('Cannot load module object');
  46. } else
  47. $this->_errors[] = Tools::displayError('You do not have permission to add anything here.');
  48. }
  49. elseif (Tools::isSubmit('desactive'))
  50. {
  51. if ($this->tabAccess['edit'] === '1')
  52. {
  53. $module = Module::getInstanceByName(Tools::getValue('module_name'));
  54. if (Validate::isLoadedObject($module))
  55. {
  56. Db::getInstance()->Execute('
  57. UPDATE `'._DB_PREFIX_.'module`
  58. SET `active`= 0
  59. WHERE `name` = \''.pSQL(Tools::getValue('module_name')).'\'');
  60. Tools::redirectAdmin($currentIndex.'&conf=5'.'&token='.$this->token);
  61. } else
  62. $this->_errors[] = Tools::displayError('Cannot load module object');
  63. } else
  64. $this->_errors[] = Tools::displayError('You do not have permission to add anything here.');
  65. }
  66. if (Tools::isSubmit('submitDownload'))
  67. {
  68. if ($this->tabAccess['add'] === '1')
  69. {
  70. if (Validate::isModuleUrl($url = Tools::getValue('url'), $this->_errors))
  71. {
  72. if (!@copy($url, _PS_MODULE_DIR_.basename($url)))
  73. $this->_errors[] = Tools::displayError('404 Module not found');
  74. else
  75. $this->extractArchive(_PS_MODULE_DIR_.basename($url));
  76. }
  77. }
  78. else
  79. $this->_errors[] = Tools::displayError('You do not have permission to add anything here.');
  80. }
  81. if (Tools::isSubmit('submitDownload2'))
  82. {
  83. if ($this->tabAccess['add'] === '1')
  84. {
  85. if (!isset($_FILES['file']['tmp_name']) OR empty($_FILES['file']['tmp_name']))
  86. $this->_errors[] = $this->l('no file selected');
  87. elseif (substr($_FILES['file']['name'], -4) != '.tar' AND substr($_FILES['file']['name'], -4) != '.zip' AND substr($_FILES['file']['name'], -4) != '.tgz' AND substr($_FILES['file']['name'], -7) != '.tar.gz')
  88. $this->_errors[] = Tools::displayError('unknown archive type');
  89. elseif (!@copy($_FILES['file']['tmp_name'], _PS_MODULE_DIR_.$_FILES['file']['name']))
  90. $this->_errors[] = Tools::displayError('an error occured while copying archive to module directory');
  91. else
  92. $this->extractArchive(_PS_MODULE_DIR_.$_FILES['file']['name']);
  93. }
  94. else
  95. $this->_errors[] = Tools::displayError('You do not have permission to add anything here.');
  96. }
  97. /* Call appropriate module callback */
  98. else
  99. {
  100. $return = false;
  101. foreach ($this->map as $key => $method)
  102. {
  103. $modules = Tools::getValue($key);
  104. if (strpos($modules, '|'))
  105. $modules = explode('|', $modules);
  106. else
  107. $modules = empty($modules) ? false : array($modules);
  108. $module_errors = array();
  109. if ($modules)
  110. foreach ($modules AS $name)
  111. {
  112. if (!($module = @Module::getInstanceByName(urldecode($name))))
  113. $this->_errors[] = $this->l('module not found');
  114. elseif ($key == 'install' AND $this->tabAccess['add'] !== '1')
  115. $this->_errors[] = Tools::displayError('You do not have permission to add anything here.');
  116. elseif ($key == 'uninstall' AND $this->tabAccess['delete'] !== '1')
  117. $this->_errors[] = Tools::displayError('You do not have permission to delete here.');
  118. elseif ($key == 'configure' AND $this->tabAccess['edit'] !== '1')
  119. $this->_errors[] = Tools::displayError('You do not have permission to edit anything here.');
  120. elseif (($echo = $module->{$method}()) AND ($key == 'configure') AND Module::isInstalled($module->name))
  121. {
  122. echo '
  123. <p><a href="'.$currentIndex.'&token='.$this->token.'"><img src="../img/admin/arrow2.gif" /> '.$this->l('Back to modules list').'</a></p>
  124. <br />'.$echo.'<br />
  125. <p><a href="'.$currentIndex.'&token='.$this->token.'"><img src="../img/admin/arrow2.gif" /> '.$this->l('Back to modules list').'</a></p>';
  126. }
  127. elseif($echo)
  128. $return = ($method == 'install' ? 12 : 13);
  129. elseif ($echo === false)
  130. $module_errors[] = $name;
  131. if ($key != 'configure' AND isset($_GET['bpay']))
  132. Tools::redirectAdmin('index.php?tab=AdminPayment&conf='.$return.'&token='.Tools::getAdminToken('AdminPayment'.intval(Tab::getIdFromClassName('AdminPayment')).intval($cookie->id_employee)));
  133. }
  134. if (sizeof($module_errors))
  135. {
  136. echo '<div class="alert">'.$this->l('The following module(s) were not installed successfully:').'<ul>';
  137. foreach ($module_errors AS $module_error)
  138. echo '<li>'.$module_error.'</li>';
  139. echo '</ul></div>';
  140. }
  141. }
  142. if ($return)
  143. Tools::redirectAdmin($currentIndex.'&conf='.$return.'&token='.$this->token);
  144. }
  145. }
  146. function extractArchive($file)
  147. {
  148. global $currentIndex;
  149. $success = false;
  150. if (substr($file, -4) == '.zip')
  151. {
  152. if (class_exists('ZipArchive', false))
  153. {
  154. $zip = new ZipArchive();
  155. if ($zip->open($file) === true AND $zip->extractTo(_PS_MODULE_DIR_) AND $zip->close())
  156. $success = true;
  157. else
  158. $this->_errors[] = Tools::displayError('error while extracting module (file may be corrupted)');
  159. }
  160. else
  161. $this->_errors[] = Tools::displayError('zip is not installed on your server. Ask your host for further information.');
  162. }
  163. else
  164. {
  165. $archive = new Archive_Tar($file);
  166. if ($archive->extract(_PS_MODULE_DIR_))
  167. $success = true;
  168. else
  169. $this->_errors[] = Tools::displayError('error while extracting module (file may be corrupted)');
  170. }
  171. @unlink($file);
  172. if ($success)
  173. Tools::redirectAdmin($currentIndex.'&conf=8'.'&token='.$this->token);
  174. }
  175. public function display()
  176. {
  177. if (!isset($_GET['configure']) OR sizeof($this->_errors))
  178. $this->displayList();
  179. }
  180. public function displayJavascript()
  181. {
  182. global $currentIndex;
  183. echo '
  184. <script type="text/javascript">
  185. function modules_management(action)
  186. {
  187. var modules = document.getElementsByName(\'modules\');
  188. var module_list = \'\';
  189. for (var i = 0; i < modules.length; i++)
  190. {
  191. if (modules[i].checked == true)
  192. {
  193. rel = modules[i].getAttribute(\'rel\');
  194. if (rel != "false" && action == "uninstall")
  195. {
  196. if (!confirm(rel))
  197. return false;
  198. }
  199. module_list += \'|\'+modules[i].value;
  200. }
  201. }
  202. document.location.href=\''.$currentIndex.'&token='.$this->token.'&\'+action+\'=\'+module_list.substring(1, module_list.length);
  203. }
  204. </script>';
  205. }
  206. public function displayList()
  207. {
  208. global $currentIndex, $cookie;
  209. $serialModules = '';
  210. $modules = Module::getModulesOnDisk();
  211. foreach ($modules AS $module)
  212. $serialModules .= $module->name.' '.$module->version.'-'.($module->active ? 'a' : 'i')."\n";
  213. $serialModules = urlencode($serialModules);
  214. $this->displayJavascript();
  215. $linkToSettings = 'index.php?tab=AdminPreferences&token='.Tools::getAdminToken('AdminPreferences'.intval(Tab::getIdFromClassName('AdminPreferences')).intval($cookie->id_employee));
  216. echo '<span onclick="openCloseLayer(\'module_install\', 0);" style="cursor: pointer;font-weight: 700; float: left;"><img src="../img/admin/add.gif" alt="'.$this->l('Add a new module').'" class="middle" /> '.$this->l('Add a new module').'</span>';
  217. if (Configuration::get('PRESTASTORE_LIVE') AND @ini_get('allow_url_fopen'))
  218. echo '<script type="text/javascript">
  219. function getPrestaStore(){if (getE("prestastore").style.display!=\'block\')return;$.post("'.dirname($currentIndex).'/ajax.php",{page:"prestastore"},function(a){getE("prestastore-content").innerHTML=a;})}
  220. </script>
  221. <span onclick="openCloseLayer(\'prestastore\', 0); getPrestaStore();" style="cursor: pointer;font-weight: 700; float: left;margin-left:20px;"><img src="../img/admin/prestastore.gif" class="middle" /> '.$this->l('PrestaShop Addons').'</span>&nbsp;(<a href="'.$linkToSettings.'">'.$this->l('disable').'</a>)';
  222. echo '
  223. <div class="clear">&nbsp;</div>
  224. <div id="module_install" style="float: left;'.((Tools::isSubmit('submitDownload') OR Tools::isSubmit('submitDownload2')) ? '' : 'display: none;').'" class="width1">
  225. <fieldset class="width2">
  226. <legend><img src="../img/admin/add.gif" alt="'.$this->l('Add a new module').'" class="middle" /> '.$this->l('Add a new module').'</legend>
  227. <p>'.$this->l('The module must be either a zip file or a tarball.').'</p>
  228. <hr />
  229. <form action="'.$currentIndex.'&token='.$this->token.'" method="post">
  230. <label style="width: 100px">'.$this->l('Module URL:').'</label>
  231. <div class="margin-form" style="padding-left: 140px">
  232. <input type="text" name="url" style="width: 200px;" value="'.(Tools::getValue('url') ? Tools::getValue('url') : 'http://').'" />
  233. <p>'.$this->l('Download the module directly from a website.').'</p>
  234. </div>
  235. <div class="margin-form" style="padding-left: 140px">
  236. <input type="submit" name="submitDownload" value="'.$this->l('Download this module').'" class="button" />
  237. </div>
  238. </form>
  239. <hr />
  240. <form action="'.$currentIndex.'&token='.$this->token.'" method="post" enctype="multipart/form-data">
  241. <label style="width: 100px">'.$this->l('Module file:').'</label>
  242. <div class="margin-form" style="padding-left: 140px">
  243. <input type="file" name="file" />
  244. <p>'.$this->l('Upload the module from your computer.').'</p>
  245. </div>
  246. <div class="margin-form" style="padding-left: 140px">
  247. <input type="submit" name="submitDownload2" value="'.$this->l('Upload this module').'" class="button" />
  248. </div>
  249. </form>
  250. </fieldset>
  251. </div>';
  252. if (Configuration::get('PRESTASTORE_LIVE'))
  253. echo '
  254. <div id="prestastore" style="margin-left:40px; display:none; float: left" class="width1">
  255. <fieldset>
  256. <legend><img src="http://addons.prestashop.com/modules.php?'.(isset($_SERVER['SERVER_ADDR']) ? 'server='.ip2long($_SERVER['SERVER_ADDR']).'&' : '').'mods='.$serialModules.'" class="middle" />'.$this->l('Live from PrestaShop Addons!').'</legend>
  257. <div id="prestastore-content"></div>
  258. </fieldset>
  259. </div>';
  260. echo '<div class="clear">&nbsp;</div>';
  261. $nameCountryDefault = Country::getNameById($cookie->id_lang, Configuration::get('PS_COUNTRY_DEFAULT'));
  262. $showAllModules = Configuration::get('PS_SHOW_ALL_MODULES');
  263. $isoCountryDefault = Country::getIsoById(Configuration::get('PS_COUNTRY_DEFAULT'));
  264. /* Scan modules directories and load modules classes */
  265. $warnings = array();
  266. $orderModule = array();
  267. $irow = 0;
  268. foreach ($modules AS $module)
  269. {
  270. if ($showAllModules || (!$showAllModules && ((isset($module->limited_countries) && in_array(strtolower($isoCountryDefault), $module->limited_countries)) || !isset($module->limited_countries))))
  271. $orderModule[(isset($module->tab) AND !empty($module->tab)) ? $module->tab : $this->l('Not specified')][] = $module;
  272. }
  273. asort($orderModule);
  274. foreach ($orderModule AS $tabModule)
  275. foreach ($tabModule AS $module)
  276. if ($module->active AND $module->warning)
  277. $this->displayWarning('<a href="'.$currentIndex.'&configure='.urlencode($module->name).'&token='.$this->token.'">'.$module->displayName.'</a> - '.stripslashes(pSQL($module->warning)));
  278. echo '
  279. <form method="POST" id="form_all_module" name="fomr_all_module" action="">
  280. <input type="hidden" name="all_module_send" value="" />
  281. <input type="checkbox" name="all_module" style="vertical-align: middle;" id="all_module" '.(!Configuration::get('PS_SHOW_ALL_MODULES') ? 'checked="checked"' : '').' onclick="document.getElementById(\'form_all_module\').submit();" />
  282. <label class="t" for="all_module">'.$this->l('Show only modules that can be used in my country').'</label> ('.$this->l('Current country:').' <a href="index.php?tab=AdminCountries&token='.Tools::getAdminToken('AdminCountries'.intval(Tab::getIdFromClassName('AdminCountries')).intval($cookie->id_employee)).'">'.$nameCountryDefault.'</a>)
  283. </form>';
  284. echo '
  285. <div style="float:left; width:300px;">';
  286. /* Browse modules by tab type */
  287. foreach ($orderModule AS $tab => $tabModule)
  288. {
  289. echo '<br />
  290. <table cellpadding="0" cellspacing="0" class="table width3">
  291. <tr>
  292. <th colspan="4" class="center" style="cursor: pointer" onclick="openCloseLayer(\''.addslashes($tab).'\');"><b>'.$tab.'</b> - <span style="color: red">'.sizeof($tabModule).'</span> '.((sizeof($tabModule) > 1) ? $this->l('modules') : $this->l('module')).'</th>
  293. </tr>
  294. </table>
  295. <div id="'.$tab.'" style="width:600px;">
  296. <table cellpadding="0" cellspacing="0" class="table width3">';
  297. /* Display modules for each tab type */
  298. foreach ($tabModule as $module)
  299. {
  300. if ($module->id)
  301. {
  302. $img = '<img src="../img/admin/enabled.gif" alt="'.$this->l('Module enabled').'" title="'.$this->l('Module enabled').'" />';
  303. if ($module->warning)
  304. $img = '<img src="../img/admin/warning.gif" alt="'.$this->l('Module installed but with warnings').'" title="'.$this->l('Module installed but with warnings').'" />';
  305. if (!$module->active)
  306. $img = '<img src="../img/admin/disabled.gif" alt="'.$this->l('Module disabled').'" title="'.$this->l('Module disabled').'" />';
  307. } else
  308. $img = '<img src="../img/admin/cog.gif" alt="'.$this->l('Module not installed').'" title="'.$this->l('Module not installed').'" />';
  309. echo '
  310. <tr'.($irow++ % 2 ? ' class="alt_row"' : '').' style="height: 42px;">
  311. <td style="padding-left: 10px;"><img src="../modules/'.$module->name.'/logo.gif" alt="" /> <b>'.stripslashes($module->displayName).'</b>'.($module->version ? ' v'.$module->version.(strpos($module->version, '.') !== false ? '' : '.0') : '').'<br />'.$module->description.'</td>
  312. <td width="85">'.(($module->id AND method_exists($module, 'getContent')) ? '<a href="'.$currentIndex.'&configure='.urlencode($module->name).'&token='.$this->token.'">&gt;&gt;&nbsp;'.$this->l('Configure').'</a>' : '').'</td>
  313. <td class="center" width="20">';
  314. if ($module->id)
  315. echo '<a href="'.$currentIndex.'&token='.$this->token.'&module_name='.$module->name.'&'.($module->active ? 'desactive' : 'active').'">';
  316. echo $img;
  317. if ($module->id)
  318. '</a>';
  319. echo '
  320. </td>
  321. <td class="center" width="80">'.((!$module->id)
  322. ? '<input type="button" class="button small" name="Install" value="'.$this->l('Install').'"
  323. onclick="javascript:document.location.href=\''.$currentIndex.'&install='.urlencode($module->name).'&token='.$this->token.'\'" />'
  324. : '<input type="button" class="button small" name="Uninstall" value="'.$this->l('Uninstall').'"
  325. onclick="'.(empty($module->confirmUninstall) ? '' : 'if(confirm(\''.addslashes($module->confirmUninstall).'\')) ').'document.location.href=\''.$currentIndex.'&uninstall='.urlencode($module->name).'&token='.$this->token.'\';" />').'</td>
  326. <td style="padding-right: 10px">
  327. <input type="checkbox" name="modules" value="'.urlencode($module->name).'" '.(empty($module->confirmUninstall) ? 'rel="false"' : 'rel="'.addslashes($module->confirmUninstall).'"').' />
  328. </td>
  329. </tr>';
  330. }
  331. echo '</table>
  332. </div>';
  333. }
  334. echo '
  335. <div style="margin-top: 12px; width:600px;" class="center">
  336. <input type="button" class="button small" value="'.$this->l('Install the selection').'" onclick="modules_management(\'install\')"/>
  337. <input type="button" class="button small" value="'.$this->l('Uninstall the selection').'" onclick="modules_management(\'uninstall\')" />
  338. </div>
  339. </div>
  340. <div style="float:right; width:300px;">
  341. <br />
  342. <table cellpadding="0" cellspacing="0" class="table width3" style="width:300px;"><tr><th colspan="4" class="center"><strong>'.$this->l('Icon legend').'</strong></th></tr></table>
  343. <table cellpadding="0" cellspacing="0" class="table width3" style="width:300px;"><tr style="height: 42px;">
  344. <td>
  345. <table cellpadding="10" cellspacing="5">
  346. <tr><td><img src="../img/admin/cog.gif" />&nbsp;&nbsp;'.$this->l('Module not installed').'</td></tr>
  347. <tr><td><img src="../img/admin/enabled.gif" />&nbsp;&nbsp;'.$this->l('Module installed and enabled').'</td></tr>
  348. <tr><td><img src="../img/admin/disabled.gif" />&nbsp;&nbsp;'.$this->l('Module installed but disabled').'</td></tr>
  349. <tr><td><img src="../img/admin/warning.gif" />&nbsp;&nbsp;'.$this->l('Module installed but some warnings').'</td></tr>
  350. </table>
  351. </td>
  352. </tr></table>
  353. </div>
  354. <div style="clear:both">&nbsp;</div>';
  355. }
  356. }
  357. ?>