dolibarr  7.0.0-beta
functions.lib.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2000-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
4  * Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
5  * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
6  * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
7  * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
8  * Copyright (C) 2005-2017 Regis Houssin <regis.houssin@capnetworks.com>
9  * Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
10  * Copyright (C) 2010-2016 Juanjo Menent <jmenent@2byte.es>
11  * Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
12  * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro@zendsi.com>
13  * Copyright (C) 2014 Cédric GROSS <c.gross@kreiz-it.fr>
14  * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
15  * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  * or see http://www.gnu.org/
30  */
31 
38 include_once DOL_DOCUMENT_ROOT .'/core/lib/json.lib.php';
39 
40 
52 function getStaticMember($class, $member)
53 {
54  dol_syslog(__FUNCTION__ . " is deprecated", LOG_WARNING);
55 
56  // This part is deprecated. Uncomment if for php 5.2.*, and comment next isset class::member
57  /*if (version_compare(phpversion(), '5.3.0', '<'))
58  {
59  if (is_object($class)) $class = get_class($class);
60  $classObj = new ReflectionClass($class);
61  $result = null;
62 
63  $found=0;
64  foreach($classObj->getStaticProperties() as $prop => $value)
65  {
66  if ($prop == $member)
67  {
68  $result = $value;
69  $found++;
70  break;
71  }
72  }
73 
74  if ($found) return $result;
75  }*/
76 
77  if (isset($class::$member)) return $class::$member;
78  dol_print_error('','Try to get a static member "'.$member.'" in class "'.$class.'" that does not exists or is not static.');
79  return null;
80 }
81 
82 
94 function getDoliDBInstance($type, $host, $user, $pass, $name, $port)
95 {
96  require_once DOL_DOCUMENT_ROOT ."/core/db/".$type.'.class.php';
97 
98  $class='DoliDB'.ucfirst($type);
99  $dolidb=new $class($type, $host, $user, $pass, $name, $port);
100  return $dolidb;
101 }
102 
120 function getEntity($element, $shared=1, $forceentity=null)
121 {
122  global $conf, $mc;
123 
124  // For backward compatibilty
125  if ($element == 'actioncomm') $element='agenda';
126  if ($element == 'fichinter') $element='intervention';
127  if ($element == 'categorie') $element='category';
128 
129  if (is_object($mc))
130  {
131  return $mc->getEntity($element, $shared, $forceentity);
132  }
133  else
134  {
135  $out='';
136  $addzero = array('user', 'usergroup', 'c_email_templates', 'email_template', 'default_values');
137  if (in_array($element, $addzero)) $out.= '0,';
138  $out.= $conf->entity;
139  return $out;
140  }
141 }
142 
159 function getBrowserInfo($user_agent)
160 {
161  include_once DOL_DOCUMENT_ROOT.'/includes/mobiledetect/mobiledetectlib/Mobile_Detect.php';
162 
163  $name='unknown';
164  $version='';
165  $os='unknown';
166  $phone = '';
167 
168  $detectmobile = new Mobile_Detect(null, $user_agent);
169  $tablet = $detectmobile->isTablet();
170 
171  if ($detectmobile->isMobile()) {
172 
173  $phone = 'unknown';
174 
175  // If phone/smartphone, we set phone os name.
176  if ($detectmobile->is('AndroidOS')) {
177  $os = $phone = 'android';
178  } elseif ($detectmobile->is('BlackBerryOS')) {
179  $os = $phone = 'blackberry';
180  } elseif ($detectmobile->is('iOS')) {
181  $os = 'ios';
182  $phone = 'iphone';
183  } elseif ($detectmobile->is('PalmOS')) {
184  $os = $phone = 'palm';
185  } elseif ($detectmobile->is('SymbianOS')) {
186  $os = 'symbian';
187  } elseif ($detectmobile->is('webOS')) {
188  $os = 'webos';
189  } elseif ($detectmobile->is('MaemoOS')) {
190  $os = 'maemo';
191  } elseif ($detectmobile->is('WindowsMobileOS') || $detectmobile->is('WindowsPhoneOS')) {
192  $os = 'windows';
193  }
194  }
195 
196  // OS
197  if (preg_match('/linux/i', $user_agent)) { $os='linux'; }
198  elseif (preg_match('/macintosh/i', $user_agent)) { $os='macintosh'; }
199  elseif (preg_match('/windows/i', $user_agent)) { $os='windows'; }
200 
201  // Name
202  if (preg_match('/firefox(\/|\s)([\d\.]*)/i', $user_agent, $reg)) { $name='firefox'; $version=$reg[2]; }
203  elseif (preg_match('/edge(\/|\s)([\d\.]*)/i', $user_agent, $reg)) { $name='edge'; $version=$reg[2]; }
204  elseif (preg_match('/chrome(\/|\s)([\d\.]+)/i', $user_agent, $reg)) { $name='chrome'; $version=$reg[2]; } // we can have 'chrome (Mozilla...) chrome x.y' in one string
205  elseif (preg_match('/chrome/i', $user_agent, $reg)) { $name='chrome'; }
206  elseif (preg_match('/iceweasel/i', $user_agent)) { $name='iceweasel'; }
207  elseif (preg_match('/epiphany/i', $user_agent)) { $name='epiphany'; }
208  elseif (preg_match('/safari(\/|\s)([\d\.]*)/i', $user_agent, $reg)) { $name='safari'; $version=$reg[2]; } // Safari is often present in string for mobile but its not.
209  elseif (preg_match('/opera(\/|\s)([\d\.]*)/i', $user_agent, $reg)) { $name='opera'; $version=$reg[2]; }
210  elseif (preg_match('/(MSIE\s([0-9]+\.[0-9]))|.*(Trident\/[0-9]+.[0-9];.*rv:([0-9]+\.[0-9]+))/i', $user_agent, $reg)) { $name='ie'; $version=end($reg); } // MS products at end
211  elseif (preg_match('/(Windows NT\s([0-9]+\.[0-9])).*(Trident\/[0-9]+.[0-9];.*rv:([0-9]+\.[0-9]+))/i', $user_agent, $reg)) { $name='ie'; $version=end($reg); } // MS products at end
212  elseif (preg_match('/l(i|y)n(x|ks)(\(|\/|\s)*([\d\.]+)/i', $user_agent, $reg)) { $name='lynxlinks'; $version=$reg[4]; }
213 
214  if ($tablet) {
215  $layout = 'tablet';
216  } elseif ($phone) {
217  $layout = 'phone';
218  } else {
219  $layout = 'classic';
220  }
221 
222  return array(
223  'browsername' => $name,
224  'browserversion' => $version,
225  'browseros' => $os,
226  'layout' => $layout,
227  'phone' => $phone,
228  'tablet' => $tablet
229  );
230 }
231 
237 function dol_shutdown()
238 {
239  global $conf,$user,$langs,$db;
240  $disconnectdone=false; $depth=0;
241  if (is_object($db) && ! empty($db->connected)) { $depth=$db->transaction_opened; $disconnectdone=$db->close(); }
242  dol_syslog("--- End access to ".$_SERVER["PHP_SELF"].(($disconnectdone && $depth)?' (Warn: db disconnection forced, transaction depth was '.$depth.')':''), (($disconnectdone && $depth)?LOG_WARNING:LOG_INFO));
243 }
244 
251 function GETPOSTISSET($paramname)
252 {
253  return (isset($_POST[$paramname]) || isset($_GET[$paramname]));
254 }
255 
279 function GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
280 {
281  global $mysoc,$user,$conf;
282 
283  if (empty($paramname)) return 'BadFirstParameterForGETPOST';
284  if (empty($check))
285  {
286  dol_syslog("Deprecated use of GETPOST, called with 1st param = ".$paramname." and 2nd param is '', when calling page ".$_SERVER["PHP_SELF"], LOG_WARNING);
287  // Enable this line to know who call the GETPOST with '' $check parameter.
288  //var_dump(debug_backtrace()[0]);
289  }
290 
291  if (empty($method)) $out = isset($_GET[$paramname])?$_GET[$paramname]:(isset($_POST[$paramname])?$_POST[$paramname]:'');
292  elseif ($method==1) $out = isset($_GET[$paramname])?$_GET[$paramname]:'';
293  elseif ($method==2) $out = isset($_POST[$paramname])?$_POST[$paramname]:'';
294  elseif ($method==3) $out = isset($_POST[$paramname])?$_POST[$paramname]:(isset($_GET[$paramname])?$_GET[$paramname]:'');
295  elseif ($method==4) $out = isset($_POST[$paramname])?$_POST[$paramname]:(isset($_GET[$paramname])?$_GET[$paramname]:(isset($_COOKIE[$paramname])?$_COOKIE[$paramname]:''));
296  else return 'BadThirdParameterForGETPOST';
297 
298  if (empty($method) || $method == 3 || $method == 4)
299  {
300 
301  $relativepathstring = $_SERVER["PHP_SELF"];
302  // Clean $relativepathstring
303  if (constant('DOL_URL_ROOT')) $relativepathstring = preg_replace('/^'.preg_quote(constant('DOL_URL_ROOT'),'/').'/', '', $relativepathstring);
304  $relativepathstring = preg_replace('/^\//', '', $relativepathstring);
305  $relativepathstring = preg_replace('/^custom\//', '', $relativepathstring);
306  //var_dump($relativepathstring);
307  //var_dump($user->default_values);
308 
309  // Code for search criteria persistence.
310  // Retrieve values if restore_lastsearch_values is set and there is saved values
311  if (! empty($_GET['restore_lastsearch_values']) && ! empty($_SESSION['lastsearch_values_'.$relativepathstring])) // Keep $_GET here
312  {
313  $tmp=json_decode($_SESSION['lastsearch_values_'.$relativepathstring], true);
314  if (is_array($tmp))
315  {
316  foreach($tmp as $key => $val)
317  {
318  if ($key == $paramname)
319  {
320  $out=$val;
321  break;
322  }
323  }
324  }
325  }
326  // Else, retreive default values if we are not doing a sort
327  elseif (! isset($_GET['sortfield'])) // If we did a click on a field to sort, we do no apply default values. Same if option MAIN_ENABLE_DEFAULT_VALUES is not set
328  {
329  if (! empty($_GET['action']) && $_GET['action'] == 'create' && ! isset($_GET[$paramname]) && ! isset($_POST[$paramname]))
330  {
331  // Search default value from $object->field
332  global $object;
333  if (is_object($object) && isset($object->fields[$paramname]['default']))
334  {
335  $out = $object->fields[$paramname]['default'];
336  }
337  }
338  if (! empty($conf->global->MAIN_ENABLE_DEFAULT_VALUES))
339  {
340  if (! empty($_GET['action']) && $_GET['action'] == 'create' && ! isset($_GET[$paramname]) && ! isset($_POST[$paramname]))
341  {
342  // Now search in setup to overwrite default values
343  if (! empty($user->default_values)) // $user->default_values defined from menu 'Setup - Default values'
344  {
345  if (isset($user->default_values[$relativepathstring]['createform']))
346  {
347  foreach($user->default_values[$relativepathstring]['createform'] as $defkey => $defval)
348  {
349  $qualified = 0;
350  if ($defkey != '_noquery_')
351  {
352  $tmpqueryarraytohave=explode('&', $defkey);
353  $tmpqueryarraywehave=explode('&', dol_string_nohtmltag($_SERVER['QUERY_STRING']));
354  $foundintru=0;
355  foreach($tmpqueryarraytohave as $tmpquerytohave)
356  {
357  if (! in_array($tmpquerytohave, $tmpqueryarraywehave)) $foundintru=1;
358  }
359  if (! $foundintru) $qualified=1;
360  //var_dump($defkey.'-'.$qualified);
361  }
362  else $qualified = 1;
363 
364  if ($qualified)
365  {
366  //var_dump($user->default_values[$relativepathstring][$defkey]['createform']);
367  if (isset($user->default_values[$relativepathstring]['createform'][$defkey][$paramname]))
368  {
369  $out = $user->default_values[$relativepathstring]['createform'][$defkey][$paramname];
370  break;
371  }
372  }
373  }
374  }
375  }
376  }
377  // Management of default search_filters and sort order
378  //elseif (preg_match('/list.php$/', $_SERVER["PHP_SELF"]) && ! empty($paramname) && ! isset($_GET[$paramname]) && ! isset($_POST[$paramname]))
379  elseif (! empty($paramname) && ! isset($_GET[$paramname]) && ! isset($_POST[$paramname]))
380  {
381  if (! empty($user->default_values)) // $user->default_values defined from menu 'Setup - Default values'
382  {
383  //var_dump($user->default_values[$relativepathstring]);
384  if ($paramname == 'sortfield' || $paramname == 'sortorder') // Sorted on which fields ? ASC or DESC ?
385  {
386  if (isset($user->default_values[$relativepathstring]['sortorder'])) // Even if paramname is sortfield, data are stored into ['sortorder...']
387  {
388  foreach($user->default_values[$relativepathstring]['sortorder'] as $defkey => $defval)
389  {
390  $qualified = 0;
391  if ($defkey != '_noquery_')
392  {
393  $tmpqueryarraytohave=explode('&', $defkey);
394  $tmpqueryarraywehave=explode('&', dol_string_nohtmltag($_SERVER['QUERY_STRING']));
395  $foundintru=0;
396  foreach($tmpqueryarraytohave as $tmpquerytohave)
397  {
398  if (! in_array($tmpquerytohave, $tmpqueryarraywehave)) $foundintru=1;
399  }
400  if (! $foundintru) $qualified=1;
401  //var_dump($defkey.'-'.$qualified);
402  }
403  else $qualified = 1;
404 
405  if ($qualified)
406  {
407  $forbidden_chars_to_replace=array(" ","'","/","\\",":","*","?","\"","<",">","|","[","]",";","="); // we accept _, -, . and ,
408  foreach($user->default_values[$relativepathstring]['sortorder'][$defkey] as $key => $val)
409  {
410  if ($out) $out.=', ';
411  if ($paramname == 'sortfield')
412  {
413  $out.=dol_string_nospecial($key, '', $forbidden_chars_to_replace);
414  }
415  if ($paramname == 'sortorder')
416  {
417  $out.=dol_string_nospecial($val, '', $forbidden_chars_to_replace);
418  }
419  }
420  //break; // No break for sortfield and sortorder so we can cumulate fields (is it realy usefull ?)
421  }
422  }
423  }
424  }
425  elseif (isset($user->default_values[$relativepathstring]['filters']))
426  {
427  foreach($user->default_values[$relativepathstring]['filters'] as $defkey => $defval)
428  {
429  $qualified = 0;
430  if ($defkey != '_noquery_')
431  {
432  $tmpqueryarraytohave=explode('&', $defkey);
433  $tmpqueryarraywehave=explode('&', dol_string_nohtmltag($_SERVER['QUERY_STRING']));
434  $foundintru=0;
435  foreach($tmpqueryarraytohave as $tmpquerytohave)
436  {
437  if (! in_array($tmpquerytohave, $tmpqueryarraywehave)) $foundintru=1;
438  }
439  if (! $foundintru) $qualified=1;
440  //var_dump($defkey.'-'.$qualified);
441  }
442  else $qualified = 1;
443 
444  if ($qualified)
445  {
446  if (isset($_POST['sall']) || isset($_POST['search_all']) || isset($_GET['sall']) || isset($_GET['search_all']))
447  {
448  // We made a search from quick search menu, do we still use default filter ?
449  if (empty($conf->global->MAIN_DISABLE_DEFAULT_FILTER_FOR_QUICK_SEARCH))
450  {
451  $forbidden_chars_to_replace=array(" ","'","/","\\",":","*","?","\"","<",">","|","[","]",";","="); // we accept _, -, . and ,
452  $out = dol_string_nospecial($user->default_values[$relativepathstring]['filters'][$defkey][$paramname], '', $forbidden_chars_to_replace);
453  }
454  }
455  else
456  {
457  $forbidden_chars_to_replace=array(" ","'","/","\\",":","*","?","\"","<",">","|","[","]",";","="); // we accept _, -, . and ,
458  $out = dol_string_nospecial($user->default_values[$relativepathstring]['filters'][$defkey][$paramname], '', $forbidden_chars_to_replace);
459  }
460  break;
461  }
462  }
463  }
464  }
465  }
466  }
467  }
468  }
469 
470  // Substitution variables for GETPOST (used to get final url with variable parameters or final default value with variable paramaters)
471  // Example of variables: __DAY__, __MONTH__, __YEAR__, __MYCOMPANY_COUNTRY_ID__, __USER_ID__, ...
472  // We do this only if var is a GET. If it is a POST, may be we want to post the text with vars as the setup text.
473  if (! is_array($out) && empty($_POST[$paramname]) && empty($noreplace))
474  {
475  $maxloop=20; $loopnb=0; // Protection against infinite loop
476  while (preg_match('/__([A-Z0-9]+_?[A-Z0-9]+)__/i', $out, $reg) && ($loopnb < $maxloop)) // Detect '__ABCDEF__' as key 'ABCDEF' and '__ABC_DEF__' as key 'ABC_DEF'. Detection is also correct when 2 vars are side by side.
477  {
478  $loopnb++; $newout = '';
479 
480  if ($reg[1] == 'DAY') { $tmp=dol_getdate(dol_now(), true); $newout = $tmp['mday']; }
481  elseif ($reg[1] == 'MONTH') { $tmp=dol_getdate(dol_now(), true); $newout = $tmp['mon']; }
482  elseif ($reg[1] == 'YEAR') { $tmp=dol_getdate(dol_now(), true); $newout = $tmp['year']; }
483  elseif ($reg[1] == 'PREVIOUS_DAY') { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_prev_day($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['day']; }
484  elseif ($reg[1] == 'PREVIOUS_MONTH') { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_prev_month($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['month']; }
485  elseif ($reg[1] == 'PREVIOUS_YEAR') { $tmp=dol_getdate(dol_now(), true); $newout = ($tmp['year'] - 1); }
486  elseif ($reg[1] == 'NEXT_DAY') { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_next_day($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['day']; }
487  elseif ($reg[1] == 'NEXT_MONTH') { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_next_month($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['month']; }
488  elseif ($reg[1] == 'NEXT_YEAR') { $tmp=dol_getdate(dol_now(), true); $newout = ($tmp['year'] + 1); }
489  elseif ($reg[1] == 'MYCOMPANY_COUNTRY_ID' || $reg[1] == 'MYCOUNTRY_ID' || $reg[1] == 'MYCOUNTRYID')
490  {
491  $newout = $mysoc->country_id;
492  }
493  elseif ($reg[1] == 'USER_ID' || $reg[1] == 'USERID')
494  {
495  $newout = $user->id;
496  }
497  elseif ($reg[1] == 'USER_SUPERVISOR_ID' || $reg[1] == 'SUPERVISOR_ID' || $reg[1] == 'SUPERVISORID')
498  {
499  $newout = $user->fk_user;
500  }
501  elseif ($reg[1] == 'ENTITY_ID' || $reg[1] == 'ENTITYID')
502  {
503  $newout = $conf->entity;
504  }
505  else $newout = ''; // Key not found, we replace with empty string
506  //var_dump('__'.$reg[1].'__ -> '.$newout);
507  $out = preg_replace('/__'.preg_quote($reg[1],'/').'__/', $newout, $out);
508  }
509  }
510 
511  // Check is done after replacement
512  switch ($check)
513  {
514  case 'none':
515  break;
516  case 'int': // Check param is a numeric value (integer but also float or hexadecimal)
517  if (! is_numeric($out)) { $out=''; }
518  break;
519  case 'intcomma':
520  if (preg_match('/[^0-9,-]+/i',$out)) $out='';
521  break;
522  case 'alpha':
523  if (! is_array($out))
524  {
525  $out=trim($out);
526  // '"' is dangerous because param in url can close the href= or src= and add javascript functions.
527  // '../' is dangerous because it allows dir transversals
528  if (preg_match('/"/',$out)) $out='';
529  else if (preg_match('/\.\.\//',$out)) $out='';
530  }
531  break;
532  case 'san_alpha':
533  $out=filter_var($out,FILTER_SANITIZE_STRING);
534  break;
535  case 'aZ':
536  if (! is_array($out))
537  {
538  $out=trim($out);
539  if (preg_match('/[^a-z]+/i',$out)) $out='';
540  }
541  break;
542  case 'aZ09':
543  if (! is_array($out))
544  {
545  $out=trim($out);
546  if (preg_match('/[^a-z0-9_\-\.]+/i',$out)) $out='';
547  }
548  break;
549  case 'array':
550  if (! is_array($out) || empty($out)) $out=array();
551  break;
552  case 'nohtml':
553  $out=dol_string_nohtmltag($out);
554  break;
555  case 'alphanohtml': // Recommended for search params
556  if (! is_array($out))
557  {
558  $out=trim($out);
559  // '"' is dangerous because param in url can close the href= or src= and add javascript functions.
560  // '../' is dangerous because it allows dir transversals
561  if (preg_match('/"/',$out)) $out='';
562  else if (preg_match('/\.\.\//',$out)) $out='';
563  $out=dol_string_nohtmltag($out);
564  }
565  break;
566  case 'custom':
567  if (empty($filter)) return 'BadFourthParameterForGETPOST';
568  $out=filter_var($out, $filter, $options);
569  break;
570  }
571 
572  // Code for search criteria persistence.
573  // Save data into session if key start with 'search_' or is 'smonth', 'syear', 'month', 'year'
574  if (empty($method) || $method == 3 || $method == 4)
575  {
576  //if (preg_match('/^search_/', $paramname) || in_array($paramname, array('sortorder', 'sortfield", 'smonth', 'syear', 'month', 'year')))
577  if (preg_match('/^search_/', $paramname) || in_array($paramname, array('sortorder','sortfield')))
578  {
579  //var_dump($paramname.' - '.$out.' '.$user->default_values[$relativepathstring]['filters'][$paramname]);
580 
581  // We save search key only if:
582  // - not empty, or
583  // - if value is empty and a default value exists that is not empty (it means we did a filter to an empty value when default was not).
584 
585  //if (! empty($out) || ! empty($user->default_values[$relativepathstring]['filters'][$paramname]))
586  if (! empty($out))
587  {
588  $user->lastsearch_values_tmp[$relativepathstring][$paramname]=$out;
589  }
590  }
591  }
592 
593  return $out;
594 }
595 
596 
606 if (! function_exists('dol_getprefix'))
607 {
608  function dol_getprefix($mode='')
609  {
610  global $conf;
611 
612  // If MAIL_PREFIX_FOR_EMAIL_ID is set and prefix is for email
613  if ($mode == 'email' && ! empty($conf->global->MAIL_PREFIX_FOR_EMAIL_ID))
614  {
615  if ($conf->global->MAIL_PREFIX_FOR_EMAIL_ID != 'SERVER_NAME') return $conf->global->MAIL_PREFIX_FOR_EMAIL_ID;
616  else if (isset($_SERVER["SERVER_NAME"])) return $_SERVER["SERVER_NAME"];
617  }
618 
619  if (isset($_SERVER["SERVER_NAME"]) && isset($_SERVER["DOCUMENT_ROOT"]))
620  {
621  return dol_hash($_SERVER["SERVER_NAME"].$_SERVER["DOCUMENT_ROOT"].DOL_DOCUMENT_ROOT.DOL_URL_ROOT);
622  // Use this for a "readable" cookie name
623  //return dol_sanitizeFileName($_SERVER["SERVER_NAME"].$_SERVER["DOCUMENT_ROOT"].DOL_DOCUMENT_ROOT.DOL_URL_ROOT);
624  }
625  else return dol_hash(DOL_DOCUMENT_ROOT.DOL_URL_ROOT);
626  }
627 }
628 
639 function dol_include_once($relpath, $classname='')
640 {
641  global $conf,$langs,$user,$mysoc; // Do not remove this. They must be defined for files we include. Other globals var must be retreived with $GLOBALS['var']
642 
643  $fullpath = dol_buildpath($relpath);
644 
645  if (!file_exists($fullpath)) {
646  dol_syslog('functions::dol_include_once Tried to load unexisting file: '.$relpath, LOG_ERR);
647  return false;
648  }
649 
650  if (! empty($classname) && ! class_exists($classname)) {
651  return include $fullpath;
652  } else {
653  return include_once $fullpath;
654  }
655 }
656 
657 
668 function dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
669 {
670  global $conf;
671 
672  $path=preg_replace('/^\//','',$path);
673 
674  if (empty($type)) // For a filesystem path
675  {
676  $res = DOL_DOCUMENT_ROOT.'/'.$path; // Standard default path
677  foreach ($conf->file->dol_document_root as $key => $dirroot) // ex: array(["main"]=>"/home/main/htdocs", ["alt0"]=>"/home/dirmod/htdocs", ...)
678  {
679  if ($key == 'main') continue;
680  if (file_exists($dirroot.'/'.$path))
681  {
682  $res=$dirroot.'/'.$path;
683  return $res;
684  }
685  }
686  if ($returnemptyifnotfound) // Not found into alternate dir
687  {
688  if ($returnemptyifnotfound == 1 || ! file_exists($res)) return '';
689  }
690  }
691  else // For an url path
692  {
693  // We try to get local path of file on filesystem from url
694  // Note that trying to know if a file on disk exist by forging path on disk from url
695  // works only for some web server and some setup. This is bugged when
696  // using proxy, rewriting, virtual path, etc...
697  $res='';
698  if ($type == 1) $res = DOL_URL_ROOT.'/'.$path; // Standard value
699  if ($type == 2) $res = DOL_MAIN_URL_ROOT.'/'.$path; // Standard value
700  if ($type == 3) $res = DOL_URL_ROOT.'/'.$path;
701 
702  foreach ($conf->file->dol_document_root as $key => $dirroot) // ex: array(["main"]=>"/home/main/htdocs", ["alt0"]=>"/home/dirmod/htdocs", ...)
703  {
704  if ($key == 'main')
705  {
706  if ($type == 3)
707  {
708  global $dolibarr_main_url_root;
709 
710  // Define $urlwithroot
711  $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
712  $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
713  //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
714 
715  $res=(preg_match('/^http/i',$conf->file->dol_url_root[$key])?'':$urlwithroot).'/'.$path; // Test on start with http is for old conf syntax
716  }
717  continue;
718  }
719  preg_match('/^([^\?]+(\.css\.php|\.css|\.js\.php|\.js|\.png|\.jpg|\.php)?)/i',$path,$regs); // Take part before '?'
720  if (! empty($regs[1]))
721  {
722  //print $key.'-'.$dirroot.'/'.$path.'-'.$conf->file->dol_url_root[$type].'<br>'."\n";
723  if (file_exists($dirroot.'/'.$regs[1]))
724  {
725  if ($type == 1)
726  {
727  $res=(preg_match('/^http/i',$conf->file->dol_url_root[$key])?'':DOL_URL_ROOT).$conf->file->dol_url_root[$key].'/'.$path;
728  }
729  if ($type == 2)
730  {
731  $res=(preg_match('/^http/i',$conf->file->dol_url_root[$key])?'':DOL_MAIN_URL_ROOT).$conf->file->dol_url_root[$key].'/'.$path;
732  }
733  if ($type == 3)
734  {
735  global $dolibarr_main_url_root;
736 
737  // Define $urlwithroot
738  $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
739  $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
740  //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
741 
742  $res=(preg_match('/^http/i',$conf->file->dol_url_root[$key])?'':$urlwithroot).$conf->file->dol_url_root[$key].'/'.$path; // Test on start with http is for old conf syntax
743  }
744  break;
745  }
746  }
747  }
748  }
749 
750  return $res;
751 }
752 
763 function dol_clone($object, $native=0)
764 {
765  //dol_syslog(__FUNCTION__ . " is deprecated", LOG_WARNING);
766 
767  if (empty($native))
768  {
769  $myclone=unserialize(serialize($object));
770  }
771  else
772  {
773  $myclone = clone $object; // PHP clone is a shallow copy only, not a real clone, so properties of references will keep references (refer to the same target/variable)
774  }
775 
776  return $myclone;
777 }
778 
788 function dol_size($size,$type='')
789 {
790  global $conf;
791  if (empty($conf->dol_optimize_smallscreen)) return $size;
792  if ($type == 'width' && $size > 250) return 250;
793  else return 10;
794 }
795 
796 
807 function dol_sanitizeFileName($str,$newstr='_',$unaccent=1)
808 {
809  $filesystem_forbidden_chars = array('<','>','/','\\','?','*','|','"','°');
810  return dol_string_nospecial($unaccent?dol_string_unaccent($str):$str, $newstr, $filesystem_forbidden_chars);
811 }
812 
823 function dol_sanitizePathName($str,$newstr='_',$unaccent=1)
824 {
825  $filesystem_forbidden_chars = array('<','>','?','*','|','"','°');
826  return dol_string_nospecial($unaccent?dol_string_unaccent($str):$str, $newstr, $filesystem_forbidden_chars);
827 }
828 
837 function dol_string_unaccent($str)
838 {
839  if (utf8_check($str))
840  {
841  // See http://www.utf8-chartable.de/
842  $string = rawurlencode($str);
843  $replacements = array(
844  '%C3%80' => 'A','%C3%81' => 'A','%C3%82' => 'A','%C3%83' => 'A','%C3%84' => 'A','%C3%85' => 'A',
845  '%C3%88' => 'E','%C3%89' => 'E','%C3%8A' => 'E','%C3%8B' => 'E',
846  '%C3%8C' => 'I','%C3%8D' => 'I','%C3%8E' => 'I','%C3%8F' => 'I',
847  '%C3%92' => 'O','%C3%93' => 'O','%C3%94' => 'O','%C3%95' => 'O','%C3%96' => 'O',
848  '%C3%99' => 'U','%C3%9A' => 'U','%C3%9B' => 'U','%C3%9C' => 'U',
849  '%C3%A0' => 'a','%C3%A1' => 'a','%C3%A2' => 'a','%C3%A3' => 'a','%C3%A4' => 'a','%C3%A5' => 'a',
850  '%C3%A7' => 'c',
851  '%C3%A8' => 'e','%C3%A9' => 'e','%C3%AA' => 'e','%C3%AB' => 'e',
852  '%C3%AC' => 'i','%C3%AD' => 'i','%C3%AE' => 'i','%C3%AF' => 'i',
853  '%C3%B1' => 'n',
854  '%C3%B2' => 'o','%C3%B3' => 'o','%C3%B4' => 'o','%C3%B5' => 'o','%C3%B6' => 'o',
855  '%C3%B9' => 'u','%C3%BA' => 'u','%C3%BB' => 'u','%C3%BC' => 'u',
856  '%C3%BF' => 'y'
857  );
858  $string=strtr($string, $replacements);
859  return rawurldecode($string);
860  }
861  else
862  {
863  // See http://www.ascii-code.com/
864  $string = strtr(
865  $str,
866  "\xC0\xC1\xC2\xC3\xC4\xC5\xC7
867  \xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1
868  \xD2\xD3\xD4\xD5\xD8\xD9\xDA\xDB\xDD
869  \xE0\xE1\xE2\xE3\xE4\xE5\xE7\xE8\xE9\xEA\xEB
870  \xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF8
871  \xF9\xFA\xFB\xFC\xFD\xFF",
872  "AAAAAAC
873  EEEEIIIIDN
874  OOOOOUUUY
875  aaaaaaceeee
876  iiiidnooooo
877  uuuuyy"
878  );
879  $string = strtr($string, array("\xC4"=>"Ae", "\xC6"=>"AE", "\xD6"=>"Oe", "\xDC"=>"Ue", "\xDE"=>"TH", "\xDF"=>"ss", "\xE4"=>"ae", "\xE6"=>"ae", "\xF6"=>"oe", "\xFC"=>"ue", "\xFE"=>"th"));
880  return $string;
881  }
882 }
883 
895 function dol_string_nospecial($str,$newstr='_',$badcharstoreplace='')
896 {
897  $forbidden_chars_to_replace=array(" ", "'", "/", "\\", ":", "*", "?", "\"", "<", ">", "|", "[", "]", ",", ";", "=", '°'); // more complete than dol_sanitizeFileName
898  $forbidden_chars_to_remove=array();
899  if (is_array($badcharstoreplace)) $forbidden_chars_to_replace=$badcharstoreplace;
900  //$forbidden_chars_to_remove=array("(",")");
901 
902  return str_replace($forbidden_chars_to_replace,$newstr,str_replace($forbidden_chars_to_remove,"",$str));
903 }
904 
905 
912 function dolEscapeXML($string)
913 {
914  return strtr($string, array('\''=>'&apos;','"'=>'&quot;','&'=>'&amp;','<'=>'&lt;','>'=>'&gt;'));
915 }
916 
925 function dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
926 {
927  // escape quotes and backslashes, newlines, etc.
928  $substitjs=array("&#039;"=>"\\'","\r"=>'\\r');
929  //$substitjs['</']='<\/'; // We removed this. Should be useless.
930  if (empty($noescapebackslashn)) { $substitjs["\n"]='\\n'; $substitjs['\\']='\\\\'; }
931  if (empty($mode)) { $substitjs["'"]="\\'"; $substitjs['"']="\\'"; }
932  else if ($mode == 1) $substitjs["'"]="\\'";
933  else if ($mode == 2) { $substitjs['"']='\\"'; }
934  else if ($mode == 3) { $substitjs["'"]="\\'"; $substitjs['"']="\\\""; }
935  return strtr($stringtoescape, $substitjs);
936 }
937 
938 
948 function dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
949 {
950  // escape quotes and backslashes, newlines, etc.
951  $tmp=html_entity_decode($stringtoescape, ENT_COMPAT, 'UTF-8'); // TODO Use htmlspecialchars_decode instead, that make only required change for html tags
952  if (! $keepb) $tmp=strtr($tmp, array("<b>"=>'','</b>'=>''));
953  if (! $keepn) $tmp=strtr($tmp, array("\r"=>'\\r',"\n"=>'\\n'));
954  return htmlentities($tmp, ENT_COMPAT, 'UTF-8'); // TODO Use htmlspecialchars instead, that make only required change for html tags
955 }
956 
957 
964 function dol_strtolower($utf8_string)
965 {
966  return mb_strtolower($utf8_string, "UTF-8");
967 }
968 
975 function dol_strtoupper($utf8_string)
976 {
977  return mb_strtoupper($utf8_string, "UTF-8");
978 }
979 
980 
1001 function dol_syslog($message, $level = LOG_INFO, $ident = 0, $suffixinfilename='', $restricttologhandler='')
1002 {
1003  global $conf, $user;
1004 
1005  // If syslog module enabled
1006  if (empty($conf->syslog->enabled)) return;
1007 
1008  if (! empty($message))
1009  {
1010  // Test log level
1011  $logLevels = array(LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG);
1012  if (!in_array($level, $logLevels, true))
1013  {
1014  throw new Exception('Incorrect log level');
1015  }
1016  if ($level > $conf->global->SYSLOG_LEVEL) return;
1017 
1018  // If adding log inside HTML page is required
1019  if (! empty($_REQUEST['logtohtml']) && (! empty($conf->global->MAIN_ENABLE_LOG_TO_HTML) || ! empty($conf->global->MAIN_LOGTOHTML))) // MAIN_LOGTOHTML kept for backward compatibility
1020  {
1021  $conf->logbuffer[] = dol_print_date(time(),"%Y-%m-%d %H:%M:%S")." ".$message;
1022  }
1023 
1024  //TODO: Remove this. MAIN_ENABLE_LOG_INLINE_HTML should be deprecated and use a log handler dedicated to HTML output
1025  // If enable html log tag enabled and url parameter log defined, we show output log on HTML comments
1026  if (! empty($conf->global->MAIN_ENABLE_LOG_INLINE_HTML) && ! empty($_GET["log"]))
1027  {
1028  print "\n\n<!-- Log start\n";
1029  print $message."\n";
1030  print "Log end -->\n";
1031  }
1032 
1033  $data = array(
1034  'message' => $message,
1035  'script' => (isset($_SERVER['PHP_SELF'])? basename($_SERVER['PHP_SELF'],'.php') : false),
1036  'level' => $level,
1037  'user' => ((is_object($user) && $user->id) ? $user->login : false),
1038  'ip' => false
1039  );
1040 
1041  // This is when server run behind a reverse proxy
1042  if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) $data['ip'] = $_SERVER['HTTP_X_FORWARDED_FOR'].(empty($_SERVER["REMOTE_ADDR"])?'':'->'.$_SERVER['REMOTE_ADDR']);
1043  // This is when server run normally on a server
1044  else if (! empty($_SERVER["REMOTE_ADDR"])) $data['ip'] = $_SERVER['REMOTE_ADDR'];
1045  // This is when PHP session is ran inside a web server but not inside a client request (example: init code of apache)
1046  else if (! empty($_SERVER['SERVER_ADDR'])) $data['ip'] = $_SERVER['SERVER_ADDR'];
1047  // This is when PHP session is ran outside a web server, like from Windows command line (Not always defined, but useful if OS defined it).
1048  else if (! empty($_SERVER['COMPUTERNAME'])) $data['ip'] = $_SERVER['COMPUTERNAME'].(empty($_SERVER['USERNAME'])?'':'@'.$_SERVER['USERNAME']);
1049  // This is when PHP session is ran outside a web server, like from Linux command line (Not always defined, but usefull if OS defined it).
1050  else if (! empty($_SERVER['LOGNAME'])) $data['ip'] = '???@'.$_SERVER['LOGNAME'];
1051  // Loop on each log handler and send output
1052  foreach ($conf->loghandlers as $loghandlerinstance)
1053  {
1054  if ($restricttologhandler && $loghandlerinstance->code != $restricttologhandler) continue;
1055  $loghandlerinstance->export($data,$suffixinfilename);
1056  }
1057  unset($data);
1058  }
1059 
1060  if (! empty($ident))
1061  {
1062  foreach ($conf->loghandlers as $loghandlerinstance)
1063  {
1064  $loghandlerinstance->setIdent($ident);
1065  }
1066  }
1067 }
1068 
1069 
1082 function dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='')
1083 {
1084  print dol_get_fiche_head($links, $active, $title, $notab, $picto, $pictoisfullpath, $morehtmlright);
1085 }
1086 
1099 function dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='')
1100 {
1101  global $conf, $langs, $hookmanager;
1102 
1103  $out="\n".'<div class="tabs" data-role="controlgroup" data-type="horizontal">'."\n";
1104 
1105  if ($morehtmlright) $out.='<div class="inline-block floatright tabsElem">'.$morehtmlright.'</div>'; // Output right area first so when space is missing, text is in front of tabs and not under.
1106 
1107  // Show title
1108  $showtitle=1;
1109  if (! empty($conf->dol_optimize_smallscreen)) $showtitle=0;
1110  if (! empty($title) && $showtitle)
1111  {
1112  $limittitle=30;
1113  $out.='<a class="tabTitle">';
1114  if ($picto) $out.=img_picto($title,($pictoisfullpath?'':'object_').$picto,'',$pictoisfullpath).' ';
1115  $out.='<span class="tabTitleText">'.dol_trunc($title,$limittitle).'</span>';
1116  $out.='</a>';
1117  }
1118 
1119  // Define max of key (max may be higher than sizeof because of hole due to module disabling some tabs).
1120  $maxkey=-1;
1121  if (is_array($links) && ! empty($links))
1122  {
1123  $keys=array_keys($links);
1124  if (count($keys)) $maxkey=max($keys);
1125  }
1126 
1127  if (! empty($conf->dol_optimize_smallscreen)) $conf->global->MAIN_MAXTABS_IN_CARD=2;
1128 
1129  // Show tabs
1130  $bactive=false;
1131  // if =0 we don't use the feature
1132  $limittoshow=(empty($conf->global->MAIN_MAXTABS_IN_CARD)?99:$conf->global->MAIN_MAXTABS_IN_CARD);
1133  $displaytab=0;
1134  $nbintab=0;
1135  $popuptab=0; $outmore='';
1136  for ($i = 0 ; $i <= $maxkey ; $i++)
1137  {
1138  if ((is_numeric($active) && $i == $active) || (! empty($links[$i][2]) && ! is_numeric($active) && $active == $links[$i][2]))
1139  {
1140  // If active tab is already present
1141  if ($i >= $limittoshow) $limittoshow--;
1142  }
1143  }
1144 
1145  for ($i = 0 ; $i <= $maxkey ; $i++)
1146  {
1147  if ((is_numeric($active) && $i == $active) || (! empty($links[$i][2]) && ! is_numeric($active) && $active == $links[$i][2]))
1148  {
1149  $isactive=true;
1150  $bactive=true;
1151  }
1152  else
1153  {
1154  $isactive=false;
1155  }
1156 
1157  if ($i < $limittoshow || $isactive)
1158  {
1159  $out.='<div class="inline-block tabsElem'.($isactive ? ' tabsElemActive' : '').((! $isactive && ! empty($conf->global->MAIN_HIDE_INACTIVETAB_ON_PRINT))?' hideonprint':'').'"><!-- id tab = '.(empty($links[$i][2])?'':$links[$i][2]).' -->';
1160  if (isset($links[$i][2]) && $links[$i][2] == 'image')
1161  {
1162  if (!empty($links[$i][0]))
1163  {
1164  $out.='<a class="tabimage" href="'.$links[$i][0].'">'.$links[$i][1].'</a>'."\n";
1165  }
1166  else
1167  {
1168  $out.='<span class="tabspan">'.$links[$i][1].'</span>'."\n";
1169  }
1170  }
1171  else if (! empty($links[$i][1]))
1172  {
1173  //print "x $i $active ".$links[$i][2]." z";
1174  if ($isactive)
1175  {
1176  $out.='<a'.(! empty($links[$i][2])?' id="'.$links[$i][2].'"':'').' class="tabactive tab inline-block" href="'.$links[$i][0].'">';
1177  $out.=$links[$i][1];
1178  $out.='</a>'."\n";
1179  }
1180  else
1181  {
1182  $out.='<a'.(! empty($links[$i][2])?' id="'.$links[$i][2].'"':'').' class="tabunactive tab inline-block" href="'.$links[$i][0].'">';
1183  $out.=$links[$i][1];
1184  $out.='</a>'."\n";
1185  }
1186  }
1187  $out.='</div>';
1188  }
1189  else
1190  {
1191  // The popup with the other tabs
1192  if (! $popuptab)
1193  {
1194  $popuptab=1;
1195  $outmore.='<div class="popuptabset wordwrap">'; // The css used to hide/show popup
1196  }
1197  $outmore.='<div class="popuptab wordwrap" style="display:inherit;">';
1198  if (isset($links[$i][2]) && $links[$i][2] == 'image')
1199  {
1200  if (!empty($links[$i][0]))
1201  $outmore.='<a class="tabimage" href="'.$links[$i][0].'">'.$links[$i][1].'</a>'."\n";
1202  else
1203  $outmore.='<span class="tabspan">'.$links[$i][1].'</span>'."\n";
1204 
1205  }
1206  else if (! empty($links[$i][1]))
1207  {
1208  $outmore.='<a'.(! empty($links[$i][2])?' id="'.$links[$i][2].'"':'').' class="wordwrap inline-block" href="'.$links[$i][0].'">';
1209  $outmore.=preg_replace('/([a-z])\/([a-z])/i', '\\1 / \\2', $links[$i][1]); // Replace x/y with x / y to allow wrap on long composed texts.
1210  $outmore.='</a>'."\n";
1211  }
1212  $outmore.='</div>';
1213 
1214  $nbintab++;
1215  }
1216  $displaytab=$i;
1217  }
1218  if ($popuptab) $outmore.='</div>';
1219 
1220  if ($displaytab > $limittoshow)
1221  {
1222  $left=($langs->trans("DIRECTION") == 'rtl'?'right':'left');
1223  $right=($langs->trans("DIRECTION") == 'rtl'?'left':'right');
1224 
1225  $tabsname=str_replace("@", "", $picto);
1226  $out.='<div id="moretabs'.$tabsname.'" class="inline-block tabsElem">';
1227  $out.='<a href="#" class="tab moretab inline-block tabunactive">'.$langs->trans("More").'... ('.$nbintab.')</a>';
1228  $out.='<div id="moretabsList'.$tabsname.'" style="position: absolute; '.$left.': -999em; text-align: '.$left.'; margin:0px; padding:2px">';
1229  $out.=$outmore;
1230  $out.='</div>';
1231  $out.='<div></div>';
1232  $out.="</div>\n";
1233 
1234  $out.="<script>";
1235  $out.="$('#moretabs".$tabsname."').mouseenter( function() { console.log('mouseenter ".$left."'); $('#moretabsList".$tabsname."').css('".$left."','auto');});";
1236  $out.="$('#moretabs".$tabsname."').mouseleave( function() { console.log('mouseleave ".$left."'); $('#moretabsList".$tabsname."').css('".$left."','-999em');});";
1237  $out.="</script>";
1238  }
1239 
1240  $out.="</div>\n";
1241 
1242  if (! $notab || $notab == -1) $out.="\n".'<div class="tabBar'.($notab == -1 ? '' : ' tabBarWithBottom').'">'."\n";
1243 
1244  $parameters=array('tabname' => $active, 'out' => $out);
1245  $reshook=$hookmanager->executeHooks('printTabsHead',$parameters); // This hook usage is called just before output the head of tabs. Take also a look at "completeTabsHead"
1246  if ($reshook > 0)
1247  {
1248  $out = $hookmanager->resPrint;
1249  }
1250 
1251  return $out;
1252 }
1253 
1260 function dol_fiche_end($notab=0)
1261 {
1262  print dol_get_fiche_end($notab);
1263 }
1264 
1271 function dol_get_fiche_end($notab=0)
1272 {
1273  if (! $notab || $notab == -1) return "\n</div>\n";
1274  else return '';
1275 }
1276 
1296 function dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
1297 {
1298  global $conf, $form, $user, $langs;
1299 
1300  $error = 0;
1301 
1302  $maxvisiblephotos=1;
1303  $showimage=1;
1304  $showbarcode=empty($conf->barcode->enabled)?0:($object->barcode?1:0);
1305  if (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->barcode->lire_advance)) $showbarcode=0;
1306  $modulepart='unknown';
1307 
1308  if ($object->element == 'societe') $modulepart='societe';
1309  if ($object->element == 'contact') $modulepart='contact';
1310  if ($object->element == 'member') $modulepart='memberphoto';
1311  if ($object->element == 'user') $modulepart='userphoto';
1312  if ($object->element == 'product') $modulepart='product';
1313 
1314  if (class_exists("Imagick"))
1315  {
1316  if ($object->element == 'propal') $modulepart='propal';
1317  if ($object->element == 'commande') $modulepart='commande';
1318  if ($object->element == 'facture') $modulepart='facture';
1319  if ($object->element == 'fichinter') $modulepart='ficheinter';
1320  if ($object->element == 'contrat') $modulepart='contract';
1321  if ($object->element == 'supplier_proposal') $modulepart='supplier_proposal';
1322  if ($object->element == 'order_supplier') $modulepart='supplier_order';
1323  if ($object->element == 'invoice_supplier') $modulepart='supplier_invoice';
1324  if ($object->element == 'expensereport') $modulepart='expensereport';
1325  }
1326 
1327  if ($object->element == 'product')
1328  {
1329  $width=80; $cssclass='photoref';
1330  $showimage=$object->is_photo_available($conf->product->multidir_output[$object->entity]);
1331  $maxvisiblephotos=(isset($conf->global->PRODUCT_MAX_VISIBLE_PHOTO)?$conf->global->PRODUCT_MAX_VISIBLE_PHOTO:5);
1332  if ($conf->browser->phone) $maxvisiblephotos=1;
1333  if ($showimage) $morehtmlleft.='<div class="floatleft inline-block valignmiddle divphotoref">'.$object->show_photos($conf->product->multidir_output[$object->entity],'small',$maxvisiblephotos,0,0,0,$width,0).'</div>';
1334  else
1335  {
1336  if (!empty($conf->global->PRODUCT_NODISPLAYIFNOPHOTO)) {
1337  $nophoto='';
1338  $morehtmlleft.='<div class="floatleft inline-block valignmiddle divphotoref"></div>';
1339  }
1340  //elseif ($conf->browser->layout != 'phone') { // Show no photo link
1341  $nophoto='/public/theme/common/nophoto.png';
1342  $morehtmlleft.='<div class="floatleft inline-block valignmiddle divphotoref"><img class="photo'.$modulepart.($cssclass?' '.$cssclass:'').'" alt="No photo" border="0"'.($width?' width="'.$width.'"':'').' src="'.DOL_URL_ROOT.$nophoto.'"></div>';
1343  //}
1344  }
1345  }
1346  else
1347  {
1348  if ($showimage)
1349  {
1350  if ($modulepart != 'unknown')
1351  {
1352  $phototoshow='';
1353  // Check if a preview file is available
1354  if (in_array($modulepart, array('propal', 'commande', 'facture', 'ficheinter', 'contract', 'supplier_order', 'supplier_proposal', 'supplier_invoice', 'expensereport')) && class_exists("Imagick"))
1355  {
1356  $objectref = dol_sanitizeFileName($object->ref);
1357  $dir_output = $conf->$modulepart->dir_output . "/";
1358  if (in_array($modulepart, array('invoice_supplier', 'supplier_invoice')))
1359  {
1360  $subdir = get_exdir($object->id, 2, 0, 0, $object, $modulepart).$objectref; // the objectref dir is not include into get_exdir when used with level=2, so we add it here
1361  }
1362  else
1363  {
1364  $subdir = get_exdir($object->id, 0, 0, 0, $object, $modulepart);
1365  }
1366 
1367  $filepath = $dir_output . $subdir . "/";
1368  $file = $filepath . $objectref . ".pdf";
1369  $relativepath = $subdir.'/'.$objectref.'.pdf';
1370 
1371  // Define path to preview pdf file (preview precompiled "file.ext" are "file.ext_preview.png")
1372  $fileimage = $file.'_preview.png'; // If PDF has 1 page
1373  $fileimagebis = $file.'_preview-0.png'; // If PDF has more than one page
1374  $relativepathimage = $relativepath.'_preview.png';
1375 
1376  // Si fichier PDF existe
1377  if (file_exists($file))
1378  {
1379  $encfile = urlencode($file);
1380  // Conversion du PDF en image png si fichier png non existant
1381  if ( (! file_exists($fileimage) || (filemtime($fileimage) < filemtime($file)))
1382  && (! file_exists($fileimagebis) || (filemtime($fileimagebis) < filemtime($file)))
1383  )
1384  {
1385  if (empty($conf->global->MAIN_DISABLE_PDF_THUMBS)) // If you experienc trouble with pdf thumb generation and imagick, you can disable here.
1386  {
1387  $ret = dol_convert_file($file, 'png', $fileimage);
1388  if ($ret < 0) $error++;
1389  }
1390  }
1391 
1392  $heightforphotref=70;
1393  if (! empty($conf->dol_optimize_smallscreen)) $heightforphotref=60;
1394  // Si fichier png PDF d'1 page trouve
1395  if (file_exists($fileimage))
1396  {
1397  $phototoshow = '<div class="floatleft inline-block valignmiddle divphotoref"><div class="photoref">';
1398  $phototoshow.= '<img height="'.$heightforphotref.'" class="photo photowithmargin photowithborder" src="'.DOL_URL_ROOT . '/viewimage.php?modulepart=apercu'.$modulepart.'&amp;file='.urlencode($relativepathimage).'">';
1399  $phototoshow.= '</div></div>';
1400  }
1401  // Si fichier png PDF de plus d'1 page trouve
1402  elseif (file_exists($fileimagebis))
1403  {
1404  $preview = preg_replace('/\.png/','',$relativepathimage) . "-0.png";
1405  $phototoshow = '<div class="floatleft inline-block valignmiddle divphotoref"><div class="photoref">';
1406  $phototoshow.= '<img height="'.$heightforphotref.'" class="photo photowithmargin photowithborder" src="'.DOL_URL_ROOT . '/viewimage.php?modulepart=apercu'.$modulepart.'&amp;file='.urlencode($preview).'"><p>';
1407  $phototoshow.= '</div></div>';
1408  }
1409  }
1410  }
1411  else if (! $phototoshow)
1412  {
1413  $phototoshow = $form->showphoto($modulepart,$object,0,0,0,'photoref','small',1,0,$maxvisiblephotos);
1414  }
1415 
1416  if ($phototoshow)
1417  {
1418  $morehtmlleft.='<div class="floatleft inline-block valignmiddle divphotoref">';
1419  $morehtmlleft.=$phototoshow;
1420  $morehtmlleft.='</div>';
1421  }
1422  }
1423 
1424  if (! $phototoshow) // Show No photo link (picto of pbject)
1425  {
1426  $morehtmlleft.='<div class="floatleft inline-block valignmiddle divphotoref">';
1427  if ($object->element == 'action')
1428  {
1429  $width=80;
1430  $cssclass='photorefcenter';
1431  $nophoto=img_picto('', 'title_agenda', '', false, 1);
1432  }
1433  else
1434  {
1435  $width=14; $cssclass='photorefcenter';
1436  $picto = $object->picto;
1437  if ($object->element == 'project' && ! $object->public) $picto = 'project'; // instead of projectpub
1438  $nophoto=img_picto('', 'object_'.$picto, '', false, 1);
1439  }
1440  $morehtmlleft.='<!-- No photo to show -->';
1441  $morehtmlleft.='<div class="floatleft inline-block valignmiddle divphotoref"><div class="photoref"><img class="photo'.$modulepart.($cssclass?' '.$cssclass:'').'" alt="No photo" border="0"'.($width?' width="'.$width.'"':'').' src="'.$nophoto.'"></div></div>';
1442 
1443  $morehtmlleft.='</div>';
1444  }
1445  }
1446  }
1447 
1448  if ($showbarcode) $morehtmlleft.='<div class="floatleft inline-block valignmiddle divphotoref">'.$form->showbarcode($object).'</div>';
1449 
1450  if ($object->element == 'societe')
1451  {
1452  if (! empty($conf->use_javascript_ajax) && $user->rights->societe->creer && ! empty($conf->global->MAIN_DIRECT_STATUS_UPDATE))
1453  {
1454  $morehtmlstatus.=ajax_object_onoff($object, 'status', 'status', 'InActivity', 'ActivityCeased');
1455  }
1456  else {
1457  $morehtmlstatus.=$object->getLibStatut(6);
1458  }
1459  }
1460  elseif ($object->element == 'product')
1461  {
1462  //$morehtmlstatus.=$langs->trans("Status").' ('.$langs->trans("Sell").') ';
1463  if (! empty($conf->use_javascript_ajax) && $user->rights->produit->creer && ! empty($conf->global->MAIN_DIRECT_STATUS_UPDATE)) {
1464  $morehtmlstatus.=ajax_object_onoff($object, 'status', 'tosell', 'ProductStatusOnSell', 'ProductStatusNotOnSell');
1465  } else {
1466  $morehtmlstatus.='<span class="statusrefsell">'.$object->getLibStatut(5,0).'</span>';
1467  }
1468  $morehtmlstatus.=' &nbsp; ';
1469  //$morehtmlstatus.=$langs->trans("Status").' ('.$langs->trans("Buy").') ';
1470  if (! empty($conf->use_javascript_ajax) && $user->rights->produit->creer && ! empty($conf->global->MAIN_DIRECT_STATUS_UPDATE)) {
1471  $morehtmlstatus.=ajax_object_onoff($object, 'status_buy', 'tobuy', 'ProductStatusOnBuy', 'ProductStatusNotOnBuy');
1472  } else {
1473  $morehtmlstatus.='<span class="statusrefbuy">'.$object->getLibStatut(5,1).'</span>';
1474  }
1475  }
1476  elseif (in_array($object->element, array('facture', 'invoice', 'invoice_supplier', 'chargesociales', 'loan')))
1477  {
1478  $tmptxt=$object->getLibStatut(6, $object->totalpaye);
1479  if (empty($tmptxt) || $tmptxt == $object->getLibStatut(3) || $conf->browser->layout=='phone') $tmptxt=$object->getLibStatut(5, $object->totalpaye);
1480  $morehtmlstatus.=$tmptxt;
1481  }
1482  elseif ($object->element == 'contrat' || $object->element == 'contract')
1483  {
1484  if ($object->statut == 0) $morehtmlstatus.=$object->getLibStatut(5);
1485  else $morehtmlstatus.=$object->getLibStatut(4);
1486  }
1487  elseif ($object->element == 'facturerec')
1488  {
1489  if ($object->frequency == 0) $morehtmlstatus.=$object->getLibStatut(2);
1490  else $morehtmlstatus.=$object->getLibStatut(5);
1491  }
1492  else { // Generic case
1493  $tmptxt=$object->getLibStatut(6);
1494  if (empty($tmptxt) || $tmptxt == $object->getLibStatut(3) || $conf->browser->layout=='phone') $tmptxt=$object->getLibStatut(5);
1495  $morehtmlstatus.=$tmptxt;
1496  }
1497  if (! empty($object->name_alias)) $morehtmlref.='<div class="refidno">'.$object->name_alias.'</div>'; // For thirdparty
1498 
1499  // Add label
1500  if ($object->element == 'product' || $object->element == 'bank_account' || $object->element == 'project_task')
1501  {
1502  if (! empty($object->label)) $morehtmlref.='<div class="refidno">'.$object->label.'</div>';
1503  }
1504 
1505  if (method_exists($object, 'getBannerAddress') && $object->element != 'product' && $object->element != 'bookmark' && $object->element != 'ecm_directories' && $object->element != 'ecm_files')
1506  {
1507  $morehtmlref.='<div class="refidno">';
1508  $morehtmlref.=$object->getBannerAddress('refaddress',$object);
1509  $morehtmlref.='</div>';
1510  }
1511  if (! empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && in_array($object->element, array('societe', 'contact', 'member', 'product')))
1512  {
1513  $morehtmlref.='<div style="clear: both;"></div><div class="refidno">';
1514  $morehtmlref.=$langs->trans("TechnicalID").': '.$object->id;
1515  $morehtmlref.='</div>';
1516  }
1517 
1518  print '<div class="'.($onlybanner?'arearefnobottom ':'arearef ').'heightref valignmiddle" width="100%">';
1519  print $form->showrefnav($object, $paramid, $morehtml, $shownav, $fieldid, $fieldref, $morehtmlref, $moreparam, $nodbprefix, $morehtmlleft, $morehtmlstatus, $morehtmlright);
1520  print '</div>';
1521  print '<div class="underrefbanner clearboth"></div>';
1522 }
1523 
1532 function fieldLabel($langkey, $fieldkey, $fieldrequired=0)
1533 {
1534  global $conf, $langs;
1535  $ret='';
1536  if ($fieldrequired) $ret.='<span class="fieldrequired">';
1537  if (($conf->dol_use_jmobile != 4)) $ret.='<label for="'.$fieldkey.'">';
1538  $ret.=$langs->trans($langkey);
1539  if (($conf->dol_use_jmobile != 4)) $ret.='</label>';
1540  if ($fieldrequired) $ret.='</span>';
1541  return $ret;
1542 }
1543 
1551 function dol_bc($var,$moreclass='')
1552 {
1553  global $bc;
1554  $ret=' '.$bc[$var];
1555  if ($moreclass) $ret=preg_replace('/class=\"/','class="'.$moreclass.' ',$ret);
1556  return $ret;
1557 }
1558 
1570 function dol_format_address($object, $withcountry=0, $sep="\n", $outputlangs='', $mode=0)
1571 {
1572  global $conf,$langs;
1573 
1574  $ret='';
1575  $countriesusingstate=array('AU','CA','US','IN','GB','ES','UK','TR'); // See also MAIN_FORCE_STATE_INTO_ADDRESS
1576 
1577  // Address
1578  if (empty($mode)) {
1579  $ret .= $object->address;
1580  }
1581  // Zip/Town/State
1582  if (in_array($object->country_code,array('AU', 'CA', 'US')) || ! empty($conf->global->MAIN_FORCE_STATE_INTO_ADDRESS)) // US: title firstname name \n address lines \n town, state, zip \n country
1583  {
1584  $ret .= ($ret ? $sep : '' ).$object->town;
1585  if ($object->state)
1586  {
1587  $ret.=($ret?", ":'').$object->state;
1588  }
1589  if ($object->zip) $ret .= ($ret?", ":'').$object->zip;
1590  }
1591  else if (in_array($object->country_code,array('GB','UK'))) // UK: title firstname name \n address lines \n town state \n zip \n country
1592  {
1593  $ret .= ($ret ? $sep : '' ).$object->town;
1594  if ($object->state)
1595  {
1596  $ret.=($ret?", ":'').$object->state;
1597  }
1598  if ($object->zip) $ret .= ($ret ? $sep : '' ).$object->zip;
1599  }
1600  else if (in_array($object->country_code,array('ES','TR'))) // ES: title firstname name \n address lines \n zip town \n state \n country
1601  {
1602  $ret .= ($ret ? $sep : '' ).$object->zip;
1603  $ret .= ($object->town?(($object->zip?' ':'').$object->town):'');
1604  if ($object->state)
1605  {
1606  $ret.="\n".$object->state;
1607  }
1608  }
1609  else if (in_array($object->country_code,array('IT'))) // IT: tile firstname name\n address lines \n zip (Code Departement) \n country
1610  {
1611  $ret .= ($ret ? $sep : '' ).$object->zip;
1612  $ret .= ($object->town?(($object->zip?' ':'').$object->town):'');
1613  $ret .= ($object->departement_id?(' ('.($object->departement_id).')'):'');
1614  }
1615  else // Other: title firstname name \n address lines \n zip town \n country
1616  {
1617  $ret .= $object->zip ? (($ret ? $sep : '' ).$object->zip) : '';
1618  $ret .= ($object->town?(($object->zip?' ':($ret ? $sep : '' )).$object->town):'');
1619  if ($object->state && in_array($object->country_code,$countriesusingstate))
1620  {
1621  $ret.=($ret?", ":'').$object->state;
1622  }
1623  }
1624  if (! is_object($outputlangs)) $outputlangs=$langs;
1625  if ($withcountry) $ret.=($object->country_code?($ret?$sep:'').$outputlangs->convToOutputCharset($outputlangs->transnoentitiesnoconv("Country".$object->country_code)):'');
1626 
1627  return $ret;
1628 }
1629 
1630 
1631 
1640 function dol_strftime($fmt, $ts=false, $is_gmt=false)
1641 {
1642  if ((abs($ts) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range
1643  return ($is_gmt)? @gmstrftime($fmt,$ts): @strftime($fmt,$ts);
1644  }
1645  else return 'Error date into a not supported range';
1646 }
1647 
1669 function dol_print_date($time,$format='',$tzoutput='tzserver',$outputlangs='',$encodetooutput=false)
1670 {
1671  global $conf,$langs;
1672 
1673  // Clean parameters
1674  $to_gmt=false;
1675  $offsettz=$offsetdst=0;
1676  if ($tzoutput)
1677  {
1678  $to_gmt=true; // For backward compatibility
1679  if (is_string($tzoutput))
1680  {
1681  if ($tzoutput == 'tzserver')
1682  {
1683  $to_gmt=false;
1684  $offsettzstring=@date_default_timezone_get(); // Example 'Europe/Berlin' or 'Indian/Reunion'
1685  $offsettz=0;
1686  $offsetdst=0;
1687  }
1688  elseif ($tzoutput == 'tzuser' || $tzoutput == 'tzuserrel')
1689  {
1690  $to_gmt=true;
1691  $offsettzstring=(empty($_SESSION['dol_tz_string'])?'UTC':$_SESSION['dol_tz_string']); // Example 'Europe/Berlin' or 'Indian/Reunion'
1692  $offsettz=(empty($_SESSION['dol_tz'])?0:$_SESSION['dol_tz'])*60*60; // Will not be used anymore
1693  $offsetdst=(empty($_SESSION['dol_dst'])?0:$_SESSION['dol_dst'])*60*60; // Will not be used anymore
1694  }
1695  }
1696  }
1697  if (! is_object($outputlangs)) $outputlangs=$langs;
1698  if (! $format) $format='daytextshort';
1699  $reduceformat=(! empty($conf->dol_optimize_smallscreen) && in_array($format,array('day','dayhour')))?1:0;
1700  $formatwithoutreduce = preg_replace('/reduceformat/','',$format);
1701  if ($formatwithoutreduce != $format) { $format = $formatwithoutreduce; $reduceformat=1; } // so format 'dayreduceformat' is processed like day
1702 
1703  // Change predefined format into computer format. If found translation in lang file we use it, otherwise we use default.
1704  // TODO Add format daysmallyear and dayhoursmallyear
1705  if ($format == 'day') $format=($outputlangs->trans("FormatDateShort")!="FormatDateShort"?$outputlangs->trans("FormatDateShort"):$conf->format_date_short);
1706  else if ($format == 'hour') $format=($outputlangs->trans("FormatHourShort")!="FormatHourShort"?$outputlangs->trans("FormatHourShort"):$conf->format_hour_short);
1707  else if ($format == 'hourduration') $format=($outputlangs->trans("FormatHourShortDuration")!="FormatHourShortDuration"?$outputlangs->trans("FormatHourShortDuration"):$conf->format_hour_short_duration);
1708  else if ($format == 'daytext') $format=($outputlangs->trans("FormatDateText")!="FormatDateText"?$outputlangs->trans("FormatDateText"):$conf->format_date_text);
1709  else if ($format == 'daytextshort') $format=($outputlangs->trans("FormatDateTextShort")!="FormatDateTextShort"?$outputlangs->trans("FormatDateTextShort"):$conf->format_date_text_short);
1710  else if ($format == 'dayhour') $format=($outputlangs->trans("FormatDateHourShort")!="FormatDateHourShort"?$outputlangs->trans("FormatDateHourShort"):$conf->format_date_hour_short);
1711  else if ($format == 'dayhoursec') $format=($outputlangs->trans("FormatDateHourSecShort")!="FormatDateHourSecShort"?$outputlangs->trans("FormatDateHourSecShort"):$conf->format_date_hour_sec_short);
1712  else if ($format == 'dayhourtext') $format=($outputlangs->trans("FormatDateHourText")!="FormatDateHourText"?$outputlangs->trans("FormatDateHourText"):$conf->format_date_hour_text);
1713  else if ($format == 'dayhourtextshort') $format=($outputlangs->trans("FormatDateHourTextShort")!="FormatDateHourTextShort"?$outputlangs->trans("FormatDateHourTextShort"):$conf->format_date_hour_text_short);
1714  // Format not sensitive to language
1715  else if ($format == 'dayhourlog') $format='%Y%m%d%H%M%S';
1716  else if ($format == 'dayhourldap') $format='%Y%m%d%H%M%SZ';
1717  else if ($format == 'dayhourxcard') $format='%Y%m%dT%H%M%SZ';
1718  else if ($format == 'dayxcard') $format='%Y%m%d';
1719  else if ($format == 'dayrfc') $format='%Y-%m-%d'; // DATE_RFC3339
1720  else if ($format == 'dayhourrfc') $format='%Y-%m-%dT%H:%M:%SZ'; // DATETIME RFC3339
1721  else if ($format == 'standard') $format='%Y-%m-%d %H:%M:%S';
1722 
1723  if ($reduceformat)
1724  {
1725  $format=str_replace('%Y','%y',$format);
1726  $format=str_replace('yyyy','yy',$format);
1727  }
1728 
1729  // If date undefined or "", we return ""
1730  if (dol_strlen($time) == 0) return ''; // $time=0 allowed (it means 01/01/1970 00:00:00)
1731 
1732  // Clean format
1733  if (preg_match('/%b/i',$format)) // There is some text to translate
1734  {
1735  // We inhibate translation to text made by strftime functions. We will use trans instead later.
1736  $format=str_replace('%b','__b__',$format);
1737  $format=str_replace('%B','__B__',$format);
1738  }
1739  if (preg_match('/%a/i',$format)) // There is some text to translate
1740  {
1741  // We inhibate translation to text made by strftime functions. We will use trans instead later.
1742  $format=str_replace('%a','__a__',$format);
1743  $format=str_replace('%A','__A__',$format);
1744  }
1745 
1746  // Analyze date
1747  if (preg_match('/^([0-9]+)\-([0-9]+)\-([0-9]+) ?([0-9]+)?:?([0-9]+)?:?([0-9]+)?/i',$time,$reg)
1748  || preg_match('/^([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])$/i',$time,$reg)) // Deprecated. Ex: 1970-01-01, 1970-01-01 01:00:00, 19700101010000
1749  {
1750  // TODO Remove this.
1751  // This part of code should not be used.
1752  dol_syslog("Functions.lib::dol_print_date function call with deprecated value of time in page ".$_SERVER["PHP_SELF"], LOG_ERR);
1753  // Date has format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS' or 'YYYYMMDDHHMMSS'
1754  $syear = (! empty($reg[1]) ? $reg[1] : '');
1755  $smonth = (! empty($reg[2]) ? $reg[2] : '');
1756  $sday = (! empty($reg[3]) ? $reg[3] : '');
1757  $shour = (! empty($reg[4]) ? $reg[4] : '');
1758  $smin = (! empty($reg[5]) ? $reg[5] : '');
1759  $ssec = (! empty($reg[6]) ? $reg[6] : '');
1760 
1761  $time=dol_mktime($shour,$smin,$ssec,$smonth,$sday,$syear,true);
1762  $ret=adodb_strftime($format, $time+$offsettz+$offsetdst, $to_gmt);
1763  }
1764  else
1765  {
1766  // Date is a timestamps
1767  if ($time < 100000000000) // Protection against bad date values
1768  {
1769  $timetouse = $time+$offsettz+$offsetdst; // TODO Replace this with function Date PHP. We also should not use anymore offsettz and offsetdst but only offsettzstring.
1770 
1771  $ret=adodb_strftime($format, $timetouse, $to_gmt);
1772  }
1773  else $ret='Bad value '.$time.' for date';
1774  }
1775 
1776  if (preg_match('/__b__/i',$format))
1777  {
1778  $timetouse = $time+$offsettz+$offsetdst; // TODO Replace this with function Date PHP. We also should not use anymore offsettz and offsetdst but only offsettzstring.
1779 
1780  // Here ret is string in PHP setup language (strftime was used). Now we convert to $outputlangs.
1781  $month=adodb_strftime('%m', $timetouse);
1782  $month=sprintf("%02d", $month); // $month may be return with format '06' on some installation and '6' on other, so we force it to '06'.
1783  if ($encodetooutput)
1784  {
1785  $monthtext=$outputlangs->transnoentities('Month'.$month);
1786  $monthtextshort=$outputlangs->transnoentities('MonthShort'.$month);
1787  }
1788  else
1789  {
1790  $monthtext=$outputlangs->transnoentitiesnoconv('Month'.$month);
1791  $monthtextshort=$outputlangs->transnoentitiesnoconv('MonthShort'.$month);
1792  }
1793  //print 'monthtext='.$monthtext.' monthtextshort='.$monthtextshort;
1794  $ret=str_replace('__b__',$monthtextshort,$ret);
1795  $ret=str_replace('__B__',$monthtext,$ret);
1796  //print 'x'.$outputlangs->charset_output.'-'.$ret.'x';
1797  //return $ret;
1798  }
1799  if (preg_match('/__a__/i',$format))
1800  {
1801  $timetouse = $time+$offsettz+$offsetdst; // TODO Replace this with function Date PHP. We also should not use anymore offsettz and offsetdst but only offsettzstring.
1802 
1803  $w=adodb_strftime('%w', $timetouse); // TODO Replace this with function Date PHP. We also should not use anymore offsettz and offsetdst but only offsettzstring.
1804  $dayweek=$outputlangs->transnoentitiesnoconv('Day'.$w);
1805  $ret=str_replace('__A__',$dayweek,$ret);
1806  $ret=str_replace('__a__',dol_substr($dayweek,0,3),$ret);
1807  }
1808 
1809  return $ret;
1810 }
1811 
1812 
1846 function dol_getdate($timestamp,$fast=false)
1847 {
1848  global $conf;
1849 
1850  $usealternatemethod=false;
1851  if ($timestamp <= 0) $usealternatemethod=true; // <= 1970
1852  if ($timestamp >= 2145913200) $usealternatemethod=true; // >= 2038
1853 
1854  if ($usealternatemethod)
1855  {
1856  $arrayinfo=adodb_getdate($timestamp,$fast);
1857  }
1858  else
1859  {
1860  $arrayinfo=getdate($timestamp);
1861  }
1862 
1863  return $arrayinfo;
1864 }
1865 
1885 function dol_mktime($hour,$minute,$second,$month,$day,$year,$gm=false,$check=1)
1886 {
1887  global $conf;
1888  //print "- ".$hour.",".$minute.",".$second.",".$month.",".$day.",".$year.",".$_SERVER["WINDIR"]." -";
1889 
1890  // Clean parameters
1891  if ($hour == -1 || empty($hour)) $hour=0;
1892  if ($minute == -1 || empty($minute)) $minute=0;
1893  if ($second == -1 || empty($second)) $second=0;
1894 
1895  // Check parameters
1896  if ($check)
1897  {
1898  if (! $month || ! $day) return '';
1899  if ($day > 31) return '';
1900  if ($month > 12) return '';
1901  if ($hour < 0 || $hour > 24) return '';
1902  if ($minute< 0 || $minute > 60) return '';
1903  if ($second< 0 || $second > 60) return '';
1904  }
1905 
1906  if (method_exists('DateTime','getTimestamp'))
1907  {
1908  if (empty($gm) || $gm === 'server')
1909  {
1910  $default_timezone=@date_default_timezone_get(); // Example 'Europe/Berlin'
1911  $localtz = new DateTimeZone($default_timezone);
1912  }
1913  else if ($gm === 'user')
1914  {
1915  // We use dol_tz_string first because it is more reliable.
1916  $default_timezone=(empty($_SESSION["dol_tz_string"])?@date_default_timezone_get():$_SESSION["dol_tz_string"]); // Example 'Europe/Berlin'
1917  try {
1918  $localtz = new DateTimeZone($default_timezone);
1919  }
1920  catch(Exception $e)
1921  {
1922  dol_syslog("Warning dol_tz_string contains an invalid value ".$_SESSION["dol_tz_string"], LOG_WARNING);
1923  $default_timezone=@date_default_timezone_get();
1924  }
1925  }
1926  else if (strrpos($gm, "tz,") !== false)
1927  {
1928  $timezone=str_replace("tz,", "", $gm); // Example 'tz,Europe/Berlin'
1929  try
1930  {
1931  $localtz = new DateTimeZone($timezone);
1932  }
1933  catch(Exception $e)
1934  {
1935  dol_syslog("Warning passed timezone contains an invalid value ".$timezone, LOG_WARNING);
1936  }
1937  }
1938 
1939  if (empty($localtz)) {
1940  $localtz = new DateTimeZone('UTC');
1941  }
1942  //var_dump($localtz);
1943  //var_dump($year.'-'.$month.'-'.$day.'-'.$hour.'-'.$minute);
1944  $dt = new DateTime(null,$localtz);
1945  $dt->setDate($year,$month,$day);
1946  $dt->setTime((int) $hour, (int) $minute, (int) $second);
1947  $date=$dt->getTimestamp(); // should include daylight saving time
1948  //var_dump($date);
1949  return $date;
1950  }
1951  else
1952  {
1953  dol_print_error('','PHP version must be 5.3+');
1954  return '';
1955  }
1956 }
1957 
1958 
1968 function dol_now($mode='gmt')
1969 {
1970  $ret=0;
1971 
1972  // Note that gmmktime and mktime return same value (GMT) when used without parameters
1973  //if ($mode == 'gmt') $ret=gmmktime(); // Strict Standards: gmmktime(): You should be using the time() function instead
1974  if ($mode == 'gmt') $ret=time(); // Time for now at greenwich.
1975  else if ($mode == 'tzserver') // Time for now with PHP server timezone added
1976  {
1977  require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
1978  $tzsecond=getServerTimeZoneInt('now'); // Contains tz+dayling saving time
1979  $ret=(int) dol_now('gmt')+($tzsecond*3600);
1980  }
1981  /*else if ($mode == 'tzref') // Time for now with parent company timezone is added
1982  {
1983  require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
1984  $tzsecond=getParentCompanyTimeZoneInt(); // Contains tz+dayling saving time
1985  $ret=dol_now('gmt')+($tzsecond*3600);
1986  }*/
1987  else if ($mode == 'tzuser') // Time for now with user timezone added
1988  {
1989  //print 'eeee'.time().'-'.mktime().'-'.gmmktime();
1990  $offsettz=(empty($_SESSION['dol_tz'])?0:$_SESSION['dol_tz'])*60*60;
1991  $offsetdst=(empty($_SESSION['dol_dst'])?0:$_SESSION['dol_dst'])*60*60;
1992  $ret=(int) dol_now('gmt')+($offsettz+$offsetdst);
1993  }
1994 
1995  return $ret;
1996 }
1997 
1998 
2007 function dol_print_size($size,$shortvalue=0,$shortunit=0)
2008 {
2009  global $conf,$langs;
2010  $level=1024;
2011 
2012  if (! empty($conf->dol_optimize_smallscreen)) $shortunit=1;
2013 
2014  // Set value text
2015  if (empty($shortvalue) || $size < ($level*10))
2016  {
2017  $ret=$size;
2018  $textunitshort=$langs->trans("b");
2019  $textunitlong=$langs->trans("Bytes");
2020  }
2021  else
2022  {
2023  $ret=round($size/$level,0);
2024  $textunitshort=$langs->trans("Kb");
2025  $textunitlong=$langs->trans("KiloBytes");
2026  }
2027  // Use long or short text unit
2028  if (empty($shortunit)) { $ret.=' '.$textunitlong; }
2029  else { $ret.=' '.$textunitshort; }
2030 
2031  return $ret;
2032 }
2033 
2043 function dol_print_url($url,$target='_blank',$max=32,$withpicto=0)
2044 {
2045  global $langs;
2046 
2047  if (empty($url)) return '';
2048 
2049  $link='<a href="';
2050  if (! preg_match('/^http/i',$url)) $link.='http://';
2051  $link.=$url;
2052  $link.='"';
2053  if ($target) $link.=' target="'.$target.'"';
2054  $link.='>';
2055  if (! preg_match('/^http/i',$url)) $link.='http://';
2056  $link.=dol_trunc($url,$max);
2057  $link.='</a>';
2058  return '<div class="nospan float" style="margin-right: 10px">'.($withpicto?img_picto($langs->trans("Url"), 'object_globe.png').' ':'').$link.'</div>';
2059 }
2060 
2073 function dol_print_email($email,$cid=0,$socid=0,$addlink=0,$max=64,$showinvalid=1,$withpicto=0)
2074 {
2075  global $conf,$user,$langs;
2076 
2077  $newemail=$email;
2078 
2079  if (empty($email)) return '&nbsp;';
2080 
2081  if (! empty($addlink))
2082  {
2083  $newemail='<a style="text-overflow: ellipsis;" href="';
2084  if (! preg_match('/^mailto:/i',$email)) $newemail.='mailto:';
2085  $newemail.=$email;
2086  $newemail.='">';
2087  $newemail.=dol_trunc($email,$max);
2088  $newemail.='</a>';
2089  if ($showinvalid && ! isValidEmail($email))
2090  {
2091  $langs->load("errors");
2092  $newemail.=img_warning($langs->trans("ErrorBadEMail",$email));
2093  }
2094 
2095  if (($cid || $socid) && ! empty($conf->agenda->enabled) && $user->rights->agenda->myactions->create)
2096  {
2097  $type='AC_EMAIL'; $link='';
2098  if (! empty($conf->global->AGENDA_ADDACTIONFOREMAIL)) $link='<a href="'.DOL_URL_ROOT.'/comm/action/card.php?action=create&amp;backtopage=1&amp;actioncode='.$type.'&amp;contactid='.$cid.'&amp;socid='.$socid.'">'.img_object($langs->trans("AddAction"),"calendar").'</a>';
2099  if ($link) $newemail='<div>'.$newemail.' '.$link.'</div>';
2100  }
2101  }
2102  else
2103  {
2104  if ($showinvalid && ! isValidEmail($email))
2105  {
2106  $langs->load("errors");
2107  $newemail.=img_warning($langs->trans("ErrorBadEMail",$email));
2108  }
2109  }
2110  return '<div class="nospan float" style="margin-right: 10px">'.($withpicto?img_picto($langs->trans("EMail"), 'object_email.png').' ':'').$newemail.'</div>';
2111 }
2112 
2123 function dol_print_skype($skype,$cid=0,$socid=0,$addlink=0,$max=64)
2124 {
2125  global $conf,$user,$langs;
2126 
2127  $newskype=$skype;
2128 
2129  if (empty($skype)) return '&nbsp;';
2130 
2131  if (! empty($addlink))
2132  {
2133  $newskype =img_picto($langs->trans("Skype"), 'object_skype.png');
2134  $newskype.= '&nbsp;';
2135  $newskype.=dol_trunc($skype,$max);
2136  $newskype.= '&nbsp;';
2137  $newskype.='<a href="skype:';
2138  $newskype.=dol_trunc($skype,$max);
2139  $newskype.='?call" alt="'.$langs->trans("Call").'&nbsp;'.$skype.'" title="'.$langs->trans("Call").'&nbsp;'.$skype.'">';
2140  $newskype.='<img src="'.DOL_URL_ROOT.'/theme/common/skype_callbutton.png" border="0">';
2141  $newskype.='</a>&nbsp;&nbsp;&nbsp;<a href="skype:';
2142  $newskype.=dol_trunc($skype,$max);
2143  $newskype.='?chat" alt="'.$langs->trans("Chat").'&nbsp;'.$skype.'" title="'.$langs->trans("Chat").'&nbsp;'.$skype.'">';
2144  $newskype.='<img src="'.DOL_URL_ROOT.'/theme/common/skype_chatbutton.png" border="0">';
2145  $newskype.='</a>';
2146 
2147  if (($cid || $socid) && ! empty($conf->agenda->enabled) && $user->rights->agenda->myactions->create)
2148  {
2149  $type='AC_SKYPE'; $link='';
2150  if (! empty($conf->global->AGENDA_ADDACTIONFORSKYPE)) $link='<a href="'.DOL_URL_ROOT.'/comm/action/card.php?action=create&amp;backtopage=1&amp;actioncode='.$type.'&amp;contactid='.$cid.'&amp;socid='.$socid.'">'.img_object($langs->trans("AddAction"),"calendar").'</a>';
2151  $newskype='<div class="divskype nowrap">'.$newskype.($link?' '.$link:'').'</div>';
2152  }
2153  }
2154  else
2155  {
2156  $langs->load("errors");
2157  $newskype.=img_warning($langs->trans("ErrorBadSkype",$skype));
2158  }
2159  return $newskype;
2160 }
2161 
2176 function dol_print_phone($phone,$countrycode='',$cid=0,$socid=0,$addlink='',$separ="&nbsp;",$withpicto='',$titlealt='',$adddivfloat=0)
2177 {
2178  global $conf,$user,$langs,$mysoc;
2179 
2180  // Clean phone parameter
2181  $phone = preg_replace("/[\s.-]/","",trim($phone));
2182  if (empty($phone)) { return ''; }
2183  if (empty($countrycode)) $countrycode=$mysoc->country_code;
2184 
2185  // Short format for small screens
2186  if ($conf->dol_optimize_smallscreen) $separ='';
2187 
2188  $newphone=$phone;
2189  if (strtoupper($countrycode) == "FR")
2190  {
2191  // France
2192  if (dol_strlen($phone) == 10) {
2193  $newphone=substr($newphone,0,2).$separ.substr($newphone,2,2).$separ.substr($newphone,4,2).$separ.substr($newphone,6,2).$separ.substr($newphone,8,2);
2194  }
2195  elseif (dol_strlen($newphone) == 7)
2196  {
2197  $newphone=substr($newphone,0,3).$separ.substr($newphone,3,2).$separ.substr($newphone,5,2);
2198  }
2199  elseif (dol_strlen($newphone) == 9)
2200  {
2201  $newphone=substr($newphone,0,2).$separ.substr($newphone,2,3).$separ.substr($newphone,5,2).$separ.substr($newphone,7,2);
2202  }
2203  elseif (dol_strlen($newphone) == 11)
2204  {
2205  $newphone=substr($newphone,0,3).$separ.substr($newphone,3,2).$separ.substr($newphone,5,2).$separ.substr($newphone,7,2).$separ.substr($newphone,9,2);
2206  }
2207  elseif (dol_strlen($newphone) == 12)
2208  {
2209  $newphone=substr($newphone,0,4).$separ.substr($newphone,4,2).$separ.substr($newphone,6,2).$separ.substr($newphone,8,2).$separ.substr($newphone,10,2);
2210  }
2211  }
2212 
2213  if (strtoupper($countrycode) == "CA")
2214  {
2215  if (dol_strlen($phone) == 10) {
2216  $newphone=($separ!=''?'(':'').substr($newphone,0,3).($separ!=''?')':'').$separ.substr($newphone,3,3).($separ!=''?'-':'').substr($newphone,6,4);
2217  }
2218  }
2219 
2220  if (! empty($addlink)) // Link on phone number (+ link to add action if conf->global->AGENDA_ADDACTIONFORPHONE set)
2221  {
2222  if (! empty($conf->browser->phone) || (! empty($conf->clicktodial->enabled) && ! empty($conf->global->CLICKTODIAL_USE_TEL_LINK_ON_PHONE_NUMBERS))) // If phone or option for, we use link of phone
2223  {
2224  $newphone ='<a href="tel:'.$phone.'"';
2225  $newphone.='>'.$phone.'</a>';
2226  }
2227  else if (! empty($conf->clicktodial->enabled) && $addlink == 'AC_TEL') // If click to dial, we use click to dial url
2228  {
2229  if (empty($user->clicktodial_loaded)) $user->fetch_clicktodial();
2230 
2231  // Define urlmask
2232  $urlmask='ErrorClickToDialModuleNotConfigured';
2233  if (! empty($conf->global->CLICKTODIAL_URL)) $urlmask=$conf->global->CLICKTODIAL_URL;
2234  if (! empty($user->clicktodial_url)) $urlmask=$user->clicktodial_url;
2235 
2236  $clicktodial_poste=(! empty($user->clicktodial_poste)?urlencode($user->clicktodial_poste):'');
2237  $clicktodial_login=(! empty($user->clicktodial_login)?urlencode($user->clicktodial_login):'');
2238  $clicktodial_password=(! empty($user->clicktodial_password)?urlencode($user->clicktodial_password):'');
2239  // This line is for backward compatibility
2240  $url = sprintf($urlmask, urlencode($phone), $clicktodial_poste, $clicktodial_login, $clicktodial_password);
2241  // Thoose lines are for substitution
2242  $substitarray=array('__PHONEFROM__'=>$clicktodial_poste,
2243  '__PHONETO__'=>urlencode($phone),
2244  '__LOGIN__'=>$clicktodial_login,
2245  '__PASS__'=>$clicktodial_password);
2246  $url = make_substitutions($url, $substitarray);
2247  $newphonesav=$newphone;
2248  $newphone ='<a href="'.$url.'"';
2249  if (! empty($conf->global->CLICKTODIAL_FORCENEWTARGET)) $newphone.=' target="_blank"';
2250  $newphone.='>'.$newphonesav.'</a>';
2251  }
2252 
2253  //if (($cid || $socid) && ! empty($conf->agenda->enabled) && $user->rights->agenda->myactions->create)
2254  if (! empty($conf->agenda->enabled) && $user->rights->agenda->myactions->create)
2255  {
2256  $type='AC_TEL'; $link='';
2257  if ($addlink == 'AC_FAX') $type='AC_FAX';
2258  if (! empty($conf->global->AGENDA_ADDACTIONFORPHONE)) $link='<a href="'.DOL_URL_ROOT.'/comm/action/card.php?action=create&amp;backtopage=1&amp;actioncode='.$type.($cid?'&amp;contactid='.$cid:'').($socid?'&amp;socid='.$socid:'').'">'.img_object($langs->trans("AddAction"),"calendar").'</a>';
2259  if ($link) $newphone='<div>'.$newphone.' '.$link.'</div>';
2260  }
2261  }
2262 
2263  if (empty($titlealt))
2264  {
2265  $titlealt=($withpicto=='fax'?$langs->trans("Fax"):$langs->trans("Phone"));
2266  }
2267  $rep='';
2268  $picto = '';
2269  if($withpicto){
2270  if($withpicto=='fax'){
2271  $picto = 'phoning_fax';
2272  }elseif($withpicto=='phone'){
2273  $picto = 'phoning';
2274  }elseif($withpicto=='mobile'){
2275  $picto = 'phoning_mobile';
2276  }else{
2277  $picto = '';
2278  }
2279  }
2280  if ($adddivfloat) $rep.='<div class="nospan float" style="margin-right: 10px">';
2281  else $rep.='<span style="margin-right: 10px;">';
2282  $rep.=($withpicto?img_picto($titlealt, 'object_'.$picto.'.png').' ':'').$newphone;
2283  if ($adddivfloat) $rep.='</div>';
2284  else $rep.='</span>';
2285  return $rep;
2286 }
2287 
2295 function dol_print_ip($ip,$mode=0)
2296 {
2297  global $conf,$langs;
2298 
2299  $ret='';
2300 
2301  if (empty($mode)) $ret.=$ip;
2302 
2303  if ($mode != 2)
2304  {
2305  $countrycode=dolGetCountryCodeFromIp($ip);
2306  if ($countrycode) // If success, countrycode is us, fr, ...
2307  {
2308  if (file_exists(DOL_DOCUMENT_ROOT.'/theme/common/flags/'.$countrycode.'.png'))
2309  {
2310  $ret.=' '.img_picto($countrycode.' '.$langs->trans("AccordingToGeoIPDatabase"),DOL_URL_ROOT.'/theme/common/flags/'.$countrycode.'.png','',1);
2311  }
2312  else $ret.=' ('.$countrycode.')';
2313  }
2314  }
2315 
2316  return $ret;
2317 }
2318 
2326 {
2327  global $conf;
2328 
2329  $countrycode='';
2330 
2331  if (! empty($conf->geoipmaxmind->enabled))
2332  {
2333  $datafile=$conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE;
2334  //$ip='24.24.24.24';
2335  //$datafile='E:\Mes Sites\Web\Admin1\awstats\maxmind\GeoIP.dat'; Note that this must be downloaded datafile (not same than datafile provided with ubuntu packages)
2336 
2337  include_once DOL_DOCUMENT_ROOT.'/core/class/dolgeoip.class.php';
2338  $geoip=new DolGeoIP('country',$datafile);
2339  //print 'ip='.$ip.' databaseType='.$geoip->gi->databaseType." GEOIP_CITY_EDITION_REV1=".GEOIP_CITY_EDITION_REV1."\n";
2340  //print "geoip_country_id_by_addr=".geoip_country_id_by_addr($geoip->gi,$ip)."\n";
2341  $countrycode=$geoip->getCountryCodeFromIP($ip);
2342  }
2343 
2344  return $countrycode;
2345 }
2346 
2347 
2355 {
2356  global $conf,$langs,$user;
2357 
2358  //$ret=$user->xxx;
2359  $ret='';
2360  if (! empty($conf->geoipmaxmind->enabled))
2361  {
2362  $ip=$_SERVER["REMOTE_ADDR"];
2363  $datafile=$conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE;
2364  //$ip='24.24.24.24';
2365  //$datafile='E:\Mes Sites\Web\Admin1\awstats\maxmind\GeoIP.dat';
2366  include_once DOL_DOCUMENT_ROOT.'/core/class/dolgeoip.class.php';
2367  $geoip=new DolGeoIP('country',$datafile);
2368  $countrycode=$geoip->getCountryCodeFromIP($ip);
2369  $ret=$countrycode;
2370  }
2371  return $ret;
2372 }
2373 
2386 function dol_print_address($address, $htmlid, $mode, $id, $noprint=0, $charfornl='')
2387 {
2388  global $conf, $user, $langs, $hookmanager;
2389 
2390  $out = '';
2391 
2392  if ($address)
2393  {
2394  if ($hookmanager) {
2395  $parameters = array('element' => $mode, 'id' => $id);
2396  $reshook = $hookmanager->executeHooks('printAddress', $parameters, $address);
2397  $out.=$hookmanager->resPrint;
2398  }
2399  if (empty($reshook))
2400  {
2401  if (empty($charfornl)) $out.=nl2br($address);
2402  else $out.=preg_replace('/[\r\n]+/', $charfornl, $address);
2403 
2404  $showgmap=$showomap=0;
2405 
2406  // TODO Add a hook here
2407  if (($mode=='thirdparty' || $mode =='societe') && ! empty($conf->google->enabled) && ! empty($conf->global->GOOGLE_ENABLE_GMAPS)) $showgmap=1;
2408  if ($mode=='contact' && ! empty($conf->google->enabled) && ! empty($conf->global->GOOGLE_ENABLE_GMAPS_CONTACTS)) $showgmap=1;
2409  if ($mode=='member' && ! empty($conf->google->enabled) && ! empty($conf->global->GOOGLE_ENABLE_GMAPS_MEMBERS)) $showgmap=1;
2410  if (($mode=='thirdparty' || $mode =='societe') && ! empty($conf->openstreetmap->enabled) && ! empty($conf->global->OPENSTREETMAP_ENABLE_MAPS)) $showomap=1;
2411  if ($mode=='contact' && ! empty($conf->openstreetmap->enabled) && ! empty($conf->global->OPENSTREETMAP_ENABLE_MAPS_CONTACTS)) $showomap=1;
2412  if ($mode=='member' && ! empty($conf->openstreetmap->enabled) && ! empty($conf->global->OPENSTREETMAP_ENABLE_MAPS_MEMBERS)) $showomap=1;
2413 
2414  if ($showgmap)
2415  {
2416  $url=dol_buildpath('/google/gmaps.php?mode='.$mode.'&id='.$id,1);
2417  $out.=' <a href="'.$url.'" target="_gmaps"><img id="'.$htmlid.'" class="valigntextbottom" src="'.DOL_URL_ROOT.'/theme/common/gmap.png"></a>';
2418  }
2419  if ($showomap)
2420  {
2421  $url=dol_buildpath('/openstreetmap/maps.php?mode='.$mode.'&id='.$id,1);
2422  $out.=' <a href="'.$url.'" target="_gmaps"><img id="'.$htmlid.'_openstreetmap" class="valigntextbottom" src="'.DOL_URL_ROOT.'/theme/common/gmap.png"></a>';
2423  }
2424  }
2425  }
2426  if ($noprint) return $out;
2427  else print $out;
2428 }
2429 
2430 
2438 function isValidEmail($address, $acceptsupervisorkey=0)
2439 {
2440  if ($acceptsupervisorkey && $address == '__SUPERVISOREMAIL__') return true;
2441  if (filter_var($address, FILTER_VALIDATE_EMAIL)) return true;
2442 
2443  return false;
2444 }
2445 
2453 function isValidPhone($phone)
2454 {
2455  return true;
2456 }
2457 
2458 
2466 function dol_strlen($string,$stringencoding='UTF-8')
2467 {
2468  if (function_exists('mb_strlen')) return mb_strlen($string,$stringencoding);
2469  else return strlen($string);
2470 }
2471 
2481 function dol_substr($string,$start,$length,$stringencoding='')
2482 {
2483  global $langs;
2484 
2485  if (empty($stringencoding)) $stringencoding=$langs->charset_output;
2486 
2487  $ret='';
2488  if (function_exists('mb_substr'))
2489  {
2490  $ret=mb_substr($string,$start,$length,$stringencoding);
2491  }
2492  else
2493  {
2494  $ret=substr($string,$start,$length);
2495  }
2496  return $ret;
2497 }
2498 
2499 
2518 function dol_print_graph($htmlid,$width,$height,$data,$showlegend=0,$type='pie',$showpercent=0,$url='',$combineother=0.05,$shownographyet=0)
2519 {
2520  dol_syslog(__FUNCTION__ . " is deprecated", LOG_WARNING);
2521 
2522  global $conf,$langs;
2523  global $theme_datacolor; // To have var kept when function is called several times
2524 
2525  if ($shownographyet)
2526  {
2527  print '<div class="nographyet" style="width:'.$width.'px;height:'.$height.'px;"></div>';
2528  print '<div class="nographyettext">'.$langs->trans("NotEnoughDataYet").'</div>';
2529  return;
2530  }
2531 
2532  if (empty($conf->use_javascript_ajax)) return;
2533  $jsgraphlib='flot';
2534  $datacolor=array();
2535 
2536  // Load colors of theme into $datacolor array
2537  $color_file = DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/graph-color.php";
2538  if (is_readable($color_file))
2539  {
2540  include_once $color_file;
2541  if (isset($theme_datacolor))
2542  {
2543  $datacolor=array();
2544  foreach($theme_datacolor as $val)
2545  {
2546  $datacolor[]="#".sprintf("%02x",$val[0]).sprintf("%02x",$val[1]).sprintf("%02x",$val[2]);
2547  }
2548  }
2549  }
2550  print '<div id="'.$htmlid.'" style="width:'.$width.'px;height:'.$height.'px;"></div>';
2551 
2552  // We use Flot js lib
2553  if ($jsgraphlib == 'flot')
2554  {
2555  if ($type == 'pie')
2556  {
2557  // data is array('series'=>array(serie1,serie2,...),
2558  // 'seriestype'=>array('bar','line',...),
2559  // 'seriescolor'=>array(0=>'#999999',1=>'#999999',...)
2560  // 'xlabel'=>array(0=>labelx1,1=>labelx2,...));
2561  // serieX is array('label'=>'label', data=>val)
2562  print '
2563  <script type="text/javascript">
2564  $(function () {
2565  var data = '.json_encode($data['series']).';
2566 
2567  function plotWithOptions() {
2568  $.plot($("#'.$htmlid.'"), data,
2569  {
2570  series: {
2571  pie: {
2572  show: true,
2573  radius: 0.8,';
2574  if ($combineother)
2575  {
2576  print '
2577  combine: {
2578  threshold: '.$combineother.'
2579  },';
2580  }
2581  print '
2582  label: {
2583  show: true,
2584  radius: 0.9,
2585  formatter: function(label, series) {
2586  var percent=Math.round(series.percent);
2587  var number=series.data[0][1];
2588  return \'';
2589  print '<div style="font-size:8pt;text-align:center;padding:2px;color:black;">';
2590  if ($url) print '<a style="color: #FFFFFF;" border="0" href="'.$url.'">';
2591  print '\'+'.($showlegend?'number':'label+\' \'+number');
2592  if (! empty($showpercent)) print '+\'<br/>\'+percent+\'%\'';
2593  print '+\'';
2594  if ($url) print '</a>';
2595  print '</div>\';
2596  },
2597  background: {
2598  opacity: 0.0,
2599  color: \'#000000\'
2600  },
2601  }
2602  }
2603  },
2604  zoom: {
2605  interactive: true
2606  },
2607  pan: {
2608  interactive: true
2609  },';
2610  if (count($datacolor))
2611  {
2612  print 'colors: '.(! empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor)).',';
2613  }
2614  print 'legend: {show: '.($showlegend?'true':'false').', position: \'ne\' }
2615  });
2616  }
2617  plotWithOptions();
2618  });
2619  </script>';
2620  }
2621  else if ($type == 'barline')
2622  {
2623  // data is array('series'=>array(serie1,serie2,...),
2624  // 'seriestype'=>array('bar','line',...),
2625  // 'seriescolor'=>array(0=>'#999999',1=>'#999999',...)
2626  // 'xlabel'=>array(0=>labelx1,1=>labelx2,...));
2627  // serieX is array('label'=>'label', data=>array(0=>y1,1=>y2,...)) with same nb of value than into xlabel
2628  print '
2629  <script type="text/javascript">
2630  $(function () {
2631  var data = [';
2632  $i=0; $outputserie=0;
2633  foreach($data['series'] as $serie)
2634  {
2635  if ($data['seriestype'][$i]=='line') { $i++; continue; };
2636  if ($outputserie > 0) print ',';
2637  print '{ bars: { stack: 0, show: true, barWidth: 0.9, align: \'center\' }, label: \''.dol_escape_js($serie['label']).'\', data: '.json_encode($serie['data']).'}'."\n";
2638  $outputserie++; $i++;
2639  }
2640  if ($outputserie) print ', ';
2641  //print '];
2642  //var datalines = [';
2643  $i=0; $outputserie=0;
2644  foreach($data['series'] as $serie)
2645  {
2646  if (empty($data['seriestype'][$i]) || $data['seriestype'][$i]=='bar') { $i++; continue; };
2647  if ($outputserie > 0) print ',';
2648  print '{ lines: { show: true }, label: \''.dol_escape_js($serie['label']).'\', data: '.json_encode($serie['data']).'}'."\n";
2649  $outputserie++; $i++;
2650  }
2651  print '];
2652  var dataticks = '.json_encode($data['xlabel']).'
2653 
2654  function plotWithOptions() {
2655  $.plot(jQuery("#'.$htmlid.'"), data,
2656  {
2657  series: {
2658  stack: 0
2659  },
2660  zoom: {
2661  interactive: true
2662  },
2663  pan: {
2664  interactive: true
2665  },';
2666  if (count($datacolor))
2667  {
2668  print 'colors: '.json_encode($datacolor).',';
2669  }
2670  print 'legend: {show: '.($showlegend?'true':'false').'},
2671  xaxis: {ticks: dataticks}
2672  });
2673  }
2674  plotWithOptions();
2675  });
2676  </script>';
2677  }
2678  else print 'BadValueForParameterType';
2679  }
2680 }
2681 
2695 function dol_trunc($string,$size=40,$trunc='right',$stringencoding='UTF-8',$nodot=0, $display=0)
2696 {
2697  global $conf;
2698 
2699  if ($size==0 || ! empty($conf->global->MAIN_DISABLE_TRUNC)) return $string;
2700 
2701  if (empty($stringencoding)) $stringencoding='UTF-8';
2702  // reduce for small screen
2703  if ($conf->dol_optimize_smallscreen==1 && $display==1) $size = round($size/3);
2704 
2705  // We go always here
2706  if ($trunc == 'right')
2707  {
2708  $newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
2709  if (dol_strlen($newstring,$stringencoding) > ($size+($nodot?0:3))) // If nodot is 0 and size is 1,2 or 3 chars more, we don't trunc and don't add ...
2710  return dol_substr($newstring,0,$size,$stringencoding).($nodot?'':'...');
2711  else
2712  //return 'u'.$size.'-'.$newstring.'-'.dol_strlen($newstring,$stringencoding).'-'.$string;
2713  return $string;
2714  }
2715  elseif ($trunc == 'middle')
2716  {
2717  $newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
2718  if (dol_strlen($newstring,$stringencoding) > 2 && dol_strlen($newstring,$stringencoding) > ($size+1))
2719  {
2720  $size1=round($size/2);
2721  $size2=round($size/2);
2722  return dol_substr($newstring,0,$size1,$stringencoding).'...'.dol_substr($newstring,dol_strlen($newstring,$stringencoding) - $size2,$size2,$stringencoding);
2723  }
2724  else
2725  return $string;
2726  }
2727  elseif ($trunc == 'left')
2728  {
2729  $newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
2730  if (dol_strlen($newstring,$stringencoding) > ($size+($nodot?0:3))) // If nodot is 0 and size is 1,2 or 3 chars more, we don't trunc and don't add ...
2731  return '...'.dol_substr($newstring,dol_strlen($newstring,$stringencoding) - $size,$size,$stringencoding);
2732  else
2733  return $string;
2734  }
2735  elseif ($trunc == 'wrap')
2736  {
2737  $newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
2738  if (dol_strlen($newstring,$stringencoding) > ($size+1))
2739  return dol_substr($newstring,0,$size,$stringencoding)."\n".dol_trunc(dol_substr($newstring,$size,dol_strlen($newstring,$stringencoding)-$size,$stringencoding),$size,$trunc);
2740  else
2741  return $string;
2742  }
2743  else return 'BadParam3CallingDolTrunc';
2744 }
2745 
2764 function img_picto($titlealt, $picto, $moreatt = '', $pictoisfullpath = false, $srconly=0, $notitle=0, $alt='', $morecss='')
2765 {
2766  global $conf, $langs;
2767 
2768  // Define fullpathpicto to use into src
2769  if ($pictoisfullpath)
2770  {
2771  // Clean parameters
2772  if (! preg_match('/(\.png|\.gif|\.svg)$/i',$picto)) $picto .= '.png';
2773  $fullpathpicto = $picto;
2774  }
2775  else
2776  {
2777  //if (in_array($picto, array('switch_off', 'switch_on', 'off', 'on')))
2778  if (in_array($picto, array('switch_off', 'switch_on', 'off', 'on')))
2779  {
2780  $fakey = $picto; $facolor=''; $fasize='';
2781  if ($picto == 'switch_off') { $fakey = 'fa-toggle-off'; $facolor='#999'; $fasize='2em'; }
2782  if ($picto == 'switch_on') { $fakey = 'fa-toggle-on'; $facolor='#227722'; $fasize='2em'; }
2783  if ($picto == 'off') { $fakey = 'fa-square-o'; $fasize='1.3em'; }
2784  if ($picto == 'on') { $fakey = 'fa-check-square-o'; $fasize='1.3em'; }
2785  $enabledisablehtml='';
2786  $enabledisablehtml.='<span class="fa '.$fakey.' valignmiddle'.($morecss?' '.$morecss:'').'" style="'.($fasize?('font-size: '.$fasize.';'):'').($facolor?(' color: '.$facolor.';'):'').'" alt="'.dol_escape_htmltag($titlealt).'" title="'.dol_escape_htmltag($titlealt).'"'.($moreatt?' '.$moreatt:'').'">';
2787  if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) $enabledisablehtml.=$titlealt;
2788  $enabledisablehtml.='</span>';
2789  return $enabledisablehtml;
2790  }
2791 
2792  // We forge fullpathpicto for image to $path/img/$picto. By default, we take DOL_URL_ROOT/theme/$conf->theme/img/$picto
2793  $url = DOL_URL_ROOT;
2794  $theme = $conf->theme;
2795 
2796  $path = 'theme/'.$theme;
2797  if (! empty($conf->global->MAIN_OVERWRITE_THEME_PATH)) $path = $conf->global->MAIN_OVERWRITE_THEME_PATH.'/theme/'.$theme; // If the theme does not have the same name as the module
2798  else if (! empty($conf->global->MAIN_OVERWRITE_THEME_RES)) $path = $conf->global->MAIN_OVERWRITE_THEME_RES.'/theme/'.$conf->global->MAIN_OVERWRITE_THEME_RES; // To allow an external module to overwrite image resources whatever is activated theme
2799  else if (! empty($conf->modules_parts['theme']) && array_key_exists($theme, $conf->modules_parts['theme'])) $path = $theme.'/theme/'.$theme; // If the theme have the same name as the module
2800 
2801  // If we ask an image into $url/$mymodule/img (instead of default path)
2802  if (preg_match('/^([^@]+)@([^@]+)$/i',$picto,$regs))
2803  {
2804  $picto = $regs[1];
2805  $path = $regs[2]; // $path is $mymodule
2806  }
2807 
2808  // Clean parameters
2809  if (! preg_match('/(\.png|\.gif|\.svg)$/i',$picto)) $picto .= '.png';
2810  // If alt path are defined, define url where img file is, according to physical path
2811  foreach ($conf->file->dol_document_root as $type => $dirroot) // ex: array(["main"]=>"/home/maindir/htdocs", ["alt0"]=>"/home/moddir0/htdocs", ...)
2812  {
2813  if ($type == 'main') continue;
2814  if (file_exists($dirroot.'/'.$path.'/img/'.$picto)) // This need a lot of time, that's why enabling alternative dir like "custom" dir is not recommanded
2815  {
2816  $url=DOL_URL_ROOT.$conf->file->dol_url_root[$type];
2817  break;
2818  }
2819  }
2820 
2821  // $url is '' or '/custom', $path is current theme or
2822  $fullpathpicto = $url.'/'.$path.'/img/'.$picto;
2823  }
2824 
2825  if ($srconly) return $fullpathpicto;
2826  else
2827  {
2828  // tag title is used for tooltip on <a>, tag alt can be used with very simple text on image for bind people
2829  //$tmparray=array(0=>$titlealt);
2830  //if (empty($notitle) && preg_match('/:[^\s0-9]/',$titlealt)) $tmparray=explode(':',$titlealt); // We explode if we have TextA:TextB. Not if we have TextA: TextB
2831  //$title=$tmparray[0];
2832  //$alt=empty($tmparray[1])?'':$tmparray[1];
2833  $title=$titlealt;
2834  return '<img src="'.$fullpathpicto.'" alt="'.dol_escape_htmltag($alt).'"'.(($notitle || empty($title))?'':' title="'.dol_escape_htmltag($title).'"').($moreatt?' '.$moreatt:' class="inline-block"').'>'; // Alt is used for accessibility, title for popup
2835  }
2836 }
2837 
2851 function img_object($titlealt, $picto, $moreatt = '', $pictoisfullpath = false, $srconly=0, $notitle=0)
2852 {
2853  return img_picto($titlealt, 'object_'.$picto, $moreatt, $pictoisfullpath, $srconly, $notitle);
2854 }
2855 
2866 function img_weather($titlealt, $picto, $moreatt = '', $pictoisfullpath = 0)
2867 {
2868  global $conf;
2869 
2870  if (! preg_match('/(\.png|\.gif)$/i', $picto)) $picto .= '.png';
2871 
2872  $path = DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/weather/'.$picto;
2873 
2874  return img_picto($titlealt, $path, $moreatt, 1);
2875 }
2876 
2887 function img_picto_common($titlealt, $picto, $moreatt = '', $pictoisfullpath = 0)
2888 {
2889  global $conf;
2890 
2891  if (! preg_match('/(\.png|\.gif)$/i', $picto)) $picto .= '.png';
2892 
2893  if ($pictoisfullpath) $path = $picto;
2894  else
2895  {
2896  $path = DOL_URL_ROOT.'/theme/common/'.$picto;
2897 
2898  if (! empty($conf->global->MAIN_MODULE_CAN_OVERWRITE_COMMONICONS))
2899  {
2900  $themepath = DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/img/'.$picto;
2901 
2902  if (file_exists($themepath)) $path = $themepath;
2903  }
2904  }
2905 
2906  return img_picto($titlealt, $path, $moreatt, 1);
2907 }
2908 
2916 function img_action($titlealt, $numaction)
2917 {
2918  global $conf, $langs;
2919 
2920  if (empty($titlealt) || $titlealt == 'default')
2921  {
2922  if ($numaction == '-1' || $numaction == 'ST_NO') { $numaction = -1; $titlealt = $langs->transnoentitiesnoconv('ChangeDoNotContact'); }
2923  elseif ($numaction == '0' || $numaction == 'ST_NEVER') { $numaction = 0; $titlealt = $langs->transnoentitiesnoconv('ChangeNeverContacted'); }
2924  elseif ($numaction == '1' || $numaction == 'ST_TODO') { $numaction = 1; $titlealt = $langs->transnoentitiesnoconv('ChangeToContact'); }
2925  elseif ($numaction == '2' || $numaction == 'ST_PEND') { $numaction = 2; $titlealt = $langs->transnoentitiesnoconv('ChangeContactInProcess'); }
2926  elseif ($numaction == '3' || $numaction == 'ST_DONE') { $numaction = 3; $titlealt = $langs->transnoentitiesnoconv('ChangeContactDone'); }
2927  else { $titlealt = $langs->transnoentitiesnoconv('ChangeStatus '.$numaction); $numaction = 0; }
2928  }
2929  if (! is_numeric($numaction)) $numaction=0;
2930 
2931  return img_picto($titlealt, 'stcomm'.$numaction.'.png');
2932 }
2933 
2941 function img_pdf($titlealt = 'default', $size = 3)
2942 {
2943  global $conf, $langs;
2944 
2945  if ($titlealt == 'default') $titlealt = $langs->trans('Show');
2946 
2947  return img_picto($titlealt, 'pdf'.$size.'.png');
2948 }
2949 
2957 function img_edit_add($titlealt = 'default', $other = '')
2958 {
2959  global $conf, $langs;
2960 
2961  if ($titlealt == 'default') $titlealt = $langs->trans('Add');
2962 
2963  return img_picto($titlealt, 'edit_add.png', $other);
2964 }
2972 function img_edit_remove($titlealt = 'default', $other='')
2973 {
2974  global $conf, $langs;
2975 
2976  if ($titlealt == 'default') $titlealt = $langs->trans('Remove');
2977 
2978  return img_picto($titlealt, 'edit_remove.png', $other);
2979 }
2980 
2989 function img_edit($titlealt = 'default', $float = 0, $other = 'class="pictoedit"')
2990 {
2991  global $conf, $langs;
2992 
2993  if ($titlealt == 'default') $titlealt = $langs->trans('Modify');
2994 
2995  return img_picto($titlealt, 'edit.png', ($float ? 'style="float: '.($langs->tab_translate["DIRECTION"] == 'rtl'?'left':'right').'"' : "") . ($other?' '.$other:''));
2996 }
2997 
3006 function img_view($titlealt = 'default', $float = 0, $other = '')
3007 {
3008  global $conf, $langs;
3009 
3010  if ($titlealt == 'default') $titlealt = $langs->trans('View');
3011 
3012  $moreatt = ($float ? 'style="float: right" ' : '').$other;
3013 
3014  return img_picto($titlealt, 'view.png', $moreatt);
3015 }
3016 
3024 function img_delete($titlealt = 'default', $other = 'class="pictodelete"')
3025 {
3026  global $conf, $langs;
3027 
3028  if ($titlealt == 'default') $titlealt = $langs->trans('Delete');
3029 
3030  return img_picto($titlealt, 'delete.png', $other);
3031 }
3032 
3040 function img_printer($titlealt = "default", $other='')
3041 {
3042  global $conf,$langs;
3043  if ($titlealt=="default") $titlealt=$langs->trans("Print");
3044  return img_picto($titlealt,'printer.png',$other);
3045 }
3046 
3054 function img_split($titlealt = 'default', $other = 'class="pictosplit"')
3055 {
3056  global $conf, $langs;
3057 
3058  if ($titlealt == 'default') $titlealt = $langs->trans('Split');
3059 
3060  return img_picto($titlealt, 'split.png', $other);
3061 }
3062 
3070 function img_help($usehelpcursor = 1, $usealttitle = 1)
3071 {
3072  global $conf, $langs;
3073 
3074  if ($usealttitle)
3075  {
3076  if (is_string($usealttitle)) $usealttitle = dol_escape_htmltag($usealttitle);
3077  else $usealttitle = $langs->trans('Info');
3078  }
3079 
3080  return img_picto($usealttitle, 'info.png', 'style="vertical-align: middle;'.($usehelpcursor == 1 ? ' cursor: help': ($usehelpcursor == 2 ? ' cursor: pointer':'')).'"');
3081 }
3082 
3089 function img_info($titlealt = 'default')
3090 {
3091  global $conf, $langs;
3092 
3093  if ($titlealt == 'default') $titlealt = $langs->trans('Informations');
3094 
3095  return img_picto($titlealt, 'info.png', 'style="vertical-align: middle;"');
3096 }
3097 
3105 function img_warning($titlealt = 'default', $moreatt = '')
3106 {
3107  global $conf, $langs;
3108 
3109  if ($titlealt == 'default') $titlealt = $langs->trans('Warning');
3110 
3111  //return '<div class="imglatecoin">'.img_picto($titlealt, 'warning_white.png', 'class="pictowarning valignmiddle"'.($moreatt ? ($moreatt == '1' ? ' style="float: right"' : ' '.$moreatt): '')).'</div>';
3112  return img_picto($titlealt, 'warning.png', 'class="pictowarning valignmiddle"'.($moreatt ? ($moreatt == '1' ? ' style="float: right"' : ' '.$moreatt): ''));
3113 }
3114 
3121 function img_error($titlealt = 'default')
3122 {
3123  global $conf, $langs;
3124 
3125  if ($titlealt == 'default') $titlealt = $langs->trans('Error');
3126 
3127  return img_picto($titlealt, 'error.png', 'class="valigntextbottom"');
3128 }
3129 
3137 function img_next($titlealt = 'default', $moreatt='')
3138 {
3139  global $conf, $langs;
3140 
3141  if ($titlealt == 'default') $titlealt = $langs->trans('Next');
3142 
3143  //return img_picto($titlealt, 'next.png', $moreatt);
3144  return '<span class="fa fa-chevron-right paddingright paddingleft" title="'.dol_escape_htmltag($titlealt).'"></span>';
3145 }
3146 
3154 function img_previous($titlealt = 'default', $moreatt='')
3155 {
3156  global $conf, $langs;
3157 
3158  if ($titlealt == 'default') $titlealt = $langs->trans('Previous');
3159 
3160  //return img_picto($titlealt, 'previous.png', $moreatt);
3161  return '<span class="fa fa-chevron-left paddingright paddingleft" title="'.dol_escape_htmltag($titlealt).'"></span>';
3162 }
3163 
3172 function img_down($titlealt = 'default', $selected = 0, $moreclass='')
3173 {
3174  global $conf, $langs;
3175 
3176  if ($titlealt == 'default') $titlealt = $langs->trans('Down');
3177 
3178  return img_picto($titlealt, ($selected ? '1downarrow_selected.png' : '1downarrow.png'), 'class="imgdown'.($moreclass?" ".$moreclass:"").'"');
3179 }
3180 
3189 function img_up($titlealt = 'default', $selected = 0, $moreclass='')
3190 {
3191  global $conf, $langs;
3192 
3193  if ($titlealt == 'default') $titlealt = $langs->trans('Up');
3194 
3195  return img_picto($titlealt, ($selected ? '1uparrow_selected.png' : '1uparrow.png'), 'class="imgup'.($moreclass?" ".$moreclass:"").'"');
3196 }
3197 
3206 function img_left($titlealt = 'default', $selected = 0, $moreatt='')
3207 {
3208  global $conf, $langs;
3209 
3210  if ($titlealt == 'default') $titlealt = $langs->trans('Left');
3211 
3212  return img_picto($titlealt, ($selected ? '1leftarrow_selected.png' : '1leftarrow.png'), $moreatt);
3213 }
3214 
3223 function img_right($titlealt = 'default', $selected = 0, $moreatt='')
3224 {
3225  global $conf, $langs;
3226 
3227  if ($titlealt == 'default') $titlealt = $langs->trans('Right');
3228 
3229  return img_picto($titlealt, ($selected ? '1rightarrow_selected.png' : '1rightarrow.png'), $moreatt);
3230 }
3231 
3239 function img_allow($allow, $titlealt = 'default')
3240 {
3241  global $conf, $langs;
3242 
3243  if ($titlealt == 'default') $titlealt = $langs->trans('Active');
3244 
3245  if ($allow == 1) return img_picto($titlealt, 'tick.png');
3246 
3247  return '-';
3248 }
3249 
3250 
3259 function img_mime($file, $titlealt = '', $morecss='')
3260 {
3261  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
3262 
3263  $mimetype = dol_mimetype($file, '', 1);
3264  $mimeimg = dol_mimetype($file, '', 2);
3265  $mimefa = dol_mimetype($file, '', 4);
3266 
3267  if (empty($titlealt)) $titlealt = 'Mime type: '.$mimetype;
3268 
3269  //return img_picto_common($titlealt, 'mime/'.$mimeimg, 'class="'.$morecss.'"');
3270  return '<i class="fa fa-'.$mimefa.' paddingright"></i>';
3271 }
3272 
3273 
3284 function img_phone($titlealt = 'default', $option = 0)
3285 {
3286  dol_syslog(__FUNCTION__ . " is deprecated", LOG_WARNING);
3287 
3288  global $conf,$langs;
3289 
3290  if ($titlealt == 'default') $titlealt = $langs->trans('Call');
3291 
3292  if ($option == 1) $img = 'call';
3293  else $img = 'call_out';
3294 
3295  return img_picto($titlealt, $img);
3296 }
3297 
3305 function img_search($titlealt = 'default', $other = '')
3306 {
3307  global $conf, $langs;
3308 
3309  if ($titlealt == 'default') $titlealt = $langs->trans('Search');
3310 
3311  $img = img_picto($titlealt, 'search.png', $other, false, 1);
3312 
3313  $input = '<input type="image" class="liste_titre" name="button_search" src="'.$img.'" ';
3314  $input.= 'value="'.dol_escape_htmltag($titlealt).'" title="'.dol_escape_htmltag($titlealt).'" >';
3315 
3316  return $input;
3317 }
3318 
3326 function img_searchclear($titlealt = 'default', $other = '')
3327 {
3328  global $conf, $langs;
3329 
3330  if ($titlealt == 'default') $titlealt = $langs->trans('Search');
3331 
3332  $img = img_picto($titlealt, 'searchclear.png', $other, false, 1);
3333 
3334  $input = '<input type="image" class="liste_titre" name="button_removefilter" src="'.$img.'" ';
3335  $input.= 'value="'.dol_escape_htmltag($titlealt).'" title="'.dol_escape_htmltag($titlealt).'" >';
3336 
3337  return $input;
3338 }
3339 
3349 function info_admin($text, $infoonimgalt = 0, $nodiv=0, $admin='1')
3350 {
3351  global $conf, $langs;
3352 
3353  if ($infoonimgalt)
3354  {
3355  return img_picto($text, 'info', 'class="hideonsmartphone"');
3356  }
3357 
3358  return ($nodiv?'':'<div class="'.(empty($admin)?'':($admin=='1'?'info':$admin)).' hideonsmartphone">').'<span class="fa fa-info-circle" title="'.dol_escape_htmltag($admin?$langs->trans('InfoAdmin'):$langs->trans('Note')).'"></span> '.$text.($nodiv?'':'</div>');
3359 }
3360 
3361 
3374 function dol_print_error($db='',$error='',$errors=null)
3375 {
3376  global $conf,$langs,$argv;
3377  global $dolibarr_main_prod;
3378 
3379  $out = '';
3380  $syslog = '';
3381 
3382  // Si erreur intervenue avant chargement langue
3383  if (! $langs)
3384  {
3385  require_once DOL_DOCUMENT_ROOT .'/core/class/translate.class.php';
3386  $langs = new Translate('', $conf);
3387  $langs->load("main");
3388  }
3389  $langs->load("main");
3390  $langs->load("errors");
3391 
3392  if ($_SERVER['DOCUMENT_ROOT']) // Mode web
3393  {
3394  $out.=$langs->trans("DolibarrHasDetectedError").".<br>\n";
3395  if (! empty($conf->global->MAIN_FEATURES_LEVEL)) $out.="You use an experimental or develop level of features, so please do NOT report any bugs, except if problem is confirmed moving option MAIN_FEATURES_LEVEL back to 0.<br>\n";
3396  $out.=$langs->trans("InformationToHelpDiagnose").":<br>\n";
3397 
3398  $out.="<b>".$langs->trans("Date").":</b> ".dol_print_date(time(),'dayhourlog')."<br>\n";
3399  $out.="<b>".$langs->trans("Dolibarr").":</b> ".DOL_VERSION."<br>\n";
3400  if (isset($conf->global->MAIN_FEATURES_LEVEL)) $out.="<b>".$langs->trans("LevelOfFeature").":</b> ".$conf->global->MAIN_FEATURES_LEVEL."<br>\n";
3401  if (function_exists("phpversion"))
3402  {
3403  $out.="<b>".$langs->trans("PHP").":</b> ".phpversion()."<br>\n";
3404  }
3405  $out.="<b>".$langs->trans("Server").":</b> ".$_SERVER["SERVER_SOFTWARE"]."<br>\n";
3406  if (function_exists("php_uname"))
3407  {
3408  $out.="<b>".$langs->trans("OS").":</b> ".php_uname()."<br>\n";
3409  }
3410  $out.="<b>".$langs->trans("UserAgent").":</b> ".$_SERVER["HTTP_USER_AGENT"]."<br>\n";
3411  $out.="<br>\n";
3412  $out.="<b>".$langs->trans("RequestedUrl").":</b> ".dol_htmlentities($_SERVER["REQUEST_URI"],ENT_COMPAT,'UTF-8')."<br>\n";
3413  $out.="<b>".$langs->trans("Referer").":</b> ".(isset($_SERVER["HTTP_REFERER"])?dol_htmlentities($_SERVER["HTTP_REFERER"],ENT_COMPAT,'UTF-8'):'')."<br>\n";
3414  $out.="<b>".$langs->trans("MenuManager").":</b> ".(isset($conf->standard_menu)?$conf->standard_menu:'')."<br>\n";
3415  $out.="<br>\n";
3416  $syslog.="url=".dol_escape_htmltag($_SERVER["REQUEST_URI"]);
3417  $syslog.=", query_string=".dol_escape_htmltag($_SERVER["QUERY_STRING"]);
3418  }
3419  else // Mode CLI
3420  {
3421  $out.='> '.$langs->transnoentities("ErrorInternalErrorDetected").":\n".$argv[0]."\n";
3422  $syslog.="pid=".dol_getmypid();
3423  }
3424 
3425  if (is_object($db))
3426  {
3427  if ($_SERVER['DOCUMENT_ROOT']) // Mode web
3428  {
3429  $out.="<b>".$langs->trans("DatabaseTypeManager").":</b> ".$db->type."<br>\n";
3430  $out.="<b>".$langs->trans("RequestLastAccessInError").":</b> ".($db->lastqueryerror()?dol_escape_htmltag($db->lastqueryerror()):$langs->trans("ErrorNoRequestInError"))."<br>\n";
3431  $out.="<b>".$langs->trans("ReturnCodeLastAccessInError").":</b> ".($db->lasterrno()?dol_escape_htmltag($db->lasterrno()):$langs->trans("ErrorNoRequestInError"))."<br>\n";
3432  $out.="<b>".$langs->trans("InformationLastAccessInError").":</b> ".($db->lasterror()?dol_escape_htmltag($db->lasterror()):$langs->trans("ErrorNoRequestInError"))."<br>\n";
3433  $out.="<br>\n";
3434  }
3435  else // Mode CLI
3436  {
3437  // No dol_escape_htmltag for output, we are in CLI mode
3438  $out.='> '.$langs->transnoentities("DatabaseTypeManager").":\n".$db->type."\n";
3439  $out.='> '.$langs->transnoentities("RequestLastAccessInError").":\n".($db->lastqueryerror()?$db->lastqueryerror():$langs->transnoentities("ErrorNoRequestInError"))."\n";
3440  $out.='> '.$langs->transnoentities("ReturnCodeLastAccessInError").":\n".($db->lasterrno()?$db->lasterrno():$langs->transnoentities("ErrorNoRequestInError"))."\n";
3441  $out.='> '.$langs->transnoentities("InformationLastAccessInError").":\n".($db->lasterror()?$db->lasterror():$langs->transnoentities("ErrorNoRequestInError"))."\n";
3442 
3443  }
3444  $syslog.=", sql=".$db->lastquery();
3445  $syslog.=", db_error=".$db->lasterror();
3446  }
3447 
3448  if ($error || $errors)
3449  {
3450  $langs->load("errors");
3451 
3452  // Merge all into $errors array
3453  if (is_array($error) && is_array($errors)) $errors=array_merge($error,$errors);
3454  elseif (is_array($error)) $errors=$error;
3455  elseif (is_array($errors)) $errors=array_merge(array($error),$errors);
3456  else $errors=array_merge(array($error));
3457 
3458  foreach($errors as $msg)
3459  {
3460  if (empty($msg)) continue;
3461  if ($_SERVER['DOCUMENT_ROOT']) // Mode web
3462  {
3463  $out.="<b>".$langs->trans("Message").":</b> ".dol_escape_htmltag($msg)."<br>\n" ;
3464  }
3465  else // Mode CLI
3466  {
3467  $out.='> '.$langs->transnoentities("Message").":\n".$msg."\n" ;
3468  }
3469  $syslog.=", msg=".$msg;
3470  }
3471  }
3472  if (empty($dolibarr_main_prod) && $_SERVER['DOCUMENT_ROOT'] && function_exists('xdebug_print_function_stack') && function_exists('xdebug_call_file'))
3473  {
3474  xdebug_print_function_stack();
3475  $out.='<b>XDebug informations:</b>'."<br>\n";
3476  $out.='File: '.xdebug_call_file()."<br>\n";
3477  $out.='Line: '.xdebug_call_line()."<br>\n";
3478  $out.='Function: '.xdebug_call_function()."<br>\n";
3479  $out.="<br>\n";
3480  }
3481 
3482  if (empty($dolibarr_main_prod)) print $out;
3483  else
3484  {
3485  print $langs->trans("DolibarrHasDetectedError").'. ';
3486  print $langs->trans("YouCanSetOptionDolibarrMainProdToZero");
3487  define("MAIN_CORE_ERROR", 1);
3488  }
3489  //else print 'Sorry, an error occured but the parameter $dolibarr_main_prod is defined in conf file so no message is reported to your browser. Please read the log file for error message.';
3490  dol_syslog("Error ".$syslog, LOG_ERR);
3491 }
3492 
3501 function dol_print_error_email($prefixcode, $errormessage='', $errormessages=array())
3502 {
3503  global $langs,$conf;
3504 
3505  $langs->load("errors");
3506  $now=dol_now();
3507  print '<br><div class="center login_main_message"><div class="error">';
3508  print $langs->trans("ErrorContactEMail", $conf->global->MAIN_INFO_SOCIETE_MAIL, $prefixcode.dol_print_date($now,'%Y%m%d'));
3509  if ($errormessage) print '<br><br>'.$errormessage;
3510  if (is_array($errormessages) && count($errormessages))
3511  {
3512  foreach($errormessages as $mesgtoshow)
3513  {
3514  print '<br><br>'.$mesgtoshow;
3515  }
3516  }
3517  print '</div></div>';
3518 }
3519 
3535 function print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="")
3536 {
3537  print getTitleFieldOfList($name, 0, $file, $field, $begin, $moreparam, $moreattrib, $sortfield, $sortorder, $prefix, 0, $tooltip);
3538 }
3539 
3557 function getTitleFieldOfList($name, $thead=0, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $disablesortlink=0, $tooltip='')
3558 {
3559  global $conf, $langs, $form;
3560  //print "$name, $file, $field, $begin, $options, $moreattrib, $sortfield, $sortorder<br>\n";
3561 
3562  $sortorder=strtoupper($sortorder);
3563  $out='';
3564  $sortimg='';
3565 
3566  $tag='th';
3567  if ($thead==2) $tag='div';
3568 
3569  $tmpsortfield=explode(',',$sortfield);
3570  $sortfield1=trim($tmpsortfield[0]); // If $sortfield is 'd.datep,d.id', it becomes 'd.datep'
3571  $tmpfield=explode(',',$field);
3572  $field1=trim($tmpfield[0]); // If $field is 'd.datep,d.id', it becomes 'd.datep'
3573 
3574  //var_dump('field='.$field.' field1='.$field1.' sortfield='.$sortfield.' sortfield1='.$sortfield1);
3575  // If field is used as sort criteria we use a specific css class liste_titre_sel
3576  // Example if (sortfield,field)=("nom","xxx.nom") or (sortfield,field)=("nom","nom")
3577  if ($field1 && ($sortfield1 == $field1 || $sortfield1 == preg_replace("/^[^\.]+\./","",$field1))) $out.= '<'.$tag.' class="'.$prefix.'liste_titre_sel" '. $moreattrib.'>';
3578  else $out.= '<'.$tag.' class="'.$prefix.'liste_titre" '. $moreattrib.'>';
3579 
3580  if (empty($thead) && $field && empty($disablesortlink)) // If this is a sort field
3581  {
3582  $options=preg_replace('/sortfield=([a-zA-Z0-9,\s\.]+)/i','',$moreparam);
3583  $options=preg_replace('/sortorder=([a-zA-Z0-9,\s\.]+)/i','',$options);
3584  $options=preg_replace('/&+/i','&',$options);
3585  if (! preg_match('/^&/',$options)) $options='&'.$options;
3586 
3587  if ($field1 != $sortfield1) // We are on another field
3588  {
3589  if (preg_match('/^DESC/', $sortorder)) $out.= '<a class="reposition" href="'.$file.'?sortfield='.$field.'&sortorder=desc&begin='.$begin.$options.'">';
3590  else $out.= '<a class="reposition" href="'.$file.'?sortfield='.$field.'&sortorder=asc&begin='.$begin.$options.'">';
3591  }
3592  else // We are of first sorting criteria
3593  {
3594  if (preg_match('/^ASC/', $sortorder)) $out.= '<a class="reposition" href="'.$file.'?sortfield='.$sortfield.'&sortorder=desc&begin='.$begin.$options.'">';
3595  else $out.= '<a class="reposition" href="'.$file.'?sortfield='.$sortfield.'&sortorder=asc&begin='.$begin.$options.'">';
3596  }
3597  }
3598 
3599  if ($tooltip) $out.=$form->textwithpicto($langs->trans($name), $langs->trans($tooltip));
3600  else $out.=$langs->trans($name);
3601 
3602  if (empty($thead) && $field && empty($disablesortlink)) // If this is a sort field
3603  {
3604  $out.='</a>';
3605  }
3606 
3607  if (empty($thead) && $field) // If this is a sort field
3608  {
3609  $options=preg_replace('/sortfield=([a-zA-Z0-9,\s\.]+)/i','',$moreparam);
3610  $options=preg_replace('/sortorder=([a-zA-Z0-9,\s\.]+)/i','',$options);
3611  $options=preg_replace('/&+/i','&',$options);
3612  if (! preg_match('/^&/',$options)) $options='&'.$options;
3613 
3614  //print "&nbsp;";
3615  //$sortimg.= '<img width="2" src="'.DOL_URL_ROOT.'/theme/common/transparent.png" alt="">';
3616  //$sortimg.= '<span class="nowrap">';
3617 
3618  if (! $sortorder || $field1 != $sortfield1)
3619  {
3620  //$out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=asc&begin='.$begin.$options.'">'.img_down("A-Z",0).'</a>';
3621  //$out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=desc&begin='.$begin.$options.'">'.img_up("Z-A",0).'</a>';
3622  }
3623  else
3624  {
3625  if (preg_match('/^DESC/', $sortorder)) {
3626  //$out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=asc&begin='.$begin.$options.'">'.img_down("A-Z",0).'</a>';
3627  //$out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=desc&begin='.$begin.$options.'">'.img_up("Z-A",1).'</a>';
3628  $sortimg.= '<span class="nowrap">'.img_up("Z-A",0).'</span>';
3629  }
3630  if (preg_match('/^ASC/', $sortorder)) {
3631  //$out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=asc&begin='.$begin.$options.'">'.img_down("A-Z",1).'</a>';
3632  //$out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=desc&begin='.$begin.$options.'">'.img_up("Z-A",0).'</a>';
3633  $sortimg.= '<span class="nowrap">'.img_down("A-Z",0).'</span>';
3634  }
3635  }
3636 
3637  //$sortimg.= '</span>';
3638  }
3639 
3640  $out.=$sortimg;
3641 
3642  $out.='</'.$tag.'>';
3643 
3644  return $out;
3645 }
3646 
3655 function print_titre($title)
3656 {
3657  dol_syslog(__FUNCTION__ . " is deprecated", LOG_WARNING);
3658 
3659  print '<div class="titre">'.$title.'</div>';
3660 }
3661 
3673 function print_fiche_titre($title, $mesg='', $picto='title_generic.png', $pictoisfullpath=0, $id='')
3674 {
3675  print load_fiche_titre($title, $mesg, $picto, $pictoisfullpath, $id);
3676 }
3677 
3691 function load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
3692 {
3693  global $conf;
3694 
3695  $return='';
3696 
3697  if ($picto == 'setup') $picto='title.png';
3698  if (($conf->browser->name == 'ie') && $picto=='title.png') $picto='title.gif';
3699 
3700  $return.= "\n";
3701  $return.= '<table '.($id?'id="'.$id.'" ':'').'summary="" class="centpercent notopnoleftnoright'.($morecssontable?' '.$morecssontable:'').'" style="margin-bottom: 2px;"><tr>';
3702  if ($picto) $return.= '<td class="nobordernopadding widthpictotitle" valign="middle">'.img_picto('',$picto, 'class="valignmiddle widthpictotitle" id="pictotitle"', $pictoisfullpath).'</td>';
3703  $return.= '<td class="nobordernopadding" valign="middle">';
3704  $return.= '<div class="titre">'.$titre.'</div>';
3705  $return.= '</td>';
3706  if (dol_strlen($morehtmlcenter))
3707  {
3708  $return.= '<td class="nobordernopadding" align="center" valign="middle">'.$morehtmlcenter.'</td>';
3709  }
3710  if (dol_strlen($morehtmlright))
3711  {
3712  $return.= '<td class="nobordernopadding titre_right" align="right" valign="middle">'.$morehtmlright.'</td>';
3713  }
3714  $return.= '</tr></table>'."\n";
3715 
3716  return $return;
3717 }
3718 
3740 function print_barre_liste($titre, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='title_generic.png', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $hideselectlimit=0, $hidenavigation=0)
3741 {
3742  global $conf,$langs;
3743 
3744  $savlimit = $limit;
3745  $savtotalnboflines = $totalnboflines;
3746  $totalnboflines=abs($totalnboflines);
3747 
3748  if ($picto == 'setup') $picto='title_setup.png';
3749  if (($conf->browser->name == 'ie') && $picto=='title_generic.png') $picto='title.gif';
3750  if ($limit < 0) $limit = $conf->liste_limit;
3751  if ($savlimit != 0 && (($num > $limit) || ($num == -1) || ($limit == 0)))
3752  {
3753  $nextpage = 1;
3754  }
3755  else
3756  {
3757  $nextpage = 0;
3758  }
3759  //print 'totalnboflines='.$totalnboflines.'-savlimit='.$savlimit.'-limit='.$limit.'-num='.$num.'-nextpage='.$nextpage;
3760 
3761  print "\n";
3762  print "<!-- Begin title '".$titre."' -->\n";
3763  print '<table width="100%" border="0" class="notopnoleftnoright'.($morecss?' '.$morecss:'').'" style="margin-bottom: 6px;"><tr>';
3764 
3765  // Left
3766  //if ($picto && $titre) print '<td class="nobordernopadding hideonsmartphone" width="40" align="left" valign="middle">'.img_picto('', $picto, 'id="pictotitle"', $pictoisfullpath).'</td>';
3767  print '<td class="nobordernopadding valignmiddle">';
3768  if ($picto && $titre) print img_picto('', $picto, 'class="hideonsmartphone valignmiddle" id="pictotitle"', $pictoisfullpath);
3769  print '<div class="titre inline-block">'.$titre;
3770  if (!empty($titre) && $savtotalnboflines >= 0 && (string) $savtotalnboflines != '') print ' ('.$totalnboflines.')';
3771  print '</div></td>';
3772 
3773  // Center
3774  if ($morehtmlcenter)
3775  {
3776  print '<td class="nobordernopadding center valignmiddle">'.$morehtmlcenter.'</td>';
3777  }
3778 
3779  // Right
3780  print '<td class="nobordernopadding valignmiddle" align="right">';
3781  if ($sortfield) $options .= "&sortfield=".urlencode($sortfield);
3782  if ($sortorder) $options .= "&sortorder=".urlencode($sortorder);
3783  // Show navigation bar
3784  $pagelist = '';
3785  if ($savlimit != 0 && ($page > 0 || $num > $limit))
3786  {
3787  if ($totalnboflines) // If we know total nb of lines
3788  {
3789  $maxnbofpage=(empty($conf->dol_optimize_smallscreen) ? 4 : 1); // page nb before and after selected page + ... + first or last
3790 
3791  if ($limit > 0) $nbpages=ceil($totalnboflines/$limit);
3792  else $nbpages=1;
3793  $cpt=($page-$maxnbofpage);
3794  if ($cpt < 0) { $cpt=0; }
3795 
3796  if ($cpt>=1)
3797  {
3798  $pagelist.= '<li'.(($conf->dol_use_jmobile != 4)?' class="pagination"':'').'><a href="'.$file.'?page=0'.$options.'">1</a></li>';
3799  if ($cpt > 2) $pagelist.='<li'.(($conf->dol_use_jmobile != 4)?' class="pagination"':'').'><span '.(($conf->dol_use_jmobile != 4)?'class="inactive"':'').'>...</span></li>';
3800  else if ($cpt == 2) $pagelist.='<li'.(($conf->dol_use_jmobile != 4)?' class="pagination"':'').'><a href="'.$file.'?page=1'.$options.'">2</a></li>';
3801  }
3802 
3803  do
3804  {
3805  if ($cpt==$page)
3806  {
3807  $pagelist.= '<li'.(($conf->dol_use_jmobile != 4)?' class="pagination"':'').'><span '.(($conf->dol_use_jmobile != 4)?'class="active"':'').'>'.($page+1).'</span></li>';
3808  }
3809  else
3810  {
3811  $pagelist.= '<li'.(($conf->dol_use_jmobile != 4)?' class="pagination"':'').'><a href="'.$file.'?page='.$cpt.$options.'">'.($cpt+1).'</a></li>';
3812  }
3813  $cpt++;
3814  }
3815  while ($cpt < $nbpages && $cpt<=$page+$maxnbofpage);
3816 
3817  if ($cpt<$nbpages)
3818  {
3819  if ($cpt<$nbpages-2) $pagelist.= '<li'.(($conf->dol_use_jmobile != 4)?' class="pagination"':'').'><span '.(($conf->dol_use_jmobile != 4)?'class="inactive"':'').'>...</span></li>';
3820  else if ($cpt == $nbpages-2) $pagelist.= '<li'.(($conf->dol_use_jmobile != 4)?' class="pagination"':'').'><a href="'.$file.'?page='.($nbpages-2).$options.'">'.($nbpages - 1).'</a></li>';
3821  $pagelist.= '<li'.(($conf->dol_use_jmobile != 4)?' class="pagination"':'').'><a href="'.$file.'?page='.($nbpages-1).$options.'">'.$nbpages.'</a></li>';
3822  }
3823  }
3824  else
3825  {
3826  $pagelist.= '<li'.(($conf->dol_use_jmobile != 4)?' class="pagination"':'').'><span '.(($conf->dol_use_jmobile != 4)?'class="active"':'').'>'.($page+1)."</li>";
3827  }
3828  }
3829 
3830  print_fleche_navigation($page, $file, $options, $nextpage, $pagelist, $morehtmlright, $savlimit, $totalnboflines, $hideselectlimit); // output the div and ul for previous/last completed with page numbers into $pagelist
3831 
3832  print '</td>';
3833 
3834  print '</tr></table>'."\n";
3835  print "<!-- End title -->\n\n";
3836 }
3837 
3852 function print_fleche_navigation($page, $file, $options='', $nextpage=0, $betweenarrows='', $afterarrows='', $limit=-1, $totalnboflines=0, $hideselectlimit=0)
3853 {
3854  global $conf, $langs;
3855 
3856  print '<div class="pagination"><ul>';
3857  if ((int) $limit >= 0 && empty($hideselectlimit))
3858  {
3859  $pagesizechoices='10:10,20:20,30:30,40:40,50:50,100:100,250:250,500:500,1000:1000,5000:5000';
3860  //$pagesizechoices.=',0:'.$langs->trans("All"); // Not yet supported
3861  //$pagesizechoices.=',2:2';
3862  if (! empty($conf->global->MAIN_PAGESIZE_CHOICES)) $pagesizechoices=$conf->global->MAIN_PAGESIZE_CHOICES;
3863 
3864  print '<li class="pagination">';
3865  print '<select class="flat selectlimit" name="limit" title="'.dol_escape_htmltag($langs->trans("MaxNbOfRecordPerPage")).'">';
3866  $tmpchoice=explode(',',$pagesizechoices);
3867  $tmpkey=$limit.':'.$limit;
3868  if (! in_array($tmpkey, $tmpchoice)) $tmpchoice[]=$tmpkey;
3869  $tmpkey=$conf->liste_limit.':'.$conf->liste_limit;
3870  if (! in_array($tmpkey, $tmpchoice)) $tmpchoice[]=$tmpkey;
3871  asort($tmpchoice, SORT_NUMERIC);
3872  $found=false;
3873  foreach($tmpchoice as $val)
3874  {
3875  $selected='';
3876  $tmp=explode(':',$val);
3877  $key=$tmp[0];
3878  $val=$tmp[1];
3879  if ($key != '' && $val != '')
3880  {
3881  if ((int) $key == (int) $limit)
3882  {
3883  $selected = ' selected="selected"';
3884  $found = true;
3885  }
3886  print '<option name="'.$key.'"'.$selected.'>'.dol_escape_htmltag($val).'</option>'."\n";
3887  }
3888  }
3889  print '</select>';
3890  if ($conf->use_javascript_ajax)
3891  {
3892  print '<!-- JS CODE TO ENABLE select limit to launch submit of page -->
3893  <script type="text/javascript">
3894  jQuery(document).ready(function () {
3895  jQuery(".selectlimit").change(function() {
3896  console.log("Change limit. Send submit");
3897  $(this).parents(\'form:first\').submit();
3898  });
3899  });
3900  </script>
3901  ';
3902  }
3903  print '</li>';
3904  }
3905  if ($page > 0)
3906  {
3907  print '<li class="pagination"><a class="paginationprevious" href="'.$file.'?page='.($page-1).$options.'"><i class="fa fa-chevron-left" title="'.dol_escape_htmltag($langs->trans("Previous")).'"></i></a></li>';
3908  }
3909  if ($betweenarrows)
3910  {
3911  print $betweenarrows;
3912  }
3913  if ($nextpage > 0)
3914  {
3915  print '<li class="pagination"><a class="paginationnext" href="'.$file.'?page='.($page+1).$options.'"><i class="fa fa-chevron-right" title="'.dol_escape_htmltag($langs->trans("Next")).'"></i></a></li>';
3916  }
3917  if ($afterarrows)
3918  {
3919  print '<li class="paginationafterarrows">';
3920  print $afterarrows;
3921  print '</li>';
3922  }
3923  print '</ul></div>'."\n";
3924 }
3925 
3926 
3937 function vatrate($rate, $addpercent=false, $info_bits=0, $usestarfornpr=0)
3938 {
3939  $morelabel='';
3940 
3941  if (preg_match('/%/',$rate))
3942  {
3943  $rate=str_replace('%','',$rate);
3944  $addpercent=true;
3945  }
3946  if (preg_match('/\((.*)\)/',$rate,$reg))
3947  {
3948  $morelabel=' ('.$reg[1].')';
3949  $rate=preg_replace('/\s*'.preg_quote($morelabel,'/').'/','',$rate);
3950  }
3951  if (preg_match('/\*/',$rate))
3952  {
3953  $rate=str_replace('*','',$rate);
3954  $info_bits |= 1;
3955  }
3956 
3957  // If rate is '9/9/9' we don't change it. If rate is '9.000' we apply price()
3958  if (! preg_match('/\//', $rate)) $ret=price($rate,0,'',0,0).($addpercent?'%':'');
3959  else
3960  {
3961  // TODO Split on / and output with a price2num to have clean numbers without ton of 000.
3962  $ret=$rate.($addpercent?'%':'');
3963  }
3964  if (($info_bits & 1) && $usestarfornpr >= 0) $ret.=' *';
3965  $ret.=$morelabel;
3966  return $ret;
3967 }
3968 
3969 
3985 function price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
3986 {
3987  global $langs,$conf;
3988 
3989  // Clean parameters
3990  if (empty($amount)) $amount=0; // To have a numeric value if amount not defined or = ''
3991  $amount = (is_numeric($amount)?$amount:0); // Check if amount is numeric, for example, an error occured when amount value = o (letter) instead 0 (number)
3992  if ($rounding < 0) $rounding=min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT);
3993  $nbdecimal=$rounding;
3994 
3995  // Output separators by default (french)
3996  $dec=','; $thousand=' ';
3997 
3998  // If $outlangs not forced, we use use language
3999  if (! is_object($outlangs)) $outlangs=$langs;
4000 
4001  if ($outlangs->transnoentitiesnoconv("SeparatorDecimal") != "SeparatorDecimal") $dec=$outlangs->transnoentitiesnoconv("SeparatorDecimal");
4002  if ($outlangs->transnoentitiesnoconv("SeparatorThousand")!= "SeparatorThousand") $thousand=$outlangs->transnoentitiesnoconv("SeparatorThousand");
4003  if ($thousand == 'None') $thousand='';
4004  else if ($thousand == 'Space') $thousand=' ';
4005  //print "outlangs=".$outlangs->defaultlang." amount=".$amount." html=".$form." trunc=".$trunc." nbdecimal=".$nbdecimal." dec='".$dec."' thousand='".$thousand."'<br>";
4006 
4007  //print "amount=".$amount."-";
4008  $amount = str_replace(',','.',$amount); // should be useless
4009  //print $amount."-";
4010  $datas = explode('.',$amount);
4011  $decpart = isset($datas[1])?$datas[1]:'';
4012  $decpart = preg_replace('/0+$/i','',$decpart); // Supprime les 0 de fin de partie decimale
4013  //print "decpart=".$decpart."<br>";
4014  $end='';
4015 
4016  // We increase nbdecimal if there is more decimal than asked (to not loose information)
4017  if (dol_strlen($decpart) > $nbdecimal) $nbdecimal=dol_strlen($decpart);
4018  // Si on depasse max
4019  if ($trunc && $nbdecimal > $conf->global->MAIN_MAX_DECIMALS_SHOWN)
4020  {
4021  $nbdecimal=$conf->global->MAIN_MAX_DECIMALS_SHOWN;
4022  if (preg_match('/\.\.\./i',$conf->global->MAIN_MAX_DECIMALS_SHOWN))
4023  {
4024  // Si un affichage est tronque, on montre des ...
4025  $end='...';
4026  }
4027  }
4028 
4029  // If force rounding
4030  if ($forcerounding >= 0) $nbdecimal = $forcerounding;
4031 
4032  // Format number
4033  $output=number_format($amount, $nbdecimal, $dec, $thousand);
4034  if ($form)
4035  {
4036  $output=preg_replace('/\s/','&nbsp;',$output);
4037  $output=preg_replace('/\'/','&#039;',$output);
4038  }
4039  // Add symbol of currency if requested
4040  $cursymbolbefore=$cursymbolafter='';
4041  if ($currency_code)
4042  {
4043  if ($currency_code == 'auto') $currency_code=$conf->currency;
4044 
4045  $listofcurrenciesbefore=array('USD','GBP','AUD','MXN','PEN','CNY');
4046  if (in_array($currency_code,$listofcurrenciesbefore)) $cursymbolbefore.=$outlangs->getCurrencySymbol($currency_code);
4047  else
4048  {
4049  $tmpcur=$outlangs->getCurrencySymbol($currency_code);
4050  $cursymbolafter.=($tmpcur == $currency_code ? ' '.$tmpcur : $tmpcur);
4051  }
4052  }
4053  $output=$cursymbolbefore.$output.$end.($cursymbolafter?' ':'').$cursymbolafter;
4054 
4055  return $output;
4056 }
4057 
4072 function price2num($amount,$rounding='',$alreadysqlnb=0)
4073 {
4074  global $langs,$conf;
4075 
4076  // Round PHP function does not allow number like '1,234.56' nor '1.234,56' nor '1 234,56'
4077  // Numbers must be '1234.56'
4078  // Decimal delimiter for PHP and database SQL requests must be '.'
4079  $dec=','; $thousand=' ';
4080  if ($langs->transnoentitiesnoconv("SeparatorDecimal") != "SeparatorDecimal") $dec=$langs->transnoentitiesnoconv("SeparatorDecimal");
4081  if ($langs->transnoentitiesnoconv("SeparatorThousand")!= "SeparatorThousand") $thousand=$langs->transnoentitiesnoconv("SeparatorThousand");
4082  if ($thousand == 'None') $thousand='';
4083  elseif ($thousand == 'Space') $thousand=' ';
4084  //print "amount=".$amount." html=".$form." trunc=".$trunc." nbdecimal=".$nbdecimal." dec='".$dec."' thousand='".$thousand."'<br>";
4085 
4086  // Convert value to universal number format (no thousand separator, '.' as decimal separator)
4087  if ($alreadysqlnb != 1) // If not a PHP number or unknown, we change format
4088  {
4089  //print 'PP'.$amount.' - '.$dec.' - '.$thousand.' - '.intval($amount).'<br>';
4090 
4091  // Convert amount to format with dolibarr dec and thousand (this is because PHP convert a number
4092  // to format defined by LC_NUMERIC after a calculation and we want source format to be like defined by Dolibarr setup.
4093  if (is_numeric($amount))
4094  {
4095  // We put in temps value of decimal ("0.00001"). Works with 0 and 2.0E-5 and 9999.10
4096  $temps=sprintf("%0.10F",$amount-intval($amount)); // temps=0.0000000000 or 0.0000200000 or 9999.1000000000
4097  $temps=preg_replace('/([\.1-9])0+$/','\\1',$temps); // temps=0. or 0.00002 or 9999.1
4098  $nbofdec=max(0,dol_strlen($temps)-2); // -2 to remove "0."
4099  $amount=number_format($amount,$nbofdec,$dec,$thousand);
4100  }
4101  //print "QQ".$amount.'<br>';
4102 
4103  // Now make replace (the main goal of function)
4104  if ($thousand != ',' && $thousand != '.') $amount=str_replace(',','.',$amount); // To accept 2 notations for french users
4105  $amount=str_replace(' ','',$amount); // To avoid spaces
4106  $amount=str_replace($thousand,'',$amount); // Replace of thousand before replace of dec to avoid pb if thousand is .
4107  $amount=str_replace($dec,'.',$amount);
4108  }
4109 
4110  // Now, make a rounding if required
4111  if ($rounding)
4112  {
4113  $nbofdectoround='';
4114  if ($rounding == 'MU') $nbofdectoround=$conf->global->MAIN_MAX_DECIMALS_UNIT;
4115  elseif ($rounding == 'MT') $nbofdectoround=$conf->global->MAIN_MAX_DECIMALS_TOT;
4116  elseif ($rounding == 'MS') $nbofdectoround=empty($conf->global->MAIN_MAX_DECIMALS_STOCK)?5:$conf->global->MAIN_MAX_DECIMALS_STOCK;
4117  elseif (is_numeric($rounding)) $nbofdectoround=$rounding; // For admin info page
4118  //print "RR".$amount.' - '.$nbofdectoround.'<br>';
4119  if (dol_strlen($nbofdectoround)) $amount = round($amount,$nbofdectoround); // $nbofdectoround can be 0.
4120  else return 'ErrorBadParameterProvidedToFunction';
4121  //print 'SS'.$amount.' - '.$nbofdec.' - '.$dec.' - '.$thousand.' - '.$nbofdectoround.'<br>';
4122 
4123  // Convert amount to format with dolibarr dec and thousand (this is because PHP convert a number
4124  // to format defined by LC_NUMERIC after a calculation and we want source format to be defined by Dolibarr setup.
4125  if (is_numeric($amount))
4126  {
4127  // We put in temps value of decimal ("0.00001"). Works with 0 and 2.0E-5 and 9999.10
4128  $temps=sprintf("%0.10F",$amount-intval($amount)); // temps=0.0000000000 or 0.0000200000 or 9999.1000000000
4129  $temps=preg_replace('/([\.1-9])0+$/','\\1',$temps); // temps=0. or 0.00002 or 9999.1
4130  $nbofdec=max(0,dol_strlen($temps)-2); // -2 to remove "0."
4131  $amount=number_format($amount,min($nbofdec,$nbofdectoround),$dec,$thousand); // Convert amount to format with dolibarr dec and thousand
4132  }
4133  //print "TT".$amount.'<br>';
4134 
4135  // Always make replace because each math function (like round) replace
4136  // with local values and we want a number that has a SQL string format x.y
4137  if ($thousand != ',' && $thousand != '.') $amount=str_replace(',','.',$amount); // To accept 2 notations for french users
4138  $amount=str_replace(' ','',$amount); // To avoid spaces
4139  $amount=str_replace($thousand,'',$amount); // Replace of thousand before replace of dec to avoid pb if thousand is .
4140  $amount=str_replace($dec,'.',$amount);
4141  }
4142 
4143  return $amount;
4144 }
4145 
4146 
4158 function showDimensionInBestUnit($dimension, $unit, $type, $outputlangs, $round=-1, $forceunitoutput='no')
4159 {
4160  require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
4161 
4162  if (($forceunitoutput == 'no' && $dimension < 1/10000) || (is_numeric($forceunitoutput) && $forceunitoutput == -6))
4163  {
4164  $dimension = $dimension * 1000000;
4165  $unit = $unit - 6;
4166  }
4167  elseif (($forceunitoutput == 'no' && $dimension < 1/10) || (is_numeric($forceunitoutput) && $forceunitoutput == -3))
4168  {
4169  $dimension = $dimension * 1000;
4170  $unit = $unit - 3;
4171  }
4172  elseif (($forceunitoutput == 'no' && $dimension > 100000000) || (is_numeric($forceunitoutput) && $forceunitoutput == 6))
4173  {
4174  $dimension = $dimension / 1000000;
4175  $unit = $unit + 6;
4176  }
4177  elseif (($forceunitoutput == 'no' && $dimension > 100000) || (is_numeric($forceunitoutput) && $forceunitoutput == 3))
4178  {
4179  $dimension = $dimension / 1000;
4180  $unit = $unit + 3;
4181  }
4182 
4183  $ret=price($dimension, 0, $outputlangs, 0, 0, $round).' '.measuring_units_string($unit, $type);
4184 
4185  return $ret;
4186 }
4187 
4188 
4201 function get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
4202 {
4203  global $db, $conf, $mysoc;
4204 
4205  if (empty($thirdparty_seller) || ! is_object($thirdparty_seller)) $thirdparty_seller=$mysoc;
4206 
4207  dol_syslog("get_localtax tva=".$vatrate." local=".$local." thirdparty_buyer id=".(is_object($thirdparty_buyer)?$thirdparty_buyer->id:'')."/country_code=".(is_object($thirdparty_buyer)?$thirdparty_buyer->country_code:'')." thirdparty_seller id=".$thirdparty_seller->id."/country_code=".$thirdparty_seller->country_code." thirdparty_seller localtax1_assuj=".$thirdparty_seller->localtax1_assuj." thirdparty_seller localtax2_assuj=".$thirdparty_seller->localtax2_assuj);
4208 
4209  $vatratecleaned = $vatrate;
4210  if (preg_match('/^(.*)\s*\((.*)\)$/', $vatrate, $reg)) // If vat is "xx (yy)"
4211  {
4212  $vatratecleaned = trim($reg[1]);
4213  $vatratecode = $reg[2];
4214  }
4215 
4216  /*if ($thirdparty_buyer->country_code != $thirdparty_seller->country_code)
4217  {
4218  return 0;
4219  }*/
4220 
4221  // Some test to guess with no need to make database access
4222  if ($mysoc->country_code == 'ES') // For spain localtaxes 1 and 2, tax is qualified if buyer use local tax
4223  {
4224  if ($local == 1)
4225  {
4226  if (! $mysoc->localtax1_assuj || (string) $vatratecleaned == "0") return 0;
4227  if ($thirdparty_seller->id == $mysoc->id)
4228  {
4229  if (! $thirdparty_buyer->localtax1_assuj) return 0;
4230  }
4231  else
4232  {
4233  if (! $thirdparty_seller->localtax1_assuj) return 0;
4234  }
4235  }
4236 
4237  if ($local == 2)
4238  {
4239  if (! $mysoc->localtax2_assuj || (string) $vatratecleaned == "0") return 0;
4240  if ($thirdparty_seller->id == $mysoc->id)
4241  {
4242  if (! $thirdparty_buyer->localtax2_assuj) return 0;
4243  }
4244  else
4245  {
4246  if (! $thirdparty_seller->localtax2_assuj) return 0;
4247  }
4248  }
4249  }
4250  else
4251  {
4252  if ($local == 1 && ! $thirdparty_seller->localtax1_assuj) return 0;
4253  if ($local == 2 && ! $thirdparty_seller->localtax2_assuj) return 0;
4254  }
4255 
4256  // For some country MAIN_GET_LOCALTAXES_VALUES_FROM_THIRDPARTY is forced to on.
4257  if (in_array($mysoc->country_code, array('ES')))
4258  {
4259  $conf->global->MAIN_GET_LOCALTAXES_VALUES_FROM_THIRDPARTY = 1;
4260  }
4261 
4262  // Search local taxes
4263  if (! empty($conf->global->MAIN_GET_LOCALTAXES_VALUES_FROM_THIRDPARTY))
4264  {
4265  if ($local==1)
4266  {
4267  if ($thirdparty_seller != $mysoc)
4268  {
4269  if (!isOnlyOneLocalTax($local)) // TODO We should provide $vatrate to search on correct line and not always on line with highest vat rate
4270  {
4271  return $thirdparty_seller->localtax1_value;
4272  }
4273  }
4274  else // i am the seller
4275  {
4276  if (!isOnlyOneLocalTax($local)) // TODO If seller is me, why not always returning this, even if there is only one locatax vat.
4277  {
4278  return $conf->global->MAIN_INFO_VALUE_LOCALTAX1;
4279  }
4280  }
4281  }
4282  if ($local==2)
4283  {
4284  if ($thirdparty_seller != $mysoc)
4285  {
4286  if (!isOnlyOneLocalTax($local)) // TODO We should provide $vatrate to search on correct line and not always on line with highest vat rate
4287  // TODO We should also return value defined on thirdparty only if defined
4288  {
4289  return $thirdparty_seller->localtax2_value;
4290  }
4291  }
4292  else // i am the seller
4293  {
4294  if (!isOnlyOneLocalTax($local)) // This is for spain only, we don't return value found into datbase even if there is only one locatax vat.
4295  {
4296  return $conf->global->MAIN_INFO_VALUE_LOCALTAX2;
4297  }
4298  }
4299  }
4300  }
4301 
4302  // By default, search value of local tax on line of common tax
4303  $sql = "SELECT t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
4304  $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
4305  $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$thirdparty_seller->country_code."'";
4306  $sql .= " AND t.taux = ".((float) $vatratecleaned)." AND t.active = 1";
4307  if ($vatratecode) $sql.= " AND t.code ='".$vatratecode."'"; // If we have the code, we use it in priority
4308  else $sql.= " AND t.recuperableonly ='".$vatnpr."'";
4309  dol_syslog("get_localtax", LOG_DEBUG);
4310  $resql=$db->query($sql);
4311 
4312  if ($resql)
4313  {
4314  $obj = $db->fetch_object($resql);
4315  if ($local==1) return $obj->localtax1;
4316  elseif ($local==2) return $obj->localtax2;
4317  }
4318 
4319  return 0;
4320 }
4321 
4322 
4331 function isOnlyOneLocalTax($local)
4332 {
4333  $tax=get_localtax_by_third($local);
4334 
4335  $valors=explode(":", $tax);
4336 
4337  if (count($valors)>1)
4338  {
4339  return false;
4340  }
4341  else
4342  {
4343  return true;
4344  }
4345 }
4346 
4353 function get_localtax_by_third($local)
4354 {
4355  global $db, $mysoc;
4356  $sql ="SELECT t.localtax1, t.localtax2 ";
4357  $sql.=" FROM ".MAIN_DB_PREFIX."c_tva as t inner join ".MAIN_DB_PREFIX."c_country as c ON c.rowid=t.fk_pays";
4358  $sql.=" WHERE c.code = '".$mysoc->country_code."' AND t.active = 1 AND t.taux=(";
4359  $sql.=" SELECT max(tt.taux) FROM ".MAIN_DB_PREFIX."c_tva as tt inner join ".MAIN_DB_PREFIX."c_country as c ON c.rowid=tt.fk_pays";
4360  $sql.=" WHERE c.code = '".$mysoc->country_code."' AND tt.active = 1";
4361  $sql.=" )";
4362 
4363  $resql=$db->query($sql);
4364  if ($resql)
4365  {
4366  $obj = $db->fetch_object($resql);
4367  if ($local==1) return $obj->localtax1;
4368  elseif ($local==2) return $obj->localtax2;
4369  }
4370 
4371  return 0;
4372 
4373 }
4374 
4375 
4387 function getTaxesFromId($vatrate, $buyer=null, $seller=null, $firstparamisid=1)
4388 {
4389  global $db, $mysoc;
4390 
4391  dol_syslog("getTaxesFromId vatrowid=".$vatrate);
4392 
4393  // Search local taxes
4394  $sql = "SELECT t.rowid, t.code, t.taux as rate, t.recuperableonly as npr, t.accountancy_code_sell, t.accountancy_code_buy";
4395  $sql.= " FROM ".MAIN_DB_PREFIX."c_tva as t";
4396  if ($firstparamisid) $sql.= " WHERE t.rowid = ".(int) $vatrate;
4397  else
4398  {
4399  $vatratecleaned = $vatrate;
4400  $vatratecode = '';
4401  if (preg_match('/^(.*)\s*\((.*)\)$/', $vatrate, $reg)) // If vat is "xx (yy)"
4402  {
4403  $vatratecleaned = $reg[1];
4404  $vatratecode = $reg[2];
4405  }
4406 
4407  $sql.=", ".MAIN_DB_PREFIX."c_country as c";
4408  if ($mysoc->country_code == 'ES') $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$buyer->country_code."'"; // local tax in spain use the buyer country ??
4409  else $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$seller->country_code."'";
4410  $sql.= " AND t.taux = ".((float) $vatratecleaned)." AND t.active = 1";
4411  if ($vatratecode) $sql.= " AND t.code = '".$vatratecode."'";
4412  }
4413 
4414  $resql=$db->query($sql);
4415  if ($resql)
4416  {
4417  $obj = $db->fetch_object($resql);
4418  if ($obj) return array('rowid'=>$obj->rowid, 'code'=>$obj->code, 'rate'=>$obj->rate, 'npr'=>$obj->npr, 'accountancy_code_sell'=>$obj->accountancy_code_sell, 'accountancy_code_buy'=>$obj->accountancy_code_buy);
4419  else return array();
4420  }
4421  else dol_print_error($db);
4422 
4423  return array();
4424 }
4425 
4442 function getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
4443 {
4444  global $db, $mysoc;
4445 
4446  dol_syslog("getLocalTaxesFromRate vatrate=".$vatrate." local=".$local);
4447 
4448  // Search local taxes
4449  $sql = "SELECT t.localtax1, t.localtax1_type, t.localtax2, t.localtax2_type, t.accountancy_code_sell, t.accountancy_code_buy";
4450  $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t";
4451  if ($firstparamisid) $sql.= " WHERE t.rowid = ".(int) $vatrate;
4452  else
4453  {
4454  $vatratecleaned = $vatrate;
4455  $vatratecode = '';
4456  if (preg_match('/^(.*)\s*\((.*)\)$/', $vatrate, $reg)) // If vat is "xx (yy)"
4457  {
4458  $vatratecleaned = $reg[1];
4459  $vatratecode = $reg[2];
4460  }
4461 
4462  $sql.=", ".MAIN_DB_PREFIX."c_country as c";
4463  if ($mysoc->country_code == 'ES') $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$buyer->country_code."'"; // local tax in spain use the buyer country ??
4464  else $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$seller->country_code."'";
4465  $sql.= " AND t.taux = ".((float) $vatratecleaned)." AND t.active = 1";
4466  if ($vatratecode) $sql.= " AND t.code = '".$vatratecode."'";
4467  }
4468 
4469  $resql=$db->query($sql);
4470  if ($resql)
4471  {
4472  $obj = $db->fetch_object($resql);
4473  if ($local == 1)
4474  {
4475  if (! isOnlyOneLocalTax(1))
4476  {
4477  return array($obj->localtax1_type, get_localtax($vatrate, $local, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy);
4478  }
4479  else
4480  {
4481  return array($obj->localtax1_type, $obj->localtax1,$obj->accountancy_code_sell, $obj->accountancy_code_buy);
4482  }
4483  }
4484  elseif ($local == 2)
4485  {
4486  if (! isOnlyOneLocalTax(2))
4487  {
4488  return array($obj->localtax2_type, get_localtax($vatrate, $local, $buyer, $seller),$obj->accountancy_code_sell, $obj->accountancy_code_buy);
4489  }
4490  else
4491  {
4492  return array($obj->localtax2_type, $obj->localtax2,$obj->accountancy_code_sell, $obj->accountancy_code_buy);
4493  }
4494  }
4495  else
4496  {
4497  if(! isOnlyOneLocalTax(1))
4498  {
4499  if(! isOnlyOneLocalTax(2))
4500  {
4501  return array($obj->localtax1_type, get_localtax($vatrate, 1, $buyer, $seller), $obj->localtax2_type, get_localtax($vatrate, 2, $buyer, $seller), $obj->accountancy_code_sell,$obj->accountancy_code_buy);
4502  }
4503  else
4504  {
4505  return array($obj->localtax1_type, get_localtax($vatrate, 1, $buyer, $seller), $obj->localtax2_type, $obj->localtax2, $obj->accountancy_code_sell, $obj->accountancy_code_buy);
4506  }
4507  }
4508  else
4509  {
4510  if(! isOnlyOneLocalTax(2))
4511  {
4512  return array($obj->localtax1_type, $obj->localtax1, $obj->localtax2_type, get_localtax($vatrate, 2, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy);
4513  }
4514  else
4515  {
4516  return array($obj->localtax1_type, $obj->localtax1, $obj->localtax2_type, $obj->localtax2, $obj->accountancy_code_sell, $obj->accountancy_code_buy);
4517  }
4518  }
4519  }
4520  }
4521 
4522  return 0;
4523 }
4524 
4535 function get_product_vat_for_country($idprod, $thirdparty_seller, $idprodfournprice=0)
4536 {
4537  global $db,$conf,$mysoc;
4538 
4539  require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
4540 
4541  $ret=0;
4542  $found=0;
4543 
4544  if ($idprod > 0)
4545  {
4546  // Load product
4547  $product=new Product($db);
4548  $result=$product->fetch($idprod);
4549 
4550  if ($mysoc->country_code == $thirdparty_seller->country_code) // If selling country is ours
4551  {
4552  if ($idprodfournprice > 0) // We want vat for product for a "supplier" order or invoice
4553  {
4554  $product->get_buyprice($idprodfournprice,0,0,0);
4555  $ret=$product->vatrate_supplier;
4556  if ($product->default_vat_code) $ret.=' ('.$product->default_vat_code.')';
4557  }
4558  else
4559  {
4560  $ret=$product->tva_tx; // Default vat of product we defined
4561  if ($product->default_vat_code) $ret.=' ('.$product->default_vat_code.')';
4562  }
4563  $found=1;
4564  }
4565  else
4566  {
4567  // TODO Read default product vat according to countrycode and product. Vat for couple countrycode/product is a feature not implemeted yet.
4568  // May be usefull/required if hidden option SERVICE_ARE_ECOMMERCE_200238EC is on
4569  }
4570  }
4571 
4572  if (! $found)
4573  {
4574  if (empty($conf->global->MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS))
4575  {
4576  // If vat of product for the country not found or not defined, we return the first higher vat of country.
4577  $sql = "SELECT taux as vat_rate";
4578  $sql.= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
4579  $sql.= " WHERE t.active=1 AND t.fk_pays = c.rowid AND c.code='".$thirdparty_seller->country_code."'";
4580  $sql.= " ORDER BY t.taux DESC, t.code ASC, t.recuperableonly ASC";
4581  $sql.= $db->plimit(1);
4582 
4583  $resql=$db->query($sql);
4584  if ($resql)
4585  {
4586  $obj=$db->fetch_object($resql);
4587  if ($obj)
4588  {
4589  $ret=$obj->vat_rate;
4590  }
4591  $db->free($sql);
4592  }
4593  else dol_print_error($db);
4594  }
4595  else $ret=$conf->global->MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS; // Forced value if autodetect fails
4596  }
4597 
4598  dol_syslog("get_product_vat_for_country: ret=".$ret);
4599  return $ret;
4600 }
4601 
4611 function get_product_localtax_for_country($idprod, $local, $thirdparty_seller)
4612 {
4613  global $db,$mysoc;
4614 
4615  if (! class_exists('Product')) {
4616  require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
4617  }
4618 
4619  $ret=0;
4620  $found=0;
4621 
4622  if ($idprod > 0)
4623  {
4624  // Load product
4625  $product=new Product($db);
4626  $result=$product->fetch($idprod);
4627 
4628  if ($mysoc->country_code == $thirdparty_seller->country_code) // If selling country is ours
4629  {
4630  /* Not defined yet, so we don't use this
4631  if ($local==1) $ret=$product->localtax1_tx;
4632  elseif ($local==2) $ret=$product->localtax2_tx;
4633  $found=1;
4634  */
4635  }
4636  else
4637  {
4638  // TODO Read default product vat according to countrycode and product
4639 
4640 
4641  }
4642  }
4643 
4644  if (! $found)
4645  {
4646  // If vat of product for the country not found or not defined, we return higher vat of country.
4647  $sql = "SELECT taux as vat_rate, localtax1, localtax2";
4648  $sql.= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
4649  $sql.= " WHERE t.active=1 AND t.fk_pays = c.rowid AND c.code='".$thirdparty_seller->country_code."'";
4650  $sql.= " ORDER BY t.taux DESC, t.recuperableonly ASC";
4651  $sql.= $db->plimit(1);
4652 
4653  $resql=$db->query($sql);
4654  if ($resql)
4655  {
4656  $obj=$db->fetch_object($resql);
4657  if ($obj)
4658  {
4659  if ($local==1) $ret=$obj->localtax1;
4660  elseif ($local==2) $ret=$obj->localtax2;
4661  }
4662  }
4663  else dol_print_error($db);
4664  }
4665 
4666  dol_syslog("get_product_localtax_for_country: ret=".$ret);
4667  return $ret;
4668 }
4669 
4686 function get_default_tva(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
4687 {
4688  global $conf;
4689 
4690  require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
4691 
4692  // Note: possible values for tva_assuj are 0/1 or franchise/reel
4693  $seller_use_vat=((is_numeric($thirdparty_seller->tva_assuj) && ! $thirdparty_seller->tva_assuj) || (! is_numeric($thirdparty_seller->tva_assuj) && $thirdparty_seller->tva_assuj=='franchise'))?0:1;
4694 
4695  $seller_country_code = $thirdparty_seller->country_code;
4696  $seller_in_cee = isInEEC($thirdparty_seller);
4697 
4698  $buyer_country_code = $thirdparty_buyer->country_code;
4699  $buyer_in_cee = isInEEC($thirdparty_buyer);
4700 
4701  dol_syslog("get_default_tva: seller use vat=".$seller_use_vat.", seller country=".$seller_country_code.", seller in cee=".$seller_in_cee.", buyer country=".$buyer_country_code.", buyer in cee=".$buyer_in_cee.", idprod=".$idprod.", idprodfournprice=".$idprodfournprice.", SERVICE_ARE_ECOMMERCE_200238EC=".(! empty($conf->global->SERVICES_ARE_ECOMMERCE_200238EC)?$conf->global->SERVICES_ARE_ECOMMERCE_200238EC:''));
4702 
4703  // If services are eServices according to EU Council Directive 2002/38/EC (http://ec.europa.eu/taxation_customs/taxation/vat/traders/e-commerce/article_1610_en.htm)
4704  // we use the buyer VAT.
4705  if (! empty($conf->global->SERVICE_ARE_ECOMMERCE_200238EC))
4706  {
4707  if ($seller_in_cee && $buyer_in_cee && ! $thirdparty_buyer->isACompany())
4708  {
4709  //print 'VATRULE 0';
4710  return get_product_vat_for_country($idprod,$thirdparty_buyer,$idprodfournprice);
4711  }
4712  }
4713 
4714  // If seller does not use VAT
4715  if (! $seller_use_vat)
4716  {
4717  //print 'VATRULE 1';
4718  return 0;
4719  }
4720 
4721  // Le test ci-dessus ne devrait pas etre necessaire. Me signaler l'exemple du cas juridique concerne si le test suivant n'est pas suffisant.
4722 
4723  // Si le (pays vendeur = pays acheteur) alors la TVA par defaut=TVA du produit vendu. Fin de regle.
4724  if (($seller_country_code == $buyer_country_code)
4725  || (in_array($seller_country_code,array('FR,MC')) && in_array($buyer_country_code,array('FR','MC')))) // Warning ->country_code not always defined
4726  {
4727  //print 'VATRULE 2';
4728  return get_product_vat_for_country($idprod,$thirdparty_seller,$idprodfournprice);
4729  }
4730 
4731  // Si (vendeur et acheteur dans Communaute europeenne) et (bien vendu = moyen de transports neuf comme auto, bateau, avion) alors TVA par defaut=0 (La TVA doit etre paye par l'acheteur au centre d'impots de son pays et non au vendeur). Fin de regle.
4732  // Not supported
4733 
4734  // Si (vendeur et acheteur dans Communaute europeenne) et (acheteur = entreprise) alors TVA par defaut=0. Fin de regle
4735  // Si (vendeur et acheteur dans Communaute europeenne) et (acheteur = particulier) alors TVA par defaut=TVA du produit vendu. Fin de regle
4736  if (($seller_in_cee && $buyer_in_cee))
4737  {
4738  $isacompany=$thirdparty_buyer->isACompany();
4739  if ($isacompany)
4740  {
4741  //print 'VATRULE 3';
4742  return 0;
4743  }
4744  else
4745  {
4746  //print 'VATRULE 4';
4747  return get_product_vat_for_country($idprod,$thirdparty_seller,$idprodfournprice);
4748  }
4749  }
4750 
4751  // Sinon la TVA proposee par defaut=0. Fin de regle.
4752  // Rem: Cela signifie qu'au moins un des 2 est hors Communaute europeenne et que le pays differe
4753  //print 'VATRULE 5';
4754  return 0;
4755 }
4756 
4757 
4768 function get_default_npr(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
4769 {
4770  global $db;
4771 
4772  if ($idprodfournprice > 0)
4773  {
4774  if (! class_exists('ProductFournisseur'))
4775  require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php';
4776  $prodprice = new ProductFournisseur($db);
4777  $prodprice->fetch_product_fournisseur_price($idprodfournprice);
4778  return $prodprice->fourn_tva_npr;
4779  }
4780  elseif ($idprod > 0)
4781  {
4782  if (! class_exists('Product'))
4783  require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
4784  $prod = new Product($db);
4785  $prod->fetch($idprod);
4786  return $prod->tva_npr;
4787  }
4788 
4789  return 0;
4790 }
4791 
4805 function get_default_localtax($thirdparty_seller, $thirdparty_buyer, $local, $idprod=0)
4806 {
4807  global $mysoc;
4808 
4809  if (!is_object($thirdparty_seller)) return -1;
4810  if (!is_object($thirdparty_buyer)) return -1;
4811 
4812  if ($local==1) // Localtax 1
4813  {
4814  if ($mysoc->country_code == 'ES')
4815  {
4816  if (is_numeric($thirdparty_buyer->localtax1_assuj) && ! $thirdparty_buyer->localtax1_assuj) return 0;
4817  }
4818  else
4819  {
4820  // Si vendeur non assujeti a Localtax1, localtax1 par default=0
4821  if (is_numeric($thirdparty_seller->localtax1_assuj) && ! $thirdparty_seller->localtax1_assuj) return 0;
4822  if (! is_numeric($thirdparty_seller->localtax1_assuj) && $thirdparty_seller->localtax1_assuj=='localtax1off') return 0;
4823  }
4824  }
4825  elseif ($local==2) //I Localtax 2
4826  {
4827  // Si vendeur non assujeti a Localtax2, localtax2 par default=0
4828  if (is_numeric($thirdparty_seller->localtax2_assuj) && ! $thirdparty_seller->localtax2_assuj) return 0;
4829  if (! is_numeric($thirdparty_seller->localtax2_assuj) && $thirdparty_seller->localtax2_assuj=='localtax2off') return 0;
4830  }
4831 
4832  if ($thirdparty_seller->country_code == $thirdparty_buyer->country_code)
4833  {
4834  return get_product_localtax_for_country($idprod, $local, $thirdparty_seller);
4835  }
4836 
4837  return 0;
4838 }
4839 
4848 function yn($yesno, $case=1, $color=0)
4849 {
4850  global $langs;
4851  $result='unknown'; $classname='';
4852  if ($yesno == 1 || strtolower($yesno) == 'yes' || strtolower($yesno) == 'true') // A mettre avant test sur no a cause du == 0
4853  {
4854  $result=$langs->trans('yes');
4855  if ($case == 1 || $case == 3) $result=$langs->trans("Yes");
4856  if ($case == 2) $result='<input type="checkbox" value="1" checked disabled>';
4857  if ($case == 3) $result='<input type="checkbox" value="1" checked disabled> '.$result;
4858 
4859  $classname='ok';
4860  }
4861  elseif ($yesno == 0 || strtolower($yesno) == 'no' || strtolower($yesno) == 'false')
4862  {
4863  $result=$langs->trans("no");
4864  if ($case == 1 || $case == 3) $result=$langs->trans("No");
4865  if ($case == 2) $result='<input type="checkbox" value="0" disabled>';
4866  if ($case == 3) $result='<input type="checkbox" value="0" disabled> '.$result;
4867 
4868  if ($color == 2) $classname='ok';
4869  else $classname='error';
4870  }
4871  if ($color) return '<font class="'.$classname.'">'.$result.'</font>';
4872  return $result;
4873 }
4874 
4875 
4891 function get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart)
4892 {
4893  global $conf;
4894 
4895  $path = '';
4896 
4897  $arrayforoldpath=array('cheque','user','category','holiday','supplier_invoice','invoice_supplier','mailing','supplier_payment');
4898  if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) $arrayforoldpath[]='product';
4899  if (! empty($level) && in_array($modulepart, $arrayforoldpath))
4900  {
4901  // This part should be removed once all code is using "get_exdir" to forge path, with all parameters provided.
4902  if (empty($alpha)) $num = preg_replace('/([^0-9])/i','',$num);
4903  else $num = preg_replace('/^.*\-/i','',$num);
4904  $num = substr("000".$num, -$level);
4905  if ($level == 1) $path = substr($num,0,1);
4906  if ($level == 2) $path = substr($num,1,1).'/'.substr($num,0,1);
4907  if ($level == 3) $path = substr($num,2,1).'/'.substr($num,1,1).'/'.substr($num,0,1);
4908  }
4909  else
4910  {
4911  // TODO
4912  // We will enhance here a common way of forging path for document storage
4913  // Here, object->id, object->ref and modulepart are required.
4914  if (in_array($modulepart, array('thirdparty','contact','member','propal','proposal','commande','order','facture','invoice','shipment')))
4915  {
4916  $path=($object->ref?$object->ref:$object->id);
4917  }
4918  }
4919 
4920  if (empty($withoutslash) && ! empty($path)) $path.='/';
4921 
4922  return $path;
4923 }
4924 
4933 function dol_mkdir($dir, $dataroot='', $newmask=null)
4934 {
4935  global $conf;
4936 
4937  dol_syslog("functions.lib::dol_mkdir: dir=".$dir,LOG_INFO);
4938 
4939  $dir_osencoded=dol_osencode($dir);
4940  if (@is_dir($dir_osencoded)) return 0;
4941 
4942  $nberr=0;
4943  $nbcreated=0;
4944 
4945  $ccdir='';
4946  if (! empty($dataroot)) {
4947  // Remove data root from loop
4948  $dir = str_replace($dataroot.'/', '', $dir);
4949  $ccdir = $dataroot.'/';
4950  }
4951 
4952  $cdir = explode("/", $dir);
4953  $num=count($cdir);
4954  for ($i = 0; $i < $num; $i++)
4955  {
4956  if ($i > 0) $ccdir .= '/'.$cdir[$i];
4957  else $ccdir .= $cdir[$i];
4958  if (preg_match("/^.:$/",$ccdir,$regs)) continue; // Si chemin Windows incomplet, on poursuit par rep suivant
4959 
4960  // Attention, le is_dir() peut echouer bien que le rep existe.
4961  // (ex selon config de open_basedir)
4962  if ($ccdir)
4963  {
4964  $ccdir_osencoded=dol_osencode($ccdir);
4965  if (! @is_dir($ccdir_osencoded))
4966  {
4967  dol_syslog("functions.lib::dol_mkdir: Directory '".$ccdir."' does not exists or is outside open_basedir PHP setting.",LOG_DEBUG);
4968 
4969  umask(0);
4970  $dirmaskdec=octdec($newmask);
4971  if (empty($newmask)) {
4972  $dirmaskdec = empty( $conf->global->MAIN_UMASK ) ? octdec( '0755' ) : octdec( $conf->global->MAIN_UMASK );
4973  }
4974  $dirmaskdec |= octdec('0111'); // Set x bit required for directories
4975  if (! @mkdir($ccdir_osencoded, $dirmaskdec))
4976  {
4977  // Si le is_dir a renvoye une fausse info, alors on passe ici.
4978  dol_syslog("functions.lib::dol_mkdir: Fails to create directory '".$ccdir."' or directory already exists.",LOG_WARNING);
4979  $nberr++;
4980  }
4981  else
4982  {
4983  dol_syslog("functions.lib::dol_mkdir: Directory '".$ccdir."' created",LOG_DEBUG);
4984  $nberr=0; // On remet a zero car si on arrive ici, cela veut dire que les echecs precedents peuvent etre ignore
4985  $nbcreated++;
4986  }
4987  }
4988  else
4989  {
4990  $nberr=0; // On remet a zero car si on arrive ici, cela veut dire que les echecs precedents peuvent etre ignores
4991  }
4992  }
4993  }
4994  return ($nberr ? -$nberr : $nbcreated);
4995 }
4996 
4997 
5003 function picto_required()
5004 {
5005  return '<span class="fieldrequired">*</span>';
5006 }
5007 
5008 
5023 function dol_string_nohtmltag($stringtoclean,$removelinefeed=1,$pagecodeto='UTF-8')
5024 {
5025  // TODO Try to replace with strip_tags($stringtoclean)
5026  $pattern = "/<[^<>]+>/";
5027  $stringtoclean = preg_replace('/<br[^>]*>/', "\n", $stringtoclean);
5028  $temp = dol_html_entity_decode($stringtoclean,ENT_COMPAT,$pagecodeto);
5029 
5030  // Exemple of $temp: <a href="/myurl" title="<u>A title</u>">0000-021</a>
5031  $temp = preg_replace($pattern,"",$temp); // pass 1
5032  // $temp after pass 1: <a href="/myurl" title="A title">0000-021
5033  $temp = preg_replace($pattern,"",$temp); // pass 2
5034  // $temp after pass 2: 0000-021
5035 
5036  // Supprime aussi les retours
5037  if ($removelinefeed) $temp=str_replace(array("\r\n","\r","\n")," ",$temp);
5038 
5039  // et les espaces doubles
5040  while(strpos($temp," "))
5041  {
5042  $temp = str_replace(" "," ",$temp);
5043  }
5044 
5045  return trim($temp);
5046 }
5047 
5048 
5057 function dolGetFirstLineOfText($text, $nboflines=1)
5058 {
5059  if ($nboflines == 1)
5060  {
5061  if (dol_textishtml($text))
5062  {
5063  $firstline=preg_replace('/<br[^>]*>.*$/s','',$text); // The s pattern modifier means the . can match newline characters
5064  $firstline=preg_replace('/<div[^>]*>.*$/s','',$firstline); // The s pattern modifier means the . can match newline characters
5065 
5066  }
5067  else
5068  {
5069  $firstline=preg_replace('/[\n\r].*/','',$text);
5070  }
5071  return $firstline.((strlen($firstline) != strlen($text))?'...':'');
5072  }
5073  else
5074  {
5075  $ishtml=0;
5076  if (dol_textishtml($text))
5077  {
5078  $text=preg_replace('/\n/','',$text);
5079  $ishtml=1;
5080  $repTable = array("\t" => " ", "\n" => " ", "\r" => " ", "\0" => " ", "\x0B" => " ");
5081  }
5082  else
5083  {
5084  $repTable = array("\t" => " ", "\n" => "<br>", "\r" => " ", "\0" => " ", "\x0B" => " ");
5085  }
5086 
5087  $text = strtr($text, $repTable);
5088  if ($charset == 'UTF-8') { $pattern = '/(<br[^>]*>)/Uu'; } // /U is to have UNGREEDY regex to limit to one html tag. /u is for UTF8 support
5089  else $pattern = '/(<br[^>]*>)/U'; // /U is to have UNGREEDY regex to limit to one html tag.
5090  $a = preg_split($pattern, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
5091 
5092  $firstline='';
5093  $i=0;
5094  $nba = count($a); // 2x nb of lines in $a because $a contains also a line for each new line separator
5095  while (($i < $nba) && ($i < ($nboflines * 2)))
5096  {
5097  if ($i % 2 == 0) $firstline .= $a[$i];
5098  elseif (($i < (($nboflines * 2) - 1)) && ($i < ($nba - 1))) $firstline .= ($ishtml?"<br>\n":"\n");
5099  $i++;
5100  }
5101  unset($a);
5102  return $firstline.(($i < $nba)?'...':'');
5103  }
5104 }
5105 
5106 
5116 function dol_nl2br($stringtoencode,$nl2brmode=0,$forxml=false)
5117 {
5118  if (!$nl2brmode) {
5119  return nl2br($stringtoencode, $forxml);
5120  } else {
5121  $ret=preg_replace('/(\r\n|\r|\n)/i', ($forxml?'<br />':'<br>'), $stringtoencode);
5122  return $ret;
5123  }
5124 }
5125 
5126 
5144 function dol_htmlentitiesbr($stringtoencode, $nl2brmode=0, $pagecodefrom='UTF-8', $removelasteolbr=1)
5145 {
5146  $newstring=$stringtoencode;
5147  if (dol_textishtml($stringtoencode)) // Check if text is already HTML or not
5148  {
5149  $newstring=preg_replace('/<br(\s[\sa-zA-Z_="]*)?\/?>/i','<br>',$newstring); // Replace "<br type="_moz" />" by "<br>". It's same and avoid pb with FPDF.
5150  if ($removelasteolbr) $newstring=preg_replace('/<br>$/i','',$newstring); // Remove last <br> (remove only last one)
5151  $newstring=strtr($newstring,array('&'=>'__and__','<'=>'__lt__','>'=>'__gt__','"'=>'__dquot__'));
5152  $newstring=dol_htmlentities($newstring,ENT_COMPAT,$pagecodefrom); // Make entity encoding
5153  $newstring=strtr($newstring,array('__and__'=>'&','__lt__'=>'<','__gt__'=>'>','__dquot__'=>'"'));
5154  }
5155  else
5156  {
5157  if ($removelasteolbr) $newstring=preg_replace('/(\r\n|\r|\n)$/i','',$newstring); // Remove last \n (may remove several)
5158  $newstring=dol_nl2br(dol_htmlentities($newstring,ENT_COMPAT,$pagecodefrom),$nl2brmode);
5159  }
5160  // Other substitutions that htmlentities does not do
5161  //$newstring=str_replace(chr(128),'&euro;',$newstring); // 128 = 0x80. Not in html entity table. // Seems useles with TCPDF. Make bug with UTF8 languages
5162  return $newstring;
5163 }
5164 
5172 function dol_htmlentitiesbr_decode($stringtodecode,$pagecodeto='UTF-8')
5173 {
5174  $ret=dol_html_entity_decode($stringtodecode,ENT_COMPAT,$pagecodeto);
5175  $ret=preg_replace('/'."\r\n".'<br(\s[\sa-zA-Z_="]*)?\/?>/i',"<br>",$ret);
5176  $ret=preg_replace('/<br(\s[\sa-zA-Z_="]*)?\/?>'."\r\n".'/i',"\r\n",$ret);
5177  $ret=preg_replace('/<br(\s[\sa-zA-Z_="]*)?\/?>'."\n".'/i',"\n",$ret);
5178  $ret=preg_replace('/<br(\s[\sa-zA-Z_="]*)?\/?>/i',"\n",$ret);
5179  return $ret;
5180 }
5181 
5188 function dol_htmlcleanlastbr($stringtodecode)
5189 {
5190  $ret=preg_replace('/(<br>|<br(\s[\sa-zA-Z_="]*)?\/?>|'."\n".'|'."\r".')+$/i',"",$stringtodecode);
5191  return $ret;
5192 }
5193 
5202 function dol_html_entity_decode($a,$b,$c='UTF-8')
5203 {
5204  return html_entity_decode($a,$b,$c);
5205 }
5206 
5217 function dol_htmlentities($string, $flags=null, $encoding='UTF-8', $double_encode=false)
5218 {
5219  return htmlentities($string, $flags, $encoding, $double_encode);
5220 }
5221 
5231 {
5232  $len=dol_strlen($s);
5233  $ok=1;
5234  for($scursor=0;$scursor<$len;$scursor++)
5235  {
5236  $ordchar=ord($s{$scursor});
5237  //print $scursor.'-'.$ordchar.'<br>';
5238  if ($ordchar < 32 && $ordchar != 13 && $ordchar != 10) { $ok=0; break; }
5239  if ($ordchar > 126 && $ordchar < 160) { $ok=0; break; }
5240  }
5241  return $ok;
5242 }
5243 
5244 
5253 function dol_nboflines($s,$maxchar=0)
5254 {
5255  if ($s == '') return 0;
5256  $arraystring=explode("\n",$s);
5257  $nb=count($arraystring);
5258 
5259  return $nb;
5260 }
5261 
5262 
5272 function dol_nboflines_bis($text,$maxlinesize=0,$charset='UTF-8')
5273 {
5274  $repTable = array("\t" => " ", "\n" => "<br>", "\r" => " ", "\0" => " ", "\x0B" => " ");
5275  if (dol_textishtml($text)) $repTable = array("\t" => " ", "\n" => " ", "\r" => " ", "\0" => " ", "\x0B" => " ");
5276 
5277  $text = strtr($text, $repTable);
5278  if ($charset == 'UTF-8') { $pattern = '/(<br[^>]*>)/Uu'; } // /U is to have UNGREEDY regex to limit to one html tag. /u is for UTF8 support
5279  else $pattern = '/(<br[^>]*>)/U'; // /U is to have UNGREEDY regex to limit to one html tag.
5280  $a = preg_split($pattern, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
5281 
5282  $nblines = (int) floor((count($a)+1)/2);
5283  // count possible auto line breaks
5284  if($maxlinesize)
5285  {
5286  foreach ($a as $line)
5287  {
5288  if (dol_strlen($line)>$maxlinesize)
5289  {
5290  //$line_dec = html_entity_decode(strip_tags($line));
5291  $line_dec = html_entity_decode($line);
5292  if(dol_strlen($line_dec)>$maxlinesize)
5293  {
5294  $line_dec=wordwrap($line_dec,$maxlinesize,'\n',true);
5295  $nblines+=substr_count($line_dec,'\n');
5296  }
5297  }
5298  }
5299  }
5300 
5301  unset($a);
5302  return $nblines;
5303 }
5304 
5313 {
5314  dol_syslog(__FUNCTION__ . " is deprecated", LOG_WARNING);
5315 
5316  return microtime(true);
5317 }
5318 
5327 function dol_textishtml($msg,$option=0)
5328 {
5329  if ($option == 1)
5330  {
5331  if (preg_match('/<html/i',$msg)) return true;
5332  elseif (preg_match('/<body/i',$msg)) return true;
5333  elseif (preg_match('/<br/i',$msg)) return true;
5334  return false;
5335  }
5336  else
5337  {
5338  if (preg_match('/<html/i',$msg)) return true;
5339  elseif (preg_match('/<body/i',$msg)) return true;
5340  elseif (preg_match('/<(b|em|i|u)>/i',$msg)) return true;
5341  elseif (preg_match('/<(br|div|font|li|p|span|strong|table)>/i',$msg)) return true;
5342  elseif (preg_match('/<(br|div|font|li|p|span|strong|table)\s+[^<>\/]*>/i',$msg)) return true;
5343  elseif (preg_match('/<(br|div|font|li|p|span|strong|table)\s+[^<>\/]*\/>/i',$msg)) return true;
5344  elseif (preg_match('/<img\s+[^<>]*src[^<>]*>/i',$msg)) return true; // must accept <img src="http://example.com/aaa.png" />
5345  elseif (preg_match('/<a\s+[^<>]*href[^<>]*>/i',$msg)) return true; // must accept <a href="http://example.com/aaa.png" />
5346  elseif (preg_match('/<h[0-9]>/i',$msg)) return true;
5347  elseif (preg_match('/&[A-Z0-9]{1,6};/i',$msg)) return true; // Html entities names (http://www.w3schools.com/tags/ref_entities.asp)
5348  elseif (preg_match('/&#[0-9]{2,3};/i',$msg)) return true; // Html entities numbers (http://www.w3schools.com/tags/ref_entities.asp)
5349  return false;
5350  }
5351 }
5352 
5366 function dol_concatdesc($text1,$text2,$forxml=false)
5367 {
5368  $ret='';
5369  $ret.= (! dol_textishtml($text1) && dol_textishtml($text2))?dol_nl2br($text1, 0, $forxml):$text1;
5370  $ret.= (! empty($text1) && ! empty($text2)) ? ((dol_textishtml($text1) || dol_textishtml($text2))?($forxml?"<br >\n":"<br>\n") : "\n") : "";
5371  $ret.= (dol_textishtml($text1) && ! dol_textishtml($text2))?dol_nl2br($text2, 0, $forxml):$text2;
5372  return $ret;
5373 }
5374 
5375 
5386 function getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null)
5387 {
5388  global $db, $conf, $mysoc, $user;
5389 
5390  $substitutionarray=array();
5391 
5392  if (empty($exclude) || ! in_array('system', $exclude))
5393  {
5394  $substitutionarray['__(AnyTranslationKey)__']=$outputlangs->trans('TranslationOfKey');
5395  $substitutionarray['__[AnyConstantKey]__']=$outputlangs->trans('ValueOfConstant');
5396  $substitutionarray['__DOL_MAIN_URL_ROOT__']=DOL_MAIN_URL_ROOT;
5397  }
5398  if ((empty($exclude) || ! in_array('mycompany', $exclude)) && is_object($mysoc))
5399  {
5400  $substitutionarray=array_merge($substitutionarray, array(
5401  '__MYCOMPANY_NAME__' => $mysoc->name,
5402  '__MYCOMPANY_EMAIL__' => $mysoc->email,
5403  '__MYCOMPANY_PROFID1__' => $mysoc->idprof1,
5404  '__MYCOMPANY_PROFID2__' => $mysoc->idprof2,
5405  '__MYCOMPANY_PROFID3__' => $mysoc->idprof3,
5406  '__MYCOMPANY_PROFID4__' => $mysoc->idprof4,
5407  '__MYCOMPANY_PROFID5__' => $mysoc->idprof5,
5408  '__MYCOMPANY_PROFID6__' => $mysoc->idprof6,
5409  '__MYCOMPANY_CAPITAL__' => $mysoc->capital,
5410  '__MYCOMPANY_FULLADDRESS__' => $mysoc->getFullAddress(1, ', '),
5411  '__MYCOMPANY_ADDRESS__' => $mysoc->address,
5412  '__MYCOMPANY_ZIP__' => $mysoc->zip,
5413  '__MYCOMPANY_TOWN__' => $mysoc->town,
5414  '__MYCOMPANY_COUNTRY__' => $mysoc->country,
5415  '__MYCOMPANY_COUNTRY_ID__' => $mysoc->country_id
5416  ));
5417  }
5418  if (($onlykey || is_object($object)) && (empty($exclude) || ! in_array('object', $exclude)))
5419  {
5420  if ($onlykey)
5421  {
5422  $substitutionarray['__ID__'] = '__ID__';
5423  $substitutionarray['__REF__'] = '__REF__';
5424  $substitutionarray['__REFCLIENT__'] = '__REFCLIENT__';
5425  $substitutionarray['__REFSUPPLIER__'] = '__REFSUPPLIER__';
5426  $substitutionarray['__EXTRAFIELD_XXX__'] = '__EXTRAFIELD_XXX__';
5427 
5428  $substitutionarray['__THIRDPARTY_ID__'] = '__THIRDPARTY_ID__';
5429  $substitutionarray['__THIRDPARTY_NAME__'] = '__THIRDPARTY_NAME__';
5430 
5431  if (is_object($object) && $object->element == 'shipping')
5432  {
5433  $substitutionarray['__MEMBER_CIVILITY__'] = '__MEMBER_CIVILITY__';
5434  $substitutionarray['__MEMBER_FIRSTNAME__'] = '__MEMBER_FIRSTNAME__';
5435  $substitutionarray['__MEMBER_LASTNAME__'] = '__MEMBER_LASTNAME__';
5436  }
5437  $substitutionarray['__PROJECT_ID__'] = '__PROJECT_ID__';
5438  $substitutionarray['__PROJECT_REF__'] = '__PROJECT_REF__';
5439  $substitutionarray['__PROJECT_NAME__'] = '__PROJECT_NAME__';
5440 
5441  $substitutionarray['__CONTRACT_HIGHEST_PLANNED_START_DATE__'] = 'Highest date planned for a service start';
5442  $substitutionarray['__CONTRACT_HIGHEST_PLANNED_START_DATETIME__'] = 'Highest date and hour planned for service start';
5443  $substitutionarray['__CONTRACT_LOWEST_EXPIRATION_DATE__'] = 'Lowest data for planned expiration of service';
5444  $substitutionarray['__CONTRACT_LOWEST_EXPIRATION_DATETIME__'] = 'Lowest date and hour for planned expiration of service';
5445 
5446  $substitutionarray['__ONLINE_PAYMENT_URL__'] = 'LinkToPayOnlineIfApplicable';
5447  $substitutionarray['__SECUREKEYPAYMENT__'] = 'Security key (if key is not unique per record)';
5448  $substitutionarray['__SECUREKEYPAYMENT_MEMBER__'] = 'Security key for payment on a member subscription (one key per member)';
5449  $substitutionarray['__SECUREKEYPAYMENT_ORDER__'] = 'Security key for payment on an order';
5450  $substitutionarray['__SECUREKEYPAYMENT_INVOICE__'] = 'Security key for payment on an invoice';
5451  $substitutionarray['__SECUREKEYPAYMENT_CONTRACTLINE__'] = 'Security key for payment on a a service';
5452 
5453  if (is_object($object) && $object->element == 'shipping')
5454  {
5455  $substitutionarray['__SHIPPINGTRACKNUM__']='Shipping tacking number';
5456  $substitutionarray['__SHIPPINGTRACKNUMURL__']='Shipping tracking url';
5457  }
5458  }
5459  else
5460  {
5461  $substitutionarray['__ID__'] = $object->id;
5462  $substitutionarray['__REF__'] = $object->ref;
5463  $substitutionarray['__REFCLIENT__'] = (isset($object->ref_client) ? $object->ref_client : (isset($object->ref_customer) ? $object->ref_customer : ''));
5464  $substitutionarray['__REFSUPPLIER__'] = (isset($object->ref_supplier) ? $object->ref_supplier : '');
5465 
5466  // TODO USe this ?
5467  $msgishtml = 0;
5468 
5469  $birthday = dol_print_date($object->birth,'day');
5470 
5471  if (method_exists($object, 'getCivilityLabel')) $substitutionarray['__MEMBER_CIVILITY__'] = $object->getCivilityLabel();
5472  $substitutionarray['__MEMBER_FIRSTNAME__']=$msgishtml?dol_htmlentitiesbr($object->firstname):$object->firstname;
5473  $substitutionarray['__MEMBER_LASTNAME__']=$msgishtml?dol_htmlentitiesbr($object->lastname):$object->lastname;
5474  if (method_exists($object, 'getFullName')) $substitutionarray['__MEMBER_FULLNAME__']=$msgishtml?dol_htmlentitiesbr($object->getFullName($outputlangs)):$object->getFullName($outputlangs);
5475  $substitutionarray['__MEMBER_COMPANY__']=$msgishtml?dol_htmlentitiesbr($object->societe):$object->societe;
5476  $substitutionarray['__MEMBER_ADDRESS__']=$msgishtml?dol_htmlentitiesbr($object->address):$object->address;
5477  $substitutionarray['__MEMBER_ZIP__']=$msgishtml?dol_htmlentitiesbr($object->zip):$object->zip;
5478  $substitutionarray['__MEMBER_TOWN__']=$msgishtml?dol_htmlentitiesbr($object->town):$object->town;
5479  $substitutionarray['__MEMBER_COUNTRY__']=$msgishtml?dol_htmlentitiesbr($object->country):$object->country;
5480  $substitutionarray['__MEMBER_EMAIL__']=$msgishtml?dol_htmlentitiesbr($object->email):$object->email;
5481  $substitutionarray['__MEMBER_BIRTH__']=$msgishtml?dol_htmlentitiesbr($birthday):$birthday;
5482  $substitutionarray['__MEMBER_PHOTO__']=$msgishtml?dol_htmlentitiesbr($object->photo):$object->photo;
5483  $substitutionarray['__MEMBER_LOGIN__']=$msgishtml?dol_htmlentitiesbr($object->login):$object->login;
5484  $substitutionarray['__MEMBER_PASSWORD__']=$msgishtml?dol_htmlentitiesbr($object->pass):$object->pass;
5485  $substitutionarray['__MEMBER_PHONE__']=$msgishtml?dol_htmlentitiesbr($object->phone):$object->phone;
5486  $substitutionarray['__MEMBER_PHONEPRO__']=$msgishtml?dol_htmlentitiesbr($object->phone_perso):$object->phone_perso;
5487  $substitutionarray['__MEMBER_PHONEMOBILE__']=$msgishtml?dol_htmlentitiesbr($object->phone_mobile):$object->phone_mobile;
5488 
5489  if (is_object($object) && $object->element == 'societe')
5490  {
5491  $substitutionarray['__THIRDPARTY_ID__'] = (is_object($object)?$object->id:'');
5492  $substitutionarray['__THIRDPARTY_NAME__'] = (is_object($object)?$object->name:'');
5493  }
5494  elseif (is_object($object->thirdparty) && $object->thirdparty->id > 0)
5495  {
5496  $substitutionarray['__THIRDPARTY_ID__'] = (is_object($object->thirdparty)?$object->thirdparty->id:'');
5497  $substitutionarray['__THIRDPARTY_NAME__'] = (is_object($object->thirdparty)?$object->thirdparty->name:'');
5498  }
5499 
5500  if (is_object($object->projet) && $object->projet->id > 0)
5501  {
5502  $substitutionarray['__PROJECT_ID__'] = (is_object($object->projet)?$object->projet->id:'');
5503  $substitutionarray['__PROJECT_REF__'] = (is_object($object->projet)?$object->projet->ref:'');
5504  $substitutionarray['__PROJECT_NAME__'] = (is_object($object->projet)?$object->projet->title:'');
5505  }
5506 
5507  if (is_object($object) && $object->element == 'shipping')
5508  {
5509  $substitutionarray['__SHIPPINGTRACKNUM__']=$object->tracking_number;
5510  $substitutionarray['__SHIPPINGTRACKNUMURL__']=$object->tracking_url;
5511  }
5512 
5513  if (is_object($object) && $object->element == 'contrat' && is_array($object->lines))
5514  {
5515  $dateplannedstart='';
5516  $datenextexpiration='';
5517  foreach($object->lines as $line)
5518  {
5519  if ($line->date_ouverture_prevue > $dateplannedstart) $dateplannedstart = $line->date_ouverture_prevue;
5520  if ($line->statut == 4 && $line->date_fin_prevue && (! $datenextexpiration || $line->date_fin_prevue < $datenextexpiration)) $datenextexpiration = $line->date_fin_prevue;
5521  }
5522  $substitutionarray['__CONTRACT_HIGHEST_PLANNED_START_DATE__'] = dol_print_date($dateplannedstart, 'dayrfc');
5523  $substitutionarray['__CONTRACT_HIGHEST_PLANNED_START_DATETIME__'] = dol_print_date($dateplannedstart, 'standard');
5524  $substitutionarray['__CONTRACT_LOWEST_EXPIRATION_DATE__'] = dol_print_date($datenextexpiration, 'dayrfc');
5525  $substitutionarray['__CONTRACT_LOWEST_EXPIRATION_DATETIME__'] = dol_print_date($datenextexpiration, 'standard');
5526  }
5527 
5528  // Create dynamic tags for __EXTRAFIELD_FIELD__
5529  if ($object->table_element && $object->id > 0)
5530  {
5531  $extrafieldstmp = new ExtraFields($db);
5532  $extralabels = $extrafieldstmp->fetch_name_optionals_label($object->table_element, true);
5533  $object->fetch_optionals($object->id, $extralabels);
5534  foreach ($extrafieldstmp->attribute_label as $key => $label) {
5535  $substitutionarray['__EXTRAFIELD_' . strtoupper($key) . '__'] = $object->array_options['options_' . $key];
5536  }
5537  }
5538 
5539  $substitutionarray['__ONLINE_PAYMENT_URL__'] = 'TODO';
5540  }
5541  }
5542  if (empty($exclude) || ! in_array('objectamount', $exclude))
5543  {
5544  $substitutionarray['__DATE_YMD__'] = is_object($object)?(isset($object->date) ? dol_print_date($object->date, 'day', 0, $outputlangs) : '') : '';
5545  $substitutionarray['__DATE_DUE_YMD__'] = is_object($object)?(isset($object->date_lim_reglement)? dol_print_date($object->date_lim_reglement, 'day', 0, $outputlangs) : '') : '';
5546  $substitutionarray['__AMOUNT__'] = is_object($object)?$object->total_ttc:'';
5547  $substitutionarray['__AMOUNT_EXCL_TAX__'] = is_object($object)?$object->total_ht:'';
5548  $substitutionarray['__AMOUNT_VAT__'] = is_object($object)?($object->total_vat?$object->total_vat:$object->total_tva):'';
5549  if ($onlykey != 2 || $mysoc->useLocalTax(1)) $substitutionarray['__AMOUNT_TAX2__'] = is_object($object)?($object->total_localtax1?$object->total_localtax1:$object->total_localtax1):'';
5550  if ($onlykey != 2 || $mysoc->useLocalTax(2)) $substitutionarray['__AMOUNT_TAX3__'] = is_object($object)?($object->total_localtax2?$object->total_localtax2:$object->total_localtax2):'';
5551 
5552  /* TODO Add key for multicurrency
5553  $substitutionarray['__AMOUNT_FORMATED__'] = is_object($object)?price($object->total_ttc, 0, $outputlangs, 0, 0, -1, $conf->currency_code):'';
5554  $substitutionarray['__AMOUNT_EXCL_TAX_FORMATED__'] = is_object($object)?price($object->total_ht, 0, $outputlangs, 0, 0, -1, $conf->currency_code):'';
5555  $substitutionarray['__AMOUNT_VAT_FORMATED__'] = is_object($object)?($object->total_vat?price($object->total_vat, 0, $outputlangs, 0, 0, -1, $conf->currency_code):price($object->total_tva, 0, $outputlangs, 0, 0, -1, $conf->currency_code)):'';
5556  */
5557  // For backward compatibility
5558  if ($onlykey != 2)
5559  {
5560  $substitutionarray['__TOTAL_TTC__'] = is_object($object)?$object->total_ttc:'';
5561  $substitutionarray['__TOTAL_HT__'] = is_object($object)?$object->total_ht:'';
5562  $substitutionarray['__TOTAL_VAT__'] = is_object($object)?($object->total_vat?$object->total_vat:$object->total_tva):'';
5563  }
5564  }
5565 
5566  if (empty($exclude) || ! in_array('date', $exclude))
5567  {
5568  include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
5569 
5570  $tmp=dol_getdate(dol_now(), true);
5571  $tmp2=dol_get_prev_day($tmp['mday'], $tmp['mon'], $tmp['year']);
5572  $tmp3=dol_get_prev_month($tmp['mday'], $tmp['mon'], $tmp['year']);
5573  $tmp4=dol_get_next_day($tmp['mday'], $tmp['mon'], $tmp['year']);
5574  $tmp5=dol_get_next_month($tmp['mday'], $tmp['mon'], $tmp['year']);
5575 
5576  $substitutionarray=array_merge($substitutionarray, array(
5577  '__DAY__' => (string) $tmp['mday'],
5578  '__MONTH__' => (string) $tmp['mon'],
5579  '__YEAR__' => (string) $tmp['year'],
5580  '__PREVIOUS_DAY__' => (string) $tmp2['day'],
5581  '__PREVIOUS_MONTH__' => (string) $tmp3['month'],
5582  '__PREVIOUS_YEAR__' => (string) ($tmp['year'] - 1),
5583  '__NEXT_DAY__' => (string) $tmp4['day'],
5584  '__NEXT_MONTH__' => (string) $tmp5['month'],
5585  '__NEXT_YEAR__' => (string) ($tmp['year'] + 1),
5586  ));
5587  }
5588 
5589  if (empty($exclude) || ! in_array('user', $exclude))
5590  {
5591  // Add SIGNATURE into substitutionarray first, so, when we will make the substitution,
5592  // this will also replace var found into content of signature
5593  $signature = $user->signature;
5594  $substitutionarray=array_merge($substitutionarray, array(
5595  '__USER_SIGNATURE__' => (string) (($signature && empty($conf->global->MAIN_MAIL_DO_NOT_USE_SIGN)) ? ($onlykey == 2 ? dol_trunc(dol_string_nohtmltag($signature), 30) : $signature) : '')
5596  )
5597  );
5598  // For backward compatibility
5599  if ($onlykey != 2)
5600  {
5601  $substitutionarray['__SIGNATURE__'] = (string) (($signature && empty($conf->global->MAIN_MAIL_DO_NOT_USE_SIGN)) ? ($onlykey == 2 ? dol_trunc(dol_string_nohtmltag($signature), 30) : $signature) : '');
5602  }
5603 
5604  $substitutionarray=array_merge($substitutionarray, array(
5605  '__USER_ID__' => (string) $user->id,
5606  '__USER_LOGIN__' => (string) $user->login,
5607  '__USER_LASTNAME__' => (string) $user->lastname,
5608  '__USER_FIRSTNAME__' => (string) $user->firstname,
5609  '__USER_FULLNAME__' => (string) $user->getFullName($outputlangs),
5610  '__USER_SUPERVISOR_ID__' => (string) $user->fk_user
5611  )
5612  );
5613  }
5614  if (! empty($conf->multicompany->enabled))
5615  {
5616  $substitutionarray=array_merge($substitutionarray, array('__ENTITY_ID__' => $conf->entity));
5617  }
5618 
5619  return $substitutionarray;
5620 }
5621 
5631 function make_substitutions($text, $substitutionarray, $outputlangs=null)
5632 {
5633  global $conf, $langs;
5634 
5635  if (! is_array($substitutionarray)) return 'ErrorBadParameterSubstitutionArrayWhenCalling_make_substitutions';
5636 
5637  if (empty($outputlangs)) $outputlangs=$langs;
5638 
5639  // Make substitution for language keys
5640  if (is_object($outputlangs))
5641  {
5642  while (preg_match('/__\(([^\)]+)\)__/', $text, $reg))
5643  {
5644  $msgishtml = 0;
5645  if (dol_textishtml($text,1)) $msgishtml = 1;
5646 
5647  // If key is __(TranslationKey|langfile)__, then force load of langfile.lang
5648  $tmp=explode('|',$reg[1]);
5649  if (! empty($tmp[1])) $outputlangs->load($tmp[1]);
5650 
5651  $text = preg_replace('/__\('.preg_quote($reg[1], '/').'\)__/', $msgishtml?dol_htmlentitiesbr($outputlangs->transnoentitiesnoconv($reg[1])):$outputlangs->transnoentitiesnoconv($reg[1]), $text);
5652  }
5653  }
5654 
5655  // Make substitution for constant keys. Must be after the substitution of translation, so if text of translation contains a constant,
5656  // it is also converted.
5657  while (preg_match('/__\[([^\]]+)\]__/', $text, $reg))
5658  {
5659  $msgishtml = 0;
5660  if (dol_textishtml($text,1)) $msgishtml = 1;
5661 
5662  $keyfound = $reg[1];
5663  $newval=empty($conf->global->$keyfound)?'':$conf->global->$keyfound;
5664  $text = preg_replace('/__\['.preg_quote($keyfound, '/').'\]__/', $msgishtml?dol_htmlentitiesbr($newval):$newval, $text);
5665  }
5666 
5667  // Make substitition for array $substitutionarray
5668  foreach ($substitutionarray as $key => $value)
5669  {
5670  if ($key == '__SIGNATURE__' && (! empty($conf->global->MAIN_MAIL_DO_NOT_USE_SIGN))) $value=''; // Protection
5671  if ($key == '__USER_SIGNATURE__' && (! empty($conf->global->MAIN_MAIL_DO_NOT_USE_SIGN))) $value=''; // Protection
5672 
5673  $text=str_replace("$key","$value",$text); // We must keep the " to work when value is 123.5 for example
5674  }
5675 
5676  return $text;
5677 }
5678 
5691 function complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
5692 {
5693  global $conf,$user;
5694 
5695  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
5696 
5697  // Add a substitution key for each extrafields, using key __EXTRA_XXX__
5698  // TODO Remove this. Already available into the getCommonSubstitutionArray used to build the substitution array.
5699  /*if (is_object($object) && is_array($object->array_options))
5700  {
5701  foreach($object->array_options as $key => $val)
5702  {
5703  $keyshort=preg_replace('/^(options|extra)_/','',$key);
5704  $substitutionarray['__EXTRAFIELD_'.$keyshort.'__']=$val;
5705  // For backward compatibiliy
5706  $substitutionarray['%EXTRA_'.$keyshort.'%']=$val;
5707  }
5708  }*/
5709 
5710  // Check if there is external substitution to do, requested by plugins
5711  $dirsubstitutions=array_merge(array(),(array) $conf->modules_parts['substitutions']);
5712 
5713  foreach($dirsubstitutions as $reldir)
5714  {
5715  $dir=dol_buildpath($reldir,0);
5716 
5717  // Check if directory exists
5718  if (! dol_is_dir($dir)) continue;
5719 
5720  $substitfiles=dol_dir_list($dir,'files',0,'functions_');
5721  foreach($substitfiles as $substitfile)
5722  {
5723  if (preg_match('/functions_(.*)\.lib\.php/i',$substitfile['name'],$reg))
5724  {
5725  $module=$reg[1];
5726 
5727  dol_syslog("Library functions_".$substitfile['name']." found into ".$dir);
5728  // Include the user's functions file
5729  require_once $dir.$substitfile['name'];
5730  // Call the user's function, and only if it is defined
5731  $function_name=$module."_".$callfunc;
5732  if (function_exists($function_name)) $function_name($substitutionarray,$outputlangs,$object,$parameters);
5733  }
5734  }
5735  }
5736 }
5737 
5747 function print_date_range($date_start,$date_end,$format = '',$outputlangs='')
5748 {
5749  print get_date_range($date_start,$date_end,$format,$outputlangs);
5750 }
5751 
5762 function get_date_range($date_start,$date_end,$format = '',$outputlangs='', $withparenthesis=1)
5763 {
5764  global $langs;
5765 
5766  $out='';
5767 
5768  if (! is_object($outputlangs)) $outputlangs=$langs;
5769 
5770  if ($date_start && $date_end)
5771  {
5772  $out.= ($withparenthesis?' (':'').$outputlangs->transnoentitiesnoconv('DateFromTo',dol_print_date($date_start, $format, false, $outputlangs),dol_print_date($date_end, $format, false, $outputlangs)).($withparenthesis?')':'');
5773  }
5774  if ($date_start && ! $date_end)
5775  {
5776  $out.= ($withparenthesis?' (':'').$outputlangs->transnoentitiesnoconv('DateFrom',dol_print_date($date_start, $format, false, $outputlangs)).($withparenthesis?')':'');
5777  }
5778  if (! $date_start && $date_end)
5779  {
5780  $out.= ($withparenthesis?' (':'').$outputlangs->transnoentitiesnoconv('DateUntil',dol_print_date($date_end, $format, false, $outputlangs)).($withparenthesis?')':'');
5781  }
5782 
5783  return $out;
5784 }
5785 
5794 function dolGetFirstLastname($firstname,$lastname,$nameorder=-1)
5795 {
5796  global $conf;
5797 
5798  $ret='';
5799  // If order not defined, we use the setup
5800  if ($nameorder < 0) $nameorder=(empty($conf->global->MAIN_FIRSTNAME_NAME_POSITION));
5801  if ($nameorder && ((string) $nameorder != '2'))
5802  {
5803  $ret.=$firstname;
5804  if ($firstname && $lastname) $ret.=' ';
5805  $ret.=$lastname;
5806  }
5807  else if ($nameorder == 2)
5808  {
5809  $ret.=$firstname;
5810  }
5811  else
5812  {
5813  $ret.=$lastname;
5814  if ($firstname && $lastname) $ret.=' ';
5815  $ret.=$firstname;
5816  }
5817  return $ret;
5818 }
5819 
5820 
5831 function setEventMessage($mesgs, $style='mesgs')
5832 {
5833  //dol_syslog(__FUNCTION__ . " is deprecated", LOG_WARNING); This is not deprecated, it is used by setEventMessages function
5834  if (! is_array($mesgs)) // If mesgs is a string
5835  {
5836  if ($mesgs) $_SESSION['dol_events'][$style][] = $mesgs;
5837  }
5838  else // If mesgs is an array
5839  {
5840  foreach($mesgs as $mesg)
5841  {
5842  if ($mesg) $_SESSION['dol_events'][$style][] = $mesg;
5843  }
5844  }
5845 }
5846 
5857 function setEventMessages($mesg, $mesgs, $style='mesgs')
5858 {
5859  if (! in_array((string) $style, array('mesgs','warnings','errors'))) dol_print_error('','Bad parameter style='.$style.' for setEventMessages');
5860  if (empty($mesgs)) setEventMessage($mesg, $style);
5861  else
5862  {
5863  if (! empty($mesg) && ! in_array($mesg, $mesgs)) setEventMessage($mesg, $style); // Add message string if not already into array
5864  setEventMessage($mesgs, $style);
5865  }
5866 }
5867 
5877 function dol_htmloutput_events($disabledoutputofmessages=0)
5878 {
5879  // Show mesgs
5880  if (isset($_SESSION['dol_events']['mesgs'])) {
5881  if (empty($disabledoutputofmessages)) dol_htmloutput_mesg('', $_SESSION['dol_events']['mesgs']);
5882  unset($_SESSION['dol_events']['mesgs']);
5883  }
5884 
5885  // Show errors
5886  if (isset($_SESSION['dol_events']['errors'])) {
5887  if (empty($disabledoutputofmessages)) dol_htmloutput_mesg('', $_SESSION['dol_events']['errors'], 'error');
5888  unset($_SESSION['dol_events']['errors']);
5889  }
5890 
5891  // Show warnings
5892  if (isset($_SESSION['dol_events']['warnings'])) {
5893  if (empty($disabledoutputofmessages)) dol_htmloutput_mesg('', $_SESSION['dol_events']['warnings'], 'warning');
5894  unset($_SESSION['dol_events']['warnings']);
5895  }
5896 }
5897 
5912 function get_htmloutput_mesg($mesgstring='',$mesgarray='', $style='ok', $keepembedded=0)
5913 {
5914  global $conf, $langs;
5915 
5916  $ret=0; $return='';
5917  $out='';
5918  $divstart=$divend='';
5919 
5920  // If inline message with no format, we add it.
5921  if ((empty($conf->use_javascript_ajax) || ! empty($conf->global->MAIN_DISABLE_JQUERY_JNOTIFY) || $keepembedded) && ! preg_match('/<div class=".*">/i',$out))
5922  {
5923  $divstart='<div class="'.$style.' clearboth">';
5924  $divend='</div>';
5925  }
5926 
5927  if ((is_array($mesgarray) && count($mesgarray)) || $mesgstring)
5928  {
5929  $langs->load("errors");
5930  $out.=$divstart;
5931  if (is_array($mesgarray) && count($mesgarray))
5932  {
5933  foreach($mesgarray as $message)
5934  {
5935  $ret++;
5936  $out.= $langs->trans($message);
5937  if ($ret < count($mesgarray)) $out.= "<br>\n";
5938  }
5939  }
5940  if ($mesgstring)
5941  {
5942  $langs->load("errors");
5943  $ret++;
5944  $out.= $langs->trans($mesgstring);
5945  }
5946  $out.=$divend;
5947  }
5948 
5949  if ($out)
5950  {
5951  if (! empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_DISABLE_JQUERY_JNOTIFY) && empty($keepembedded))
5952  {
5953  $return = '<script type="text/javascript">
5954  $(document).ready(function() {
5955  var block = '.(! empty($conf->global->MAIN_USE_JQUERY_BLOCKUI)?"true":"false").'
5956  if (block) {
5957  $.dolEventValid("","'.dol_escape_js($out).'");
5958  } else {
5959  /* jnotify(message, preset of message type, keepmessage) */
5960  $.jnotify("'.dol_escape_js($out).'",
5961  "'.($style=="ok" ? 3000 : $style).'",
5962  '.($style=="ok" ? "false" : "true").',
5963  { remove: function (){} } );
5964  }
5965  });
5966  </script>';
5967  }
5968  else
5969  {
5970  $return = $out;
5971  }
5972  }
5973 
5974  return $return;
5975 }
5976 
5988 function get_htmloutput_errors($mesgstring='', $mesgarray='', $keepembedded=0)
5989 {
5990  return get_htmloutput_mesg($mesgstring, $mesgarray,'error',$keepembedded);
5991 }
5992 
6006 function dol_htmloutput_mesg($mesgstring='',$mesgarray='', $style='ok', $keepembedded=0)
6007 {
6008  if (empty($mesgstring) && (! is_array($mesgarray) || count($mesgarray) == 0)) return;
6009 
6010  $iserror=0;
6011  $iswarning=0;
6012  if (is_array($mesgarray))
6013  {
6014  foreach($mesgarray as $val)
6015  {
6016  if ($val && preg_match('/class="error"/i',$val)) { $iserror++; break; }
6017  if ($val && preg_match('/class="warning"/i',$val)) { $iswarning++; break; }
6018  }
6019  }
6020  else if ($mesgstring && preg_match('/class="error"/i',$mesgstring)) $iserror++;
6021  else if ($mesgstring && preg_match('/class="warning"/i',$mesgstring)) $iswarning++;
6022  if ($style=='error') $iserror++;
6023  if ($style=='warning') $iswarning++;
6024 
6025  if ($iserror || $iswarning)
6026  {
6027  // Remove div from texts
6028  $mesgstring=preg_replace('/<\/div><div class="(error|warning)">/','<br>',$mesgstring);
6029  $mesgstring=preg_replace('/<div class="(error|warning)">/','',$mesgstring);
6030  $mesgstring=preg_replace('/<\/div>/','',$mesgstring);
6031  // Remove div from texts array
6032  if (is_array($mesgarray))
6033  {
6034  $newmesgarray=array();
6035  foreach($mesgarray as $val)
6036  {
6037  $tmpmesgstring=preg_replace('/<\/div><div class="(error|warning)">/','<br>',$val);
6038  $tmpmesgstring=preg_replace('/<div class="(error|warning)">/','',$tmpmesgstring);
6039  $tmpmesgstring=preg_replace('/<\/div>/','',$tmpmesgstring);
6040  $newmesgarray[]=$tmpmesgstring;
6041  }
6042  $mesgarray=$newmesgarray;
6043  }
6044  print get_htmloutput_mesg($mesgstring,$mesgarray,($iserror?'error':'warning'),$keepembedded);
6045  }
6046  else print get_htmloutput_mesg($mesgstring,$mesgarray,'ok',$keepembedded);
6047 }
6048 
6060 function dol_htmloutput_errors($mesgstring='', $mesgarray='', $keepembedded=0)
6061 {
6062  dol_htmloutput_mesg($mesgstring, $mesgarray, 'error', $keepembedded);
6063 }
6064 
6078 function dol_sort_array(&$array, $index, $order='asc', $natsort=0, $case_sensitive=0, $keepindex=0)
6079 {
6080  // Clean parameters
6081  $order=strtolower($order);
6082 
6083  $sizearray=count($array);
6084  if (is_array($array) && $sizearray>0)
6085  {
6086  $temp = array();
6087  foreach(array_keys($array) as $key) $temp[$key]=$array[$key][$index];
6088 
6089  if (!$natsort) ($order=='asc') ? asort($temp) : arsort($temp);
6090  else
6091  {
6092  ($case_sensitive) ? natsort($temp) : natcasesort($temp);
6093  if($order!='asc') $temp=array_reverse($temp,TRUE);
6094  }
6095 
6096  $sorted = array();
6097 
6098  foreach(array_keys($temp) as $key)
6099  {
6100  (is_numeric($key) && empty($keepindex)) ? $sorted[]=$array[$key] : $sorted[$key]=$array[$key];
6101  }
6102 
6103  return $sorted;
6104  }
6105  return $array;
6106 }
6107 
6108 
6115 function utf8_check($str)
6116 {
6117  // We must use here a binary strlen function (so not dol_strlen)
6118  $strLength = dol_strlen($str);
6119  for ($i=0; $i<$strLength; $i++)
6120  {
6121  if (ord($str[$i]) < 0x80) continue; // 0bbbbbbb
6122  elseif ((ord($str[$i]) & 0xE0) == 0xC0) $n=1; // 110bbbbb
6123  elseif ((ord($str[$i]) & 0xF0) == 0xE0) $n=2; // 1110bbbb
6124  elseif ((ord($str[$i]) & 0xF8) == 0xF0) $n=3; // 11110bbb
6125  elseif ((ord($str[$i]) & 0xFC) == 0xF8) $n=4; // 111110bb
6126  elseif ((ord($str[$i]) & 0xFE) == 0xFC) $n=5; // 1111110b
6127  else return false; // Does not match any model
6128  for ($j=0; $j<$n; $j++) { // n bytes matching 10bbbbbb follow ?
6129  if ((++$i == strlen($str)) || ((ord($str[$i]) & 0xC0) != 0x80))
6130  return false;
6131  }
6132  }
6133  return true;
6134 }
6135 
6136 
6144 function dol_osencode($str)
6145 {
6146  global $conf;
6147 
6148  $tmp=ini_get("unicode.filesystem_encoding"); // Disponible avec PHP 6.0
6149  if (empty($tmp) && ! empty($_SERVER["WINDIR"])) $tmp='iso-8859-1'; // By default for windows
6150  if (empty($tmp)) $tmp='utf-8'; // By default for other
6151  if (! empty($conf->global->MAIN_FILESYSTEM_ENCODING)) $tmp=$conf->global->MAIN_FILESYSTEM_ENCODING;
6152 
6153  if ($tmp == 'iso-8859-1') return utf8_decode($str);
6154  return $str;
6155 }
6156 
6157 
6171 function dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0)
6172 {
6173  global $cache_codes;
6174 
6175  // If key empty
6176  if ($key == '') return '';
6177 
6178  // Check in cache
6179  if (isset($cache_codes[$tablename][$key])) // Can be defined to 0 or ''
6180  {
6181  return $cache_codes[$tablename][$key]; // Found in cache
6182  }
6183 
6184  $sql = "SELECT ".$fieldid." as valuetoget";
6185  $sql.= " FROM ".MAIN_DB_PREFIX.$tablename;
6186  $sql.= " WHERE ".$fieldkey." = '".$db->escape($key)."'";
6187  if (! empty($entityfilter))
6188  $sql.= " AND entity IN (" . getEntity($tablename) . ")";
6189 
6190  dol_syslog('dol_getIdFromCode', LOG_DEBUG);
6191  $resql = $db->query($sql);
6192  if ($resql)
6193  {
6194  $obj = $db->fetch_object($resql);
6195  if ($obj) $cache_codes[$tablename][$key]=$obj->valuetoget;
6196  else $cache_codes[$tablename][$key]='';
6197  $db->free($resql);
6198  return $cache_codes[$tablename][$key];
6199  }
6200  else
6201  {
6202  return -1;
6203  }
6204 }
6205 
6212 function verifCond($strRights)
6213 {
6214  global $user,$conf,$langs;
6215  global $leftmenu;
6216  global $rights; // To export to dol_eval function
6217 
6218  //print $strRights."<br>\n";
6219  $rights = true;
6220  if ($strRights != '')
6221  {
6222  //$tab_rights = explode('&&', $strRights);
6223  //$i = 0;
6224  //while (($i < count($tab_rights)) && ($rights == true)) {
6225  $str = 'if(!(' . $strRights . ')) { $rights = false; }';
6226  dol_eval($str);
6227  // $i++;
6228  //}
6229  }
6230  return $rights;
6231 }
6232 
6242 function dol_eval($s, $returnvalue=0, $hideerrors=1)
6243 {
6244  // Only global variables can be changed by eval function and returned to caller
6245  global $db, $langs, $user, $conf;
6246  global $mainmenu, $leftmenu;
6247  global $rights;
6248  global $object;
6249  global $mysoc;
6250 
6251  global $obj; // To get $obj used into list when dol_eval is used for computed fields and $obj is not yet $object
6252  global $soc; // For backward compatibility
6253 
6254  //print $s."<br>\n";
6255  if ($returnvalue)
6256  {
6257  if ($hideerrors) return @eval('return '.$s.';');
6258  else return eval('return '.$s.';');
6259  }
6260  else
6261  {
6262  if ($hideerrors) @eval($s);
6263  else eval($s);
6264  }
6265 }
6266 
6273 function dol_validElement($element)
6274 {
6275  return (trim($element) != '');
6276 }
6277 
6285 function picto_from_langcode($codelang, $moreatt = '')
6286 {
6287  global $langs;
6288 
6289  if (empty($codelang)) return '';
6290 
6291  if (empty($codelang)) return '';
6292 
6293  if ($codelang == 'auto')
6294  {
6295  return img_picto_common($langs->trans('AutoDetectLang'), 'flags/int.png', $moreatt);
6296  }
6297 
6298  $langtocountryflag = array(
6299  'ar_AR' => '',
6300  'ca_ES' => 'catalonia',
6301  'da_DA' => 'dk',
6302  'fr_CA' => 'mq',
6303  'sv_SV' => 'se'
6304  );
6305 
6306  if (isset($langtocountryflag[$codelang])) $flagImage = $langtocountryflag[$codelang];
6307  else
6308  {
6309  $tmparray = explode('_', $codelang);
6310  $flagImage = empty($tmparray[1]) ? $tmparray[0] : $tmparray[1];
6311  }
6312 
6313  return img_picto_common($codelang, 'flags/'.strtolower($flagImage).'.png', $moreatt);
6314 }
6315 
6345 function complete_head_from_modules($conf,$langs,$object,&$head,&$h,$type,$mode='add')
6346 {
6347  global $hookmanager;
6348 
6349  if (isset($conf->modules_parts['tabs'][$type]) && is_array($conf->modules_parts['tabs'][$type]))
6350  {
6351  foreach ($conf->modules_parts['tabs'][$type] as $value)
6352  {
6353  $values=explode(':',$value);
6354 
6355  if ($mode == 'add' && ! preg_match('/^\-/',$values[1]))
6356  {
6357  if (count($values) == 6) // new declaration with permissions: $value='objecttype:+tabname1:Title1:langfile@mymodule:$user->rights->mymodule->read:/mymodule/mynewtab1.php?id=__ID__'
6358  {
6359  if ($values[0] != $type) continue;
6360 
6361  if (verifCond($values[4]))
6362  {
6363  if ($values[3]) $langs->load($values[3]);
6364  if (preg_match('/SUBSTITUTION_([^_]+)/i',$values[2],$reg))
6365  {
6366  $substitutionarray=array();
6367  complete_substitutions_array($substitutionarray,$langs,$object,array('needforkey'=>$values[2]));
6368  $label=make_substitutions($reg[1], $substitutionarray);
6369  }
6370  else $label=$langs->trans($values[2]);
6371 
6372  $head[$h][0] = dol_buildpath(preg_replace('/__ID__/i', ((is_object($object) && ! empty($object->id))?$object->id:''), $values[5]), 1);
6373  $head[$h][1] = $label;
6374  $head[$h][2] = str_replace('+','',$values[1]);
6375  $h++;
6376  }
6377  }
6378  else if (count($values) == 5) // deprecated
6379  {
6380  dol_syslog('Passing 5 values in tabs module_parts is deprecated. Please update to 6 with permissions.', LOG_WARNING);
6381 
6382  if ($values[0] != $type) continue;
6383  if ($values[3]) $langs->load($values[3]);
6384  if (preg_match('/SUBSTITUTION_([^_]+)/i',$values[2],$reg))
6385  {
6386  $substitutionarray=array();
6387  complete_substitutions_array($substitutionarray,$langs,$object,array('needforkey'=>$values[2]));
6388  $label=make_substitutions($reg[1], $substitutionarray);
6389  }
6390  else $label=$langs->trans($values[2]);
6391 
6392  $head[$h][0] = dol_buildpath(preg_replace('/__ID__/i', ((is_object($object) && ! empty($object->id))?$object->id:''), $values[4]), 1);
6393  $head[$h][1] = $label;
6394  $head[$h][2] = str_replace('+','',$values[1]);
6395  $h++;
6396  }
6397  }
6398  else if ($mode == 'remove' && preg_match('/^\-/',$values[1]))
6399  {
6400  if ($values[0] != $type) continue;
6401  $tabname=str_replace('-','',$values[1]);
6402  foreach($head as $key => $val)
6403  {
6404  $condition = (! empty($values[3]) ? verifCond($values[3]) : 1);
6405  if ($head[$key][2]==$tabname && $condition)
6406  {
6407  unset($head[$key]);
6408  break;
6409  }
6410  }
6411  }
6412  }
6413  }
6414 
6415  // No need to make a return $head. Var is modified as a reference
6416  if (! empty($hookmanager))
6417  {
6418  $parameters=array('object' => $object, 'mode' => $mode, 'head'=>$head);
6419  $reshook=$hookmanager->executeHooks('completeTabsHead',$parameters);
6420  if ($reshook > 0)
6421  {
6422  $head = $hookmanager->resArray;
6423  }
6424  }
6425 }
6426 
6438 function printCommonFooter($zone='private')
6439 {
6440  global $conf, $hookmanager;
6441  global $micro_start_time;
6442 
6443  if ($zone == 'private') print "\n".'<!-- Common footer for private page -->'."\n";
6444  else print "\n".'<!-- Common footer for public page -->'."\n";
6445 
6446  $parameters=array();
6447  $reshook=$hookmanager->executeHooks('printCommonFooter',$parameters); // Note that $action and $object may have been modified by some hooks
6448  if (empty($reshook))
6449  {
6450  if (! empty($conf->global->MAIN_HTML_FOOTER)) print $conf->global->MAIN_HTML_FOOTER."\n";
6451 
6452  print "\n";
6453  if (! empty($conf->use_javascript_ajax))
6454  {
6455  print '<script type="text/javascript" language="javascript">'."\n";
6456 
6457  if ($zone == 'private' && empty($conf->dol_use_jmobile))
6458  {
6459  print "\n";
6460  print '/* JS CODE TO ENABLE to enable handler to switch left menu page (menuhider) */'."\n";
6461  print 'jQuery(".menuhider").click(function() {';
6462  print ' console.log("We click on .menuhider");'."\n";
6463  //print " $('.side-nav').animate({width:'toggle'},200);\n"; // OK with eldy theme but not with md
6464  print " $('.side-nav').toggle()\n";
6465  print " $('.login_block').toggle()\n";
6466  print '});'."\n";
6467  }
6468 
6469  // Google Analytics (need Google module)
6470  if (! empty($conf->google->enabled) && ! empty($conf->global->MAIN_GOOGLE_AN_ID))
6471  {
6472  if (($conf->dol_use_jmobile != 4))
6473  {
6474  print "\n";
6475  print "/* JS CODE TO ENABLE for google analtics tag */\n";
6476  print ' var _gaq = _gaq || [];'."\n";
6477  print ' _gaq.push([\'_setAccount\', \''.$conf->global->MAIN_GOOGLE_AN_ID.'\']);'."\n";
6478  print ' _gaq.push([\'_trackPageview\']);'."\n";
6479  print ''."\n";
6480  print ' (function() {'."\n";
6481  print ' var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;'."\n";
6482  print ' ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';'."\n";
6483  print ' var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);'."\n";
6484  print ' })();'."\n";
6485  }
6486  }
6487 
6488  // End of tuning
6489  if (! empty($_SERVER['MAIN_SHOW_TUNING_INFO']) || ! empty($conf->global->MAIN_SHOW_TUNING_INFO))
6490  {
6491  print "\n";
6492  print "/* JS CODE TO ENABLE to add memory info */\n";
6493  print 'window.console && console.log("';
6494  if (! empty($conf->global->MEMCACHED_SERVER)) print 'MEMCACHED_SERVER='.$conf->global->MEMCACHED_SERVER.' - ';
6495  print 'MAIN_OPTIMIZE_SPEED='.(isset($conf->global->MAIN_OPTIMIZE_SPEED)?$conf->global->MAIN_OPTIMIZE_SPEED:'off');
6496  if (! empty($micro_start_time)) // Works only if MAIN_SHOW_TUNING_INFO is defined at $_SERVER level. Not in global variable.
6497  {
6498  $micro_end_time = microtime(true);
6499  print ' - Build time: '.ceil(1000*($micro_end_time-$micro_start_time)).' ms';
6500  }
6501  if (function_exists("memory_get_usage"))
6502  {
6503  print ' - Mem: '.memory_get_usage();
6504  }
6505  if (function_exists("xdebug_memory_usage"))
6506  {
6507  print ' - XDebug time: '.ceil(1000*xdebug_time_index()).' ms';
6508  print ' - XDebug mem: '.xdebug_memory_usage();
6509  print ' - XDebug mem peak: '.xdebug_peak_memory_usage();
6510  }
6511  if (function_exists("zend_loader_file_encoded"))
6512  {
6513  print ' - Zend encoded file: '.(zend_loader_file_encoded()?'yes':'no');
6514  }
6515  print '");'."\n";
6516  }
6517 
6518  print "\n".'</script>'."\n";
6519  }
6520 
6521  // Add Xdebug coverage of code
6522  if (defined('XDEBUGCOVERAGE'))
6523  {
6524  print_r(xdebug_get_code_coverage());
6525  }
6526 
6527  // If there is some logs in buffer to show
6528  if (count($conf->logbuffer))
6529  {
6530  print "\n";
6531  print "<!-- Start of log output\n";
6532  //print '<div class="hidden">'."\n";
6533  foreach($conf->logbuffer as $logline)
6534  {
6535  print $logline."<br>\n";
6536  }
6537  //print '</div>'."\n";
6538  print "End of log output -->\n";
6539  }
6540  }
6541 }
6542 
6552 function dolExplodeIntoArray($string, $delimiter = ';', $kv = '=')
6553 {
6554  if ($a = explode($delimiter, $string))
6555  {
6556  $ka = array();
6557  foreach ($a as $s) { // each part
6558  if ($s) {
6559  if ($pos = strpos($s, $kv)) { // key/value delimiter
6560  $ka[trim(substr($s, 0, $pos))] = trim(substr($s, $pos + strlen($kv)));
6561  } else { // key delimiter not found
6562  $ka[] = trim($s);
6563  }
6564  }
6565  }
6566  return $ka;
6567  }
6568  return array();
6569 }
6570 
6571 
6578 function dol_set_focus($selector)
6579 {
6580  print "\n".'<!-- Set focus onto a specific field -->'."\n";
6581  print '<script type="text/javascript" language="javascript">jQuery(document).ready(function() { jQuery("'.dol_escape_js($selector).'").focus(); });</script>'."\n";
6582 }
6583 
6584 
6592 function dol_getmypid()
6593 {
6594  if (! function_exists('getmypid')) {
6595  return mt_rand(1,32768);
6596  } else {
6597  return getmypid();
6598  }
6599 }
6600 
6601 
6617 function natural_search($fields, $value, $mode=0, $nofirstand=0)
6618 {
6619  global $db,$langs;
6620 
6621  $value=trim($value);
6622 
6623  if ($mode == 0)
6624  {
6625  $value=preg_replace('/\*/','%',$value); // Replace * with %
6626  }
6627  if ($mode == 1)
6628  {
6629  $value=preg_replace('/([<>=]+)\s+([0-9'.preg_quote($langs->trans("DecimalSeparator"),'/').'\-])/','\1\2',$value); // Clean string '< 10' into '<10' so we can the explode on space to get all tests to do
6630  }
6631 
6632  $value = preg_replace('/\s*\|\s*/','|', $value);
6633 
6634  $crits = explode(' ', $value);
6635  $res = '';
6636  if (! is_array($fields)) $fields = array($fields);
6637 
6638  $nboffields = count($fields);
6639  $end2 = count($crits);
6640  $j = 0;
6641  foreach ($crits as $crit)
6642  {
6643  $i = 0; $i2 = 0;
6644  $newres = '';
6645  foreach ($fields as $field)
6646  {
6647  if ($mode == 1)
6648  {
6649  $operator='=';
6650  $newcrit = preg_replace('/([<>=]+)/','',trim($crit));
6651 
6652  preg_match('/([<>=]+)/',trim($crit), $reg);
6653  if ($reg[1])
6654  {
6655  $operator = $reg[1];
6656  }
6657  if ($newcrit != '')
6658  {
6659  $numnewcrit = price2num($newcrit);
6660  if (is_numeric($numnewcrit))
6661  {
6662  $newres .= ($i2 > 0 ? ' OR ' : '') . $field . ' '.$operator.' '.$numnewcrit;
6663  }
6664  else
6665  {
6666  $newres .= ($i2 > 0 ? ' OR ' : '') . '1 = 2'; // force false
6667  }
6668  $i2++; // a criteria was added to string
6669  }
6670  }
6671  else if ($mode == 2)
6672  {
6673  $newres .= ($i2 > 0 ? ' OR ' : '') . $field . " IN (" . $db->escape(trim($crit)) . ")";
6674  $i2++; // a criteria was added to string
6675  }
6676  else if ($mode == 3)
6677  {
6678  $tmparray=explode(',',trim($crit));
6679  if (count($tmparray))
6680  {
6681  $listofcodes='';
6682  foreach($tmparray as $val)
6683  {
6684  if ($val)
6685  {
6686  $listofcodes.=($listofcodes?',':'');
6687  $listofcodes.="'".$db->escape(trim($val))."'";
6688  }
6689  }
6690  $newres .= ($i2 > 0 ? ' OR ' : '') . $field . " IN (" . $listofcodes . ")";
6691  $i2++; // a criteria was added to string
6692  }
6693  }
6694  else // $mode=0
6695  {
6696  $textcrit = '';
6697  $tmpcrits = explode('|',$crit);
6698  $i3 = 0;
6699  foreach($tmpcrits as $tmpcrit)
6700  {
6701  $newres .= (($i2 > 0 || $i3 > 0) ? ' OR ' : '');
6702 
6703  if (preg_match('/\.(id|rowid)$/', $field)) // Special case for rowid that is sometimes a ref so used as a search field
6704  {
6705  $newres .= $field . " = " . (is_numeric(trim($tmpcrit))?trim($tmpcrit):'0');
6706  }
6707  else
6708  {
6709  $newres .= $field . " LIKE '";
6710 
6711  $tmpcrit=trim($tmpcrit);
6712  $tmpcrit2=$tmpcrit;
6713  $tmpbefore='%'; $tmpafter='%';
6714  if (preg_match('/^[\^\$]/', $tmpcrit))
6715  {
6716  $tmpbefore='';
6717  $tmpcrit2 = preg_replace('/^[\^\$]/', '', $tmpcrit2);
6718  }
6719  if (preg_match('/[\^\$]$/', $tmpcrit))
6720  {
6721  $tmpafter='';
6722  $tmpcrit2 = preg_replace('/[\^\$]$/', '', $tmpcrit2);
6723  }
6724  $newres .= $tmpbefore;
6725  $newres .= $db->escape($tmpcrit2);
6726  $newres .= $tmpafter;
6727  $newres .= "'";
6728  if ($tmpcrit2 == '')
6729  {
6730  $newres .= ' OR ' . $field . " IS NULL";
6731  }
6732  }
6733 
6734  $i3++;
6735  }
6736  $i2++; // a criteria was added to string
6737  }
6738  $i++;
6739  }
6740  if ($newres) $res = $res . ($res ? ' AND ' : '') . ($i2 > 1 ? '(' : '') .$newres . ($i2 > 1 ? ')' : '');
6741  $j++;
6742  }
6743  $res = ($nofirstand?"":" AND ")."(" . $res . ")";
6744  //print 'xx'.$res.'yy';
6745  return $res;
6746 }
6747 
6754 function showDirectDownloadLink($object)
6755 {
6756  global $conf, $langs;
6757 
6758  $out='';
6759  $url = $object->getLastMainDocLink($object->element);
6760 
6761  if ($url)
6762  {
6763  $out.= img_picto('','object_globe.png').' '.$langs->trans("DirectDownloadLink").'<br>';
6764  $out.= '<input type="text" id="directdownloadlink" class="quatrevingtpercent" value="'.$url.'">';
6765  $out.= ajax_autoselect("directdownloadlink", 0);
6766  }
6767  return $out;
6768 }
6769 
6778 function getImageFileNameForSize($file, $extName, $extImgTarget='')
6779 {
6780  $dirName = dirname($file);
6781  if ($dirName == '.') $dirName='';
6782 
6783  $fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i','',$file); // We remove extension, whatever is its case
6784  $fileName = basename($fileName);
6785 
6786  if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.jpg$/i',$file)?'.jpg':'');
6787  if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.jpeg$/i',$file)?'.jpeg':'');
6788  if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.gif$/i',$file)?'.gif':'');
6789  if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.png$/i',$file)?'.png':'');
6790  if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.bmp$/i',$file)?'.bmp':'');
6791 
6792  if (! $extImgTarget) return $file;
6793 
6794  $subdir='';
6795  if ($extName) $subdir = 'thumbs/';
6796 
6797  return ($dirName?$dirName.'/':'').$subdir.$fileName.$extName.$extImgTarget; // New filename for thumb
6798 }
6799 
6800 
6810 function getAdvancedPreviewUrl($modulepart, $relativepath, $alldata=0, $param='')
6811 {
6812  global $conf, $langs;
6813 
6814  if (empty($conf->use_javascript_ajax)) return '';
6815 
6816  $mime_preview = array('bmp', 'jpeg', 'png', 'gif', 'tiff', 'pdf', 'plain', 'css');
6817  //$mime_preview[]='vnd.oasis.opendocument.presentation';
6818  //$mime_preview[]='archive';
6819  $num_mime = array_search(dol_mimetype($relativepath, '', 1), $mime_preview);
6820 
6821  if ($alldata == 1)
6822  {
6823  if ($num_mime !== false) return array('target'=>'_blank', 'css'=>'documentpreview', 'url'=>DOL_URL_ROOT.'/document.php?modulepart='.$modulepart.'&attachment=0&file='.urlencode($relativepath), 'mime'=>dol_mimetype($relativepath), );
6824  else return array();
6825  }
6826 
6827  // old behavior
6828  if ($num_mime !== false) return 'javascript:document_preview(\''.dol_escape_js(DOL_URL_ROOT.'/document.php?modulepart='.$modulepart.'&attachment=0&file='.urlencode($relativepath).($param?'&'.$param:'')).'\', \''.dol_mimetype($relativepath).'\', \''.dol_escape_js($langs->trans('Preview')).'\')';
6829  else return '';
6830 }
6831 
6832 
6842 function dol_mimetype($file, $default='application/octet-stream', $mode=0)
6843 {
6844  $mime=$default;
6845  $imgmime='other.png';
6846  $famime='file-o';
6847  $srclang='';
6848 
6849  $tmpfile=preg_replace('/\.noexe$/','',$file);
6850 
6851  // Text files
6852  if (preg_match('/\.txt$/i',$tmpfile)) { $mime='text/plain'; $imgmime='text.png'; $famime='file-text-o'; }
6853  if (preg_match('/\.rtx$/i',$tmpfile)) { $mime='text/richtext'; $imgmime='text.png'; $famime='file-text-o'; }
6854  if (preg_match('/\.csv$/i',$tmpfile)) { $mime='text/csv'; $imgmime='text.png'; $famime='file-text-o'; }
6855  if (preg_match('/\.tsv$/i',$tmpfile)) { $mime='text/tab-separated-values'; $imgmime='text.png'; $famime='file-text-o'; }
6856  if (preg_match('/\.(cf|conf|log)$/i',$tmpfile)) { $mime='text/plain'; $imgmime='text.png'; $famime='file-text-o'; }
6857  if (preg_match('/\.ini$/i',$tmpfile)) { $mime='text/plain'; $imgmime='text.png'; $srclang='ini'; $famime='file-text-o'; }
6858  if (preg_match('/\.css$/i',$tmpfile)) { $mime='text/css'; $imgmime='css.png'; $srclang='css'; $famime='file-text-o'; }
6859  // Certificate files
6860  if (preg_match('/\.(crt|cer|key|pub)$/i',$tmpfile)) { $mime='text/plain'; $imgmime='text.png'; $famime='file-text-o'; }
6861  // HTML/XML
6862  if (preg_match('/\.(html|htm|shtml)$/i',$tmpfile)) { $mime='text/html'; $imgmime='html.png'; $srclang='html'; $famime='file-text-o'; }
6863  if (preg_match('/\.(xml|xhtml)$/i',$tmpfile)) { $mime='text/xml'; $imgmime='other.png'; $srclang='xml'; $famime='file-text-o'; }
6864  // Languages
6865  if (preg_match('/\.bas$/i',$tmpfile)) { $mime='text/plain'; $imgmime='text.png'; $srclang='bas'; $famime='file-code-o'; }
6866  if (preg_match('/\.(c)$/i',$tmpfile)) { $mime='text/plain'; $imgmime='text.png'; $srclang='c'; $famime='file-code-o'; }
6867  if (preg_match('/\.(cpp)$/i',$tmpfile)) { $mime='text/plain'; $imgmime='text.png'; $srclang='cpp'; $famime='file-code-o'; }
6868  if (preg_match('/\.(h)$/i',$tmpfile)) { $mime='text/plain'; $imgmime='text.png'; $srclang='h'; $famime='file-code-o'; }
6869  if (preg_match('/\.(java|jsp)$/i',$tmpfile)) { $mime='text/plain'; $imgmime='text.png'; $srclang='java'; $famime='file-code-o'; }
6870  if (preg_match('/\.php([0-9]{1})?$/i',$tmpfile)) { $mime='text/plain'; $imgmime='php.png'; $srclang='php'; $famime='file-code-o'; }
6871  if (preg_match('/\.phtml$/i',$tmpfile)) { $mime='text/plain'; $imgmime='php.png'; $srclang='php'; $famime='file-code-o'; }
6872  if (preg_match('/\.(pl|pm)$/i',$tmpfile)) { $mime='text/plain'; $imgmime='pl.png'; $srclang='perl'; $famime='file-code-o'; }
6873  if (preg_match('/\.sql$/i',$tmpfile)) { $mime='text/plain'; $imgmime='text.png'; $srclang='sql'; $famime='file-code-o'; }
6874  if (preg_match('/\.js$/i',$tmpfile)) { $mime='text/x-javascript'; $imgmime='jscript.png'; $srclang='js'; $famime='file-code-o'; }
6875  // Open office
6876  if (preg_match('/\.odp$/i',$tmpfile)) { $mime='application/vnd.oasis.opendocument.presentation'; $imgmime='ooffice.png'; $famime='file-powerpoint-o'; }
6877  if (preg_match('/\.ods$/i',$tmpfile)) { $mime='application/vnd.oasis.opendocument.spreadsheet'; $imgmime='ooffice.png'; $famime='file-excel-o'; }
6878  if (preg_match('/\.odt$/i',$tmpfile)) { $mime='application/vnd.oasis.opendocument.text'; $imgmime='ooffice.png'; $famime='file-word-o'; }
6879  // MS Office
6880  if (preg_match('/\.mdb$/i',$tmpfile)) { $mime='application/msaccess'; $imgmime='mdb.png'; $famime='file-o'; }
6881  if (preg_match('/\.doc(x|m)?$/i',$tmpfile)) { $mime='application/msword'; $imgmime='doc.png'; $famime='file-word-o'; }
6882  if (preg_match('/\.dot(x|m)?$/i',$tmpfile)) { $mime='application/msword'; $imgmime='doc.png'; $famime='file-word-o'; }
6883  if (preg_match('/\.xlt(x)?$/i',$tmpfile)) { $mime='application/vnd.ms-excel'; $imgmime='xls.png'; $famime='file-excel-o'; }
6884  if (preg_match('/\.xla(m)?$/i',$tmpfile)) { $mime='application/vnd.ms-excel'; $imgmime='xls.png'; $famime='file-excel-o'; }
6885  if (preg_match('/\.xls$/i',$tmpfile)) { $mime='application/vnd.ms-excel'; $imgmime='xls.png'; $famime='file-excel-o'; }
6886  if (preg_match('/\.xls(b|m|x)$/i',$tmpfile)) { $mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; $imgmime='xls.png'; $famime='file-excel-o'; }
6887  if (preg_match('/\.pps(m|x)?$/i',$tmpfile)) { $mime='application/vnd.ms-powerpoint'; $imgmime='ppt.png'; $famime='file-powerpoint-o'; }
6888  if (preg_match('/\.ppt(m|x)?$/i',$tmpfile)) { $mime='application/x-mspowerpoint'; $imgmime='ppt.png'; $famime='file-powerpoint-o'; }
6889  // Other
6890  if (preg_match('/\.pdf$/i',$tmpfile)) { $mime='application/pdf'; $imgmime='pdf.png'; $famime='file-pdf-o'; }
6891  // Scripts
6892  if (preg_match('/\.bat$/i',$tmpfile)) { $mime='text/x-bat'; $imgmime='script.png'; $srclang='dos'; $famime='file-code-o'; }
6893  if (preg_match('/\.sh$/i',$tmpfile)) { $mime='text/x-sh'; $imgmime='script.png'; $srclang='bash'; $famime='file-code-o'; }
6894  if (preg_match('/\.ksh$/i',$tmpfile)) { $mime='text/x-ksh'; $imgmime='script.png'; $srclang='bash'; $famime='file-code-o'; }
6895  if (preg_match('/\.bash$/i',$tmpfile)) { $mime='text/x-bash'; $imgmime='script.png'; $srclang='bash'; $famime='file-code-o'; }
6896  // Images
6897  if (preg_match('/\.ico$/i',$tmpfile)) { $mime='image/x-icon'; $imgmime='image.png'; $famime='file-image-o'; }
6898  if (preg_match('/\.(jpg|jpeg)$/i',$tmpfile)) { $mime='image/jpeg'; $imgmime='image.png'; $famime='file-image-o'; }
6899  if (preg_match('/\.png$/i',$tmpfile)) { $mime='image/png'; $imgmime='image.png'; $famime='file-image-o'; }
6900  if (preg_match('/\.gif$/i',$tmpfile)) { $mime='image/gif'; $imgmime='image.png'; $famime='file-image-o'; }
6901  if (preg_match('/\.bmp$/i',$tmpfile)) { $mime='image/bmp'; $imgmime='image.png'; $famime='file-image-o'; }
6902  if (preg_match('/\.(tif|tiff)$/i',$tmpfile)) { $mime='image/tiff'; $imgmime='image.png'; $famime='file-image-o'; }
6903  // Calendar
6904  if (preg_match('/\.vcs$/i',$tmpfile)) { $mime='text/calendar'; $imgmime='other.png'; $famime='file-text-o'; }
6905  if (preg_match('/\.ics$/i',$tmpfile)) { $mime='text/calendar'; $imgmime='other.png'; $famime='file-text-o'; }
6906  // Other
6907  if (preg_match('/\.torrent$/i',$tmpfile)) { $mime='application/x-bittorrent'; $imgmime='other.png'; $famime='file-o'; }
6908  // Audio
6909  if (preg_match('/\.(mp3|ogg|au|wav|wma|mid)$/i',$tmpfile)) { $mime='audio'; $imgmime='audio.png'; $famime='file-audio-o'; }
6910  // Video
6911  if (preg_match('/\.ogv$/i',$tmpfile)) { $mime='video/ogg'; $imgmime='video.png'; $famime='file-video-o'; }
6912  if (preg_match('/\.webm$/i',$tmpfile)) { $mime='video/webm'; $imgmime='video.png'; $famime='file-video-o'; }
6913  if (preg_match('/\.avi$/i',$tmpfile)) { $mime='video/x-msvideo'; $imgmime='video.png'; $famime='file-video-o'; }
6914  if (preg_match('/\.divx$/i',$tmpfile)) { $mime='video/divx'; $imgmime='video.png'; $famime='file-video-o'; }
6915  if (preg_match('/\.xvid$/i',$tmpfile)) { $mime='video/xvid'; $imgmime='video.png'; $famime='file-video-o'; }
6916  if (preg_match('/\.(wmv|mpg|mpeg)$/i',$tmpfile)) { $mime='video'; $imgmime='video.png'; $famime='file-video-o'; }
6917  // Archive
6918  if (preg_match('/\.(zip|rar|gz|tgz|z|cab|bz2|7z|tar|lzh)$/i',$tmpfile)) { $mime='archive'; $imgmime='archive.png'; $famime='file-archive-o'; } // application/xxx where zzz is zip, ...
6919  // Exe
6920  if (preg_match('/\.(exe|com)$/i',$tmpfile)) { $mime='application/octet-stream'; $imgmime='other.png'; $famime='file-o'; }
6921  // Lib
6922  if (preg_match('/\.(dll|lib|o|so|a)$/i',$tmpfile)) { $mime='library'; $imgmime='library.png'; $famime='file-o'; }
6923  // Err
6924  if (preg_match('/\.err$/i',$tmpfile)) { $mime='error'; $imgmime='error.png'; $famime='file-text-o'; }
6925 
6926  // Return string
6927  if ($mode == 1)
6928  {
6929  $tmp=explode('/',$mime);
6930  return (! empty($tmp[1])?$tmp[1]:$tmp[0]);
6931  }
6932  if ($mode == 2)
6933  {
6934  return $imgmime;
6935  }
6936  if ($mode == 3)
6937  {
6938  return $srclang;
6939  }
6940  if ($mode == 4)
6941  {
6942  return $famime;
6943  }
6944  return $mime;
6945 }
6946 
6956 function getDictvalue($tablename, $field, $id, $checkentity=false, $rowidfield='rowid')
6957 {
6958  global $dictvalues,$db,$langs;
6959 
6960  if (!isset($dictvalues[$tablename]))
6961  {
6962  $dictvalues[$tablename] = array();
6963  $sql = 'SELECT * FROM '.$tablename.' WHERE 1';
6964  if ($checkentity) $sql.= ' entity IN (0,'.getEntity('').')';
6965 
6966  $resql = $db->query($sql);
6967  if ($resql)
6968  {
6969  while ($obj = $db->fetch_object($resql))
6970  {
6971  $dictvalues[$tablename][$obj->{$rowidfield}] = $obj;
6972  }
6973  }
6974  else
6975  {
6976  dol_print_error($db);
6977  }
6978  }
6979 
6980  if (!empty($dictvalues[$tablename][$id])) return $dictvalues[$tablename][$id]->{$field}; // Found
6981  else // Not found
6982  {
6983  if ($id > 0) return $id;
6984  return '';
6985  }
6986 }
6987 
6994 function colorIsLight($stringcolor)
6995 {
6996  $res = -1;
6997  if (!empty($stringcolor))
6998  {
6999  $res = 0;
7000  $tmp=explode(',', $stringcolor);
7001  if (count($tmp) > 1) // This is a comma RGB ('255','255','255')
7002  {
7003  $r = $tmp[0];
7004  $g = $tmp[1];
7005  $b = $tmp[2];
7006  }
7007  else
7008  {
7009  $hexr=$stringcolor[0].$stringcolor[1];
7010  $hexg=$stringcolor[2].$stringcolor[3];
7011  $hexb=$stringcolor[4].$stringcolor[5];
7012  $r = hexdec($hexr);
7013  $g = hexdec($hexg);
7014  $b = hexdec($hexb);
7015  }
7016  $bright = (max($r, $g, $b) + min($r, $g, $b)) / 510.0; // HSL algorithm
7017  if ($bright > 0.6) $res = 1;
7018  }
7019  return $res;
7020 }
7021 
7030 function isVisibleToUserType($type_user, &$menuentry, &$listofmodulesforexternal)
7031 {
7032  global $conf;
7033 
7034  //print 'type_user='.$type_user.' module='.$menuentry['module'].' enabled='.$menuentry['enabled'].' perms='.$menuentry['perms'];
7035  //print 'ok='.in_array($menuentry['module'], $listofmodulesforexternal);
7036  if (empty($menuentry['enabled'])) return 0; // Entry disabled by condition
7037  if ($type_user && $menuentry['module'])
7038  {
7039  $tmploops=explode('|',$menuentry['module']);
7040  $found=0;
7041  foreach($tmploops as $tmploop)
7042  {
7043  if (in_array($tmploop, $listofmodulesforexternal)) {
7044  $found++; break;
7045  }
7046  }
7047  if (! $found) return 0; // Entry is for menus all excluded to external users
7048  }
7049  if (! $menuentry['perms'] && $type_user) return 0; // No permissions and user is external
7050  if (! $menuentry['perms'] && ! empty($conf->global->MAIN_MENU_HIDE_UNAUTHORIZED)) return 0; // No permissions and option to hide when not allowed, even for internal user, is on
7051  if (! $menuentry['perms']) return 2; // No permissions and user is external
7052  return 1;
7053 }
verifCond($strRights)
Verify if condition in string is ok or not.
dol_htmlentitiesbr_decode($stringtodecode, $pagecodeto='UTF-8')
This function is called to decode a HTML string (it decodes entities and br tags) ...
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_sanitizePathName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a path name.
dol_htmloutput_events($disabledoutputofmessages=0)
Print formated messages to output (Used to show messages on html output).
img_picto_common($titlealt, $picto, $moreatt= '', $pictoisfullpath=0)
Show picto (generic function)
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
getDoliDBInstance($type, $host, $user, $pass, $name, $port)
Return a DoliDB instance (database handler).
dol_eval($s, $returnvalue=0, $hideerrors=1)
Replace eval function to add more security.
get_htmloutput_mesg($mesgstring='', $mesgarray='', $style='ok', $keepembedded=0)
Get formated messages to output (Used to show messages on html output).
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
dol_get_prev_month($month, $year)
Return previous month.
Definition: date.lib.php:362
isACompany()
Return if third party is a company (Business) or an end user (Consumer)
getImageFileNameForSize($file, $extName, $extImgTarget='')
Return the filename of file to get the thumbs.
dol_print_url($url, $target='_blank', $max=32, $withpicto=0)
Show Url link.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition: inc.php:282
getStaticMember($class, $member)
Function to return value of a static property when class name is dynamically defined (not hard coded)...
dol_sort_array(&$array, $index, $order='asc', $natsort=0, $case_sensitive=0, $keepindex=0)
Advanced sort array by second index function, which produces ascending (default) or descending output...
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_print_graph($htmlid, $width, $height, $data, $showlegend=0, $type='pie', $showpercent=0, $url='', $combineother=0.05, $shownographyet=0)
Show a javascript graph.
printCommonFooter($zone='private')
Print common footer : conf->global->MAIN_HTML_FOOTER js for switch of menu hider js for conf->global-...
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='')
Show tab header of a card.
dol_format_address($object, $withcountry=0, $sep="\n", $outputlangs='', $mode=0)
Return a formated address (part address/zip/town/state) according to country rules.
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add')
Complete or removed entries into a head array (used to build tabs).
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_size($size, $type='')
Optimize a size for some browsers (phone, smarphone, ...)
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart)
Return a path to have a the directory according to object where files are stored. ...
if(GETPOST('cancel','alpha')) if(!GETPOST('confirmmassaction','alpha')&&$massaction!= 'presend'&&$massaction!= 'confirm_presend')
Draft customers invoices.
Definition: list.php:147
dol_shutdown()
Function called at end of web php process.
Class to manage products or services.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into javascript code.
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
picto_required()
Return picto saying a field is required.
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return mime type of a file.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0)
Return an id or code from a code or id.
ajax_autoselect($htmlname, $addlink='')
Make content of an input box selected when we click into input field.
Definition: ajax.lib.php:367
getTaxesFromId($vatrate, $buyer=null, $seller=null, $firstparamisid=1)
Get vat main information from Id.
Class to manage menu Auguria.
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for properties) With native = 0: P...
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_strtolower($utf8_string)
Convert a string to lower.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
dol_strtoupper($utf8_string)
Convert a string to upper.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:414
dol_concatdesc($text1, $text2, $forxml=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
get_default_npr(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Fonction qui renvoie si tva doit etre tva percue recuperable.
isOnlyOneLocalTax($local)
Return true if LocalTax (1 or 2) is unique.
if(empty($reshook)) $form
View.
Definition: perms.php:103
dol_get_prev_day($day, $month, $year)
Return previous day.
Definition: date.lib.php:333
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_validElement($element)
Return if var element is ok.
get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate, from a $thirdparty_buyer to a $thirdparty_seller Note: This function applies same rules than get_default_tva.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
get_date_range($date_start, $date_end, $format= '', $outputlangs='', $withparenthesis=1)
Format output for start and end date.
get_product_localtax_for_country($idprod, $local, $thirdparty_seller)
Return localtax vat rate of a product in a particular selling country or default country vat if produ...
get_htmloutput_errors($mesgstring='', $mesgarray='', $keepembedded=0)
Get formated error messages to output (Used to show messages on html output).
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="")
Scan a directory and return a list of files/directories.
Definition: files.lib.php:58
dol_htmloutput_errors($mesgstring='', $mesgarray='', $keepembedded=0)
Print formated error messages to output (Used to show messages on html output).
dol_htmloutput_mesg($mesgstring='', $mesgarray='', $style='ok', $keepembedded=0)
Print formated messages to output (Used to show messages on html output).
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_address($address, $htmlid, $mode, $id, $noprint=0, $charfornl='')
Format address string.
dol_print_ip($ip, $mode=0)
Return an IP formated to be shown on screen.
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null)
Return array of possible common substitutions.
dol_nboflines($s, $maxchar=0)
Return nb of lines of a clear text.
Class to manage standard extra fields.
showDirectDownloadLink($object)
Return string with full Url.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting a parameter.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage third parties objects (customers, suppliers, prospects...)
get_default_localtax($thirdparty_seller, $thirdparty_buyer, $local, $idprod=0)
Function that return localtax of a product line (according to seller, buyer and product vat rate) Si ...
img_warning($titlealt= 'default', $moreatt= '')
Show warning logo.
dolGetCountryCodeFromIp($ip)
Return a country code from IP.
getAdvancedPreviewUrl($modulepart, $relativepath, $alldata=0, $param='')
Return URL we can use for advanced preview links.
dolEscapeXML($string)
Encode string for xml usage.
dol_print_skype($skype, $cid=0, $socid=0, $addlink=0, $max=64)
Show Skype link.
if($_POST["cancel"]==$langs->trans("Cancel")&&!$id) if($action== 'setdatev'&&$user->rights->tax->charges->creer) if($action== 'add'&&$_POST["cancel"]<> $langs->trans("Cancel")) if($action== 'delete') $title
Actions.
Definition: card.php:183
dol_get_next_month($month, $year)
Return next month.
Definition: date.lib.php:383
dol_fiche_end($notab=0)
Show tab footer of a card.
dol_get_next_day($day, $month, $year)
Return next day.
Definition: date.lib.php:348
getEntity($element, $shared=1, $forceentity=null)
Get list of entity id to use.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
getBrowserInfo($user_agent)
Return information about user browser.
natural_search($fields, $value, $mode=0, $nofirstand=0)
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
dolGetFirstLineOfText($text, $nboflines=1)
Return first line of text.
dol_string_is_good_iso($s)
Check if a string is a correct iso string If not, it will we considered not HTML encoded even if it i...
getServerTimeZoneInt($refgmtdate='now')
Return server timezone int.
Definition: date.lib.php:82
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
dol_now($mode='gmt')
Return date for now.
dol_user_country()
Return country code for current user.
utf8_check($str)
Check if a string is in UTF8.
dolExplodeIntoArray($string, $delimiter= ';', $kv= '=')
Split a string with 2 keys into key array.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='')
Show tab header of a card.
dol_string_nohtmltag($stringtoclean, $removeli