modules.php 14 KB

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