dolibarr  18.0.0-alpha
admin.lib.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2005-2016 Regis Houssin <regis.houssin@inodbox.com>
4  * Copyright (C) 2012 J. Fernando Lagrange <fernando@demo-tic.org>
5  * Copyright (C) 2015 RaphaĆ«l Doursenaud <rdoursenaud@gpcsolutions.fr>
6  * Copyright (C) 2023 Eric Seigne <eric.seigne@cap-rel.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  * or see https://www.gnu.org/
21  */
22 
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
29 
37 function versiontostring($versionarray)
38 {
39  $string = '?';
40  if (isset($versionarray[0])) {
41  $string = $versionarray[0];
42  }
43  if (isset($versionarray[1])) {
44  $string .= '.'.$versionarray[1];
45  }
46  if (isset($versionarray[2])) {
47  $string .= '.'.$versionarray[2];
48  }
49  return $string;
50 }
51 
67 function versioncompare($versionarray1, $versionarray2)
68 {
69  $ret = 0;
70  $level = 0;
71  $count1 = count($versionarray1);
72  $count2 = count($versionarray2);
73  $maxcount = max($count1, $count2);
74  while ($level < $maxcount) {
75  $operande1 = isset($versionarray1[$level]) ? $versionarray1[$level] : 0;
76  $operande2 = isset($versionarray2[$level]) ? $versionarray2[$level] : 0;
77  if (preg_match('/alpha|dev/i', $operande1)) {
78  $operande1 = -5;
79  }
80  if (preg_match('/alpha|dev/i', $operande2)) {
81  $operande2 = -5;
82  }
83  if (preg_match('/beta$/i', $operande1)) {
84  $operande1 = -4;
85  }
86  if (preg_match('/beta$/i', $operande2)) {
87  $operande2 = -4;
88  }
89  if (preg_match('/beta([0-9])+/i', $operande1)) {
90  $operande1 = -3;
91  }
92  if (preg_match('/beta([0-9])+/i', $operande2)) {
93  $operande2 = -3;
94  }
95  if (preg_match('/rc$/i', $operande1)) {
96  $operande1 = -2;
97  }
98  if (preg_match('/rc$/i', $operande2)) {
99  $operande2 = -2;
100  }
101  if (preg_match('/rc([0-9])+/i', $operande1)) {
102  $operande1 = -1;
103  }
104  if (preg_match('/rc([0-9])+/i', $operande2)) {
105  $operande2 = -1;
106  }
107  $level++;
108  //print 'level '.$level.' '.$operande1.'-'.$operande2.'<br>';
109  if ($operande1 < $operande2) {
110  $ret = -$level;
111  break;
112  }
113  if ($operande1 > $operande2) {
114  $ret = $level;
115  break;
116  }
117  }
118  //print join('.',$versionarray1).'('.count($versionarray1).') / '.join('.',$versionarray2).'('.count($versionarray2).') => '.$ret.'<br>'."\n";
119  return $ret;
120 }
121 
122 
129 function versionphparray()
130 {
131  return explode('.', PHP_VERSION);
132 }
133 
141 {
142  return explode('.', DOL_VERSION);
143 }
144 
145 
168 function run_sql($sqlfile, $silent = 1, $entity = '', $usesavepoint = 1, $handler = '', $okerror = 'default', $linelengthlimit = 32768, $nocommentremoval = 0, $offsetforchartofaccount = 0, $colspan = 0, $onlysqltoimportwebsite = 0)
169 {
170  global $db, $conf, $langs, $user;
171 
172  dol_syslog("Admin.lib::run_sql run sql file ".$sqlfile." silent=".$silent." entity=".$entity." usesavepoint=".$usesavepoint." handler=".$handler." okerror=".$okerror, LOG_DEBUG);
173 
174  if (!is_numeric($linelengthlimit)) {
175  dol_syslog("Admin.lib::run_sql param linelengthlimit is not a numeric", LOG_ERR);
176  return -1;
177  }
178 
179  $ok = 0;
180  $error = 0;
181  $i = 0;
182  $buffer = '';
183  $arraysql = array();
184 
185  // Get version of database
186  $versionarray = $db->getVersionArray();
187 
188  $fp = fopen($sqlfile, "r");
189  if ($fp) {
190  while (!feof($fp)) {
191  // Warning fgets with second parameter that is null or 0 hang.
192  if ($linelengthlimit > 0) {
193  $buf = fgets($fp, $linelengthlimit);
194  } else {
195  $buf = fgets($fp);
196  }
197 
198  // Test if request must be ran only for particular database or version (if yes, we must remove the -- comment)
199  $reg = array();
200  if (preg_match('/^--\sV(MYSQL|PGSQL)([^\s]*)/i', $buf, $reg)) {
201  $qualified = 1;
202 
203  // restrict on database type
204  if (!empty($reg[1])) {
205  if (!preg_match('/'.preg_quote($reg[1]).'/i', $db->type)) {
206  $qualified = 0;
207  }
208  }
209 
210  // restrict on version
211  if ($qualified) {
212  if (!empty($reg[2])) {
213  if (is_numeric($reg[2])) { // This is a version
214  $versionrequest = explode('.', $reg[2]);
215  //var_dump($versionrequest);
216  //var_dump($versionarray);
217  if (!count($versionrequest) || !count($versionarray) || versioncompare($versionrequest, $versionarray) > 0) {
218  $qualified = 0;
219  }
220  } else // This is a test on a constant. For example when we have -- VMYSQLUTF8UNICODE, we test constant $conf->global->UTF8UNICODE
221  {
222  $dbcollation = strtoupper(preg_replace('/_/', '', $conf->db->dolibarr_main_db_collation));
223  //var_dump($reg[2]);
224  //var_dump($dbcollation);
225  if (empty($conf->db->dolibarr_main_db_collation) || ($reg[2] != $dbcollation)) {
226  $qualified = 0;
227  }
228  //var_dump($qualified);
229  }
230  }
231  }
232 
233  if ($qualified) {
234  // Version qualified, delete SQL comments
235  $buf = preg_replace('/^--\sV(MYSQL|PGSQL)([^\s]*)/i', '', $buf);
236  //print "Ligne $i qualifi?e par version: ".$buf.'<br>';
237  }
238  }
239 
240  // Add line buf to buffer if not a comment
241  if ($nocommentremoval || !preg_match('/^\s*--/', $buf)) {
242  if (empty($nocommentremoval)) {
243  $buf = preg_replace('/([,;ERLT\)])\s*--.*$/i', '\1', $buf); //remove comment from a line that not start with -- before add it to the buffer
244  }
245  if ($buffer) $buffer .= ' ';
246  $buffer .= trim($buf);
247  }
248 
249  //print $buf.'<br>';exit;
250 
251  if (preg_match('/;/', $buffer)) { // If string contains ';', it's end of a request string, we save it in arraysql.
252  // Found new request
253  if ($buffer) {
254  $arraysql[$i] = $buffer;
255  }
256  $i++;
257  $buffer = '';
258  }
259  }
260 
261  if ($buffer) {
262  $arraysql[$i] = $buffer;
263  }
264  fclose($fp);
265  } else {
266  dol_syslog("Admin.lib::run_sql failed to open file ".$sqlfile, LOG_ERR);
267  }
268 
269  // Loop on each request to see if there is a __+MAX_table__ key
270  $listofmaxrowid = array(); // This is a cache table
271  foreach ($arraysql as $i => $sql) {
272  $newsql = $sql;
273 
274  // Replace __+MAX_table__ with max of table
275  while (preg_match('/__\+MAX_([A-Za-z0-9_]+)__/i', $newsql, $reg)) {
276  $table = $reg[1];
277  if (!isset($listofmaxrowid[$table])) {
278  //var_dump($db);
279  $sqlgetrowid = 'SELECT MAX(rowid) as max from '.preg_replace('/^llx_/', MAIN_DB_PREFIX, $table);
280  $resql = $db->query($sqlgetrowid);
281  if ($resql) {
282  $obj = $db->fetch_object($resql);
283  $listofmaxrowid[$table] = $obj->max;
284  if (empty($listofmaxrowid[$table])) {
285  $listofmaxrowid[$table] = 0;
286  }
287  } else {
288  if (!$silent) {
289  print '<tr><td class="tdtop"'.($colspan ? ' colspan="'.$colspan.'"' : '').'>';
290  print '<div class="error">'.$langs->trans("Failed to get max rowid for ".$table)."</div>";
291  print '</td></tr>';
292  }
293  $error++;
294  break;
295  }
296  }
297  // Replace __+MAX_llx_table__ with +999
298  $from = '__+MAX_'.$table.'__';
299  $to = '+'.$listofmaxrowid[$table];
300  $newsql = str_replace($from, $to, $newsql);
301  dol_syslog('Admin.lib::run_sql New Request '.($i + 1).' (replacing '.$from.' to '.$to.')', LOG_DEBUG);
302 
303  $arraysql[$i] = $newsql;
304  }
305 
306  if ($offsetforchartofaccount > 0) {
307  // Replace lines
308  // 'INSERT INTO llx_accounting_account (entity, rowid, fk_pcg_version, pcg_type, account_number, account_parent, label, active) VALUES (__ENTITY__, 1401, 'PCG99-ABREGE', 'CAPIT', '1234', 1400,...'
309  // with
310  // 'INSERT INTO llx_accounting_account (entity, rowid, fk_pcg_version, pcg_type, account_number, account_parent, label, active) VALUES (__ENTITY__, 1401 + 200100000, 'PCG99-ABREGE','CAPIT', '1234', 1400 + 200100000,...'
311  // Note: string with 'PCG99-ABREGE','CAPIT', 1234 instead of 'PCG99-ABREGE','CAPIT', '1234' is also supported
312  $newsql = preg_replace('/VALUES\s*\(__ENTITY__, \s*(\d+)\s*,(\s*\'[^\',]*\'\s*,\s*\'[^\',]*\'\s*,\s*\'?[^\',]*\'?\s*),\s*\'?([^\',]*)\'?/ims', 'VALUES (__ENTITY__, \1 + '.((int) $offsetforchartofaccount).', \2, \3 + '.((int) $offsetforchartofaccount), $newsql);
313  $newsql = preg_replace('/([,\s])0 \+ '.((int) $offsetforchartofaccount).'/ims', '\1 0', $newsql);
314  //var_dump($newsql);
315  $arraysql[$i] = $newsql;
316 
317  // FIXME Because we force the rowid during insert, we must also update the sequence with postgresql by running
318  // SELECT dol_util_rebuild_sequences();
319  }
320  }
321 
322  // Loop on each request to execute request
323  $cursorinsert = 0;
324  $listofinsertedrowid = array();
325  $keyforsql = md5($sqlfile);
326  foreach ($arraysql as $i => $sql) {
327  if ($sql) {
328  // Test if th SQL is allowed SQL
329  if ($onlysqltoimportwebsite) {
330  $newsql = str_replace(array("\'"), '__BACKSLASHQUOTE__', $sql); // Replace the \' seque,ce
331 
332  // Remove all strings contents including the ' so we can analyse SQL instruction only later
333  $l = strlen($newsql);
334  $is = 0;
335  $quoteopen = 0;
336  $newsqlclean = '';
337  while ($is < $l) {
338  $char = $newsql[$is];
339  if ($char == "'") {
340  if ($quoteopen) {
341  $quoteopen--;
342  } else {
343  $quoteopen++;
344  }
345  } elseif (empty($quoteopen)) {
346  $newsqlclean .= $char;
347  }
348  $is++;
349  }
350  $newsqlclean = str_replace(array("null"), '__000__', $newsqlclean);
351  //print $newsqlclean."<br>\n";
352 
353  $qualified = 0;
354 
355  // A very small control. This can still by bypassed by adding a second SQL request concatenated
356  if (preg_match('/^--/', $newsqlclean)) {
357  $qualified = 1;
358  } elseif (preg_match('/^UPDATE llx_website SET \w+ = \d+\+\d+ WHERE rowid = \d+;$/', $newsqlclean)) {
359  $qualified = 1;
360  } elseif (preg_match('/^INSERT INTO llx_website_page\([a-z0-9_\s,]+\) VALUES\([0-9_\s,\+]+\);$/', $newsqlclean)) {
361  // Insert must match
362  // INSERT INTO llx_website_page(rowid, fk_page, fk_website, pageurl, aliasalt, title, description, lang, image, keywords, status, date_creation, tms, import_key, grabbed_from, type_container, htmlheader, content, author_alias) VALUES(1+123, null, 17, , , , , , , , , , , null, , , , , );
363  $qualified = 1;
364  }
365 
366  // Another check to allow some legitimate original urls
367  if (!$qualified) {
368  if (preg_match('/^UPDATE llx_website SET \w+ = \'[a-zA-Z,\s]*\' WHERE rowid = \d+;$/', $sql)) {
369  $qualified = 1;
370  }
371  }
372 
373  if (!$qualified) {
374  $error++;
375  //print 'Request '.($i + 1)." contains non allowed instructions.<br>\n";
376  //print "newsqlclean = ".$newsqlclean."<br>\n";
377  dol_syslog('Admin.lib::run_sql Request '.($i + 1)." contains non allowed instructions.", LOG_WARNING);
378  dol_syslog('$newsqlclean='.$newsqlclean, LOG_DEBUG);
379  break;
380  }
381  }
382 
383  // Replace the prefix tables
384  if (MAIN_DB_PREFIX != 'llx_') {
385  $sql = preg_replace('/llx_/i', MAIN_DB_PREFIX, $sql);
386  }
387 
388  if (!empty($handler)) {
389  $sql = preg_replace('/__HANDLER__/i', "'".$db->escape($handler)."'", $sql);
390  }
391 
392  $newsql = preg_replace('/__ENTITY__/i', (!empty($entity) ? $entity : $conf->entity), $sql);
393 
394  // Add log of request
395  if (!$silent) {
396  print '<tr class="trforrunsql'.$keyforsql.'"><td class="tdtop opacitymedium"'.($colspan ? ' colspan="'.$colspan.'"' : '').'>'.$langs->trans("Request").' '.($i + 1)." sql='".dol_htmlentities($newsql, ENT_NOQUOTES)."'</td></tr>\n";
397  }
398  dol_syslog('Admin.lib::run_sql Request '.($i + 1), LOG_DEBUG);
399  $sqlmodified = 0;
400 
401  // Replace for encrypt data
402  if (preg_match_all('/__ENCRYPT\(\'([^\']+)\'\)__/i', $newsql, $reg)) {
403  $num = count($reg[0]);
404 
405  for ($j = 0; $j < $num; $j++) {
406  $from = $reg[0][$j];
407  $to = $db->encrypt($reg[1][$j]);
408  $newsql = str_replace($from, $to, $newsql);
409  }
410  $sqlmodified++;
411  }
412 
413  // Replace for decrypt data
414  if (preg_match_all('/__DECRYPT\(\'([A-Za-z0-9_]+)\'\)__/i', $newsql, $reg)) {
415  $num = count($reg[0]);
416 
417  for ($j = 0; $j < $num; $j++) {
418  $from = $reg[0][$j];
419  $to = $db->decrypt($reg[1][$j]);
420  $newsql = str_replace($from, $to, $newsql);
421  }
422  $sqlmodified++;
423  }
424 
425  // Replace __x__ with the rowid of the result of the insert number x
426  while (preg_match('/__([0-9]+)__/', $newsql, $reg)) {
427  $cursor = $reg[1];
428  if (empty($listofinsertedrowid[$cursor])) {
429  if (!$silent) {
430  print '<tr><td class="tdtop"'.($colspan ? ' colspan="'.$colspan.'"' : '').'>';
431  print '<div class="error">'.$langs->trans("FileIsNotCorrect")."</div>";
432  print '</td></tr>';
433  }
434  $error++;
435  break;
436  }
437 
438  $from = '__'.$cursor.'__';
439  $to = $listofinsertedrowid[$cursor];
440  $newsql = str_replace($from, $to, $newsql);
441  $sqlmodified++;
442  }
443 
444  if ($sqlmodified) {
445  dol_syslog('Admin.lib::run_sql New Request '.($i + 1), LOG_DEBUG);
446  }
447 
448  $result = $db->query($newsql, $usesavepoint);
449  if ($result) {
450  if (!$silent) {
451  print '<!-- Result = OK -->'."\n";
452  }
453 
454  if (preg_replace('/insert into ([^\s]+)/i', $newsql, $reg)) {
455  $cursorinsert++;
456 
457  // It's an insert
458  $table = preg_replace('/([^a-zA-Z_]+)/i', '', $reg[1]);
459  $insertedrowid = $db->last_insert_id($table);
460  $listofinsertedrowid[$cursorinsert] = $insertedrowid;
461  dol_syslog('Admin.lib::run_sql Insert nb '.$cursorinsert.', done in table '.$table.', rowid is '.$listofinsertedrowid[$cursorinsert], LOG_DEBUG);
462  }
463  // print '<td class="right">OK</td>';
464  } else {
465  $errno = $db->errno();
466  if (!$silent) {
467  print '<!-- Result = '.$errno.' -->'."\n";
468  }
469 
470  // Define list of errors we accept (array $okerrors)
471  $okerrors = array( // By default
472  'DB_ERROR_TABLE_ALREADY_EXISTS',
473  'DB_ERROR_COLUMN_ALREADY_EXISTS',
474  'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
475  'DB_ERROR_TABLE_OR_KEY_ALREADY_EXISTS', // PgSql use same code for table and key already exist
476  'DB_ERROR_RECORD_ALREADY_EXISTS',
477  'DB_ERROR_NOSUCHTABLE',
478  'DB_ERROR_NOSUCHFIELD',
479  'DB_ERROR_NO_FOREIGN_KEY_TO_DROP',
480  'DB_ERROR_NO_INDEX_TO_DROP',
481  'DB_ERROR_CANNOT_CREATE', // Qd contrainte deja existante
482  'DB_ERROR_CANT_DROP_PRIMARY_KEY',
483  'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS',
484  'DB_ERROR_22P02'
485  );
486  if ($okerror == 'none') {
487  $okerrors = array();
488  }
489 
490  // Is it an error we accept
491  if (!in_array($errno, $okerrors)) {
492  if (!$silent) {
493  print '<tr><td class="tdtop"'.($colspan ? ' colspan="'.$colspan.'"' : '').'>';
494  print '<div class="error">'.$langs->trans("Error")." ".$db->errno().": ".$newsql."<br>".$db->error()."</div>";
495  print '</td></tr>'."\n";
496  }
497  dol_syslog('Admin.lib::run_sql Request '.($i + 1)." Error ".$db->errno()." ".$newsql."<br>".$db->error(), LOG_ERR);
498  $error++;
499  }
500  }
501  }
502  }
503 
504  if (!$silent) {
505  print '<tr><td>'.$langs->trans("ProcessMigrateScript").'</td>';
506  print '<td class="right">';
507  if ($error == 0) {
508  print '<span class="ok">'.$langs->trans("OK").'</span>';
509  } else {
510  print '<span class="error">'.$langs->trans("Error").'</span>';
511  }
512 
513  //if (!empty($conf->use_javascript_ajax)) { // use_javascript_ajax is not defined
514  print '<script type="text/javascript">
515  jQuery(document).ready(function() {
516  function init_trrunsql'.$keyforsql.'()
517  {
518  console.log("toggle .trforrunsql'.$keyforsql.'");
519  jQuery(".trforrunsql'.$keyforsql.'").toggle();
520  }
521  init_trrunsql'.$keyforsql.'();
522  jQuery(".trforrunsqlshowhide'.$keyforsql.'").click(function() {
523  init_trrunsql'.$keyforsql.'();
524  });
525  });
526  </script>';
527  if (count($arraysql)) {
528  print ' - <a class="trforrunsqlshowhide'.$keyforsql.'" href="#" title="'.($langs->trans("ShowHideTheNRequests", count($arraysql))).'">'.$langs->trans("ShowHideDetails").'</a>';
529  } else {
530  print ' - <span class="opacitymedium">'.$langs->trans("ScriptIsEmpty").'</span>';
531  }
532  //}
533 
534  print '</td></tr>'."\n";
535  }
536 
537  if ($error == 0) {
538  $ok = 1;
539  } else {
540  $ok = 0;
541  }
542 
543  return $ok;
544 }
545 
546 
557 function dolibarr_del_const($db, $name, $entity = 1)
558 {
559  global $conf;
560 
561  if (empty($name)) {
562  dol_print_error('', 'Error call dolibar_del_const with parameter name empty');
563  return -1;
564  }
565 
566  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
567  $sql .= " WHERE (".$db->decrypt('name')." = '".$db->escape($name)."'";
568  if (is_numeric($name)) {
569  $sql .= " OR rowid = ".((int) $name);
570  }
571  $sql .= ")";
572  if ($entity >= 0) {
573  $sql .= " AND entity = ".((int) $entity);
574  }
575 
576  dol_syslog("admin.lib::dolibarr_del_const", LOG_DEBUG);
577  $resql = $db->query($sql);
578  if ($resql) {
579  $conf->global->$name = '';
580  return 1;
581  } else {
582  dol_print_error($db);
583  return -1;
584  }
585 }
586 
597 function dolibarr_get_const($db, $name, $entity = 1)
598 {
599  $value = '';
600 
601  $sql = "SELECT ".$db->decrypt('value')." as value";
602  $sql .= " FROM ".MAIN_DB_PREFIX."const";
603  $sql .= " WHERE name = ".$db->encrypt($name);
604  $sql .= " AND entity = ".((int) $entity);
605 
606  dol_syslog("admin.lib::dolibarr_get_const", LOG_DEBUG);
607  $resql = $db->query($sql);
608  if ($resql) {
609  $obj = $db->fetch_object($resql);
610  if ($obj) {
611  include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
612  $value = dolDecrypt($obj->value);
613  }
614  }
615  return $value;
616 }
617 
618 
633 function dolibarr_set_const($db, $name, $value, $type = 'chaine', $visible = 0, $note = '', $entity = 1)
634 {
635  global $conf;
636 
637  // Clean parameters
638  $name = trim($name);
639 
640  // Check parameters
641  if (empty($name)) {
642  dol_print_error($db, "Error: Call to function dolibarr_set_const with wrong parameters", LOG_ERR);
643  exit;
644  }
645 
646  //dol_syslog("dolibarr_set_const name=$name, value=$value type=$type, visible=$visible, note=$note entity=$entity");
647 
648  $db->begin();
649 
650  $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
651  $sql .= " WHERE name = ".$db->encrypt($name);
652  if ($entity >= 0) {
653  $sql .= " AND entity = ".((int) $entity);
654  }
655 
656  dol_syslog("admin.lib::dolibarr_set_const", LOG_DEBUG);
657  $resql = $db->query($sql);
658 
659  if (strcmp($value, '')) { // true if different. Must work for $value='0' or $value=0
660  if (!preg_match('/^MAIN_LOGEVENTS/', $name) && (preg_match('/(_KEY|_EXPORTKEY|_SECUREKEY|_SERVERKEY|_PASS|_PASSWORD|_PW|_PW_TICKET|_PW_EMAILING|_SECRET|_SECURITY_TOKEN|_WEB_TOKEN)$/', $name))) {
661  // This seems a sensitive constant, we encrypt its value
662  // To list all sensitive constant, you can make a
663  // WHERE name like '%\_KEY' or name like '%\_EXPORTKEY' or name like '%\_SECUREKEY' or name like '%\_SERVERKEY' or name like '%\_PASS' or name like '%\_PASSWORD' or name like '%\_SECRET'
664  // or name like '%\_SECURITY_TOKEN' or name like '%\WEB_TOKEN'
665  include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
666  $newvalue = dolEncrypt($value);
667  } else {
668  $newvalue = $value;
669  }
670 
671  $sql = "INSERT INTO ".MAIN_DB_PREFIX."const(name, value, type, visible, note, entity)";
672  $sql .= " VALUES (";
673  $sql .= $db->encrypt($name);
674  $sql .= ", ".$db->encrypt($newvalue);
675  $sql .= ", '".$db->escape($type)."', ".((int) $visible).", '".$db->escape($note)."', ".((int) $entity).")";
676 
677  //print "sql".$value."-".pg_escape_string($value)."-".$sql;exit;
678  //print "xx".$db->escape($value);
679  dol_syslog("admin.lib::dolibarr_set_const", LOG_DEBUG);
680  $resql = $db->query($sql);
681  }
682 
683  if ($resql) {
684  $db->commit();
685  $conf->global->$name = $value;
686  return 1;
687  } else {
688  $error = $db->lasterror();
689  $db->rollback();
690  return -1;
691  }
692 }
693 
694 
695 
696 
705 function modules_prepare_head($nbofactivatedmodules, $nboftotalmodules, $nbmodulesnotautoenabled)
706 {
707  global $langs, $conf, $user, $form;
708 
709  $desc = $langs->trans("ModulesDesc", '{picto}');
710  $desc = str_replace('{picto}', img_picto('', 'switch_off'), $desc);
711 
712  $h = 0;
713  $head = array();
714  $mode = empty($conf->global->MAIN_MODULE_SETUP_ON_LIST_BY_DEFAULT) ? 'commonkanban' : $conf->global->MAIN_MODULE_SETUP_ON_LIST_BY_DEFAULT;
715  $head[$h][0] = DOL_URL_ROOT."/admin/modules.php?mode=".$mode;
716  if ($nbmodulesnotautoenabled <= getDolGlobalInt('MAIN_MIN_NB_ENABLED_MODULE_FOR_WARNING', 1)) { // If only minimal initial modules enabled)
717  //$head[$h][1] = $form->textwithpicto($langs->trans("AvailableModules"), $desc);
718  $head[$h][1] = $langs->trans("AvailableModules");
719  $head[$h][1] .= $form->textwithpicto('', $langs->trans("YouMustEnableOneModule").'.<br><br><span class="opacitymedium">'.$desc.'</span>', 1, 'warning');
720  } else {
721  //$head[$h][1] = $langs->trans("AvailableModules").$form->textwithpicto('<span class="badge marginleftonly">'.$nbofactivatedmodules.' / '.$nboftotalmodules.'</span>', $desc, 1, 'help', '', 1, 3);
722  $head[$h][1] = $langs->trans("AvailableModules").'<span class="badge marginleftonly">'.$nbofactivatedmodules.' / '.$nboftotalmodules.'</span>';
723  }
724  $head[$h][2] = 'modules';
725  $h++;
726 
727  $head[$h][0] = DOL_URL_ROOT."/admin/modules.php?mode=marketplace";
728  $head[$h][1] = $langs->trans("ModulesMarketPlaces");
729  $head[$h][2] = 'marketplace';
730  $h++;
731 
732  $head[$h][0] = DOL_URL_ROOT."/admin/modules.php?mode=deploy";
733  $head[$h][1] = $langs->trans("AddExtensionThemeModuleOrOther");
734  $head[$h][2] = 'deploy';
735  $h++;
736 
737  $head[$h][0] = DOL_URL_ROOT."/admin/modules.php?mode=develop";
738  $head[$h][1] = $langs->trans("ModulesDevelopYourModule");
739  $head[$h][2] = 'develop';
740  $h++;
741 
742  return $head;
743 }
744 
751 {
752  global $langs, $conf, $user;
753  $h = 0;
754  $head = array();
755 
756  $head[$h][0] = DOL_URL_ROOT."/admin/ihm.php?mode=other";
757  $head[$h][1] = $langs->trans("LanguageAndPresentation");
758  $head[$h][2] = 'other';
759  $h++;
760 
761  $head[$h][0] = DOL_URL_ROOT."/admin/ihm.php?mode=template";
762  $head[$h][1] = $langs->trans("SkinAndColors");
763  $head[$h][2] = 'template';
764  $h++;
765 
766  $head[$h][0] = DOL_URL_ROOT."/admin/ihm.php?mode=dashboard";
767  $head[$h][1] = $langs->trans("Dashboard");
768  $head[$h][2] = 'dashboard';
769  $h++;
770 
771  $head[$h][0] = DOL_URL_ROOT."/admin/ihm.php?mode=login";
772  $head[$h][1] = $langs->trans("LoginPage");
773  $head[$h][2] = 'login';
774  $h++;
775 
776  $head[$h][0] = DOL_URL_ROOT."/admin/ihm.php?mode=css";
777  $head[$h][1] = $langs->trans("CSSPage");
778  $head[$h][2] = 'css';
779  $h++;
780 
781  complete_head_from_modules($conf, $langs, null, $head, $h, 'ihm_admin');
782 
783  complete_head_from_modules($conf, $langs, null, $head, $h, 'ihm_admin', 'remove');
784 
785 
786  return $head;
787 }
788 
789 
796 {
797  global $db, $langs, $conf, $user;
798  $h = 0;
799  $head = array();
800 
801  $head[$h][0] = DOL_URL_ROOT."/admin/security_other.php";
802  $head[$h][1] = $langs->trans("Miscellaneous");
803  $head[$h][2] = 'misc';
804  $h++;
805 
806  $head[$h][0] = DOL_URL_ROOT."/admin/security.php";
807  $head[$h][1] = $langs->trans("Passwords");
808  $head[$h][2] = 'passwords';
809  $h++;
810 
811  $head[$h][0] = DOL_URL_ROOT."/admin/security_file.php";
812  $head[$h][1] = $langs->trans("Files").' ('.$langs->trans("Upload").')';
813  $head[$h][2] = 'file';
814  $h++;
815 
816  /*
817  $head[$h][0] = DOL_URL_ROOT."/admin/security_file_download.php";
818  $head[$h][1] = $langs->trans("Files").' ('.$langs->trans("Download").')';
819  $head[$h][2] = 'filedownload';
820  $h++;
821  */
822 
823  $head[$h][0] = DOL_URL_ROOT."/admin/proxy.php";
824  $head[$h][1] = $langs->trans("ExternalAccess");
825  $head[$h][2] = 'proxy';
826  $h++;
827 
828  $head[$h][0] = DOL_URL_ROOT."/admin/events.php";
829  $head[$h][1] = $langs->trans("Audit");
830  $head[$h][2] = 'audit';
831  $h++;
832 
833 
834  // Show permissions lines
835  $nbPerms = 0;
836  $sql = "SELECT COUNT(r.id) as nb";
837  $sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r";
838  $sql .= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
839  $sql .= " AND entity = ".((int) $conf->entity);
840  $sql .= " AND bydefault = 1";
841  if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
842  $sql .= " AND r.perms NOT LIKE '%_advance'"; // Hide advanced perms if option is not enabled
843  }
844  $resql = $db->query($sql);
845  if ($resql) {
846  $obj = $db->fetch_object($resql);
847  if ($obj) {
848  $nbPerms = $obj->nb;
849  }
850  } else {
851  dol_print_error($db);
852  }
853 
854  $head[$h][0] = DOL_URL_ROOT."/admin/perms.php";
855  $head[$h][1] = $langs->trans("DefaultRights");
856  if ($nbPerms > 0) {
857  $head[$h][1] .= (empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '<span class="badge marginleftonlyshort">'.$nbPerms.'</span>' : '');
858  }
859  $head[$h][2] = 'default';
860  $h++;
861 
862  return $head;
863 }
864 
870 function modulehelp_prepare_head($object)
871 {
872  global $langs, $conf, $user;
873  $h = 0;
874  $head = array();
875 
876  // FIX for compatibity habitual tabs
877  $object->id = $object->numero;
878 
879  $head[$h][0] = DOL_URL_ROOT."/admin/modulehelp.php?id=".$object->id.'&mode=desc';
880  $head[$h][1] = $langs->trans("Description");
881  $head[$h][2] = 'desc';
882  $h++;
883 
884  $head[$h][0] = DOL_URL_ROOT."/admin/modulehelp.php?id=".$object->id.'&mode=feature';
885  $head[$h][1] = $langs->trans("TechnicalServicesProvided");
886  $head[$h][2] = 'feature';
887  $h++;
888 
889  if ($object->isCoreOrExternalModule() == 'external') {
890  $head[$h][0] = DOL_URL_ROOT."/admin/modulehelp.php?id=".$object->id.'&mode=changelog';
891  $head[$h][1] = $langs->trans("ChangeLog");
892  $head[$h][2] = 'changelog';
893  $h++;
894  }
895 
896  complete_head_from_modules($conf, $langs, $object, $head, $h, 'modulehelp_admin');
897 
898  complete_head_from_modules($conf, $langs, $object, $head, $h, 'modulehelp_admin', 'remove');
899 
900 
901  return $head;
902 }
909 {
910  global $langs, $conf, $user;
911  $h = 0;
912  $head = array();
913 
914  $head[$h][0] = DOL_URL_ROOT."/admin/translation.php?mode=searchkey";
915  $head[$h][1] = $langs->trans("TranslationKeySearch");
916  $head[$h][2] = 'searchkey';
917  $h++;
918 
919  $head[$h][0] = DOL_URL_ROOT."/admin/translation.php?mode=overwrite";
920  $head[$h][1] = '<span class="valignmiddle">'.$langs->trans("TranslationOverwriteKey").'</span><span class="fa fa-plus-circle valignmiddle paddingleft"></span>';
921  $head[$h][2] = 'overwrite';
922  $h++;
923 
924  complete_head_from_modules($conf, $langs, null, $head, $h, 'translation_admin');
925 
926  complete_head_from_modules($conf, $langs, null, $head, $h, 'translation_admin', 'remove');
927 
928 
929  return $head;
930 }
931 
932 
939 {
940  global $langs, $conf, $user;
941  $h = 0;
942  $head = array();
943 
944  $head[$h][0] = DOL_URL_ROOT."/admin/defaultvalues.php?mode=createform";
945  $head[$h][1] = $langs->trans("DefaultCreateForm");
946  $head[$h][2] = 'createform';
947  $h++;
948 
949  $head[$h][0] = DOL_URL_ROOT."/admin/defaultvalues.php?mode=filters";
950  $head[$h][1] = $langs->trans("DefaultSearchFilters");
951  $head[$h][2] = 'filters';
952  $h++;
953 
954  $head[$h][0] = DOL_URL_ROOT."/admin/defaultvalues.php?mode=sortorder";
955  $head[$h][1] = $langs->trans("DefaultSortOrder");
956  $head[$h][2] = 'sortorder';
957  $h++;
958 
959  if (!empty($conf->use_javascript_ajax)) {
960  $head[$h][0] = DOL_URL_ROOT."/admin/defaultvalues.php?mode=focus";
961  $head[$h][1] = $langs->trans("DefaultFocus");
962  $head[$h][2] = 'focus';
963  $h++;
964 
965  $head[$h][0] = DOL_URL_ROOT."/admin/defaultvalues.php?mode=mandatory";
966  $head[$h][1] = $langs->trans("DefaultMandatory");
967  $head[$h][2] = 'mandatory';
968  $h++;
969  }
970 
971  /*$head[$h][0] = DOL_URL_ROOT."/admin/translation.php?mode=searchkey";
972  $head[$h][1] = $langs->trans("TranslationKeySearch");
973  $head[$h][2] = 'searchkey';
974  $h++;*/
975 
976  complete_head_from_modules($conf, $langs, null, $head, $h, 'defaultvalues_admin');
977 
978  complete_head_from_modules($conf, $langs, null, $head, $h, 'defaultvalues_admin', 'remove');
979 
980 
981  return $head;
982 }
983 
984 
990 function listOfSessions()
991 {
992  global $conf;
993 
994  $arrayofSessions = array();
995  // session.save_path can be returned empty so we set a default location and work from there
996  $sessPath = '/tmp';
997  $iniPath = ini_get("session.save_path");
998  if ($iniPath) {
999  $sessPath = $iniPath;
1000  }
1001  $sessPath .= '/'; // We need the trailing slash
1002  dol_syslog('admin.lib:listOfSessions sessPath='.$sessPath);
1003 
1004  $dh = @opendir(dol_osencode($sessPath));
1005  if ($dh) {
1006  while (($file = @readdir($dh)) !== false) {
1007  if (preg_match('/^sess_/i', $file) && $file != "." && $file != "..") {
1008  $fullpath = $sessPath.$file;
1009  if (!@is_dir($fullpath) && is_readable($fullpath)) {
1010  $sessValues = file_get_contents($fullpath); // get raw session data
1011  // Example of possible value
1012  //$sessValues = 'newtoken|s:32:"1239f7a0c4b899200fe9ca5ea394f307";dol_loginmesg|s:0:"";newtoken|s:32:"1236457104f7ae0f328c2928973f3cb5";dol_loginmesg|s:0:"";token|s:32:"123615ad8d650c5cc4199b9a1a76783f";
1013  // dol_login|s:5:"admin";dol_authmode|s:8:"dolibarr";dol_tz|s:1:"1";dol_tz_string|s:13:"Europe/Berlin";dol_dst|i:0;dol_dst_observed|s:1:"1";dol_dst_first|s:0:"";dol_dst_second|s:0:"";dol_screenwidth|s:4:"1920";
1014  // dol_screenheight|s:3:"971";dol_company|s:12:"MyBigCompany";dol_entity|i:1;mainmenu|s:4:"home";leftmenuopened|s:10:"admintools";idmenu|s:0:"";leftmenu|s:10:"admintools";';
1015 
1016  if (preg_match('/dol_login/i', $sessValues) && // limit to dolibarr session
1017  (preg_match('/dol_entity\|i:'.$conf->entity.';/i', $sessValues) || preg_match('/dol_entity\|s:([0-9]+):"'.$conf->entity.'"/i', $sessValues)) && // limit to current entity
1018  preg_match('/dol_company\|s:([0-9]+):"('.getDolGlobalString('MAIN_INFO_SOCIETE_NOM').')"/i', $sessValues)) { // limit to company name
1019  $tmp = explode('_', $file);
1020  $idsess = $tmp[1];
1021  $regs = array();
1022  $loginfound = preg_match('/dol_login\|s:[0-9]+:"([A-Za-z0-9]+)"/i', $sessValues, $regs);
1023  if ($loginfound) {
1024  $arrayofSessions[$idsess]["login"] = $regs[1];
1025  }
1026  $arrayofSessions[$idsess]["age"] = time() - filectime($fullpath);
1027  $arrayofSessions[$idsess]["creation"] = filectime($fullpath);
1028  $arrayofSessions[$idsess]["modification"] = filemtime($fullpath);
1029  $arrayofSessions[$idsess]["raw"] = $sessValues;
1030  }
1031  }
1032  }
1033  }
1034  @closedir($dh);
1035  }
1036 
1037  return $arrayofSessions;
1038 }
1039 
1046 function purgeSessions($mysessionid)
1047 {
1048  global $conf;
1049 
1050  $sessPath = ini_get("session.save_path")."/";
1051  dol_syslog('admin.lib:purgeSessions mysessionid='.$mysessionid.' sessPath='.$sessPath);
1052 
1053  $error = 0;
1054 
1055  $dh = @opendir(dol_osencode($sessPath));
1056  if ($dh) {
1057  while (($file = @readdir($dh)) !== false) {
1058  if ($file != "." && $file != "..") {
1059  $fullpath = $sessPath.$file;
1060  if (!@is_dir($fullpath)) {
1061  $sessValues = file_get_contents($fullpath); // get raw session data
1062 
1063  if (preg_match('/dol_login/i', $sessValues) && // limit to dolibarr session
1064  preg_match('/dol_entity\|s:([0-9]+):"('.$conf->entity.')"/i', $sessValues) && // limit to current entity
1065  preg_match('/dol_company\|s:([0-9]+):"('.$conf->global->MAIN_INFO_SOCIETE_NOM.')"/i', $sessValues)) { // limit to company name
1066  $tmp = explode('_', $file);
1067  $idsess = $tmp[1];
1068  // We remove session if it's not ourself
1069  if ($idsess != $mysessionid) {
1070  $res = @unlink($fullpath);
1071  if (!$res) {
1072  $error++;
1073  }
1074  }
1075  }
1076  }
1077  }
1078  }
1079  @closedir($dh);
1080  }
1081 
1082  if (!$error) {
1083  return 1;
1084  } else {
1085  return -$error;
1086  }
1087 }
1088 
1089 
1090 
1099 function activateModule($value, $withdeps = 1, $noconfverification = 0)
1100 {
1101  global $db, $langs, $conf, $mysoc;
1102 
1103  $ret = array();
1104 
1105  // Check parameters
1106  if (empty($value)) {
1107  $ret['errors'][] = 'ErrorBadParameter';
1108  return $ret;
1109  }
1110 
1111  $ret = array('nbmodules'=>0, 'errors'=>array(), 'nbperms'=>0);
1112  $modName = $value;
1113  $modFile = $modName.".class.php";
1114 
1115  // Loop on each directory to fill $modulesdir
1116  $modulesdir = dolGetModulesDirs();
1117 
1118  // Loop on each modulesdir directories
1119  $found = false;
1120  foreach ($modulesdir as $dir) {
1121  if (file_exists($dir.$modFile)) {
1122  $found = @include_once $dir.$modFile;
1123  if ($found) {
1124  break;
1125  }
1126  }
1127  }
1128 
1129  $objMod = new $modName($db);
1130 
1131  // Test if PHP version ok
1132  $verphp = versionphparray();
1133  $vermin = isset($objMod->phpmin) ? $objMod->phpmin : 0;
1134  if (is_array($vermin) && versioncompare($verphp, $vermin) < 0) {
1135  $ret['errors'][] = $langs->trans("ErrorModuleRequirePHPVersion", versiontostring($vermin));
1136  return $ret;
1137  }
1138 
1139  // Test if Dolibarr version ok
1140  $verdol = versiondolibarrarray();
1141  $vermin = isset($objMod->need_dolibarr_version) ? $objMod->need_dolibarr_version : 0;
1142  //print 'version: '.versioncompare($verdol,$vermin).' - '.join(',',$verdol).' - '.join(',',$vermin);exit;
1143  if (is_array($vermin) && versioncompare($verdol, $vermin) < 0) {
1144  $ret['errors'][] = $langs->trans("ErrorModuleRequireDolibarrVersion", versiontostring($vermin));
1145  return $ret;
1146  }
1147 
1148  // Test if javascript requirement ok
1149  if (!empty($objMod->need_javascript_ajax) && empty($conf->use_javascript_ajax)) {
1150  $ret['errors'][] = $langs->trans("ErrorModuleRequireJavascript");
1151  return $ret;
1152  }
1153 
1154  $const_name = $objMod->const_name;
1155  if ($noconfverification == 0) {
1156  if (!empty($conf->global->$const_name)) {
1157  return $ret;
1158  }
1159  }
1160 
1161  $result = $objMod->init(); // Enable module
1162 
1163  if ($result <= 0) {
1164  $ret['errors'][] = $objMod->error;
1165  } else {
1166  if ($withdeps) {
1167  if (isset($objMod->depends) && is_array($objMod->depends) && !empty($objMod->depends)) {
1168  // Activation of modules this module depends on
1169  // this->depends may be array('modModule1', 'mmodModule2') or array('always1'=>"modModule1", 'FR'=>'modModule2')
1170  foreach ($objMod->depends as $key => $modulestring) {
1171  //var_dump((! is_numeric($key)) && ! preg_match('/^always/', $key) && $mysoc->country_code && ! preg_match('/^'.$mysoc->country_code.'/', $key));exit;
1172  if ((!is_numeric($key)) && !preg_match('/^always/', $key) && $mysoc->country_code && !preg_match('/^'.$mysoc->country_code.'/', $key)) {
1173  dol_syslog("We are not concerned by dependency with key=".$key." because our country is ".$mysoc->country_code);
1174  continue;
1175  }
1176  $activate = false;
1177  foreach ($modulesdir as $dir) {
1178  if (file_exists($dir.$modulestring.".class.php")) {
1179  $resarray = activateModule($modulestring);
1180  if (empty($resarray['errors'])) {
1181  $activate = true;
1182  } else {
1183  foreach ($resarray['errors'] as $errorMessage) {
1184  dol_syslog($errorMessage, LOG_ERR);
1185  }
1186  }
1187  break;
1188  }
1189  }
1190 
1191  if ($activate) {
1192  $ret['nbmodules'] += $resarray['nbmodules'];
1193  $ret['nbperms'] += $resarray['nbperms'];
1194  } else {
1195  $ret['errors'][] = $langs->trans('activateModuleDependNotSatisfied', $objMod->name, $modulestring);
1196  }
1197  }
1198  }
1199 
1200  if (isset($objMod->conflictwith) && is_array($objMod->conflictwith) && !empty($objMod->conflictwith)) {
1201  // Desactivation des modules qui entrent en conflit
1202  $num = count($objMod->conflictwith);
1203  for ($i = 0; $i < $num; $i++) {
1204  foreach ($modulesdir as $dir) {
1205  if (file_exists($dir.$objMod->conflictwith[$i].".class.php")) {
1206  unActivateModule($objMod->conflictwith[$i], 0);
1207  }
1208  }
1209  }
1210  }
1211  }
1212  }
1213 
1214  if (!count($ret['errors'])) {
1215  $ret['nbmodules']++;
1216  $ret['nbperms'] += (is_array($objMod->rights)?count($objMod->rights):0);
1217  }
1218 
1219  return $ret;
1220 }
1221 
1222 
1230 function unActivateModule($value, $requiredby = 1)
1231 {
1232  global $db, $modules, $conf;
1233 
1234  // Check parameters
1235  if (empty($value)) {
1236  return 'ErrorBadParameter';
1237  }
1238 
1239  $ret = '';
1240  $modName = $value;
1241  $modFile = $modName.".class.php";
1242 
1243  // Loop on each directory to fill $modulesdir
1244  $modulesdir = dolGetModulesDirs();
1245 
1246  // Loop on each modulesdir directories
1247  $found = false;
1248  foreach ($modulesdir as $dir) {
1249  if (file_exists($dir.$modFile)) {
1250  $found = @include_once $dir.$modFile;
1251  if ($found) {
1252  break;
1253  }
1254  }
1255  }
1256 
1257  if ($found) {
1258  $objMod = new $modName($db);
1259  $result = $objMod->remove();
1260  if ($result <= 0) {
1261  $ret = $objMod->error;
1262  }
1263  } else // We come here when we try to unactivate a module when module does not exists anymore in sources
1264  {
1265  //print $dir.$modFile;exit;
1266  // TODO Replace this after DolibarrModules is moved as abstract class with a try catch to show module we try to disable has not been found or could not be loaded
1267  include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
1268  $genericMod = new DolibarrModules($db);
1269  $genericMod->name = preg_replace('/^mod/i', '', $modName);
1270  $genericMod->rights_class = strtolower(preg_replace('/^mod/i', '', $modName));
1271  $genericMod->const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', $modName));
1272  dol_syslog("modules::unActivateModule Failed to find module file, we use generic function with name ".$modName);
1273  $genericMod->remove('');
1274  }
1275 
1276  // Disable modules that depends on module we disable
1277  if (!$ret && $requiredby && is_object($objMod) && is_array($objMod->requiredby)) {
1278  $countrb = count($objMod->requiredby);
1279  for ($i = 0; $i < $countrb; $i++) {
1280  //var_dump($objMod->requiredby[$i]);
1281  unActivateModule($objMod->requiredby[$i]);
1282  }
1283  }
1284 
1285  return $ret;
1286 }
1287 
1288 
1307 function complete_dictionary_with_modules(&$taborder, &$tabname, &$tablib, &$tabsql, &$tabsqlsort, &$tabfield, &$tabfieldvalue, &$tabfieldinsert, &$tabrowid, &$tabcond, &$tabhelp, &$tabcomplete)
1308 {
1309  global $db, $modules, $conf, $langs;
1310 
1311  dol_syslog("complete_dictionary_with_modules Search external modules to complete the list of dictionnary tables", LOG_DEBUG, 1);
1312 
1313  // Search modules
1314  $modulesdir = dolGetModulesDirs();
1315  $i = 0; // is a sequencer of modules found
1316  $j = 0; // j is module number. Automatically affected if module number not defined.
1317 
1318  foreach ($modulesdir as $dir) {
1319  // Load modules attributes in arrays (name, numero, orders) from dir directory
1320  //print $dir."\n<br>";
1321  dol_syslog("Scan directory ".$dir." for modules");
1322  $handle = @opendir(dol_osencode($dir));
1323  if (is_resource($handle)) {
1324  while (($file = readdir($handle)) !== false) {
1325  //print "$i ".$file."\n<br>";
1326  if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
1327  $modName = substr($file, 0, dol_strlen($file) - 10);
1328 
1329  if ($modName) {
1330  include_once $dir.$file;
1331  $objMod = new $modName($db);
1332 
1333  if ($objMod->numero > 0) {
1334  $j = $objMod->numero;
1335  } else {
1336  $j = 1000 + $i;
1337  }
1338 
1339  $modulequalified = 1;
1340 
1341  // We discard modules according to features level (PS: if module is activated we always show it)
1342  $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($objMod)));
1343  if ($objMod->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2 && empty(getDolGlobalString($const_name))) {
1344  $modulequalified = 0;
1345  }
1346  if ($objMod->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1 && empty(getDolGlobalString($const_name))) {
1347  $modulequalified = 0;
1348  }
1349  //If module is not activated disqualified
1350  if (empty(getDolGlobalString($const_name))) {
1351  $modulequalified = 0;
1352  }
1353 
1354  if ($modulequalified) {
1355  // Load languages files of module
1356  if (isset($objMod->langfiles) && is_array($objMod->langfiles)) {
1357  foreach ($objMod->langfiles as $langfile) {
1358  $langs->load($langfile);
1359  }
1360  }
1361 
1362  // Complete the arrays &$tabname,&$tablib,&$tabsql,&$tabsqlsort,&$tabfield,&$tabfieldvalue,&$tabfieldinsert,&$tabrowid,&$tabcond
1363  if (empty($objMod->dictionaries) && !empty($objMod->dictionnaries)) {
1364  $objMod->dictionaries = $objMod->dictionnaries; // For backward compatibility
1365  }
1366 
1367  if (!empty($objMod->dictionaries)) {
1368  //var_dump($objMod->dictionaries['tabname']);
1369  $nbtabname = $nbtablib = $nbtabsql = $nbtabsqlsort = $nbtabfield = $nbtabfieldvalue = $nbtabfieldinsert = $nbtabrowid = $nbtabcond = $nbtabfieldcheck = $nbtabhelp = 0;
1370  $tabnamerelwithkey = array();
1371  foreach ($objMod->dictionaries['tabname'] as $key => $val) {
1372  $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $val);
1373  $nbtabname++;
1374  $taborder[] = max($taborder) + 1;
1375  $tabname[] = $val;
1376  $tabnamerelwithkey[$key] = $val;
1377  $tabcomplete[$tmptablename]['picto'] = $objMod->picto;
1378  } // Position
1379  foreach ($objMod->dictionaries['tablib'] as $key => $val) {
1380  $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
1381  $nbtablib++;
1382  $tablib[] = $val;
1383  $tabcomplete[$tmptablename]['lib'] = $val;
1384  }
1385  foreach ($objMod->dictionaries['tabsql'] as $key => $val) {
1386  $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
1387  $nbtabsql++;
1388  $tabsql[] = $val;
1389  $tabcomplete[$tmptablename]['sql'] = $val;
1390  }
1391  foreach ($objMod->dictionaries['tabsqlsort'] as $key => $val) {
1392  $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
1393  $nbtabsqlsort++;
1394  $tabsqlsort[] = $val;
1395  $tabcomplete[$tmptablename]['sqlsort'] = $val;
1396  }
1397  foreach ($objMod->dictionaries['tabfield'] as $key => $val) {
1398  $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
1399  $nbtabfield++;
1400  $tabfield[] = $val;
1401  $tabcomplete[$tmptablename]['field'] = $val;
1402  }
1403  foreach ($objMod->dictionaries['tabfieldvalue'] as $key => $val) {
1404  $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
1405  $nbtabfieldvalue++;
1406  $tabfieldvalue[] = $val;
1407  $tabcomplete[$tmptablename]['value'] = $val;
1408  }
1409  foreach ($objMod->dictionaries['tabfieldinsert'] as $key => $val) {
1410  $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
1411  $nbtabfieldinsert++;
1412  $tabfieldinsert[] = $val;
1413  $tabcomplete[$tmptablename]['fieldinsert'] = $val;
1414  }
1415  foreach ($objMod->dictionaries['tabrowid'] as $key => $val) {
1416  $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
1417  $nbtabrowid++;
1418  $tabrowid[] = $val;
1419  $tabcomplete[$tmptablename]['rowid'] = $val;
1420  }
1421  foreach ($objMod->dictionaries['tabcond'] as $key => $val) {
1422  $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
1423  $nbtabcond++;
1424  $tabcond[] = $val;
1425  $tabcomplete[$tmptablename]['rowid'] = $val;
1426  }
1427  if (!empty($objMod->dictionaries['tabhelp'])) {
1428  foreach ($objMod->dictionaries['tabhelp'] as $key => $val) {
1429  $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
1430  $nbtabhelp++;
1431  $tabhelp[] = $val;
1432  $tabcomplete[$tmptablename]['help'] = $val;
1433  }
1434  }
1435  if (!empty($objMod->dictionaries['tabfieldcheck'])) {
1436  foreach ($objMod->dictionaries['tabfieldcheck'] as $key => $val) {
1437  $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
1438  $nbtabfieldcheck++;
1439  $tabcomplete[$tmptablename]['fieldcheck'] = $val;
1440  }
1441  }
1442 
1443  if ($nbtabname != $nbtablib || $nbtablib != $nbtabsql || $nbtabsql != $nbtabsqlsort) {
1444  print 'Error in descriptor of module '.$const_name.'. Array ->dictionaries has not same number of record for key "tabname", "tablib", "tabsql" and "tabsqlsort"';
1445  //print "$const_name: $nbtabname=$nbtablib=$nbtabsql=$nbtabsqlsort=$nbtabfield=$nbtabfieldvalue=$nbtabfieldinsert=$nbtabrowid=$nbtabcond=$nbtabfieldcheck=$nbtabhelp\n";
1446  } else {
1447  $taborder[] = 0; // Add an empty line
1448  }
1449  }
1450 
1451  $j++;
1452  $i++;
1453  } else {
1454  dol_syslog("Module ".get_class($objMod)." not qualified");
1455  }
1456  }
1457  }
1458  }
1459  closedir($handle);
1460  } else {
1461  dol_syslog("htdocs/admin/modules.php: Failed to open directory ".$dir.". See permission and open_basedir option.", LOG_WARNING);
1462  }
1463  }
1464 
1465  dol_syslog("", LOG_DEBUG, -1);
1466 
1467  return 1;
1468 }
1469 
1476 function activateModulesRequiredByCountry($country_code)
1477 {
1478  global $db, $conf, $langs;
1479 
1480  $modulesdir = dolGetModulesDirs();
1481 
1482  foreach ($modulesdir as $dir) {
1483  // Load modules attributes in arrays (name, numero, orders) from dir directory
1484  dol_syslog("Scan directory ".$dir." for modules");
1485  $handle = @opendir(dol_osencode($dir));
1486  if (is_resource($handle)) {
1487  while (($file = readdir($handle)) !== false) {
1488  if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
1489  $modName = substr($file, 0, dol_strlen($file) - 10);
1490 
1491  if ($modName) {
1492  include_once $dir.$file;
1493  $objMod = new $modName($db);
1494 
1495  $modulequalified = 1;
1496 
1497  // We discard modules according to features level (PS: if module is activated we always show it)
1498  $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($objMod)));
1499 
1500  if ($objMod->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
1501  $modulequalified = 0;
1502  }
1503  if ($objMod->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
1504  $modulequalified = 0;
1505  }
1506  if (!empty($conf->global->$const_name)) {
1507  $modulequalified = 0; // already activated
1508  }
1509 
1510  if ($modulequalified) {
1511  // Load languages files of module
1512  if (isset($objMod->automatic_activation) && is_array($objMod->automatic_activation) && isset($objMod->automatic_activation[$country_code])) {
1513  activateModule($modName);
1514 
1515  setEventMessages($objMod->automatic_activation[$country_code], null, 'warnings');
1516  }
1517  } else {
1518  dol_syslog("Module ".get_class($objMod)." not qualified");
1519  }
1520  }
1521  }
1522  }
1523  closedir($handle);
1524  } else {
1525  dol_syslog("htdocs/admin/modules.php: Failed to open directory ".$dir.". See permission and open_basedir option.", LOG_WARNING);
1526  }
1527  }
1528 
1529  return 1;
1530 }
1531 
1538 function complete_elementList_with_modules(&$elementList)
1539 {
1540  global $db, $modules, $conf, $langs;
1541 
1542  // Search modules
1543  $filename = array();
1544  $modules = array();
1545  $orders = array();
1546  $categ = array();
1547  $dirmod = array();
1548 
1549  $i = 0; // is a sequencer of modules found
1550  $j = 0; // j is module number. Automatically affected if module number not defined.
1551 
1552  dol_syslog("complete_elementList_with_modules Search external modules to complete the list of contact element", LOG_DEBUG, 1);
1553 
1554  $modulesdir = dolGetModulesDirs();
1555 
1556  foreach ($modulesdir as $dir) {
1557  // Load modules attributes in arrays (name, numero, orders) from dir directory
1558  //print $dir."\n<br>";
1559  dol_syslog("Scan directory ".$dir." for modules");
1560  $handle = @opendir(dol_osencode($dir));
1561  if (is_resource($handle)) {
1562  while (($file = readdir($handle)) !== false) {
1563  //print "$i ".$file."\n<br>";
1564  if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
1565  $modName = substr($file, 0, dol_strlen($file) - 10);
1566 
1567  if ($modName) {
1568  include_once $dir.$file;
1569  $objMod = new $modName($db);
1570 
1571  if ($objMod->numero > 0) {
1572  $j = $objMod->numero;
1573  } else {
1574  $j = 1000 + $i;
1575  }
1576 
1577  $modulequalified = 1;
1578 
1579  // We discard modules according to features level (PS: if module is activated we always show it)
1580  $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($objMod)));
1581  if ($objMod->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2 && getDolGlobalString($const_name)) {
1582  $modulequalified = 0;
1583  }
1584  if ($objMod->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1 && getDolGlobalString($const_name)) {
1585  $modulequalified = 0;
1586  }
1587  //If module is not activated disqualified
1588  if (empty($conf->global->$const_name)) {
1589  $modulequalified = 0;
1590  }
1591 
1592  if ($modulequalified) {
1593  // Load languages files of module
1594  if (isset($objMod->langfiles) && is_array($objMod->langfiles)) {
1595  foreach ($objMod->langfiles as $langfile) {
1596  $langs->load($langfile);
1597  }
1598  }
1599 
1600  $modules[$i] = $objMod;
1601  $filename[$i] = $modName;
1602  $orders[$i] = $objMod->family."_".$j; // Sort on family then module number
1603  $dirmod[$i] = $dir;
1604  //print "x".$modName." ".$orders[$i]."\n<br>";
1605 
1606  if (!empty($objMod->module_parts['contactelement'])) {
1607  if (is_array($objMod->module_parts['contactelement'])) {
1608  foreach ($objMod->module_parts['contactelement'] as $elem => $title) {
1609  $elementList[$elem] = $langs->trans($title);
1610  }
1611  } else {
1612  $elementList[$objMod->name] = $langs->trans($objMod->name);
1613  }
1614  }
1615 
1616  $j++;
1617  $i++;
1618  } else {
1619  dol_syslog("Module ".get_class($objMod)." not qualified");
1620  }
1621  }
1622  }
1623  }
1624  closedir($handle);
1625  } else {
1626  dol_syslog("htdocs/admin/modules.php: Failed to open directory ".$dir.". See permission and open_basedir option.", LOG_WARNING);
1627  }
1628  }
1629 
1630  dol_syslog("", LOG_DEBUG, -1);
1631 
1632  return 1;
1633 }
1634 
1645 function form_constantes($tableau, $strictw3c = 0, $helptext = '', $text = 'Value')
1646 {
1647  global $db, $langs, $conf, $user;
1648  global $_Avery_Labels;
1649 
1650  $form = new Form($db);
1651 
1652  if (empty($strictw3c)) {
1653  dol_syslog("Warning: Function form_constantes is calle with parameter strictw3c = 0, this is deprecated. Value must be 2 now.", LOG_DEBUG);
1654  }
1655  if (!empty($strictw3c) && $strictw3c == 1) {
1656  print "\n".'<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
1657  print '<input type="hidden" name="token" value="'.newToken().'">';
1658  print '<input type="hidden" name="action" value="updateall">';
1659  }
1660 
1661  print '<div class="div-table-responsive-no-min">';
1662  print '<table class="noborder centpercent">';
1663  print '<tr class="liste_titre">';
1664  print '<td class="">'.$langs->trans("Description").'</td>';
1665  print '<td>';
1666  $text = $langs->trans($text);
1667  print $form->textwithpicto($text, $helptext, 1, 'help', '', 0, 2, 'idhelptext');
1668  print '</td>';
1669  if (empty($strictw3c)) {
1670  print '<td class="center" width="80">'.$langs->trans("Action").'</td>';
1671  }
1672  print "</tr>\n";
1673 
1674  $label = '';
1675  foreach ($tableau as $key => $const) { // Loop on each param
1676  $label = '';
1677  // $const is a const key like 'MYMODULE_ABC'
1678  if (is_numeric($key)) { // Very old behaviour
1679  $type = 'string';
1680  } else {
1681  if (is_array($const)) {
1682  $type = $const['type'];
1683  $label = $const['label'];
1684  $const = $key;
1685  } else {
1686  $type = $const;
1687  $const = $key;
1688  }
1689  }
1690 
1691  $sql = "SELECT ";
1692  $sql .= "rowid";
1693  $sql .= ", ".$db->decrypt('name')." as name";
1694  $sql .= ", ".$db->decrypt('value')." as value";
1695  $sql .= ", type";
1696  $sql .= ", note";
1697  $sql .= " FROM ".MAIN_DB_PREFIX."const";
1698  $sql .= " WHERE ".$db->decrypt('name')." = '".$db->escape($const)."'";
1699  $sql .= " AND entity IN (0, ".$conf->entity.")";
1700  $sql .= " ORDER BY name ASC, entity DESC";
1701  $result = $db->query($sql);
1702 
1703  dol_syslog("List params", LOG_DEBUG);
1704  if ($result) {
1705  $obj = $db->fetch_object($result); // Take first result of select
1706 
1707  if (empty($obj)) { // If not yet into table
1708  $obj = (object) array('rowid'=>'', 'name'=>$const, 'value'=>'', 'type'=>$type, 'note'=>'');
1709  }
1710 
1711  if (empty($strictw3c)) {
1712  print "\n".'<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
1713  print '<input type="hidden" name="token" value="'.newToken().'">';
1714  print '<input type="hidden" name="page_y" value="'.newToken().'">';
1715  }
1716 
1717  print '<tr class="oddeven">';
1718 
1719  // Show label of parameter
1720  print '<td>';
1721  if (empty($strictw3c)) {
1722  print '<input type="hidden" name="action" value="update">';
1723  }
1724  print '<input type="hidden" name="rowid'.(empty($strictw3c) ? '' : '[]').'" value="'.$obj->rowid.'">';
1725  print '<input type="hidden" name="constname'.(empty($strictw3c) ? '' : '[]').'" value="'.$const.'">';
1726  print '<input type="hidden" name="constnote_'.$obj->name.'" value="'.nl2br(dol_escape_htmltag($obj->note)).'">';
1727  print '<input type="hidden" name="consttype_'.$obj->name.'" value="'.($obj->type ? $obj->type : 'string').'">';
1728  if (!empty($tableau[$key]['tooltip'])) {
1729  print $form->textwithpicto($label ? $label : $langs->trans('Desc'.$const), $tableau[$key]['tooltip']);
1730  } else {
1731  print ($label ? $label : $langs->trans('Desc'.$const));
1732  }
1733 
1734  if ($const == 'ADHERENT_MAILMAN_URL') {
1735  print '. '.$langs->trans("Example").': <a href="#" id="exampleclick1">'.img_down().'</a><br>';
1736  //print 'http://lists.exampe.com/cgi-bin/mailman/admin/%LISTE%/members?adminpw=%MAILMAN_ADMINPW%&subscribees=%EMAIL%&send_welcome_msg_to_this_batch=1';
1737  print '<div id="example1" class="hidden">';
1738  print 'http://lists.example.com/cgi-bin/mailman/admin/%LISTE%/members/add?subscribees_upload=%EMAIL%&amp;adminpw=%MAILMAN_ADMINPW%&amp;subscribe_or_invite=0&amp;send_welcome_msg_to_this_batch=0&amp;notification_to_list_owner=0';
1739  print '</div>';
1740  } elseif ($const == 'ADHERENT_MAILMAN_UNSUB_URL') {
1741  print '. '.$langs->trans("Example").': <a href="#" id="exampleclick2">'.img_down().'</a><br>';
1742  print '<div id="example2" class="hidden">';
1743  print 'http://lists.example.com/cgi-bin/mailman/admin/%LISTE%/members/remove?unsubscribees_upload=%EMAIL%&amp;adminpw=%MAILMAN_ADMINPW%&amp;send_unsub_ack_to_this_batch=0&amp;send_unsub_notifications_to_list_owner=0';
1744  print '</div>';
1745  //print 'http://lists.example.com/cgi-bin/mailman/admin/%LISTE%/members/remove?adminpw=%MAILMAN_ADMINPW%&unsubscribees=%EMAIL%';
1746  } elseif ($const == 'ADHERENT_MAILMAN_LISTS') {
1747  print '. '.$langs->trans("Example").': <a href="#" id="exampleclick3">'.img_down().'</a><br>';
1748  print '<div id="example3" class="hidden">';
1749  print 'mymailmanlist<br>';
1750  print 'mymailmanlist1,mymailmanlist2<br>';
1751  print 'TYPE:Type1:mymailmanlist1,TYPE:Type2:mymailmanlist2<br>';
1752  if (isModEnabled('categorie')) {
1753  print 'CATEG:Categ1:mymailmanlist1,CATEG:Categ2:mymailmanlist2<br>';
1754  }
1755  print '</div>';
1756  //print 'http://lists.example.com/cgi-bin/mailman/admin/%LISTE%/members/remove?adminpw=%MAILMAN_ADMINPW%&unsubscribees=%EMAIL%';
1757  } elseif ($const == 'ADHERENT_MAIL_FROM') {
1758  print ' '.img_help(1, $langs->trans("EMailHelpMsgSPFDKIM"));
1759  }
1760 
1761  print "</td>\n";
1762 
1763  // Value
1764  if ($const == 'ADHERENT_CARD_TYPE' || $const == 'ADHERENT_ETIQUETTE_TYPE') {
1765  print '<td>';
1766  // List of possible labels (defined into $_Avery_Labels variable set into format_cards.lib.php)
1767  require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
1768  $arrayoflabels = array();
1769  foreach (array_keys($_Avery_Labels) as $codecards) {
1770  $arrayoflabels[$codecards] = $_Avery_Labels[$codecards]['name'];
1771  }
1772  print $form->selectarray('constvalue'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')), $arrayoflabels, ($obj->value ? $obj->value : 'CARD'), 1, 0, 0);
1773  print '<input type="hidden" name="consttype" value="yesno">';
1774  print '<input type="hidden" name="constnote'.(empty($strictw3c) ? '' : '[]').'" value="'.nl2br(dol_escape_htmltag($obj->note)).'">';
1775  print '</td>';
1776  } else {
1777  print '<td>';
1778  print '<input type="hidden" name="consttype'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')).'" value="'.($obj->type ? $obj->type : 'string').'">';
1779  print '<input type="hidden" name="constnote'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')).'" value="'.nl2br(dol_escape_htmltag($obj->note)).'">';
1780  if ($obj->type == 'textarea' || in_array($const, array('ADHERENT_CARD_TEXT', 'ADHERENT_CARD_TEXT_RIGHT', 'ADHERENT_ETIQUETTE_TEXT'))) {
1781  print '<textarea class="flat" name="constvalue'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')).'" cols="50" rows="5" wrap="soft">'."\n";
1782  print $obj->value;
1783  print "</textarea>\n";
1784  } elseif ($obj->type == 'html') {
1785  require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
1786  $doleditor = new DolEditor('constvalue'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')), $obj->value, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
1787  $doleditor->Create();
1788  } elseif ($obj->type == 'yesno') {
1789  print $form->selectyesno('constvalue'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')), $obj->value, 1);
1790  } elseif (preg_match('/emailtemplate/', $obj->type)) {
1791  include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
1792  $formmail = new FormMail($db);
1793 
1794  $tmp = explode(':', $obj->type);
1795 
1796  $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, -1); // We set lang=null to get in priority record with no lang
1797  //$arraydefaultmessage = $formmail->getEMailTemplate($db, $tmp[1], $user, null, 0, 1, '');
1798  $arrayofmessagename = array();
1799  if (is_array($formmail->lines_model)) {
1800  foreach ($formmail->lines_model as $modelmail) {
1801  //var_dump($modelmail);
1802  $moreonlabel = '';
1803  if (!empty($arrayofmessagename[$modelmail->label])) {
1804  $moreonlabel = ' <span class="opacitymedium">('.$langs->trans("SeveralLangugeVariatFound").')</span>';
1805  }
1806  // The 'label' is the key that is unique if we exclude the language
1807  $arrayofmessagename[$modelmail->label.':'.$tmp[1]] = $langs->trans(preg_replace('/\(|\)/', '', $modelmail->label)).$moreonlabel;
1808  }
1809  }
1810  //var_dump($arraydefaultmessage);
1811  //var_dump($arrayofmessagename);
1812  print $form->selectarray('constvalue'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')), $arrayofmessagename, $obj->value.':'.$tmp[1], 'None', 0, 0, '', 0, 0, 0, '', '', 1);
1813  } elseif (preg_match('/MAIL_FROM$/i', $const)) {
1814  print img_picto('', 'email', 'class="pictofixedwidth"').'<input type="text" class="flat minwidth300" name="constvalue'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')).'" value="'.dol_escape_htmltag($obj->value).'">';
1815  } else { // type = 'string' ou 'chaine'
1816  print '<input type="text" class="flat minwidth300" name="constvalue'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')).'" value="'.dol_escape_htmltag($obj->value).'">';
1817  }
1818  print '</td>';
1819  }
1820 
1821  // Submit
1822  if (empty($strictw3c)) {
1823  print '<td class="center">';
1824  print '<input type="submit" class="button small reposition" value="'.$langs->trans("Update").'" name="update">';
1825  print "</td>";
1826  }
1827 
1828  print "</tr>\n";
1829 
1830  if (empty($strictw3c)) {
1831  print "</form>\n";
1832  }
1833  }
1834  }
1835  print '</table>';
1836  print '</div>';
1837 
1838  if (!empty($strictw3c) && $strictw3c == 1) {
1839  print '<div align="center"><input type="submit" class="button small reposition" value="'.$langs->trans("Update").'" name="update"></div>';
1840  print "</form>\n";
1841  }
1842 }
1843 
1844 
1852 {
1853  global $conf, $langs;
1854 
1855  $text = $langs->trans("OnlyFollowingModulesAreOpenedToExternalUsers");
1856  $listofmodules = explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL); // List of modules qualified for external user management
1857 
1858  $i = 0;
1859  if (!empty($modules)) {
1860  $tmpmodules = dol_sort_array($modules, 'module_position');
1861  foreach ($tmpmodules as $module) { // Loop on array of modules
1862  $moduleconst = $module->const_name;
1863  $modulename = strtolower($module->name);
1864  //print 'modulename='.$modulename;
1865 
1866  //if (empty($conf->global->$moduleconst)) continue;
1867  if (!in_array($modulename, $listofmodules)) {
1868  continue;
1869  }
1870  //var_dump($modulename.' - '.$langs->trans('Module'.$module->numero.'Name'));
1871 
1872  if ($i > 0) {
1873  $text .= ', ';
1874  } else {
1875  $text .= ' ';
1876  }
1877  $i++;
1878 
1879  $tmptext = $langs->trans('Module'.$module->numero.'Name');
1880  if ($tmptext != 'Module'.$module->numero.'Name') {
1881  $text .= $langs->trans('Module'.$module->numero.'Name');
1882  } else {
1883  $text .= $langs->trans($module->name);
1884  }
1885  }
1886  }
1887 
1888  return $text;
1889 }
1890 
1891 
1901 function addDocumentModel($name, $type, $label = '', $description = '')
1902 {
1903  global $db, $conf;
1904 
1905  $db->begin();
1906 
1907  $sql = "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity, libelle, description)";
1908  $sql .= " VALUES ('".$db->escape($name)."','".$db->escape($type)."',".((int) $conf->entity).", ";
1909  $sql .= ($label ? "'".$db->escape($label)."'" : 'null').", ";
1910  $sql .= (!empty($description) ? "'".$db->escape($description)."'" : "null");
1911  $sql .= ")";
1912 
1913  dol_syslog("admin.lib::addDocumentModel", LOG_DEBUG);
1914  $resql = $db->query($sql);
1915  if ($resql) {
1916  $db->commit();
1917  return 1;
1918  } else {
1919  dol_print_error($db);
1920  $db->rollback();
1921  return -1;
1922  }
1923 }
1924 
1932 function delDocumentModel($name, $type)
1933 {
1934  global $db, $conf;
1935 
1936  $db->begin();
1937 
1938  $sql = "DELETE FROM ".MAIN_DB_PREFIX."document_model";
1939  $sql .= " WHERE nom = '".$db->escape($name)."'";
1940  $sql .= " AND type = '".$db->escape($type)."'";
1941  $sql .= " AND entity = ".((int) $conf->entity);
1942 
1943  dol_syslog("admin.lib::delDocumentModel", LOG_DEBUG);
1944  $resql = $db->query($sql);
1945  if ($resql) {
1946  $db->commit();
1947  return 1;
1948  } else {
1949  dol_print_error($db);
1950  $db->rollback();
1951  return -1;
1952  }
1953 }
1954 
1955 
1961 function phpinfo_array()
1962 {
1963  ob_start();
1964  phpinfo();
1965  $phpinfostring = ob_get_contents();
1966  ob_end_clean();
1967 
1968  $info_arr = array();
1969  $info_lines = explode("\n", strip_tags($phpinfostring, "<tr><td><h2>"));
1970  $cat = "General";
1971  foreach ($info_lines as $line) {
1972  // new cat?
1973  $title = array();
1974  preg_match("~<h2>(.*)</h2>~", $line, $title) ? $cat = $title[1] : null;
1975  $val = array();
1976  if (preg_match("~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~", $line, $val)) {
1977  $info_arr[trim($cat)][trim($val[1])] = $val[2];
1978  } elseif (preg_match("~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~", $line, $val)) {
1979  $info_arr[trim($cat)][trim($val[1])] = array("local" => $val[2], "master" => $val[3]);
1980  }
1981  }
1982  return $info_arr;
1983 }
1984 
1991 {
1992  global $langs, $conf;
1993 
1994  $h = 0;
1995  $head = array();
1996 
1997  $head[$h][0] = DOL_URL_ROOT."/admin/company.php";
1998  $head[$h][1] = $langs->trans("Company");
1999  $head[$h][2] = 'company';
2000  $h++;
2001 
2002  $head[$h][0] = DOL_URL_ROOT."/admin/openinghours.php";
2003  $head[$h][1] = $langs->trans("OpeningHours");
2004  $head[$h][2] = 'openinghours';
2005  $h++;
2006 
2007  $head[$h][0] = DOL_URL_ROOT."/admin/accountant.php";
2008  $head[$h][1] = $langs->trans("Accountant");
2009  $head[$h][2] = 'accountant';
2010  $h++;
2011 
2012  $head[$h][0] = DOL_URL_ROOT."/admin/company_socialnetworks.php";
2013  $head[$h][1] = $langs->trans("SocialNetworksInformation");
2014  $head[$h][2] = 'socialnetworks';
2015  $h++;
2016 
2017  complete_head_from_modules($conf, $langs, null, $head, $h, 'mycompany_admin', 'add');
2018 
2019  complete_head_from_modules($conf, $langs, null, $head, $h, 'mycompany_admin', 'remove');
2020 
2021  return $head;
2022 }
2023 
2030 {
2031  global $langs, $conf, $user;
2032 
2033  $h = 0;
2034  $head = array();
2035 
2036  if (!empty($user->admin) && (empty($_SESSION['leftmenu']) || $_SESSION['leftmenu'] != 'email_templates')) {
2037  $head[$h][0] = DOL_URL_ROOT."/admin/mails.php";
2038  $head[$h][1] = $langs->trans("OutGoingEmailSetup");
2039  $head[$h][2] = 'common';
2040  $h++;
2041 
2042  if (isModEnabled('mailing')) {
2043  $head[$h][0] = DOL_URL_ROOT."/admin/mails_emailing.php";
2044  $head[$h][1] = $langs->trans("OutGoingEmailSetupForEmailing", $langs->transnoentitiesnoconv("EMailing"));
2045  $head[$h][2] = 'common_emailing';
2046  $h++;
2047  }
2048 
2049  if (isModEnabled('ticket')) {
2050  $head[$h][0] = DOL_URL_ROOT."/admin/mails_ticket.php";
2051  $head[$h][1] = $langs->trans("OutGoingEmailSetupForEmailing", $langs->transnoentitiesnoconv("Ticket"));
2052  $head[$h][2] = 'common_ticket';
2053  $h++;
2054  }
2055  }
2056 
2057  // admin and non admin can view this menu entry, but it is not shown yet when we on user menu "Email templates"
2058  if (empty($_SESSION['leftmenu']) || $_SESSION['leftmenu'] != 'email_templates') {
2059  $head[$h][0] = DOL_URL_ROOT."/admin/mails_senderprofile_list.php";
2060  $head[$h][1] = $langs->trans("EmailSenderProfiles");
2061  $head[$h][2] = 'senderprofiles';
2062  $h++;
2063  }
2064 
2065  $head[$h][0] = DOL_URL_ROOT."/admin/mails_templates.php";
2066  $head[$h][1] = $langs->trans("EMailTemplates");
2067  $head[$h][2] = 'templates';
2068  $h++;
2069 
2070  $head[$h][0] = DOL_URL_ROOT."/admin/mails_ingoing.php";
2071  $head[$h][1] = $langs->trans("InGoingEmailSetup", $langs->transnoentitiesnoconv("EMailing"));
2072  $head[$h][2] = 'common_ingoing';
2073  $h++;
2074 
2075  complete_head_from_modules($conf, $langs, null, $head, $h, 'email_admin', 'remove');
2076 
2077  return $head;
2078 }
activateModule
activateModule($value, $withdeps=1, $noconfverification=0)
Enable a module.
Definition: admin.lib.php:1099
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1534
versioncompare
versioncompare($versionarray1, $versionarray2)
Compare 2 versions (stored into 2 arrays).
Definition: admin.lib.php:67
security_prepare_head
security_prepare_head()
Prepare array with list of tabs.
Definition: admin.lib.php:795
dolibarr_del_const
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
Definition: admin.lib.php:557
dol_osencode
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Definition: functions.lib.php:8918
$sql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:745
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:5031
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:8801
modules_prepare_head
modules_prepare_head($nbofactivatedmodules, $nboftotalmodules, $nbmodulesnotautoenabled)
Prepare array with list of tabs.
Definition: admin.lib.php:705
complete_dictionary_with_modules
complete_dictionary_with_modules(&$taborder, &$tabname, &$tablib, &$tabsql, &$tabsqlsort, &$tabfield, &$tabfieldvalue, &$tabfieldinsert, &$tabrowid, &$tabcond, &$tabhelp, &$tabcomplete)
Add external modules to list of dictionaries.
Definition: admin.lib.php:1307
phpinfo_array
phpinfo_array()
Return the php_info into an array.
Definition: admin.lib.php:1961
activateModulesRequiredByCountry
activateModulesRequiredByCountry($country_code)
Activate external modules mandatory when country is country_code.
Definition: admin.lib.php:1476
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
defaultvalues_prepare_head
defaultvalues_prepare_head()
Prepare array with list of tabs.
Definition: admin.lib.php:938
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:4061
versionphparray
versionphparray()
Return version PHP.
Definition: admin.lib.php:129
complete_head_from_modules
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add', $filterorigmodule='')
Complete or removed entries into a head array (used to build tabs).
Definition: functions.lib.php:9465
delDocumentModel
delDocumentModel($name, $type)
Delete document model used by doc generator.
Definition: admin.lib.php:1932
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1669
dolEncrypt
dolEncrypt($chain, $key='', $ciphering='AES-256-CTR', $forceseed='')
Encode a string with a symetric encryption.
Definition: security.lib.php:120
dolibarr_get_const
dolibarr_get_const($db, $name, $entity=1)
Get the value of a setup constant from database.
Definition: admin.lib.php:597
complete_elementList_with_modules
complete_elementList_with_modules(&$elementList)
Search external modules to complete the list of contact element.
Definition: admin.lib.php:1538
unActivateModule
unActivateModule($value, $requiredby=1)
Disable a module.
Definition: admin.lib.php:1230
img_down
img_down($titlealt='default', $selected=0, $moreclass='')
Show down arrow logo.
Definition: functions.lib.php:4783
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3923
versiondolibarrarray
versiondolibarrarray()
Return version Dolibarr.
Definition: admin.lib.php:140
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:166
dolGetModulesDirs
dolGetModulesDirs($subdir='')
Return list of modules directories.
Definition: functions2.lib.php:80
listOfSessions
listOfSessions()
Return list of session.
Definition: admin.lib.php:990
getDolGlobalString
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:101
dolibarr_set_const
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
Definition: admin.lib.php:633
purgeSessions
purgeSessions($mysessionid)
Purge existing sessions.
Definition: admin.lib.php:1046
email_admin_prepare_head
email_admin_prepare_head()
Return array head with list of tabs to view object informations.
Definition: admin.lib.php:2029
addDocumentModel
addDocumentModel($name, $type, $label='', $description='')
Add document model used by doc generator.
Definition: admin.lib.php:1901
modulehelp_prepare_head
modulehelp_prepare_head($object)
Prepare array with list of tabs.
Definition: admin.lib.php:870
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
form_constantes
form_constantes($tableau, $strictw3c=0, $helptext='', $text='Value')
Show array with constants to edit.
Definition: admin.lib.php:1645
run_sql
run_sql($sqlfile, $silent=1, $entity='', $usesavepoint=1, $handler='', $okerror='default', $linelengthlimit=32768, $nocommentremoval=0, $offsetforchartofaccount=0, $colspan=0, $onlysqltoimportwebsite=0)
Launch a sql file.
Definition: admin.lib.php:168
showModulesExludedForExternal
showModulesExludedForExternal($modules)
Show array with constants to edit.
Definition: admin.lib.php:1851
dolDecrypt
dolDecrypt($chain, $key='')
Decode a string with a symetric encryption.
Definition: security.lib.php:174
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8552
FormMail
Classe permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new For...
Definition: html.formmail.class.php:38
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:115
versiontostring
versiontostring($versionarray)
Renvoi une version en chaine depuis une version en tableau.
Definition: admin.lib.php:37
DolEditor
Class to manage a WYSIWYG editor.
Definition: doleditor.class.php:30
translation_prepare_head
translation_prepare_head()
Prepare array with list of tabs.
Definition: admin.lib.php:908
DolibarrModules
Class DolibarrModules.
Definition: DolibarrModules.class.php:37
dol_htmlentities
dol_htmlentities($string, $flags=ENT_QUOTES|ENT_SUBSTITUTE, $encoding='UTF-8', $double_encode=false)
Replace htmlentities functions.
Definition: functions.lib.php:7457
company_admin_prepare_head
company_admin_prepare_head()
Return array head with list of tabs to view object informations.
Definition: admin.lib.php:1990
ihm_prepare_head
ihm_prepare_head()
Prepare array with list of tabs.
Definition: admin.lib.php:750