2008-08-06 16:50:06 +02:00
< ? php
/* Copyright ( C ) 2000 - 2007 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2003 Jean - Louis Bergamo < jlb @ j1b . org >
2012-02-10 16:37:41 +01:00
* Copyright ( C ) 2004 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
2008-08-06 16:50:06 +02:00
* Copyright ( C ) 2004 Sebastien Di Cintio < sdicintio @ ressource - toi . org >
* Copyright ( C ) 2004 Benoit Mortier < benoit . mortier @ opensides . be >
* Copyright ( C ) 2004 Christophe Combelles < ccomb @ free . fr >
2012-12-30 15:13:49 +01:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ capnetworks . com >
2008-08-06 16:58:26 +02:00
* Copyright ( C ) 2008 Raphael Bertrand ( Resultic ) < raphael . bertrand @ resultic . fr >
2011-10-29 23:52:05 +02:00
* Copyright ( C ) 2010 - 2011 Juanjo Menent < jmenent @ 2 byte . es >
2008-08-06 16:50:06 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2008-08-06 16:50:06 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2011-08-01 01:24:38 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2008-08-06 16:50:06 +02:00
* or see http :// www . gnu . org /
*/
/**
2011-10-24 12:59:44 +02:00
* \file htdocs / core / lib / functions . lib . php
2008-11-16 02:09:04 +01:00
* \brief A set of functions for Dolibarr
* This file contains all frequently used functions .
2008-08-06 16:50:06 +02:00
*/
2011-11-05 03:15:35 +01:00
if ( ! function_exists ( 'json_encode' ))
{
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/json.lib.php' ;
2011-11-05 03:15:35 +01:00
}
2011-12-27 00:07:42 +01:00
/**
2011-12-28 01:19:52 +01:00
* Function to return value of a static property when class
* name is dynamically defined ( not hard coded ) .
* This is because $myclass :: $myvar works from PHP 5.3 . 0 + only
2011-12-29 23:50:02 +01:00
*
2011-12-28 01:19:52 +01:00
* @ param string $class Class name
* @ param string $member Name of property
* @ return string Return value of static property .
2011-12-27 00:07:42 +01:00
*/
function getStaticMember ( $class , $member )
{
if ( is_object ( $class )) $class = get_class ( $class );
$classObj = new ReflectionClass ( $class );
$result = null ;
2011-12-29 23:50:02 +01:00
2011-12-27 00:07:42 +01:00
foreach ( $classObj -> getStaticProperties () as $prop => $value )
{
if ( $prop == $member )
{
$result = $value ;
break ;
}
}
2011-12-29 23:50:02 +01:00
2011-12-27 00:07:42 +01:00
return $result ;
}
2011-11-05 03:15:35 +01:00
2011-09-28 16:26:49 +02:00
/**
* Return a DoliDB instance ( database handler ) .
*
* @ param string $type Type of database ( mysql , pgsql ... )
* @ param string $host Address of database server
* @ param string $user Nom de l ' utilisateur autorise
* @ param string $pass Mot de passe
* @ param string $name Nom de la database
* @ param int $port Port of database server
* @ return DoliDB A DoliDB instance
*/
function getDoliDBInstance ( $type , $host , $user , $pass , $name , $port )
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . " /core/db/ " . $type . '.class.php' ;
2011-09-28 16:26:49 +02:00
2012-08-03 23:29:08 +02:00
$class = 'DoliDB' . ucfirst ( $type );
$dolidb = new $class ( $type , $host , $user , $pass , $name , $port );
return $dolidb ;
2011-09-28 16:26:49 +02:00
}
2012-01-11 14:14:14 +01:00
/**
* Get entity to use
2012-01-15 02:39:43 +01:00
*
2012-01-11 14:14:14 +01:00
* @ param string $element Current element
* @ param int $shared 1 = Return shared entities
* @ return mixed Entity id ( s ) to use
*/
function getEntity ( $element = false , $shared = false )
{
global $conf , $mc ;
2012-01-15 02:39:43 +01:00
2012-01-11 14:14:14 +01:00
if ( is_object ( $mc ))
{
return $mc -> getEntity ( $element , $shared );
}
else
{
2012-01-11 15:07:17 +01:00
$out = '' ;
2012-01-15 02:39:43 +01:00
2012-01-11 15:07:17 +01:00
$addzero = array ( 'user' , 'usergroup' );
if ( in_array ( $element , $addzero )) $out .= '0,' ;
2012-01-15 02:39:43 +01:00
2012-01-11 15:07:17 +01:00
$out .= $conf -> entity ;
2012-01-15 02:39:43 +01:00
2012-01-11 15:07:17 +01:00
return $out ;
2012-01-11 14:14:14 +01:00
}
}
2011-09-28 16:26:49 +02:00
2012-02-29 19:41:12 +01:00
/**
* Return information about user browser
*
* @ return array Array of information ( 'browsername' => , 'browseros' => , 'phone' => , 'browserfirefox' => )
*/
function getBrowserInfo ()
{
2012-08-03 23:29:08 +02:00
$name = 'unknown' ; $version = '' ; $os = 'unknown' ; $phone = '' ;
// If phone/smartphone, we set phone os name.
if ( preg_match ( '/android/i' , $_SERVER [ " HTTP_USER_AGENT " ])) { $os = $phone = 'android' ; }
elseif ( preg_match ( '/blackberry/i' , $_SERVER [ " HTTP_USER_AGENT " ])) { $os = $phone = 'blackberry' ; }
elseif ( preg_match ( '/iphone/i' , $_SERVER [ " HTTP_USER_AGENT " ])) { $os = 'ios' ; $phone = 'iphone' ; }
elseif ( preg_match ( '/ipod/i' , $_SERVER [ " HTTP_USER_AGENT " ])) { $os = 'ios' ; $phone = 'iphone' ; }
elseif ( preg_match ( '/palm/i' , $_SERVER [ " HTTP_USER_AGENT " ])) { $os = $phone = 'palm' ; }
elseif ( preg_match ( '/symbian/i' , $_SERVER [ " HTTP_USER_AGENT " ])) { $os = 'symbian' ; $phone = 'unknown' ; }
elseif ( preg_match ( '/webos/i' , $_SERVER [ " HTTP_USER_AGENT " ])) { $os = 'webos' ; $phone = 'unknown' ; }
elseif ( preg_match ( '/maemo/i' , $_SERVER [ " HTTP_USER_AGENT " ])) { $os = 'maemo' ; $phone = 'unknown' ; }
// MS products at end
elseif ( preg_match ( '/iemobile/i' , $_SERVER [ " HTTP_USER_AGENT " ])) { $os = 'windows' ; $phone = 'unkown' ; }
elseif ( preg_match ( '/windows ce/i' , $_SERVER [ " HTTP_USER_AGENT " ])) { $os = 'windows' ; $phone = 'unkown' ; }
// Name
if ( preg_match ( '/firefox(\/|\s)([\d\.]*)/i' , $_SERVER [ " HTTP_USER_AGENT " ], $reg )) { $name = 'firefox' ; $version = $reg [ 2 ]; }
elseif ( preg_match ( '/chrome(\/|\s)([\d\.]+)/i' , $_SERVER [ " HTTP_USER_AGENT " ], $reg )) { $name = 'chrome' ; $version = $reg [ 2 ]; } // we can have 'chrome (Mozilla...) chrome x.y' in one string
elseif ( preg_match ( '/chrome/i' , $_SERVER [ " HTTP_USER_AGENT " ], $reg )) { $name = 'chrome' ; }
elseif ( preg_match ( '/iceweasel/i' , $_SERVER [ " HTTP_USER_AGENT " ])) { $name = 'iceweasel' ; $version = $reg [ 2 ]; }
elseif ( preg_match ( '/epiphany/i' , $_SERVER [ " HTTP_USER_AGENT " ])) { $name = 'epiphany' ; $version = $reg [ 2 ]; }
elseif (( empty ( $phone ) || preg_match ( '/iphone/i' , $_SERVER [ " HTTP_USER_AGENT " ])) && preg_match ( '/safari(\/|\s)([\d\.]*)/i' , $_SERVER [ " HTTP_USER_AGENT " ], $reg )) { $name = 'safari' ; $version = $reg [ 2 ]; } // Safari is often present in string for mobile but its not.
elseif ( preg_match ( '/opera(\/|\s)([\d\.]*)/i' , $_SERVER [ " HTTP_USER_AGENT " ], $reg )) { $name = 'opera' ; $version = $reg [ 2 ]; }
elseif ( preg_match ( '/msie(\/|\s)([\d\.]*)/i' , $_SERVER [ " HTTP_USER_AGENT " ], $reg )) { $name = 'ie' ; $version = $reg [ 2 ]; } // MS products at end
// Other
$firefox = 0 ;
if ( in_array ( $name , array ( 'firefox' , 'iceweasel' ))) $firefox = 1 ;
return array ( 'browsername' => $name , 'browserversion' => $version , 'browseros' => $os , 'phone' => $phone , 'browserfirefox' => $firefox );
2012-02-29 19:41:12 +01:00
}
2011-03-09 16:02:52 +01:00
/**
* Function called at end of web php process
2011-09-20 13:43:14 +02:00
*
2011-09-17 02:47:01 +02:00
* @ return void
2011-03-09 16:02:52 +01:00
*/
function dol_shutdown ()
{
2012-08-03 23:29:08 +02:00
global $conf , $user , $langs , $db ;
$disconnectdone = false ; $depth = 0 ;
if ( is_object ( $db ) && ! empty ( $db -> connected )) { $depth = $db -> transaction_opened ; $disconnectdone = $db -> close (); }
2012-10-25 14:16:06 +02:00
dol_syslog ( " --- End access to " . $_SERVER [ " PHP_SELF " ] . (( $disconnectdone && $depth ) ? ' (Warn: db disconnection forced, transaction depth was ' . $depth . ')' : '' ), (( $disconnectdone && $depth ) ? LOG_WARNING : LOG_DEBUG ));
2011-03-09 16:02:52 +01:00
}
2009-08-12 01:42:21 +02:00
2011-11-05 03:15:35 +01:00
2010-08-29 20:29:19 +02:00
/**
2010-11-20 14:08:44 +01:00
* Return value of a param into GET or POST supervariable
2011-08-17 18:18:12 +02:00
*
2011-09-17 02:47:01 +02:00
* @ param string $paramname Name of parameter to found
2012-03-24 14:19:22 +01:00
* @ param string $check Type of check ( '' = no check , 'int' = check it 's numeric, ' alpha '=check it' s alpha only , 'array' = check it ' s array )
2011-09-17 02:47:01 +02:00
* @ param int $method Type of method ( 0 = get then post , 1 = only get , 2 = only post , 3 = post then get )
* @ return string Value found or '' if check fails
2010-08-29 20:29:19 +02:00
*/
2010-11-11 00:05:35 +01:00
function GETPOST ( $paramname , $check = '' , $method = 0 )
2010-08-29 20:29:19 +02:00
{
2011-11-25 16:47:57 +01:00
if ( empty ( $method )) $out = isset ( $_GET [ $paramname ]) ? $_GET [ $paramname ] : ( isset ( $_POST [ $paramname ]) ? $_POST [ $paramname ] : '' );
elseif ( $method == 1 ) $out = isset ( $_GET [ $paramname ]) ? $_GET [ $paramname ] : '' ;
elseif ( $method == 2 ) $out = isset ( $_POST [ $paramname ]) ? $_POST [ $paramname ] : '' ;
elseif ( $method == 3 ) $out = isset ( $_POST [ $paramname ]) ? $_POST [ $paramname ] : ( isset ( $_GET [ $paramname ]) ? $_GET [ $paramname ] : '' );
2012-08-03 23:29:08 +02:00
else return 'BadParameter' ;
2011-11-27 02:23:55 +01:00
2011-11-25 16:47:57 +01:00
if ( ! empty ( $check ))
{
// Check if numeric
2012-03-24 14:19:22 +01:00
if ( $check == 'int' && ! preg_match ( '/^[-\.,0-9]+$/i' , $out ))
{
$out = trim ( $out );
$out = '' ;
}
2011-11-25 16:47:57 +01:00
// Check if alpha
2012-02-29 11:48:03 +01:00
elseif ( $check == 'alpha' )
2012-02-27 17:02:56 +01:00
{
2012-03-24 14:19:22 +01:00
$out = trim ( $out );
2012-08-03 23:29:08 +02:00
// '"' is dangerous because param in url can close the href= or src= and add javascript functions.
// '../' is dangerous because it allows dir transversals
if ( preg_match ( '/"/' , $out )) $out = '' ;
2012-02-29 11:48:03 +01:00
else if ( preg_match ( '/\.\.\//' , $out )) $out = '' ;
2012-02-27 17:02:56 +01:00
}
2012-03-24 14:19:22 +01:00
elseif ( $check == 'array' )
{
if ( ! is_array ( $out ) || empty ( $out )) $out = array ();
}
2011-11-25 16:47:57 +01:00
}
2011-11-27 02:23:55 +01:00
2011-11-25 16:47:57 +01:00
return $out ;
2010-08-29 20:29:19 +02:00
}
2010-12-27 20:13:06 +01:00
/**
2010-12-27 20:25:59 +01:00
* Return a prefix to use for this Dolibarr instance for session or cookie names .
2011-08-17 18:18:12 +02:00
* This prefix is unique for instance and avoid conflict between multi - instances ,
* even when having two instances with one root dir or two instances in virtual servers
*
2011-09-17 02:47:01 +02:00
* @ return string A calculated prefix
2010-12-27 20:13:06 +01:00
*/
function dol_getprefix ()
{
2012-08-03 23:29:08 +02:00
return dol_hash ( $_SERVER [ " SERVER_NAME " ] . $_SERVER [ " DOCUMENT_ROOT " ] . DOL_DOCUMENT_ROOT . DOL_URL_ROOT );
2010-12-27 20:13:06 +01:00
}
2010-12-19 03:42:53 +01:00
/**
* Make an include_once using default root and alternate root if it fails .
2010-12-29 11:39:41 +01:00
* WARNING : In most cases , you should not use this function :
* To link to a core file , use include ( DOL_DOCUMENT_ROOT . '/pathtofile' )
2012-08-23 02:04:35 +02:00
* To link to a module file from a module file , use include './mymodulefile' ;
2011-08-17 18:18:12 +02:00
* To link to a module file from a core file , then this function can be used
*
2011-09-17 02:47:01 +02:00
* @ param string $relpath Relative path to file ( Ie : mydir / myfile , ../ myfile , ... )
2012-08-02 08:02:49 +02:00
* @ param string $classname Class name
2011-09-17 02:47:01 +02:00
* @ return int false if include fails .
2010-12-19 03:42:53 +01:00
*/
2012-08-02 08:02:49 +02:00
function dol_include_once ( $relpath , $classname = '' )
2010-12-19 03:42:53 +01:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs , $user , $mysoc ; // Other global var must be retreived with $GLOBALS['var']
2012-12-02 12:59:06 +01:00
2012-08-03 23:29:08 +02:00
if ( ! empty ( $classname ) && ! class_exists ( $classname )) {
2012-12-02 12:59:06 +01:00
return @ include dol_buildpath ( $relpath ); // Remove @ to find error into php log file if you have problems
2012-08-03 23:29:08 +02:00
} else {
2012-12-02 12:59:06 +01:00
return @ include_once dol_buildpath ( $relpath ); // Remove @ to find error into php log file if you have problems
2012-08-03 23:29:08 +02:00
}
2010-12-19 03:42:53 +01:00
}
2010-12-19 12:05:07 +01:00
/**
2011-08-17 18:18:12 +02:00
* Return path of url or filesystem . Return default_root or alternate root if file_exist fails
*
2011-09-17 02:47:01 +02:00
* @ param string $path Relative path to file ( if mode = 0 , ie : mydir / myfile , ../ myfile , ... ) or relative url ( if mode = 1 ) .
* @ param int $type 0 = Used for a Filesystem path , 1 = Used for an URL path ( output relative ), 2 = Used for an URL path ( output full path )
* @ return string Full filsystem path ( if mode = 0 ), Full url path ( if mode = 1 )
2010-12-19 12:05:07 +01:00
*/
2012-08-02 08:02:49 +02:00
function dol_buildpath ( $path , $type = 0 )
2010-12-19 12:05:07 +01:00
{
2012-08-03 23:29:08 +02:00
if ( empty ( $type )) // For a filesystem path
{
$res = DOL_DOCUMENT_ROOT . $path ; // Standard value
if ( defined ( 'DOL_DOCUMENT_ROOT_ALT' ) && DOL_DOCUMENT_ROOT_ALT ) // We check only if alternate feature is used
{
if ( ! file_exists ( DOL_DOCUMENT_ROOT . $path )) $res = DOL_DOCUMENT_ROOT_ALT . $path ;
}
}
else // For an url path
{
// We try to get local path of file on filesystem from url
// Note that trying to know if a file on disk exist by forging path on disk from url
// works only for some web server and some setup. This is bugged when
// using proxy, rewriting, virtual path, etc...
if ( $type == 1 )
{
$res = DOL_URL_ROOT . $path ; // Standard value
if ( defined ( 'DOL_URL_ROOT_ALT' ) && DOL_URL_ROOT_ALT ) // We check only if alternate feature is used
{
preg_match ( '/^([^\?]+(\.css\.php|\.css|\.js\.php|\.js|\.png|\.jpg|\.php)?)/i' , $path , $regs ); // Take part before '?'
if ( ! empty ( $regs [ 1 ]))
{
if ( ! file_exists ( DOL_DOCUMENT_ROOT . $regs [ 1 ])) $res = DOL_URL_ROOT_ALT . $path ;
}
}
}
else if ( $type == 2 )
{
$res = DOL_MAIN_URL_ROOT . $path ; // Standard value
if ( defined ( 'DOL_URL_ROOT_ALT' ) && DOL_URL_ROOT_ALT ) // We check only if alternate feature is used
{
preg_match ( '/^([^\?]+(\.css\.php|\.css|\.js\.php|\.js|\.png|\.jpg|\.php)?)/i' , $path , $regs ); // Take part before '?'
if ( ! empty ( $regs [ 1 ]))
{
if ( ! file_exists ( DOL_DOCUMENT_ROOT . $regs [ 1 ])) $res = DOL_MAIN_URL_ROOT_ALT . $path ;
}
}
}
}
return $res ;
2010-12-19 12:05:07 +01:00
}
2009-08-12 01:42:21 +02:00
/**
2010-11-03 16:54:06 +01:00
* Create a clone of instance of object ( new instance with same properties )
2011-08-17 18:18:12 +02:00
* This function works for both PHP4 and PHP5
*
2011-09-17 02:47:01 +02:00
* @ param object $object Object to clone
* @ return object Object clone
2009-08-12 01:42:21 +02:00
*/
function dol_clone ( $object )
{
2012-08-03 23:29:08 +02:00
dol_syslog ( " Functions.lib::dol_clone Clone object " );
// We create dynamically a clone function, making a =
if ( version_compare ( phpversion (), '5.0' ) < 0 && ! function_exists ( 'clone' ))
{
eval ( 'function clone($object){return($object);}' );
}
$myclone = clone ( $object );
return $myclone ;
2009-08-12 01:42:21 +02:00
}
2009-09-15 13:24:00 +02:00
/**
2011-03-09 16:02:52 +01:00
* Optimize a size for some browsers ( phone , smarphone , ... )
2011-08-17 18:18:12 +02:00
*
2011-09-17 02:47:01 +02:00
* @ param int $size Size we want
* @ param string $type Type of optimizing :
2011-09-03 01:57:26 +02:00
* '' = function used to define a size for truncation
* 'width' = function is used to define a width
2011-09-17 02:47:01 +02:00
* @ return int New size after optimizing
2009-09-15 13:24:00 +02:00
*/
function dol_size ( $size , $type = '' )
{
2012-08-03 23:29:08 +02:00
global $conf ;
if ( empty ( $conf -> browser -> phone )) return $size ;
if ( $type == 'width' && $size > 250 ) return 250 ;
else return 10 ;
2009-09-15 13:24:00 +02:00
}
2009-08-12 01:42:21 +02:00
2008-08-06 16:50:06 +02:00
/**
2011-08-17 18:18:12 +02:00
* Clean a string to use it as a file name
*
2011-09-17 02:47:01 +02:00
* @ param string $str String to clean
* @ param string $newstr String to replace bad chars with
2012-03-21 15:58:04 +01:00
* @ param string $unaccent 1 = Remove also accent ( default ), 0 do not remove them
2011-09-17 02:47:01 +02:00
* @ return string String cleaned ( a - zA - Z_ )
*
2010-10-03 17:42:01 +02:00
* @ see dol_string_nospecial , dol_string_unaccent
2008-10-25 23:35:27 +02:00
*/
2012-03-21 15:58:04 +01:00
function dol_sanitizeFileName ( $str , $newstr = '_' , $unaccent = 1 )
2008-10-25 23:35:27 +02:00
{
2011-12-03 20:21:45 +01:00
$filesystem_forbidden_chars = array ( '<' , '>' , ':' , '/' , '\\' , '?' , '*' , '|' , '"' );
2012-08-03 23:29:08 +02:00
return dol_string_nospecial ( $unaccent ? dol_string_unaccent ( $str ) : $str , $newstr , $filesystem_forbidden_chars );
2008-10-25 23:35:27 +02:00
}
/**
2011-08-17 18:18:12 +02:00
* Clean a string from all accent characters to be used as ref , login or by dol_sanitizeFileName
*
2011-09-17 02:47:01 +02:00
* @ param string $str String to clean
* @ return string Cleaned string
*
2010-10-03 17:42:01 +02:00
* @ see dol_sanitizeFilename , dol_string_nospecial
2008-08-06 16:50:06 +02:00
*/
2008-10-25 23:35:27 +02:00
function dol_string_unaccent ( $str )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
if ( utf8_check ( $str ))
{
$string = rawurlencode ( $str );
$replacements = array (
2008-11-09 01:08:52 +01:00
'%C3%80' => 'A' , '%C3%81' => 'A' ,
'%C3%88' => 'E' , '%C3%89' => 'E' ,
'%C3%8C' => 'I' , '%C3%8D' => 'I' ,
'%C3%92' => 'O' , '%C3%93' => 'O' ,
'%C3%99' => 'U' , '%C3%9A' => 'U' ,
'%C3%A0' => 'a' , '%C3%A1' => 'a' , '%C3%A2' => 'a' ,
'%C3%A8' => 'e' , '%C3%A9' => 'e' , '%C3%AA' => 'e' , '%C3%AB' => 'e' ,
2011-01-12 18:49:18 +01:00
'%C3%AC' => 'i' , '%C3%AD' => 'i' , '%C3%AE' => 'i' ,
2008-11-09 01:08:52 +01:00
'%C3%B2' => 'o' , '%C3%B3' => 'o' ,
'%C3%B9' => 'u' , '%C3%BA' => 'u'
2008-10-26 00:23:22 +02:00
);
2008-10-26 00:54:31 +02:00
$string = strtr ( $string , $replacements );
return rawurldecode ( $string );
2012-08-03 23:29:08 +02:00
}
else
{
$string = strtr (
$str ,
" \xC0 \xC1 \xC2 \xC3 \xC5 \xC7
\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1
\xD2\xD3\xD4\xD5\xD8\xD9\xDA\xDB\xDD
\xE0\xE1\xE2\xE3\xE5\xE7\xE8\xE9\xEA\xEB
\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF8
\xF9\xFA\xFB\xFD\xFF " ,
" AAAAAC
EEEEIIIIDN
OOOOOUUUY
aaaaaceeee
iiiidnooooo
uuuyy "
);
$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 " ));
return $string ;
}
2008-08-06 16:50:06 +02:00
}
/**
2011-08-17 18:18:12 +02:00
* Clean a string from all punctuation characters to use it as a ref or login
*
2011-09-17 02:47:01 +02:00
* @ param string $str String to clean
* @ param string $newstr String to replace forbidden chars with
* @ param array $badchars List of forbidden characters
* @ return string Cleaned string
2011-09-20 13:43:14 +02:00
*
2010-10-03 17:42:01 +02:00
* @ see dol_sanitizeFilename , dol_string_unaccent
2008-08-06 16:50:06 +02:00
*/
2010-10-03 17:42:01 +02:00
function dol_string_nospecial ( $str , $newstr = '_' , $badchars = '' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
$forbidden_chars_to_replace = array ( " " , " ' " , " / " , " \\ " , " : " , " * " , " ? " , " \" " , " < " , " > " , " | " , " [ " , " ] " , " , " , " ; " , " = " );
$forbidden_chars_to_remove = array ();
if ( is_array ( $badchars )) $forbidden_chars_to_replace = $badchars ;
//$forbidden_chars_to_remove=array("(",")");
2008-10-09 19:29:32 +02:00
2012-08-03 23:29:08 +02:00
return str_replace ( $forbidden_chars_to_replace , $newstr , str_replace ( $forbidden_chars_to_remove , " " , $str ));
2008-08-06 16:50:06 +02:00
}
/**
2011-05-01 12:52:10 +02:00
* Returns text escaped for inclusion into javascript code
2011-08-17 18:18:12 +02:00
*
2011-09-03 01:57:26 +02:00
* @ param string $stringtoescape String to escape
* @ return string Escaped string
2008-08-06 16:50:06 +02:00
*/
function dol_escape_js ( $stringtoescape )
{
2012-08-03 23:29:08 +02:00
// escape quotes and backslashes, newlines, etc.
$substitjs = array ( " ' " => " \\ ' " , '\\' => '\\\\' , " ' " => " \\ ' " , '"' => " \\ ' " , " \r " => '\\r' , " \n " => '\\n' , '</' => '<\/' );
return strtr ( $stringtoescape , $substitjs );
2008-08-06 16:50:06 +02:00
}
2008-10-09 19:29:32 +02:00
2009-01-28 10:00:29 +01:00
/**
2011-07-04 09:28:11 +02:00
* Returns text escaped for inclusion in HTML alt or title tags
2011-08-17 18:18:12 +02:00
*
2011-09-03 01:57:26 +02:00
* @ param string $stringtoescape String to escape
* @ param int $keepb Do not clean b tags
* @ return string Escaped string
2009-01-28 10:00:29 +01:00
*/
2011-07-04 09:28:11 +02:00
function dol_escape_htmltag ( $stringtoescape , $keepb = 0 )
2009-01-28 10:00:29 +01:00
{
2012-08-03 23:29:08 +02:00
// escape quotes and backslashes, newlines, etc.
$tmp = dol_html_entity_decode ( $stringtoescape , ENT_COMPAT , 'UTF-8' );
if ( $keepb ) $tmp = strtr ( $tmp , array ( " \r " => '\\r' , " \n " => '\\n' ));
else $tmp = strtr ( $tmp , array ( " \r " => '\\r' , " \n " => '\\n' , " <b> " => '' , '</b>' => '' ));
return dol_htmlentities ( $tmp , ENT_COMPAT , 'UTF-8' );
2009-01-28 10:00:29 +01:00
}
2012-10-18 16:30:12 +02:00
2012-10-23 09:30:48 +02:00
/**
2012-10-18 16:30:12 +02:00
* Convert a string to lower . Never use strtolower because it does not works with UTF8 strings .
2012-10-23 09:30:48 +02:00
*
2012-10-18 16:30:12 +02:00
* @ param string $utf8_string String to encode
2012-10-23 09:30:48 +02:00
* @ return string String converted
2012-10-18 16:30:12 +02:00
*/
function dol_strtolower ( $utf8_string )
{
return mb_strtolower ( $utf8_string , " UTF-8 " );
}
2012-10-23 09:30:48 +02:00
/**
2012-10-18 16:30:12 +02:00
* Convert a string to upper . Never use strtolower because it does not works with UTF8 strings .
2012-10-23 09:30:48 +02:00
*
2012-10-18 16:30:12 +02:00
* @ param string $utf8_string String to encode
2012-10-23 09:30:48 +02:00
* @ return string String converted
2012-10-18 16:30:12 +02:00
*/
2012-10-28 13:57:21 +01:00
function dol_strtoupper ( $utf8_string )
{
return mb_strtoupper ( $utf8_string , " UTF-8 " );
}
2012-10-18 16:30:12 +02:00
2008-08-06 16:50:06 +02:00
/**
2011-10-03 17:19:39 +02:00
* Write log message into outputs . Possible outputs can be :
2013-01-14 17:12:13 +01:00
* SYSLOG_HANDLERS = [ " mod_syslog_file " ] file name is then defined by SYSLOG_FILE
* SYSLOG_HANDLERS = [ " mod_syslog_syslog " ] facility is then defined by SYSLOG_FACILITY
2012-04-02 18:37:37 +02:00
* Warning , syslog functions are bugged on Windows , generating memory protection faults . To solve
* this , use logging to files instead of syslog ( see setup of module ) .
* Note : If SYSLOG_FILE_NO_ERROR defined , we never output any error message when writing to log fails .
2012-02-17 23:24:47 +01:00
* Note : You can get log message into html sources by adding parameter & logtohtml = 1 ( constant MAIN_LOGTOHTML must be set )
2012-04-02 18:37:37 +02:00
* This function works only if syslog module is enabled .
2011-10-03 17:19:39 +02:00
* This must not use any call to other function calling dol_syslog ( avoid infinite loop ) .
2011-08-28 18:22:09 +02:00
*
2011-10-03 17:19:39 +02:00
* @ param string $message Line to log . Ne doit pas etre traduit si level = LOG_ERR
2012-04-02 18:37:37 +02:00
* @ param int $level Log level
2012-12-02 15:20:45 +01:00
* 0 = Show nothing
2011-10-03 17:19:39 +02:00
* On Windows LOG_ERR = 4 , LOG_WARNING = 5 , LOG_NOTICE = LOG_INFO = 6 , LOG_DEBUG = 6 si define_syslog_variables ou PHP 5.3 + , 7 si dolibarr
* On Linux LOG_ERR = 3 , LOG_WARNING = 4 , LOG_INFO = 6 , LOG_DEBUG = 7
2012-12-02 15:20:45 +01:00
* @ param int $ident 1 = Increase ident of 1 , - 1 = Decrease ident of 1
2012-04-02 18:37:37 +02:00
* @ return void
2008-08-06 16:50:06 +02:00
*/
2012-12-02 15:20:45 +01:00
function dol_syslog ( $message , $level = LOG_INFO , $ident = 0 )
2008-08-06 16:50:06 +02:00
{
2012-12-02 15:20:45 +01:00
global $conf , $user ;
2012-10-16 02:01:37 +02:00
// If syslog module enabled
if ( empty ( $conf -> syslog -> enabled )) return false ;
2012-12-02 15:20:45 +01:00
if ( ! empty ( $level ))
2012-10-16 02:01:37 +02:00
{
2012-12-02 15:20:45 +01:00
// Test log level
$logLevels = array ( LOG_EMERG , LOG_ALERT , LOG_CRIT , LOG_ERR , LOG_WARNING , LOG_NOTICE , LOG_INFO , LOG_DEBUG );
if ( ! in_array ( $level , $logLevels ))
{
throw new Exception ( 'Incorrect log level' );
}
if ( $level > $conf -> global -> SYSLOG_LEVEL ) return false ;
2012-08-03 23:29:08 +02:00
2012-12-02 15:20:45 +01:00
// If adding log inside HTML page is required
if ( ! empty ( $_REQUEST [ 'logtohtml' ]) && ! empty ( $conf -> global -> MAIN_LOGTOHTML ))
{
$conf -> logbuffer [] = dol_print_date ( time (), " %Y-%m-%d %H:%M:%S " ) . " " . $message ;
}
2012-08-03 23:29:08 +02:00
2012-12-02 15:20:45 +01:00
// If enable html log tag enabled and url parameter log defined, we show output log on HTML comments
if ( ! empty ( $conf -> global -> MAIN_ENABLE_LOG_HTML ) && ! empty ( $_GET [ " log " ]))
{
print " \n \n <!-- Log start \n " ;
print $message . " \n " ;
print " Log end --> \n " ;
}
$data = array (
'message' => $message ,
'script' => ( isset ( $_SERVER [ 'PHP_SELF' ]) ? basename ( $_SERVER [ 'PHP_SELF' ], '.php' ) : false ),
'level' => $level ,
'user' => (( is_object ( $user ) && $user -> id ) ? $user -> login : false ),
'ip' => false
);
if ( ! empty ( $_SERVER [ " REMOTE_ADDR " ])) $data [ 'ip' ] = $_SERVER [ 'REMOTE_ADDR' ];
// This is when PHP session is ran inside a web server but not inside a client request (example: init code of apache)
else if ( ! empty ( $_SERVER [ 'SERVER_ADDR' ])) $data [ 'ip' ] = $_SERVER [ 'SERVER_ADDR' ];
// 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).
else if ( ! empty ( $_SERVER [ 'COMPUTERNAME' ])) $data [ 'ip' ] = $_SERVER [ 'COMPUTERNAME' ] . ( empty ( $_SERVER [ 'USERNAME' ]) ? '' : '@' . $_SERVER [ 'USERNAME' ]);
// 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).
else if ( ! empty ( $_SERVER [ 'LOGNAME' ])) $data [ 'ip' ] = '???@' . $_SERVER [ 'LOGNAME' ];
// Loop on each log handler and send output
foreach ( $conf -> loghandlers as $loghandlerinstance )
{
$loghandlerinstance -> export ( $data );
}
unset ( $data );
2012-10-16 02:01:37 +02:00
}
2012-08-03 23:29:08 +02:00
2012-12-02 15:20:45 +01:00
if ( ! empty ( $ident ))
2012-10-16 02:01:37 +02:00
{
2012-12-05 11:18:45 +01:00
foreach ( $conf -> loghandlers as $loghandlerinstance )
{
$loghandlerinstance -> setIdent ( $ident );
}
2012-08-03 23:29:08 +02:00
}
2008-08-06 16:50:06 +02:00
}
2009-01-07 11:57:36 +01:00
2008-08-06 16:50:06 +02:00
/**
2010-12-28 01:10:13 +01:00
* Show tab header of a card
2011-09-03 01:57:26 +02:00
*
2011-09-17 02:47:01 +02:00
* @ param array $links Array of tabs
2012-01-15 15:06:02 +01:00
* @ param string $active Active tab name ( document ', ' info ', ' ldap ' , .... )
2011-09-17 02:47:01 +02:00
* @ param string $title Title
* @ param int $notab 0 = Add tab header , 1 = no tab header
* @ param string $picto Add a picto on tab title
* @ return void
2008-08-06 16:50:06 +02:00
*/
2011-04-30 03:18:26 +02:00
function dol_fiche_head ( $links = array (), $active = '0' , $title = '' , $notab = 0 , $picto = '' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
print dol_get_fiche_head ( $links , $active , $title , $notab , $picto );
2011-07-08 15:07:44 +02:00
}
/**
* Show tab header of a card
2011-09-03 01:57:26 +02:00
*
2011-09-17 02:47:01 +02:00
* @ param array $links Array of tabs
* @ param int $active Active tab name
* @ param string $title Title
* @ param int $notab 0 = Add tab header , 1 = no tab header
* @ param string $picto Add a picto on tab title
* @ return void
2011-07-08 15:07:44 +02:00
*/
function dol_get_fiche_head ( $links = array (), $active = '0' , $title = '' , $notab = 0 , $picto = '' )
{
2012-08-03 23:29:08 +02:00
$out = " \n " . '<div class="tabs">' . " \n " ;
// Affichage titre
if ( ! empty ( $title ))
{
$limittitle = 30 ;
$out .= '<a class="tabTitle">' ;
if ( $picto ) $out .= img_object ( '' , $picto ) . ' ' ;
$out .= dol_trunc ( $title , $limittitle );
$out .= '</a>' ;
}
// Define max of key (max may be higher than sizeof because of hole due to module disabling some tabs).
$maxkey =- 1 ;
if ( is_array ( $links ) && ! empty ( $links ))
{
$keys = array_keys ( $links );
if ( count ( $keys )) $maxkey = max ( $keys );
}
// Show tabs
for ( $i = 0 ; $i <= $maxkey ; $i ++ )
{
if ( isset ( $links [ $i ][ 2 ]) && $links [ $i ][ 2 ] == 'image' )
{
if ( ! empty ( $links [ $i ][ 0 ]))
{
$out .= '<a class="tabimage" href="' . $links [ $i ][ 0 ] . '">' . $links [ $i ][ 1 ] . '</a>' . " \n " ;
}
else
{
$out .= '<span class="tabspan">' . $links [ $i ][ 1 ] . '</span>' . " \n " ;
}
}
else if ( ! empty ( $links [ $i ][ 1 ]))
{
//print "x $i $active ".$links[$i][2]." z";
if (( is_numeric ( $active ) && $i == $active )
|| ( ! is_numeric ( $active ) && $active == $links [ $i ][ 2 ]))
{
$out .= '<a id="active" class="tab" href="' . $links [ $i ][ 0 ] . '">' . $links [ $i ][ 1 ] . '</a>' . " \n " ;
}
else
{
2012-10-25 22:30:17 +02:00
$out .= '<a' . ( ! empty ( $links [ $i ][ 2 ]) ? ' id="' . $links [ $i ][ 2 ] . '"' : '' ) . ' class="tab" href="' . $links [ $i ][ 0 ] . '">' . $links [ $i ][ 1 ] . '</a>' . " \n " ;
2012-08-03 23:29:08 +02:00
}
}
}
$out .= " </div> \n " ;
if ( ! $notab ) $out .= " \n " . '<div class="tabBar">' . " \n " ;
return $out ;
2008-08-06 16:50:06 +02:00
}
2010-05-26 16:52:32 +02:00
/**
2011-07-08 15:07:44 +02:00
* Show tab footer of a card
2011-09-03 01:57:26 +02:00
*
2011-09-17 02:47:01 +02:00
* @ param int $notab 0 = Add tab footer , 1 = no tab footer
2011-09-20 13:43:14 +02:00
* @ return void
2010-05-26 16:52:32 +02:00
*/
function dol_fiche_end ( $notab = 0 )
{
2012-08-03 23:29:08 +02:00
print dol_get_fiche_end ( $notab );
2011-07-08 15:07:44 +02:00
}
/**
* Return tab footer of a card
2011-09-03 01:57:26 +02:00
*
2011-09-17 02:47:01 +02:00
* @ param int $notab 0 = Add tab footer , 1 = no tab footer
2011-09-20 13:43:14 +02:00
* @ return void
2011-07-08 15:07:44 +02:00
*/
function dol_get_fiche_end ( $notab = 0 )
{
2012-08-03 23:29:08 +02:00
if ( ! $notab ) return " \n </div> \n " ;
else return '' ;
2010-05-26 16:52:32 +02:00
}
2011-10-12 19:24:27 +02:00
/**
* Return a formated address ( part address / zip / town / state ) according to country rules
*
2011-12-07 18:10:24 +01:00
* @ param Object $object A company or contact object
* @ return string Formated string
2011-10-12 19:24:27 +02:00
*/
2011-12-07 18:10:24 +01:00
function dol_format_address ( $object )
2011-10-12 19:24:27 +02:00
{
$ret = '' ;
2012-09-12 17:39:02 +02:00
$countriesusingstate = array ( 'US' , 'IN' , 'GB' );
2011-10-12 19:24:27 +02:00
// Address
2011-12-07 18:10:24 +01:00
$ret .= $object -> address ;
2011-10-12 19:24:27 +02:00
// Zip/Town/State
2012-09-12 17:39:02 +02:00
if ( in_array ( $object -> country_code , array ( 'US' ))) // US: title firstname name \n address lines \n town, state, zip \n country
2011-10-12 19:24:27 +02:00
{
2011-12-07 18:10:24 +01:00
$ret .= ( $ret ? " \n " : '' ) . $object -> town ;
2011-10-12 19:24:27 +02:00
if ( $object -> state && in_array ( $object -> country_code , $countriesusingstate ))
{
2012-10-31 01:45:48 +01:00
$ret .= " , " . $object -> state ;
2011-10-12 19:24:27 +02:00
}
2011-12-07 18:10:24 +01:00
if ( $object -> zip ) $ret .= ', ' . $object -> zip ;
2011-10-12 19:24:27 +02:00
}
2012-09-12 17:39:02 +02:00
else if ( in_array ( $object -> country_code , array ( 'GB' ))) // UK: title firstname name \n address lines \n town state \n zip \n country
{
2012-09-15 09:02:20 +02:00
$ret .= ( $ret ? " \n " : '' ) . $object -> town ;
if ( $object -> state && in_array ( $object -> country_code , $countriesusingstate ))
{
2012-10-31 01:45:48 +01:00
$ret .= " , " . $object -> state ;
2012-09-15 09:02:20 +02:00
}
if ( $object -> zip ) $ret .= ( $ret ? " \n " : '' ) . $object -> zip ;
2012-09-12 17:39:02 +02:00
}
else // Other: title firstname name \n address lines \n zip town \n country
2011-10-12 19:24:27 +02:00
{
2011-12-07 18:10:24 +01:00
$ret .= ( $ret ? " \n " : '' ) . $object -> zip ;
$ret .= ' ' . $object -> town ;
2011-10-12 19:24:27 +02:00
if ( $object -> state && in_array ( $object -> country_code , $countriesusingstate ))
{
2011-12-07 18:10:24 +01:00
$ret .= " , " . $object -> state ;
2011-10-12 19:24:27 +02:00
}
}
return $ret ;
}
2008-08-06 16:50:06 +02:00
/**
2010-11-21 17:11:52 +01:00
* Output date in a string format according to outputlangs ( or langs if not defined ) .
2012-01-22 02:12:11 +01:00
* Return charset is always UTF - 8 , except if encodetoouput is defined . In this case charset is output charset
2011-09-03 01:57:26 +02:00
*
2012-01-22 02:12:11 +01:00
* @ param timestamp $time GM Timestamps date
2011-10-03 18:10:50 +02:00
* @ param string $format Output date format
* " %d %b %Y " ,
* " %d/%m/%Y %H:%M " ,
* " %d/%m/%Y %H:%M:%S " ,
2012-07-12 17:07:56 +02:00
* " day " , " daytext " , " dayhour " , " dayhourldap " , " dayhourtext " , " dayrfc " , " dayhourrfc "
2011-10-03 18:10:50 +02:00
* @ param string $tzoutput true = output or 'gmt' => string is for Greenwich location
* false or 'tzserver' => output string is for local PHP server TZ usage
* 'tzuser' => output string is for local browser TZ usage
* @ param Tranlsate $outputlangs Object lang that contains language for text translation .
* @ param boolean $encodetooutput false = no convert into output pagecode
* @ return string Formated date or '' if time is null
*
2011-02-02 16:51:18 +01:00
* @ see dol_mktime , dol_stringtotime , dol_getdate
2008-08-06 16:50:06 +02:00
*/
2010-12-08 14:13:17 +01:00
function dol_print_date ( $time , $format = '' , $tzoutput = 'tzserver' , $outputlangs = '' , $encodetooutput = false )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
$to_gmt = false ;
$offsettz = $offsetdst = 0 ;
if ( $tzoutput )
{
$to_gmt = true ; // For backward compatibility
if ( is_string ( $tzoutput ))
{
if ( $tzoutput == 'tzserver' )
{
$to_gmt = false ;
$offsettz = $offsetdst = 0 ;
}
elseif ( $tzoutput == 'tzuser' )
{
$to_gmt = true ;
$offsettz = ( empty ( $_SESSION [ 'dol_tz' ]) ? 0 : $_SESSION [ 'dol_tz' ]) * 60 * 60 ;
$offsetdst = ( empty ( $_SESSION [ 'dol_dst' ]) ? 0 : $_SESSION [ 'dol_dst' ]) * 60 * 60 ;
}
elseif ( $tzoutput == 'tzcompany' )
{
$to_gmt = false ;
$offsettz = $offsetdst = 0 ; // TODO Define this and use it later
}
}
}
if ( ! is_object ( $outputlangs )) $outputlangs = $langs ;
// Si format non defini, on prend $conf->format_date_text_short sinon %Y-%m-%d %H:%M:%S
if ( ! $format ) $format = ( isset ( $conf -> format_date_text_short ) ? $conf -> format_date_text_short : '%Y-%m-%d %H:%M:%S' );
// Change predefined format into computer format. If found translation in lang file we use it, otherwise we use default.
if ( $format == 'day' ) $format = ( $outputlangs -> trans ( " FormatDateShort " ) != " FormatDateShort " ? $outputlangs -> trans ( " FormatDateShort " ) : $conf -> format_date_short );
if ( $format == 'hour' ) $format = ( $outputlangs -> trans ( " FormatHourShort " ) != " FormatHourShort " ? $outputlangs -> trans ( " FormatHourShort " ) : $conf -> format_hour_short );
if ( $format == 'hourduration' ) $format = ( $outputlangs -> trans ( " FormatHourShortDuration " ) != " FormatHourShortDuration " ? $outputlangs -> trans ( " FormatHourShortDuration " ) : $conf -> format_hour_short_duration );
if ( $format == 'daytext' ) $format = ( $outputlangs -> trans ( " FormatDateText " ) != " FormatDateText " ? $outputlangs -> trans ( " FormatDateText " ) : $conf -> format_date_text );
if ( $format == 'daytextshort' ) $format = ( $outputlangs -> trans ( " FormatDateTextShort " ) != " FormatDateTextShort " ? $outputlangs -> trans ( " FormatDateTextShort " ) : $conf -> format_date_text_short );
if ( $format == 'dayhour' ) $format = ( $outputlangs -> trans ( " FormatDateHourShort " ) != " FormatDateHourShort " ? $outputlangs -> trans ( " FormatDateHourShort " ) : $conf -> format_date_hour_short );
if ( $format == 'dayhourtext' ) $format = ( $outputlangs -> trans ( " FormatDateHourText " ) != " FormatDateHourText " ? $outputlangs -> trans ( " FormatDateHourText " ) : $conf -> format_date_hour_text );
if ( $format == 'dayhourtextshort' ) $format = ( $outputlangs -> trans ( " FormatDateHourTextShort " ) != " FormatDateHourTextShort " ? $outputlangs -> trans ( " FormatDateHourTextShort " ) : $conf -> format_date_hour_text_short );
// Format not sensitive to language
if ( $format == 'dayhourlog' ) $format = '%Y%m%d%H%M%S' ;
if ( $format == 'dayhourldap' ) $format = '%Y%m%d%H%M%SZ' ;
if ( $format == 'dayhourxcard' ) $format = '%Y%m%dT%H%M%SZ' ;
if ( $format == 'dayxcard' ) $format = '%Y%m%d' ;
if ( $format == 'dayrfc' ) $format = '%Y-%m-%d' ; // DATE_RFC3339
if ( $format == 'dayhourrfc' ) $format = '%Y-%m-%dT%H:%M:%SZ' ; // DATETIME RFC3339
// If date undefined or "", we return ""
if ( dol_strlen ( $time ) == 0 ) return '' ; // $time=0 allowed (it means 01/01/1970 00:00:00)
//print 'x'.$time;
if ( preg_match ( '/%b/i' , $format )) // There is some text to translate
{
// We inhibate translation to text made by strftime functions. We will use trans instead later.
$format = str_replace ( '%b' , '__b__' , $format );
$format = str_replace ( '%B' , '__B__' , $format );
}
if ( preg_match ( '/%a/i' , $format )) // There is some text to translate
{
// We inhibate translation to text made by strftime functions. We will use trans instead later.
$format = str_replace ( '%a' , '__a__' , $format );
$format = str_replace ( '%A' , '__A__' , $format );
}
// Analyze date (deprecated) Ex: 1970-01-01, 1970-01-01 01:00:00, 19700101010000
if ( preg_match ( '/^([0-9]+)\-([0-9]+)\-([0-9]+) ?([0-9]+)?:?([0-9]+)?:?([0-9]+)?/i' , $time , $reg )
|| 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 ))
{
// This part of code should not be used.
dol_syslog ( " Functions.lib::dol_print_date function call with deprecated value of time in page " . $_SERVER [ " PHP_SELF " ], LOG_WARNING );
// Date has format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS' or 'YYYYMMDDHHMMSS'
$syear = ( ! empty ( $reg [ 1 ]) ? $reg [ 1 ] : '' );
$smonth = ( ! empty ( $reg [ 2 ]) ? $reg [ 2 ] : '' );
$sday = ( ! empty ( $reg [ 3 ]) ? $reg [ 3 ] : '' );
$shour = ( ! empty ( $reg [ 4 ]) ? $reg [ 4 ] : '' );
$smin = ( ! empty ( $reg [ 5 ]) ? $reg [ 5 ] : '' );
$ssec = ( ! empty ( $reg [ 6 ]) ? $reg [ 6 ] : '' );
$time = dol_mktime ( $shour , $smin , $ssec , $smonth , $sday , $syear , true );
$ret = adodb_strftime ( $format , $time + $offsettz + $offsetdst , $to_gmt );
}
else
{
// Date is a timestamps
if ( $time < 100000000000 ) // Protection against bad date values
{
$ret = adodb_strftime ( $format , $time + $offsettz + $offsetdst , $to_gmt );
}
else $ret = 'Bad value ' . $time . ' for date' ;
}
if ( preg_match ( '/__b__/i' , $format ))
{
// Here ret is string in PHP setup language (strftime was used). Now we convert to $outputlangs.
$month = adodb_strftime ( '%m' , $time + $offsettz + $offsetdst );
if ( $encodetooutput )
{
$monthtext = $outputlangs -> transnoentities ( 'Month' . $month );
$monthtextshort = $outputlangs -> transnoentities ( 'MonthShort' . $month );
}
else
{
$monthtext = $outputlangs -> transnoentitiesnoconv ( 'Month' . $month );
$monthtextshort = $outputlangs -> transnoentitiesnoconv ( 'MonthShort' . $month );
}
//print 'monthtext='.$monthtext.' monthtextshort='.$monthtextshort;
$ret = str_replace ( '__b__' , $monthtextshort , $ret );
$ret = str_replace ( '__B__' , $monthtext , $ret );
//print 'x'.$outputlangs->charset_output.'-'.$ret.'x';
//return $ret;
}
if ( preg_match ( '/__a__/i' , $format ))
{
$w = adodb_strftime ( '%w' , $time + $offsettz + $offsetdst );
$dayweek = $outputlangs -> transnoentitiesnoconv ( 'Day' . $w );
$ret = str_replace ( '__A__' , $dayweek , $ret );
$ret = str_replace ( '__a__' , dol_substr ( $dayweek , 0 , 3 ), $ret );
}
return $ret ;
2008-08-06 16:50:06 +02:00
}
/**
2012-01-22 02:12:11 +01:00
* Return an array with locale date info .
2011-09-03 01:57:26 +02:00
* PHP getdate is restricted to the years 1901 - 2038 on Unix and 1970 - 2038 on Windows
2012-01-22 02:12:11 +01:00
* WARNING : This function always use PHP server timezone to return locale informations .
* Usage must be avoid .
*
2012-04-02 18:37:37 +02:00
* @ param timestamp $timestamp Timestamp
* @ param boolean $fast Fast mode
2011-10-03 18:10:50 +02:00
* @ return array Array of informations
* If no fast mode :
* 'seconds' => $secs ,
* 'minutes' => $min ,
* 'hours' => $hour ,
* 'mday' => $day ,
* 'wday' => $dow ,
* 'mon' => $month ,
* 'year' => $year ,
* 'yday' => floor ( $secsInYear / $_day_power ),
* 'weekday' => gmdate ( 'l' , $_day_power * ( 3 + $dow )),
* 'month' => gmdate ( 'F' , mktime ( 0 , 0 , 0 , $month , 2 , 1971 )),
* If fast mode :
* 'seconds' => $secs ,
* 'minutes' => $min ,
* 'hours' => $hour ,
* 'mday' => $day ,
* 'mon' => $month ,
* 'year' => $year ,
* 'yday' => floor ( $secsInYear / $_day_power ),
* 'leap' => $leaf ,
* 'ndays' => $ndays
2012-01-22 02:12:11 +01:00
* @ see dol_print_date , dol_stringtotime , dol_mktime
2008-08-06 16:50:06 +02:00
*/
2009-01-07 11:57:36 +01:00
function dol_getdate ( $timestamp , $fast = false )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
$usealternatemethod = false ;
if ( $timestamp <= 0 ) $usealternatemethod = true ; // <= 1970
if ( $timestamp >= 2145913200 ) $usealternatemethod = true ; // >= 2038
if ( $usealternatemethod )
{
$arrayinfo = adodb_getdate ( $timestamp , $fast );
}
else
{
$arrayinfo = getdate ( $timestamp );
}
return $arrayinfo ;
2008-08-06 16:50:06 +02:00
}
/**
2010-02-03 03:22:15 +01:00
* Return a timestamp date built from detailed informations ( by default a local PHP server timestamp )
2009-01-04 23:09:02 +01:00
* Replace function mktime not available under Windows if year < 1970
2008-10-11 00:27:11 +02:00
* PHP mktime is restricted to the years 1901 - 2038 on Unix and 1970 - 2038 on Windows
2011-09-03 01:57:26 +02:00
*
2012-01-22 02:12:11 +01:00
* @ param int $hour Hour ( can be - 1 for undefined )
* @ param int $minute Minute ( can be - 1 for undefined )
* @ param int $second Second ( can be - 1 for undefined )
* @ param int $month Month ( 1 to 12 )
* @ param int $day Day ( 1 to 31 )
* @ param int $year Year
* @ param int $gm 1 = Input informations are GMT values , otherwise local to server TZ
* @ param int $check 0 = No check on parameters ( Can use day 32 , etc ... )
* @ return timestamp Date as a timestamp , '' if error
* @ see dol_print_date , dol_stringtotime , dol_getdate
2008-08-06 16:50:06 +02:00
*/
2012-02-05 15:13:31 +01:00
function dol_mktime ( $hour , $minute , $second , $month , $day , $year , $gm = false , $check = 1 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf ;
//print "- ".$hour.",".$minute.",".$second.",".$month.",".$day.",".$year.",".$_SERVER["WINDIR"]." -";
// Clean parameters
2012-09-27 13:25:31 +02:00
if ( $hour == - 1 || empty ( $hour )) $hour = 0 ;
if ( $minute == - 1 || empty ( $minute )) $minute = 0 ;
if ( $second == - 1 || empty ( $second )) $second = 0 ;
2012-08-03 23:29:08 +02:00
// Check parameters
if ( $check )
{
if ( ! $month || ! $day ) return '' ;
if ( $day > 31 ) return '' ;
if ( $month > 12 ) return '' ;
if ( $hour < 0 || $hour > 24 ) return '' ;
if ( $minute < 0 || $minute > 60 ) return '' ;
if ( $second < 0 || $second > 60 ) return '' ;
}
if ( method_exists ( 'DateTime' , 'getTimestamp' ) && empty ( $conf -> global -> MAIN_OLD_DATE ))
{
if ( empty ( $gm )) $localtz = new DateTimeZone ( date_default_timezone_get ());
else $localtz = new DateTimeZone ( 'UTC' );
$dt = new DateTime ( null , $localtz );
$dt -> setDate ( $year , $month , $day );
$dt -> setTime (( int ) $hour , ( int ) $minute , ( int ) $second );
$date = $dt -> getTimestamp ();
}
else
{
$usealternatemethod = false ;
if ( $year <= 1970 ) $usealternatemethod = true ; // <= 1970
if ( $year >= 2038 ) $usealternatemethod = true ; // >= 2038
if ( $usealternatemethod || $gm ) // Si time gm, seule adodb peut convertir
{
$date = adodb_mktime ( $hour , $minute , $second , $month , $day , $year , 0 , $gm );
}
else
{
$date = mktime ( $hour , $minute , $second , $month , $day , $year );
}
}
return $date ;
2008-08-06 16:50:06 +02:00
}
2012-02-06 05:31:19 +01:00
/**
* Return date for now . We should always use this function without parameters ( that means GMT time )
*
* @ param string $mode 'gmt' => we return GMT timestamp ,
* 'tzserver' => we add the PHP server timezone
* 'tzref' => we add the company timezone
* 'tzuser' => we add the user timezone
* @ return timestamp $date Timestamp
*/
function dol_now ( $mode = 'gmt' )
{
2012-08-03 23:29:08 +02:00
// Note that gmmktime and mktime return same value (GMT) whithout parameters
2012-03-23 18:19:50 +01:00
//if ($mode == 'gmt') $ret=gmmktime(); // Strict Standards: gmmktime(): You should be using the time() function instead
2012-08-03 23:29:08 +02:00
if ( $mode == 'gmt' ) $ret = time (); // Time for now at greenwich.
else if ( $mode == 'tzserver' ) // Time for now with PHP server timezone added
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
2012-08-03 23:29:08 +02:00
$tzsecond = getServerTimeZoneInt ( 'now' ); // Contains tz+dayling saving time
$ret = dol_now ( 'gmt' ) + ( $tzsecond * 3600 );
}
/* else if ( $mode == 'tzref' ) // Time for now with parent company timezone is added
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
2012-08-03 23:29:08 +02:00
$tzsecond = getParentCompanyTimeZoneInt (); // Contains tz+dayling saving time
$ret = dol_now ( 'gmt' ) + ( $tzsecond * 3600 );
} */
else if ( $mode == 'tzuser' ) // Time for now with user timezone is added
{
//print 'eeee'.time().'-'.mktime().'-'.gmmktime();
$offsettz = ( empty ( $_SESSION [ 'dol_tz' ]) ? 0 : $_SESSION [ 'dol_tz' ]) * 60 * 60 ;
$offsetdst = ( empty ( $_SESSION [ 'dol_dst' ]) ? 0 : $_SESSION [ 'dol_dst' ]) * 60 * 60 ;
$ret = dol_now ( 'gmt' ) + ( $offsettz + $offsetdst );
}
return $ret ;
2012-02-06 05:31:19 +01:00
}
2009-01-07 13:39:40 +01:00
/**
2011-02-02 16:51:18 +01:00
* Return string with formated size
2011-09-03 01:57:26 +02:00
*
2011-09-17 02:47:01 +02:00
* @ param int $size Size to print
* @ param int $shortvalue Tell if we want long value to use another unit ( Ex : 1.5 Kb instead of 1500 b )
* @ param int $shortunit Use short value of size unit
* @ return string Link
2009-01-07 13:39:40 +01:00
*/
2009-10-04 17:52:16 +02:00
function dol_print_size ( $size , $shortvalue = 0 , $shortunit = 0 )
2009-01-07 13:39:40 +01:00
{
2012-08-03 23:29:08 +02:00
global $langs ;
$level = 1024 ;
// Set value text
if ( empty ( $shortvalue ) || $size < ( $level * 10 ))
{
$ret = $size ;
$textunitshort = $langs -> trans ( " b " );
$textunitlong = $langs -> trans ( " Bytes " );
}
else
{
$ret = round ( $size / $level , 0 );
$textunitshort = $langs -> trans ( " Kb " );
$textunitlong = $langs -> trans ( " KiloBytes " );
}
// Use long or short text unit
if ( empty ( $shortunit )) { $ret .= ' ' . $textunitlong ; }
else { $ret .= ' ' . $textunitshort ; }
return $ret ;
2009-01-07 13:39:40 +01:00
}
/**
2011-07-04 10:38:51 +02:00
* Show Url link
2011-09-03 01:57:26 +02:00
*
2011-09-17 02:47:01 +02:00
* @ param string $url Url to show
* @ param string $target Target for link
* @ param int $max Max number of characters to show
* @ return string HTML Link
2009-01-07 13:39:40 +01:00
*/
function dol_print_url ( $url , $target = '_blank' , $max = 32 )
{
2012-08-03 23:29:08 +02:00
if ( empty ( $url )) return '' ;
$link = '<a href="' ;
if ( ! preg_match ( '/^http/i' , $url )) $link .= 'http://' ;
$link .= $url ;
if ( $target ) $link .= '" target="' . $target . '">' ;
if ( ! preg_match ( '/^http/i' , $url )) $link .= 'http://' ;
$link .= dol_trunc ( $url , $max );
$link .= '</a>' ;
return $link ;
2009-01-07 13:39:40 +01:00
}
/**
2011-07-04 10:38:51 +02:00
* Show EMail link
2011-09-03 01:57:26 +02:00
*
2011-09-17 02:47:01 +02:00
* @ param string $email EMail to show ( only email , without 'Name of recipient' before )
* @ param int $cid Id of contact if known
* @ param int $socid Id of third party if known
* @ param int $addlink 0 = no link to create action
* @ param int $max Max number of characters to show
* @ param int $showinvalid Show warning if syntax email is wrong
* @ return string HTML Link
2009-01-07 13:39:40 +01:00
*/
2009-08-11 19:58:25 +02:00
function dol_print_email ( $email , $cid = 0 , $socid = 0 , $addlink = 0 , $max = 64 , $showinvalid = 1 )
2009-01-07 13:39:40 +01:00
{
2012-08-03 23:29:08 +02:00
global $conf , $user , $langs ;
$newemail = $email ;
if ( empty ( $email )) return ' ' ;
if ( ! empty ( $addlink ))
{
$newemail = '<a href="' ;
if ( ! preg_match ( '/^mailto:/i' , $email )) $newemail .= 'mailto:' ;
$newemail .= $email ;
$newemail .= '">' ;
$newemail .= dol_trunc ( $email , $max );
$newemail .= '</a>' ;
if ( $showinvalid && ! isValidEmail ( $email ))
{
$langs -> load ( " errors " );
$newemail .= img_warning ( $langs -> trans ( " ErrorBadEMail " , $email ));
}
2012-09-15 11:21:22 +02:00
if (( $cid || $socid ) && ! empty ( $conf -> agenda -> enabled ) && $user -> rights -> agenda -> myactions -> create )
2012-08-03 23:29:08 +02:00
{
$type = 'AC_EMAIL' ; $link = '' ;
if ( ! empty ( $conf -> global -> AGENDA_ADDACTIONFOREMAIL )) $link = '<a href="' . DOL_URL_ROOT . '/comm/action/fiche.php?action=create&backtopage=1&actioncode=' . $type . '&contactid=' . $cid . '&socid=' . $socid . '">' . img_object ( $langs -> trans ( " AddAction " ), " calendar " ) . '</a>' ;
$newemail = '<table class="nobordernopadding"><tr><td>' . $newemail . ' </td><td> ' . $link . '</td></tr></table>' ;
}
}
else
{
if ( $showinvalid && ! isValidEmail ( $email ))
{
$langs -> load ( " errors " );
$newemail .= img_warning ( $langs -> trans ( " ErrorBadEMail " , $email ));
}
}
return $newemail ;
2009-01-07 13:39:40 +01:00
}
2008-08-06 16:50:06 +02:00
/**
2010-11-07 12:27:22 +01:00
* Format phone numbers according to country
2011-09-03 01:57:26 +02:00
*
2011-10-03 18:10:50 +02:00
* @ param string $phone Phone number to format
* @ param string $country Country code to use for formatting
* @ param int $cid Id of contact if known
* @ param int $socid Id of third party if known
* @ param int $addlink 0 = no link to create action
* @ param string $separ separation between numbers for a better visibility example : xx . xx . xx . xx . xx
* @ return string Formated phone number
2008-08-06 16:50:06 +02:00
*/
2009-01-07 13:39:40 +01:00
function dol_print_phone ( $phone , $country = " FR " , $cid = 0 , $socid = 0 , $addlink = 0 , $separ = " " )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $user , $langs ;
// Clean phone parameter
$phone = preg_replace ( " /[ \ s.-]/ " , " " , trim ( $phone ));
if ( empty ( $phone )) { return '' ; }
$newphone = $phone ;
if ( strtoupper ( $country ) == " FR " )
{
// France
if ( dol_strlen ( $phone ) == 10 ) {
$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 );
}
elseif ( dol_strlen ( $newphone ) == 7 )
{
$newphone = substr ( $newphone , 0 , 3 ) . $separ . substr ( $newphone , 3 , 2 ) . $separ . substr ( $newphone , 5 , 2 );
}
elseif ( dol_strlen ( $newphone ) == 9 )
{
$newphone = substr ( $newphone , 0 , 2 ) . $separ . substr ( $newphone , 2 , 3 ) . $separ . substr ( $newphone , 5 , 2 ) . $separ . substr ( $newphone , 7 , 2 );
}
elseif ( dol_strlen ( $newphone ) == 11 )
{
$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 );
}
elseif ( dol_strlen ( $newphone ) == 12 )
{
$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 );
}
}
if ( ! empty ( $addlink ))
{
if ( ! empty ( $conf -> clicktodial -> enabled ) && $addlink == 'AC_TEL' )
{
if ( empty ( $user -> clicktodial_loaded )) $user -> fetch_clicktodial ();
if ( empty ( $conf -> global -> CLICKTODIAL_URL )) $urlmask = 'ErrorClickToDialModuleNotConfigured' ;
else $urlmask = $conf -> global -> CLICKTODIAL_URL ;
$clicktodial_poste = ( ! empty ( $user -> clicktodial_poste ) ? urlencode ( $user -> clicktodial_poste ) : '' );
$clicktodial_login = ( ! empty ( $user -> clicktodial_login ) ? urlencode ( $user -> clicktodial_login ) : '' );
$clicktodial_password = ( ! empty ( $user -> clicktodial_password ) ? urlencode ( $user -> clicktodial_password ) : '' );
// This line is for backward compatibility
$url = sprintf ( $urlmask , urlencode ( $phone ), $clicktodial_poste , $clicktodial_login , $clicktodial_password );
// Thoose lines are for substitution
$substitarray = array ( '__PHONEFROM__' => $clicktodial_poste ,
'__PHONETO__' => urlencode ( $phone ),
'__LOGIN__' => $clicktodial_login ,
'__PASS__' => $clicktodial_password );
$url = make_substitutions ( $url , $substitarray );
$newphonesav = $newphone ;
$newphone = '<a href="' . $url . '"' ;
if ( ! empty ( $conf -> global -> CLICKTODIAL_FORCENEWTARGET )) $newphone .= ' target="_blank"' ;
$newphone .= '>' . $newphonesav . '</a>' ;
}
2012-09-15 11:21:22 +02:00
//if (($cid || $socid) && ! empty($conf->agenda->enabled) && $user->rights->agenda->myactions->create)
2012-08-03 23:29:08 +02:00
if ( ! empty ( $conf -> agenda -> enabled ) && $user -> rights -> agenda -> myactions -> create )
{
$type = 'AC_TEL' ; $link = '' ;
if ( $addlink == 'AC_FAX' ) $type = 'AC_FAX' ;
if ( ! empty ( $conf -> global -> AGENDA_ADDACTIONFORPHONE )) $link = '<a href="' . DOL_URL_ROOT . '/comm/action/fiche.php?action=create&backtopage=1&actioncode=' . $type . ( $cid ? '&contactid=' . $cid : '' ) . ( $socid ? '&socid=' . $socid : '' ) . '">' . img_object ( $langs -> trans ( " AddAction " ), " calendar " ) . '</a>' ;
$newphone = '<table class="nobordernopadding"><tr><td>' . $newphone . ' </td><td> ' . $link . '</td></tr></table>' ;
}
}
return $newphone ;
2008-08-06 16:50:06 +02:00
}
2009-10-14 13:04:09 +02:00
/**
2010-11-07 12:27:22 +01:00
* Return an IP formated to be shown on screen
2011-09-03 01:57:26 +02:00
*
2011-10-03 18:10:50 +02:00
* @ param string $ip IP
2012-01-15 02:39:43 +01:00
* @ param int $mode 0 = return IP + country / flag , 1 = return only country / flag , 2 = return only IP
2011-10-03 18:10:50 +02:00
* @ return string Formated IP , with country if GeoIP module is enabled
2009-10-14 13:04:09 +02:00
*/
function dol_print_ip ( $ip , $mode = 0 )
2009-10-14 00:01:27 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
$ret = '' ;
if ( empty ( $mode )) $ret .= $ip ;
if ( ! empty ( $conf -> geoipmaxmind -> enabled ) && $mode != 2 )
{
$datafile = $conf -> global -> GEOIPMAXMIND_COUNTRY_DATAFILE ;
//$ip='24.24.24.24';
//$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)
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/dolgeoip.class.php' ;
2012-08-03 23:29:08 +02:00
$geoip = new DolGeoIP ( 'country' , $datafile );
//print 'ip='.$ip.' databaseType='.$geoip->gi->databaseType." GEOIP_CITY_EDITION_REV1=".GEOIP_CITY_EDITION_REV1."\n";
//print "geoip_country_id_by_addr=".geoip_country_id_by_addr($geoip->gi,$ip)."\n";
$countrycode = $geoip -> getCountryCodeFromIP ( $ip );
if ( $countrycode ) // If success, countrycode is us, fr, ...
{
if ( file_exists ( DOL_DOCUMENT_ROOT . '/theme/common/flags/' . $countrycode . '.png' ))
{
$ret .= ' ' . img_picto ( $countrycode . ' ' . $langs -> trans ( " AccordingToGeoIPDatabase " ), DOL_URL_ROOT . '/theme/common/flags/' . $countrycode . '.png' , '' , 1 );
}
else $ret .= ' (' . $countrycode . ')' ;
}
}
return $ret ;
2011-05-12 21:01:14 +02:00
}
2009-10-17 15:47:17 +02:00
2011-07-03 20:31:13 +02:00
/**
* Return country code for current user .
* If software is used inside a local network , detection may fails ( we need a public ip )
2011-09-03 01:57:26 +02:00
*
2011-10-03 18:10:50 +02:00
* @ return string Country code ( fr , es , it , us , ... )
2011-07-03 20:31:13 +02:00
*/
function dol_user_country ()
{
2012-08-03 23:29:08 +02:00
global $conf , $langs , $user ;
//$ret=$user->xxx;
$ret = '' ;
if ( ! empty ( $conf -> geoipmaxmind -> enabled ))
{
$ip = $_SERVER [ " REMOTE_ADDR " ];
$datafile = $conf -> global -> GEOIPMAXMIND_COUNTRY_DATAFILE ;
//$ip='24.24.24.24';
//$datafile='E:\Mes Sites\Web\Admin1\awstats\maxmind\GeoIP.dat';
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/dolgeoip.class.php' ;
2012-08-03 23:29:08 +02:00
$geoip = new DolGeoIP ( 'country' , $datafile );
$countrycode = $geoip -> getCountryCodeFromIP ( $ip );
$ret = $countrycode ;
}
return $ret ;
2011-07-03 20:31:13 +02:00
}
2011-05-12 21:01:14 +02:00
/**
* Format address string
2011-09-03 01:57:26 +02:00
*
2011-10-03 18:10:50 +02:00
* @ param string $address Address
2011-12-01 22:58:02 +01:00
* @ param int $htmlid Html ID ( for example 'gmap' )
2011-10-03 18:10:50 +02:00
* @ param int $mode thirdparty | contact | member | other
* @ param int $id Id of object
* @ return void
2011-05-12 21:01:14 +02:00
*/
2011-12-01 22:58:02 +01:00
function dol_print_address ( $address , $htmlid , $mode , $id )
2011-05-12 21:01:14 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $user , $langs ;
if ( $address )
{
print nl2br ( $address );
$showgmap = $showomap = 0 ;
if ( $mode == 'thirdparty' && ! empty ( $conf -> google -> enabled ) && ! empty ( $conf -> global -> GOOGLE_ENABLE_GMAPS )) $showgmap = 1 ;
if ( $mode == 'contact' && ! empty ( $conf -> google -> enabled ) && ! empty ( $conf -> global -> GOOGLE_ENABLE_GMAPS_CONTACTS )) $showgmap = 1 ;
if ( $mode == 'member' && ! empty ( $conf -> google -> enabled ) && ! empty ( $conf -> global -> GOOGLE_ENABLE_GMAPS_MEMBERS )) $showgmap = 1 ;
if ( $mode == 'thirdparty' && ! empty ( $conf -> openstreetmap -> enabled ) && ! empty ( $conf -> global -> OPENSTREETMAP_ENABLE_MAPS )) $showomap = 1 ;
if ( $mode == 'contact' && ! empty ( $conf -> openstreetmap -> enabled ) && ! empty ( $conf -> global -> OPENSTREETMAP_ENABLE_MAPS_CONTACTS )) $showomap = 1 ;
if ( $mode == 'member' && ! empty ( $conf -> openstreetmap -> enabled ) && ! empty ( $conf -> global -> OPENSTREETMAP_ENABLE_MAPS_MEMBERS )) $showomap = 1 ;
// TODO Add a hook here
if ( $showgmap )
{
$url = dol_buildpath ( '/google/gmaps.php?mode=' . $mode . '&id=' . $id , 1 );
print ' <a href="' . $url . '" target="_gmaps"><img id="' . $htmlid . '" border="0" src="' . DOL_URL_ROOT . '/theme/common/gmap.png"></a>' ;
}
if ( $showomap )
{
$url = dol_buildpath ( '/openstreetmap/maps.php?mode=' . $mode . '&id=' . $id , 1 );
print ' <a href="' . $url . '" target="_gmaps"><img id="' . $htmlid . '_openstreetmap" border="0" src="' . DOL_URL_ROOT . '/theme/common/gmap.png"></a>' ;
}
}
2009-10-14 00:01:27 +02:00
}
2011-05-12 21:01:14 +02:00
2009-08-11 19:58:25 +02:00
/**
2011-09-03 01:57:26 +02:00
* Return true if email syntax is ok
*
* @ param string $address email ( Ex : " toto@titi.com " , " John Do <johndo@titi.com> " )
* @ return boolean true if email syntax is OK , false if KO or empty string
2009-08-11 19:58:25 +02:00
*/
function isValidEmail ( $address )
{
2012-08-03 23:29:08 +02:00
if ( preg_match ( " /.*<(.+)>/i " , $address , $regs )) {
$address = $regs [ 1 ];
}
// 2 letters domains extensions are for countries
// 3 letters domains extensions: biz|com|edu|gov|int|mil|net|org|pro|...
if ( preg_match ( " /^[^@ \ s \t ]+@([a-zA-Z0-9 \ -]+ \ .)+([a-zA-Z0-9 \ -] { 2,3}|asso|aero|coop|info|name) \$ /i " , $address ))
{
return true ;
}
else
{
return false ;
}
2009-08-11 19:58:25 +02:00
}
2011-03-05 17:27:26 +01:00
/**
2011-09-03 01:57:26 +02:00
* Return true if phone number syntax is ok
*
2012-03-18 19:23:01 +01:00
* @ param string $phone phone ( Ex : " 0601010101 " )
* @ return boolean true if phone syntax is OK , false if KO or empty string
2011-03-05 17:27:26 +01:00
*/
2012-02-19 18:34:22 +01:00
function isValidPhone ( $phone )
2011-03-05 17:27:26 +01:00
{
2012-08-03 23:29:08 +02:00
return true ;
2011-03-05 17:27:26 +01:00
}
2008-08-06 16:50:06 +02:00
2008-11-17 00:43:10 +01:00
/**
2011-09-03 01:57:26 +02:00
* Make a strlen call . Works even if mbstring module not enabled
*
* @ param string $string String to calculate length
* @ param string $stringencoding Encoding of string
* @ return int Length of string
2008-11-17 00:43:10 +01:00
*/
2010-08-24 23:29:47 +02:00
function dol_strlen ( $string , $stringencoding = 'UTF-8' )
2008-11-17 00:43:10 +01:00
{
2012-08-03 23:29:08 +02:00
if ( function_exists ( 'mb_strlen' )) return mb_strlen ( $string , $stringencoding );
else return strlen ( $string );
2008-11-17 00:43:10 +01:00
}
/**
2011-10-03 18:10:50 +02:00
* Make a substring . Works even in mbstring module is not enabled .
2011-09-03 01:57:26 +02:00
*
2011-10-03 18:10:50 +02:00
* @ param string $string String to scan
* @ param string $start Start position
* @ param int $length Length
* @ param string $stringencoding Page code used for input string encoding
* @ return string substring
2008-11-17 00:43:10 +01:00
*/
function dol_substr ( $string , $start , $length , $stringencoding = '' )
{
2012-08-03 23:29:08 +02:00
global $langs ;
if ( empty ( $stringencoding )) $stringencoding = $langs -> charset_output ;
$ret = '' ;
if ( function_exists ( 'mb_substr' ))
{
$ret = mb_substr ( $string , $start , $length , $stringencoding );
}
else
{
$ret = substr ( $string , $start , $length );
}
return $ret ;
2008-11-17 00:43:10 +01:00
}
2009-01-07 11:57:36 +01:00
2011-05-13 20:08:55 +02:00
/**
2012-02-18 12:54:23 +01:00
* Show a javascript graph .
* Do not use this function anymore . Use DolGraph class instead .
2011-11-27 02:23:55 +01:00
*
2011-11-20 15:25:08 +01:00
* @ param string $htmlid Html id name
* @ param int $width Width in pixel
* @ param int $height Height in pixel
* @ param array $data Data array
* @ param int $showlegend 1 to show legend , 0 otherwise
* @ param string $type Type of graph ( 'pie' , 'barline' )
* @ param int $showpercent Show percent ( with type = 'pie' only )
* @ param string $url Param to add an url to click values
* @ return void
2012-02-18 12:54:23 +01:00
* @ deprecated
2011-05-13 20:08:55 +02:00
*/
2011-06-07 00:34:10 +02:00
function dol_print_graph ( $htmlid , $width , $height , $data , $showlegend = 0 , $type = 'pie' , $showpercent = 0 , $url = '' )
2011-05-13 20:08:55 +02:00
{
2011-11-20 15:25:08 +01:00
global $conf , $langs ;
global $theme_datacolor ; // To have var kept when function is called several times
if ( empty ( $conf -> use_javascript_ajax )) return ;
$jsgraphlib = 'flot' ;
$datacolor = array ();
2011-11-27 02:23:55 +01:00
2011-05-16 15:59:46 +02:00
// Load colors of theme into $datacolor array
2011-11-20 15:25:08 +01:00
$color_file = DOL_DOCUMENT_ROOT . " /theme/ " . $conf -> theme . " /graph-color.php " ;
if ( is_readable ( $color_file ))
{
2012-08-23 02:04:35 +02:00
include_once $color_file ;
2011-11-20 15:25:08 +01:00
if ( isset ( $theme_datacolor ))
{
$datacolor = array ();
foreach ( $theme_datacolor as $val )
{
$datacolor [] = " # " . sprintf ( " %02x " , $val [ 0 ]) . sprintf ( " %02x " , $val [ 1 ]) . sprintf ( " %02x " , $val [ 2 ]);
}
}
}
print '<div id="' . $htmlid . '" style="width:' . $width . 'px;height:' . $height . 'px;"></div>' ;
2011-11-27 02:23:55 +01:00
2011-11-20 15:25:08 +01:00
// We use Flot js lib
if ( $jsgraphlib == 'flot' )
{
if ( $type == 'pie' )
{
// data is array('series'=>array(serie1,serie2,...),
// 'seriestype'=>array('bar','line',...),
// 'seriescolor'=>array(0=>'#999999',1=>'#999999',...)
// 'xlabel'=>array(0=>labelx1,1=>labelx2,...));
// serieX is array('label'=>'label', data=>val)
print '
< script type = " text/javascript " >
$ ( function () {
var data = '.json_encode($data[' series ']).' ;
2011-11-27 02:23:55 +01:00
2011-11-20 15:25:08 +01:00
function plotWithOptions () {
$ . plot ( $ ( " #'. $htmlid .' " ), data ,
{
series : {
pie : {
show : true ,
radius : 3 / 4 ,
label : {
show : true ,
radius : 3 / 4 ,
formatter : function ( label , series ) {
var percent = Math . round ( series . percent );
var number = series . data [ 0 ][ 1 ];
return \ '' ;
print '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">' ;
if ( $url ) print '<a style="color: #FFFFFF;" border="0" href="' . $url . '=">' ;
print '\'+' . ( $showlegend ? 'number' : 'label+\'<br/>\'+number' );
if ( ! empty ( $showpercent )) print '+\'<br/>\'+percent+\'%\'' ;
print '+\'' ;
if ( $url ) print '</a>' ;
print ' </ div > \ ' ;
},
background : {
opacity : 0.5 ,
color : \ ' #000000\'
}
}
}
},
zoom : {
interactive : true
},
pan : {
interactive : true
}, ' ;
if ( count ( $datacolor ))
{
print 'colors: ' . ( ! empty ( $data [ 'seriescolor' ]) ? json_encode ( $data [ 'seriescolor' ]) : json_encode ( $datacolor )) . ',' ;
}
print 'legend: {show: ' . ( $showlegend ? 'true' : 'false' ) . ' , position : \ ' ne\ ' }
});
}
plotWithOptions ();
});
</ script > ' ;
}
else if ( $type == 'barline' )
{
// data is array('series'=>array(serie1,serie2,...),
// 'seriestype'=>array('bar','line',...),
// 'seriescolor'=>array(0=>'#999999',1=>'#999999',...)
// 'xlabel'=>array(0=>labelx1,1=>labelx2,...));
2011-11-20 16:54:41 +01:00
// serieX is array('label'=>'label', data=>array(0=>y1,1=>y2,...)) with same nb of value than into xlabel
2011-11-20 15:25:08 +01:00
print '
< script type = " text/javascript " >
$ ( function () {
var data = [ ' ;
2011-11-20 16:34:30 +01:00
$i = 0 ; $outputserie = 0 ;
2011-11-20 15:25:08 +01:00
foreach ( $data [ 'series' ] as $serie )
{
2011-11-20 16:34:30 +01:00
if ( $data [ 'seriestype' ][ $i ] == 'line' ) { $i ++ ; continue ; };
2011-11-20 15:25:08 +01:00
if ( $outputserie > 0 ) print ',' ;
2011-11-20 16:34:30 +01:00
print '{ bars: { stack: 0, show: true, barWidth: 0.9, align: \'center\' }, label: \'' . dol_escape_js ( $serie [ 'label' ]) . '\', data: ' . json_encode ( $serie [ 'data' ]) . '}' . " \n " ;
2011-11-20 15:25:08 +01:00
$outputserie ++ ; $i ++ ;
}
if ( $outputserie ) print ', ' ;
//print '];
//var datalines = [';
2011-11-20 16:34:30 +01:00
$i = 0 ; $outputserie = 0 ;
2011-11-20 15:25:08 +01:00
foreach ( $data [ 'series' ] as $serie )
{
2011-11-20 16:34:30 +01:00
if ( empty ( $data [ 'seriestype' ][ $i ]) || $data [ 'seriestype' ][ $i ] == 'bar' ) { $i ++ ; continue ; };
2011-11-20 15:25:08 +01:00
if ( $outputserie > 0 ) print ',' ;
2011-11-20 16:34:30 +01:00
print '{ lines: { show: true }, label: \'' . dol_escape_js ( $serie [ 'label' ]) . '\', data: ' . json_encode ( $serie [ 'data' ]) . '}' . " \n " ;
2011-11-20 15:25:08 +01:00
$outputserie ++ ; $i ++ ;
}
print ' ];
2011-11-20 16:34:30 +01:00
var dataticks = '.json_encode($data[' xlabel ']).'
2011-11-27 02:23:55 +01:00
2011-11-20 15:25:08 +01:00
function plotWithOptions () {
$ . plot ( jQuery ( " #'. $htmlid .' " ), data ,
{
series : {
stack : 0
},
zoom : {
interactive : true
},
pan : {
interactive : true
}, ' ;
if ( count ( $datacolor ))
{
print 'colors: ' . json_encode ( $datacolor ) . ',' ;
}
print 'legend: {show: ' . ( $showlegend ? 'true' : 'false' ) . ' },
xaxis : { ticks : dataticks }
});
}
plotWithOptions ();
});
</ script > ' ;
}
else print 'BadValueForPArameterType' ;
}
2011-05-13 20:08:55 +02:00
}
2008-08-06 16:50:06 +02:00
/**
2011-05-13 20:08:55 +02:00
* Truncate a string to a particular length adding '...' if string larger than length .
* If length = max length + 1 , we do no truncate to avoid having just 1 char replaced with '...' .
* MAIN_DISABLE_TRUNC = 1 can disable all truncings
2011-08-17 17:56:22 +02:00
*
2011-12-01 22:58:02 +01:00
* @ param string $string String to truncate
2011-12-17 16:44:48 +01:00
* @ param int $size Max string size visible . 0 for no limit . finale string size can be 1 more ( if size was max + 1 ) or 3 more ( if we added ... )
* @ param string $trunc Where to trunc : right , left , middle ( size must be a 2 power ), wrap
2011-12-01 22:58:02 +01:00
* @ param string $stringencoding Tell what is source string encoding
2011-12-17 16:44:48 +01:00
* @ param int $nodot Truncation do not add ... after truncation . So it ' s an exact truncation .
2011-12-01 22:58:02 +01:00
* @ return string Truncated string
2008-08-06 16:50:06 +02:00
*/
2011-12-17 16:44:48 +01:00
function dol_trunc ( $string , $size = 40 , $trunc = 'right' , $stringencoding = 'UTF-8' , $nodot = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf ;
if ( $size == 0 || ! empty ( $conf -> global -> MAIN_DISABLE_TRUNC )) return $string ;
// We go always here
if ( $trunc == 'right' )
{
$newstring = dol_textishtml ( $string ) ? dol_string_nohtmltag ( $string , 1 ) : $string ;
if ( dol_strlen ( $newstring , $stringencoding ) > ( $size + ( $nodot ? 0 : 1 )))
return dol_substr ( $newstring , 0 , $size , $stringencoding ) . ( $nodot ? '' : '...' );
else
return $string ;
}
elseif ( $trunc == 'middle' )
{
$newstring = dol_textishtml ( $string ) ? dol_string_nohtmltag ( $string , 1 ) : $string ;
if ( dol_strlen ( $newstring , $stringencoding ) > 2 && dol_strlen ( $newstring , $stringencoding ) > ( $size + 1 ))
{
$size1 = round ( $size / 2 );
$size2 = round ( $size / 2 );
return dol_substr ( $newstring , 0 , $size1 , $stringencoding ) . '...' . dol_substr ( $newstring , dol_strlen ( $newstring , $stringencoding ) - $size2 , $size2 , $stringencoding );
}
else
return $string ;
}
elseif ( $trunc == 'left' )
{
$newstring = dol_textishtml ( $string ) ? dol_string_nohtmltag ( $string , 1 ) : $string ;
if ( dol_strlen ( $newstring , $stringencoding ) > ( $size + 1 ))
return '...' . dol_substr ( $newstring , dol_strlen ( $newstring , $stringencoding ) - $size , $size , $stringencoding );
else
return $string ;
}
elseif ( $trunc == 'wrap' )
{
$newstring = dol_textishtml ( $string ) ? dol_string_nohtmltag ( $string , 1 ) : $string ;
if ( dol_strlen ( $newstring , $stringencoding ) > ( $size + 1 ))
return dol_substr ( $newstring , 0 , $size , $stringencoding ) . " \n " . dol_trunc ( dol_substr ( $newstring , $size , dol_strlen ( $newstring , $stringencoding ) - $size , $stringencoding ), $size , $trunc );
else
return $string ;
}
else return 'BadParam3CallingDolTrunc' ;
2008-08-06 16:50:06 +02:00
}
/**
2011-07-18 05:49:10 +02:00
* Show picto whatever it ' s its name ( generic function )
2011-08-17 17:56:22 +02:00
*
2011-09-12 19:08:02 +02:00
* @ param string $alt Text on alt and title of image
* @ param string $picto Name of image file to show ( 'filenew' , ... )
* If no extension provided , we use '.png' . Image must be stored into theme / xxx / img directory .
* Example : picto . png if picto . png is stored into htdocs / theme / mytheme / img
* Example : picto . png @ mymodule if picto . png is stored into htdocs / mymodule / img
* Example : / mydir / mysubdir / picto . png if picto . png is stored into htdocs / mydir / mysubdir ( pictoisfullpath must be set to 1 )
* @ param string $options Add more attribute on img tag ( For example 'style="float: right"' )
* @ param int $pictoisfullpath If 1 , image path is a full path
* @ return string Return img tag
* @ see #img_object, #img_picto_common
2008-08-06 16:50:06 +02:00
*/
2012-08-04 02:15:39 +02:00
function img_picto ( $alt , $picto , $options = '' , $pictoisfullpath = false )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf ;
// Define fullpathpicto to use into src
2012-08-04 14:18:02 +02:00
if ( $pictoisfullpath ) {
2012-08-04 08:45:38 +02:00
// Clean parameters
if ( ! preg_match ( '/(\.png|\.gif)$/i' , $picto ))
$picto .= '.png' ;
$fullpathpicto = $picto ;
}
2012-08-03 23:29:08 +02:00
else
{
// By default, we search into theme directory
$url = DOL_URL_ROOT ;
$path = 'theme/' . $conf -> theme ;
2012-08-04 08:45:38 +02:00
if ( ! empty ( $conf -> global -> MAIN_FORCETHEMEDIR ))
$path = preg_replace ( '/^\//' , '' , $conf -> global -> MAIN_FORCETHEMEDIR ) . '/' . $path ;
2012-08-03 23:29:08 +02:00
// If we ask an image into module/img (not into a theme path)
2012-08-04 02:15:39 +02:00
if ( preg_match ( '/^([^@]+)@([^@]+)$/i' , $picto , $regs ))
{
$picto = $regs [ 1 ];
$path = $regs [ 2 ];
}
2012-08-04 08:45:38 +02:00
// Clean parameters
if ( ! preg_match ( '/(\.png|\.gif)$/i' , $picto ))
$picto .= '.png' ;
2012-08-03 23:29:08 +02:00
// If img file not into standard path, we use alternate path
2012-08-04 08:45:38 +02:00
if ( defined ( 'DOL_URL_ROOT_ALT' ) && DOL_URL_ROOT_ALT && ! file_exists ( DOL_DOCUMENT_ROOT . '/' . $path . '/img/' . $picto ))
$url = DOL_URL_ROOT_ALT ;
2012-08-03 23:29:08 +02:00
2012-08-04 02:15:39 +02:00
$fullpathpicto = $url . '/' . $path . '/img/' . $picto ;
2012-08-03 23:29:08 +02:00
}
2012-08-04 14:18:02 +02:00
return '<img src="' . $fullpathpicto . '" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '"' . ( ! empty ( $options ) ? ' ' . $options : '' ) . '>' ;
2008-08-06 16:50:06 +02:00
}
2012-08-04 02:15:39 +02:00
/**
* Show a picto called object_picto ( generic function )
*
* @ param string $alt Text of alt on image
* @ param string $picto Name of image to show object_picto ( example : user , group , action , bill , contract , propal , product , ... )
* For external modules use imagename @ mymodule to search into directory " img " of module .
* @ param string $options Add more attribute on img tag ( ie : class = " datecallink " )
* @ param int $pictoisfullpath If 1 , image path is a full path
* @ return string Return img tag
* @ see #img_picto, #img_picto_common
*/
function img_object ( $alt , $picto , $options = '' , $pictoisfullpath = false )
{
return img_picto ( $alt , 'object_' . $picto , $options , $pictoisfullpath );
}
2008-09-02 02:27:05 +02:00
/**
2010-11-13 17:59:53 +01:00
* Show picto ( generic function )
2011-08-17 17:56:22 +02:00
*
2011-09-12 19:08:02 +02:00
* @ param string $alt Text on alt and title of image
* @ param string $picto Name of image file to show ( If no extension provided , we use '.png' ) . Image must be stored into htdocs / theme / common directory .
* @ param string $options Add more attribute on img tag
* @ param int $pictoisfullpath If 1 , image path is a full path
* @ return string Return img tag
* @ see #img_object, #img_picto
2008-09-02 02:27:05 +02:00
*/
2012-07-30 23:23:58 +02:00
function img_picto_common ( $alt , $picto , $options = '' , $pictoisfullpath = 0 )
2008-09-02 02:27:05 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf ;
2012-07-30 23:23:58 +02:00
2012-08-03 23:29:08 +02:00
if ( ! preg_match ( '/(\.png|\.gif)$/i' , $picto )) $picto .= '.png' ;
2012-07-30 23:23:58 +02:00
2012-08-03 23:29:08 +02:00
if ( $pictoisfullpath ) $path = $picto ;
else
{
$path = DOL_URL_ROOT . '/theme/common/' . $picto ;
2012-08-04 08:45:38 +02:00
2012-08-03 23:29:08 +02:00
if ( ! empty ( $conf -> global -> MAIN_MODULE_CAN_OVERWRITE_COMMONICONS ))
{
$themepath = DOL_DOCUMENT_ROOT . '/theme/' . $conf -> theme . '/img/' . $picto ;
2012-07-30 23:23:58 +02:00
2012-08-04 09:56:25 +02:00
if ( file_exists ( $themepath )) $path = $themepath ;
2012-08-03 23:29:08 +02:00
}
}
2012-07-30 23:23:58 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , $path , $options , 1 );
2008-09-02 02:27:05 +02:00
}
2008-08-06 16:50:06 +02:00
/**
2011-05-03 21:03:08 +02:00
* Show logo action
2011-08-17 17:56:22 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Text for image alt and title ( 'default' , ... )
2011-12-01 22:58:02 +01:00
* @ param int $numaction Action to show
* @ return string Return an img tag
2008-08-06 16:50:06 +02:00
*/
2012-02-04 15:20:32 +01:00
function img_action ( $alt , $numaction )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
if ( $alt == 'default' )
{
if ( $numaction == - 1 ) $alt = $langs -> transnoentitiesnoconv ( 'ChangeDoNotContact' );
if ( $numaction == 0 ) $alt = $langs -> transnoentitiesnoconv ( 'ChangeNeverContacted' );
if ( $numaction == 1 ) $alt = $langs -> transnoentitiesnoconv ( 'ChangeToContact' );
if ( $numaction == 2 ) $alt = $langs -> transnoentitiesnoconv ( 'ChangeContactInProcess' );
if ( $numaction == 3 ) $alt = $langs -> transnoentitiesnoconv ( 'ChangeContactDone' );
}
return img_picto ( $alt , 'stcomm' . $numaction . '.png' );
2008-08-06 16:50:06 +02:00
}
/**
2011-08-17 17:56:22 +02:00
* Show pdf logo
*
2011-12-01 22:58:02 +01:00
* @ param string $alt Texte sur le alt de l ' image
* @ param int $size Taille de l ' icone : 3 = 16 x16px , 2 = 14 x14px
* @ return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_pdf ( $alt = 'default' , $size = 3 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Show' );
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , 'pdf' . $size . '.png' );
2008-08-06 16:50:06 +02:00
}
/**
2011-08-17 17:56:22 +02:00
* Show logo +
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Texte sur le alt de l ' image
* @ return string Return tag img
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_edit_add ( $alt = 'default' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Add' );
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , 'edit_add.png' );
2008-08-06 16:50:06 +02:00
}
/**
2011-08-17 17:56:22 +02:00
* Show logo -
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Texte sur le alt de l ' image
* @ return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_edit_remove ( $alt = 'default' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Remove' );
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , 'edit_remove.png' );
2008-08-06 16:50:06 +02:00
}
/**
2011-08-17 17:56:22 +02:00
* Show logo editer / modifier fiche
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Texte sur le alt de l ' image
* @ param float $float Si il faut y mettre le style " float: right "
* @ param string $other Add more attributes on img
* @ return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_edit ( $alt = 'default' , $float = 0 , $other = '' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Modify' );
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , 'edit.png' , ( $float ? 'style="float: right"' : $other ));
2008-08-06 16:50:06 +02:00
}
2008-10-03 01:08:14 +02:00
/**
2011-08-17 17:56:22 +02:00
* Show logo view card
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Texte sur le alt de l ' image
* @ param float $float Si il faut y mettre le style " float: right "
* @ param string $other Add more attributes on img
* @ return string Retourne tag img
2008-10-03 01:08:14 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_view ( $alt = 'default' , $float = 0 , $other = '' )
2008-10-03 01:08:14 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'View' );
2012-07-30 23:27:12 +02:00
2012-08-03 23:29:08 +02:00
$options = ( $float ? 'style="float: right" ' : '' ) . $other ;
2012-08-04 08:45:38 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , 'view.png' , $options );
2008-10-03 01:08:14 +02:00
}
2008-08-06 16:50:06 +02:00
/**
2011-03-09 00:50:27 +01:00
* Show delete logo
2011-08-17 17:56:22 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Text on alt image
* @ param string $other Add more attributes on img
* @ return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_delete ( $alt = 'default' , $other = '' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Delete' );
2008-08-06 16:50:06 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , 'delete.png' , $other );
2012-07-30 22:36:43 +02:00
}
2008-08-06 16:50:06 +02:00
/**
2011-03-09 00:50:27 +01:00
* Show help logo with cursor " ? "
2011-08-17 17:56:22 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $usehelpcursor Use help cursor
* @ param string $usealttitle Text to use as alt title
* @ return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
2012-07-30 23:23:58 +02:00
function img_help ( $usehelpcursor = 1 , $usealttitle = 1 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 23:23:58 +02:00
2012-08-03 23:29:08 +02:00
if ( $usealttitle )
{
2012-09-16 15:04:55 +02:00
if ( is_string ( $usealttitle )) $usealttitle = dol_escape_htmltag ( $usealttitle );
else $usealttitle = $langs -> trans ( 'Info' );
2012-08-03 23:29:08 +02:00
}
2012-07-30 23:23:58 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $usealttitle , 'info.png' , ( $usehelpcursor ? 'style="cursor: help"' : '' ));
2008-08-06 16:50:06 +02:00
}
/**
2012-07-31 14:41:15 +02:00
* Show info logo
2011-08-17 17:56:22 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Text to show on alt image
* @ return string Return img tag
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_info ( $alt = 'default' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Informations' );
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , 'info.png' );
2008-08-06 16:50:06 +02:00
}
/**
2012-07-31 14:41:15 +02:00
* Show warning logo
2011-08-17 17:56:22 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Text to show on alt image
* @ param int $float If we must add style " float: right "
* @ return string Return img tag
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_warning ( $alt = 'default' , $float = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Warning' );
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , 'warning.png' , ( $float ? 'style="float: right"' : '' ));
2008-08-06 16:50:06 +02:00
}
/**
2012-07-31 14:41:15 +02:00
* Show error logo
2011-08-17 17:56:22 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Text to show on alt image
* @ return string Return img tag
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_error ( $alt = 'default' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Error' );
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , 'error.png' );
2008-08-06 16:50:06 +02:00
}
/**
2012-07-31 14:41:15 +02:00
* Show next logo
2011-08-17 17:56:22 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Text to show on alt image
* @ return string Return img tag
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_next ( $alt = 'default' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2008-08-06 16:50:06 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Next' );
2008-08-06 16:50:06 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , 'next.png' );
2008-08-06 16:50:06 +02:00
}
/**
2012-07-31 14:41:15 +02:00
* Show previous logo
2011-08-17 17:56:22 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Text to show on alt image
* @ return string Return img tag
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_previous ( $alt = 'default' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Previous' );
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , 'previous.png' );
2008-08-06 16:50:06 +02:00
}
/**
2012-07-31 14:41:15 +02:00
* Show down arrow logo
2011-08-17 17:56:22 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Text to show on alt image
* @ param int $selected Selected
* @ return string Return img tag
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_down ( $alt = 'default' , $selected = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Down' );
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , ( $selected ? '1downarrow_selected.png' : '1downarrow.png' ), 'class="imgdown"' );
2008-08-06 16:50:06 +02:00
}
/**
2012-07-31 14:41:15 +02:00
* Show top arrow logo
2011-08-17 17:56:22 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Text to show on alt image
* @ param int $selected Selected
* @ return string Return img tag
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_up ( $alt = 'default' , $selected = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Up' );
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , ( $selected ? '1uparrow_selected.png' : '1uparrow.png' ), 'class="imgup"' );
2008-08-06 16:50:06 +02:00
}
/**
2012-07-31 14:41:15 +02:00
* Show left arrow logo
2011-08-17 17:56:22 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Text to show on alt image
* @ param int $selected Selected
* @ return string Return img tag
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_left ( $alt = 'default' , $selected = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Left' );
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , ( $selected ? '1leftarrow_selected.png' : '1leftarrow.png' ));
2008-08-06 16:50:06 +02:00
}
/**
2012-07-31 14:41:15 +02:00
* Show right arrow logo
2011-08-17 17:56:22 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $alt Text to show on alt image
* @ param int $selected Selected
* @ return string Return img tag
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_right ( $alt = 'default' , $selected = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Right' );
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
return img_picto ( $alt , ( $selected ? '1rightarrow_selected.png' : '1rightarrow.png' ));
2008-08-06 16:50:06 +02:00
}
/**
2012-07-31 14:41:15 +02:00
* Show tick logo if allowed
2011-08-17 17:56:22 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $allow Allow
* @ param string $alt Text to show on alt image
* @ return string Return img tag
2008-08-06 16:50:06 +02:00
*/
2012-07-30 22:36:43 +02:00
function img_allow ( $allow , $alt = 'default' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
2011-05-12 21:01:14 +02:00
2012-08-03 23:29:08 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Active' );
2012-07-30 22:36:43 +02:00
2012-08-03 23:29:08 +02:00
if ( $allow == 1 ) return img_picto ( $alt , 'tick.png' );
2012-08-04 08:45:38 +02:00
2012-08-03 23:29:08 +02:00
return '-' ;
2011-05-12 21:01:14 +02:00
}
2008-08-06 16:50:06 +02:00
/**
2010-11-13 15:34:06 +01:00
* Show MIME img of a file
2012-02-12 16:50:58 +01:00
*
* @ param string $file Filename
* @ param string $alt Alternate text to show on img mous hover
* @ return string Return img tag
2008-08-06 16:50:06 +02:00
*/
2012-07-30 23:23:58 +02:00
function img_mime ( $file , $alt = '' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2010-11-20 14:08:44 +01:00
2012-08-03 23:29:08 +02:00
$mimetype = dol_mimetype ( $file , '' , 1 );
$mimeimg = dol_mimetype ( $file , '' , 2 );
2009-05-19 02:14:27 +02:00
2012-08-03 23:29:08 +02:00
if ( empty ( $alt )) $alt = 'Mime type: ' . $mimetype ;
2009-10-23 16:58:33 +02:00
2012-08-03 23:29:08 +02:00
return img_picto_common ( $alt , 'mime/' . $mimeimg );
2008-08-06 16:50:06 +02:00
}
2012-08-03 14:30:11 +02:00
/**
2012-08-03 13:10:15 +02:00
* Show phone logo .
* Use img_picto instead .
2012-08-03 14:30:11 +02:00
*
* @ param string $alt Text to show on alt image
* @ param int $option Option
2012-08-03 13:10:15 +02:00
* @ return string Return img tag
2012-08-03 14:30:11 +02:00
* @ deprecated
*/
function img_phone ( $alt = 'default' , $option = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 14:30:11 +02:00
global $conf , $langs ;
2010-11-20 14:08:44 +01:00
2012-08-03 14:30:11 +02:00
if ( $alt == 'default' ) $alt = $langs -> trans ( 'Call' );
2009-05-19 02:14:27 +02:00
2012-08-03 14:31:10 +02:00
if ( $option == 1 ) $img = 'call' ;
2012-08-03 14:30:11 +02:00
else $img = 'call_out' ;
2009-10-23 16:58:33 +02:00
2012-08-03 14:30:11 +02:00
return img_picto ( $alt , $img );
2008-08-06 16:50:06 +02:00
}
/**
2011-05-03 21:03:08 +02:00
* Show information for admin users
2012-02-12 16:50:58 +01:00
*
* @ param string $text Text info
* @ param string $infoonimgalt Info is shown only on alt of star picto , otherwise it is show on output after the star picto
2013-01-02 18:43:59 +01:00
* @ param int $nodiv No div
2012-02-12 16:50:58 +01:00
* @ return string String with info text
2008-08-06 16:50:06 +02:00
*/
2013-01-02 18:43:59 +01:00
function info_admin ( $text , $infoonimgalt = 0 , $nodiv = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
if ( $infoonimgalt )
{
return img_picto ( $text , 'star' );
}
2012-08-04 08:45:38 +02:00
2013-01-02 18:43:59 +01:00
return ( $nodiv ? '' : '<div class="info">' ) . img_picto ( $langs -> trans ( 'InfoAdmin' ), 'star' ) . ' ' . $text . ( $nodiv ? '' : '</div>' );
2008-08-06 16:50:06 +02:00
}
/**
2010-12-01 22:16:28 +01:00
* Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remontee des bugs .
* On doit appeler cette fonction quand une erreur technique bloquante est rencontree .
* Toutefois , il faut essayer de ne l 'appeler qu' au sein de pages php , les classes devant
* renvoyer leur erreur par l ' intermediaire de leur propriete " error " .
2012-02-04 14:39:47 +01:00
*
2012-07-17 01:22:19 +02:00
* @ param DoliDB $db Database handler
2012-02-04 14:39:47 +01:00
* @ param string $error String or array of errors strings to show
2012-07-17 01:22:19 +02:00
* @ return void
2012-02-04 14:39:47 +01:00
* @ see dol_htmloutput_errors
2008-08-06 16:50:06 +02:00
*/
2009-11-11 16:46:56 +01:00
function dol_print_error ( $db = '' , $error = '' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs , $argv ;
global $dolibarr_main_prod ;
$out = '' ;
$syslog = '' ;
// Si erreur intervenue avant chargement langue
if ( ! $langs )
{
require_once DOL_DOCUMENT_ROOT . '/core/class/translate.class.php' ;
$langs = new Translate ( '' , $conf );
$langs -> load ( " main " );
}
$langs -> load ( " main " );
$langs -> load ( " errors " );
if ( $_SERVER [ 'DOCUMENT_ROOT' ]) // Mode web
{
$out .= $langs -> trans ( " DolibarrHasDetectedError " ) . " .<br> \n " ;
if ( ! empty ( $conf -> global -> MAIN_FEATURES_LEVEL ))
$out .= " You use an experimental level of features, so please do NOT report any bugs, anywhere, until going back to MAIN_FEATURES_LEVEL = 0.<br> \n " ;
$out .= $langs -> trans ( " InformationToHelpDiagnose " ) . " :<br> \n " ;
$out .= " <b> " . $langs -> trans ( " Date " ) . " :</b> " . dol_print_date ( time (), 'dayhourlog' ) . " <br> \n " ;;
$out .= " <b> " . $langs -> trans ( " Dolibarr " ) . " :</b> " . DOL_VERSION . " <br> \n " ;;
if ( isset ( $conf -> global -> MAIN_FEATURES_LEVEL )) $out .= " <b> " . $langs -> trans ( " LevelOfFeature " ) . " :</b> " . $conf -> global -> MAIN_FEATURES_LEVEL . " <br> \n " ;;
if ( function_exists ( " phpversion " ))
{
$out .= " <b> " . $langs -> trans ( " PHP " ) . " :</b> " . phpversion () . " <br> \n " ;
//phpinfo(); // This is to show location of php.ini file
}
$out .= " <b> " . $langs -> trans ( " Server " ) . " :</b> " . $_SERVER [ " SERVER_SOFTWARE " ] . " <br> \n " ;;
$out .= " <br> \n " ;
$out .= " <b> " . $langs -> trans ( " RequestedUrl " ) . " :</b> " . $_SERVER [ " REQUEST_URI " ] . " <br> \n " ;;
$out .= " <b> " . $langs -> trans ( " Referer " ) . " :</b> " . ( isset ( $_SERVER [ " HTTP_REFERER " ]) ? $_SERVER [ " HTTP_REFERER " ] : '' ) . " <br> \n " ;;
$out .= " <b> " . $langs -> trans ( " MenuManager " ) . " :</b> " . $conf -> top_menu . " <br> \n " ;
$out .= " <br> \n " ;
$syslog .= " url= " . $_SERVER [ " REQUEST_URI " ];
$syslog .= " , query_string= " . $_SERVER [ " QUERY_STRING " ];
}
else // Mode CLI
{
$out .= '> ' . $langs -> transnoentities ( " ErrorInternalErrorDetected " ) . " : \n " . $argv [ 0 ] . " \n " ;
$syslog .= " pid= " . getmypid ();
}
if ( is_object ( $db ))
{
if ( $_SERVER [ 'DOCUMENT_ROOT' ]) // Mode web
{
$out .= " <b> " . $langs -> trans ( " DatabaseTypeManager " ) . " :</b> " . $db -> type . " <br> \n " ;
$out .= " <b> " . $langs -> trans ( " RequestLastAccessInError " ) . " :</b> " . ( $db -> lastqueryerror () ? $db -> lastqueryerror () : $langs -> trans ( " ErrorNoRequestInError " )) . " <br> \n " ;
$out .= " <b> " . $langs -> trans ( " ReturnCodeLastAccessInError " ) . " :</b> " . ( $db -> lasterrno () ? $db -> lasterrno () : $langs -> trans ( " ErrorNoRequestInError " )) . " <br> \n " ;
$out .= " <b> " . $langs -> trans ( " InformationLastAccessInError " ) . " :</b> " . ( $db -> lasterror () ? $db -> lasterror () : $langs -> trans ( " ErrorNoRequestInError " )) . " <br> \n " ;
$out .= " <br> \n " ;
}
else // Mode CLI
{
$out .= '> ' . $langs -> transnoentities ( " DatabaseTypeManager " ) . " : \n " . $db -> type . " \n " ;
$out .= '> ' . $langs -> transnoentities ( " RequestLastAccessInError " ) . " : \n " . ( $db -> lastqueryerror () ? $db -> lastqueryerror () : $langs -> trans ( " ErrorNoRequestInError " )) . " \n " ;
$out .= '> ' . $langs -> transnoentities ( " ReturnCodeLastAccessInError " ) . " : \n " . ( $db -> lasterrno () ? $db -> lasterrno () : $langs -> trans ( " ErrorNoRequestInError " )) . " \n " ;
$out .= '> ' . $langs -> transnoentities ( " InformationLastAccessInError " ) . " : \n " . ( $db -> lasterror () ? $db -> lasterror () : $langs -> trans ( " ErrorNoRequestInError " )) . " \n " ;
}
$syslog .= " , sql= " . $db -> lastquery ();
$syslog .= " , db_error= " . $db -> lasterror ();
}
if ( $error )
{
$langs -> load ( " errors " );
if ( is_array ( $error )) $errors = $error ;
else $errors = array ( $error );
foreach ( $errors as $msg )
{
$msg = $langs -> trans ( $msg );
if ( $_SERVER [ 'DOCUMENT_ROOT' ]) // Mode web
{
$out .= " <b> " . $langs -> trans ( " Message " ) . " :</b> " . $msg . " <br> \n " ;
}
else // Mode CLI
{
$out .= '> ' . $langs -> transnoentities ( " Message " ) . " : \n " . $msg . " \n " ;
}
$syslog .= " , msg= " . $msg ;
}
}
if ( empty ( $dolibarr_main_prod ) && $_SERVER [ 'DOCUMENT_ROOT' ] && function_exists ( 'xdebug_print_function_stack' ) && function_exists ( 'xdebug_call_file' ))
{
xdebug_print_function_stack ();
$out .= '<b>XDebug informations:</b>' . " <br> \n " ;
$out .= 'File: ' . xdebug_call_file () . " <br> \n " ;
$out .= 'Line: ' . xdebug_call_line () . " <br> \n " ;
$out .= 'Function: ' . xdebug_call_function () . " <br> \n " ;
$out .= " <br> \n " ;
}
if ( empty ( $dolibarr_main_prod )) print $out ;
else define ( " MAIN_CORE_ERROR " , 1 );
//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.';
dol_syslog ( " Error " . $syslog , LOG_ERR );
2008-08-06 16:50:06 +02:00
}
2009-10-10 17:32:28 +02:00
/**
2012-11-20 11:23:34 +01:00
* Show a public email and error code to contact if technical error
2012-02-04 14:39:47 +01:00
*
2012-12-02 12:59:06 +01:00
* @ param string $prefixcode Prefix of public error code
2012-02-04 15:20:32 +01:00
* @ return void
2009-10-10 17:32:28 +02:00
*/
2012-11-20 11:23:34 +01:00
function dol_print_error_email ( $prefixcode )
2009-10-10 17:32:28 +02:00
{
2012-08-03 23:29:08 +02:00
global $langs , $conf ;
2009-10-10 17:32:28 +02:00
2012-08-03 23:29:08 +02:00
$langs -> load ( " errors " );
$now = dol_now ();
2012-11-20 11:23:34 +01:00
print '<br><div class="error">' . $langs -> trans ( " ErrorContactEMail " , $conf -> global -> MAIN_INFO_SOCIETE_MAIL , $prefixcode . dol_print_date ( $now , '%Y%m%d' )) . '</div>' ;
2009-10-10 17:32:28 +02:00
}
2008-08-06 16:50:06 +02:00
/**
2011-05-03 21:03:08 +02:00
* Show title line of an array
2011-11-02 14:14:46 +01:00
*
2012-02-04 14:39:47 +01:00
* @ param string $name Label of field
* @ param string $file Url used when we click on sort picto
* @ param string $field Field to use for new sorting
* @ param string $begin ( " " by defaut )
* @ param string $moreparam Add more parameters on sort url links ( " " by default )
* @ param string $td Options of attribute td ( " " by defaut )
* @ param string $sortfield Current field used to sort
* @ param string $sortorder Current sort order
* @ return void
2011-07-06 11:25:05 +02:00
*/
2011-07-06 23:12:33 +02:00
function print_liste_field_titre ( $name , $file = " " , $field = " " , $begin = " " , $moreparam = " " , $td = " " , $sortfield = " " , $sortorder = " " )
2011-08-30 19:56:45 +02:00
{
print getTitleFieldOfList ( $name , 0 , $file , $field , $begin , $moreparam , $td , $sortfield , $sortorder );
}
/**
* Get title line of an array
2011-09-02 23:56:37 +02:00
*
2012-02-04 14:39:47 +01:00
* @ param string $name Label of field
2012-02-19 18:34:22 +01:00
* @ param int $thead For thead format
2012-02-04 14:39:47 +01:00
* @ param string $file Url used when we click on sort picto
2012-09-16 15:04:55 +02:00
* @ param string $field Field to use for new sorting . Empty if this field is not sortable .
2012-02-04 14:39:47 +01:00
* @ param string $begin ( " " by defaut )
* @ param string $moreparam Add more parameters on sort url links ( " " by default )
* @ param string $moreattrib Add more attributes on th ( " " by defaut )
* @ param string $sortfield Current field used to sort
* @ param string $sortorder Current sort order
* @ return void
2011-08-30 19:56:45 +02:00
*/
2011-09-02 23:56:37 +02:00
function getTitleFieldOfList ( $name , $thead = 0 , $file = " " , $field = " " , $begin = " " , $moreparam = " " , $moreattrib = " " , $sortfield = " " , $sortorder = " " )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf ;
//print "$name, $file, $field, $begin, $options, $moreattrib, $sortfield, $sortorder<br>\n";
2011-09-03 01:57:26 +02:00
2012-08-03 23:29:08 +02:00
$out = '' ;
2011-09-02 23:56:37 +02:00
// If field is used as sort criteria we use a specific class
2012-08-03 23:29:08 +02:00
// Example if (sortfield,field)=("nom","xxx.nom") or (sortfield,field)=("nom","nom")
if ( $field && ( $sortfield == $field || $sortfield == preg_replace ( " /^[^ \ .]+ \ ./ " , " " , $field )))
{
$out .= '<th class="liste_titre_sel" ' . $moreattrib . '>' ;
}
else
{
$out .= '<th class="liste_titre" ' . $moreattrib . '>' ;
}
$out .= $name ;
if ( empty ( $thead ) && $field ) // If this is a sort field
{
$options = preg_replace ( '/sortfield=([a-zA-Z0-9,\s\.]+)/i' , '' , $moreparam );
$options = preg_replace ( '/sortorder=([a-zA-Z0-9,\s\.]+)/i' , '' , $options );
$options = preg_replace ( '/&+/i' , '&' , $options );
if ( ! preg_match ( '/^&/' , $options )) $options = '&' . $options ;
//print " ";
$out .= '<img width="2" src="' . DOL_URL_ROOT . '/theme/common/transparent.png" alt="">' ;
if ( ! $sortorder )
{
$out .= '<a href="' . $file . '?sortfield=' . $field . '&sortorder=asc&begin=' . $begin . $options . '">' . img_down ( " A-Z " , 0 ) . '</a>' ;
$out .= '<a href="' . $file . '?sortfield=' . $field . '&sortorder=desc&begin=' . $begin . $options . '">' . img_up ( " Z-A " , 0 ) . '</a>' ;
}
else
{
if ( $field != $sortfield )
{
$out .= '<a href="' . $file . '?sortfield=' . $field . '&sortorder=asc&begin=' . $begin . $options . '">' . img_down ( " A-Z " , 0 ) . '</a>' ;
$out .= '<a href="' . $file . '?sortfield=' . $field . '&sortorder=desc&begin=' . $begin . $options . '">' . img_up ( " Z-A " , 0 ) . '</a>' ;
}
else {
$sortorder = strtoupper ( $sortorder );
if ( $sortorder == 'DESC' ) {
$out .= '<a href="' . $file . '?sortfield=' . $field . '&sortorder=asc&begin=' . $begin . $options . '">' . img_down ( " A-Z " , 0 ) . '</a>' ;
$out .= '<a href="' . $file . '?sortfield=' . $field . '&sortorder=desc&begin=' . $begin . $options . '">' . img_up ( " Z-A " , 1 ) . '</a>' ;
}
if ( $sortorder == 'ASC' ) {
$out .= '<a href="' . $file . '?sortfield=' . $field . '&sortorder=asc&begin=' . $begin . $options . '">' . img_down ( " A-Z " , 1 ) . '</a>' ;
$out .= '<a href="' . $file . '?sortfield=' . $field . '&sortorder=desc&begin=' . $begin . $options . '">' . img_up ( " Z-A " , 0 ) . '</a>' ;
}
}
}
}
$out .= '</th>' ;
return $out ;
2008-08-06 16:50:06 +02:00
}
/**
2010-12-05 21:33:24 +01:00
* Show a title ( deprecated . use print_fiche_titre instrad )
2012-02-04 14:39:47 +01:00
*
* @ param string $title Title to show
* @ return string Title to show
2008-08-06 16:50:06 +02:00
*/
2012-02-04 14:39:47 +01:00
function print_titre ( $title )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
print '<div class="titre">' . $title . '</div>' ;
2008-08-06 16:50:06 +02:00
}
2010-05-02 09:34:08 +02:00
/**
2010-12-05 21:33:24 +01:00
* Show a title with picto
2012-02-04 15:20:32 +01:00
*
* @ param string $titre Title to show
* @ param string $mesg Added message to show on right
* @ param string $picto Icon to use before title ( should be a 32 x32 transparent png file )
* @ param int $pictoisfullpath 1 = Icon name is a full absolute url of image
* @ param int $id To force an id on html objects
* @ return void
2010-05-02 09:34:08 +02:00
*/
function print_fiche_titre ( $titre , $mesg = '' , $picto = 'title.png' , $pictoisfullpath = 0 , $id = '' )
{
2012-08-03 23:29:08 +02:00
print load_fiche_titre ( $titre , $mesg , $picto , $pictoisfullpath , $id );
2010-05-02 09:34:08 +02:00
}
2008-08-06 16:50:06 +02:00
/**
2010-12-05 21:33:24 +01:00
* Load a title with picto
2011-09-02 23:56:37 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $titre Title to show
* @ param string $mesg Added message to show on right
* @ param string $picto Icon to use before title ( should be a 32 x32 transparent png file )
2012-03-17 14:04:16 +01:00
* @ param int $pictoisfullpath 1 = Icon name is a full absolute url of image
2012-02-04 15:20:32 +01:00
* @ param int $id To force an id on html objects
* @ return void
2008-08-06 16:50:06 +02:00
*/
2010-05-02 06:57:15 +02:00
function load_fiche_titre ( $titre , $mesg = '' , $picto = 'title.png' , $pictoisfullpath = 0 , $id = '' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf ;
$return = '' ;
if ( $picto == 'setup' ) $picto = 'title.png' ;
if ( ! empty ( $conf -> browser -> ie ) && $picto == 'title.png' ) $picto = 'title.gif' ;
$return .= " \n " ;
$return .= '<table ' . ( $id ? 'id="' . $id . '" ' : '' ) . 'summary="" width="100%" border="0" class="notopnoleftnoright" style="margin-bottom: 2px;"><tr>' ;
if ( $picto ) $return .= '<td class="nobordernopadding hideonsmartphone" width="40" align="left" valign="middle">' . img_picto ( '' , $picto , 'id="pictotitle"' , $pictoisfullpath ) . '</td>' ;
$return .= '<td class="nobordernopadding" valign="middle">' ;
$return .= '<div class="titre">' . $titre . '</div>' ;
$return .= '</td>' ;
if ( dol_strlen ( $mesg ))
{
$return .= '<td class="nobordernopadding" align="right" valign="middle"><b>' . $mesg . '</b></td>' ;
}
$return .= '</tr></table>' . " \n " ;
return $return ;
2010-05-02 06:57:15 +02:00
}
2008-08-06 16:50:06 +02:00
/**
2011-05-03 21:03:08 +02:00
* Print a title with navigation controls for pagination
2011-09-02 23:56:37 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $titre Title to show ( required )
* @ param string $page Numero of page ( required )
* @ param string $file Url of page ( required )
* @ param string $options parametres complementaires lien ( '' par defaut )
* @ param string $sortfield champ de tri ( '' par defaut )
* @ param string $sortorder ordre de tri ( '' par defaut )
* @ param string $center chaine du centre ( '' par defaut )
* @ param int $num number of records found by select with limit + 1
* @ param int $totalnboflines Total number of records / lines for all pages ( if known )
* @ param string $picto Icon to use before title ( should be a 32 x32 transparent png file )
* @ param int $pictoisfullpath 1 = Icon name is a full absolute url of image
2011-10-03 18:10:50 +02:00
* @ return void
2008-08-06 16:50:06 +02:00
*/
2009-06-05 18:56:01 +02:00
function print_barre_liste ( $titre , $page , $file , $options = '' , $sortfield = '' , $sortorder = '' , $center = '' , $num =- 1 , $totalnboflines = 0 , $picto = 'title.png' , $pictoisfullpath = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
if ( $picto == 'setup' ) $picto = 'title.png' ;
if ( ! empty ( $conf -> browser -> ie ) && $picto == 'title.png' ) $picto = 'title.gif' ;
if ( $num > $conf -> liste_limit or $num == - 1 )
{
$nextpage = 1 ;
}
else
{
$nextpage = 0 ;
}
print " \n " ;
print " <!-- Begin title ' " . $titre . " ' --> \n " ;
print '<table width="100%" border="0" class="notopnoleftnoright" style="margin-bottom: 2px;"><tr>' ;
$pagelist = '' ;
// Left
if ( $page > 0 || $num > $conf -> liste_limit )
{
if ( $totalnboflines )
{
if ( $picto && $titre ) print '<td class="nobordernopadding" width="40" align="left" valign="middle">' . img_picto ( '' , $picto , '' , $pictoisfullpath ) . '</td>' ;
print '<td class="nobordernopadding">' ;
print '<div class="titre">' . $titre . '</div>' ;
print '</td>' ;
$maxnbofpage = 10 ;
$nbpages = ceil ( $totalnboflines / $conf -> liste_limit );
$cpt = ( $page - $maxnbofpage );
if ( $cpt < 0 ) { $cpt = 0 ; }
$pagelist .= $langs -> trans ( 'Page' );
if ( $cpt >= 1 )
{
$pagelist .= ' <a href="' . $file . '?page=0' . $options . '&sortfield=' . $sortfield . '&sortorder=' . $sortorder . '">1</a>' ;
if ( $cpt >= 2 ) $pagelist .= ' ...' ;
}
do
{
if ( $cpt == $page )
{
$pagelist .= ' <u>' . ( $page + 1 ) . '</u>' ;
}
else
{
$pagelist .= ' <a href="' . $file . '?page=' . $cpt . $options . '&sortfield=' . $sortfield . '&sortorder=' . $sortorder . '">' . ( $cpt + 1 ) . '</a>' ;
}
$cpt ++ ;
}
while ( $cpt < $nbpages && $cpt <= $page + $maxnbofpage );
if ( $cpt < $nbpages )
{
if ( $cpt < $nbpages - 1 ) $pagelist .= ' ...' ;
$pagelist .= ' <a href="' . $file . '?page=' . ( $nbpages - 1 ) . $options . '&sortfield=' . $sortfield . '&sortorder=' . $sortorder . '">' . $nbpages . '</a>' ;
}
}
else
{
if ( empty ( $conf -> browser -> phone ) && $picto && $titre ) print '<td class="nobordernopadding" width="40" align="left" valign="middle">' . img_picto ( '' , $picto , '' , $pictoisfullpath ) . '</td>' ;
print '<td class="nobordernopadding">' ;
print '<div class="titre">' . $titre . '</div>' ;
$pagelist .= $langs -> trans ( 'Page' ) . ' ' . ( $page + 1 );
print '</td>' ;
}
}
else
{
if ( empty ( $conf -> browser -> phone ) && $picto && $titre ) print '<td class="nobordernopadding" width="40" align="left" valign="middle">' . img_picto ( '' , $picto , '' , $pictoisfullpath ) . '</td>' ;
print '<td class="nobordernopadding"><div class="titre">' . $titre . '</div></td>' ;
}
// Center
if ( $center )
{
print '<td class="nobordernopadding" align="left" valign="middle">' . $center . '</td>' ;
}
// Right
print '<td class="nobordernopadding" align="right" valign="middle">' ;
if ( $sortfield ) $options .= " &sortfield= " . $sortfield ;
if ( $sortorder ) $options .= " &sortorder= " . $sortorder ;
// Affichage des fleches de navigation
print_fleche_navigation ( $page , $file , $options , $nextpage , $pagelist );
print '</td>' ;
print '</tr></table>' . " \n " ;
print " <!-- End title --> \n \n " ;
2008-08-06 16:50:06 +02:00
}
/**
2011-03-09 16:48:25 +01:00
* Fonction servant a afficher les fleches de navigation dans les pages de listes
2011-09-02 23:56:37 +02:00
*
2011-10-03 18:10:50 +02:00
* @ param int $page Numero of page
* @ param string $file Lien
* @ param string $options Autres parametres d ' url a propager dans les liens ( " " par defaut )
* @ param int $nextpage Faut - il une page suivante
* @ param string $betweenarrows HTML Content to show between arrows
* @ return void
2008-08-06 16:50:06 +02:00
*/
2012-02-12 16:50:58 +01:00
function print_fleche_navigation ( $page , $file , $options = '' , $nextpage = 0 , $betweenarrows = '' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
if ( $page > 0 )
{
print '<a href="' . $file . '?page=' . ( $page - 1 ) . $options . '">' . img_previous ( $langs -> trans ( " Previous " )) . '</a>' ;
}
if ( $betweenarrows ) print ( $page > 0 ? ' ' : '' ) . $betweenarrows . ( $nextpage > 0 ? ' ' : '' );
if ( $nextpage > 0 )
{
print '<a href="' . $file . '?page=' . ( $page + 1 ) . $options . '">' . img_next ( $langs -> trans ( " Next " )) . '</a>' ;
}
2008-08-06 16:50:06 +02:00
}
2008-11-16 19:55:00 +01:00
/**
2011-10-09 17:37:27 +02:00
* Return a string with VAT rate label formated for view output
* Used into pdf and HTML pages
2011-09-02 23:56:37 +02:00
*
2011-10-03 18:10:50 +02:00
* @ param float $rate Rate value to format ( 19.6 19 , 6 19.6 % 19 , 6 % , ... )
* @ param boolean $addpercent Add a percent % sign in output
2011-10-09 17:37:27 +02:00
* @ param int $info_bits Miscellanous information on vat ( 0 = Default , 1 = French NPR vat )
* @ param int $usestarfornpr 1 = Use '*' for NPR vat rate intead of MAIN_LABEL_MENTION_NPR
* @ return string String with formated amounts ( 19 , 6 or 19 , 6 % or 8.5 % NPR or 8.5 % * )
2008-08-06 16:50:06 +02:00
*/
2011-10-09 17:37:27 +02:00
function vatrate ( $rate , $addpercent = false , $info_bits = 0 , $usestarfornpr = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
// Test for compatibility
if ( preg_match ( '/%/' , $rate ))
{
$rate = str_replace ( '%' , '' , $rate );
$addpercent = true ;
}
if ( preg_match ( '/\*/' , $rate ) || preg_match ( '/' . constant ( 'MAIN_LABEL_MENTION_NPR' ) . '/i' , $rate ))
{
$rate = str_replace ( '*' , '' , $rate );
$info_bits |= 1 ;
}
$ret = price ( $rate , 0 , '' , 0 , 0 ) . ( $addpercent ? '%' : '' );
if ( $info_bits & 1 ) $ret .= ' ' . ( $usestarfornpr ? '*' : constant ( 'MAIN_LABEL_MENTION_NPR' ));
return $ret ;
2008-08-06 16:50:06 +02:00
}
/**
2010-09-30 10:12:03 +02:00
* Fonction qui formate un montant pour visualisation
* Fonction utilisee dans les pdf et les pages html
2011-09-02 23:56:37 +02:00
*
2011-10-03 18:10:50 +02:00
* @ param float $amount Montant a formater
2011-11-08 10:18:45 +01:00
* @ param string $form Type de formatage , html ou pas ( par defaut )
2011-10-03 18:10:50 +02:00
* @ param Translate $outlangs Objet langs pour formatage text
* @ param int $trunc 1 = Tronque affichage si trop de decimales , 0 = Force le non troncage
* @ param int $rounding Minimum number of decimal . If not defined we use min ( $conf -> global -> MAIN_MAX_DECIMALS_UNIT , $conf -> global -> MAIN_MAX_DECIMALS_TOTAL )
* @ param int $forcerounding Force the number of decimal
* @ return string Chaine avec montant formate
*
* @ see price2num Revert function of price
2010-09-30 10:12:03 +02:00
*/
2011-11-08 10:18:45 +01:00
function price ( $amount , $form = 0 , $outlangs = '' , $trunc = 1 , $rounding =- 1 , $forcerounding =- 1 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $langs , $conf ;
// Clean parameters
if ( empty ( $amount )) $amount = 0 ; // To have a numeric value if amount not defined or = ''
2012-12-07 10:50:16 +01:00
$amount = ( is_numeric ( $amount ) ? $amount : 0 ); // Check if amount is numeric, for example, an error occured when amount value = o (letter) instead 0 (number)
2012-08-03 23:29:08 +02:00
if ( $rounding < 0 ) $rounding = min ( $conf -> global -> MAIN_MAX_DECIMALS_UNIT , $conf -> global -> MAIN_MAX_DECIMALS_TOT );
$nbdecimal = $rounding ;
// Output separators by default (french)
$dec = ',' ; $thousand = ' ' ;
// If $outlangs not forced, we use use language
if ( ! is_object ( $outlangs )) $outlangs = $langs ;
if ( $outlangs -> trans ( " SeparatorDecimal " ) != " SeparatorDecimal " ) $dec = $outlangs -> trans ( " SeparatorDecimal " );
if ( $outlangs -> trans ( " SeparatorThousand " ) != " SeparatorThousand " ) $thousand = $outlangs -> trans ( " SeparatorThousand " );
if ( $thousand == 'None' ) $thousand = '' ;
//print "amount=".$amount." html=".$form." trunc=".$trunc." nbdecimal=".$nbdecimal." dec='".$dec."' thousand='".$thousand."'<br>";
//print "amount=".$amount."-";
$amount = str_replace ( ',' , '.' , $amount ); // should be useless
//print $amount."-";
$datas = explode ( '.' , $amount );
$decpart = isset ( $datas [ 1 ]) ? $datas [ 1 ] : '' ;
$decpart = preg_replace ( '/0+$/i' , '' , $decpart ); // Supprime les 0 de fin de partie decimale
//print "decpart=".$decpart."<br>";
$end = '' ;
// We increase nbdecimal if there is more decimal than asked (to not loose information)
if ( dol_strlen ( $decpart ) > $nbdecimal ) $nbdecimal = dol_strlen ( $decpart );
// Si on depasse max
if ( $trunc && $nbdecimal > $conf -> global -> MAIN_MAX_DECIMALS_SHOWN )
{
$nbdecimal = $conf -> global -> MAIN_MAX_DECIMALS_SHOWN ;
if ( preg_match ( '/\.\.\./i' , $conf -> global -> MAIN_MAX_DECIMALS_SHOWN ))
{
// Si un affichage est tronque, on montre des ...
$end = '...' ;
}
}
// If force rounding
if ( $forcerounding >= 0 ) $nbdecimal = $forcerounding ;
// Format number
if ( $form )
{
$output = preg_replace ( '/\s/' , ' ' , number_format ( $amount , $nbdecimal , $dec , $thousand ));
}
else
{
$output = number_format ( $amount , $nbdecimal , $dec , $thousand );
}
$output .= $end ;
return $output ;
2008-08-06 16:50:06 +02:00
}
/**
2010-09-07 02:38:03 +02:00
* Function that return a number with universal decimal format ( decimal separator is '.' ) from
* an amount typed by a user .
2011-09-02 23:56:37 +02:00
* Function to use on each input amount before any numeric test or database insert
*
2011-10-03 18:10:50 +02:00
* @ param float $amount Amount to convert / clean
* @ param string $rounding '' = No rounding
2009-01-25 17:45:04 +01:00
* 'MU' = Round to Max unit price ( MAIN_MAX_DECIMALS_UNIT )
* 'MT' = Round to Max for totals with Tax ( MAIN_MAX_DECIMALS_TOT )
* 'MS' = Round to Max Shown ( MAIN_MAX_DECIMALS_SHOWN )
2011-10-03 18:10:50 +02:00
* @ param int $alreadysqlnb Put 1 if you know that content is already universal format number
* @ return string Amount with universal numeric format ( Example : '99.99999' )
*
* @ see price Opposite function of price2num
2009-01-25 17:45:04 +01:00
*/
function price2num ( $amount , $rounding = '' , $alreadysqlnb = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $langs , $conf ;
// Round PHP function does not allow number like '1,234.56' nor '1.234,56' nor '1 234,56'
// Numbers must be '1234.56'
// Decimal delimiter for PHP and database SQL requests must be '.'
$dec = ',' ; $thousand = ' ' ;
if ( $langs -> trans ( " SeparatorDecimal " ) != " SeparatorDecimal " ) $dec = $langs -> trans ( " SeparatorDecimal " );
if ( $langs -> trans ( " SeparatorThousand " ) != " SeparatorThousand " ) $thousand = $langs -> trans ( " SeparatorThousand " );
if ( $thousand == 'None' ) $thousand = '' ;
//print "amount=".$amount." html=".$form." trunc=".$trunc." nbdecimal=".$nbdecimal." dec='".$dec."' thousand='".$thousand."'<br>";
// Convert value to universal number format (no thousand separator, '.' as decimal separator)
if ( $alreadysqlnb != 1 ) // If not a PHP number or unknown, we change format
{
//print 'PP'.$amount.' - '.$dec.' - '.$thousand.'<br>';
// Convert amount to format with dolibarr dec and thousand (this is because PHP convert a number
// to format defined by LC_NUMERIC after a calculation and we want source format to be like defined by Dolibarr setup.
if ( is_numeric ( $amount ))
{
// We put in temps value of decimal ("0.00001"). Works with 0 and 2.0E-5 and 9999.10
$temps = sprintf ( " %0.10F " , $amount - intval ( $amount )); // temps=0.0000000000 or 0.0000200000 or 9999.1000000000
$temps = preg_replace ( '/([\.1-9])0+$/' , '\\1' , $temps ); // temps=0. or 0.00002 or 9999.1
$nbofdec = max ( 0 , dol_strlen ( $temps ) - 2 ); // -2 to remove "0."
$amount = number_format ( $amount , $nbofdec , $dec , $thousand );
}
//print "QQ".$amount.'<br>';
// Now make replace (the main goal of function)
if ( $thousand != ',' && $thousand != '.' ) $amount = str_replace ( ',' , '.' , $amount ); // To accept 2 notations for french users
$amount = str_replace ( ' ' , '' , $amount ); // To avoid spaces
$amount = str_replace ( $thousand , '' , $amount ); // Replace of thousand before replace of dec to avoid pb if thousand is .
$amount = str_replace ( $dec , '.' , $amount );
}
// Now, make a rounding if required
if ( $rounding )
{
$nbofdectoround = '' ;
if ( $rounding == 'MU' ) $nbofdectoround = $conf -> global -> MAIN_MAX_DECIMALS_UNIT ;
elseif ( $rounding == 'MT' ) $nbofdectoround = $conf -> global -> MAIN_MAX_DECIMALS_TOT ;
elseif ( $rounding == 'MS' ) $nbofdectoround = $conf -> global -> MAIN_MAX_DECIMALS_SHOWN ;
2012-09-06 23:58:09 +02:00
elseif ( is_numeric ( $rounding )) $nbofdectoround = $rounding ; // For admin info page
2012-08-03 23:29:08 +02:00
//print "RR".$amount.' - '.$nbofdectoround.'<br>';
if ( dol_strlen ( $nbofdectoround )) $amount = round ( $amount , $nbofdectoround ); // $nbofdectoround can be 0.
else return 'ErrorBadParameterProvidedToFunction' ;
//print 'SS'.$amount.' - '.$nbofdec.' - '.$dec.' - '.$thousand.' - '.$nbofdectoround.'<br>';
// Convert amount to format with dolibarr dec and thousand (this is because PHP convert a number
// to format defined by LC_NUMERIC after a calculation and we want source format to be defined by Dolibarr setup.
if ( is_numeric ( $amount ))
{
// We put in temps value of decimal ("0.00001"). Works with 0 and 2.0E-5 and 9999.10
$temps = sprintf ( " %0.10F " , $amount - intval ( $amount )); // temps=0.0000000000 or 0.0000200000 or 9999.1000000000
$temps = preg_replace ( '/([\.1-9])0+$/' , '\\1' , $temps ); // temps=0. or 0.00002 or 9999.1
$nbofdec = max ( 0 , dol_strlen ( $temps ) - 2 ); // -2 to remove "0."
$amount = number_format ( $amount , min ( $nbofdec , $nbofdectoround ), $dec , $thousand ); // Convert amount to format with dolibarr dec and thousand
}
//print "TT".$amount.'<br>';
// Always make replace because each math function (like round) replace
// with local values and we want a number that has a SQL string format x.y
if ( $thousand != ',' && $thousand != '.' ) $amount = str_replace ( ',' , '.' , $amount ); // To accept 2 notations for french users
$amount = str_replace ( ' ' , '' , $amount ); // To avoid spaces
$amount = str_replace ( $thousand , '' , $amount ); // Replace of thousand before replace of dec to avoid pb if thousand is .
$amount = str_replace ( $dec , '.' , $amount );
}
return $amount ;
2008-08-06 16:50:06 +02:00
}
2010-03-28 17:13:17 +02:00
/**
2012-11-06 23:45:16 +01:00
* Return localtax rate for a particular vat , when selling a product with vat $tva , from a $thirdparty_buyer to a $thirdparty_seller
* Note : It applies same rule than get_default_tva
2011-09-02 23:56:37 +02:00
*
2011-10-03 18:10:50 +02:00
* @ param float $tva Vat taxe
2012-09-05 14:05:17 +02:00
* @ param int $local Local tax to search and return ( 1 or 2 return only tax rate 1 or tax rate 2 )
* @ param Societe $thirdparty_buyer Object of buying third party
* @ param Societe $thirdparty_seller Object of selling third party
2012-12-19 18:51:49 +01:00
* @ return mixed 0 if not found , localtax if found
2012-11-06 23:45:16 +01:00
* @ see get_default_tva
2010-03-28 17:13:17 +02:00
*/
2012-09-05 14:05:17 +02:00
function get_localtax ( $tva , $local , $thirdparty_buyer = " " , $thirdparty_seller = " " )
2010-03-28 17:13:17 +02:00
{
2012-08-03 23:29:08 +02:00
global $db , $conf , $mysoc ;
2012-09-05 14:05:17 +02:00
if ( empty ( $thirdparty_seller ) || ! is_object ( $thirdparty_seller )) $thirdparty_seller = $mysoc ;
2012-08-03 23:29:08 +02:00
2012-12-19 18:51:49 +01:00
dol_syslog ( " get_localtax tva= " . $tva . " local= " . $local . " thirdparty_buyer id= " . ( is_object ( $thirdparty_buyer ) ? $thirdparty_buyer -> id : '' ) . " thirdparty_seller id= " . $thirdparty_seller -> id );
2012-08-03 23:29:08 +02:00
2012-09-05 14:05:17 +02:00
// Some test to guess with no need to make database access
2013-03-01 20:32:49 +01:00
if ( $mysoc -> country_code == 'ES' ) // For spain localtaxes 1 and 2, tax is qualified if buyer use local taxe
2012-11-16 16:23:51 +01:00
{
2013-04-04 16:32:06 +02:00
if ( $local == 1 )
{
if ( $thirdparty_seller -> id == $mysoc -> id )
{
if ( ! $thirdparty_buyer -> localtax1_assuj ) return 0 ;
}
else
{
if ( ! $thirdparty_seller -> localtax1_assuj ) return 0 ;
}
}
2013-03-01 20:32:49 +01:00
if ( $local == 2 && ! $thirdparty_buyer -> localtax2_assuj ) return 0 ;
2012-11-16 16:23:51 +01:00
}
2012-12-02 12:59:06 +01:00
else
2012-11-16 16:23:51 +01:00
{
if ( $local == 1 && ! $thirdparty_seller -> localtax1_assuj ) return 0 ;
if ( $local == 2 && ! $thirdparty_seller -> localtax2_assuj ) return 0 ;
}
2012-09-07 17:23:16 +02:00
//if ($local == 0 && ! $thirdparty_seller->localtax1_assuj && ! $thirdparty_seller->localtax2_assuj) return array('localtax1'=>0,'localtax2'=>0);
2012-09-05 14:05:17 +02:00
$code_country = $thirdparty_seller -> country_code ;
if ( is_object ( $thirdparty_buyer ))
2012-08-03 23:29:08 +02:00
{
2012-11-06 23:45:16 +01:00
if ( $code_country != $thirdparty_buyer -> country_code ) return 0 ;
2012-08-03 23:29:08 +02:00
}
// Search local taxes
2012-11-27 15:33:11 +01:00
$sql = " SELECT t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type " ;
2012-08-03 23:29:08 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_tva as t, " . MAIN_DB_PREFIX . " c_pays as p " ;
2012-09-05 14:05:17 +02:00
$sql .= " WHERE t.fk_pays = p.rowid AND p.code = ' " . $code_country . " ' " ;
2012-08-03 23:29:08 +02:00
$sql .= " AND t.taux = " . $tva . " AND t.active = 1 " ;
2012-09-05 14:05:17 +02:00
dol_syslog ( " get_localtax sql= " . $sql );
2012-08-03 23:29:08 +02:00
$resql = $db -> query ( $sql );
if ( $resql )
{
$obj = $db -> fetch_object ( $resql );
2012-11-27 15:33:11 +01:00
if ( $local == 1 && $obj -> localtax1_type != '7' ) return $obj -> localtax1 ;
elseif ( $local == 2 && $obj -> localtax2_type != '7' ) return $obj -> localtax2 ;
2012-08-03 23:29:08 +02:00
}
return 0 ;
2010-03-28 17:13:17 +02:00
}
2008-08-06 16:50:06 +02:00
2012-12-19 18:51:49 +01:00
/**
* Get type and rate of localtaxes for a particular vat rate / country fo thirdparty
*
* @ param real $vatrate VAT Rate
* @ param int $local Number of localtax ( 1 / 2 )
* @ param int $thirdparty company object
* @ return array array ( Type of local tax ( 1 to 7 / 0 if not found ), rate or amount of localtax )
* @ deprecated TODO We should remove this function by storing rate and type into detail lines .
*/
function getLocalTaxesFromRate ( $vatrate , $local , $thirdparty )
{
global $db ;
dol_syslog ( " getLocalTaxesFromRate vatrate= " . $vatrate . " local= " . $local . " thirdparty id= " . ( is_object ( $thirdparty ) ? $thirdparty -> id : '' ));
// Search local taxes
$sql = " SELECT t.localtax1, t.localtax1_type, t.localtax2, t.localtax2_type " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " c_tva as t, " . MAIN_DB_PREFIX . " c_pays as p " ;
$sql .= " WHERE t.fk_pays = p.rowid AND p.code = ' " . $thirdparty -> country_code . " ' " ;
$sql .= " AND t.taux = " . $vatrate . " AND t.active = 1 " ;
$resql = $db -> query ( $sql );
if ( $resql )
{
$obj = $db -> fetch_object ( $resql );
if ( $local == 1 ) return array ( $obj -> localtax1_type , $obj -> localtax1 );
elseif ( $local == 2 ) return array ( $obj -> localtax2_type , $obj -> localtax2 );
}
return 0 ;
}
2008-08-06 16:50:06 +02:00
/**
2012-11-06 23:45:16 +01:00
* Return vat rate of a product in a particular selling country or default country vat if product is unknown
2011-09-02 23:56:37 +02:00
*
2012-05-08 21:55:52 +02:00
* @ param int $idprod Id of product or 0 if not a predefined product
* @ param Societe $thirdparty_seller Thirdparty with a -> country_code defined ( FR , US , IT , ... )
* @ param int $idprodfournprice Id product_fournisseur_price ( for supplier order / invoice )
* @ return int < 0 if KO , Vat rate if OK
2012-11-06 23:45:16 +01:00
* @ see get_product_localtax_for_country
2008-08-06 16:50:06 +02:00
*/
2012-05-08 21:55:52 +02:00
function get_product_vat_for_country ( $idprod , $thirdparty_seller , $idprodfournprice = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $db , $mysoc ;
2012-08-29 08:17:25 +02:00
if ( ! class_exists ( 'Product' )) {
require DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
}
2012-08-03 23:29:08 +02:00
$ret = 0 ;
$found = 0 ;
if ( $idprod > 0 )
{
// Load product
$product = new Product ( $db );
$result = $product -> fetch ( $idprod );
2012-09-05 14:05:17 +02:00
if ( $mysoc -> country_code == $thirdparty_seller -> country_code ) // If selling country is ours
2012-08-03 23:29:08 +02:00
{
if ( $idprodfournprice > 0 ) // We want vat for product for a supplier order or invoice
{
$product -> get_buyprice ( $idprodfournprice , 0 , 0 , 0 );
$ret = $product -> vatrate_supplier ;
}
else
{
$ret = $product -> tva_tx ; // Default vat of product we defined
}
$found = 1 ;
}
else
{
// TODO Read default product vat according to countrycode and product
}
}
if ( ! $found )
{
// If vat of product for the country not found or not defined, we return higher vat of country.
$sql = " SELECT taux as vat_rate " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " c_tva as t, " . MAIN_DB_PREFIX . " c_pays as p " ;
$sql .= " WHERE t.active=1 AND t.fk_pays = p.rowid AND p.code=' " . $thirdparty_seller -> country_code . " ' " ;
$sql .= " ORDER BY t.taux DESC, t.recuperableonly ASC " ;
$sql .= $db -> plimit ( 1 );
$resql = $db -> query ( $sql );
if ( $resql )
{
$obj = $db -> fetch_object ( $resql );
if ( $obj )
{
$ret = $obj -> vat_rate ;
}
}
else dol_print_error ( $db );
}
dol_syslog ( " get_product_vat_for_country: ret= " . $ret );
return $ret ;
2008-08-06 16:50:06 +02:00
}
2010-03-27 17:18:10 +01:00
/**
2012-11-06 23:45:16 +01:00
* Return localtax vat rate of a product in a particular selling country or default country vat if product is unknown
2011-09-02 23:56:37 +02:00
*
2012-11-06 23:45:16 +01:00
* @ param int $idprod Id of product
* @ param int $local 1 for localtax1 , 2 for localtax 2
* @ param Societe $thirdparty_seller Thirdparty with a -> country_code defined ( FR , US , IT , ... )
* @ return int < 0 if KO , Vat rate if OK
* @ see get_product_vat_for_country
2010-03-27 17:18:10 +01:00
*/
2012-11-06 23:45:16 +01:00
function get_product_localtax_for_country ( $idprod , $local , $thirdparty_seller )
2010-03-27 17:18:10 +01:00
{
2012-11-06 23:45:16 +01:00
global $db , $mysoc ;
2010-03-27 17:18:10 +01:00
2012-11-06 23:45:16 +01:00
if ( ! class_exists ( 'Product' )) {
require DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
}
2010-03-29 01:36:52 +02:00
2012-11-14 11:31:08 +01:00
$ret = 0 ;
$found = 0 ;
if ( $idprod > 0 )
{
// Load product
$product = new Product ( $db );
$result = $product -> fetch ( $idprod );
if ( $mysoc -> country_code == $thirdparty_seller -> country_code ) // If selling country is ours
{
2012-11-06 23:45:16 +01:00
/* Not defined yet , so we don ' t use this
if ( $local == 1 ) $ret = $product -> localtax1_tx ;
elseif ( $local == 2 ) $ret = $product -> localtax2_tx ;
$found = 1 ;
2012-11-14 11:31:08 +01:00
*/
}
else
{
// TODO Read default product vat according to countrycode and product
}
}
if ( ! $found )
{
// If vat of product for the country not found or not defined, we return higher vat of country.
$sql = " SELECT taux as vat_rate, localtax1, localtax2 " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " c_tva as t, " . MAIN_DB_PREFIX . " c_pays as p " ;
$sql .= " WHERE t.active=1 AND t.fk_pays = p.rowid AND p.code=' " . $thirdparty_seller -> country_code . " ' " ;
$sql .= " ORDER BY t.taux DESC, t.recuperableonly ASC " ;
$sql .= $db -> plimit ( 1 );
$resql = $db -> query ( $sql );
if ( $resql )
{
$obj = $db -> fetch_object ( $resql );
if ( $obj )
{
if ( $local == 1 ) $ret = $obj -> localtax1 ;
2012-11-06 23:45:16 +01:00
elseif ( $local == 2 ) $ret = $obj -> localtax2 ;
2012-11-14 11:31:08 +01:00
}
}
else dol_print_error ( $db );
}
dol_syslog ( " get_product_localtax_for_country: ret= " . $ret );
2012-11-06 23:45:16 +01:00
return $ret ;
2010-03-27 17:18:10 +01:00
}
2008-08-06 16:50:06 +02:00
/**
2010-09-07 02:38:03 +02:00
* Function that return vat rate of a product line ( according to seller , buyer and product vat rate )
* Si vendeur non assujeti a TVA , TVA par defaut = 0. Fin de regle .
* Si le ( pays vendeur = pays acheteur ) alors TVA par defaut = TVA du produit vendu . Fin de regle .
* 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 acheteur au centre d ' impots de son pays et non au vendeur ) . Fin de regle .
* Si ( vendeur et acheteur dans Communaute europeenne ) et ( acheteur = particulier ou entreprise sans num TVA intra ) alors TVA par defaut = TVA du produit vendu . Fin de regle
* Si ( vendeur et acheteur dans Communaute europeenne ) et ( acheteur = entreprise avec num TVA ) intra alors TVA par defaut = 0. Fin de regle
* Sinon TVA proposee par defaut = 0. Fin de regle .
2011-10-09 17:37:27 +02:00
*
2012-09-05 14:05:17 +02:00
* @ param Societe $thirdparty_seller Objet societe vendeuse
* @ param Societe $thirdparty_buyer Objet societe acheteuse
2011-10-09 17:37:27 +02:00
* @ param int $idprod Id product
2012-05-08 21:55:52 +02:00
* @ param int $idprodfournprice Id product_fournisseur_price ( for supplier order / invoice )
2011-10-09 17:37:27 +02:00
* @ return float Taux de tva a appliquer , - 1 si ne peut etre determine
2012-11-06 23:45:16 +01:00
* @ see get_default_localtax
2008-08-06 16:50:06 +02:00
*/
2012-09-05 14:05:17 +02:00
function get_default_tva ( $thirdparty_seller , $thirdparty_buyer , $idprod = 0 , $idprodfournprice = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf ;
2012-09-05 14:05:17 +02:00
if ( ! is_object ( $thirdparty_seller )) return - 1 ;
if ( ! is_object ( $thirdparty_buyer )) return - 1 ;
2012-08-03 23:29:08 +02:00
2012-09-05 14:05:17 +02:00
dol_syslog ( " get_default_tva: seller use vat= " . $thirdparty_seller -> tva_assuj . " , seller country= " . $thirdparty_seller -> country_code . " , seller in cee= " . $thirdparty_seller -> isInEEC () . " , buyer country= " . $thirdparty_buyer -> country_code . " , buyer in cee= " . $thirdparty_buyer -> isInEEC () . " , idprod= " . $idprod . " , idprodfournprice= " . $idprodfournprice . " , SERVICE_ARE_ECOMMERCE_200238EC= " . ( ! empty ( $conf -> global -> SERVICES_ARE_ECOMMERCE_200238EC ) ? $conf -> global -> SERVICES_ARE_ECOMMERCE_200238EC : '' ));
2012-08-03 23:29:08 +02:00
2013-01-09 19:56:43 +01:00
// 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)
// we use the buyer VAT.
if ( ! empty ( $conf -> global -> SERVICE_ARE_ECOMMERCE_200238EC ))
{
//print "eee".$thirdparty_buyer->isACompany();exit;
if ( ! $thirdparty_seller -> isInEEC () && $thirdparty_buyer -> isInEEC () && ! $thirdparty_buyer -> isACompany ())
{
//print 'VATRULE 6';
return get_product_vat_for_country ( $idprod , $thirdparty_buyer , $idprodfournprice );
}
}
2012-08-03 23:29:08 +02:00
// Si vendeur non assujeti a TVA (tva_assuj vaut 0/1 ou franchise/reel)
2012-09-05 14:05:17 +02:00
if ( is_numeric ( $thirdparty_seller -> tva_assuj ) && ! $thirdparty_seller -> tva_assuj )
2012-08-03 23:29:08 +02:00
{
//print 'VATRULE 1';
return 0 ;
}
2012-09-05 14:05:17 +02:00
if ( ! is_numeric ( $thirdparty_seller -> tva_assuj ) && $thirdparty_seller -> tva_assuj == 'franchise' )
2012-08-03 23:29:08 +02:00
{
//print 'VATRULE 2';
return 0 ;
}
2012-09-05 14:05:17 +02:00
//if (is_object($thirdparty_buyer) && ($thirdparty_seller->country_id == $thirdparty_buyer->country_id) && ($thirdparty_buyer->tva_assuj == 1 || $thirdparty_buyer->tva_assuj == 'reel'))
2012-08-03 23:29:08 +02:00
// 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.
// Si le (pays vendeur = pays acheteur) alors la TVA par defaut=TVA du produit vendu. Fin de regle.
2012-09-05 14:05:17 +02:00
if (( $thirdparty_seller -> country_code == $thirdparty_buyer -> country_code )
|| ( in_array ( $thirdparty_seller -> country_code , array ( 'FR,MC' )) && in_array ( $thirdparty_buyer -> country_code , array ( 'FR' , 'MC' )))) // Warning ->country_code not always defined
2012-08-03 23:29:08 +02:00
{
//print 'VATRULE 3';
2012-09-05 14:05:17 +02:00
return get_product_vat_for_country ( $idprod , $thirdparty_seller , $idprodfournprice );
2012-08-03 23:29:08 +02:00
}
// 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.
// Non gere
// Si (vendeur et acheteur dans Communaute europeenne) et (acheteur = entreprise) alors TVA par defaut=0. Fin de regle
// Si (vendeur et acheteur dans Communaute europeenne) et (acheteur = particulier) alors TVA par defaut=TVA du produit vendu. Fin de regle
2012-09-05 14:05:17 +02:00
if (( $thirdparty_seller -> isInEEC () && $thirdparty_buyer -> isInEEC ()))
2012-08-03 23:29:08 +02:00
{
2012-09-05 14:05:17 +02:00
$isacompany = $thirdparty_buyer -> isACompany ();
2012-08-03 23:29:08 +02:00
if ( $isacompany )
{
//print 'VATRULE 4';
return 0 ;
}
else
{
//print 'VATRULE 5';
2012-09-05 14:05:17 +02:00
return get_product_vat_for_country ( $idprod , $thirdparty_seller , $idprodfournprice );
2012-08-03 23:29:08 +02:00
}
}
// Sinon la TVA proposee par defaut=0. Fin de regle.
// Rem: Cela signifie qu'au moins un des 2 est hors Communaute europeenne et que le pays differe
//print 'VATRULE 7';
return 0 ;
2008-08-06 16:50:06 +02:00
}
/**
2011-05-03 21:03:08 +02:00
* Fonction qui renvoie si tva doit etre tva percue recuperable
2011-10-09 17:37:27 +02:00
*
2012-09-05 14:05:17 +02:00
* @ param Societe $thirdparty_seller Objet societe vendeuse
* @ param Societe $thirdparty_buyer Objet societe acheteuse
2011-10-09 17:37:27 +02:00
* @ param int $idprod Id product
* @ return float 0 or 1
2008-08-06 16:50:06 +02:00
*/
2012-09-05 14:05:17 +02:00
function get_default_npr ( $thirdparty_seller , $thirdparty_buyer , $idprod )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
return 0 ;
2008-08-06 16:50:06 +02:00
}
2010-03-27 17:18:10 +01:00
/**
2011-05-03 21:03:08 +02:00
* Function that return localtax of a product line ( according to seller , buyer and product vat rate )
2012-11-06 23:45:16 +01:00
* Si vendeur non assujeti a TVA , TVA par defaut = 0. Fin de regle .
* Si le ( pays vendeur = pays acheteur ) alors TVA par defaut = TVA du produit vendu . Fin de regle .
* Sinon TVA proposee par defaut = 0. Fin de regle .
2011-08-30 00:55:04 +02:00
*
2012-09-05 14:05:17 +02:00
* @ param Societe $thirdparty_seller Objet societe vendeuse
* @ param Societe $thirdparty_buyer Objet societe acheteuse
2011-10-09 17:37:27 +02:00
* @ param int $local Localtax to process ( 1 or 2 )
* @ param int $idprod Id product
2012-11-06 23:45:16 +01:00
* @ return float localtax , - 1 si ne peut etre determine
* @ see get_default_tva
2010-03-27 17:18:10 +01:00
*/
2012-09-05 14:05:17 +02:00
function get_default_localtax ( $thirdparty_seller , $thirdparty_buyer , $local , $idprod = 0 )
2010-03-27 17:18:10 +01:00
{
2012-12-01 15:45:05 +01:00
global $mysoc ;
2012-12-02 12:59:06 +01:00
2012-09-05 14:05:17 +02:00
if ( ! is_object ( $thirdparty_seller )) return - 1 ;
if ( ! is_object ( $thirdparty_buyer )) return - 1 ;
2012-08-03 23:29:08 +02:00
2012-12-01 15:45:05 +01:00
if ( $local == 1 ) // Localtax 1
2012-08-03 23:29:08 +02:00
{
2012-12-01 15:45:05 +01:00
if ( $mysoc -> country_code == 'ES' )
{
if ( is_numeric ( $thirdparty_buyer -> localtax1_assuj ) && ! $thirdparty_buyer -> localtax1_assuj ) return 0 ;
}
2012-12-02 12:59:06 +01:00
else
2012-12-01 15:45:05 +01:00
{
// Si vendeur non assujeti a Localtax1, localtax1 par default=0
if ( is_numeric ( $thirdparty_seller -> localtax1_assuj ) && ! $thirdparty_seller -> localtax1_assuj ) return 0 ;
if ( ! is_numeric ( $thirdparty_seller -> localtax1_assuj ) && $thirdparty_seller -> localtax1_assuj == 'localtax1off' ) return 0 ;
}
2012-11-06 23:45:16 +01:00
}
2012-12-01 15:45:05 +01:00
elseif ( $local == 2 ) //I Localtax 2
2012-11-06 23:45:16 +01:00
{
2012-12-01 15:45:05 +01:00
// Si vendeur non assujeti a Localtax2, localtax2 par default=0
2012-11-06 23:45:16 +01:00
if ( is_numeric ( $thirdparty_seller -> localtax2_assuj ) && ! $thirdparty_seller -> localtax2_assuj ) return 0 ;
if ( ! is_numeric ( $thirdparty_seller -> localtax2_assuj ) && $thirdparty_seller -> localtax2_assuj == 'localtax2off' ) return 0 ;
}
2012-08-03 23:29:08 +02:00
2012-11-06 23:45:16 +01:00
if ( $thirdparty_seller -> country_code == $thirdparty_buyer -> country_code )
{
return get_product_localtax_for_country ( $idprod , $local , $thirdparty_seller );
2012-08-03 23:29:08 +02:00
}
2012-11-06 23:45:16 +01:00
2012-08-03 23:29:08 +02:00
return 0 ;
2010-03-27 17:18:10 +01:00
}
2008-08-06 16:50:06 +02:00
/**
2011-05-03 21:03:08 +02:00
* Return yes or no in current language
2011-09-02 23:56:37 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $yesno Value to test ( 1 , 'yes' , 'true' or 0 , 'no' , 'false' )
* @ param string $case 1 = Yes / No , 0 = yes / no
* @ param int $color 0 = texte only , 1 = Text is formated with a color font style ( 'ok' or 'error' ), 2 = Text is formated with 'ok' color .
* @ return string HTML string
2008-08-06 16:50:06 +02:00
*/
function yn ( $yesno , $case = 1 , $color = 0 )
{
2012-08-03 23:29:08 +02:00
global $langs ;
$result = 'unknown' ;
if ( $yesno == 1 || strtolower ( $yesno ) == 'yes' || strtolower ( $yesno ) == 'true' ) // A mettre avant test sur no a cause du == 0
{
$result = ( $case ? $langs -> trans ( " Yes " ) : $langs -> trans ( " yes " ));
$classname = 'ok' ;
}
elseif ( $yesno == 0 || strtolower ( $yesno ) == 'no' || strtolower ( $yesno ) == 'false' )
{
$result = ( $case ? $langs -> trans ( " No " ) : $langs -> trans ( " no " ));
if ( $color == 2 ) $classname = 'ok' ;
else $classname = 'error' ;
}
if ( $color ) return '<font class="' . $classname . '">' . $result . '</font>' ;
return $result ;
2008-08-06 16:50:06 +02:00
}
/**
2010-11-06 18:07:06 +01:00
* Return a path to have a directory according to an id
* Examples : '001' with level 3 -> " 0/0/1/ " , '015' with level 3 -> " 0/1/5/ "
* Examples : 'ABC-1' with level 3 -> " 0/0/1/ " , '015' with level 1 -> " 5/ "
2011-09-02 23:56:37 +02:00
*
2012-02-04 14:39:47 +01:00
* @ param string $num Id to develop
* @ param int $level Level of development ( 1 , 2 or 3 level )
* @ param int $alpha Use alpha ref
* @ param int $withoutslash 0 = With slash at end , 1 = without slash at end
* @ return string Dir to use
2008-08-06 16:50:06 +02:00
*/
2010-02-13 23:20:32 +01:00
function get_exdir ( $num , $level = 3 , $alpha = 0 , $withoutslash = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
$path = '' ;
if ( empty ( $alpha )) $num = preg_replace ( '/([^0-9])/i' , '' , $num );
else $num = preg_replace ( '/^.*\-/i' , '' , $num );
$num = substr ( " 000 " . $num , - $level );
if ( $level == 1 ) $path = substr ( $num , 0 , 1 );
if ( $level == 2 ) $path = substr ( $num , 1 , 1 ) . '/' . substr ( $num , 0 , 1 );
if ( $level == 3 ) $path = substr ( $num , 2 , 1 ) . '/' . substr ( $num , 1 , 1 ) . '/' . substr ( $num , 0 , 1 );
if ( empty ( $withoutslash )) $path .= '/' ;
return $path ;
2008-08-06 16:50:06 +02:00
}
/**
2011-09-24 04:01:26 +02:00
* Creation of a directory ( this can create recursive subdir )
2011-09-02 23:56:37 +02:00
*
2012-09-02 22:48:52 +02:00
* @ param string $dir Directory to create ( Separator must be '/' . Example : '/mydir/mysubdir' )
2012-09-03 10:17:58 +02:00
* @ param string $dataroot Data root directory ( To avoid having the data root in the loop . Using this will also lost the warning on first dir PHP has no permission when open_basedir is used )
2012-09-02 22:48:52 +02:00
* @ return int < 0 if KO , 0 = already exists , > 0 if OK
2008-08-06 16:50:06 +02:00
*/
2012-09-02 22:48:52 +02:00
function dol_mkdir ( $dir , $dataroot = '' )
2008-08-06 16:50:06 +02:00
{
2012-09-03 10:17:58 +02:00
global $conf ;
2012-08-03 23:29:08 +02:00
dol_syslog ( " functions.lib::dol_mkdir: dir= " . $dir , LOG_INFO );
$dir_osencoded = dol_osencode ( $dir );
if ( @ is_dir ( $dir_osencoded )) return 0 ;
$nberr = 0 ;
$nbcreated = 0 ;
2012-09-02 22:48:52 +02:00
$ccdir = '' ;
if ( ! empty ( $dataroot )) {
// Remove data root from loop
$dir = str_replace ( $dataroot . '/' , '' , $dir );
$ccdir = $dataroot . '/' ;
}
$cdir = explode ( " / " , $dir );
2012-08-03 23:29:08 +02:00
$num = count ( $cdir );
for ( $i = 0 ; $i < $num ; $i ++ )
{
if ( $i > 0 ) $ccdir .= '/' . $cdir [ $i ];
2012-09-03 17:25:35 +02:00
else $ccdir .= $cdir [ $i ];
2012-08-03 23:29:08 +02:00
if ( preg_match ( " /^.: $ / " , $ccdir , $regs )) continue ; // Si chemin Windows incomplet, on poursuit par rep suivant
// Attention, le is_dir() peut echouer bien que le rep existe.
// (ex selon config de open_basedir)
if ( $ccdir )
{
$ccdir_osencoded = dol_osencode ( $ccdir );
if ( ! @ is_dir ( $ccdir_osencoded ))
{
dol_syslog ( " functions.lib::dol_mkdir: Directory ' " . $ccdir . " ' does not exists or is outside open_basedir PHP setting. " , LOG_DEBUG );
umask ( 0 );
$dirmaskdec = octdec ( '0755' );
if ( ! empty ( $conf -> global -> MAIN_UMASK )) $dirmaskdec = octdec ( $conf -> global -> MAIN_UMASK );
$dirmaskdec |= octdec ( '0111' ); // Set x bit required for directories
if ( ! @ mkdir ( $ccdir_osencoded , $dirmaskdec ))
{
// Si le is_dir a renvoye une fausse info, alors on passe ici.
dol_syslog ( " functions.lib::dol_mkdir: Fails to create directory ' " . $ccdir . " ' or directory already exists. " , LOG_WARNING );
$nberr ++ ;
}
else
{
dol_syslog ( " functions.lib::dol_mkdir: Directory ' " . $ccdir . " ' created " , LOG_DEBUG );
$nberr = 0 ; // On remet a zero car si on arrive ici, cela veut dire que les echecs precedents peuvent etre ignore
$nbcreated ++ ;
}
}
else
{
$nberr = 0 ; // On remet a zero car si on arrive ici, cela veut dire que les echecs precedents peuvent etre ignores
}
}
}
return ( $nberr ? - $nberr : $nbcreated );
2008-08-06 16:50:06 +02:00
}
/**
2011-05-03 21:03:08 +02:00
* Return picto saying a field is required
2011-09-02 23:56:37 +02:00
*
2011-05-03 21:03:08 +02:00
* @ return string Chaine avec picto obligatoire
2008-08-06 16:50:06 +02:00
*/
function picto_required ()
{
2012-08-03 23:29:08 +02:00
return '<span class="fieldrequired">*</span>' ;
2008-08-06 16:50:06 +02:00
}
/**
2010-11-13 01:16:41 +01:00
* Clean a string from all HTML tags and entities
2011-09-02 23:56:37 +02:00
*
2011-12-28 01:19:52 +01:00
* @ param string $StringHtml String to clean
* @ param string $removelinefeed Replace also all lines feeds by a space
* @ param string $pagecodeto Encoding of input string
* @ return string String cleaned
2008-08-06 16:50:06 +02:00
*/
2011-12-21 16:10:11 +01:00
function dol_string_nohtmltag ( $StringHtml , $removelinefeed = 1 , $pagecodeto = 'UTF-8' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
$pattern = " /<[^>]+>/ " ;
$temp = dol_html_entity_decode ( $StringHtml , ENT_COMPAT , $pagecodeto );
$temp = preg_replace ( $pattern , " " , $temp );
// Supprime aussi les retours
if ( $removelinefeed ) $temp = str_replace ( array ( " \r \n " , " \r " , " \n " ), " " , $temp );
// et les espaces doubles
while ( strpos ( $temp , " " ))
{
$temp = str_replace ( " " , " " , $temp );
}
$CleanString = trim ( $temp );
return $CleanString ;
2008-08-06 16:50:06 +02:00
}
/**
2011-09-02 23:56:37 +02:00
* Replace CRLF in string with a HTML BR tag
*
2011-12-28 01:19:52 +01:00
* @ param string $stringtoencode String to encode
* @ param string $nl2brmode 0 = Adding br before \n , 1 = Replacing \n by br
* @ param string $forxml false = Use < br > , true = Use < br />
* @ return string String encoded
2008-08-06 16:50:06 +02:00
*/
2010-08-21 20:53:19 +02:00
function dol_nl2br ( $stringtoencode , $nl2brmode = 0 , $forxml = false )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
if ( ! $nl2brmode )
{
// We use @ to avoid warning on PHP4 that does not support entity encoding from UTF8;
if ( version_compare ( PHP_VERSION , '5.3.0' ) < 0 ) return @ nl2br ( $stringtoencode );
else return @ nl2br ( $stringtoencode , $forxml );
}
else
{
$ret = preg_replace ( '/(\r\n|\r|\n)/i' ,( $forxml ? '<br />' : '<br>' ), $stringtoencode );
return $ret ;
}
2008-08-06 16:50:06 +02:00
}
/**
2011-05-01 12:24:46 +02:00
* This function is called to encode a string into a HTML string but differs from htmlentities because
* all entities but & , < , > are converted . This permits to encode special chars to entities with no double
* encoding for already encoded HTML strings .
* This function also remove last CR / BR .
* For PDF usage , you can show text by 2 ways :
* - writeHTMLCell -> param must be encoded into HTML .
* - MultiCell -> param must not be encoded into HTML .
* Because writeHTMLCell convert also \n into < br > , if function
2011-09-02 23:56:37 +02:00
* is used to build PDF , nl2brmode must be 1
*
2011-12-08 20:16:57 +01:00
* @ param string $stringtoencode String to encode
* @ param int $nl2brmode 0 = Adding br before \n , 1 = Replacing \n by br ( for use with FPDF writeHTMLCell function for example )
* @ param string $pagecodefrom Pagecode stringtoencode is encoded
2011-12-28 08:32:30 +01:00
* @ return string String encoded
2008-08-06 16:50:06 +02:00
*/
2008-11-08 20:23:00 +01:00
function dol_htmlentitiesbr ( $stringtoencode , $nl2brmode = 0 , $pagecodefrom = 'UTF-8' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
if ( dol_textishtml ( $stringtoencode ))
{
$newstring = $stringtoencode ;
$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.
$newstring = preg_replace ( '/<br>$/i' , '' , $newstring ); // Remove last <br>
$newstring = strtr ( $newstring , array ( '&' => '__and__' , '<' => '__lt__' , '>' => '__gt__' , '"' => '__dquot__' ));
$newstring = dol_htmlentities ( $newstring , ENT_COMPAT , $pagecodefrom ); // Make entity encoding
$newstring = strtr ( $newstring , array ( '__and__' => '&' , '__lt__' => '<' , '__gt__' => '>' , '__dquot__' => '"' ));
//$newstring=strtr($newstring,array('__li__'=>"<li>\n")); // Restore <li>\n
}
else
{
$newstring = dol_nl2br ( dol_htmlentities ( $stringtoencode , ENT_COMPAT , $pagecodefrom ), $nl2brmode );
}
// Other substitutions that htmlentities does not do
//$newstring=str_replace(chr(128),'€',$newstring); // 128 = 0x80. Not in html entity table. // Seems useles with TCPDF. Make bug with UTF8 languages
return $newstring ;
2008-08-06 16:50:06 +02:00
}
2008-10-24 03:01:56 +02:00
/**
2011-05-03 21:03:08 +02:00
* This function is called to decode a HTML string ( it decodes entities and br tags )
2011-09-02 23:56:37 +02:00
*
2011-12-28 01:19:52 +01:00
* @ param string $stringtodecode String to decode
* @ param string $pagecodeto Page code for result
2011-12-28 08:32:30 +01:00
* @ return string String decoded
2008-08-06 16:50:06 +02:00
*/
2008-10-24 03:01:56 +02:00
function dol_htmlentitiesbr_decode ( $stringtodecode , $pagecodeto = 'UTF-8' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
$ret = dol_html_entity_decode ( $stringtodecode , ENT_COMPAT , $pagecodeto );
$ret = preg_replace ( '/' . " \r \n " . '<br(\s[\sa-zA-Z_="]*)?\/?>/i' , " <br> " , $ret );
$ret = preg_replace ( '/<br(\s[\sa-zA-Z_="]*)?\/?>' . " \r \n " . '/i' , " \r \n " , $ret );
$ret = preg_replace ( '/<br(\s[\sa-zA-Z_="]*)?\/?>' . " \n " . '/i' , " \n " , $ret );
$ret = preg_replace ( '/<br(\s[\sa-zA-Z_="]*)?\/?>/i' , " \n " , $ret );
return $ret ;
2008-08-06 16:50:06 +02:00
}
2008-10-25 19:27:15 +02:00
/**
2011-05-03 21:03:08 +02:00
* This function remove all ending \n and br at end
2011-09-02 23:56:37 +02:00
*
2011-12-28 01:19:52 +01:00
* @ param string $stringtodecode String to decode
* @ return string String decoded
2008-10-25 19:27:15 +02:00
*/
function dol_htmlcleanlastbr ( $stringtodecode )
{
2012-08-03 23:29:08 +02:00
$ret = preg_replace ( '/(<br>|<br(\s[\sa-zA-Z_="]*)?\/?>|' . " \n " . '|' . " \r " . ')+$/i' , " " , $stringtodecode );
return $ret ;
2008-10-25 19:27:15 +02:00
}
2010-08-10 01:57:45 +02:00
/**
* Replace html_entity_decode functions to manage errors
2011-09-02 23:56:37 +02:00
*
2011-12-28 01:19:52 +01:00
* @ param string $a Operand a
* @ param string $b Operand b
* @ param string $c Operand c
2011-12-28 08:32:30 +01:00
* @ return string String decoded
2010-08-10 01:57:45 +02:00
*/
2012-03-18 00:59:24 +01:00
function dol_html_entity_decode ( $a , $b , $c = 'UTF-8' )
2010-08-10 01:57:45 +02:00
{
2012-08-03 23:29:08 +02:00
// We use @ to avoid warning on PHP4 that does not support entity decoding to UTF8;
$ret =@ html_entity_decode ( $a , $b , $c );
return $ret ;
2010-08-10 01:57:45 +02:00
}
/**
* Replace htmlentities functions to manage errors
2011-09-02 23:56:37 +02:00
*
2011-12-28 01:19:52 +01:00
* @ param string $a Operand a
* @ param string $b Operand b
* @ param string $c Operand c
* @ return string String encoded
2010-08-10 01:57:45 +02:00
*/
2012-03-18 00:59:24 +01:00
function dol_htmlentities ( $a , $b , $c = 'UTF-8' )
2010-08-10 01:57:45 +02:00
{
2012-08-03 23:29:08 +02:00
// We use @ to avoid warning on PHP4 that does not support entity decoding to UTF8;
$ret =@ htmlentities ( $a , $b , $c );
return $ret ;
2010-08-10 01:57:45 +02:00
}
2008-08-06 16:50:06 +02:00
/**
2011-05-03 21:03:08 +02:00
* Check if a string is a correct iso string
* If not , it will we considered not HTML encoded even if it is by FPDF .
2011-09-02 23:56:37 +02:00
* Example , if string contains euro symbol that has ascii code 128
*
2011-12-28 01:19:52 +01:00
* @ param string $s String to check
* @ return int 0 if bad iso , 1 if good iso
2008-08-06 16:50:06 +02:00
*/
function dol_string_is_good_iso ( $s )
{
2012-08-03 23:29:08 +02:00
$len = dol_strlen ( $s );
$ok = 1 ;
for ( $scursor = 0 ; $scursor < $len ; $scursor ++ )
{
$ordchar = ord ( $s { $scursor });
//print $scursor.'-'.$ordchar.'<br>';
if ( $ordchar < 32 && $ordchar != 13 && $ordchar != 10 ) { $ok = 0 ; break ; }
if ( $ordchar > 126 && $ordchar < 160 ) { $ok = 0 ; break ; }
}
return $ok ;
2008-08-06 16:50:06 +02:00
}
/**
2011-05-03 21:03:08 +02:00
* Return nb of lines of a clear text
2011-09-02 23:56:37 +02:00
*
2012-02-12 16:50:58 +01:00
* @ param string $s String to check
* @ param string $maxchar Not yet used
* @ return int Number of lines
2008-08-06 16:50:06 +02:00
*/
function dol_nboflines ( $s , $maxchar = 0 )
{
2012-08-03 23:29:08 +02:00
if ( $s == '' ) return 0 ;
$arraystring = explode ( " \n " , $s );
$nb = count ( $arraystring );
2008-10-09 19:29:32 +02:00
2012-08-03 23:29:08 +02:00
return $nb ;
2008-08-06 16:50:06 +02:00
}
/**
2012-06-14 19:58:35 +02:00
* Return nb of lines of a formated text with \n and < br > ( we can ' t have both \n and br )
2011-09-02 23:56:37 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $text Text
* @ param int $maxlinesize Largeur de ligne en caracteres ( ou 0 si pas de limite - defaut )
* @ param string $charset Give the charset used to encode the $text variable in memory .
* @ return int Number of lines
2008-08-06 16:50:06 +02:00
*/
2011-07-04 10:38:51 +02:00
function dol_nboflines_bis ( $text , $maxlinesize = 0 , $charset = 'UTF-8' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
$repTable = array ( " \t " => " " , " \n " => " <br> " , " \r " => " " , " \0 " => " " , " \x0B " => " " );
if ( dol_textishtml ( $text )) $repTable = array ( " \t " => " " , " \n " => " " , " \r " => " " , " \0 " => " " , " \x0B " => " " );
$text = strtr ( $text , $repTable );
if ( $charset == 'UTF-8' ) { $pattern = '/(<br[^>]*>)/Uu' ; } // /U is to have UNGREEDY regex to limit to one html tag. /u is for UTF8 support
else $pattern = '/(<br[^>]*>)/U' ; // /U is to have UNGREEDY regex to limit to one html tag.
$a = preg_split ( $pattern , $text , - 1 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
$nblines = floor (( count ( $a ) + 1 ) / 2 );
// count possible auto line breaks
if ( $maxlinesize )
{
foreach ( $a as $line )
{
if ( dol_strlen ( $line ) > $maxlinesize )
{
//$line_dec = html_entity_decode(strip_tags($line));
$line_dec = html_entity_decode ( $line );
if ( dol_strlen ( $line_dec ) > $maxlinesize )
{
$line_dec = wordwrap ( $line_dec , $maxlinesize , '\n' , true );
$nblines += substr_count ( $line_dec , '\n' );
}
}
}
}
return $nblines ;
2008-08-06 16:50:06 +02:00
}
/**
2011-05-03 21:03:08 +02:00
* Same function than microtime in PHP 5 but compatible with PHP4
2011-09-02 23:56:37 +02:00
*
2011-05-03 21:03:08 +02:00
* @ return float Time ( millisecondes ) with microsecondes in decimal part
2008-08-06 16:50:06 +02:00
*/
function dol_microtime_float ()
{
2012-08-03 23:29:08 +02:00
list ( $usec , $sec ) = explode ( " " , microtime ());
return (( float ) $usec + ( float ) $sec );
2008-08-06 16:50:06 +02:00
}
2011-05-25 15:27:07 +02:00
/**
2012-09-16 20:23:57 +02:00
* Return if a text is a html content
2011-09-02 23:56:37 +02:00
*
2012-09-16 20:23:57 +02:00
* @ param string $msg Content to check
* @ param int $option 0 = Full detection , 1 = Fast check
* @ return boolean true / false
* @ see dol_concatdesc
2008-08-06 16:50:06 +02:00
*/
function dol_textishtml ( $msg , $option = 0 )
{
2012-08-03 23:29:08 +02:00
if ( $option == 1 )
{
if ( preg_match ( '/<html/i' , $msg )) return true ;
elseif ( preg_match ( '/<body/i' , $msg )) return true ;
elseif ( preg_match ( '/<br/i' , $msg )) return true ;
return false ;
}
else
{
if ( preg_match ( '/<html/i' , $msg )) return true ;
elseif ( preg_match ( '/<body/i' , $msg )) return true ;
elseif ( preg_match ( '/<br/i' , $msg )) return true ;
elseif ( preg_match ( '/<span/i' , $msg )) return true ;
elseif ( preg_match ( '/<div/i' , $msg )) return true ;
elseif ( preg_match ( '/<li/i' , $msg )) return true ;
elseif ( preg_match ( '/<table/i' , $msg )) return true ;
elseif ( preg_match ( '/<font/i' , $msg )) return true ;
elseif ( preg_match ( '/<strong/i' , $msg )) return true ;
elseif ( preg_match ( '/<img/i' , $msg )) return true ;
elseif ( preg_match ( '/<i>/i' , $msg )) return true ;
elseif ( preg_match ( '/<b>/i' , $msg )) return true ;
elseif ( preg_match ( '/&[A-Z0-9]{1,6};/i' , $msg )) return true ; // Html entities names (http://www.w3schools.com/tags/ref_entities.asp)
elseif ( preg_match ( '/&#[0-9]{2,3};/i' , $msg )) return true ; // Html entities numbers (http://www.w3schools.com/tags/ref_entities.asp)
return false ;
}
2008-08-06 16:50:06 +02:00
}
2012-09-16 20:23:57 +02:00
/**
* Concat 2 descriptions ( second one after first one )
* text1 html + text2 html => text1 + '<br>' + text2
* text1 html + text2 txt => text1 + '<br>' + dol_nl2br ( text2 )
* text1 txt + text2 html => dol_nl2br ( text1 ) + '<br>' + text2
* text1 txt + text2 txt => text1 + '\n' + text2
*
* @ param string $text1 Text 1
* @ param string $text2 Text 2
* @ param string $forxml false = Use < br > , true = Use < br />
* @ return string Text 1 + new line + Text2
* @ see dol_textishtml
*/
function dol_concatdesc ( $text1 , $text2 , $forxml = false )
{
$ret = '' ;
2012-09-17 19:33:44 +02:00
$ret .= ( ! dol_textishtml ( $text1 ) && dol_textishtml ( $text2 )) ? dol_nl2br ( $text1 , 0 , $forxml ) : $text1 ;
$ret .= ( ! empty ( $text1 ) && ! empty ( $text2 )) ? (( dol_textishtml ( $text1 ) || dol_textishtml ( $text2 )) ? ( $forxml ? " <br \ > \n " : " <br> \n " ) : " \n " ) : " " ;
2012-09-16 20:23:57 +02:00
$ret .= ( dol_textishtml ( $text1 ) && ! dol_textishtml ( $text2 )) ? dol_nl2br ( $text2 , 0 , $forxml ) : $text2 ;
2012-09-17 19:33:44 +02:00
return $ret ;
2012-09-16 20:23:57 +02:00
}
2008-11-16 02:09:04 +01:00
/**
2012-02-22 16:18:03 +01:00
* Make substition into a string
* There is two type of substitions :
* - From $substitutionarray ( oldval => newval )
* - From special constants ( __XXX__ => f ( objet -> xxx )) by substitutions modules
2011-10-03 18:10:50 +02:00
*
2012-02-22 16:18:03 +01:00
* @ param string $chaine Source string in which we must do substitution
* @ param array $substitutionarray Array with key -> val to substitute
* @ return string Output string after subsitutions
2012-02-27 00:58:00 +01:00
* @ see complete_substitutions_array
2008-08-06 16:50:06 +02:00
*/
2011-06-09 00:08:02 +02:00
function make_substitutions ( $chaine , $substitutionarray )
2008-08-06 16:50:06 +02:00
{
2012-09-06 19:07:51 +02:00
global $conf ;
2012-08-03 23:29:08 +02:00
if ( ! is_array ( $substitutionarray )) return 'ErrorBadParameterSubstitutionArrayWhenCalling_make_substitutions' ;
// Make substitition
foreach ( $substitutionarray as $key => $value )
{
2012-12-31 13:40:13 +01:00
if ( $key == '__SIGNATURE__' && ( ! empty ( $conf -> global -> MAIN_MAIL_DO_NOT_USE_SIGN ))) $value = '' ;
2012-08-03 23:29:08 +02:00
$chaine = str_replace ( " $key " , " $value " , $chaine ); // We must keep the " to work when value is 123.5 for example
}
2012-09-06 19:07:51 +02:00
2012-08-03 23:29:08 +02:00
return $chaine ;
2008-08-06 16:50:06 +02:00
}
2011-06-08 23:11:52 +02:00
/**
2012-02-12 16:50:58 +01:00
* Complete the $substitutionarray with more entries
2011-08-17 18:07:41 +02:00
*
2012-02-22 16:18:03 +01:00
* @ param array & $substitutionarray Array substitution old value => new value value
* @ param Translate $outputlangs If we want substitution from special constants , we provide a language
* @ param Object $object If we want substitution from special constants , we provide data in a source object
2012-08-14 16:03:45 +02:00
* @ param Object / array $parameters Add more parameters ( useful to pass product lines )
* @ param string $callfunc What is the name of the custom function that will be called ? ( default : completesubstitutionarray )
2012-02-12 16:50:58 +01:00
* @ return void
2012-02-22 16:18:03 +01:00
* @ see make_substitutions
2011-06-08 23:11:52 +02:00
*/
2012-08-14 16:03:45 +02:00
function complete_substitutions_array ( & $substitutionarray , $outputlangs , $object = '' , $parameters = null , $callfunc = " completesubstitutionarray " )
2011-06-08 23:11:52 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $user ;
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2012-08-03 23:29:08 +02:00
// Check if there is external substitution to do asked by plugins
$dirsubstitutions = array_merge ( array (),( array ) $conf -> modules_parts [ 'substitutions' ]);
foreach ( $dirsubstitutions as $reldir )
{
$dir = dol_buildpath ( $reldir , 0 );
// Check if directory exists
if ( ! dol_is_dir ( $dir )) continue ;
$substitfiles = dol_dir_list ( $dir , 'files' , 0 , 'functions_' );
foreach ( $substitfiles as $substitfile )
{
if ( preg_match ( '/functions_(.*)\.lib\.php/i' , $substitfile [ 'name' ], $reg ))
{
$module = $reg [ 1 ];
dol_syslog ( " Library functions_ " . $substitfile [ 'name' ] . " found into " . $dir );
2012-08-22 23:11:24 +02:00
require_once $dir . $substitfile [ 'name' ];
2012-08-14 16:03:45 +02:00
$function_name = $module . " _ " . $callfunc ;
2012-09-11 20:15:03 +02:00
if ( function_exists ( $function_name )) $function_name ( $substitutionarray , $outputlangs , $object , $parameters );
2012-08-03 23:29:08 +02:00
}
}
}
2011-06-08 23:11:52 +02:00
}
2008-10-17 02:24:44 +02:00
/**
2010-08-29 14:54:39 +02:00
* Format output for start and end date
2011-08-17 18:07:41 +02:00
*
2011-10-29 18:15:54 +02:00
* @ param timestamp $date_start Start date
* @ param timestamp $date_end End date
* @ param string $format Output format
* @ param Translate $outputlangs Output language
2011-10-03 18:10:50 +02:00
* @ return void
2008-08-06 16:50:06 +02:00
*/
2008-10-29 00:36:36 +01:00
function print_date_range ( $date_start , $date_end , $format = '' , $outputlangs = '' )
2010-12-17 11:08:31 +01:00
{
2012-08-03 23:29:08 +02:00
print get_date_range ( $date_start , $date_end , $format , $outputlangs );
2010-12-17 11:08:31 +01:00
}
/**
* Format output for start and end date
2011-08-17 18:07:41 +02:00
*
2011-10-29 18:15:54 +02:00
* @ param timestamp $date_start Start date
* @ param timestamp $date_end End date
* @ param string $format Output format
* @ param Translate $outputlangs Output language
* @ return string String
2010-12-17 11:08:31 +01:00
*/
function get_date_range ( $date_start , $date_end , $format = '' , $outputlangs = '' )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $langs ;
$out = '' ;
if ( ! is_object ( $outputlangs )) $outputlangs = $langs ;
if ( $date_start && $date_end )
{
$out .= ' (' . $outputlangs -> trans ( 'DateFromTo' , dol_print_date ( $date_start , $format , false , $outputlangs ), dol_print_date ( $date_end , $format , false , $outputlangs )) . ')' ;
}
if ( $date_start && ! $date_end )
{
$out .= ' (' . $outputlangs -> trans ( 'DateFrom' , dol_print_date ( $date_start , $format , false , $outputlangs )) . ')' ;
}
if ( ! $date_start && $date_end )
{
$out .= ' (' . $outputlangs -> trans ( 'DateUntil' , dol_print_date ( $date_end , $format , false , $outputlangs )) . ')' ;
}
return $out ;
2008-08-06 16:50:06 +02:00
}
2012-07-28 22:07:27 +02:00
/**
2012-07-29 08:26:33 +02:00
* Set event message in dol_events session
2012-07-28 22:07:27 +02:00
*
2012-10-17 18:47:18 +02:00
* @ param mixed $mesgs Message string or array
* @ param string $style Which style to use ( 'mesgs' , 'warnings' , 'errors' )
2012-07-28 22:07:27 +02:00
* @ return void
2012-07-29 08:26:33 +02:00
* @ see dol_htmloutput_events
*/
2012-10-17 18:47:18 +02:00
function setEventMessage ( $mesgs , $style = 'mesgs' )
2012-07-29 08:26:33 +02:00
{
2012-11-14 18:56:47 +01:00
if ( ! in_array (( string ) $style , array ( 'mesgs' , 'warnings' , 'errors' ))) dol_print_error ( '' , 'Bad parameter for setEventMessage' );
2012-10-17 18:47:18 +02:00
if ( ! is_array ( $mesgs )) // If mesgs is a string
{
2012-10-17 19:02:21 +02:00
if ( $mesgs ) $_SESSION [ 'dol_events' ][ $style ][] = $mesgs ;
2012-10-17 18:47:18 +02:00
}
else // If mesgs is an array
{
foreach ( $mesgs as $mesg )
{
2012-10-17 19:02:21 +02:00
if ( $mesg ) $_SESSION [ 'dol_events' ][ $style ][] = $mesg ;
2012-10-17 18:47:18 +02:00
}
}
2012-07-29 08:26:33 +02:00
}
/**
* Print formated messages to output ( Used to show messages on html output ) .
2012-07-28 22:07:27 +02:00
*
2012-07-29 08:26:33 +02:00
* @ return void
2012-07-28 22:07:27 +02:00
* @ see dol_htmloutput_mesg
*/
2012-07-29 08:26:33 +02:00
function dol_htmloutput_events ()
2012-07-28 22:07:27 +02:00
{
2012-07-28 22:28:33 +02:00
// Show mesgs
2012-07-29 08:26:33 +02:00
if ( isset ( $_SESSION [ 'dol_events' ][ 'mesgs' ])) {
dol_htmloutput_mesg ( '' , $_SESSION [ 'dol_events' ][ 'mesgs' ]);
unset ( $_SESSION [ 'dol_events' ][ 'mesgs' ]);
2012-07-28 22:28:33 +02:00
}
// Show errors
2012-07-29 08:26:33 +02:00
if ( isset ( $_SESSION [ 'dol_events' ][ 'errors' ])) {
dol_htmloutput_mesg ( '' , $_SESSION [ 'dol_events' ][ 'errors' ], 'error' );
unset ( $_SESSION [ 'dol_events' ][ 'errors' ]);
2012-07-28 22:28:33 +02:00
}
// Show warnings
2012-07-29 08:26:33 +02:00
if ( isset ( $_SESSION [ 'dol_events' ][ 'warnings' ])) {
dol_htmloutput_mesg ( '' , $_SESSION [ 'dol_events' ][ 'warnings' ], 'warning' );
unset ( $_SESSION [ 'dol_events' ][ 'warnings' ]);
2012-07-28 22:07:27 +02:00
}
}
2008-08-06 16:50:06 +02:00
/**
2011-08-17 18:07:41 +02:00
* Get formated messages to output ( Used to show messages on html output ) .
*
2011-10-03 18:10:50 +02:00
* @ param string $mesgstring Message string
* @ param array $mesgarray Messages array
* @ param string $style Style of message output ( 'ok' or 'error' )
* @ param int $keepembedded Set to 1 in error message must be kept embedded into its html place ( this disable jnotify )
* @ return string Return html output
*
2011-12-28 01:19:52 +01:00
* @ see dol_print_error
* @ see dol_htmloutput_errors
2008-08-06 16:50:06 +02:00
*/
2011-05-14 13:34:15 +02:00
function get_htmloutput_mesg ( $mesgstring = '' , $mesgarray = '' , $style = 'ok' , $keepembedded = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
global $conf , $langs ;
$ret = '' ;
$out = '' ;
$divstart = $divend = '' ;
// If inline message with no format, we add it.
if (( empty ( $conf -> use_javascript_ajax ) || ! empty ( $conf -> global -> MAIN_DISABLE_JQUERY_JNOTIFY ) || $keepembedded ) && ! preg_match ( '/<div class=".*">/i' , $out ))
{
$divstart = '<div class="' . $style . '">' ;
$divend = '</div>' ;
}
if (( is_array ( $mesgarray ) && count ( $mesgarray )) || $mesgstring )
{
$langs -> load ( " errors " );
$out .= $divstart ;
if ( is_array ( $mesgarray ) && count ( $mesgarray ))
{
foreach ( $mesgarray as $message )
{
$ret ++ ;
$out .= $langs -> trans ( $message );
if ( $ret < count ( $mesgarray )) $out .= " <br> \n " ;
}
}
if ( $mesgstring )
{
$langs -> load ( " errors " );
$ret ++ ;
$out .= $langs -> trans ( $mesgstring );
}
$out .= $divend ;
}
if ( $out )
{
2012-09-03 22:05:13 +02:00
if ( ! empty ( $conf -> use_javascript_ajax ) && empty ( $conf -> global -> MAIN_DISABLE_JQUERY_JNOTIFY ) && empty ( $keepembedded ))
2012-08-03 23:29:08 +02:00
{
$return = ' < script type = " text/javascript " >
2012-09-03 22:05:13 +02:00
$ ( document ) . ready ( function () {
var block = '.(! empty($conf->global->MAIN_USE_JQUERY_BLOCKUI)?"true":"false").'
if ( block ) {
2012-09-04 09:38:38 +02:00
$ . dolEventValid ( " " , " '.dol_escape_js( $out ).' " );
2012-09-03 22:05:13 +02:00
} else {
$ . jnotify ( " '.dol_escape_js( $out ).' " ,
" '.( $style == " ok " ? 3000 : $style ).' " ,
'.($style=="ok" ? "false" : "true").' ,
{ remove : function (){} } );
}
2012-08-03 23:29:08 +02:00
});
</ script > ' ;
}
else
{
$return = $out ;
}
}
return $return ;
2011-05-14 13:34:15 +02:00
}
/**
2011-08-17 18:07:41 +02:00
* Get formated error messages to output ( Used to show messages on html output ) .
*
2011-10-03 18:10:50 +02:00
* @ param string $mesgstring Error message
* @ param array $mesgarray Error messages array
* @ param int $keepembedded Set to 1 in error message must be kept embedded into its html place ( this disable jnotify )
* @ return string Return html output
*
2011-12-28 01:19:52 +01:00
* @ see dol_print_error
* @ see dol_htmloutput_mesg
2011-05-14 13:34:15 +02:00
*/
function get_htmloutput_errors ( $mesgstring = '' , $mesgarray = '' , $keepembedded = 0 )
{
2012-08-03 23:29:08 +02:00
return get_htmloutput_mesg ( $mesgstring , $mesgarray , 'error' , $keepembedded );
2011-05-14 13:34:15 +02:00
}
/**
2011-08-17 18:07:41 +02:00
* Print formated messages to output ( Used to show messages on html output ) .
*
2011-12-05 23:05:11 +01:00
* @ param string $mesgstring Message
2011-10-03 18:10:50 +02:00
* @ param array $mesgarray Messages array
2012-01-26 23:05:59 +01:00
* @ param string $style Which style to use ( 'ok' , 'warning' , 'error' )
2011-10-03 18:10:50 +02:00
* @ param int $keepembedded Set to 1 if message must be kept embedded into its html place ( this disable jnotify )
* @ return void
*
2011-12-28 01:19:52 +01:00
* @ see dol_print_error
* @ see dol_htmloutput_errors
2011-05-14 13:34:15 +02:00
*/
function dol_htmloutput_mesg ( $mesgstring = '' , $mesgarray = '' , $style = 'ok' , $keepembedded = 0 )
{
2012-08-03 23:29:08 +02:00
if ( empty ( $mesgstring ) && ( ! is_array ( $mesgarray ) || count ( $mesgarray ) == 0 )) return ;
$iserror = 0 ;
$iswarning = 0 ;
if ( is_array ( $mesgarray ))
{
foreach ( $mesgarray as $val )
{
if ( $val && preg_match ( '/class="error"/i' , $val )) { $iserror ++ ; break ; }
if ( $val && preg_match ( '/class="warning"/i' , $val )) { $iswarning ++ ; break ; }
}
}
else if ( $mesgstring && preg_match ( '/class="error"/i' , $mesgstring )) $iserror ++ ;
else if ( $mesgstring && preg_match ( '/class="warning"/i' , $mesgstring )) $iswarning ++ ;
if ( $style == 'error' ) $iserror ++ ;
if ( $style == 'warning' ) $iswarning ++ ;
if ( $iserror || $iswarning )
{
// Remove div from texts
$mesgstring = preg_replace ( '/<\/div><div class="(error|warning)">/' , '<br>' , $mesgstring );
$mesgstring = preg_replace ( '/<div class="(error|warning)">/' , '' , $mesgstring );
$mesgstring = preg_replace ( '/<\/div>/' , '' , $mesgstring );
// Remove div from texts array
if ( is_array ( $mesgarray ))
{
$newmesgarray = array ();
foreach ( $mesgarray as $val )
{
$tmpmesgstring = preg_replace ( '/<\/div><div class="(error|warning)">/' , '<br>' , $val );
$tmpmesgstring = preg_replace ( '/<div class="(error|warning)">/' , '' , $tmpmesgstring );
$tmpmesgstring = preg_replace ( '/<\/div>/' , '' , $tmpmesgstring );
$newmesgarray [] = $tmpmesgstring ;
}
$mesgarray = $newmesgarray ;
}
print get_htmloutput_mesg ( $mesgstring , $mesgarray ,( $iserror ? 'error' : 'warning' ), $keepembedded );
}
else print get_htmloutput_mesg ( $mesgstring , $mesgarray , 'ok' , $keepembedded );
2008-08-06 16:50:06 +02:00
}
2011-01-30 18:15:51 +01:00
/**
2011-08-17 18:07:41 +02:00
* Print formated error messages to output ( Used to show messages on html output ) .
*
2011-10-03 18:10:50 +02:00
* @ param string $mesgstring Error message
* @ param array $mesgarray Error messages array
* @ param int $keepembedded Set to 1 in error message must be kept embedded into its html place ( this disable jnotify )
* @ return void
*
2011-12-28 01:19:52 +01:00
* @ see dol_print_error
* @ see dol_htmloutput_mesg
2011-01-30 18:15:51 +01:00
*/
2011-05-04 21:06:33 +02:00
function dol_htmloutput_errors ( $mesgstring = '' , $mesgarray = '' , $keepembedded = 0 )
2011-01-30 18:15:51 +01:00
{
2012-08-03 23:29:08 +02:00
dol_htmloutput_mesg ( $mesgstring , $mesgarray , 'error' , $keepembedded );
2011-01-30 18:15:51 +01:00
}
2008-08-06 16:50:06 +02:00
/**
2011-08-29 23:26:42 +02:00
* Advanced sort array by second index function , which produces ascending ( default )
* or descending output and uses optionally natural case insensitive sorting ( which
* can be optionally case sensitive as well ) .
2011-08-17 18:07:41 +02:00
*
2012-09-08 21:29:03 +02:00
* @ param array & $array Array to sort ( array of array ( 'key' , 'otherkey1' , 'otherkey2' ... ))
2011-08-29 23:26:42 +02:00
* @ param string $index Key in array to use for sorting criteria
* @ param int $order Sort order
* @ param int $natsort 1 = use " natural " sort ( natsort ), 0 = use " standard sort (asort)
* @ param int $case_sensitive 1 = sort is case sensitive , 0 = not case sensitive
* @ return array Sorted array
*/
function dol_sort_array ( & $array , $index , $order = 'asc' , $natsort = 0 , $case_sensitive = 0 )
2008-08-06 16:50:06 +02:00
{
2012-08-03 23:29:08 +02:00
// Clean parameters
$order = strtolower ( $order );
$sizearray = count ( $array );
if ( is_array ( $array ) && $sizearray > 0 )
{
foreach ( array_keys ( $array ) as $key ) $temp [ $key ] = $array [ $key ][ $index ];
if ( ! $natsort ) ( $order == 'asc' ) ? asort ( $temp ) : arsort ( $temp );
else
{
( $case_sensitive ) ? natsort ( $temp ) : natcasesort ( $temp );
if ( $order != 'asc' ) $temp = array_reverse ( $temp , TRUE );
}
foreach ( array_keys ( $temp ) as $key ) ( is_numeric ( $key )) ? $sorted [] = $array [ $key ] : $sorted [ $key ] = $array [ $key ];
return $sorted ;
}
return $array ;
2008-08-06 16:50:06 +02:00
}
2008-10-09 19:29:32 +02:00
/**
2010-09-29 10:05:22 +02:00
* Check if a string is in UTF8
2011-08-17 18:07:41 +02:00
*
2011-10-03 18:10:50 +02:00
* @ param string $str String to check
* @ return boolean True if string is UTF8 or ISO compatible with UTF8 , False if not ( ISO with special char or Binary )
2008-10-09 19:29:32 +02:00
*/
2010-09-29 10:05:22 +02:00
function utf8_check ( $str )
2008-10-09 19:29:32 +02:00
{
2012-08-03 23:29:08 +02:00
// We must use here a binary strlen function (so not dol_strlen)
$strLength = dol_strlen ( $str );
for ( $i = 0 ; $i < $strLength ; $i ++ )
{
if ( ord ( $str [ $i ]) < 0x80 ) continue ; // 0bbbbbbb
elseif (( ord ( $str [ $i ]) & 0xE0 ) == 0xC0 ) $n = 1 ; // 110bbbbb
elseif (( ord ( $str [ $i ]) & 0xF0 ) == 0xE0 ) $n = 2 ; // 1110bbbb
elseif (( ord ( $str [ $i ]) & 0xF8 ) == 0xF0 ) $n = 3 ; // 11110bbb
elseif (( ord ( $str [ $i ]) & 0xFC ) == 0xF8 ) $n = 4 ; // 111110bb
elseif (( ord ( $str [ $i ]) & 0xFE ) == 0xFC ) $n = 5 ; // 1111110b
else return false ; // Does not match any model
for ( $j = 0 ; $j < $n ; $j ++ ) { // n bytes matching 10bbbbbb follow ?
if (( ++ $i == strlen ( $str )) || (( ord ( $str [ $i ]) & 0xC0 ) != 0x80 ))
return false ;
}
}
return true ;
2008-10-09 19:29:32 +02:00
}
2009-10-20 17:23:32 +02:00
2009-12-14 23:17:50 +01:00
/**
2010-10-02 12:48:28 +02:00
* Return an UTF - 8 string encoded into OS filesystem encoding . This function is used to define
2011-03-09 16:48:25 +01:00
* value to pass to filesystem PHP functions .
2011-08-17 18:07:41 +02:00
*
2011-10-03 18:10:50 +02:00
* @ param string $str String to encode ( UTF - 8 )
* @ return string Encoded string ( UTF - 8 , ISO - 8859 - 1 )
2009-12-14 23:17:50 +01:00
*/
function dol_osencode ( $str )
{
2012-08-03 23:29:08 +02:00
global $conf ;
2011-09-20 13:43:14 +02:00
2012-08-03 23:29:08 +02:00
$tmp = ini_get ( " unicode.filesystem_encoding " ); // Disponible avec PHP 6.0
if ( empty ( $tmp ) && ! empty ( $_SERVER [ " WINDIR " ])) $tmp = 'iso-8859-1' ; // By default for windows
if ( empty ( $tmp )) $tmp = 'utf-8' ; // By default for other
if ( ! empty ( $conf -> global -> MAIN_FILESYSTEM_ENCODING )) $tmp = $conf -> global -> MAIN_FILESYSTEM_ENCODING ;
2009-12-14 23:17:50 +01:00
2012-08-03 23:29:08 +02:00
if ( $tmp == 'iso-8859-1' ) return utf8_decode ( $str );
return $str ;
2009-12-14 23:17:50 +01:00
}
2009-10-20 17:23:32 +02:00
/**
2011-08-20 22:53:31 +02:00
* Return an id or code from a code or id . Store Code - Id in a cache .
2011-08-17 18:07:41 +02:00
*
2011-10-03 18:10:50 +02:00
* @ param DoliDB $db Database handler
* @ param string $key Code to get Id
* @ param string $tablename Table name without prefix
* @ param string $fieldkey Field for code
* @ param string $fieldid Field for id
* @ return int Id of code
2009-10-20 17:23:32 +02:00
*/
function dol_getIdFromCode ( $db , $key , $tablename , $fieldkey = 'code' , $fieldid = 'id' )
{
2012-08-03 23:29:08 +02:00
global $cache_codes ;
// If key empty
if ( $key == '' ) return '' ;
// Check in cache
if ( isset ( $cache_codes [ $tablename ][ $key ])) // Can be defined to 0 or ''
{
return $cache_codes [ $tablename ][ $key ]; // Found in cache
}
$sql = " SELECT " . $fieldid . " as id " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $tablename ;
$sql .= " WHERE " . $fieldkey . " = ' " . $key . " ' " ;
dol_syslog ( 'dol_getIdFromCode sql=' . $sql , LOG_DEBUG );
$resql = $db -> query ( $sql );
if ( $resql )
{
$obj = $db -> fetch_object ( $resql );
if ( $obj ) $cache_codes [ $tablename ][ $key ] = $obj -> id ;
else $cache_codes [ $tablename ][ $key ] = '' ;
$db -> free ( $resql );
return $cache_codes [ $tablename ][ $key ];
}
else
{
dol_syslog ( " dol_getIdFromCode error= " . $db -> lasterror (), LOG_ERR );
return - 1 ;
}
2009-10-20 17:23:32 +02:00
}
2010-03-25 12:16:42 +01:00
/**
* Verify if condition in string is ok or not
2011-08-17 18:07:41 +02:00
*
2011-10-03 18:10:50 +02:00
* @ param string $strRights String with condition to check
2012-02-12 16:41:43 +01:00
* @ return boolean True or False . Return true if strRights is ''
2010-03-25 12:16:42 +01:00
*/
function verifCond ( $strRights )
{
2012-08-03 23:29:08 +02:00
global $user , $conf , $langs ;
global $leftmenu ;
global $rights ; // To export to dol_eval function
//print $strRights."<br>\n";
$rights = true ;
if ( $strRights != '' )
{
//$tab_rights = explode('&&', $strRights);
//$i = 0;
//while (($i < count($tab_rights)) && ($rights == true)) {
$str = 'if(!(' . $strRights . ')) { $rights = false; }' ;
dol_eval ( $str );
// $i++;
//}
}
return $rights ;
2010-03-25 12:16:42 +01:00
}
/**
2011-02-16 23:46:01 +01:00
* Replace eval function to add more security .
2012-11-04 21:19:12 +01:00
* This function is called by verifCond () or trans () and transnoentitiesnoconv () .
2011-09-02 23:56:37 +02:00
*
2012-11-04 21:19:12 +01:00
* @ param string $s String to evaluate
* @ param int $returnvalue 0 = No return ( used to execute $a = something ) . 1 = Value of eval is returned ( used to eval $something ) .
* @ return mixed Nothing or return of eval
2010-03-25 12:16:42 +01:00
*/
2012-11-04 21:19:12 +01:00
function dol_eval ( $s , $returnvalue = 0 )
2010-03-25 12:16:42 +01:00
{
2012-08-03 23:29:08 +02:00
// Only global variables can be changed by eval function and returned to caller
global $langs , $user , $conf ;
global $leftmenu ;
global $rights ;
2010-03-25 12:16:42 +01:00
2012-08-03 23:29:08 +02:00
//print $s."<br>\n";
2012-11-04 21:19:12 +01:00
if ( $returnvalue ) return eval ( 'return ' . $s . ';' );
else eval ( $s );
2010-03-25 12:16:42 +01:00
}
2011-10-27 10:41:29 +02:00
/**
* Return if var element is ok
*
* @ param string $element Variable to check
* @ return boolean Return true of variable is not empty
*/
function dol_validElement ( $element )
{
return ( trim ( $element ) != '' );
}
2010-08-29 19:43:51 +02:00
2010-06-26 16:35:26 +02:00
/**
2010-08-29 19:43:51 +02:00
* Return img flag of country for a language code or country code
2011-08-17 18:07:41 +02:00
*
2011-10-03 18:10:50 +02:00
* @ param string $codelang Language code ( en_IN , fr_CA ... ) or Country code ( IN , FR )
* @ return string HTML img string with flag .
2010-06-26 16:35:26 +02:00
*/
function picto_from_langcode ( $codelang )
{
2012-08-03 23:29:08 +02:00
global $langs ;
if ( $codelang == 'auto' )
{
return img_picto_common ( $langs -> trans ( 'AutoDetectLang' ), 'flags/int.png' );
}
$langtocountryflag = array (
'ar_AR' => '' ,
'ca_ES' => 'catalonia' ,
'da_DA' => 'dk' ,
'fr_CA' => 'mq' ,
'sv_SV' => 'se'
);
if ( isset ( $langtocountryflag [ $codelang ])) $flagImage = $langtocountryflag [ $codelang ];
else
{
$tmparray = explode ( '_' , $codelang );
$flagImage = empty ( $tmparray [ 1 ]) ? $tmparray [ 0 ] : $tmparray [ 1 ];
}
return img_picto_common ( $codelang , 'flags/' . strtolower ( $flagImage ) . '.png' );
2010-06-26 16:35:26 +02:00
}
2011-01-25 00:35:21 +01:00
/**
2011-12-21 18:31:39 +01:00
* Complete or removed entries into a head array ( used to build tabs ) with value added by external modules .
* Such values are declared into $conf -> tabs_modules .
2011-08-17 18:07:41 +02:00
*
2011-11-02 14:14:46 +01:00
* @ param Conf $conf Object conf
* @ param Translate $langs Object langs
* @ param Object $object Object object
2012-02-04 15:20:32 +01:00
* @ param array & $head Object head
2012-02-04 14:39:47 +01:00
* @ param int & $h New position to fill
2011-11-02 14:14:46 +01:00
* @ param string $type Value for object where objectvalue can be
* 'thirdparty' to add a tab in third party view
* 'intervention' to add a tab in intervention view
* 'supplier_order' to add a tab in supplier order view
* 'supplier_invoice' to add a tab in supplier invoice view
* 'invoice' to add a tab in customer invoice view
* 'order' to add a tab in customer order view
* 'product' to add a tab in product view
* 'propal' to add a tab in propal view
* 'user' to add a tab in user view
* 'group' to add a tab in group view
* 'member' to add a tab in fundation member view
* 'categories_x' to add a tab in category view ( 'x' : type of category ( 0 = product , 1 = supplier , 2 = customer , 3 = member )
* @ param string $mode 'add' to complete head , 'remove' to remove entries
* @ return void
2011-01-25 00:35:21 +01:00
*/
2011-01-25 00:59:37 +01:00
function complete_head_from_modules ( $conf , $langs , $object , & $head , & $h , $type , $mode = 'add' )
2011-01-25 00:35:21 +01:00
{
2012-07-08 23:22:22 +02:00
if ( isset ( $conf -> tabs_modules [ $type ]) && is_array ( $conf -> tabs_modules [ $type ]))
2011-12-20 11:47:35 +01:00
{
foreach ( $conf -> tabs_modules [ $type ] as $value )
{
$values = explode ( ':' , $value );
2011-12-21 18:31:39 +01:00
2011-12-20 11:47:35 +01:00
if ( $mode == 'add' && ! preg_match ( '/^\-/' , $values [ 1 ]))
{
2011-12-21 18:31:39 +01:00
if ( count ( $values ) == 6 ) // new declaration with permissions: $value='objecttype:+tabname1:Title1:langfile@mymodule:$user->rights->mymodule->read:/mymodule/mynewtab1.php?id=__ID__'
2011-12-20 11:47:35 +01:00
{
if ( $values [ 0 ] != $type ) continue ;
2011-12-21 18:31:39 +01:00
2011-12-20 11:47:35 +01:00
if ( verifCond ( $values [ 4 ]))
{
if ( $values [ 3 ]) $langs -> load ( $values [ 3 ]);
2012-09-17 16:28:37 +02:00
$head [ $h ][ 0 ] = dol_buildpath ( preg_replace ( '/__ID__/i' , ( ! empty ( $object -> id ) ? $object -> id : '' ), $values [ 5 ]), 1 );
2011-12-20 11:47:35 +01:00
$head [ $h ][ 1 ] = $langs -> trans ( $values [ 2 ]);
$head [ $h ][ 2 ] = str_replace ( '+' , '' , $values [ 1 ]);
$h ++ ;
}
}
else if ( count ( $values ) == 5 ) // new declaration
{
if ( $values [ 0 ] != $type ) continue ;
if ( $values [ 3 ]) $langs -> load ( $values [ 3 ]);
2012-09-17 16:28:37 +02:00
$head [ $h ][ 0 ] = dol_buildpath ( preg_replace ( '/__ID__/i' , ( ! empty ( $object -> id ) ? $object -> id : '' ), $values [ 4 ]), 1 );
2011-12-20 11:47:35 +01:00
$head [ $h ][ 1 ] = $langs -> trans ( $values [ 2 ]);
$head [ $h ][ 2 ] = str_replace ( '+' , '' , $values [ 1 ]);
$h ++ ;
}
else if ( count ( $values ) == 4 ) // old declaration, for backward compatibility
{
if ( $values [ 0 ] != $type ) continue ;
if ( $values [ 2 ]) $langs -> load ( $values [ 2 ]);
2012-09-17 16:28:37 +02:00
$head [ $h ][ 0 ] = dol_buildpath ( preg_replace ( '/__ID__/i' , ( ! empty ( $object -> id ) ? $object -> id : '' ), $values [ 3 ]), 1 );
2011-12-20 11:47:35 +01:00
$head [ $h ][ 1 ] = $langs -> trans ( $values [ 1 ]);
$head [ $h ][ 2 ] = 'tab' . $values [ 1 ];
$h ++ ;
}
}
2011-12-21 18:31:39 +01:00
else if ( $mode == 'remove' && preg_match ( '/^\-/' , $values [ 1 ]))
2011-12-20 11:47:35 +01:00
{
if ( $values [ 0 ] != $type ) continue ;
$tabname = str_replace ( '-' , '' , $values [ 1 ]);
foreach ( $head as $key => $val )
{
$condition = ( ! empty ( $values [ 3 ]) ? verifCond ( $values [ 3 ]) : 1 );
if ( $head [ $key ][ 2 ] == $tabname && $condition )
{
unset ( $head [ $key ]);
break ;
}
}
}
}
}
2011-01-25 00:35:21 +01:00
}
2011-10-18 16:40:44 +02:00
/**
* Print common footer :
* conf -> global -> MAIN_HTML_FOOTER
* conf -> global -> MAIN_GOOGLE_AN_ID
* DOL_TUNING
* conf -> logbuffer
*
* @ param string $zone 'private' ( for private pages ) or 'public' ( for public pages )
* @ return void
*/
function printCommonFooter ( $zone = 'private' )
{
2012-08-03 23:29:08 +02:00
global $conf ;
2011-12-05 17:13:48 +01:00
global $micro_start_time ;
2011-12-07 18:10:24 +01:00
2012-08-03 23:29:08 +02:00
if ( $zone == 'private' ) print " \n " . '<!-- Common footer for private page -->' . " \n " ;
else print " \n " . '<!-- Common footer for public page -->' . " \n " ;
if ( ! empty ( $conf -> global -> MAIN_HTML_FOOTER )) print $conf -> global -> MAIN_HTML_FOOTER . " \n " ;
// Google Analytics (need Google module)
if ( ! empty ( $conf -> global -> MAIN_GOOGLE_AN_ID ))
{
print " \n " ;
print '<script type="text/javascript">' . " \n " ;
print ' var _gaq = _gaq || [];' . " \n " ;
print ' _gaq.push([\'_setAccount\', \'' . $conf -> global -> MAIN_GOOGLE_AN_ID . '\']);' . " \n " ;
print ' _gaq.push([\'_trackPageview\']);' . " \n " ;
print '' . " \n " ;
print ' (function() {' . " \n " ;
print ' var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;' . " \n " ;
print ' ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';' . " \n " ;
print ' var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);' . " \n " ;
print ' })();' . " \n " ;
print '</script>' . " \n " ;
}
// End of tuning
if ( ! empty ( $_SERVER [ 'DOL_TUNING' ]))
{
$micro_end_time = dol_microtime_float ( true );
print " \n " . '<script type="text/javascript">' . " \n " ;
2013-03-04 21:00:22 +01:00
print 'window.console && console.log("' ;
2012-08-03 23:29:08 +02:00
if ( ! empty ( $conf -> global -> MEMCACHED_SERVER )) print 'MEMCACHED_SERVER=' . $conf -> global -> MEMCACHED_SERVER . ' - ' ;
print 'MAIN_OPTIMIZE_SPEED=' . ( isset ( $conf -> global -> MAIN_OPTIMIZE_SPEED ) ? $conf -> global -> MAIN_OPTIMIZE_SPEED : 'off' );
print ' - Build time: ' . ceil ( 1000 * ( $micro_end_time - $micro_start_time )) . ' ms' ;
if ( function_exists ( " memory_get_usage " ))
{
print ' - Mem: ' . memory_get_usage ();
}
if ( function_exists ( " xdebug_memory_usage " ))
{
print ' - XDebug time: ' . ceil ( 1000 * xdebug_time_index ()) . ' ms' ;
print ' - XDebug mem: ' . xdebug_memory_usage ();
print ' - XDebug mem peak: ' . xdebug_peak_memory_usage ();
}
if ( function_exists ( " zend_loader_file_encoded " ))
{
print ' - Zend encoded file: ' . ( zend_loader_file_encoded () ? 'yes' : 'no' );
}
print '");' . " \n " ;
print '</script>' . " \n " ;
// Add Xdebug coverage of code
if ( defined ( 'XDEBUGCOVERAGE' )) {
var_dump ( xdebug_get_code_coverage ());
}
}
// If there is some logs in buffer to show
if ( count ( $conf -> logbuffer ))
{
print " \n " ;
print " <!-- Start of log output \n " ;
//print '<div class="hidden">'."\n";
foreach ( $conf -> logbuffer as $logline )
{
print $logline . " <br> \n " ;
}
//print '</div>'."\n";
print " End of log output --> \n " ;
}
2011-10-18 16:40:44 +02:00
}
2012-04-08 13:05:58 +02:00
/**
* Convert an array with RGB value into hex RGB value
*
* @ param array $arraycolor Array
* @ param string $colorifnotfound Color code to return if entry not defined
* @ return string RGB hex value ( without # before). For example: FF00FF
*/
function colorArrayToHex ( $arraycolor , $colorifnotfound = '888888' )
{
2012-08-03 23:29:08 +02:00
if ( ! is_array ( $arraycolor )) return $colorifnotfound ;
return dechex ( $arraycolor [ 0 ]) . dechex ( $arraycolor [ 1 ]) . dechex ( $arraycolor [ 2 ]);
2012-04-08 13:05:58 +02:00
}
2012-07-02 19:30:37 +02:00
/**
* Convert a currency code into its symbol
*
* @ param string $currency_code Currency code
* @ return string Currency symbol encoded into UTF8
*/
function getCurrencySymbol ( $currency_code )
{
global $db , $form ;
$currency_sign = '' ;
if ( ! is_object ( $form )) $form = new Form ( $db );
$form -> load_cache_currencies ();
2012-07-10 11:45:38 +02:00
if ( function_exists ( " mb_convert_encoding " ) && isset ( $form -> cache_currencies [ $currency_code ]) && is_array ( $form -> cache_currencies [ $currency_code ][ 'unicode' ]) && ! empty ( $form -> cache_currencies [ $currency_code ][ 'unicode' ]))
2012-07-02 19:30:37 +02:00
{
foreach ( $form -> cache_currencies [ $currency_code ][ 'unicode' ] as $unicode )
{
2012-05-28 22:39:04 +02:00
$currency_sign .= mb_convert_encoding ( " &# { $unicode } ; " , " UTF-8 " , 'HTML-ENTITIES' );
2012-07-02 19:30:37 +02:00
}
}
else
{
$currency_sign = $currency_code ;
}
return $currency_sign ;
2012-03-08 11:49:38 +01:00
}
2012-11-29 11:36:59 +01:00
2011-11-27 02:23:55 +01:00
if ( ! function_exists ( 'getmypid' ))
{
2012-08-03 23:29:08 +02:00
/**
* Return random PID
* Some web hosts disable this php function for security reasons
*
* @ return int
*/
function getmypid ()
{
return rand ( 1 , 32768 );
}
2011-11-27 02:23:55 +01:00
}
2011-11-05 03:18:19 +01:00
?>