dolibarr  18.0.0
modules.php
Go to the documentation of this file.
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 
25 // Load Dolibarr environment
26 require '../../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
28 
29 if (empty($user->admin)) {
31 }
32 
33 // Load translation files required by the page
34 $langs->loadLangs(array("install", "other", "admin"));
35 
36 $optioncss = GETPOST('optioncss', 'alpha');
37 $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'moduleoverview';
38 
39 $search_name = GETPOST("search_name", 'alpha');
40 $search_id = GETPOST("search_id", 'alpha');
41 $search_version = GETPOST("search_version", 'alpha');
42 $search_permission = GETPOST("search_permission", 'alpha');
43 
44 $sortfield = GETPOST('sortfield', 'aZ09comma');
45 $sortorder = GETPOST('sortorder', 'aZ09comma');
46 
47 if (!$sortfield) {
48  $sortfield = "id";
49 }
50 if (!$sortorder) {
51  $sortorder = "asc";
52 }
53 
54 // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array of hooks
55 $hookmanager->initHooks(array('moduleoverview'));
56 $form = new Form($db);
57 $object = new stdClass();
58 
59 // Definition of fields for lists
60 $arrayfields = array(
61  'name'=>array('label'=>$langs->trans("Modules"), 'checked'=>1, 'position'=>10),
62  'version'=>array('label'=>$langs->trans("Version"), 'checked'=>1, 'position'=>20),
63  'id'=>array('label'=>$langs->trans("IdModule"), 'checked'=>1, 'position'=>30),
64  'module_position'=>array('label'=>$langs->trans("Position"), 'checked'=>1, 'position'=>35),
65  'permission'=>array('label'=>$langs->trans("IdPermissions"), 'checked'=>1, 'position'=>40)
66 );
67 
68 $arrayfields = dol_sort_array($arrayfields, 'position');
69 $param = '';
70 
71 
72 /*
73  * Actions
74  */
75 
76 $parameters = array();
77 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
78 if ($reshook < 0) {
79  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
80 }
81 
82 if (empty($reshook)) {
83  // Selection of new fields
84  include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
85 }
86 
87 
88 // Load list of modules
89 $moduleList = array();
90 $modules = array();
91 $modules_files = array();
92 $modules_fullpath = array();
93 $modulesdir = dolGetModulesDirs();
94 $rights_ids = array();
95 $arrayofpermissions = array();
96 
97 foreach ($modulesdir as $dir) {
98  $handle = @opendir(dol_osencode($dir));
99  if (is_resource($handle)) {
100  while (($file = readdir($handle)) !== false) {
101  if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
102  $modName = substr($file, 0, dol_strlen($file) - 10);
103 
104  if ($modName) {
105  //print 'xx'.$dir.$file.'<br>';
106  if (in_array($file, $modules_files)) {
107  // File duplicate
108  print "Warning duplicate file found : ".$file." (Found ".$dir.$file.", already found ".$modules_fullpath[$file].")<br>";
109  } else {
110  // File to load
111  $res = include_once $dir.$file;
112  if (class_exists($modName)) {
113  try {
114  $objMod = new $modName($db);
115 
116  $modules[$objMod->numero] = $objMod;
117  $modules_files[$objMod->numero] = $file;
118  $modules_fullpath[$file] = $dir.$file;
119  } catch (Exception $e) {
120  dol_syslog("Failed to load ".$dir.$file." ".$e->getMessage(), LOG_ERR);
121  }
122  } else {
123  print "Warning bad descriptor file : ".$dir.$file." (Class ".$modName." not found into file)<br>";
124  }
125  }
126  }
127  }
128  }
129  closedir($handle);
130  }
131 }
132 
133 // create pre-filtered list for modules
134 foreach ($modules as $key => $module) {
135  $newModule = new stdClass();
136 
137  $newModule->name = $module->getName();
138  $newModule->version = $module->getVersion();
139  $newModule->id = $key;
140  $newModule->module_position = $module->getModulePosition();
141 
142  $alt = $module->name.' - '.$modules_files[$key];
143 
144  if (!empty($module->picto)) {
145  if (preg_match('/^\//', $module->picto)) {
146  $newModule->picto = img_picto($alt, $module->picto, 'width="14px"', 1);
147  } else {
148  $newModule->picto = img_object($alt, $module->picto, 'width="14px"');
149  }
150  } else {
151  $newModule->picto = img_object($alt, 'generic', 'width="14px"');
152  }
153 
154  $permission = array();
155  if ($module->rights) {
156  foreach ($module->rights as $rights) {
157  if (empty($rights[0])) {
158  continue;
159  }
160  $arrayofpermissions[$rights[0]] = array('label'=> 'user->rights->'.$module->rights_class.'->'.$rights[4].(empty($rights[5]) ? '' : '->'.$rights[5]));
161  $permission[] = $rights[0];
162 
163  array_push($rights_ids, $rights[0]);
164  }
165  }
166 
167  $newModule->permission = $permission;
168 
169  // pre-filter list
170  if (!empty($search_name) && !stristr($newModule->name, $search_name)) {
171  continue;
172  }
173  if (!empty($search_version) && !stristr($newModule->version, $search_version)) {
174  continue;
175  }
176  if (!empty($search_id) && !stristr($newModule->id, $search_id)) {
177  continue;
178  }
179 
180  if (!empty($search_permission)) {
181  $found = false;
182 
183  foreach ($newModule->permission as $permission) {
184  if (stristr($permission, $search_permission)) {
185  $found = true;
186  break;
187  }
188  }
189 
190  if (!$found) {
191  continue;
192  }
193  }
194 
195  $moduleList[] = $newModule;
196 }
197 
198 
199 
200 /*
201  * View
202  */
203 
204 llxHeader();
205 
206 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post" name="formulaire">';
207 if ($optioncss != '') {
208  print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
209 }
210 print '<input type="hidden" name="token" value="'.newToken().'">';
211 print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
212 print '<input type="hidden" name="action" value="list">';
213 print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
214 print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
215 print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
216 
217 print_barre_liste($langs->trans("AvailableModules"), empty($page) ? 0 : $page, $_SERVER["PHP_SELF"], '', $sortfield, $sortorder, '', -1, '', 'title_setup', 0, '', '', 0, 1, 1);
218 
219 print '<span class="opacitymedium">'.$langs->trans("ToActivateModule").'</span>';
220 print '<br>';
221 print '<br>';
222 
223 $varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
224 $selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
225 
226 print '<div class="div-table-responsive-no-min">';
227 print '<table class="noborder centpercent">';
228 
229 // Lines with input filters
230 print '<tr class="liste_titre_filter">';
231 
232 if ($arrayfields['name']['checked']) {
233  print '<td class="liste_titre left">';
234  print '<input class="flat" type="text" name="search_name" size="8" value="'.dol_escape_htmltag($search_name).'">';
235  print '</td>';
236 }
237 if ($arrayfields['version']['checked']) {
238  print '<td class="liste_titre left">';
239  print '<input class="flat" type="text" name="search_version" size="6" value="'.dol_escape_htmltag($search_version).'">';
240  print '</td>';
241 }
242 if ($arrayfields['id']['checked']) {
243  print '<td class="liste_titre left">';
244  print '<input class="flat" type="text" name="search_id" size="6" value="'.dol_escape_htmltag($search_id).'">';
245  print '</td>';
246 }
247 if ($arrayfields['permission']['checked']) {
248  print '<td class="liste_titre left">';
249  print '<input class="flat" type="text" name="search_permission" size="8" value="'.dol_escape_htmltag($search_permission).'">';
250  print '</td>';
251 }
252 if ($arrayfields['module_position']['checked']) {
253  print '<td class="liste_titre left">';
254  print '</td>';
255 }
256 
257 print '<td class="liste_titre center maxwidthsearch">';
258 $searchpicto = $form->showFilterButtons();
259 print $searchpicto;
260 print '</td>';
261 
262 print '</tr>';
263 
264 print '<tr class="liste_titre">';
265 
266 if ($arrayfields['name']['checked']) {
267  print_liste_field_titre($arrayfields['name']['label'], $_SERVER["PHP_SELF"], "name", "", "", "", $sortfield, $sortorder);
268 }
269 if ($arrayfields['version']['checked']) {
270  print_liste_field_titre($arrayfields['version']['label'], $_SERVER["PHP_SELF"], "version", "", "", "", $sortfield, $sortorder);
271 }
272 if ($arrayfields['id']['checked']) {
273  print_liste_field_titre($arrayfields['id']['label'], $_SERVER["PHP_SELF"], "id", "", "", "", $sortfield, $sortorder, 'nowraponall ');
274 }
275 if ($arrayfields['permission']['checked']) {
276  print_liste_field_titre($arrayfields['permission']['label'], $_SERVER["PHP_SELF"], "permission", "", "", "", $sortfield, $sortorder);
277 }
278 if ($arrayfields['module_position']['checked']) {
279  print_liste_field_titre($arrayfields['module_position']['label'], $_SERVER["PHP_SELF"], "module_position", "", "", "", $sortfield, $sortorder);
280 }
281 
282 // Fields from hook
283 $parameters = array('arrayfields'=>$arrayfields, 'param'=>$param, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder);
284 $reshook = $hookmanager->executeHooks('printFieldListOption', $parameters); // Note that $action and $object may have been modified by hook
285 print $hookmanager->resPrint;
286 
287 print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ');
288 print '</tr>';
289 
290 // sort list
291 if ($sortfield == "name" && $sortorder == "asc") {
292  usort($moduleList, function (stdClass $a, stdClass $b) {
293  return strcasecmp($a->name, $b->name);
294  });
295 } elseif ($sortfield == "name" && $sortorder == "desc") {
296  usort($moduleList, function (stdClass $a, stdClass $b) {
297  return strcasecmp($b->name, $a->name);
298  });
299 } elseif ($sortfield == "version" && $sortorder == "asc") {
300  usort($moduleList, function (stdClass $a, stdClass $b) {
301  return strcasecmp($a->version, $b->version);
302  });
303 } elseif ($sortfield == "version" && $sortorder == "desc") {
304  usort($moduleList, function (stdClass $a, stdClass $b) {
305  return strcasecmp($b->version, $a->version);
306  });
307 } elseif ($sortfield == "id" && $sortorder == "asc") {
308  usort($moduleList, "compareIdAsc");
309 } elseif ($sortfield == "id" && $sortorder == "desc") {
310  usort($moduleList, "compareIdDesc");
311 } elseif ($sortfield == "permission" && $sortorder == "asc") {
312  usort($moduleList, "comparePermissionIdsAsc");
313 } elseif ($sortfield == "permission" && $sortorder == "desc") {
314  usort($moduleList, "comparePermissionIdsDesc");
315 } else {
316  $moduleList = dol_sort_array($moduleList, 'module_position');
317 }
318 
319 foreach ($moduleList as $module) {
320  print '<tr class="oddeven">';
321 
322  if ($arrayfields['name']['checked']) {
323  print '<td width="300" class="nowrap">';
324  print $module->picto;
325  print ' '.$module->name;
326  print "</td>";
327  }
328 
329  if ($arrayfields['version']['checked']) {
330  print '<td class="nowraponall">'.$module->version.'</td>';
331  }
332 
333  if ($arrayfields['id']['checked']) {
334  print '<td class="center">'.$module->id.'</td>';
335  }
336 
337  if ($arrayfields['permission']['checked']) {
338  $idperms = '';
339 
340  foreach ($module->permission as $permission) {
341  $translationKey = "Permission".$permission;
342  $labelpermission = $langs->trans($translationKey);
343  $labelpermission .= ' : '.$arrayofpermissions[$permission]['label'];
344  $idperms .= ($idperms ? ", " : "").'<span title="'.$labelpermission.'">'.$permission.'</a>';
345 
346  if (!empty($conf->global->MAIN_SHOW_PERMISSION)) {
347  if (empty($langs->tab_translate[$translationKey])) {
348  $tooltip = 'Missing translation (key '.$translationkey.' not found in admin.lang)';
349  $idperms .= ' <img src="../../theme/eldy/img/warning.png" alt="Warning" title="'.$tooltip.'">';
350  }
351  }
352  }
353 
354  print '<td><span class="opacitymedium">'.($idperms ? $idperms : "&nbsp;").'</span></td>';
355  }
356 
357  if ($arrayfields['module_position']['checked']) {
358  print '<td class="center">'.$module->module_position.'</td>';
359  }
360 
361  print '<td></td>';
362  print '</tr>';
363 }
364 
365 print '</table>';
366 print '</div>';
367 print '</form>';
368 print '<br>';
369 
370 sort($rights_ids);
371 $old = '';
372 
373 foreach ($rights_ids as $right_id) {
374  if ($old == $right_id) {
375  print "Warning duplicate id on permission : ".$right_id."<br>";
376  }
377 
378  $old = $right_id;
379 }
380 
381 // End of page
382 llxFooter();
383 $db->close();
384 
385 
393 function compareIdAsc(stdClass $a, stdClass $b)
394 {
395  if ((int) $a->id == (int) $b->id) {
396  return 0;
397  }
398 
399  return ((int) $a->id < (int) $b->id) ? -1 : 1;
400 }
401 
409 function compareIdDesc(stdClass $a, stdClass $b)
410 {
411  if ((int) $a->id == (int) $b->id) {
412  return 0;
413  }
414 
415  return ((int) $b->id < (int) $a->id) ? -1 : 1;
416 }
417 
425 function comparePermissionIdsAsc(stdClass $a, stdClass $b)
426 {
427  if (empty($a->permission) && empty($b->permission)) {
428  return compareIdAsc($a, $b);
429  }
430 
431  if (empty($a->permission)) {
432  return 1;
433  }
434  if (empty($b->permission)) {
435  return -1;
436  }
437 
438  if ($a->permission[0] == $b->permission[0]) {
439  return 0;
440  }
441 
442  return $a->permission[0] < $b->permission[0] ? -1 : 1;
443 }
444 
452 function comparePermissionIdsDesc(stdClass $a, stdClass $b)
453 {
454  if (empty($a->permission) && empty($b->permission)) {
455  return compareIdDesc($a, $b);
456  }
457 
458  if (empty($a->permission)) {
459  return -1;
460  }
461  if (empty($b->permission)) {
462  return 1;
463  }
464 
465  if ($a->permission[0] == $b->permission[0]) {
466  return 0;
467  }
468 
469  return $b->permission[0] < $a->permission[0] ? -1 : 1;
470 }
compareIdAsc
compareIdAsc(stdClass $a, stdClass $b)
Compare two modules by their ID for a ascending order.
Definition: modules.php:393
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:70
dol_osencode
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Definition: functions.lib.php:9055
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:609
dol_sort_array
dol_sort_array(&$array, $index, $order='asc', $natsort=0, $case_sensitive=0, $keepindex=0)
Advanced sort array by second index function, which produces ascending (default) or descending output...
Definition: functions.lib.php:8922
comparePermissionIdsAsc
comparePermissionIdsAsc(stdClass $a, stdClass $b)
Compare two modules by their ID for a ascending order.
Definition: modules.php:425
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
compareIdDesc
compareIdDesc(stdClass $a, stdClass $b)
Compare two modules by their ID for a descending order.
Definition: modules.php:409
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:4135
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
Exception
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1741
print_barre_liste
print_barre_liste($titre, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $hideselectlimit=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
Definition: functions.lib.php:5530
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
Definition: functions.lib.php:8673
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3997
dolGetModulesDirs
dolGetModulesDirs($subdir='')
Return list of modules directories.
Definition: functions2.lib.php:80
print_liste_field_titre
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
Definition: functions.lib.php:5295
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:53
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4473
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
Definition: security.lib.php:1164
comparePermissionIdsDesc
comparePermissionIdsDesc(stdClass $a, stdClass $b)
Compare two modules by their permissions for a descending order.
Definition: modules.php:452
if
if(!defined( 'CSRFCHECK_WITH_TOKEN'))
Definition: journals_list.php:25