admin.lib.php 71 KB

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