admin.lib.php 70 KB

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