functions2.lib.php 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  1. <?php
  2. /* Copyright (C) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2008-2012 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
  5. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  6. * Copyright (C) 2015 Ferran Marcet <fmarcet@2byte.es>
  7. * Copyright (C) 2015-2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. * or see http://www.gnu.org/
  22. */
  23. /**
  24. * \file htdocs/core/lib/functions2.lib.php
  25. * \brief A set of functions for Dolibarr
  26. * This file contains all rare functions.
  27. */
  28. // Enable this line to trace path when function is called.
  29. //print xdebug_print_function_stack('Functions2.lib was called');exit;
  30. /**
  31. * Same function than javascript unescape() function but in PHP.
  32. *
  33. * @param string $source String to decode
  34. * @return string Unescaped string
  35. */
  36. function jsUnEscape($source)
  37. {
  38. $decodedStr = "";
  39. $pos = 0;
  40. $len = strlen($source);
  41. while ($pos < $len) {
  42. $charAt = substr($source, $pos, 1);
  43. if ($charAt == '%') {
  44. $pos++;
  45. $charAt = substr($source, $pos, 1);
  46. if ($charAt == 'u') {
  47. // we got a unicode character
  48. $pos++;
  49. $unicodeHexVal = substr($source, $pos, 4);
  50. $unicode = hexdec($unicodeHexVal);
  51. $entity = "&#". $unicode . ';';
  52. $decodedStr .= utf8_encode($entity);
  53. $pos += 4;
  54. }
  55. else {
  56. // we have an escaped ascii character
  57. $hexVal = substr($source, $pos, 2);
  58. $decodedStr .= chr(hexdec($hexVal));
  59. $pos += 2;
  60. }
  61. } else {
  62. $decodedStr .= $charAt;
  63. $pos++;
  64. }
  65. }
  66. return dol_html_entity_decode($decodedStr, ENT_COMPAT);
  67. }
  68. /**
  69. * Return list of modules directories. We detect directories that contains a subdirectory /core/modules
  70. * We discard directory modules that contains 'disabled' into their name.
  71. *
  72. * @param string $subdir Sub directory (Example: '/mailings')
  73. * @return array Array of directories that can contains module descriptors
  74. */
  75. function dolGetModulesDirs($subdir='')
  76. {
  77. global $conf;
  78. $modulesdir=array();
  79. foreach ($conf->file->dol_document_root as $type => $dirroot)
  80. {
  81. // Default core/modules dir
  82. if ($type === 'main') {
  83. $modulesdir[$dirroot . '/core/modules' . $subdir . '/'] = $dirroot . '/core/modules' . $subdir . '/';
  84. }
  85. // Scan dir from external modules
  86. $handle=@opendir($dirroot);
  87. if (is_resource($handle))
  88. {
  89. while (($file = readdir($handle))!==false)
  90. {
  91. if (preg_match('/disabled/',$file)) continue; // We discard module if it contains disabled into name.
  92. if (is_dir($dirroot.'/'.$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS' && $file != 'includes')
  93. {
  94. if (is_dir($dirroot . '/' . $file . '/core/modules'.$subdir.'/'))
  95. {
  96. $modulesdir[$dirroot . '/' . $file . '/core/modules'.$subdir.'/'] = $dirroot . '/' . $file . '/core/modules'.$subdir.'/';
  97. }
  98. }
  99. }
  100. closedir($handle);
  101. }
  102. }
  103. return $modulesdir;
  104. }
  105. /**
  106. * Try to guess default paper format according to language into $langs
  107. *
  108. * @param Translate $outputlangs Output lang to use to autodetect output format if setup not done
  109. * @return string Default paper format code
  110. */
  111. function dol_getDefaultFormat($outputlangs='')
  112. {
  113. global $langs;
  114. $selected='EUA4';
  115. if (empty($outputlangs) || ! is_object($outputlangs)) $outputlangs=$langs;
  116. if ($outputlangs->defaultlang == 'ca_CA') $selected='CAP4'; // Canada
  117. if ($outputlangs->defaultlang == 'en_US') $selected='USLetter'; // US
  118. return $selected;
  119. }
  120. /**
  121. * Output content of a file $filename in version of current language (otherwise may use an alternate language)
  122. *
  123. * @param Translate $langs Object language to use for output
  124. * @param string $filename Relative filename to output
  125. * @param int $searchalt 1=Search also in alternative languages
  126. * @return boolean true if OK, false if KO
  127. */
  128. function dol_print_file($langs,$filename,$searchalt=0)
  129. {
  130. global $conf;
  131. // Test if file is in lang directory
  132. foreach($langs->dir as $searchdir)
  133. {
  134. $formfile=($searchdir."/langs/".$langs->defaultlang."/".$filename);
  135. dol_syslog('functions2::dol_print_file search file '.$formfile, LOG_DEBUG);
  136. if (is_readable($formfile))
  137. {
  138. $content=file_get_contents($formfile);
  139. $isutf8=utf8_check($content);
  140. if (! $isutf8 && $conf->file->character_set_client == 'UTF-8') print utf8_encode($content);
  141. elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') print utf8_decode($content);
  142. else print $content;
  143. return true;
  144. }
  145. else dol_syslog('functions2::dol_print_file not found', LOG_DEBUG);
  146. if ($searchalt) {
  147. // Test si fichier dans repertoire de la langue alternative
  148. if ($langs->defaultlang != "en_US") $formfilealt = $searchdir."/langs/en_US/".$filename;
  149. else $formfilealt = $searchdir."/langs/fr_FR/".$filename;
  150. dol_syslog('functions2::dol_print_file search alt file '.$formfilealt, LOG_DEBUG);
  151. //print 'getcwd='.getcwd().' htmlfilealt='.$formfilealt.' X '.file_exists(getcwd().'/'.$formfilealt);
  152. if (is_readable($formfilealt))
  153. {
  154. $content=file_get_contents($formfilealt);
  155. $isutf8=utf8_check($content);
  156. if (! $isutf8 && $conf->file->character_set_client == 'UTF-8') print utf8_encode($content);
  157. elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') print utf8_decode($content);
  158. else print $content;
  159. return true;
  160. }
  161. else dol_syslog('functions2::dol_print_file not found', LOG_DEBUG);
  162. }
  163. }
  164. return false;
  165. }
  166. /**
  167. * Show informations on an object
  168. * TODO Move this into html.formother
  169. *
  170. * @param object $object Objet to show
  171. * @param int $usetable Output into a table
  172. * @return void
  173. */
  174. function dol_print_object_info($object, $usetable=0)
  175. {
  176. global $langs,$db;
  177. $langs->load("other");
  178. $langs->load("admin");
  179. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  180. $deltadateforserver=getServerTimeZoneInt('now');
  181. $deltadateforclient=((int) $_SESSION['dol_tz'] + (int) $_SESSION['dol_dst']);
  182. //$deltadateforcompany=((int) $_SESSION['dol_tz'] + (int) $_SESSION['dol_dst']);
  183. $deltadateforuser=round($deltadateforclient-$deltadateforserver);
  184. //print "x".$deltadateforserver." - ".$deltadateforclient." - ".$deltadateforuser;
  185. if ($usetable) print '<table class="border centpercent">';
  186. // Import key
  187. if (! empty($object->import_key))
  188. {
  189. if ($usetable) print '<tr><td class="titlefield">';
  190. print $langs->trans("ImportedWithSet");
  191. if ($usetable) print '</td><td>';
  192. else print ': ';
  193. print $object->import_key;
  194. if ($usetable) print '</td></tr>';
  195. else print '<br>';
  196. }
  197. // User creation (old method using already loaded object and not id is kept for backward compatibility)
  198. if (! empty($object->user_creation) || ! empty($object->user_creation_id))
  199. {
  200. if ($usetable) print '<tr><td class="titlefield">';
  201. print $langs->trans("CreatedBy");
  202. if ($usetable) print '</td><td>';
  203. else print ': ';
  204. if (is_object($object->user_creation))
  205. {
  206. if ($object->user_creation->id) print $object->user_creation->getNomUrl(1);
  207. else print $langs->trans("Unknown");
  208. }
  209. else
  210. {
  211. $userstatic=new User($db);
  212. $userstatic->fetch($object->user_creation_id ? $object->user_creation_id : $object->user_creation);
  213. if ($userstatic->id) print $userstatic->getNomUrl(1);
  214. else print $langs->trans("Unknown");
  215. }
  216. if ($usetable) print '</td></tr>';
  217. else print '<br>';
  218. }
  219. // Date creation
  220. if (! empty($object->date_creation))
  221. {
  222. if ($usetable) print '<tr><td class="titlefield">';
  223. print $langs->trans("DateCreation");
  224. if ($usetable) print '</td><td>';
  225. else print ': ';
  226. print dol_print_date($object->date_creation, 'dayhour');
  227. if ($deltadateforuser) print ' '.$langs->trans("CurrentHour").' &nbsp; / &nbsp; '.dol_print_date($object->date_creation+($deltadateforuser*3600),"dayhour").' &nbsp;'.$langs->trans("ClientHour");
  228. if ($usetable) print '</td></tr>';
  229. else print '<br>';
  230. }
  231. // User change (old method using already loaded object and not id is kept for backward compatibility)
  232. if (! empty($object->user_modification) || ! empty($object->user_modification_id))
  233. {
  234. if ($usetable) print '<tr><td class="titlefield">';
  235. print $langs->trans("ModifiedBy");
  236. if ($usetable) print '</td><td>';
  237. else print ': ';
  238. if (is_object($object->user_modification))
  239. {
  240. if ($object->user_modification->id) print $object->user_modification->getNomUrl(1);
  241. else print $langs->trans("Unknown");
  242. }
  243. else
  244. {
  245. $userstatic=new User($db);
  246. $userstatic->fetch($object->user_modification_id ? $object->user_modification_id : $object->user_modification);
  247. if ($userstatic->id) print $userstatic->getNomUrl(1);
  248. else print $langs->trans("Unknown");
  249. }
  250. if ($usetable) print '</td></tr>';
  251. else print '<br>';
  252. }
  253. // Date change
  254. if (! empty($object->date_modification))
  255. {
  256. if ($usetable) print '<tr><td class="titlefield">';
  257. print $langs->trans("DateLastModification");
  258. if ($usetable) print '</td><td>';
  259. else print ': ';
  260. print dol_print_date($object->date_modification, 'dayhour');
  261. if ($deltadateforuser) print ' '.$langs->trans("CurrentHour").' &nbsp; / &nbsp; '.dol_print_date($object->date_modification+($deltadateforuser*3600),"dayhour").' &nbsp;'.$langs->trans("ClientHour");
  262. if ($usetable) print '</td></tr>';
  263. else print '<br>';
  264. }
  265. // User validation (old method using already loaded object and not id is kept for backward compatibility)
  266. if (! empty($object->user_validation) || ! empty($object->user_validation_id))
  267. {
  268. if ($usetable) print '<tr><td class="titlefield">';
  269. print $langs->trans("ValidatedBy");
  270. if ($usetable) print '</td><td>';
  271. else print ': ';
  272. if (is_object($object->user_validation))
  273. {
  274. if ($object->user_validation->id) print $object->user_validation->getNomUrl(1);
  275. else print $langs->trans("Unknown");
  276. }
  277. else
  278. {
  279. $userstatic=new User($db);
  280. $userstatic->fetch($object->user_validation_id ? $object->user_validation_id : $object->user_validation);
  281. if ($userstatic->id) print $userstatic->getNomUrl(1);
  282. else print $langs->trans("Unknown");
  283. }
  284. if ($usetable) print '</td></tr>';
  285. else print '<br>';
  286. }
  287. // Date validation
  288. if (! empty($object->date_validation))
  289. {
  290. if ($usetable) print '<tr><td class="titlefield">';
  291. print $langs->trans("DateValidation");
  292. if ($usetable) print '</td><td>';
  293. else print ': ';
  294. print dol_print_date($object->date_validation, 'dayhour');
  295. if ($deltadateforuser) print ' '.$langs->trans("CurrentHour").' &nbsp; / &nbsp; '.dol_print_date($object->date_validation+($deltadateforuser*3600),"dayhour").' &nbsp;'.$langs->trans("ClientHour");
  296. if ($usetable) print '</td></tr>';
  297. else print '<br>';
  298. }
  299. // User approve (old method using already loaded object and not id is kept for backward compatibility)
  300. if (! empty($object->user_approve) || ! empty($object->user_approve_id))
  301. {
  302. if ($usetable) print '<tr><td class="titlefield">';
  303. print $langs->trans("ApprovedBy");
  304. if ($usetable) print '</td><td>';
  305. else print ': ';
  306. if (is_object($object->user_approve))
  307. {
  308. if ($object->user_approve->id) print $object->user_approve->getNomUrl(1);
  309. else print $langs->trans("Unknown");
  310. }
  311. else
  312. {
  313. $userstatic=new User($db);
  314. $userstatic->fetch($object->user_approve_id ? $object->user_approve_id : $object->user_approve);
  315. if ($userstatic->id) print $userstatic->getNomUrl(1);
  316. else print $langs->trans("Unknown");
  317. }
  318. if ($usetable) print '</td></tr>';
  319. else print '<br>';
  320. }
  321. // Date approve
  322. if (! empty($object->date_approve))
  323. {
  324. if ($usetable) print '<tr><td class="titlefield">';
  325. print $langs->trans("DateApprove");
  326. if ($usetable) print '</td><td>';
  327. else print ': ';
  328. print dol_print_date($object->date_approve, 'dayhour');
  329. if ($deltadateforuser) print ' '.$langs->trans("CurrentHour").' &nbsp; / &nbsp; '.dol_print_date($object->date_approve+($deltadateforuser*3600),"dayhour").' &nbsp;'.$langs->trans("ClientHour");
  330. if ($usetable) print '</td></tr>';
  331. else print '<br>';
  332. }
  333. // User approve
  334. if (! empty($object->user_approve_id2))
  335. {
  336. if ($usetable) print '<tr><td class="titlefield">';
  337. print $langs->trans("ApprovedBy");
  338. if ($usetable) print '</td><td>';
  339. else print ': ';
  340. $userstatic=new User($db);
  341. $userstatic->fetch($object->user_approve_id2);
  342. if ($userstatic->id) print $userstatic->getNomUrl(1);
  343. else print $langs->trans("Unknown");
  344. if ($usetable) print '</td></tr>';
  345. else print '<br>';
  346. }
  347. // Date approve
  348. if (! empty($object->date_approve2))
  349. {
  350. if ($usetable) print '<tr><td class="titlefield">';
  351. print $langs->trans("DateApprove2");
  352. if ($usetable) print '</td><td>';
  353. else print ': ';
  354. print dol_print_date($object->date_approve2, 'dayhour');
  355. if ($deltadateforuser) print ' '.$langs->trans("CurrentHour").' &nbsp; / &nbsp; '.dol_print_date($object->date_approve2+($deltadateforuser*3600),"dayhour").' &nbsp;'.$langs->trans("ClientHour");
  356. if ($usetable) print '</td></tr>';
  357. else print '<br>';
  358. }
  359. // User close
  360. if (! empty($object->user_cloture))
  361. {
  362. if ($usetable) print '<tr><td class="titlefield">';
  363. print $langs->trans("ClosedBy");
  364. if ($usetable) print '</td><td>';
  365. else print ': ';
  366. if (is_object($object->user_cloture))
  367. {
  368. if ($object->user_cloture->id) print $object->user_cloture->getNomUrl(1);
  369. else print $langs->trans("Unknown");
  370. }
  371. else
  372. {
  373. $userstatic=new User($db);
  374. $userstatic->fetch($object->user_cloture);
  375. if ($userstatic->id) print $userstatic->getNomUrl(1);
  376. else print $langs->trans("Unknown");
  377. }
  378. if ($usetable) print '</td></tr>';
  379. else print '<br>';
  380. }
  381. // Date close
  382. if (! empty($object->date_cloture))
  383. {
  384. if ($usetable) print '<tr><td class="titlefield">';
  385. print $langs->trans("DateClosing");
  386. if ($usetable) print '</td><td>';
  387. else print ': ';
  388. print dol_print_date($object->date_cloture, 'dayhour');
  389. if ($deltadateforuser) print ' '.$langs->trans("CurrentHour").' &nbsp; / &nbsp; '.dol_print_date($object->date_cloture+($deltadateforuser*3600),"dayhour").' &nbsp;'.$langs->trans("ClientHour");
  390. if ($usetable) print '</td></tr>';
  391. else print '<br>';
  392. }
  393. // User conciliate
  394. if (! empty($object->user_rappro))
  395. {
  396. if ($usetable) print '<tr><td class="titlefield">';
  397. print $langs->trans("ConciliatedBy");
  398. if ($usetable) print '</td><td>';
  399. else print ': ';
  400. if (is_object($object->user_rappro))
  401. {
  402. if ($object->user_rappro->id) print $object->user_rappro->getNomUrl(1);
  403. else print $langs->trans("Unknown");
  404. }
  405. else
  406. {
  407. $userstatic=new User($db);
  408. $userstatic->fetch($object->user_rappro);
  409. if ($userstatic->id) print $userstatic->getNomUrl(1);
  410. else print $langs->trans("Unknown");
  411. }
  412. if ($usetable) print '</td></tr>';
  413. else print '<br>';
  414. }
  415. // Date conciliate
  416. if (! empty($object->date_rappro))
  417. {
  418. if ($usetable) print '<tr><td class="titlefield">';
  419. print $langs->trans("DateConciliating");
  420. if ($usetable) print '</td><td>';
  421. else print ': ';
  422. print dol_print_date($object->date_rappro, 'dayhour');
  423. if ($deltadateforuser) print ' '.$langs->trans("CurrentHour").' &nbsp; / &nbsp; '.dol_print_date($object->date_rappro+($deltadateforuser*3600),"dayhour").' &nbsp;'.$langs->trans("ClientHour");
  424. if ($usetable) print '</td></tr>';
  425. else print '<br>';
  426. }
  427. // Date send
  428. if (! empty($object->date_envoi))
  429. {
  430. if ($usetable) print '<tr><td class="titlefield">';
  431. print $langs->trans("DateLastSend");
  432. if ($usetable) print '</td><td>';
  433. else print ': ';
  434. print dol_print_date($object->date_envoi, 'dayhour');
  435. if ($deltadateforuser) print ' '.$langs->trans("CurrentHour").' &nbsp; / &nbsp; '.dol_print_date($object->date_envoi+($deltadateforuser*3600),"dayhour").' &nbsp;'.$langs->trans("ClientHour");
  436. if ($usetable) print '</td></tr>';
  437. else print '<br>';
  438. }
  439. if ($usetable) print '</table>';
  440. }
  441. /**
  442. * Return an email formatted to include a tracking id
  443. * For example myemail@example.com becom myemail+trackingid@example.com
  444. *
  445. * @param string $email Email address (Ex: "toto@example.com", "John Do <johndo@example.com>")
  446. * @param string $trackingid Tracking id (Ex: thi123 for thirdparty with id 123)
  447. * @return string Return email tracker string
  448. */
  449. function dolAddEmailTrackId($email, $trackingid)
  450. {
  451. $tmp=explode('@',$email);
  452. return $tmp[0].'+'.$trackingid.'@'.(isset($tmp[1])?$tmp[1]:'');
  453. }
  454. /**
  455. * Return true if email has a domain name that can't be resolved
  456. *
  457. * @param string $mail Email address (Ex: "toto@example.com", "John Do <johndo@example.com>")
  458. * @return boolean True if domain email is OK, False if KO
  459. */
  460. function isValidMailDomain($mail)
  461. {
  462. list($user, $domain) = explode("@", $mail, 2);
  463. if (checkdnsrr($domain, "MX"))
  464. {
  465. return true;
  466. }
  467. else
  468. {
  469. return false;
  470. }
  471. }
  472. /**
  473. * Url string validation
  474. * <http[s]> :// [user[:pass]@] hostname [port] [/path] [?getquery] [anchor]
  475. *
  476. * @param string $url Url
  477. * @param int $http 1: verify http is provided, 0: not verify http
  478. * @param int $pass 1: verify user and pass is provided, 0: not verify user and pass
  479. * @param int $port 1: verify port is provided, 0: not verify port
  480. * @param int $path 1: verify a path is provided "/" or "/..." or "/.../", 0: not verify path
  481. * @param int $query 1: verify query is provided, 0: not verify query
  482. * @param int $anchor 1: verify anchor is provided, 0: not verify anchor
  483. * @return int 1=Check is OK, 0=Check is KO
  484. */
  485. function isValidUrl($url,$http=0,$pass=0,$port=0,$path=0,$query=0,$anchor=0)
  486. {
  487. $ValidUrl = 0;
  488. $urlregex = '';
  489. // SCHEME
  490. if ($http) $urlregex .= "^(http:\/\/|https:\/\/)";
  491. // USER AND PASS
  492. if ($pass) $urlregex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)";
  493. // HOSTNAME OR IP
  494. //$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*"; // x allowed (ex. http://localhost, http://routerlogin)
  495. //$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)+"; // x.x
  496. $urlregex .= "([a-z0-9+\$_\\\:-])+(\.[a-z0-9+\$_-][a-z0-9+\$_-]+)*"; // x ou x.xx (2 x ou plus)
  497. //use only one of the above
  498. // PORT
  499. if ($port) $urlregex .= "(\:[0-9]{2,5})";
  500. // PATH
  501. if ($path) $urlregex .= "(\/([a-z0-9+\$_-]\.?)+)*\/";
  502. // GET Query
  503. if ($query) $urlregex .= "(\?[a-z+&\$_.-][a-z0-9;:@\/&%=+\$_.-]*)";
  504. // ANCHOR
  505. if ($anchor) $urlregex .= "(#[a-z_.-][a-z0-9+\$_.-]*)$";
  506. // check
  507. if (preg_match('/'.$urlregex.'/i', $url))
  508. {
  509. $ValidUrl = 1;
  510. }
  511. //print $urlregex.' - '.$url.' - '.$ValidUrl;
  512. return $ValidUrl;
  513. }
  514. /**
  515. * Clean an url string
  516. *
  517. * @param string $url Url
  518. * @param integer $http 1 = keep both http:// and https://, 0: remove http:// but not https://
  519. * @return string Cleaned url
  520. */
  521. function clean_url($url,$http=1)
  522. {
  523. // Fixed by Matelli (see http://matelli.fr/showcases/patchs-dolibarr/fix-cleaning-url.html)
  524. // To include the minus sign in a char class, we must not escape it but put it at the end of the class
  525. // Also, there's no need of escape a dot sign in a class
  526. if (preg_match('/^(https?:[\\/]+)?([0-9A-Z.-]+\.[A-Z]{2,4})(:[0-9]+)?/i',$url,$regs))
  527. {
  528. $proto=$regs[1];
  529. $domain=$regs[2];
  530. $port=isset($regs[3])?$regs[3]:'';
  531. //print $url." -> ".$proto." - ".$domain." - ".$port;
  532. //$url = dol_string_nospecial(trim($url));
  533. $url = trim($url);
  534. // Si http: defini on supprime le http (Si https on ne supprime pas)
  535. $newproto=$proto;
  536. if ($http==0)
  537. {
  538. if (preg_match('/^http:[\\/]+/i',$url))
  539. {
  540. $url = preg_replace('/^http:[\\/]+/i','',$url);
  541. $newproto = '';
  542. }
  543. }
  544. // On passe le nom de domaine en minuscule
  545. $CleanUrl = preg_replace('/^'.preg_quote($proto.$domain,'/').'/i', $newproto.strtolower($domain), $url);
  546. return $CleanUrl;
  547. }
  548. else return $url;
  549. }
  550. /**
  551. * Returns an email value with obfuscated parts.
  552. *
  553. * @param string $mail Email
  554. * @param string $replace Replacement character (defaul: *)
  555. * @param int $nbreplace Number of replacement character (default: 8)
  556. * @param int $nbdisplaymail Number of character unchanged (default: 4)
  557. * @param int $nbdisplaydomain Number of character unchanged of domain (default: 3)
  558. * @param bool $displaytld Display tld (default: true)
  559. * @return string Return email with hidden parts or '';
  560. */
  561. function dolObfuscateEmail($mail, $replace="*", $nbreplace=8, $nbdisplaymail=4, $nbdisplaydomain=3, $displaytld=true)
  562. {
  563. if(!isValidEmail($mail))return '';
  564. $tab = explode('@', $mail);
  565. $tab2 = explode('.',$tab[1]);
  566. $string_replace = '';
  567. $mail_name = $tab[0];
  568. $mail_domaine = $tab2[0];
  569. $mail_tld = '';
  570. $nbofelem = count($tab2);
  571. for($i=1; $i < $nbofelem && $displaytld; $i++)
  572. {
  573. $mail_tld .= '.'.$tab2[$i];
  574. }
  575. for($i=0; $i < $nbreplace; $i++){
  576. $string_replace .= $replace;
  577. }
  578. if(strlen($mail_name) > $nbdisplaymail){
  579. $mail_name = substr($mail_name, 0, $nbdisplaymail);
  580. }
  581. if(strlen($mail_domaine) > $nbdisplaydomain){
  582. $mail_domaine = substr($mail_domaine, strlen($mail_domaine)-$nbdisplaydomain);
  583. }
  584. return $mail_name . $string_replace . $mail_domaine . $mail_tld;
  585. }
  586. /**
  587. * Return lines of an html table from an array
  588. * Used by array2table function only
  589. *
  590. * @param array $data Array of data
  591. * @param string $troptions Options for tr
  592. * @param string $tdoptions Options for td
  593. * @return string
  594. */
  595. function array2tr($data,$troptions='',$tdoptions='')
  596. {
  597. $text = '<tr '.$troptions.'>' ;
  598. foreach($data as $key => $item){
  599. $text.= '<td '.$tdoptions.'>'.$item.'</td>' ;
  600. }
  601. $text.= '</tr>' ;
  602. return $text ;
  603. }
  604. /**
  605. * Return an html table from an array
  606. *
  607. * @param array $data Array of data
  608. * @param int $tableMarkup Table markup
  609. * @param string $tableoptions Options for table
  610. * @param string $troptions Options for tr
  611. * @param string $tdoptions Options for td
  612. * @return string
  613. */
  614. function array2table($data,$tableMarkup=1,$tableoptions='',$troptions='',$tdoptions='')
  615. {
  616. $text='' ;
  617. if($tableMarkup) $text = '<table '.$tableoptions.'>' ;
  618. foreach($data as $key => $item){
  619. if(is_array($item)){
  620. $text.=array2tr($item,$troptions,$tdoptions);
  621. } else {
  622. $text.= '<tr '.$troptions.'>' ;
  623. $text.= '<td '.$tdoptions.'>'.$key.'</td>' ;
  624. $text.= '<td '.$tdoptions.'>'.$item.'</td>' ;
  625. $text.= '</tr>' ;
  626. }
  627. }
  628. if($tableMarkup) $text.= '</table>' ;
  629. return $text ;
  630. }
  631. /**
  632. * Return last or next value for a mask (according to area we should not reset)
  633. *
  634. * @param DoliDB $db Database handler
  635. * @param string $mask Mask to use
  636. * @param string $table Table containing field with counter
  637. * @param string $field Field containing already used values of counter
  638. * @param string $where To add a filter on selection (for exemple to filter on invoice types)
  639. * @param Societe $objsoc The company that own the object we need a counter for
  640. * @param string $date Date to use for the {y},{m},{d} tags.
  641. * @param string $mode 'next' for next value or 'last' for last value
  642. * @param bool $bentityon Activate the entity filter. Default is true (for modules not compatible with multicompany)
  643. * @return string New value (numeric) or error message
  644. */
  645. function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$mode='next', $bentityon=true)
  646. {
  647. global $conf;
  648. if (! is_object($objsoc)) $valueforccc=$objsoc;
  649. else if($table == "commande_fournisseur" || $table == "facture_fourn" ) $valueforccc=$objsoc->code_fournisseur;
  650. else $valueforccc=$objsoc->code_client;
  651. // Clean parameters
  652. if ($date == '') $date=dol_now(); // We use local year and month of PHP server to search numbers
  653. // but we should use local year and month of user
  654. // For debugging
  655. //include_once(DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php');
  656. //$mask='FA{yy}{mm}-{0000@99}';
  657. //$date=dol_mktime(12, 0, 0, 1, 1, 1900);
  658. //$date=dol_stringtotime('20130101');
  659. $hasglobalcounter=false;
  660. // Extract value for mask counter, mask raz and mask offset
  661. if (preg_match('/\{(0+)([@\+][0-9\-\+\=]+)?([@\+][0-9\-\+\=]+)?\}/i',$mask,$reg))
  662. {
  663. $masktri=$reg[1].(! empty($reg[2])?$reg[2]:'').(! empty($reg[3])?$reg[3]:'');
  664. $maskcounter=$reg[1];
  665. $hasglobalcounter=true;
  666. }
  667. else
  668. {
  669. // setting some defaults so the rest of the code won't fail if there is a third party counter
  670. $masktri='00000';
  671. $maskcounter='00000';
  672. }
  673. $maskraz=-1;
  674. $maskoffset=0;
  675. $resetEveryMonth=false;
  676. if (dol_strlen($maskcounter) < 3 && empty($conf->global->MAIN_COUNTER_WITH_LESS_3_DIGITS)) return 'ErrorCounterMustHaveMoreThan3Digits';
  677. // Extract value for third party mask counter
  678. if (preg_match('/\{(c+)(0*)\}/i',$mask,$regClientRef))
  679. {
  680. $maskrefclient=$regClientRef[1].$regClientRef[2];
  681. $maskrefclient_maskclientcode=$regClientRef[1];
  682. $maskrefclient_maskcounter=$regClientRef[2];
  683. $maskrefclient_maskoffset=0; //default value of maskrefclient_counter offset
  684. $maskrefclient_clientcode=substr($valueforccc,0,dol_strlen($maskrefclient_maskclientcode));//get n first characters of client code where n is length in mask
  685. $maskrefclient_clientcode=str_pad($maskrefclient_clientcode,dol_strlen($maskrefclient_maskclientcode),"#",STR_PAD_RIGHT);//padding maskrefclient_clientcode for having exactly n characters in maskrefclient_clientcode
  686. $maskrefclient_clientcode=dol_string_nospecial($maskrefclient_clientcode);//sanitize maskrefclient_clientcode for sql insert and sql select like
  687. if (dol_strlen($maskrefclient_maskcounter) > 0 && dol_strlen($maskrefclient_maskcounter) < 3) return 'ErrorCounterMustHaveMoreThan3Digits';
  688. }
  689. else $maskrefclient='';
  690. // fail if there is neither a global nor a third party counter
  691. if (! $hasglobalcounter && ($maskrefclient_maskcounter == ''))
  692. {
  693. return 'ErrorBadMask';
  694. }
  695. // Extract value for third party type
  696. if (preg_match('/\{(t+)\}/i',$mask,$regType))
  697. {
  698. $masktype=$regType[1];
  699. $masktype_value=substr(preg_replace('/^TE_/','',$objsoc->typent_code),0,dol_strlen($regType[1]));// get n first characters of thirdpaty typent_code (where n is length in mask)
  700. $masktype_value=str_pad($masktype_value,dol_strlen($regType[1]),"#",STR_PAD_RIGHT); // we fill on right with # to have same number of char than into mask
  701. }
  702. else
  703. {
  704. $masktype='';
  705. $masktype_value='';
  706. }
  707. $maskwithonlyymcode=$mask;
  708. $maskwithonlyymcode=preg_replace('/\{(0+)([@\+][0-9\-\+\=]+)?([@\+][0-9\-\+\=]+)?\}/i',$maskcounter,$maskwithonlyymcode);
  709. $maskwithonlyymcode=preg_replace('/\{dd\}/i','dd',$maskwithonlyymcode);
  710. $maskwithonlyymcode=preg_replace('/\{(c+)(0*)\}/i',$maskrefclient,$maskwithonlyymcode);
  711. $maskwithonlyymcode=preg_replace('/\{(t+)\}/i',$masktype_value,$maskwithonlyymcode);
  712. $maskwithnocode=$maskwithonlyymcode;
  713. $maskwithnocode=preg_replace('/\{yyyy\}/i','yyyy',$maskwithnocode);
  714. $maskwithnocode=preg_replace('/\{yy\}/i','yy',$maskwithnocode);
  715. $maskwithnocode=preg_replace('/\{y\}/i','y',$maskwithnocode);
  716. $maskwithnocode=preg_replace('/\{mm\}/i','mm',$maskwithnocode);
  717. // Now maskwithnocode = 0000ddmmyyyyccc for example
  718. // and maskcounter = 0000 for example
  719. //print "maskwithonlyymcode=".$maskwithonlyymcode." maskwithnocode=".$maskwithnocode."\n<br>";
  720. //var_dump($reg);
  721. // If an offset is asked
  722. if (! empty($reg[2]) && preg_match('/^\+/',$reg[2])) $maskoffset=preg_replace('/^\+/','',$reg[2]);
  723. if (! empty($reg[3]) && preg_match('/^\+/',$reg[3])) $maskoffset=preg_replace('/^\+/','',$reg[3]);
  724. // Define $sqlwhere
  725. $sqlwhere='';
  726. $yearoffset=0; // Use year of current $date by default
  727. $yearoffsettype=false; // false: no reset, 0,-,=,+: reset at offset SOCIETE_FISCAL_MONTH_START, x=reset at offset x
  728. // If a restore to zero after a month is asked we check if there is already a value for this year.
  729. if (! empty($reg[2]) && preg_match('/^@/',$reg[2])) $yearoffsettype = preg_replace('/^@/','',$reg[2]);
  730. if (! empty($reg[3]) && preg_match('/^@/',$reg[3])) $yearoffsettype = preg_replace('/^@/','',$reg[3]);
  731. //print "yearoffset=".$yearoffset." yearoffsettype=".$yearoffsettype;
  732. if (is_numeric($yearoffsettype) && $yearoffsettype >= 1)
  733. $maskraz=$yearoffsettype; // For backward compatibility
  734. else if ($yearoffsettype === '0' || (! empty($yearoffsettype) && ! is_numeric($yearoffsettype) && $conf->global->SOCIETE_FISCAL_MONTH_START > 1))
  735. $maskraz = $conf->global->SOCIETE_FISCAL_MONTH_START;
  736. //print "maskraz=".$maskraz; // -1=no reset
  737. if ($maskraz > 0) { // A reset is required
  738. if ($maskraz == 99) {
  739. $maskraz = date('m', $date);
  740. $resetEveryMonth = true;
  741. }
  742. if ($maskraz > 12) return 'ErrorBadMaskBadRazMonth';
  743. // Define posy, posm and reg
  744. if ($maskraz > 1) // if reset is not first month, we need month and year into mask
  745. {
  746. if (preg_match('/^(.*)\{(y+)\}\{(m+)\}/i',$maskwithonlyymcode,$reg)) { $posy=2; $posm=3; }
  747. elseif (preg_match('/^(.*)\{(m+)\}\{(y+)\}/i',$maskwithonlyymcode,$reg)) { $posy=3; $posm=2; }
  748. else return 'ErrorCantUseRazInStartedYearIfNoYearMonthInMask';
  749. if (dol_strlen($reg[$posy]) < 2) return 'ErrorCantUseRazWithYearOnOneDigit';
  750. }
  751. else // if reset is for a specific month in year, we need year
  752. {
  753. if (preg_match('/^(.*)\{(m+)\}\{(y+)\}/i',$maskwithonlyymcode,$reg)) { $posy=3; $posm=2; }
  754. else if (preg_match('/^(.*)\{(y+)\}\{(m+)\}/i',$maskwithonlyymcode,$reg)) { $posy=2; $posm=3; }
  755. else if (preg_match('/^(.*)\{(y+)\}/i',$maskwithonlyymcode,$reg)) { $posy=2; $posm=0; }
  756. else return 'ErrorCantUseRazIfNoYearInMask';
  757. }
  758. // Define length
  759. $yearlen = $posy?dol_strlen($reg[$posy]):0;
  760. $monthlen = $posm?dol_strlen($reg[$posm]):0;
  761. // Define pos
  762. $yearpos = (dol_strlen($reg[1])+1);
  763. $monthpos = ($yearpos+$yearlen);
  764. if ($posy == 3 && $posm == 2) { // if month is before year
  765. $monthpos = (dol_strlen($reg[1])+1);
  766. $yearpos = ($monthpos+$monthlen);
  767. }
  768. //print "xxx ".$maskwithonlyymcode." maskraz=".$maskraz." posy=".$posy." yearlen=".$yearlen." yearpos=".$yearpos." posm=".$posm." monthlen=".$monthlen." monthpos=".$monthpos." yearoffsettype=".$yearoffsettype." resetEveryMonth=".$resetEveryMonth."\n";
  769. // Define $yearcomp and $monthcomp (that will be use in the select where to search max number)
  770. $monthcomp=$maskraz;
  771. $yearcomp=0;
  772. if (! empty($yearoffsettype) && ! is_numeric($yearoffsettype) && $yearoffsettype != '=') // $yearoffsettype is - or +
  773. {
  774. $currentyear=date("Y", $date);
  775. $fiscaldate=dol_mktime('0','0','0',$maskraz,'1',$currentyear);
  776. $newyeardate=dol_mktime('0','0','0','1','1',$currentyear);
  777. $nextnewyeardate=dol_mktime('0','0','0','1','1',$currentyear+1);
  778. //echo 'currentyear='.$currentyear.' date='.dol_print_date($date, 'day').' fiscaldate='.dol_print_date($fiscaldate, 'day').'<br>';
  779. // If after or equal of current fiscal date
  780. if ($date >= $fiscaldate)
  781. {
  782. // If before of next new year date
  783. if ($date < $nextnewyeardate && $yearoffsettype == '+') $yearoffset=1;
  784. }
  785. // If after or equal of current new year date
  786. else if ($date >= $newyeardate && $yearoffsettype == '-') $yearoffset=-1;
  787. }
  788. // For backward compatibility
  789. else if (date("m",$date) < $maskraz && empty($resetEveryMonth)) { $yearoffset=-1; } // If current month lower that month of return to zero, year is previous year
  790. if ($yearlen == 4) $yearcomp=sprintf("%04d",date("Y",$date)+$yearoffset);
  791. elseif ($yearlen == 2) $yearcomp=sprintf("%02d",date("y",$date)+$yearoffset);
  792. elseif ($yearlen == 1) $yearcomp=substr(date("y",$date),2,1)+$yearoffset;
  793. if ($monthcomp > 1 && empty($resetEveryMonth)) // Test with month is useless if monthcomp = 0 or 1 (0 is same as 1) (regis: $monthcomp can't equal 0)
  794. {
  795. if ($yearlen == 4) $yearcomp1=sprintf("%04d",date("Y",$date)+$yearoffset+1);
  796. elseif ($yearlen == 2) $yearcomp1=sprintf("%02d",date("y",$date)+$yearoffset+1);
  797. $sqlwhere.="(";
  798. $sqlwhere.=" (SUBSTRING(".$field.", ".$yearpos.", ".$yearlen.") = '".$yearcomp."'";
  799. $sqlwhere.=" AND SUBSTRING(".$field.", ".$monthpos.", ".$monthlen.") >= '".str_pad($monthcomp, $monthlen, '0', STR_PAD_LEFT)."')";
  800. $sqlwhere.=" OR";
  801. $sqlwhere.=" (SUBSTRING(".$field.", ".$yearpos.", ".$yearlen.") = '".$yearcomp1."'";
  802. $sqlwhere.=" AND SUBSTRING(".$field.", ".$monthpos.", ".$monthlen.") < '".str_pad($monthcomp, $monthlen, '0', STR_PAD_LEFT)."') ";
  803. $sqlwhere.=')';
  804. }
  805. else if ($resetEveryMonth)
  806. {
  807. $sqlwhere.="(SUBSTRING(".$field.", ".$yearpos.", ".$yearlen.") = '".$yearcomp."'";
  808. $sqlwhere.=" AND SUBSTRING(".$field.", ".$monthpos.", ".$monthlen.") = '".str_pad($monthcomp, $monthlen, '0', STR_PAD_LEFT)."')";
  809. }
  810. else // reset is done on january
  811. {
  812. $sqlwhere.='(SUBSTRING('.$field.', '.$yearpos.', '.$yearlen.") = '".$yearcomp."')";
  813. }
  814. }
  815. //print "sqlwhere=".$sqlwhere." yearcomp=".$yearcomp."<br>\n"; // sqlwhere and yearcomp defined only if we ask a reset
  816. //print "masktri=".$masktri." maskcounter=".$maskcounter." maskraz=".$maskraz." maskoffset=".$maskoffset."<br>\n";
  817. // Define $sqlstring
  818. if (function_exists('mb_strrpos'))
  819. {
  820. $posnumstart=mb_strrpos($maskwithnocode,$maskcounter, 'UTF-8');
  821. }
  822. else
  823. {
  824. $posnumstart=strrpos($maskwithnocode,$maskcounter);
  825. } // Pos of counter in final string (from 0 to ...)
  826. if ($posnumstart < 0) return 'ErrorBadMaskFailedToLocatePosOfSequence';
  827. $sqlstring='SUBSTRING('.$field.', '.($posnumstart+1).', '.dol_strlen($maskcounter).')';
  828. // Define $maskLike
  829. $maskLike = dol_string_nospecial($mask);
  830. $maskLike = str_replace("%","_",$maskLike);
  831. // Replace protected special codes with matching number of _ as wild card caracter
  832. $maskLike = preg_replace('/\{yyyy\}/i','____',$maskLike);
  833. $maskLike = preg_replace('/\{yy\}/i','__',$maskLike);
  834. $maskLike = preg_replace('/\{y\}/i','_',$maskLike);
  835. $maskLike = preg_replace('/\{mm\}/i','__',$maskLike);
  836. $maskLike = preg_replace('/\{dd\}/i','__',$maskLike);
  837. $maskLike = str_replace(dol_string_nospecial('{'.$masktri.'}'),str_pad("",dol_strlen($maskcounter),"_"),$maskLike);
  838. if ($maskrefclient) $maskLike = str_replace(dol_string_nospecial('{'.$maskrefclient.'}'),str_pad("",dol_strlen($maskrefclient),"_"),$maskLike);
  839. if ($masktype) $maskLike = str_replace(dol_string_nospecial('{'.$masktype.'}'),$masktype_value,$maskLike);
  840. // Get counter in database
  841. $counter=0;
  842. $sql = "SELECT MAX(".$sqlstring.") as val";
  843. $sql.= " FROM ".MAIN_DB_PREFIX.$table;
  844. $sql.= " WHERE ".$field." LIKE '".$maskLike."'";
  845. $sql.= " AND ".$field." NOT LIKE '(PROV%)'";
  846. if ($bentityon) // only if entity enable
  847. $sql.= " AND entity IN (".getEntity($table, 1).")";
  848. if ($where) $sql.=$where;
  849. if ($sqlwhere) $sql.=' AND '.$sqlwhere;
  850. //print $sql.'<br>';
  851. dol_syslog("functions2::get_next_value mode=".$mode."", LOG_DEBUG);
  852. $resql=$db->query($sql);
  853. if ($resql)
  854. {
  855. $obj = $db->fetch_object($resql);
  856. $counter = $obj->val;
  857. }
  858. else dol_print_error($db);
  859. // Check if we must force counter to maskoffset
  860. if (empty($counter) || preg_match('/[^0-9]/i',$counter)) $counter=$maskoffset;
  861. else if ($counter < $maskoffset && empty($conf->global->MAIN_NUMBERING_OFFSET_ONLY_FOR_FIRST)) $counter=$maskoffset;
  862. if ($mode == 'last') // We found value for counter = last counter value. Now need to get corresponding ref of invoice.
  863. {
  864. $counterpadded=str_pad($counter,dol_strlen($maskcounter),"0",STR_PAD_LEFT);
  865. // Define $maskLike
  866. $maskLike = dol_string_nospecial($mask);
  867. $maskLike = str_replace("%","_",$maskLike);
  868. // Replace protected special codes with matching number of _ as wild card caracter
  869. $maskLike = preg_replace('/\{yyyy\}/i','____',$maskLike);
  870. $maskLike = preg_replace('/\{yy\}/i','__',$maskLike);
  871. $maskLike = preg_replace('/\{y\}/i','_',$maskLike);
  872. $maskLike = preg_replace('/\{mm\}/i','__',$maskLike);
  873. $maskLike = preg_replace('/\{dd\}/i','__',$maskLike);
  874. $maskLike = str_replace(dol_string_nospecial('{'.$masktri.'}'),$counterpadded,$maskLike);
  875. if ($maskrefclient) $maskLike = str_replace(dol_string_nospecial('{'.$maskrefclient.'}'),str_pad("",dol_strlen($maskrefclient),"_"),$maskLike);
  876. if ($masktype) $maskLike = str_replace(dol_string_nospecial('{'.$masktype.'}'),$masktype_value,$maskLike);
  877. $ref='';
  878. $sql = "SELECT ".$field." as ref";
  879. $sql.= " FROM ".MAIN_DB_PREFIX.$table;
  880. $sql.= " WHERE ".$field." LIKE '".$maskLike."'";
  881. $sql.= " AND ".$field." NOT LIKE '%PROV%'";
  882. if ($bentityon) // only if entity enable
  883. $sql.= " AND entity IN (".getEntity($table, 1).")";
  884. if ($where) $sql.=$where;
  885. if ($sqlwhere) $sql.=' AND '.$sqlwhere;
  886. dol_syslog("functions2::get_next_value mode=".$mode."", LOG_DEBUG);
  887. $resql=$db->query($sql);
  888. if ($resql)
  889. {
  890. $obj = $db->fetch_object($resql);
  891. if ($obj) $ref = $obj->ref;
  892. }
  893. else dol_print_error($db);
  894. $numFinal=$ref;
  895. }
  896. else if ($mode == 'next')
  897. {
  898. $counter++;
  899. // If value for $counter has a length higher than $maskcounter chars
  900. if ($counter >= pow(10, dol_strlen($maskcounter)))
  901. {
  902. $counter='ErrorMaxNumberReachForThisMask';
  903. }
  904. if (! empty($maskrefclient_maskcounter))
  905. {
  906. //print "maskrefclient_maskcounter=".$maskrefclient_maskcounter." maskwithnocode=".$maskwithnocode." maskrefclient=".$maskrefclient."\n<br>";
  907. // Define $sqlstring
  908. $maskrefclient_posnumstart=strpos($maskwithnocode,$maskrefclient_maskcounter,strpos($maskwithnocode,$maskrefclient)); // Pos of counter in final string (from 0 to ...)
  909. if ($maskrefclient_posnumstart <= 0) return 'ErrorBadMask';
  910. $maskrefclient_sqlstring='SUBSTRING('.$field.', '.($maskrefclient_posnumstart+1).', '.dol_strlen($maskrefclient_maskcounter).')';
  911. //print "x".$sqlstring;
  912. // Define $maskrefclient_maskLike
  913. $maskrefclient_maskLike = dol_string_nospecial($mask);
  914. $maskrefclient_maskLike = str_replace("%","_",$maskrefclient_maskLike);
  915. // Replace protected special codes with matching number of _ as wild card caracter
  916. $maskrefclient_maskLike = str_replace(dol_string_nospecial('{yyyy}'),'____',$maskrefclient_maskLike);
  917. $maskrefclient_maskLike = str_replace(dol_string_nospecial('{yy}'),'__',$maskrefclient_maskLike);
  918. $maskrefclient_maskLike = str_replace(dol_string_nospecial('{y}'),'_',$maskrefclient_maskLike);
  919. $maskrefclient_maskLike = str_replace(dol_string_nospecial('{mm}'),'__',$maskrefclient_maskLike);
  920. $maskrefclient_maskLike = str_replace(dol_string_nospecial('{dd}'),'__',$maskrefclient_maskLike);
  921. $maskrefclient_maskLike = str_replace(dol_string_nospecial('{'.$masktri.'}'),str_pad("",dol_strlen($maskcounter),"_"),$maskrefclient_maskLike);
  922. $maskrefclient_maskLike = str_replace(dol_string_nospecial('{'.$maskrefclient.'}'),$maskrefclient_clientcode.str_pad("",dol_strlen($maskrefclient_maskcounter),"_"),$maskrefclient_maskLike);
  923. // Get counter in database
  924. $maskrefclient_counter=0;
  925. $maskrefclient_sql = "SELECT MAX(".$maskrefclient_sqlstring.") as val";
  926. $maskrefclient_sql.= " FROM ".MAIN_DB_PREFIX.$table;
  927. //$sql.= " WHERE ".$field." not like '(%'";
  928. $maskrefclient_sql.= " WHERE ".$field." LIKE '".$maskrefclient_maskLike."'";
  929. if ($bentityon) // only if entity enable
  930. $maskrefclient_sql.= " AND entity IN (".getEntity($table, 1).")";
  931. if ($where) $maskrefclient_sql.=$where; //use the same optional where as general mask
  932. if ($sqlwhere) $maskrefclient_sql.=' AND '.$sqlwhere; //use the same sqlwhere as general mask
  933. $maskrefclient_sql.=' AND (SUBSTRING('.$field.', '.(strpos($maskwithnocode,$maskrefclient)+1).', '.dol_strlen($maskrefclient_maskclientcode).")='".$maskrefclient_clientcode."')";
  934. dol_syslog("functions2::get_next_value maskrefclient", LOG_DEBUG);
  935. $maskrefclient_resql=$db->query($maskrefclient_sql);
  936. if ($maskrefclient_resql)
  937. {
  938. $maskrefclient_obj = $db->fetch_object($maskrefclient_resql);
  939. $maskrefclient_counter = $maskrefclient_obj->val;
  940. }
  941. else dol_print_error($db);
  942. if (empty($maskrefclient_counter) || preg_match('/[^0-9]/i',$maskrefclient_counter)) $maskrefclient_counter=$maskrefclient_maskoffset;
  943. $maskrefclient_counter++;
  944. }
  945. // Build numFinal
  946. $numFinal = $mask;
  947. // We replace special codes except refclient
  948. if (! empty($yearoffsettype) && ! is_numeric($yearoffsettype) && $yearoffsettype != '=') // yearoffsettype is - or +, so we don't want current year
  949. {
  950. $numFinal = preg_replace('/\{yyyy\}/i',date("Y",$date)+$yearoffset, $numFinal);
  951. $numFinal = preg_replace('/\{yy\}/i', date("y",$date)+$yearoffset, $numFinal);
  952. $numFinal = preg_replace('/\{y\}/i', substr(date("y",$date),2,1)+$yearoffset, $numFinal);
  953. }
  954. else // we want yyyy to be current year
  955. {
  956. $numFinal = preg_replace('/\{yyyy\}/i',date("Y",$date), $numFinal);
  957. $numFinal = preg_replace('/\{yy\}/i', date("y",$date), $numFinal);
  958. $numFinal = preg_replace('/\{y\}/i', substr(date("y",$date),2,1), $numFinal);
  959. }
  960. $numFinal = preg_replace('/\{mm\}/i', date("m",$date), $numFinal);
  961. $numFinal = preg_replace('/\{dd\}/i', date("d",$date), $numFinal);
  962. // Now we replace the counter
  963. $maskbefore='{'.$masktri.'}';
  964. $maskafter=str_pad($counter,dol_strlen($maskcounter),"0",STR_PAD_LEFT);
  965. //print 'x'.$maskbefore.'-'.$maskafter.'y';
  966. $numFinal = str_replace($maskbefore,$maskafter,$numFinal);
  967. // Now we replace the refclient
  968. if ($maskrefclient)
  969. {
  970. //print "maskrefclient=".$maskrefclient." maskwithonlyymcode=".$maskwithonlyymcode." maskwithnocode=".$maskwithnocode."\n<br>";
  971. $maskrefclient_maskbefore='{'.$maskrefclient.'}';
  972. $maskrefclient_maskafter=$maskrefclient_clientcode.str_pad($maskrefclient_counter,dol_strlen($maskrefclient_maskcounter),"0",STR_PAD_LEFT);
  973. $numFinal = str_replace($maskrefclient_maskbefore,$maskrefclient_maskafter,$numFinal);
  974. }
  975. // Now we replace the type
  976. if ($masktype)
  977. {
  978. $masktype_maskbefore='{'.$masktype.'}';
  979. $masktype_maskafter=$masktype_value;
  980. $numFinal = str_replace($masktype_maskbefore,$masktype_maskafter,$numFinal);
  981. }
  982. }
  983. dol_syslog("functions2::get_next_value return ".$numFinal,LOG_DEBUG);
  984. return $numFinal;
  985. }
  986. /**
  987. * Check value
  988. *
  989. * @param string $mask Mask to use
  990. * @param string $value Value
  991. * @return int <0 if KO, 0 if OK
  992. */
  993. function check_value($mask,$value)
  994. {
  995. $result=0;
  996. $hasglobalcounter=false;
  997. // Extract value for mask counter, mask raz and mask offset
  998. if (preg_match('/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i',$mask,$reg))
  999. {
  1000. $masktri=$reg[1].(isset($reg[2])?$reg[2]:'').(isset($reg[3])?$reg[3]:'');
  1001. $maskcounter=$reg[1];
  1002. $hasglobalcounter=true;
  1003. }
  1004. else
  1005. {
  1006. // setting some defaults so the rest of the code won't fail if there is a third party counter
  1007. $masktri='00000';
  1008. $maskcounter='00000';
  1009. }
  1010. $maskraz=-1;
  1011. $maskoffset=0;
  1012. if (dol_strlen($maskcounter) < 3) return 'ErrorCounterMustHaveMoreThan3Digits';
  1013. // Extract value for third party mask counter
  1014. if (preg_match('/\{(c+)(0*)\}/i',$mask,$regClientRef))
  1015. {
  1016. $maskrefclient=$regClientRef[1].$regClientRef[2];
  1017. $maskrefclient_maskclientcode=$regClientRef[1];
  1018. $maskrefclient_maskcounter=$regClientRef[2];
  1019. $maskrefclient_maskoffset=0; //default value of maskrefclient_counter offset
  1020. $maskrefclient_clientcode=substr('',0,dol_strlen($maskrefclient_maskclientcode));//get n first characters of client code to form maskrefclient_clientcode
  1021. $maskrefclient_clientcode=str_pad($maskrefclient_clientcode,dol_strlen($maskrefclient_maskclientcode),"#",STR_PAD_RIGHT);//padding maskrefclient_clientcode for having exactly n characters in maskrefclient_clientcode
  1022. $maskrefclient_clientcode=dol_string_nospecial($maskrefclient_clientcode);//sanitize maskrefclient_clientcode for sql insert and sql select like
  1023. if (dol_strlen($maskrefclient_maskcounter) > 0 && dol_strlen($maskrefclient_maskcounter) < 3) return 'ErrorCounterMustHaveMoreThan3Digits';
  1024. }
  1025. else $maskrefclient='';
  1026. // fail if there is neither a global nor a third party counter
  1027. if (! $hasglobalcounter && ($maskrefclient_maskcounter == ''))
  1028. {
  1029. return 'ErrorBadMask';
  1030. }
  1031. $maskwithonlyymcode=$mask;
  1032. $maskwithonlyymcode=preg_replace('/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i',$maskcounter,$maskwithonlyymcode);
  1033. $maskwithonlyymcode=preg_replace('/\{dd\}/i','dd',$maskwithonlyymcode);
  1034. $maskwithonlyymcode=preg_replace('/\{(c+)(0*)\}/i',$maskrefclient,$maskwithonlyymcode);
  1035. $maskwithnocode=$maskwithonlyymcode;
  1036. $maskwithnocode=preg_replace('/\{yyyy\}/i','yyyy',$maskwithnocode);
  1037. $maskwithnocode=preg_replace('/\{yy\}/i','yy',$maskwithnocode);
  1038. $maskwithnocode=preg_replace('/\{y\}/i','y',$maskwithnocode);
  1039. $maskwithnocode=preg_replace('/\{mm\}/i','mm',$maskwithnocode);
  1040. // Now maskwithnocode = 0000ddmmyyyyccc for example
  1041. // and maskcounter = 0000 for example
  1042. //print "maskwithonlyymcode=".$maskwithonlyymcode." maskwithnocode=".$maskwithnocode."\n<br>";
  1043. // If an offset is asked
  1044. if (! empty($reg[2]) && preg_match('/^\+/',$reg[2])) $maskoffset=preg_replace('/^\+/','',$reg[2]);
  1045. if (! empty($reg[3]) && preg_match('^\+',$reg[3])) $maskoffset=preg_replace('/^\+/','',$reg[3]);
  1046. // Define $sqlwhere
  1047. // If a restore to zero after a month is asked we check if there is already a value for this year.
  1048. if (! empty($reg[2]) && preg_match('/^@/',$reg[2])) $maskraz=preg_replace('/^@/','',$reg[2]);
  1049. if (! empty($reg[3]) && preg_match('/^@/',$reg[3])) $maskraz=preg_replace('/^@/','',$reg[3]);
  1050. if ($maskraz >= 0)
  1051. {
  1052. if ($maskraz > 12) return 'ErrorBadMaskBadRazMonth';
  1053. // Define reg
  1054. if ($maskraz > 1 && ! preg_match('/^(.*)\{(y+)\}\{(m+)\}/i',$maskwithonlyymcode,$reg)) return 'ErrorCantUseRazInStartedYearIfNoYearMonthInMask';
  1055. if ($maskraz <= 1 && ! preg_match('/^(.*)\{(y+)\}/i',$maskwithonlyymcode,$reg)) return 'ErrorCantUseRazIfNoYearInMask';
  1056. //print "x".$maskwithonlyymcode." ".$maskraz;
  1057. }
  1058. //print "masktri=".$masktri." maskcounter=".$maskcounter." maskraz=".$maskraz." maskoffset=".$maskoffset."<br>\n";
  1059. // Check we have a number in ($posnumstart+1).', '.dol_strlen($maskcounter)
  1060. //
  1061. // Check length
  1062. $len=dol_strlen($maskwithnocode);
  1063. if (dol_strlen($value) != $len) $result=-1;
  1064. // Define $maskLike
  1065. $maskLike = dol_string_nospecial($mask);
  1066. $maskLike = str_replace("%","_",$maskLike);
  1067. // Replace protected special codes with matching number of _ as wild card caracter
  1068. $maskLike = str_replace(dol_string_nospecial('{yyyy}'),'____',$maskLike);
  1069. $maskLike = str_replace(dol_string_nospecial('{yy}'),'__',$maskLike);
  1070. $maskLike = str_replace(dol_string_nospecial('{y}'),'_',$maskLike);
  1071. $maskLike = str_replace(dol_string_nospecial('{mm}'),'__',$maskLike);
  1072. $maskLike = str_replace(dol_string_nospecial('{dd}'),'__',$maskLike);
  1073. $maskLike = str_replace(dol_string_nospecial('{'.$masktri.'}'),str_pad("",dol_strlen($maskcounter),"_"),$maskLike);
  1074. if ($maskrefclient) $maskLike = str_replace(dol_string_nospecial('{'.$maskrefclient.'}'),str_pad("",strlen($maskrefclient),"_"),$maskLike);
  1075. dol_syslog("functions2::check_value result=".$result,LOG_DEBUG);
  1076. return $result;
  1077. }
  1078. /**
  1079. * Convert a binary data to string that represent hexadecimal value
  1080. *
  1081. * @param string $bin Value to convert
  1082. * @param boolean $pad Add 0
  1083. * @param boolean $upper Convert to tupper
  1084. * @return string x
  1085. */
  1086. function binhex($bin, $pad=false, $upper=false)
  1087. {
  1088. $last = dol_strlen($bin)-1;
  1089. for($i=0; $i<=$last; $i++){ $x += $bin[$last-$i] * pow(2,$i); }
  1090. $x = dechex($x);
  1091. if($pad){ while(dol_strlen($x) < intval(dol_strlen($bin))/4){ $x = "0$x"; } }
  1092. if($upper){ $x = strtoupper($x); }
  1093. return $x;
  1094. }
  1095. /**
  1096. * Convert an hexadecimal string into a binary string
  1097. *
  1098. * @param string $hexa Hexadecimal string to convert (example: 'FF')
  1099. * @return string bin
  1100. */
  1101. function hexbin($hexa)
  1102. {
  1103. $bin='';
  1104. $strLength = dol_strlen($hexa);
  1105. for($i=0;$i<$strLength;$i++)
  1106. {
  1107. $bin.=str_pad(decbin(hexdec($hexa{$i})),4,'0',STR_PAD_LEFT);
  1108. }
  1109. return $bin;
  1110. }
  1111. /**
  1112. * Retourne le numero de la semaine par rapport a une date
  1113. *
  1114. * @param string $time Date au format 'timestamp'
  1115. * @return string Number of week
  1116. */
  1117. function numero_semaine($time)
  1118. {
  1119. $stime = strftime('%Y-%m-%d',$time);
  1120. if (preg_match('/^([0-9]+)\-([0-9]+)\-([0-9]+)\s?([0-9]+)?:?([0-9]+)?/i',$stime,$reg))
  1121. {
  1122. // Date est au format 'YYYY-MM-DD' ou 'YYYY-MM-DD HH:MM:SS'
  1123. $annee = $reg[1];
  1124. $mois = $reg[2];
  1125. $jour = $reg[3];
  1126. }
  1127. /*
  1128. * Norme ISO-8601:
  1129. * - La semaine 1 de toute annee est celle qui contient le 4 janvier ou que la semaine 1 de toute annee est celle qui contient le 1er jeudi de janvier.
  1130. * - La majorite des annees ont 52 semaines mais les annees qui commence un jeudi et les annees bissextiles commencant un mercredi en possede 53.
  1131. * - Le 1er jour de la semaine est le Lundi
  1132. */
  1133. // Definition du Jeudi de la semaine
  1134. if (date("w",mktime(12,0,0,$mois,$jour,$annee))==0) // Dimanche
  1135. $jeudiSemaine = mktime(12,0,0,$mois,$jour,$annee)-3*24*60*60;
  1136. else if (date("w",mktime(12,0,0,$mois,$jour,$annee))<4) // du Lundi au Mercredi
  1137. $jeudiSemaine = mktime(12,0,0,$mois,$jour,$annee)+(4-date("w",mktime(12,0,0,$mois,$jour,$annee)))*24*60*60;
  1138. else if (date("w",mktime(12,0,0,$mois,$jour,$annee))>4) // du Vendredi au Samedi
  1139. $jeudiSemaine = mktime(12,0,0,$mois,$jour,$annee)-(date("w",mktime(12,0,0,$mois,$jour,$annee))-4)*24*60*60;
  1140. else // Jeudi
  1141. $jeudiSemaine = mktime(12,0,0,$mois,$jour,$annee);
  1142. // Definition du premier Jeudi de l'annee
  1143. if (date("w",mktime(12,0,0,1,1,date("Y",$jeudiSemaine)))==0) // Dimanche
  1144. {
  1145. $premierJeudiAnnee = mktime(12,0,0,1,1,date("Y",$jeudiSemaine))+4*24*60*60;
  1146. }
  1147. else if (date("w",mktime(12,0,0,1,1,date("Y",$jeudiSemaine)))<4) // du Lundi au Mercredi
  1148. {
  1149. $premierJeudiAnnee = mktime(12,0,0,1,1,date("Y",$jeudiSemaine))+(4-date("w",mktime(12,0,0,1,1,date("Y",$jeudiSemaine))))*24*60*60;
  1150. }
  1151. else if (date("w",mktime(12,0,0,1,1,date("Y",$jeudiSemaine)))>4) // du Vendredi au Samedi
  1152. {
  1153. $premierJeudiAnnee = mktime(12,0,0,1,1,date("Y",$jeudiSemaine))+(7-(date("w",mktime(12,0,0,1,1,date("Y",$jeudiSemaine)))-4))*24*60*60;
  1154. }
  1155. else // Jeudi
  1156. {
  1157. $premierJeudiAnnee = mktime(12,0,0,1,1,date("Y",$jeudiSemaine));
  1158. }
  1159. // Definition du numero de semaine: nb de jours entre "premier Jeudi de l'annee" et "Jeudi de la semaine";
  1160. $numeroSemaine = (
  1161. (
  1162. date("z",mktime(12,0,0,date("m",$jeudiSemaine),date("d",$jeudiSemaine),date("Y",$jeudiSemaine)))
  1163. -
  1164. date("z",mktime(12,0,0,date("m",$premierJeudiAnnee),date("d",$premierJeudiAnnee),date("Y",$premierJeudiAnnee)))
  1165. ) / 7
  1166. ) + 1;
  1167. // Cas particulier de la semaine 53
  1168. if ($numeroSemaine==53)
  1169. {
  1170. // Les annees qui commence un Jeudi et les annees bissextiles commencant un Mercredi en possede 53
  1171. if (date("w",mktime(12,0,0,1,1,date("Y",$jeudiSemaine)))==4 || (date("w",mktime(12,0,0,1,1,date("Y",$jeudiSemaine)))==3 && date("z",mktime(12,0,0,12,31,date("Y",$jeudiSemaine)))==365))
  1172. {
  1173. $numeroSemaine = 53;
  1174. }
  1175. else
  1176. {
  1177. $numeroSemaine = 1;
  1178. }
  1179. }
  1180. //echo $jour."-".$mois."-".$annee." (".date("d-m-Y",$premierJeudiAnnee)." - ".date("d-m-Y",$jeudiSemaine).") -> ".$numeroSemaine."<BR>";
  1181. return sprintf("%02d",$numeroSemaine);
  1182. }
  1183. /**
  1184. * Convertit une masse d'une unite vers une autre unite
  1185. *
  1186. * @param float $weight Masse a convertir
  1187. * @param int $from_unit Unite originale en puissance de 10
  1188. * @param int $to_unit Nouvelle unite en puissance de 10
  1189. * @return float Masse convertie
  1190. */
  1191. function weight_convert($weight,&$from_unit,$to_unit)
  1192. {
  1193. /* Pour convertire 320 gr en Kg appeler
  1194. * $f = -3
  1195. * weigh_convert(320, $f, 0) retournera 0.32
  1196. *
  1197. */
  1198. while ($from_unit <> $to_unit)
  1199. {
  1200. if ($from_unit > $to_unit)
  1201. {
  1202. $weight = $weight * 10;
  1203. $from_unit = $from_unit - 1;
  1204. $weight = weight_convert($weight,$from_unit, $to_unit);
  1205. }
  1206. if ($from_unit < $to_unit)
  1207. {
  1208. $weight = $weight / 10;
  1209. $from_unit = $from_unit + 1;
  1210. $weight = weight_convert($weight,$from_unit, $to_unit);
  1211. }
  1212. }
  1213. return $weight;
  1214. }
  1215. /**
  1216. * Save personnal parameter
  1217. *
  1218. * @param DoliDB $db Handler database
  1219. * @param Conf $conf Object conf
  1220. * @param User $user Object user
  1221. * @param array $tab Array (key=>value) with all parameters to save
  1222. * @return int <0 if KO, >0 if OK
  1223. *
  1224. * @see dolibarr_get_const, dolibarr_set_const, dolibarr_del_const
  1225. */
  1226. function dol_set_user_param($db, $conf, &$user, $tab)
  1227. {
  1228. // Verification parametres
  1229. if (count($tab) < 1) return -1;
  1230. $db->begin();
  1231. // We remove old parameters for all keys in $tab
  1232. $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_param";
  1233. $sql.= " WHERE fk_user = ".$user->id;
  1234. $sql.= " AND entity = ".$conf->entity;
  1235. $sql.= " AND param in (";
  1236. $i=0;
  1237. foreach ($tab as $key => $value)
  1238. {
  1239. if ($i > 0) $sql.=',';
  1240. $sql.="'".$key."'";
  1241. $i++;
  1242. }
  1243. $sql.= ")";
  1244. dol_syslog("functions2.lib::dol_set_user_param", LOG_DEBUG);
  1245. $resql=$db->query($sql);
  1246. if (! $resql)
  1247. {
  1248. dol_print_error($db);
  1249. $db->rollback();
  1250. return -1;
  1251. }
  1252. foreach ($tab as $key => $value)
  1253. {
  1254. // Set new parameters
  1255. if ($value)
  1256. {
  1257. $sql = "INSERT INTO ".MAIN_DB_PREFIX."user_param(fk_user,entity,param,value)";
  1258. $sql.= " VALUES (".$user->id.",".$conf->entity.",";
  1259. $sql.= " '".$key."','".$db->escape($value)."')";
  1260. dol_syslog("functions2.lib::dol_set_user_param", LOG_DEBUG);
  1261. $result=$db->query($sql);
  1262. if (! $result)
  1263. {
  1264. dol_print_error($db);
  1265. $db->rollback();
  1266. return -1;
  1267. }
  1268. $user->conf->$key = $value;
  1269. //print "key=".$key." user->conf->key=".$user->conf->$key;
  1270. }
  1271. else
  1272. {
  1273. unset($user->conf->$key);
  1274. }
  1275. }
  1276. $db->commit();
  1277. return 1;
  1278. }
  1279. /**
  1280. * Returns formated reduction
  1281. *
  1282. * @param int $reduction Reduction percentage
  1283. * @param Translate $langs Output language
  1284. * @return string Formated reduction
  1285. */
  1286. function dol_print_reduction($reduction,$langs)
  1287. {
  1288. $string = '';
  1289. if ($reduction == 100)
  1290. {
  1291. $string = $langs->transnoentities("Offered");
  1292. }
  1293. else
  1294. {
  1295. $string = $reduction.'%';
  1296. }
  1297. return $string;
  1298. }
  1299. /**
  1300. * Return OS version.
  1301. * Note that PHP_OS returns only OS (not version) and OS PHP was built on, not necessarly OS PHP runs on.
  1302. *
  1303. * @return string OS version
  1304. */
  1305. function version_os()
  1306. {
  1307. $osversion=php_uname();
  1308. return $osversion;
  1309. }
  1310. /**
  1311. * Return PHP version
  1312. *
  1313. * @return string PHP version
  1314. * @see versionphparray
  1315. */
  1316. function version_php()
  1317. {
  1318. return phpversion();
  1319. }
  1320. /**
  1321. * Return Dolibarr version
  1322. *
  1323. * @return string Dolibarr version
  1324. * @see versiondolibarrarray
  1325. */
  1326. function version_dolibarr()
  1327. {
  1328. return DOL_VERSION;
  1329. }
  1330. /**
  1331. * Return web server version
  1332. *
  1333. * @return string Web server version
  1334. */
  1335. function version_webserver()
  1336. {
  1337. return $_SERVER["SERVER_SOFTWARE"];
  1338. }
  1339. /**
  1340. * Return list of activated modules usable for document generation
  1341. *
  1342. * @param DoliDB $db Database handler
  1343. * @param string $type Type of models (company, invoice, ...)
  1344. * @param int $maxfilenamelength Max length of value to show
  1345. * @return mixed 0 if no module is activated, or array(key=>label). For modules that need directory scan, key is completed with ":filename".
  1346. */
  1347. function getListOfModels($db,$type,$maxfilenamelength=0)
  1348. {
  1349. global $conf,$langs;
  1350. $liste=array();
  1351. $found=0;
  1352. $dirtoscan='';
  1353. $sql = "SELECT nom as id, nom as lib, libelle as label, description as description";
  1354. $sql.= " FROM ".MAIN_DB_PREFIX."document_model";
  1355. $sql.= " WHERE type = '".$type."'";
  1356. $sql.= " AND entity IN (0,".(! empty($conf->multicompany->enabled) && ! empty($conf->multicompany->transverse_mode)?"1,":"").$conf->entity.")";
  1357. $sql.= " ORDER BY description DESC";
  1358. dol_syslog('/core/lib/function2.lib.php::getListOfModels', LOG_DEBUG);
  1359. $resql = $db->query($sql);
  1360. if ($resql)
  1361. {
  1362. $num = $db->num_rows($resql);
  1363. $i = 0;
  1364. while ($i < $num)
  1365. {
  1366. $found=1;
  1367. $obj = $db->fetch_object($resql);
  1368. // If this generation module needs to scan a directory, then description field is filled
  1369. // with the constant that contains list of directories to scan (COMPANY_ADDON_PDF_ODT_PATH, ...).
  1370. if (! empty($obj->description)) // List of directories to scan is defined
  1371. {
  1372. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1373. $const=$obj->description;
  1374. $dirtoscan.=($dirtoscan?',':'').preg_replace('/[\r\n]+/',',',trim($conf->global->$const));
  1375. $listoffiles=array();
  1376. // Now we add models found in directories scanned
  1377. $listofdir=explode(',',$dirtoscan);
  1378. foreach($listofdir as $key=>$tmpdir)
  1379. {
  1380. $tmpdir=trim($tmpdir);
  1381. $tmpdir=preg_replace('/DOL_DATA_ROOT/',DOL_DATA_ROOT,$tmpdir);
  1382. if (! $tmpdir) { unset($listofdir[$key]); continue; }
  1383. if (is_dir($tmpdir))
  1384. {
  1385. $tmpfiles=dol_dir_list($tmpdir,'files',0,'\.od(s|t)$','','name',SORT_ASC,0);
  1386. if (count($tmpfiles)) $listoffiles=array_merge($listoffiles,$tmpfiles);
  1387. }
  1388. }
  1389. if (count($listoffiles))
  1390. {
  1391. foreach($listoffiles as $record)
  1392. {
  1393. $max=($maxfilenamelength?$maxfilenamelength:28);
  1394. $liste[$obj->id.':'.$record['fullname']]=dol_trunc($record['name'],$max,'middle');
  1395. }
  1396. }
  1397. else
  1398. {
  1399. $liste[0]=$obj->label.': '.$langs->trans("None");
  1400. }
  1401. }
  1402. else
  1403. {
  1404. $liste[$obj->id]=$obj->label?$obj->label:$obj->lib;
  1405. }
  1406. $i++;
  1407. }
  1408. }
  1409. else
  1410. {
  1411. dol_print_error($db);
  1412. return -1;
  1413. }
  1414. if ($found) return $liste;
  1415. else return 0;
  1416. }
  1417. /**
  1418. * This function evaluates a string that should be a valid IPv4
  1419. *
  1420. * @param string $ip IP Address
  1421. * @return int 0 if not valid or reserved range, 1 if valid and public IP, 2 if valid and private range IP
  1422. */
  1423. function is_ip($ip)
  1424. {
  1425. // First we test if it is a valid IPv4
  1426. if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  1427. // Then we test if it is a private range
  1428. if (! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) return 2;
  1429. // Then we test if it is a reserved range
  1430. if (! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE)) return 0;
  1431. return 1;
  1432. }
  1433. return 0;
  1434. }
  1435. /**
  1436. * Build a login from lastname, firstname
  1437. *
  1438. * @param string $lastname Lastname
  1439. * @param string $firstname Firstname
  1440. * @return string Login
  1441. */
  1442. function dol_buildlogin($lastname,$firstname)
  1443. {
  1444. $login=strtolower(dol_string_unaccent($firstname));
  1445. $login.=($login?'.':'');
  1446. $login.=strtolower(dol_string_unaccent($lastname));
  1447. $login=dol_string_nospecial($login,''); // For special names
  1448. return $login;
  1449. }
  1450. /**
  1451. * Return array to use for SoapClient constructor
  1452. *
  1453. * @return param
  1454. */
  1455. function getSoapParams()
  1456. {
  1457. global $conf;
  1458. $params=array();
  1459. $proxyuse =(empty($conf->global->MAIN_PROXY_USE)?false:true);
  1460. $proxyhost=(empty($conf->global->MAIN_PROXY_USE)?false:$conf->global->MAIN_PROXY_HOST);
  1461. $proxyport=(empty($conf->global->MAIN_PROXY_USE)?false:$conf->global->MAIN_PROXY_PORT);
  1462. $proxyuser=(empty($conf->global->MAIN_PROXY_USE)?false:$conf->global->MAIN_PROXY_USER);
  1463. $proxypass=(empty($conf->global->MAIN_PROXY_USE)?false:$conf->global->MAIN_PROXY_PASS);
  1464. $timeout =(empty($conf->global->MAIN_USE_CONNECT_TIMEOUT)?10:$conf->global->MAIN_USE_CONNECT_TIMEOUT); // Connection timeout
  1465. $response_timeout=(empty($conf->global->MAIN_USE_RESPONSE_TIMEOUT)?30:$conf->global->MAIN_USE_RESPONSE_TIMEOUT); // Response timeout
  1466. //print extension_loaded('soap');
  1467. if ($proxyuse)
  1468. {
  1469. $params=array('connection_timeout'=>$timeout,
  1470. 'response_timeout'=>$response_timeout,
  1471. 'proxy_use' => 1,
  1472. 'proxy_host' => $proxyhost,
  1473. 'proxy_port' => $proxyport,
  1474. 'proxy_login' => $proxyuser,
  1475. 'proxy_password' => $proxypass,
  1476. 'trace' => 1
  1477. );
  1478. }
  1479. else
  1480. {
  1481. $params=array('connection_timeout'=>$timeout,
  1482. 'response_timeout'=>$response_timeout,
  1483. 'proxy_use' => 0,
  1484. 'proxy_host' => false,
  1485. 'proxy_port' => false,
  1486. 'proxy_login' => false,
  1487. 'proxy_password' => false,
  1488. 'trace' => 1
  1489. );
  1490. }
  1491. return $params;
  1492. }
  1493. /**
  1494. * List urls of element
  1495. *
  1496. * @param int $objectid Id of record
  1497. * @param string $objecttype Type of object ('invoice', 'order', 'expedition_bon', ...)
  1498. * @param int $withpicto Picto to show
  1499. * @param string $option More options
  1500. * @return string URL of link to object id/type
  1501. */
  1502. function dolGetElementUrl($objectid,$objecttype,$withpicto=0,$option='')
  1503. {
  1504. global $db,$conf;
  1505. $ret='';
  1506. // Parse element/subelement (ex: project_task)
  1507. $module = $element = $subelement = $objecttype;
  1508. if (preg_match('/^([^_]+)_([^_]+)/i',$objecttype,$regs))
  1509. {
  1510. $module = $element = $regs[1];
  1511. $subelement = $regs[2];
  1512. }
  1513. $classpath = $element.'/class';
  1514. // To work with non standard path
  1515. if ($objecttype == 'facture' || $objecttype == 'invoice') {
  1516. $classpath = 'compta/facture/class';
  1517. $module='facture';
  1518. $subelement='facture';
  1519. }
  1520. if ($objecttype == 'commande' || $objecttype == 'order') {
  1521. $classpath = 'commande/class';
  1522. $module='commande';
  1523. $subelement='commande';
  1524. }
  1525. if ($objecttype == 'propal') {
  1526. $classpath = 'comm/propal/class';
  1527. }
  1528. if ($objecttype == 'supplier_proposal') {
  1529. $classpath = 'supplier_proposal/class';
  1530. }
  1531. if ($objecttype == 'shipping') {
  1532. $classpath = 'expedition/class';
  1533. $subelement = 'expedition';
  1534. $module = 'expedition_bon';
  1535. }
  1536. if ($objecttype == 'delivery') {
  1537. $classpath = 'livraison/class';
  1538. $subelement = 'livraison';
  1539. $module = 'livraison_bon';
  1540. }
  1541. if ($objecttype == 'contract') {
  1542. $classpath = 'contrat/class';
  1543. $module='contrat';
  1544. $subelement='contrat';
  1545. }
  1546. if ($objecttype == 'member') {
  1547. $classpath = 'adherents/class';
  1548. $module='adherent';
  1549. $subelement='adherent';
  1550. }
  1551. if ($objecttype == 'cabinetmed_cons') {
  1552. $classpath = 'cabinetmed/class';
  1553. $module='cabinetmed';
  1554. $subelement='cabinetmedcons';
  1555. }
  1556. if ($objecttype == 'fichinter') {
  1557. $classpath = 'fichinter/class';
  1558. $module='ficheinter';
  1559. $subelement='fichinter';
  1560. }
  1561. //print "objecttype=".$objecttype." module=".$module." subelement=".$subelement;
  1562. $classfile = strtolower($subelement); $classname = ucfirst($subelement);
  1563. if ($objecttype == 'invoice_supplier') {
  1564. $classfile = 'fournisseur.facture';
  1565. $classname='FactureFournisseur';
  1566. $classpath = 'fourn/class';
  1567. $module='fournisseur';
  1568. }
  1569. if ($objecttype == 'order_supplier') {
  1570. $classfile = 'fournisseur.commande';
  1571. $classname='CommandeFournisseur';
  1572. $classpath = 'fourn/class';
  1573. $module='fournisseur';
  1574. }
  1575. if (! empty($conf->$module->enabled))
  1576. {
  1577. $res=dol_include_once('/'.$classpath.'/'.$classfile.'.class.php');
  1578. if ($res)
  1579. {
  1580. $object = new $classname($db);
  1581. $res=$object->fetch($objectid);
  1582. if ($res > 0) $ret=$object->getNomUrl($withpicto,$option);
  1583. unset($object);
  1584. }
  1585. }
  1586. return $ret;
  1587. }
  1588. /**
  1589. * Clean corrupted tree (orphelins linked to a not existing parent), record linked to themself and child-parent loop
  1590. *
  1591. * @param DoliDB $db Database handler
  1592. * @param string $tabletocleantree Table to clean
  1593. * @param string $fieldfkparent Field name that contains id of parent
  1594. * @return int Nb of records fixed/deleted
  1595. */
  1596. function cleanCorruptedTree($db, $tabletocleantree, $fieldfkparent)
  1597. {
  1598. $totalnb=0;
  1599. $listofid=array();
  1600. $listofparentid=array();
  1601. // Get list of all id in array listofid and all parents in array listofparentid
  1602. $sql='SELECT rowid, '.$fieldfkparent.' as parent_id FROM '.MAIN_DB_PREFIX.$tabletocleantree;
  1603. $resql = $db->query($sql);
  1604. if ($resql)
  1605. {
  1606. $num = $db->num_rows($resql);
  1607. $i = 0;
  1608. while ($i < $num)
  1609. {
  1610. $obj = $db->fetch_object($resql);
  1611. $listofid[]=$obj->rowid;
  1612. if ($obj->parent_id > 0) $listofparentid[$obj->rowid]=$obj->parent_id;
  1613. $i++;
  1614. }
  1615. }
  1616. else
  1617. {
  1618. dol_print_error($db);
  1619. }
  1620. if (count($listofid))
  1621. {
  1622. print 'Code requested to clean tree (may be to solve data corruption), so we check/clean orphelins and loops.'."<br>\n";
  1623. // Check loops on each other
  1624. $sql = "UPDATE ".MAIN_DB_PREFIX.$tabletocleantree." SET ".$fieldfkparent." = 0 WHERE ".$fieldfkparent." = rowid"; // So we update only records linked to themself
  1625. $resql = $db->query($sql);
  1626. if ($resql)
  1627. {
  1628. $nb=$db->affected_rows($sql);
  1629. if ($nb > 0)
  1630. {
  1631. print '<br>Some record that were parent of themself were cleaned.';
  1632. }
  1633. $totalnb+=$nb;
  1634. }
  1635. //else dol_print_error($db);
  1636. // Check other loops
  1637. $listofidtoclean=array();
  1638. foreach($listofparentid as $id => $pid)
  1639. {
  1640. // Check depth
  1641. //print 'Analyse record id='.$id.' with parent '.$pid.'<br>';
  1642. $cursor=$id; $arrayidparsed=array(); // We start from child $id
  1643. while ($cursor > 0)
  1644. {
  1645. $arrayidparsed[$cursor]=1;
  1646. if ($arrayidparsed[$listofparentid[$cursor]]) // We detect a loop. A record with a parent that was already into child
  1647. {
  1648. print 'Found a loop between id '.$id.' - '.$cursor.'<br>';
  1649. unset($arrayidparsed);
  1650. $listofidtoclean[$cursor]=$id;
  1651. break;
  1652. }
  1653. $cursor=$listofparentid[$cursor];
  1654. }
  1655. if (count($listofidtoclean)) break;
  1656. }
  1657. $sql = "UPDATE ".MAIN_DB_PREFIX.$tabletocleantree;
  1658. $sql.= " SET ".$fieldfkparent." = 0";
  1659. $sql.= " WHERE rowid IN (".join(',',$listofidtoclean).")"; // So we update only records detected wrong
  1660. $resql = $db->query($sql);
  1661. if ($resql)
  1662. {
  1663. $nb=$db->affected_rows($sql);
  1664. if ($nb > 0)
  1665. {
  1666. // Removed orphelins records
  1667. print '<br>Some records were detected to have parent that is a child, we set them as root record for id: ';
  1668. print join(',',$listofidtoclean);
  1669. }
  1670. $totalnb+=$nb;
  1671. }
  1672. //else dol_print_error($db);
  1673. // Check and clean orphelins
  1674. $sql = "UPDATE ".MAIN_DB_PREFIX.$tabletocleantree;
  1675. $sql.= " SET ".$fieldfkparent." = 0";
  1676. $sql.= " WHERE ".$fieldfkparent." NOT IN (".join(',',$listofid).")"; // So we update only records linked to a non existing parent
  1677. $resql = $db->query($sql);
  1678. if ($resql)
  1679. {
  1680. $nb=$db->affected_rows($sql);
  1681. if ($nb > 0)
  1682. {
  1683. // Removed orphelins records
  1684. print '<br>Some orphelins were found and modified to be parent so records are visible again for id: ';
  1685. print join(',',$listofid);
  1686. }
  1687. $totalnb+=$nb;
  1688. }
  1689. //else dol_print_error($db);
  1690. print '<br>We fixed '.$totalnb.' record(s). Some records may still be corrupted. New check may be required.';
  1691. return $totalnb;
  1692. }
  1693. }
  1694. /**
  1695. * Get an array with properties of an element
  1696. *
  1697. * @param string $element_type Element type: 'action', 'facture', 'project_task' or 'object@modulext'...
  1698. * @return array (module, classpath, element, subelement, classfile, classname)
  1699. */
  1700. function getElementProperties($element_type)
  1701. {
  1702. // Parse element/subelement (ex: project_task)
  1703. $module = $element = $subelement = $element_type;
  1704. // If we ask an resource form external module (instead of default path)
  1705. if (preg_match('/^([^@]+)@([^@]+)$/i',$element_type,$regs))
  1706. {
  1707. $element = $subelement = $regs[1];
  1708. $module = $regs[2];
  1709. }
  1710. //print '<br />1. element : '.$element.' - module : '.$module .'<br />';
  1711. if ( preg_match('/^([^_]+)_([^_]+)/i',$element,$regs))
  1712. {
  1713. $module = $element = $regs[1];
  1714. $subelement = $regs[2];
  1715. }
  1716. $classfile = strtolower($subelement);
  1717. $classname = ucfirst($subelement);
  1718. $classpath = $module.'/class';
  1719. // For compat
  1720. if($element_type == "action") {
  1721. $classpath = 'comm/action/class';
  1722. $subelement = 'Actioncomm';
  1723. $module = 'agenda';
  1724. }
  1725. // To work with non standard path
  1726. if ($element_type == 'facture' || $element_type == 'invoice') {
  1727. $classpath = 'compta/facture/class';
  1728. $module='facture';
  1729. $subelement='facture';
  1730. }
  1731. if ($element_type == 'commande' || $element_type == 'order') {
  1732. $classpath = 'commande/class';
  1733. $module='commande';
  1734. $subelement='commande';
  1735. }
  1736. if ($element_type == 'propal') {
  1737. $classpath = 'comm/propal/class';
  1738. }
  1739. if ($element_type == 'supplier_proposal') {
  1740. $classpath = 'supplier_proposal/class';
  1741. }
  1742. if ($element_type == 'shipping') {
  1743. $classpath = 'expedition/class';
  1744. $subelement = 'expedition';
  1745. $module = 'expedition_bon';
  1746. }
  1747. if ($element_type == 'delivery') {
  1748. $classpath = 'livraison/class';
  1749. $subelement = 'livraison';
  1750. $module = 'livraison_bon';
  1751. }
  1752. if ($element_type == 'contract') {
  1753. $classpath = 'contrat/class';
  1754. $module='contrat';
  1755. $subelement='contrat';
  1756. }
  1757. if ($element_type == 'member') {
  1758. $classpath = 'adherents/class';
  1759. $module='adherent';
  1760. $subelement='adherent';
  1761. }
  1762. if ($element_type == 'cabinetmed_cons') {
  1763. $classpath = 'cabinetmed/class';
  1764. $module='cabinetmed';
  1765. $subelement='cabinetmedcons';
  1766. }
  1767. if ($element_type == 'fichinter') {
  1768. $classpath = 'fichinter/class';
  1769. $module='ficheinter';
  1770. $subelement='fichinter';
  1771. }
  1772. if ($element_type == 'dolresource' || $element_type == 'resource') {
  1773. $classpath = 'resource/class';
  1774. $module='resource';
  1775. $subelement='dolresource';
  1776. }
  1777. if ($element_type == 'propaldet') {
  1778. $classpath = 'comm/propal/class';
  1779. $module='propal';
  1780. $subelement='propaleligne';
  1781. }
  1782. $classfile = strtolower($subelement);
  1783. $classname = ucfirst($subelement);
  1784. $element_properties = array(
  1785. 'module' => $module,
  1786. 'classpath' => $classpath,
  1787. 'element' => $element,
  1788. 'subelement' => $subelement,
  1789. 'classfile' => $classfile,
  1790. 'classname' => $classname
  1791. );
  1792. return $element_properties;
  1793. }
  1794. /**
  1795. * Fetch an object from its id and element_type
  1796. * Inclusion classes is automatic
  1797. *
  1798. * @param int $element_id Element id
  1799. * @param string $element_type Element type
  1800. * @return int|object object || 0 || -1 if error
  1801. */
  1802. function fetchObjectByElement($element_id, $element_type)
  1803. {
  1804. global $conf;
  1805. global $db,$conf;
  1806. $element_prop = getElementProperties($element_type);
  1807. if (is_array($element_prop) && $conf->{$element_prop['module']}->enabled)
  1808. {
  1809. dol_include_once('/'.$element_prop['classpath'].'/'.$element_prop['classfile'].'.class.php');
  1810. $objectstat = new $element_prop['classname']($db);
  1811. $ret = $objectstat->fetch($element_id);
  1812. if ($ret >= 0)
  1813. {
  1814. return $objectstat;
  1815. }
  1816. }
  1817. return 0;
  1818. }
  1819. /**
  1820. * Convert an array with RGB value into hex RGB value.
  1821. * This is the opposite function of colorStringToArray
  1822. *
  1823. * @param array $arraycolor Array
  1824. * @param string $colorifnotfound Color code to return if entry not defined or not a RGB format
  1825. * @return string RGB hex value (without # before). For example: 'FF00FF', '01FF02'
  1826. * @see colorStringToArray
  1827. */
  1828. function colorArrayToHex($arraycolor,$colorifnotfound='888888')
  1829. {
  1830. if (! is_array($arraycolor)) return $colorifnotfound;
  1831. if (empty($arraycolor)) return $colorifnotfound;
  1832. return sprintf("%02s",dechex($arraycolor[0])).sprintf("%02s",dechex($arraycolor[1])).sprintf("%02s",dechex($arraycolor[2]));
  1833. }
  1834. /**
  1835. * Convert a string RGB value ('FFFFFF', '255,255,255') into an array RGB array(255,255,255).
  1836. * This is the opposite function of colorArrayToHex.
  1837. * If entry is already an array, return it.
  1838. *
  1839. * @param string $stringcolor String with hex (FFFFFF) or comma RGB ('255,255,255')
  1840. * @param array $colorifnotfound Color code array to return if entry not defined
  1841. * @return string RGB hex value (without # before). For example: FF00FF
  1842. * @see colorArrayToHex
  1843. */
  1844. function colorStringToArray($stringcolor,$colorifnotfound=array(88,88,88))
  1845. {
  1846. if (is_array($stringcolor)) return $stringcolor; // If already into correct output format, we return as is
  1847. $tmp=preg_match('/^#?([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])$/',$stringcolor,$reg);
  1848. if (! $tmp)
  1849. {
  1850. $tmp=explode(',',$stringcolor);
  1851. if (count($tmp) < 3) return $colorifnotfound;
  1852. return $tmp;
  1853. }
  1854. return array(hexdec($reg[1]),hexdec($reg[2]),hexdec($reg[3]));
  1855. }