modules.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. /* Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. * Copyright (C) 2010-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/admin/system/modules.php
  21. * \brief File to list all Dolibarr modules
  22. */
  23. require '../../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  25. if (empty($user->admin)) {
  26. accessforbidden();
  27. }
  28. // Load translation files required by the page
  29. $langs->loadLangs(array("install", "other", "admin"));
  30. $optioncss = GETPOST('optioncss', 'alpha');
  31. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'moduleoverview';
  32. $search_name = GETPOST("search_name", 'alpha');
  33. $search_id = GETPOST("search_id", 'alpha');
  34. $search_version = GETPOST("search_version", 'alpha');
  35. $search_permission = GETPOST("search_permission", 'alpha');
  36. $sortfield = GETPOST('sortfield', 'aZ09comma');
  37. $sortorder = GETPOST('sortorder', 'aZ09comma');
  38. if (!$sortfield) {
  39. $sortfield = "id";
  40. }
  41. if (!$sortorder) {
  42. $sortorder = "asc";
  43. }
  44. // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array of hooks
  45. $hookmanager->initHooks(array('moduleoverview'));
  46. $form = new Form($db);
  47. $object = new stdClass();
  48. // Definition of fields for lists
  49. $arrayfields = array(
  50. 'name'=>array('label'=>$langs->trans("Modules"), 'checked'=>1, 'position'=>10),
  51. 'version'=>array('label'=>$langs->trans("Version"), 'checked'=>1, 'position'=>20),
  52. 'id'=>array('label'=>$langs->trans("IdModule"), 'checked'=>1, 'position'=>30),
  53. 'module_position'=>array('label'=>$langs->trans("Position"), 'checked'=>1, 'position'=>35),
  54. 'permission'=>array('label'=>$langs->trans("IdPermissions"), 'checked'=>1, 'position'=>40)
  55. );
  56. $arrayfields = dol_sort_array($arrayfields, 'position');
  57. $param = '';
  58. /*
  59. * Actions
  60. */
  61. $parameters = array();
  62. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  63. if ($reshook < 0) {
  64. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  65. }
  66. if (empty($reshook)) {
  67. // Selection of new fields
  68. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  69. }
  70. // Load list of modules
  71. $moduleList = array();
  72. $modules = array();
  73. $modules_files = array();
  74. $modules_fullpath = array();
  75. $modulesdir = dolGetModulesDirs();
  76. $rights_ids = array();
  77. $arrayofpermissions = array();
  78. foreach ($modulesdir as $dir) {
  79. $handle = @opendir(dol_osencode($dir));
  80. if (is_resource($handle)) {
  81. while (($file = readdir($handle)) !== false) {
  82. if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
  83. $modName = substr($file, 0, dol_strlen($file) - 10);
  84. if ($modName) {
  85. //print 'xx'.$dir.$file.'<br>';
  86. if (in_array($file, $modules_files)) {
  87. // File duplicate
  88. print "Warning duplicate file found : ".$file." (Found ".$dir.$file.", already found ".$modules_fullpath[$file].")<br>";
  89. } else {
  90. // File to load
  91. $res = include_once $dir.$file;
  92. if (class_exists($modName)) {
  93. try {
  94. $objMod = new $modName($db);
  95. $modules[$objMod->numero] = $objMod;
  96. $modules_files[$objMod->numero] = $file;
  97. $modules_fullpath[$file] = $dir.$file;
  98. } catch (Exception $e) {
  99. dol_syslog("Failed to load ".$dir.$file." ".$e->getMessage(), LOG_ERR);
  100. }
  101. } else {
  102. print "Warning bad descriptor file : ".$dir.$file." (Class ".$modName." not found into file)<br>";
  103. }
  104. }
  105. }
  106. }
  107. }
  108. closedir($handle);
  109. }
  110. }
  111. // create pre-filtered list for modules
  112. foreach ($modules as $key => $module) {
  113. $newModule = new stdClass();
  114. $newModule->name = $module->getName();
  115. $newModule->version = $module->getVersion();
  116. $newModule->id = $key;
  117. $newModule->module_position = $module->getModulePosition();
  118. $alt = $module->name.' - '.$modules_files[$key];
  119. if (!empty($module->picto)) {
  120. if (preg_match('/^\//', $module->picto)) {
  121. $newModule->picto = img_picto($alt, $module->picto, 'width="14px"', 1);
  122. } else {
  123. $newModule->picto = img_object($alt, $module->picto, 'width="14px"');
  124. }
  125. } else {
  126. $newModule->picto = img_object($alt, 'generic', 'width="14px"');
  127. }
  128. $permission = array();
  129. if ($module->rights) {
  130. foreach ($module->rights as $rights) {
  131. if (empty($rights[0])) {
  132. continue;
  133. }
  134. $arrayofpermissions[$rights[0]] = array('label'=> 'user->rights->'.$module->rights_class.'->'.$rights[4].(empty($rights[5]) ? '' : '->'.$rights[5]));
  135. $permission[] = $rights[0];
  136. array_push($rights_ids, $rights[0]);
  137. }
  138. }
  139. $newModule->permission = $permission;
  140. // pre-filter list
  141. if (!empty($search_name) && !stristr($newModule->name, $search_name)) {
  142. continue;
  143. }
  144. if (!empty($search_version) && !stristr($newModule->version, $search_version)) {
  145. continue;
  146. }
  147. if (!empty($search_id) && !stristr($newModule->id, $search_id)) {
  148. continue;
  149. }
  150. if (!empty($search_permission)) {
  151. $found = false;
  152. foreach ($newModule->permission as $permission) {
  153. if (stristr($permission, $search_permission)) {
  154. $found = true;
  155. break;
  156. }
  157. }
  158. if (!$found) {
  159. continue;
  160. }
  161. }
  162. $moduleList[] = $newModule;
  163. }
  164. /*
  165. * View
  166. */
  167. llxHeader();
  168. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post" name="formulaire">';
  169. if ($optioncss != '') {
  170. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  171. }
  172. print '<input type="hidden" name="token" value="'.newToken().'">';
  173. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  174. print '<input type="hidden" name="action" value="list">';
  175. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  176. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  177. print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
  178. print_barre_liste($langs->trans("AvailableModules"), empty($page) ? 0 : $page, $_SERVER["PHP_SELF"], '', $sortfield, $sortorder, '', -1, '', 'title_setup', 0, '', '', 0, 1, 1);
  179. print '<span class="opacitymedium">'.$langs->trans("ToActivateModule").'</span>';
  180. print '<br>';
  181. print '<br>';
  182. $varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
  183. $selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
  184. print '<div class="div-table-responsive-no-min">';
  185. print '<table class="noborder centpercent">';
  186. // Lines with input filters
  187. print '<tr class="liste_titre_filter">';
  188. if ($arrayfields['name']['checked']) {
  189. print '<td class="liste_titre left">';
  190. print '<input class="flat" type="text" name="search_name" size="8" value="'.dol_escape_htmltag($search_name).'">';
  191. print '</td>';
  192. }
  193. if ($arrayfields['version']['checked']) {
  194. print '<td class="liste_titre left">';
  195. print '<input class="flat" type="text" name="search_version" size="6" value="'.dol_escape_htmltag($search_version).'">';
  196. print '</td>';
  197. }
  198. if ($arrayfields['id']['checked']) {
  199. print '<td class="liste_titre left">';
  200. print '<input class="flat" type="text" name="search_id" size="6 value="'.dol_escape_htmltag($search_id).'">';
  201. print '</td>';
  202. }
  203. if ($arrayfields['permission']['checked']) {
  204. print '<td class="liste_titre left">';
  205. print '<input class="flat" type="text" name="search_permission" size="8" value="'.dol_escape_htmltag($search_permission).'">';
  206. print '</td>';
  207. }
  208. if ($arrayfields['module_position']['checked']) {
  209. print '<td class="liste_titre left">';
  210. print '</td>';
  211. }
  212. print '<td class="liste_titre center maxwidthsearch">';
  213. $searchpicto = $form->showFilterButtons();
  214. print $searchpicto;
  215. print '</td>';
  216. print '</tr>';
  217. print '<tr class="liste_titre">';
  218. if ($arrayfields['name']['checked']) {
  219. print_liste_field_titre($arrayfields['name']['label'], $_SERVER["PHP_SELF"], "name", "", "", "", $sortfield, $sortorder);
  220. }
  221. if ($arrayfields['version']['checked']) {
  222. print_liste_field_titre($arrayfields['version']['label'], $_SERVER["PHP_SELF"], "version", "", "", "", $sortfield, $sortorder);
  223. }
  224. if ($arrayfields['id']['checked']) {
  225. print_liste_field_titre($arrayfields['id']['label'], $_SERVER["PHP_SELF"], "id", "", "", "", $sortfield, $sortorder, 'nowraponall ');
  226. }
  227. if ($arrayfields['permission']['checked']) {
  228. print_liste_field_titre($arrayfields['permission']['label'], $_SERVER["PHP_SELF"], "permission", "", "", "", $sortfield, $sortorder);
  229. }
  230. if ($arrayfields['module_position']['checked']) {
  231. print_liste_field_titre($arrayfields['module_position']['label'], $_SERVER["PHP_SELF"], "module_position", "", "", "", $sortfield, $sortorder);
  232. }
  233. // Fields from hook
  234. $parameters = array('arrayfields'=>$arrayfields, 'param'=>$param, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder);
  235. $reshook = $hookmanager->executeHooks('printFieldListOption', $parameters); // Note that $action and $object may have been modified by hook
  236. print $hookmanager->resPrint;
  237. print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ');
  238. print '</tr>';
  239. // sort list
  240. if ($sortfield == "name" && $sortorder == "asc") {
  241. usort($moduleList, function (stdClass $a, stdClass $b) {
  242. return strcasecmp($a->name, $b->name);
  243. });
  244. } elseif ($sortfield == "name" && $sortorder == "desc") {
  245. usort($moduleList, function (stdClass $a, stdClass $b) {
  246. return strcasecmp($b->name, $a->name);
  247. });
  248. } elseif ($sortfield == "version" && $sortorder == "asc") {
  249. usort($moduleList, function (stdClass $a, stdClass $b) {
  250. return strcasecmp($a->version, $b->version);
  251. });
  252. } elseif ($sortfield == "version" && $sortorder == "desc") {
  253. usort($moduleList, function (stdClass $a, stdClass $b) {
  254. return strcasecmp($b->version, $a->version);
  255. });
  256. } elseif ($sortfield == "id" && $sortorder == "asc") {
  257. usort($moduleList, "compareIdAsc");
  258. } elseif ($sortfield == "id" && $sortorder == "desc") {
  259. usort($moduleList, "compareIdDesc");
  260. } elseif ($sortfield == "permission" && $sortorder == "asc") {
  261. usort($moduleList, "comparePermissionIdsAsc");
  262. } elseif ($sortfield == "permission" && $sortorder == "desc") {
  263. usort($moduleList, "comparePermissionIdsDesc");
  264. } else {
  265. $moduleList = dol_sort_array($moduleList, 'module_position');
  266. }
  267. foreach ($moduleList as $module) {
  268. print '<tr class="oddeven">';
  269. if ($arrayfields['name']['checked']) {
  270. print '<td width="300" class="nowrap">';
  271. print $module->picto;
  272. print ' '.$module->name;
  273. print "</td>";
  274. }
  275. if ($arrayfields['version']['checked']) {
  276. print '<td class="nowraponall">'.$module->version.'</td>';
  277. }
  278. if ($arrayfields['id']['checked']) {
  279. print '<td class="center">'.$module->id.'</td>';
  280. }
  281. if ($arrayfields['permission']['checked']) {
  282. $idperms = '';
  283. foreach ($module->permission as $permission) {
  284. $translationKey = "Permission".$permission;
  285. $labelpermission = $langs->trans($translationKey);
  286. $labelpermission .= ' : '.$arrayofpermissions[$permission]['label'];
  287. $idperms .= ($idperms ? ", " : "").'<span title="'.$labelpermission.'">'.$permission.'</a>';
  288. if (!empty($conf->global->MAIN_SHOW_PERMISSION)) {
  289. if (empty($langs->tab_translate[$translationKey])) {
  290. $tooltip = 'Missing translation (key '.$translationkey.' not found in admin.lang)';
  291. $idperms .= ' <img src="../../theme/eldy/img/warning.png" alt="Warning" title="'.$tooltip.'">';
  292. }
  293. }
  294. }
  295. print '<td><span class="opacitymedium">'.($idperms ? $idperms : "&nbsp;").'</span></td>';
  296. }
  297. if ($arrayfields['module_position']['checked']) {
  298. print '<td class="center">'.$module->module_position.'</td>';
  299. }
  300. print '<td></td>';
  301. print '</tr>';
  302. }
  303. print '</table>';
  304. print '</div>';
  305. print '</form>';
  306. print '<br>';
  307. sort($rights_ids);
  308. $old = '';
  309. foreach ($rights_ids as $right_id) {
  310. if ($old == $right_id) {
  311. print "Warning duplicate id on permission : ".$right_id."<br>";
  312. }
  313. $old = $right_id;
  314. }
  315. // End of page
  316. llxFooter();
  317. $db->close();
  318. /**
  319. * Compare two modules by their ID for a ascending order
  320. *
  321. * @param stdClass $a First module
  322. * @param stdClass $b Second module
  323. * @return int Compare result (-1, 0, 1)
  324. */
  325. function compareIdAsc(stdClass $a, stdClass $b)
  326. {
  327. if ((int) $a->id == (int) $b->id) {
  328. return 0;
  329. }
  330. return ((int) $a->id < (int) $b->id) ? -1 : 1;
  331. }
  332. /**
  333. * Compare two modules by their ID for a descending order
  334. *
  335. * @param stdClass $a First module
  336. * @param stdClass $b Second module
  337. * @return int Compare result (-1, 0, 1)
  338. */
  339. function compareIdDesc(stdClass $a, stdClass $b)
  340. {
  341. if ((int) $a->id == (int) $b->id) {
  342. return 0;
  343. }
  344. return ((int) $b->id < (int) $a->id) ? -1 : 1;
  345. }
  346. /**
  347. * Compare two modules by their ID for a ascending order
  348. *
  349. * @param stdClass $a First module
  350. * @param stdClass $b Second module
  351. * @return int Compare result (-1, 0, 1)
  352. */
  353. function comparePermissionIdsAsc(stdClass $a, stdClass $b)
  354. {
  355. if (empty($a->permission) && empty($b->permission)) {
  356. return compareIdAsc($a, $b);
  357. }
  358. if (empty($a->permission)) {
  359. return 1;
  360. }
  361. if (empty($b->permission)) {
  362. return -1;
  363. }
  364. if ($a->permission[0] == $b->permission[0]) {
  365. return 0;
  366. }
  367. return $a->permission[0] < $b->permission[0] ? -1 : 1;
  368. }
  369. /**
  370. * Compare two modules by their permissions for a descending order
  371. *
  372. * @param stdClass $a First module
  373. * @param stdClass $b Second module
  374. * @return int Compare result (-1, 0, 1)
  375. */
  376. function comparePermissionIdsDesc(stdClass $a, stdClass $b)
  377. {
  378. if (empty($a->permission) && empty($b->permission)) {
  379. return compareIdDesc($a, $b);
  380. }
  381. if (empty($a->permission)) {
  382. return -1;
  383. }
  384. if (empty($b->permission)) {
  385. return 1;
  386. }
  387. if ($a->permission[0] == $b->permission[0]) {
  388. return 0;
  389. }
  390. return $b->permission[0] < $a->permission[0] ? -1 : 1;
  391. }