admin.lib.php 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  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. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. * or see https://www.gnu.org/
  20. */
  21. /**
  22. * \file htdocs/core/lib/admin.lib.php
  23. * \brief Library of admin functions
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  26. /**
  27. * Renvoi une version en chaine depuis une version en tableau
  28. *
  29. * @param array $versionarray Tableau de version (vermajeur,vermineur,autre)
  30. * @return string Chaine version
  31. * @see versioncompare()
  32. */
  33. function versiontostring($versionarray)
  34. {
  35. $string = '?';
  36. if (isset($versionarray[0])) {
  37. $string = $versionarray[0];
  38. }
  39. if (isset($versionarray[1])) {
  40. $string .= '.'.$versionarray[1];
  41. }
  42. if (isset($versionarray[2])) {
  43. $string .= '.'.$versionarray[2];
  44. }
  45. return $string;
  46. }
  47. /**
  48. * Compare 2 versions (stored into 2 arrays).
  49. * To check if Dolibarr version is lower than (x,y,z), do "if versioncompare(versiondolibarrarray(), array(x.y.z)) <= 0"
  50. * For example: if (versioncompare(versiondolibarrarray(),array(4,0,-5)) >= 0) is true if version is 4.0 alpha or higher.
  51. * For example: if (versioncompare(versiondolibarrarray(),array(4,0,0)) >= 0) is true if version is 4.0 final or higher.
  52. * For example: if (versioncompare(versiondolibarrarray(),array(4,0,1)) >= 0) is true if version is 4.0.1 or higher.
  53. * Alternative way to compare: if ((float) DOL_VERSION >= 4.0) is true if version is 4.0 alpha or higher (works only to compare first and second level)
  54. *
  55. * @param array $versionarray1 Array of version (vermajor,verminor,patch)
  56. * @param array $versionarray2 Array of version (vermajor,verminor,patch)
  57. * @return int -4,-3,-2,-1 if versionarray1<versionarray2 (value depends on level of difference)
  58. * 0 if same
  59. * 1,2,3,4 if versionarray1>versionarray2 (value depends on level of difference)
  60. * @see versiontostring()
  61. */
  62. function versioncompare($versionarray1, $versionarray2)
  63. {
  64. $ret = 0;
  65. $level = 0;
  66. $count1 = count($versionarray1);
  67. $count2 = count($versionarray2);
  68. $maxcount = max($count1, $count2);
  69. while ($level < $maxcount) {
  70. $operande1 = isset($versionarray1[$level]) ? $versionarray1[$level] : 0;
  71. $operande2 = isset($versionarray2[$level]) ? $versionarray2[$level] : 0;
  72. if (preg_match('/alpha|dev/i', $operande1)) {
  73. $operande1 = -5;
  74. }
  75. if (preg_match('/alpha|dev/i', $operande2)) {
  76. $operande2 = -5;
  77. }
  78. if (preg_match('/beta$/i', $operande1)) {
  79. $operande1 = -4;
  80. }
  81. if (preg_match('/beta$/i', $operande2)) {
  82. $operande2 = -4;
  83. }
  84. if (preg_match('/beta([0-9])+/i', $operande1)) {
  85. $operande1 = -3;
  86. }
  87. if (preg_match('/beta([0-9])+/i', $operande2)) {
  88. $operande2 = -3;
  89. }
  90. if (preg_match('/rc$/i', $operande1)) {
  91. $operande1 = -2;
  92. }
  93. if (preg_match('/rc$/i', $operande2)) {
  94. $operande2 = -2;
  95. }
  96. if (preg_match('/rc([0-9])+/i', $operande1)) {
  97. $operande1 = -1;
  98. }
  99. if (preg_match('/rc([0-9])+/i', $operande2)) {
  100. $operande2 = -1;
  101. }
  102. $level++;
  103. //print 'level '.$level.' '.$operande1.'-'.$operande2.'<br>';
  104. if ($operande1 < $operande2) {
  105. $ret = -$level;
  106. break;
  107. }
  108. if ($operande1 > $operande2) {
  109. $ret = $level;
  110. break;
  111. }
  112. }
  113. //print join('.',$versionarray1).'('.count($versionarray1).') / '.join('.',$versionarray2).'('.count($versionarray2).') => '.$ret.'<br>'."\n";
  114. return $ret;
  115. }
  116. /**
  117. * Return version PHP
  118. *
  119. * @return array Tableau de version (vermajeur,vermineur,autre)
  120. * @see versioncompare()
  121. */
  122. function versionphparray()
  123. {
  124. return explode('.', PHP_VERSION);
  125. }
  126. /**
  127. * Return version Dolibarr
  128. *
  129. * @return array Tableau de version (vermajeur,vermineur,autre)
  130. * @see versioncompare()
  131. */
  132. function versiondolibarrarray()
  133. {
  134. return explode('.', DOL_VERSION);
  135. }
  136. /**
  137. * Launch a sql file. Function is used by:
  138. * - Migrate process (dolibarr-xyz-abc.sql)
  139. * - Loading sql menus (auguria)
  140. * - Running specific Sql by a module init
  141. * - Loading sql file of website import package
  142. * Install process however does not use it.
  143. * Note that Sql files must have all comments at start of line. Also this function take ';' as the char to detect end of sql request
  144. *
  145. * @param string $sqlfile Full path to sql file
  146. * @param int $silent 1=Do not output anything, 0=Output line for update page
  147. * @param int $entity Entity targeted for multicompany module
  148. * @param int $usesavepoint 1=Run a savepoint before each request and a rollback to savepoint if error (this allow to have some request with errors inside global transactions).
  149. * @param string $handler Handler targeted for menu (replace __HANDLER__ with this value)
  150. * @param string $okerror Family of errors we accept ('default', 'none')
  151. * @param int $linelengthlimit Limit for length of each line (Use 0 if unknown, may be faster if defined)
  152. * @param int $nocommentremoval Do no try to remove comments (in such a case, we consider that each line is a request, so use also $linelengthlimit=0)
  153. * @param int $offsetforchartofaccount Offset to use to load chart of account table to update sql on the fly to add offset to rowid and account_parent value
  154. * @return int <=0 if KO, >0 if OK
  155. */
  156. function run_sql($sqlfile, $silent = 1, $entity = '', $usesavepoint = 1, $handler = '', $okerror = 'default', $linelengthlimit = 32768, $nocommentremoval = 0, $offsetforchartofaccount = 0)
  157. {
  158. global $db, $conf, $langs, $user;
  159. dol_syslog("Admin.lib::run_sql run sql file ".$sqlfile." silent=".$silent." entity=".$entity." usesavepoint=".$usesavepoint." handler=".$handler." okerror=".$okerror, LOG_DEBUG);
  160. if (!is_numeric($linelengthlimit)) {
  161. dol_syslog("Admin.lib::run_sql param linelengthlimit is not a numeric", LOG_ERR);
  162. return -1;
  163. }
  164. $ok = 0;
  165. $error = 0;
  166. $i = 0;
  167. $buffer = '';
  168. $arraysql = array();
  169. // Get version of database
  170. $versionarray = $db->getVersionArray();
  171. $fp = fopen($sqlfile, "r");
  172. if ($fp) {
  173. while (!feof($fp)) {
  174. // Warning fgets with second parameter that is null or 0 hang.
  175. if ($linelengthlimit > 0) {
  176. $buf = fgets($fp, $linelengthlimit);
  177. } else {
  178. $buf = fgets($fp);
  179. }
  180. // Test if request must be ran only for particular database or version (if yes, we must remove the -- comment)
  181. $reg = array();
  182. if (preg_match('/^--\sV(MYSQL|PGSQL)([^\s]*)/i', $buf, $reg)) {
  183. $qualified = 1;
  184. // restrict on database type
  185. if (!empty($reg[1])) {
  186. if (!preg_match('/'.preg_quote($reg[1]).'/i', $db->type)) {
  187. $qualified = 0;
  188. }
  189. }
  190. // restrict on version
  191. if ($qualified) {
  192. if (!empty($reg[2])) {
  193. if (is_numeric($reg[2])) { // This is a version
  194. $versionrequest = explode('.', $reg[2]);
  195. //print var_dump($versionrequest);
  196. //print var_dump($versionarray);
  197. if (!count($versionrequest) || !count($versionarray) || versioncompare($versionrequest, $versionarray) > 0) {
  198. $qualified = 0;
  199. }
  200. } else // This is a test on a constant. For example when we have -- VMYSQLUTF8UNICODE, we test constant $conf->global->UTF8UNICODE
  201. {
  202. $dbcollation = strtoupper(preg_replace('/_/', '', $conf->db->dolibarr_main_db_collation));
  203. //var_dump($reg[2]);
  204. //var_dump($dbcollation);
  205. if (empty($conf->db->dolibarr_main_db_collation) || ($reg[2] != $dbcollation)) {
  206. $qualified = 0;
  207. }
  208. //var_dump($qualified);
  209. }
  210. }
  211. }
  212. if ($qualified) {
  213. // Version qualified, delete SQL comments
  214. $buf = preg_replace('/^--\sV(MYSQL|PGSQL)([^\s]*)/i', '', $buf);
  215. //print "Ligne $i qualifi?e par version: ".$buf.'<br>';
  216. }
  217. }
  218. // Add line buf to buffer if not a comment
  219. if ($nocommentremoval || !preg_match('/^\s*--/', $buf)) {
  220. if (empty($nocommentremoval)) {
  221. $buf = preg_replace('/([,;ERLT\)])\s*--.*$/i', '\1', $buf); //remove comment from a line that not start with -- before add it to the buffer
  222. }
  223. $buffer .= trim($buf);
  224. }
  225. //print $buf.'<br>';exit;
  226. if (preg_match('/;/', $buffer)) { // If string contains ';', it's end of a request string, we save it in arraysql.
  227. // Found new request
  228. if ($buffer) {
  229. $arraysql[$i] = $buffer;
  230. }
  231. $i++;
  232. $buffer = '';
  233. }
  234. }
  235. if ($buffer) {
  236. $arraysql[$i] = $buffer;
  237. }
  238. fclose($fp);
  239. } else {
  240. dol_syslog("Admin.lib::run_sql failed to open file ".$sqlfile, LOG_ERR);
  241. }
  242. // Loop on each request to see if there is a __+MAX_table__ key
  243. $listofmaxrowid = array(); // This is a cache table
  244. foreach ($arraysql as $i => $sql) {
  245. $newsql = $sql;
  246. // Replace __+MAX_table__ with max of table
  247. while (preg_match('/__\+MAX_([A-Za-z0-9_]+)__/i', $newsql, $reg)) {
  248. $table = $reg[1];
  249. if (!isset($listofmaxrowid[$table])) {
  250. //var_dump($db);
  251. $sqlgetrowid = 'SELECT MAX(rowid) as max from '.preg_replace('/^llx_/', MAIN_DB_PREFIX, $table);
  252. $resql = $db->query($sqlgetrowid);
  253. if ($resql) {
  254. $obj = $db->fetch_object($resql);
  255. $listofmaxrowid[$table] = $obj->max;
  256. if (empty($listofmaxrowid[$table])) {
  257. $listofmaxrowid[$table] = 0;
  258. }
  259. } else {
  260. if (!$silent) {
  261. print '<tr><td class="tdtop" colspan="2">';
  262. }
  263. if (!$silent) {
  264. print '<div class="error">'.$langs->trans("Failed to get max rowid for ".$table)."</div></td>";
  265. }
  266. if (!$silent) {
  267. print '</tr>';
  268. }
  269. $error++;
  270. break;
  271. }
  272. }
  273. // Replace __+MAX_llx_table__ with +999
  274. $from = '__+MAX_'.$table.'__';
  275. $to = '+'.$listofmaxrowid[$table];
  276. $newsql = str_replace($from, $to, $newsql);
  277. dol_syslog('Admin.lib::run_sql New Request '.($i + 1).' (replacing '.$from.' to '.$to.')', LOG_DEBUG);
  278. $arraysql[$i] = $newsql;
  279. }
  280. if ($offsetforchartofaccount > 0) {
  281. // Replace lines
  282. // '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,...'
  283. // with
  284. // '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,...'
  285. // Note: string with 'PCG99-ABREGE','CAPIT', 1234 instead of 'PCG99-ABREGE','CAPIT', '1234' is also supported
  286. $newsql = preg_replace('/VALUES\s*\(__ENTITY__, \s*(\d+)\s*,(\s*\'[^\',]*\'\s*,\s*\'[^\',]*\'\s*,\s*\'?[^\',]*\'?\s*),\s*\'?([^\',]*)\'?/ims', 'VALUES (__ENTITY__, \1 + '.$offsetforchartofaccount.', \2, \3 + '.$offsetforchartofaccount, $newsql);
  287. $newsql = preg_replace('/([,\s])0 \+ '.$offsetforchartofaccount.'/ims', '\1 0', $newsql);
  288. //var_dump($newsql);
  289. $arraysql[$i] = $newsql;
  290. // FIXME Because we force the rowid during insert, we must also update the sequence with postgresql by running
  291. // SELECT dol_util_rebuild_sequences();
  292. }
  293. }
  294. // Loop on each request to execute request
  295. $cursorinsert = 0;
  296. $listofinsertedrowid = array();
  297. foreach ($arraysql as $i => $sql) {
  298. if ($sql) {
  299. // Replace the prefix tables
  300. if (MAIN_DB_PREFIX != 'llx_') {
  301. $sql = preg_replace('/llx_/i', MAIN_DB_PREFIX, $sql);
  302. }
  303. if (!empty($handler)) {
  304. $sql = preg_replace('/__HANDLER__/i', "'".$db->escape($handler)."'", $sql);
  305. }
  306. $newsql = preg_replace('/__ENTITY__/i', (!empty($entity) ? $entity : $conf->entity), $sql);
  307. // Add log of request
  308. if (!$silent) {
  309. print '<tr class="trforrunsql"><td class="tdtop opacitymedium">'.$langs->trans("Request").' '.($i + 1)." sql='".dol_htmlentities($newsql, ENT_NOQUOTES)."'</td></tr>\n";
  310. }
  311. dol_syslog('Admin.lib::run_sql Request '.($i + 1), LOG_DEBUG);
  312. $sqlmodified = 0;
  313. // Replace for encrypt data
  314. if (preg_match_all('/__ENCRYPT\(\'([^\']+)\'\)__/i', $newsql, $reg)) {
  315. $num = count($reg[0]);
  316. for ($j = 0; $j < $num; $j++) {
  317. $from = $reg[0][$j];
  318. $to = $db->encrypt($reg[1][$j], 1);
  319. $newsql = str_replace($from, $to, $newsql);
  320. }
  321. $sqlmodified++;
  322. }
  323. // Replace for decrypt data
  324. if (preg_match_all('/__DECRYPT\(\'([A-Za-z0-9_]+)\'\)__/i', $newsql, $reg)) {
  325. $num = count($reg[0]);
  326. for ($j = 0; $j < $num; $j++) {
  327. $from = $reg[0][$j];
  328. $to = $db->decrypt($reg[1][$j]);
  329. $newsql = str_replace($from, $to, $newsql);
  330. }
  331. $sqlmodified++;
  332. }
  333. // Replace __x__ with rowid of insert nb x
  334. while (preg_match('/__([0-9]+)__/', $newsql, $reg)) {
  335. $cursor = $reg[1];
  336. if (empty($listofinsertedrowid[$cursor])) {
  337. if (!$silent) {
  338. print '<tr><td class="tdtop" colspan="2">';
  339. }
  340. if (!$silent) {
  341. print '<div class="error">'.$langs->trans("FileIsNotCorrect")."</div></td>";
  342. }
  343. if (!$silent) {
  344. print '</tr>';
  345. }
  346. $error++;
  347. break;
  348. }
  349. $from = '__'.$cursor.'__';
  350. $to = $listofinsertedrowid[$cursor];
  351. $newsql = str_replace($from, $to, $newsql);
  352. $sqlmodified++;
  353. }
  354. if ($sqlmodified) {
  355. dol_syslog('Admin.lib::run_sql New Request '.($i + 1), LOG_DEBUG);
  356. }
  357. $result = $db->query($newsql, $usesavepoint);
  358. if ($result) {
  359. if (!$silent) {
  360. print '<!-- Result = OK -->'."\n";
  361. }
  362. if (preg_replace('/insert into ([^\s]+)/i', $newsql, $reg)) {
  363. $cursorinsert++;
  364. // It's an insert
  365. $table = preg_replace('/([^a-zA-Z_]+)/i', '', $reg[1]);
  366. $insertedrowid = $db->last_insert_id($table);
  367. $listofinsertedrowid[$cursorinsert] = $insertedrowid;
  368. dol_syslog('Admin.lib::run_sql Insert nb '.$cursorinsert.', done in table '.$table.', rowid is '.$listofinsertedrowid[$cursorinsert], LOG_DEBUG);
  369. }
  370. // print '<td class="right">OK</td>';
  371. } else {
  372. $errno = $db->errno();
  373. if (!$silent) {
  374. print '<!-- Result = '.$errno.' -->'."\n";
  375. }
  376. // Define list of errors we accept (array $okerrors)
  377. $okerrors = array( // By default
  378. 'DB_ERROR_TABLE_ALREADY_EXISTS',
  379. 'DB_ERROR_COLUMN_ALREADY_EXISTS',
  380. 'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
  381. 'DB_ERROR_TABLE_OR_KEY_ALREADY_EXISTS', // PgSql use same code for table and key already exist
  382. 'DB_ERROR_RECORD_ALREADY_EXISTS',
  383. 'DB_ERROR_NOSUCHTABLE',
  384. 'DB_ERROR_NOSUCHFIELD',
  385. 'DB_ERROR_NO_FOREIGN_KEY_TO_DROP',
  386. 'DB_ERROR_NO_INDEX_TO_DROP',
  387. 'DB_ERROR_CANNOT_CREATE', // Qd contrainte deja existante
  388. 'DB_ERROR_CANT_DROP_PRIMARY_KEY',
  389. 'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS',
  390. 'DB_ERROR_22P02'
  391. );
  392. if ($okerror == 'none') {
  393. $okerrors = array();
  394. }
  395. // Is it an error we accept
  396. if (!in_array($errno, $okerrors)) {
  397. if (!$silent) {
  398. print '<tr><td class="tdtop" colspan="2">';
  399. }
  400. if (!$silent) {
  401. print '<div class="error">'.$langs->trans("Error")." ".$db->errno().": ".$newsql."<br>".$db->error()."</div></td>";
  402. }
  403. if (!$silent) {
  404. print '</tr>'."\n";
  405. }
  406. dol_syslog('Admin.lib::run_sql Request '.($i + 1)." Error ".$db->errno()." ".$newsql."<br>".$db->error(), LOG_ERR);
  407. $error++;
  408. }
  409. }
  410. if (!$silent) {
  411. print '</tr>'."\n";
  412. }
  413. }
  414. }
  415. if (!$silent) {
  416. print '<tr><td>'.$langs->trans("ProcessMigrateScript").'</td>';
  417. print '<td class="right">';
  418. if ($error == 0) {
  419. print '<span class="ok">'.$langs->trans("OK").'</span>';
  420. } else {
  421. print '<span class="error">'.$langs->trans("Error").'</span>';
  422. }
  423. //if (!empty($conf->use_javascript_ajax)) { // use_javascript_ajax is not defined
  424. print '<script type="text/javascript" language="javascript">
  425. jQuery(document).ready(function() {
  426. function init_trrunsql()
  427. {
  428. console.log("toggle .trforrunsql");
  429. jQuery(".trforrunsql").toggle();
  430. }
  431. init_trrunsql();
  432. jQuery(".trforrunsqlshowhide").click(function() {
  433. init_trrunsql();
  434. });
  435. });
  436. </script>';
  437. print ' - <a class="trforrunsqlshowhide" href="#">'.$langs->trans("ShowHideDetails").'</a>';
  438. //}
  439. print '</td></tr>'."\n";
  440. }
  441. if ($error == 0) {
  442. $ok = 1;
  443. } else {
  444. $ok = 0;
  445. }
  446. return $ok;
  447. }
  448. /**
  449. * Effacement d'une constante dans la base de donnees
  450. *
  451. * @param DoliDB $db Database handler
  452. * @param string $name Name of constant or rowid of line
  453. * @param int $entity Multi company id, -1 for all entities
  454. * @return int <0 if KO, >0 if OK
  455. *
  456. * @see dolibarr_get_const(), dolibarr_set_const(), dol_set_user_param()
  457. */
  458. function dolibarr_del_const($db, $name, $entity = 1)
  459. {
  460. global $conf;
  461. if (empty($name)) {
  462. dol_print_error('', 'Error call dolibar_del_const with parameter name empty');
  463. return -1;
  464. }
  465. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  466. $sql .= " WHERE (".$db->decrypt('name')." = '".$db->escape($name)."'";
  467. if (is_numeric($name)) {
  468. $sql .= " OR rowid = '".$db->escape($name)."'";
  469. }
  470. $sql .= ")";
  471. if ($entity >= 0) {
  472. $sql .= " AND entity = ".((int) $entity);
  473. }
  474. dol_syslog("admin.lib::dolibarr_del_const", LOG_DEBUG);
  475. $resql = $db->query($sql);
  476. if ($resql) {
  477. $conf->global->$name = '';
  478. return 1;
  479. } else {
  480. dol_print_error($db);
  481. return -1;
  482. }
  483. }
  484. /**
  485. * Get the value of a setup constant from database
  486. *
  487. * @param DoliDB $db Database handler
  488. * @param string $name Name of constant
  489. * @param int $entity Multi company id
  490. * @return string Value of constant
  491. *
  492. * @see dolibarr_del_const(), dolibarr_set_const(), dol_set_user_param()
  493. */
  494. function dolibarr_get_const($db, $name, $entity = 1)
  495. {
  496. $value = '';
  497. $sql = "SELECT ".$db->decrypt('value')." as value";
  498. $sql .= " FROM ".MAIN_DB_PREFIX."const";
  499. $sql .= " WHERE name = ".$db->encrypt($name, 1);
  500. $sql .= " AND entity = ".((int) $entity);
  501. dol_syslog("admin.lib::dolibarr_get_const", LOG_DEBUG);
  502. $resql = $db->query($sql);
  503. if ($resql) {
  504. $obj = $db->fetch_object($resql);
  505. if ($obj) {
  506. $value = $obj->value;
  507. }
  508. }
  509. return $value;
  510. }
  511. /**
  512. * Insert a parameter (key,value) into database (delete old key then insert it again).
  513. *
  514. * @param DoliDB $db Database handler
  515. * @param string $name Name of constant
  516. * @param string $value Value of constant
  517. * @param string $type Type of constant. Deprecated, only strings are allowed for $value. Caller must json encode/decode to store other type of data.
  518. * @param int $visible Is constant visible in Setup->Other page (0 by default)
  519. * @param string $note Note on parameter
  520. * @param int $entity Multi company id (0 means all entities)
  521. * @return int -1 if KO, 1 if OK
  522. *
  523. * @see dolibarr_del_const(), dolibarr_get_const(), dol_set_user_param()
  524. */
  525. function dolibarr_set_const($db, $name, $value, $type = 'chaine', $visible = 0, $note = '', $entity = 1)
  526. {
  527. global $conf;
  528. // Clean parameters
  529. $name = trim($name);
  530. // Check parameters
  531. if (empty($name)) {
  532. dol_print_error($db, "Error: Call to function dolibarr_set_const with wrong parameters", LOG_ERR);
  533. exit;
  534. }
  535. //dol_syslog("dolibarr_set_const name=$name, value=$value type=$type, visible=$visible, note=$note entity=$entity");
  536. $db->begin();
  537. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  538. $sql .= " WHERE name = ".$db->encrypt($name, 1);
  539. if ($entity >= 0) {
  540. $sql .= " AND entity = ".((int) $entity);
  541. }
  542. dol_syslog("admin.lib::dolibarr_set_const", LOG_DEBUG);
  543. $resql = $db->query($sql);
  544. if (strcmp($value, '')) { // true if different. Must work for $value='0' or $value=0
  545. $sql = "INSERT INTO ".MAIN_DB_PREFIX."const(name,value,type,visible,note,entity)";
  546. $sql .= " VALUES (";
  547. $sql .= $db->encrypt($name, 1);
  548. $sql .= ", ".$db->encrypt($value, 1);
  549. $sql .= ",'".$db->escape($type)."',".((int) $visible).",'".$db->escape($note)."',".((int) $entity).")";
  550. //print "sql".$value."-".pg_escape_string($value)."-".$sql;exit;
  551. //print "xx".$db->escape($value);
  552. dol_syslog("admin.lib::dolibarr_set_const", LOG_DEBUG);
  553. $resql = $db->query($sql);
  554. }
  555. if ($resql) {
  556. $db->commit();
  557. $conf->global->$name = $value;
  558. return 1;
  559. } else {
  560. $error = $db->lasterror();
  561. $db->rollback();
  562. return -1;
  563. }
  564. }
  565. /**
  566. * Prepare array with list of tabs
  567. *
  568. * @param int $nbofactivatedmodules Number f oactivated modules
  569. * @param int $nboftotalmodules Nb of total modules
  570. * @return array Array of tabs to show
  571. */
  572. function modules_prepare_head($nbofactivatedmodules, $nboftotalmodules)
  573. {
  574. global $langs, $conf, $user, $form;
  575. $desc = $langs->trans("ModulesDesc", '{picto}');
  576. $desc = str_replace('{picto}', img_picto('', 'switch_off'), $desc);
  577. $h = 0;
  578. $head = array();
  579. $mode = empty($conf->global->MAIN_MODULE_SETUP_ON_LIST_BY_DEFAULT) ? 'commonkanban' : 'common';
  580. $head[$h][0] = DOL_URL_ROOT."/admin/modules.php?mode=".$mode;
  581. if ($nbofactivatedmodules <= (empty($conf->global->MAIN_MIN_NB_ENABLED_MODULE_FOR_WARNING) ? 1 : $conf->global->MAIN_MIN_NB_ENABLED_MODULE_FOR_WARNING)) { // If only minimal initial modules enabled)
  582. $head[$h][1] = $form->textwithpicto($langs->trans("AvailableModules"), $desc);
  583. $head[$h][1] .= img_warning($langs->trans("YouMustEnableOneModule"));
  584. } else {
  585. //$head[$h][1] = $langs->trans("AvailableModules").$form->textwithpicto('<span class="badge marginleftonly">'.$nbofactivatedmodules.' / '.$nboftotalmodules.'</span>', $desc, 1, 'help', '', 1, 3);
  586. $head[$h][1] = $langs->trans("AvailableModules").'<span class="badge marginleftonly">'.$nbofactivatedmodules.' / '.$nboftotalmodules.'</span>';
  587. }
  588. $head[$h][2] = 'modules';
  589. $h++;
  590. $head[$h][0] = DOL_URL_ROOT."/admin/modules.php?mode=marketplace";
  591. $head[$h][1] = $langs->trans("ModulesMarketPlaces");
  592. $head[$h][2] = 'marketplace';
  593. $h++;
  594. $head[$h][0] = DOL_URL_ROOT."/admin/modules.php?mode=deploy";
  595. $head[$h][1] = $langs->trans("AddExtensionThemeModuleOrOther");
  596. $head[$h][2] = 'deploy';
  597. $h++;
  598. $head[$h][0] = DOL_URL_ROOT."/admin/modules.php?mode=develop";
  599. $head[$h][1] = $langs->trans("ModulesDevelopYourModule");
  600. $head[$h][2] = 'develop';
  601. $h++;
  602. return $head;
  603. }
  604. /**
  605. * Prepare array with list of tabs
  606. *
  607. * @return array Array of tabs to show
  608. */
  609. function security_prepare_head()
  610. {
  611. global $db, $langs, $conf, $user;
  612. $h = 0;
  613. $head = array();
  614. $head[$h][0] = DOL_URL_ROOT."/admin/security_other.php";
  615. $head[$h][1] = $langs->trans("Miscellaneous");
  616. $head[$h][2] = 'misc';
  617. $h++;
  618. $head[$h][0] = DOL_URL_ROOT."/admin/security.php";
  619. $head[$h][1] = $langs->trans("Passwords");
  620. $head[$h][2] = 'passwords';
  621. $h++;
  622. $head[$h][0] = DOL_URL_ROOT."/admin/security_file.php";
  623. $head[$h][1] = $langs->trans("Files").' ('.$langs->trans("Upload").')';
  624. $head[$h][2] = 'file';
  625. $h++;
  626. /*
  627. $head[$h][0] = DOL_URL_ROOT."/admin/security_file_download.php";
  628. $head[$h][1] = $langs->trans("Files").' ('.$langs->trans("Download").')';
  629. $head[$h][2] = 'filedownload';
  630. $h++;
  631. */
  632. $head[$h][0] = DOL_URL_ROOT."/admin/proxy.php";
  633. $head[$h][1] = $langs->trans("ExternalAccess");
  634. $head[$h][2] = 'proxy';
  635. $h++;
  636. $head[$h][0] = DOL_URL_ROOT."/admin/events.php";
  637. $head[$h][1] = $langs->trans("Audit");
  638. $head[$h][2] = 'audit';
  639. $h++;
  640. // Show permissions lines
  641. $nbPerms = 0;
  642. $sql = "SELECT COUNT(r.id) as nb";
  643. $sql .= " FROM ".MAIN_DB_PREFIX."rights_def as r";
  644. $sql .= " WHERE r.libelle NOT LIKE 'tou%'"; // On ignore droits "tous"
  645. $sql .= " AND entity = ".$conf->entity;
  646. $sql .= " AND bydefault = 1";
  647. if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  648. $sql .= " AND r.perms NOT LIKE '%_advance'"; // Hide advanced perms if option is not enabled
  649. }
  650. $resql = $db->query($sql);
  651. if ($resql) {
  652. $obj = $db->fetch_object($resql);
  653. if ($obj) {
  654. $nbPerms = $obj->nb;
  655. }
  656. } else {
  657. dol_print_error($db);
  658. }
  659. $head[$h][0] = DOL_URL_ROOT."/admin/perms.php";
  660. $head[$h][1] = $langs->trans("DefaultRights");
  661. if ($nbPerms > 0) {
  662. $head[$h][1] .= (empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '<span class="badge marginleftonlyshort">'.$nbPerms.'</span>' : '');
  663. }
  664. $head[$h][2] = 'default';
  665. $h++;
  666. return $head;
  667. }
  668. /**
  669. * Prepare array with list of tabs
  670. * @param object $object descriptor class
  671. * @return array Array of tabs to show
  672. */
  673. function modulehelp_prepare_head($object)
  674. {
  675. global $langs, $conf, $user;
  676. $h = 0;
  677. $head = array();
  678. // FIX for compatibity habitual tabs
  679. $object->id = $object->numero;
  680. $head[$h][0] = DOL_URL_ROOT."/admin/modulehelp.php?id=".$object->id.'&mode=desc';
  681. $head[$h][1] = $langs->trans("Description");
  682. $head[$h][2] = 'desc';
  683. $h++;
  684. $head[$h][0] = DOL_URL_ROOT."/admin/modulehelp.php?id=".$object->id.'&mode=feature';
  685. $head[$h][1] = $langs->trans("TechnicalServicesProvided");
  686. $head[$h][2] = 'feature';
  687. $h++;
  688. if ($object->isCoreOrExternalModule() == 'external') {
  689. $head[$h][0] = DOL_URL_ROOT."/admin/modulehelp.php?id=".$object->id.'&mode=changelog';
  690. $head[$h][1] = $langs->trans("ChangeLog");
  691. $head[$h][2] = 'changelog';
  692. $h++;
  693. }
  694. complete_head_from_modules($conf, $langs, $object, $head, $h, 'modulehelp_admin');
  695. complete_head_from_modules($conf, $langs, $object, $head, $h, 'modulehelp_admin', 'remove');
  696. return $head;
  697. }
  698. /**
  699. * Prepare array with list of tabs
  700. *
  701. * @return array Array of tabs to show
  702. */
  703. function translation_prepare_head()
  704. {
  705. global $langs, $conf, $user;
  706. $h = 0;
  707. $head = array();
  708. $head[$h][0] = DOL_URL_ROOT."/admin/translation.php?mode=searchkey";
  709. $head[$h][1] = $langs->trans("TranslationKeySearch");
  710. $head[$h][2] = 'searchkey';
  711. $h++;
  712. $head[$h][0] = DOL_URL_ROOT."/admin/translation.php?mode=overwrite";
  713. $head[$h][1] = $langs->trans("TranslationOverwriteKey").'<span class="fa fa-plus-circle valignmiddle paddingleft"></span>';
  714. $head[$h][2] = 'overwrite';
  715. $h++;
  716. complete_head_from_modules($conf, $langs, null, $head, $h, 'translation_admin');
  717. complete_head_from_modules($conf, $langs, null, $head, $h, 'translation_admin', 'remove');
  718. return $head;
  719. }
  720. /**
  721. * Prepare array with list of tabs
  722. *
  723. * @return array Array of tabs to show
  724. */
  725. function defaultvalues_prepare_head()
  726. {
  727. global $langs, $conf, $user;
  728. $h = 0;
  729. $head = array();
  730. $head[$h][0] = DOL_URL_ROOT."/admin/defaultvalues.php?mode=createform";
  731. $head[$h][1] = $langs->trans("DefaultCreateForm");
  732. $head[$h][2] = 'createform';
  733. $h++;
  734. $head[$h][0] = DOL_URL_ROOT."/admin/defaultvalues.php?mode=filters";
  735. $head[$h][1] = $langs->trans("DefaultSearchFilters");
  736. $head[$h][2] = 'filters';
  737. $h++;
  738. $head[$h][0] = DOL_URL_ROOT."/admin/defaultvalues.php?mode=sortorder";
  739. $head[$h][1] = $langs->trans("DefaultSortOrder");
  740. $head[$h][2] = 'sortorder';
  741. $h++;
  742. if (!empty($conf->use_javascript_ajax)) {
  743. $head[$h][0] = DOL_URL_ROOT."/admin/defaultvalues.php?mode=focus";
  744. $head[$h][1] = $langs->trans("DefaultFocus");
  745. $head[$h][2] = 'focus';
  746. $h++;
  747. $head[$h][0] = DOL_URL_ROOT."/admin/defaultvalues.php?mode=mandatory";
  748. $head[$h][1] = $langs->trans("DefaultMandatory");
  749. $head[$h][2] = 'mandatory';
  750. $h++;
  751. }
  752. /*$head[$h][0] = DOL_URL_ROOT."/admin/translation.php?mode=searchkey";
  753. $head[$h][1] = $langs->trans("TranslationKeySearch");
  754. $head[$h][2] = 'searchkey';
  755. $h++;*/
  756. complete_head_from_modules($conf, $langs, null, $head, $h, 'defaultvalues_admin');
  757. complete_head_from_modules($conf, $langs, null, $head, $h, 'defaultvalues_admin', 'remove');
  758. return $head;
  759. }
  760. /**
  761. * Return list of session
  762. *
  763. * @return array Array list of sessions
  764. */
  765. function listOfSessions()
  766. {
  767. global $conf;
  768. $arrayofSessions = array();
  769. // session.save_path can be returned empty so we set a default location and work from there
  770. $sessPath = '/tmp';
  771. $iniPath = ini_get("session.save_path");
  772. if ($iniPath) {
  773. $sessPath = $iniPath;
  774. }
  775. $sessPath .= '/'; // We need the trailing slash
  776. dol_syslog('admin.lib:listOfSessions sessPath='.$sessPath);
  777. $dh = @opendir(dol_osencode($sessPath));
  778. if ($dh) {
  779. while (($file = @readdir($dh)) !== false) {
  780. if (preg_match('/^sess_/i', $file) && $file != "." && $file != "..") {
  781. $fullpath = $sessPath.$file;
  782. if (!@is_dir($fullpath) && is_readable($fullpath)) {
  783. $sessValues = file_get_contents($fullpath); // get raw session data
  784. // Example of possible value
  785. //$sessValues = 'newtoken|s:32:"1239f7a0c4b899200fe9ca5ea394f307";dol_loginmesg|s:0:"";newtoken|s:32:"1236457104f7ae0f328c2928973f3cb5";dol_loginmesg|s:0:"";token|s:32:"123615ad8d650c5cc4199b9a1a76783f";
  786. // 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";
  787. // 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";';
  788. if (preg_match('/dol_login/i', $sessValues) && // limit to dolibarr session
  789. (preg_match('/dol_entity\|i:'.$conf->entity.';/i', $sessValues) || preg_match('/dol_entity\|s:([0-9]+):"'.$conf->entity.'"/i', $sessValues)) && // limit to current entity
  790. preg_match('/dol_company\|s:([0-9]+):"('.getDolGlobalString('MAIN_INFO_SOCIETE_NOM').')"/i', $sessValues)) { // limit to company name
  791. $tmp = explode('_', $file);
  792. $idsess = $tmp[1];
  793. $regs = array();
  794. $loginfound = preg_match('/dol_login\|s:[0-9]+:"([A-Za-z0-9]+)"/i', $sessValues, $regs);
  795. if ($loginfound) {
  796. $arrayofSessions[$idsess]["login"] = $regs[1];
  797. }
  798. $arrayofSessions[$idsess]["age"] = time() - filectime($fullpath);
  799. $arrayofSessions[$idsess]["creation"] = filectime($fullpath);
  800. $arrayofSessions[$idsess]["modification"] = filemtime($fullpath);
  801. $arrayofSessions[$idsess]["raw"] = $sessValues;
  802. }
  803. }
  804. }
  805. }
  806. @closedir($dh);
  807. }
  808. return $arrayofSessions;
  809. }
  810. /**
  811. * Purge existing sessions
  812. *
  813. * @param int $mysessionid To avoid to try to delete my own session
  814. * @return int >0 if OK, <0 if KO
  815. */
  816. function purgeSessions($mysessionid)
  817. {
  818. global $conf;
  819. $sessPath = ini_get("session.save_path")."/";
  820. dol_syslog('admin.lib:purgeSessions mysessionid='.$mysessionid.' sessPath='.$sessPath);
  821. $error = 0;
  822. $dh = @opendir(dol_osencode($sessPath));
  823. if ($dh) {
  824. while (($file = @readdir($dh)) !== false) {
  825. if ($file != "." && $file != "..") {
  826. $fullpath = $sessPath.$file;
  827. if (!@is_dir($fullpath)) {
  828. $sessValues = file_get_contents($fullpath); // get raw session data
  829. if (preg_match('/dol_login/i', $sessValues) && // limit to dolibarr session
  830. preg_match('/dol_entity\|s:([0-9]+):"('.$conf->entity.')"/i', $sessValues) && // limit to current entity
  831. preg_match('/dol_company\|s:([0-9]+):"('.$conf->global->MAIN_INFO_SOCIETE_NOM.')"/i', $sessValues)) { // limit to company name
  832. $tmp = explode('_', $file);
  833. $idsess = $tmp[1];
  834. // We remove session if it's not ourself
  835. if ($idsess != $mysessionid) {
  836. $res = @unlink($fullpath);
  837. if (!$res) {
  838. $error++;
  839. }
  840. }
  841. }
  842. }
  843. }
  844. }
  845. @closedir($dh);
  846. }
  847. if (!$error) {
  848. return 1;
  849. } else {
  850. return -$error;
  851. }
  852. }
  853. /**
  854. * Enable a module
  855. *
  856. * @param string $value Name of module to activate
  857. * @param int $withdeps Activate/Disable also all dependencies
  858. * @return array array('nbmodules'=>nb modules activated with success, 'errors=>array of error messages, 'nbperms'=>Nb permission added);
  859. */
  860. function activateModule($value, $withdeps = 1)
  861. {
  862. global $db, $langs, $conf, $mysoc;
  863. $ret = array();
  864. // Check parameters
  865. if (empty($value)) {
  866. $ret['errors'][] = 'ErrorBadParameter';
  867. return $ret;
  868. }
  869. $ret = array('nbmodules'=>0, 'errors'=>array(), 'nbperms'=>0);
  870. $modName = $value;
  871. $modFile = $modName.".class.php";
  872. // Loop on each directory to fill $modulesdir
  873. $modulesdir = dolGetModulesDirs();
  874. // Loop on each modulesdir directories
  875. $found = false;
  876. foreach ($modulesdir as $dir) {
  877. if (file_exists($dir.$modFile)) {
  878. $found = @include_once $dir.$modFile;
  879. if ($found) {
  880. break;
  881. }
  882. }
  883. }
  884. $objMod = new $modName($db);
  885. // Test if PHP version ok
  886. $verphp = versionphparray();
  887. $vermin = isset($objMod->phpmin) ? $objMod->phpmin : 0;
  888. if (is_array($vermin) && versioncompare($verphp, $vermin) < 0) {
  889. $ret['errors'][] = $langs->trans("ErrorModuleRequirePHPVersion", versiontostring($vermin));
  890. return $ret;
  891. }
  892. // Test if Dolibarr version ok
  893. $verdol = versiondolibarrarray();
  894. $vermin = isset($objMod->need_dolibarr_version) ? $objMod->need_dolibarr_version : 0;
  895. //print 'version: '.versioncompare($verdol,$vermin).' - '.join(',',$verdol).' - '.join(',',$vermin);exit;
  896. if (is_array($vermin) && versioncompare($verdol, $vermin) < 0) {
  897. $ret['errors'][] = $langs->trans("ErrorModuleRequireDolibarrVersion", versiontostring($vermin));
  898. return $ret;
  899. }
  900. // Test if javascript requirement ok
  901. if (!empty($objMod->need_javascript_ajax) && empty($conf->use_javascript_ajax)) {
  902. $ret['errors'][] = $langs->trans("ErrorModuleRequireJavascript");
  903. return $ret;
  904. }
  905. $const_name = $objMod->const_name;
  906. if (!empty($conf->global->$const_name)) {
  907. return $ret;
  908. }
  909. $result = $objMod->init(); // Enable module
  910. if ($result <= 0) {
  911. $ret['errors'][] = $objMod->error;
  912. } else {
  913. if ($withdeps) {
  914. if (isset($objMod->depends) && is_array($objMod->depends) && !empty($objMod->depends)) {
  915. // Activation of modules this module depends on
  916. // this->depends may be array('modModule1', 'mmodModule2') or array('always1'=>"modModule1", 'FR'=>'modModule2')
  917. foreach ($objMod->depends as $key => $modulestring) {
  918. //var_dump((! is_numeric($key)) && ! preg_match('/^always/', $key) && $mysoc->country_code && ! preg_match('/^'.$mysoc->country_code.'/', $key));exit;
  919. if ((!is_numeric($key)) && !preg_match('/^always/', $key) && $mysoc->country_code && !preg_match('/^'.$mysoc->country_code.'/', $key)) {
  920. dol_syslog("We are not concerned by dependency with key=".$key." because our country is ".$mysoc->country_code);
  921. continue;
  922. }
  923. $activate = false;
  924. foreach ($modulesdir as $dir) {
  925. if (file_exists($dir.$modulestring.".class.php")) {
  926. $resarray = activateModule($modulestring);
  927. if (empty($resarray['errors'])) {
  928. $activate = true;
  929. } else {
  930. foreach ($resarray['errors'] as $errorMessage) {
  931. dol_syslog($errorMessage, LOG_ERR);
  932. }
  933. }
  934. break;
  935. }
  936. }
  937. if ($activate) {
  938. $ret['nbmodules'] += $resarray['nbmodules'];
  939. $ret['nbperms'] += $resarray['nbperms'];
  940. } else {
  941. $ret['errors'][] = $langs->trans('activateModuleDependNotSatisfied', $objMod->name, $modulestring);
  942. }
  943. }
  944. }
  945. if (isset($objMod->conflictwith) && is_array($objMod->conflictwith) && !empty($objMod->conflictwith)) {
  946. // Desactivation des modules qui entrent en conflit
  947. $num = count($objMod->conflictwith);
  948. for ($i = 0; $i < $num; $i++) {
  949. foreach ($modulesdir as $dir) {
  950. if (file_exists($dir.$objMod->conflictwith[$i].".class.php")) {
  951. unActivateModule($objMod->conflictwith[$i], 0);
  952. }
  953. }
  954. }
  955. }
  956. }
  957. }
  958. if (!count($ret['errors'])) {
  959. $ret['nbmodules']++;
  960. $ret['nbperms'] += count($objMod->rights);
  961. }
  962. return $ret;
  963. }
  964. /**
  965. * Disable a module
  966. *
  967. * @param string $value Nom du module a desactiver
  968. * @param int $requiredby 1=Desactive aussi modules dependants
  969. * @return string Error message or '';
  970. */
  971. function unActivateModule($value, $requiredby = 1)
  972. {
  973. global $db, $modules, $conf;
  974. // Check parameters
  975. if (empty($value)) {
  976. return 'ErrorBadParameter';
  977. }
  978. $ret = '';
  979. $modName = $value;
  980. $modFile = $modName.".class.php";
  981. // Loop on each directory to fill $modulesdir
  982. $modulesdir = dolGetModulesDirs();
  983. // Loop on each modulesdir directories
  984. $found = false;
  985. foreach ($modulesdir as $dir) {
  986. if (file_exists($dir.$modFile)) {
  987. $found = @include_once $dir.$modFile;
  988. if ($found) {
  989. break;
  990. }
  991. }
  992. }
  993. if ($found) {
  994. $objMod = new $modName($db);
  995. $result = $objMod->remove();
  996. if ($result <= 0) {
  997. $ret = $objMod->error;
  998. }
  999. } else // We come here when we try to unactivate a module when module does not exists anymore in sources
  1000. {
  1001. //print $dir.$modFile;exit;
  1002. // 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
  1003. include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
  1004. $genericMod = new DolibarrModules($db);
  1005. $genericMod->name = preg_replace('/^mod/i', '', $modName);
  1006. $genericMod->rights_class = strtolower(preg_replace('/^mod/i', '', $modName));
  1007. $genericMod->const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', $modName));
  1008. dol_syslog("modules::unActivateModule Failed to find module file, we use generic function with name ".$modName);
  1009. $genericMod->remove('');
  1010. }
  1011. // Disable modules that depends on module we disable
  1012. if (!$ret && $requiredby && is_object($objMod) && is_array($objMod->requiredby)) {
  1013. $countrb = count($objMod->requiredby);
  1014. for ($i = 0; $i < $countrb; $i++) {
  1015. //var_dump($objMod->requiredby[$i]);
  1016. unActivateModule($objMod->requiredby[$i]);
  1017. }
  1018. }
  1019. return $ret;
  1020. }
  1021. /**
  1022. * Add external modules to list of dictionaries.
  1023. * Addition is done into var $taborder, $tabname, etc... that are passed with pointers.
  1024. *
  1025. * @param array $taborder Taborder
  1026. * @param array $tabname Tabname
  1027. * @param array $tablib Tablib
  1028. * @param array $tabsql Tabsql
  1029. * @param array $tabsqlsort Tabsqlsort
  1030. * @param array $tabfield Tabfield
  1031. * @param array $tabfieldvalue Tabfieldvalue
  1032. * @param array $tabfieldinsert Tabfieldinsert
  1033. * @param array $tabrowid Tabrowid
  1034. * @param array $tabcond Tabcond
  1035. * @param array $tabhelp Tabhelp
  1036. * @param array $tabcomplete Tab complete (will replace all other in future). Key is table name.
  1037. * @return int 1
  1038. */
  1039. function complete_dictionary_with_modules(&$taborder, &$tabname, &$tablib, &$tabsql, &$tabsqlsort, &$tabfield, &$tabfieldvalue, &$tabfieldinsert, &$tabrowid, &$tabcond, &$tabhelp, &$tabcomplete)
  1040. {
  1041. global $db, $modules, $conf, $langs;
  1042. dol_syslog("complete_dictionary_with_modules Search external modules to complete the list of dictionnary tables", LOG_DEBUG, 1);
  1043. // Search modules
  1044. $modulesdir = dolGetModulesDirs();
  1045. $i = 0; // is a sequencer of modules found
  1046. $j = 0; // j is module number. Automatically affected if module number not defined.
  1047. foreach ($modulesdir as $dir) {
  1048. // Load modules attributes in arrays (name, numero, orders) from dir directory
  1049. //print $dir."\n<br>";
  1050. dol_syslog("Scan directory ".$dir." for modules");
  1051. $handle = @opendir(dol_osencode($dir));
  1052. if (is_resource($handle)) {
  1053. while (($file = readdir($handle)) !== false) {
  1054. //print "$i ".$file."\n<br>";
  1055. if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
  1056. $modName = substr($file, 0, dol_strlen($file) - 10);
  1057. if ($modName) {
  1058. include_once $dir.$file;
  1059. $objMod = new $modName($db);
  1060. if ($objMod->numero > 0) {
  1061. $j = $objMod->numero;
  1062. } else {
  1063. $j = 1000 + $i;
  1064. }
  1065. $modulequalified = 1;
  1066. // We discard modules according to features level (PS: if module is activated we always show it)
  1067. $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($objMod)));
  1068. if ($objMod->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2 && empty(getDolGlobalString($const_name))) {
  1069. $modulequalified = 0;
  1070. }
  1071. if ($objMod->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1 && empty(getDolGlobalString($const_name))) {
  1072. $modulequalified = 0;
  1073. }
  1074. //If module is not activated disqualified
  1075. if (empty(getDolGlobalString($const_name))) {
  1076. $modulequalified = 0;
  1077. }
  1078. if ($modulequalified) {
  1079. // Load languages files of module
  1080. if (isset($objMod->langfiles) && is_array($objMod->langfiles)) {
  1081. foreach ($objMod->langfiles as $langfile) {
  1082. $langs->load($langfile);
  1083. }
  1084. }
  1085. // Complete the arrays &$tabname,&$tablib,&$tabsql,&$tabsqlsort,&$tabfield,&$tabfieldvalue,&$tabfieldinsert,&$tabrowid,&$tabcond
  1086. if (empty($objMod->dictionaries) && !empty($objMod->dictionnaries)) {
  1087. $objMod->dictionaries = $objMod->dictionnaries; // For backward compatibility
  1088. }
  1089. if (!empty($objMod->dictionaries)) {
  1090. //var_dump($objMod->dictionaries['tabname']);
  1091. $nbtabname = $nbtablib = $nbtabsql = $nbtabsqlsort = $nbtabfield = $nbtabfieldvalue = $nbtabfieldinsert = $nbtabrowid = $nbtabcond = $nbtabfieldcheck = $nbtabhelp = 0;
  1092. $tabnamerelwithkey = array();
  1093. foreach ($objMod->dictionaries['tabname'] as $key => $val) {
  1094. $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $val);
  1095. $nbtabname++;
  1096. $taborder[] = max($taborder) + 1;
  1097. $tabname[] = $val;
  1098. $tabnamerelwithkey[$key] = $val;
  1099. $tabcomplete[$tmptablename]['picto'] = $objMod->picto;
  1100. } // Position
  1101. foreach ($objMod->dictionaries['tablib'] as $key => $val) {
  1102. $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
  1103. $nbtablib++;
  1104. $tablib[] = $val;
  1105. $tabcomplete[$tmptablename]['lib'] = $val;
  1106. }
  1107. foreach ($objMod->dictionaries['tabsql'] as $key => $val) {
  1108. $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
  1109. $nbtabsql++;
  1110. $tabsql[] = $val;
  1111. $tabcomplete[$tmptablename]['sql'] = $val;
  1112. }
  1113. foreach ($objMod->dictionaries['tabsqlsort'] as $key => $val) {
  1114. $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
  1115. $nbtabsqlsort++;
  1116. $tabsqlsort[] = $val;
  1117. $tabcomplete[$tmptablename]['sqlsort'] = $val;
  1118. }
  1119. foreach ($objMod->dictionaries['tabfield'] as $key => $val) {
  1120. $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
  1121. $nbtabfield++;
  1122. $tabfield[] = $val;
  1123. $tabcomplete[$tmptablename]['field'] = $val;
  1124. }
  1125. foreach ($objMod->dictionaries['tabfieldvalue'] as $key => $val) {
  1126. $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
  1127. $nbtabfieldvalue++;
  1128. $tabfieldvalue[] = $val;
  1129. $tabcomplete[$tmptablename]['value'] = $val;
  1130. }
  1131. foreach ($objMod->dictionaries['tabfieldinsert'] as $key => $val) {
  1132. $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
  1133. $nbtabfieldinsert++;
  1134. $tabfieldinsert[] = $val;
  1135. $tabcomplete[$tmptablename]['fieldinsert'] = $val;
  1136. }
  1137. foreach ($objMod->dictionaries['tabrowid'] as $key => $val) {
  1138. $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
  1139. $nbtabrowid++;
  1140. $tabrowid[] = $val;
  1141. $tabcomplete[$tmptablename]['rowid'] = $val;
  1142. }
  1143. foreach ($objMod->dictionaries['tabcond'] as $key => $val) {
  1144. $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
  1145. $nbtabcond++;
  1146. $tabcond[] = $val;
  1147. $tabcomplete[$tmptablename]['rowid'] = $val;
  1148. }
  1149. if (!empty($objMod->dictionaries['tabhelp'])) {
  1150. foreach ($objMod->dictionaries['tabhelp'] as $key => $val) {
  1151. $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
  1152. $nbtabhelp++;
  1153. $tabhelp[] = $val;
  1154. $tabcomplete[$tmptablename]['help'] = $val;
  1155. }
  1156. }
  1157. if (!empty($objMod->dictionaries['tabfieldcheck'])) {
  1158. foreach ($objMod->dictionaries['tabfieldcheck'] as $key => $val) {
  1159. $tmptablename = preg_replace('/'.MAIN_DB_PREFIX.'/', '', $tabnamerelwithkey[$key]);
  1160. $nbtabfieldcheck++;
  1161. $tabcomplete[$tmptablename]['fieldcheck'] = $val;
  1162. }
  1163. }
  1164. if ($nbtabname != $nbtablib || $nbtablib != $nbtabsql || $nbtabsql != $nbtabsqlsort) {
  1165. print 'Error in descriptor of module '.$const_name.'. Array ->dictionaries has not same number of record for key "tabname", "tablib", "tabsql" and "tabsqlsort"';
  1166. //print "$const_name: $nbtabname=$nbtablib=$nbtabsql=$nbtabsqlsort=$nbtabfield=$nbtabfieldvalue=$nbtabfieldinsert=$nbtabrowid=$nbtabcond=$nbtabfieldcheck=$nbtabhelp\n";
  1167. } else {
  1168. $taborder[] = 0; // Add an empty line
  1169. }
  1170. }
  1171. $j++;
  1172. $i++;
  1173. } else {
  1174. dol_syslog("Module ".get_class($objMod)." not qualified");
  1175. }
  1176. }
  1177. }
  1178. }
  1179. closedir($handle);
  1180. } else {
  1181. dol_syslog("htdocs/admin/modules.php: Failed to open directory ".$dir.". See permission and open_basedir option.", LOG_WARNING);
  1182. }
  1183. }
  1184. dol_syslog("", LOG_DEBUG, -1);
  1185. return 1;
  1186. }
  1187. /**
  1188. * Activate external modules mandatory when country is country_code
  1189. *
  1190. * @param string $country_code CountryCode
  1191. * @return int 1
  1192. */
  1193. function activateModulesRequiredByCountry($country_code)
  1194. {
  1195. global $db, $conf, $langs;
  1196. $modulesdir = dolGetModulesDirs();
  1197. foreach ($modulesdir as $dir) {
  1198. // Load modules attributes in arrays (name, numero, orders) from dir directory
  1199. dol_syslog("Scan directory ".$dir." for modules");
  1200. $handle = @opendir(dol_osencode($dir));
  1201. if (is_resource($handle)) {
  1202. while (($file = readdir($handle)) !== false) {
  1203. if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
  1204. $modName = substr($file, 0, dol_strlen($file) - 10);
  1205. if ($modName) {
  1206. include_once $dir.$file;
  1207. $objMod = new $modName($db);
  1208. $modulequalified = 1;
  1209. // We discard modules according to features level (PS: if module is activated we always show it)
  1210. $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($objMod)));
  1211. if ($objMod->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  1212. $modulequalified = 0;
  1213. }
  1214. if ($objMod->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  1215. $modulequalified = 0;
  1216. }
  1217. if (!empty($conf->global->$const_name)) {
  1218. $modulequalified = 0; // already activated
  1219. }
  1220. if ($modulequalified) {
  1221. // Load languages files of module
  1222. if (isset($objMod->automatic_activation) && is_array($objMod->automatic_activation) && isset($objMod->automatic_activation[$country_code])) {
  1223. activateModule($modName);
  1224. setEventMessages($objMod->automatic_activation[$country_code], null, 'warnings');
  1225. }
  1226. } else {
  1227. dol_syslog("Module ".get_class($objMod)." not qualified");
  1228. }
  1229. }
  1230. }
  1231. }
  1232. closedir($handle);
  1233. } else {
  1234. dol_syslog("htdocs/admin/modules.php: Failed to open directory ".$dir.". See permission and open_basedir option.", LOG_WARNING);
  1235. }
  1236. }
  1237. return 1;
  1238. }
  1239. /**
  1240. * Search external modules to complete the list of contact element
  1241. *
  1242. * @param array $elementList elementList
  1243. * @return int 1
  1244. */
  1245. function complete_elementList_with_modules(&$elementList)
  1246. {
  1247. global $db, $modules, $conf, $langs;
  1248. // Search modules
  1249. $filename = array();
  1250. $modules = array();
  1251. $orders = array();
  1252. $categ = array();
  1253. $dirmod = array();
  1254. $i = 0; // is a sequencer of modules found
  1255. $j = 0; // j is module number. Automatically affected if module number not defined.
  1256. dol_syslog("complete_elementList_with_modules Search external modules to complete the list of contact element", LOG_DEBUG, 1);
  1257. $modulesdir = dolGetModulesDirs();
  1258. foreach ($modulesdir as $dir) {
  1259. // Load modules attributes in arrays (name, numero, orders) from dir directory
  1260. //print $dir."\n<br>";
  1261. dol_syslog("Scan directory ".$dir." for modules");
  1262. $handle = @opendir(dol_osencode($dir));
  1263. if (is_resource($handle)) {
  1264. while (($file = readdir($handle)) !== false) {
  1265. //print "$i ".$file."\n<br>";
  1266. if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
  1267. $modName = substr($file, 0, dol_strlen($file) - 10);
  1268. if ($modName) {
  1269. include_once $dir.$file;
  1270. $objMod = new $modName($db);
  1271. if ($objMod->numero > 0) {
  1272. $j = $objMod->numero;
  1273. } else {
  1274. $j = 1000 + $i;
  1275. }
  1276. $modulequalified = 1;
  1277. // We discard modules according to features level (PS: if module is activated we always show it)
  1278. $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($objMod)));
  1279. if ($objMod->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2 && !$conf->global->$const_name) {
  1280. $modulequalified = 0;
  1281. }
  1282. if ($objMod->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1 && !$conf->global->$const_name) {
  1283. $modulequalified = 0;
  1284. }
  1285. //If module is not activated disqualified
  1286. if (empty($conf->global->$const_name)) {
  1287. $modulequalified = 0;
  1288. }
  1289. if ($modulequalified) {
  1290. // Load languages files of module
  1291. if (isset($objMod->langfiles) && is_array($objMod->langfiles)) {
  1292. foreach ($objMod->langfiles as $langfile) {
  1293. $langs->load($langfile);
  1294. }
  1295. }
  1296. $modules[$i] = $objMod;
  1297. $filename[$i] = $modName;
  1298. $orders[$i] = $objMod->family."_".$j; // Sort on family then module number
  1299. $dirmod[$i] = $dir;
  1300. //print "x".$modName." ".$orders[$i]."\n<br>";
  1301. if (!empty($objMod->module_parts['contactelement'])) {
  1302. if (is_array($objMod->module_parts['contactelement'])) {
  1303. foreach ($objMod->module_parts['contactelement'] as $elem => $title) {
  1304. $elementList[$elem] = $langs->trans($title);
  1305. }
  1306. } else {
  1307. $elementList[$objMod->name] = $langs->trans($objMod->name);
  1308. }
  1309. }
  1310. $j++;
  1311. $i++;
  1312. } else {
  1313. dol_syslog("Module ".get_class($objMod)." not qualified");
  1314. }
  1315. }
  1316. }
  1317. }
  1318. closedir($handle);
  1319. } else {
  1320. dol_syslog("htdocs/admin/modules.php: Failed to open directory ".$dir.". See permission and open_basedir option.", LOG_WARNING);
  1321. }
  1322. }
  1323. dol_syslog("", LOG_DEBUG, -1);
  1324. return 1;
  1325. }
  1326. /**
  1327. * Show array with constants to edit
  1328. *
  1329. * @param array $tableau Array of constants array('key'=>array('type'=>type, 'label'=>label)
  1330. * where type can be 'string', 'text', 'textarea', 'html', 'yesno', 'emailtemplate:xxx', ...
  1331. * @param int $strictw3c 0=Include form into table (deprecated), 1=Form is outside table to respect W3C (deprecated), 2=No form nor button at all, 3=No form nor button at all and each field has a unique name (form is output by caller, recommended)
  1332. * @param string $helptext Tooltip help to use for the column name of values
  1333. * @param string $text Text to use for the column name of values
  1334. * @return void
  1335. */
  1336. function form_constantes($tableau, $strictw3c = 0, $helptext = '', $text = 'Value')
  1337. {
  1338. global $db, $langs, $conf, $user;
  1339. global $_Avery_Labels;
  1340. $form = new Form($db);
  1341. if (empty($strictw3c)) {
  1342. dol_syslog("Warning: Function form_constantes is calle with parameter strictw3c = 0, this is deprecated. Value must be 2 now.", LOG_DEBUG);
  1343. }
  1344. if (!empty($strictw3c) && $strictw3c == 1) {
  1345. print "\n".'<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  1346. print '<input type="hidden" name="token" value="'.newToken().'">';
  1347. print '<input type="hidden" name="action" value="updateall">';
  1348. }
  1349. print '<div class="div-table-responsive-no-min">';
  1350. print '<table class="noborder centpercent">';
  1351. print '<tr class="liste_titre">';
  1352. print '<td class="">'.$langs->trans("Description").'</td>';
  1353. print '<td>';
  1354. $text = $langs->trans($text);
  1355. print $form->textwithpicto($text, $helptext, 1, 'help', '', 0, 2, 'idhelptext');
  1356. print '</td>';
  1357. if (empty($strictw3c)) {
  1358. print '<td class="center" width="80">'.$langs->trans("Action").'</td>';
  1359. }
  1360. print "</tr>\n";
  1361. $label = '';
  1362. foreach ($tableau as $key => $const) { // Loop on each param
  1363. $label = '';
  1364. // $const is a const key like 'MYMODULE_ABC'
  1365. if (is_numeric($key)) { // Very old behaviour
  1366. $type = 'string';
  1367. } else {
  1368. if (is_array($const)) {
  1369. $type = $const['type'];
  1370. $label = $const['label'];
  1371. $const = $key;
  1372. } else {
  1373. $type = $const;
  1374. $const = $key;
  1375. }
  1376. }
  1377. $sql = "SELECT ";
  1378. $sql .= "rowid";
  1379. $sql .= ", ".$db->decrypt('name')." as name";
  1380. $sql .= ", ".$db->decrypt('value')." as value";
  1381. $sql .= ", type";
  1382. $sql .= ", note";
  1383. $sql .= " FROM ".MAIN_DB_PREFIX."const";
  1384. $sql .= " WHERE ".$db->decrypt('name')." = '".$db->escape($const)."'";
  1385. $sql .= " AND entity IN (0, ".$conf->entity.")";
  1386. $sql .= " ORDER BY name ASC, entity DESC";
  1387. $result = $db->query($sql);
  1388. dol_syslog("List params", LOG_DEBUG);
  1389. if ($result) {
  1390. $obj = $db->fetch_object($result); // Take first result of select
  1391. if (empty($obj)) { // If not yet into table
  1392. $obj = (object) array('rowid'=>'', 'name'=>$const, 'value'=>'', 'type'=>$type, 'note'=>'');
  1393. }
  1394. if (empty($strictw3c)) {
  1395. print "\n".'<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  1396. print '<input type="hidden" name="token" value="'.newToken().'">';
  1397. }
  1398. print '<tr class="oddeven">';
  1399. // Show constant
  1400. print '<td>';
  1401. if (empty($strictw3c)) {
  1402. print '<input type="hidden" name="action" value="update">';
  1403. }
  1404. print '<input type="hidden" name="rowid'.(empty($strictw3c) ? '' : '[]').'" value="'.$obj->rowid.'">';
  1405. print '<input type="hidden" name="constname'.(empty($strictw3c) ? '' : '[]').'" value="'.$const.'">';
  1406. print '<input type="hidden" name="constnote_'.$obj->name.'" value="'.nl2br(dol_escape_htmltag($obj->note)).'">';
  1407. print '<input type="hidden" name="consttype_'.$obj->name.'" value="'.($obj->type ? $obj->type : 'string').'">';
  1408. print ($label ? $label : $langs->trans('Desc'.$const));
  1409. if ($const == 'ADHERENT_MAILMAN_URL') {
  1410. print '. '.$langs->trans("Example").': <a href="#" id="exampleclick1">'.img_down().'</a><br>';
  1411. //print 'http://lists.exampe.com/cgi-bin/mailman/admin/%LISTE%/members?adminpw=%MAILMAN_ADMINPW%&subscribees=%EMAIL%&send_welcome_msg_to_this_batch=1';
  1412. print '<div id="example1" class="hidden">';
  1413. 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';
  1414. print '</div>';
  1415. }
  1416. if ($const == 'ADHERENT_MAILMAN_UNSUB_URL') {
  1417. print '. '.$langs->trans("Example").': <a href="#" id="exampleclick2">'.img_down().'</a><br>';
  1418. print '<div id="example2" class="hidden">';
  1419. 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';
  1420. print '</div>';
  1421. //print 'http://lists.example.com/cgi-bin/mailman/admin/%LISTE%/members/remove?adminpw=%MAILMAN_ADMINPW%&unsubscribees=%EMAIL%';
  1422. }
  1423. if ($const == 'ADHERENT_MAILMAN_LISTS') {
  1424. print '. '.$langs->trans("Example").': <a href="#" id="exampleclick3">'.img_down().'</a><br>';
  1425. print '<div id="example3" class="hidden">';
  1426. print 'mymailmanlist<br>';
  1427. print 'mymailmanlist1,mymailmanlist2<br>';
  1428. print 'TYPE:Type1:mymailmanlist1,TYPE:Type2:mymailmanlist2<br>';
  1429. if ($conf->categorie->enabled) {
  1430. print 'CATEG:Categ1:mymailmanlist1,CATEG:Categ2:mymailmanlist2<br>';
  1431. }
  1432. print '</div>';
  1433. //print 'http://lists.example.com/cgi-bin/mailman/admin/%LISTE%/members/remove?adminpw=%MAILMAN_ADMINPW%&unsubscribees=%EMAIL%';
  1434. }
  1435. print "</td>\n";
  1436. // Value
  1437. if ($const == 'ADHERENT_CARD_TYPE' || $const == 'ADHERENT_ETIQUETTE_TYPE') {
  1438. print '<td>';
  1439. // List of possible labels (defined into $_Avery_Labels variable set into format_cards.lib.php)
  1440. require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
  1441. $arrayoflabels = array();
  1442. foreach (array_keys($_Avery_Labels) as $codecards) {
  1443. $arrayoflabels[$codecards] = $_Avery_Labels[$codecards]['name'];
  1444. }
  1445. print $form->selectarray('constvalue'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')), $arrayoflabels, ($obj->value ? $obj->value : 'CARD'), 1, 0, 0);
  1446. print '<input type="hidden" name="consttype" value="yesno">';
  1447. print '<input type="hidden" name="constnote'.(empty($strictw3c) ? '' : '[]').'" value="'.nl2br(dol_escape_htmltag($obj->note)).'">';
  1448. print '</td>';
  1449. } else {
  1450. print '<td>';
  1451. print '<input type="hidden" name="consttype'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')).'" value="'.($obj->type ? $obj->type : 'string').'">';
  1452. print '<input type="hidden" name="constnote'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')).'" value="'.nl2br(dol_escape_htmltag($obj->note)).'">';
  1453. if ($obj->type == 'textarea' || in_array($const, array('ADHERENT_CARD_TEXT', 'ADHERENT_CARD_TEXT_RIGHT', 'ADHERENT_ETIQUETTE_TEXT'))) {
  1454. print '<textarea class="flat" name="constvalue'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')).'" cols="50" rows="5" wrap="soft">'."\n";
  1455. print $obj->value;
  1456. print "</textarea>\n";
  1457. } elseif ($obj->type == 'html') {
  1458. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  1459. $doleditor = new DolEditor('constvalue'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')), $obj->value, '', 160, 'dolibarr_notes', '', false, false, $conf->fckeditor->enabled, ROWS_5, '90%');
  1460. $doleditor->Create();
  1461. } elseif ($obj->type == 'yesno') {
  1462. print $form->selectyesno('constvalue'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')), $obj->value, 1);
  1463. } elseif (preg_match('/emailtemplate/', $obj->type)) {
  1464. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  1465. $formmail = new FormMail($db);
  1466. $tmp = explode(':', $obj->type);
  1467. $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, -1); // We set lang=null to get in priority record with no lang
  1468. //$arraydefaultmessage = $formmail->getEMailTemplate($db, $tmp[1], $user, null, 0, 1, '');
  1469. $arrayofmessagename = array();
  1470. if (is_array($formmail->lines_model)) {
  1471. foreach ($formmail->lines_model as $modelmail) {
  1472. //var_dump($modelmail);
  1473. $moreonlabel = '';
  1474. if (!empty($arrayofmessagename[$modelmail->label])) {
  1475. $moreonlabel = ' <span class="opacitymedium">('.$langs->trans("SeveralLangugeVariatFound").')</span>';
  1476. }
  1477. // The 'label' is the key that is unique if we exclude the language
  1478. $arrayofmessagename[$modelmail->label.':'.$tmp[1]] = $langs->trans(preg_replace('/\(|\)/', '', $modelmail->label)).$moreonlabel;
  1479. }
  1480. }
  1481. //var_dump($arraydefaultmessage);
  1482. //var_dump($arrayofmessagename);
  1483. print $form->selectarray('constvalue'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')), $arrayofmessagename, $obj->value.':'.$tmp[1], 'None', 0, 0, '', 0, 0, 0, '', '', 1);
  1484. } elseif (preg_match('/MAIL_FROM$/i', $const)) {
  1485. 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).'">';
  1486. } else { // type = 'string' ou 'chaine'
  1487. print '<input type="text" class="flat minwidth300" name="constvalue'.(empty($strictw3c) ? '' : ($strictw3c == 3 ? '_'.$const : '[]')).'" value="'.dol_escape_htmltag($obj->value).'">';
  1488. }
  1489. print '</td>';
  1490. }
  1491. // Submit
  1492. if (empty($strictw3c)) {
  1493. print '<td class="center">';
  1494. print '<input type="submit" class="button" value="'.$langs->trans("Update").'" name="Button">';
  1495. print "</td>";
  1496. }
  1497. print "</tr>\n";
  1498. if (empty($strictw3c)) {
  1499. print "</form>\n";
  1500. }
  1501. }
  1502. }
  1503. print '</table>';
  1504. print '</div>';
  1505. if (!empty($strictw3c) && $strictw3c == 1) {
  1506. print '<div align="center"><input type="submit" class="button" value="'.$langs->trans("Update").'" name="update"></div>';
  1507. print "</form>\n";
  1508. }
  1509. }
  1510. /**
  1511. * Show array with constants to edit
  1512. *
  1513. * @param array $modules Array of all modules
  1514. * @return string HTML string with warning
  1515. */
  1516. function showModulesExludedForExternal($modules)
  1517. {
  1518. global $conf, $langs;
  1519. $text = $langs->trans("OnlyFollowingModulesAreOpenedToExternalUsers");
  1520. $listofmodules = explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL);
  1521. $i = 0;
  1522. if (!empty($modules)) {
  1523. foreach ($modules as $module) {
  1524. $moduleconst = $module->const_name;
  1525. $modulename = strtolower($module->name);
  1526. //print 'modulename='.$modulename;
  1527. //if (empty($conf->global->$moduleconst)) continue;
  1528. if (!in_array($modulename, $listofmodules)) {
  1529. continue;
  1530. }
  1531. //var_dump($modulename.' - '.$langs->trans('Module'.$module->numero.'Name'));
  1532. if ($i > 0) {
  1533. $text .= ', ';
  1534. } else {
  1535. $text .= ' ';
  1536. }
  1537. $i++;
  1538. $text .= $langs->trans('Module'.$module->numero.'Name');
  1539. }
  1540. }
  1541. return $text;
  1542. }
  1543. /**
  1544. * Add document model used by doc generator
  1545. *
  1546. * @param string $name Model name
  1547. * @param string $type Model type
  1548. * @param string $label Model label
  1549. * @param string $description Model description
  1550. * @return int <0 if KO, >0 if OK
  1551. */
  1552. function addDocumentModel($name, $type, $label = '', $description = '')
  1553. {
  1554. global $db, $conf;
  1555. $db->begin();
  1556. $sql = "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity, libelle, description)";
  1557. $sql .= " VALUES ('".$db->escape($name)."','".$db->escape($type)."',".$conf->entity.", ";
  1558. $sql .= ($label ? "'".$db->escape($label)."'" : 'null').", ";
  1559. $sql .= (!empty($description) ? "'".$db->escape($description)."'" : "null");
  1560. $sql .= ")";
  1561. dol_syslog("admin.lib::addDocumentModel", LOG_DEBUG);
  1562. $resql = $db->query($sql);
  1563. if ($resql) {
  1564. $db->commit();
  1565. return 1;
  1566. } else {
  1567. dol_print_error($db);
  1568. $db->rollback();
  1569. return -1;
  1570. }
  1571. }
  1572. /**
  1573. * Delete document model used by doc generator
  1574. *
  1575. * @param string $name Model name
  1576. * @param string $type Model type
  1577. * @return int <0 if KO, >0 if OK
  1578. */
  1579. function delDocumentModel($name, $type)
  1580. {
  1581. global $db, $conf;
  1582. $db->begin();
  1583. $sql = "DELETE FROM ".MAIN_DB_PREFIX."document_model";
  1584. $sql .= " WHERE nom = '".$db->escape($name)."'";
  1585. $sql .= " AND type = '".$db->escape($type)."'";
  1586. $sql .= " AND entity = ".$conf->entity;
  1587. dol_syslog("admin.lib::delDocumentModel", LOG_DEBUG);
  1588. $resql = $db->query($sql);
  1589. if ($resql) {
  1590. $db->commit();
  1591. return 1;
  1592. } else {
  1593. dol_print_error($db);
  1594. $db->rollback();
  1595. return -1;
  1596. }
  1597. }
  1598. /**
  1599. * Return the php_info into an array
  1600. *
  1601. * @return array Array with PHP infos
  1602. */
  1603. function phpinfo_array()
  1604. {
  1605. ob_start();
  1606. phpinfo();
  1607. $phpinfostring = ob_get_contents();
  1608. ob_end_clean();
  1609. $info_arr = array();
  1610. $info_lines = explode("\n", strip_tags($phpinfostring, "<tr><td><h2>"));
  1611. $cat = "General";
  1612. foreach ($info_lines as $line) {
  1613. // new cat?
  1614. $title = array();
  1615. preg_match("~<h2>(.*)</h2>~", $line, $title) ? $cat = $title[1] : null;
  1616. $val = array();
  1617. if (preg_match("~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~", $line, $val)) {
  1618. $info_arr[trim($cat)][trim($val[1])] = $val[2];
  1619. } elseif (preg_match("~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~", $line, $val)) {
  1620. $info_arr[trim($cat)][trim($val[1])] = array("local" => $val[2], "master" => $val[3]);
  1621. }
  1622. }
  1623. return $info_arr;
  1624. }
  1625. /**
  1626. * Return array head with list of tabs to view object informations.
  1627. *
  1628. * @return array head array with tabs
  1629. */
  1630. function company_admin_prepare_head()
  1631. {
  1632. global $langs, $conf;
  1633. $h = 0;
  1634. $head = array();
  1635. $head[$h][0] = DOL_URL_ROOT."/admin/company.php";
  1636. $head[$h][1] = $langs->trans("Company");
  1637. $head[$h][2] = 'company';
  1638. $h++;
  1639. $head[$h][0] = DOL_URL_ROOT."/admin/openinghours.php";
  1640. $head[$h][1] = $langs->trans("OpeningHours");
  1641. $head[$h][2] = 'openinghours';
  1642. $h++;
  1643. $head[$h][0] = DOL_URL_ROOT."/admin/accountant.php";
  1644. $head[$h][1] = $langs->trans("Accountant");
  1645. $head[$h][2] = 'accountant';
  1646. $h++;
  1647. $head[$h][0] = DOL_URL_ROOT."/admin/company_socialnetworks.php";
  1648. $head[$h][1] = $langs->trans("SocialNetworksInformation");
  1649. $head[$h][2] = 'socialnetworks';
  1650. $h++;
  1651. complete_head_from_modules($conf, $langs, null, $head, $h, 'mycompany_admin', 'add');
  1652. complete_head_from_modules($conf, $langs, null, $head, $h, 'mycompany_admin', 'remove');
  1653. return $head;
  1654. }
  1655. /**
  1656. * Return array head with list of tabs to view object informations.
  1657. *
  1658. * @return array head array with tabs
  1659. */
  1660. function email_admin_prepare_head()
  1661. {
  1662. global $langs, $conf, $user;
  1663. $h = 0;
  1664. $head = array();
  1665. if (!empty($user->admin) && (empty($_SESSION['leftmenu']) || $_SESSION['leftmenu'] != 'email_templates')) {
  1666. $head[$h][0] = DOL_URL_ROOT."/admin/mails.php";
  1667. $head[$h][1] = $langs->trans("OutGoingEmailSetup");
  1668. $head[$h][2] = 'common';
  1669. $h++;
  1670. if (!empty($conf->mailing->enabled)) {
  1671. $head[$h][0] = DOL_URL_ROOT."/admin/mails_emailing.php";
  1672. $head[$h][1] = $langs->trans("OutGoingEmailSetupForEmailing", $langs->transnoentitiesnoconv("EMailing"));
  1673. $head[$h][2] = 'common_emailing';
  1674. $h++;
  1675. }
  1676. if (!empty($conf->ticket->enabled)) {
  1677. $head[$h][0] = DOL_URL_ROOT."/admin/mails_ticket.php";
  1678. $head[$h][1] = $langs->trans("OutGoingEmailSetupForEmailing", $langs->transnoentitiesnoconv("Ticket"));
  1679. $head[$h][2] = 'common_ticket';
  1680. $h++;
  1681. }
  1682. }
  1683. // admin and non admin can view this menu entry, but it is not shown yet when we on user menu "Email templates"
  1684. if (empty($_SESSION['leftmenu']) || $_SESSION['leftmenu'] != 'email_templates') {
  1685. $head[$h][0] = DOL_URL_ROOT."/admin/mails_senderprofile_list.php";
  1686. $head[$h][1] = $langs->trans("EmailSenderProfiles");
  1687. $head[$h][2] = 'senderprofiles';
  1688. $h++;
  1689. }
  1690. $head[$h][0] = DOL_URL_ROOT."/admin/mails_templates.php";
  1691. $head[$h][1] = $langs->trans("EMailTemplates");
  1692. $head[$h][2] = 'templates';
  1693. $h++;
  1694. complete_head_from_modules($conf, $langs, null, $head, $h, 'email_admin', 'remove');
  1695. return $head;
  1696. }