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 >
2009-08-12 01:42:21 +02:00
* Copyright ( C ) 2004 - 2009 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 >
2010-01-28 09:52:41 +01:00
* Copyright ( C ) 2005 - 2010 Regis Houssin < regis @ dolibarr . fr >
2008-08-06 16:58:26 +02:00
* Copyright ( C ) 2008 Raphael Bertrand ( Resultic ) < raphael . bertrand @ resultic . fr >
2010-03-27 18:08:41 +01:00
* Copyright ( C ) 2010 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
* the Free Software Foundation ; either version 2 of the License , or
* ( 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
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
* or see http :// www . gnu . org /
*/
/**
* \file htdocs / 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
* \version $Id $
*/
// For compatibility during upgrade
2010-12-20 11:19:24 +01:00
if ( ! defined ( 'DOL_DOCUMENT_ROOT' )) define ( 'DOL_DOCUMENT_ROOT' , '..' );
if ( ! defined ( 'DOL_DOCUMENT_ROOT_ALT' )) define ( 'DOL_DOCUMENT_ROOT_ALT' , '' ); // If option not enabled, we keep it disabled but avoid warning
if ( ! defined ( 'ADODB_DATE_VERSION' )) include_once ( DOL_DOCUMENT_ROOT . " /includes/adodbtime/adodb-time.inc.php " );
2008-08-06 16:50:06 +02:00
2009-08-12 01:42:21 +02: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
2010-11-03 16:54:06 +01:00
* @ param paramname Name of parameter to found
2010-11-20 14:08:44 +01:00
* @ param check Type of check ( '' = no check , 'int' = check it 's numeric, ' alpha '=check it' s alpha only )
2010-11-11 00:05:35 +01:00
* @ param method Type of method ( 0 = get or post , 1 = only get , 2 = only post )
2010-11-20 14:08:44 +01:00
* @ 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
{
2010-11-11 00:54:59 +01:00
if ( $method == 1 ) $out = isset ( $_GET [ $paramname ]) ? $_GET [ $paramname ] : '' ;
2010-11-20 16:25:08 +01:00
else if ( $method == 2 ) $out = isset ( $_POST [ $paramname ]) ? $_POST [ $paramname ] : '' ;
2010-11-10 22:41:34 +01:00
else $out = isset ( $_GET [ $paramname ]) ? $_GET [ $paramname ] : ( isset ( $_POST [ $paramname ]) ? $_POST [ $paramname ] : '' );
2010-11-11 00:54:59 +01:00
2010-11-10 21:16:31 +01:00
if ( ! empty ( $check ))
{
2010-11-21 15:35:59 +01:00
// Check if numeric
if ( $check == 'int' && ! preg_match ( '/^[\.,0-9]+$/i' , trim ( $out ))) $out = '' ;
2010-11-20 14:08:44 +01:00
// Check if alpha
2010-11-21 15:35:59 +01:00
//if ($check == 'alpha' && ! preg_match('/^[ =:@#\/\\\(\)\-\._a-z0-9]+$/i',trim($out))) $out='';
if ( $check == 'alpha' && preg_match ( '/"/' , trim ( $out ))) $out = '' ; // Only " is dangerous because param in url can close the href= or src= and add javascript functions
2010-11-10 21:16:31 +01:00
}
2010-11-11 00:54:59 +01:00
2010-11-10 20:47:03 +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 .
2010-12-30 23:54:11 +01:00
* This prefix is unique for instance and avoid conflict between multi - instances Dolibarrs ,
* even when having two instances with one root dir or two instances in virtual servers .
2010-12-27 20:13:06 +01:00
* @ return string A calculated prefix
*/
function dol_getprefix ()
{
2010-12-30 23:54:11 +01:00
return md5 ( $_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' )
* To link to a module file from a module file , use include ( './mymodulefile' );
* To link to a module file from a core file , then this function can be used .
2010-12-19 03:42:53 +01:00
* @ param relpath Relative path to file ( Ie : mydir / myfile , ../ myfile , ... )
* @ return int Result
*/
function dol_include_once ( $relpath )
{
2010-12-28 03:12:39 +01:00
global $conf , $langs , $user , $mysoc ; // Other global var must be retreived with $GLOBALS['var']
2011-03-18 15:13:09 +01:00
return @ include_once ( dol_buildpath ( $relpath ));
2010-12-19 03:42:53 +01:00
}
2010-12-19 12:05:07 +01:00
/**
2010-12-19 19:05:38 +01:00
* Return path of url or filesystem . Return default_root or alternate root if file_exist fails .
2011-01-14 21:23:09 +01:00
* @ param path Relative path to file ( if mode = 0 , ie : mydir / myfile , ../ myfile , ... ) or relative url ( if mode = 1 ) .
2011-03-18 15:13:09 +01:00
* @ param type 0 = Used for a Filesystem path , 1 = Used for an URL path ( output relative ), 2 = Used for an URL path ( output full path )
2011-01-14 21:23:09 +01:00
* @ return string Full filsystem path ( if mode = 0 ), Full url path ( if mode = 1 )
2010-12-19 12:05:07 +01:00
*/
2011-03-18 15:13:09 +01:00
function dol_buildpath ( $path , $type = 0 )
2010-12-19 12:05:07 +01:00
{
2011-03-18 15:13:09 +01:00
if ( empty ( $type )) // For a filesystem path
2010-12-19 12:05:07 +01:00
{
2010-12-19 19:05:38 +01:00
$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 ;
}
2010-12-19 12:05:07 +01:00
}
2010-12-19 19:05:38 +01:00
else // For an url path
2010-12-19 12:05:07 +01:00
{
2011-03-18 15:13:09 +01:00
// 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 ;
}
}
}
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 ;
}
}
}
2010-12-19 12:05:07 +01:00
}
2010-12-21 01:44:57 +01:00
2010-12-19 12:05:07 +01:00
return $res ;
}
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 )
* This function works for both PHP4 and PHP5 .
* @ param object Object to clone
* @ return date Timestamp
2009-08-12 01:42:21 +02:00
*/
function dol_clone ( $object )
{
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-09-15 13:24:00 +02:00
/**
2011-03-18 15:13:09 +01:00
* Optimize a size for some browsers ( phone , smarphone , ... )
* @ param size Size we want
* @ param type Type of optimizing ( '' = Optimize for a truncate , 'width' = Optimize for screen width )
* @ return int New size after optimizing
2009-09-15 13:24:00 +02:00
*/
function dol_size ( $size , $type = '' )
{
global $conf ;
if ( empty ( $conf -> browser -> phone )) return $size ;
if ( $type == 'width' ) return 250 ;
else return 10 ;
}
2009-08-12 01:42:21 +02:00
2009-05-05 18:05:20 +02:00
/**
2010-12-08 14:13:17 +01:00
* Return date for now . We should always use this function without parameters ( that means GMT time ) .
* @ param 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 date Timestamp
2009-05-05 18:05:20 +02:00
*/
2010-12-08 14:13:17 +01:00
function dol_now ( $mode = 'gmt' )
2009-05-05 18:05:20 +02:00
{
2010-12-18 03:33:13 +01:00
// Note that gmmktime and mktime return same value (GMT) whithout parameters
2009-06-14 18:25:23 +02:00
if ( $mode == 'gmt' ) $ret = gmmktime (); // Time for now at greenwich.
2010-12-08 14:13:17 +01:00
else if ( $mode == 'tzserver' ) // Time for now with PHP server timezone added
2009-06-14 18:25:23 +02:00
{
2010-12-08 14:13:17 +01:00
$tzsecond =- dol_mktime ( 0 , 0 , 0 , 1 , 1 , 1970 );
$ret = gmmktime () + $tzsecond ;
2009-06-14 18:25:23 +02:00
}
2010-12-08 14:13:17 +01:00
else if ( $mode == 'tzref' ) // Time for now where parent company timezone is added
2009-05-05 18:05:20 +02:00
{
2010-12-08 14:13:17 +01:00
// TODO Should add the company timezone
$ret = gmmktime ();
2009-05-05 18:05:20 +02:00
}
2010-12-08 14:13:17 +01:00
else if ( $mode == 'tzuser' ) // Time for now where user timezone is added
2009-05-19 20:11:47 +02:00
{
2010-12-08 14:13:17 +01:00
//print 'eeee'.time().'-'.mktime().'-'.gmmktime();
$tzhour = isset ( $_SESSION [ 'dol_tz' ]) ? $_SESSION [ 'dol_tz' ] : 0 ;
$ret = gmmktime () + ( $tzhour * 60 * 60 );
2009-05-19 20:11:47 +02:00
}
2009-05-05 18:05:20 +02:00
return $ret ;
}
2008-08-06 16:50:06 +02:00
2009-07-28 19:19:46 +02:00
2008-08-06 16:50:06 +02:00
/**
2010-10-03 17:42:01 +02:00
* Clean a string to use it as a file name .
* @ param str String to clean
* @ param newstr String to replace bad chars with
* @ return string String cleaned ( a - zA - Z_ )
* @ see dol_string_nospecial , dol_string_unaccent
2008-10-25 23:35:27 +02:00
*/
2009-04-29 20:02:50 +02:00
function dol_sanitizeFileName ( $str , $newstr = '_' )
2008-10-25 23:35:27 +02:00
{
2010-10-03 17:42:01 +02:00
global $conf ;
return dol_string_nospecial ( dol_string_unaccent ( $str ), $newstr , $conf -> filesystem_forbidden_chars );
2008-10-25 23:35:27 +02:00
}
/**
2010-10-03 17:42:01 +02:00
* Clean a string from all accent characters to be used as ref , login or by dol_sanitizeFileName .
* @ param str String to clean
* @ return string Cleaned string
* @ 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
{
2008-10-25 23:18:53 +02:00
if ( utf8_check ( $str ))
{
2008-10-26 00:23:22 +02:00
$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 );
2008-10-25 23:18:53 +02:00
}
else
{
2008-11-09 01:08:52 +01:00
$string = strtr ( $str ,
2009-10-12 00:21:14 +02:00
" \xC0 \xC1 \xC2 \xC3 \xC5 \xC7
2008-10-26 00:54:31 +02:00
\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 " ,
2008-11-09 01:08:52 +01:00
" AAAAAC
2008-10-26 00:54:31 +02:00
EEEEIIIIDN
OOOOOUUUY
aaaaaceeee
iiiidnooooo
2009-01-15 00:36:51 +01:00
uuuyy " );
2008-11-09 01:08:52 +01:00
$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 " ));
2008-10-26 00:54:31 +02:00
return $string ;
2008-10-25 23:18:53 +02:00
}
2008-08-06 16:50:06 +02:00
}
/**
2010-10-03 17:42:01 +02:00
* Clean a string from all punctuation characters to use it as a ref or login .
* @ param str String to clean
* @ param newstr String to replace forbidden chars with
* @ param badchars List of forbidden characters
* @ return string Cleaned string
* @ 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
{
2010-10-03 17:42:01 +02:00
$forbidden_chars_to_replace = array ( " " , " ' " , " / " , " \\ " , " : " , " * " , " ? " , " \" " , " < " , " > " , " | " , " [ " , " ] " , " , " , " ; " , " = " );
2008-08-06 16:50:06 +02:00
$forbidden_chars_to_remove = array ();
2010-10-03 17:42:01 +02:00
if ( is_array ( $badchars )) $forbidden_chars_to_replace = $badchars ;
//$forbidden_chars_to_remove=array("(",")");
2008-10-09 19:29:32 +02:00
2010-10-03 17:42:01 +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
}
/**
* \brief Returns text escaped for inclusion in javascript code
* \param $stringtoescape String to escape
* \return string Escaped string
*/
function dol_escape_js ( $stringtoescape )
{
2008-10-09 19:29:32 +02:00
// escape quotes and backslashes, newlines, etc.
2010-01-13 00:03:34 +01:00
return strtr ( $stringtoescape , array ( " ' " => " \\ ' " , '\\' => '\\\\' , " ' " => " \\ ' " , '"' => " \\ ' " , " \r " => '\\r' , " \n " => '\\n' , '</' => '<\/' ));
2008-08-06 16:50:06 +02:00
}
2008-10-09 19:29:32 +02:00
2009-01-28 10:00:29 +01:00
/**
2009-02-02 23:11:10 +01:00
* \brief Returns text escaped for inclusion in HTML alt or title tags
2009-01-28 10:00:29 +01:00
* \param $stringtoescape String to escape
* \return string Escaped string
*/
function dol_escape_htmltag ( $stringtoescape )
{
// escape quotes and backslashes, newlines, etc.
2010-08-10 01:57:45 +02:00
$tmp = dol_html_entity_decode ( $stringtoescape , ENT_COMPAT , 'UTF-8' );
2010-04-07 21:19:56 +02:00
$tmp = strtr ( $tmp , array ( '"' => '' , " \r " => '\\r' , " \n " => '\\n' , " <b> " => '' , '</b>' => '' ));
2010-08-10 01:57:45 +02:00
return dol_htmlentities ( $tmp , ENT_COMPAT , 'UTF-8' );
2009-01-28 10:00:29 +01:00
}
2009-01-07 11:57:36 +01:00
/* For backward compatiblity */
function dolibarr_syslog ( $message , $level = LOG_INFO )
{
return dol_syslog ( $message , $level );
}
2008-08-06 16:50:06 +02:00
/**
2008-12-15 02:04:32 +01:00
* \brief Write log message in a file or to syslog process
* Pour fichier : fichier defined by SYSLOG_FILE
* Pour syslog : facility defined by SYSLOG_FACILITY
* Warning , les fonctions syslog sont buggues sous Windows et generent des
* fautes de protection memoire . Pour resoudre , utiliser le loggage fichier ,
* au lieu du loggage syslog ( configuration du module ) .
* Si SYSLOG_FILE_NO_ERROR defini , on ne gere pas erreur ecriture log
* \param message Line to log . Ne doit pas etre traduit si level = LOG_ERR
* \param level Log level
2009-02-02 19:33:44 +01:00
* \remarks This function works only if syslog module is enabled .
* \remarks This must must not use any call to other function calling dol_syslog ( avoid infinite loop ) .
2010-04-17 16:30:23 +02:00
* \remarks 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
2008-10-11 00:27:11 +02:00
* On Linux LOG_ERR = 3 , LOG_WARNING = 4 , LOG_INFO = 6 , LOG_DEBUG = 7
2008-08-06 16:50:06 +02:00
*/
2009-01-07 11:57:36 +01:00
function dol_syslog ( $message , $level = LOG_INFO )
2008-08-06 16:50:06 +02:00
{
2009-01-12 20:36:40 +01:00
global $conf , $user , $langs , $_REQUEST ;
2008-12-15 02:04:32 +01:00
// If adding log inside HTML page is required
2009-01-12 22:52:12 +01:00
if ( ! empty ( $_REQUEST [ 'logtohtml' ]) && ! empty ( $conf -> global -> MAIN_LOGTOHTML ))
2008-12-15 02:04:32 +01:00
{
2010-09-04 14:26:09 +02:00
$conf -> logbuffer [] = dol_print_date ( time (), " %Y-%m-%d %H:%M:%S " ) . " " . $message ;
2008-12-15 02:04:32 +01:00
}
2008-08-06 16:50:06 +02:00
2008-12-15 02:04:32 +01:00
// If syslog module enabled
2008-10-11 00:27:11 +02:00
if ( ! empty ( $conf -> syslog -> enabled ))
2008-08-06 16:50:06 +02:00
{
//print $level.' - '.$conf->global->SYSLOG_LEVEL.' - '.$conf->syslog->enabled." \n";
if ( $level > $conf -> global -> SYSLOG_LEVEL ) return ;
2010-02-28 15:16:46 +01:00
// Translate error message if this is an error message (rare) and langs is loaded
2008-08-06 16:50:06 +02:00
if ( $level == LOG_ERR )
{
2010-02-28 15:16:46 +01:00
if ( is_object ( $langs ))
{
$langs -> load ( " errors " );
if ( $message != $langs -> trans ( $message )) $message = $langs -> trans ( $message );
}
2008-08-06 16:50:06 +02:00
}
2009-08-11 19:58:25 +02:00
// Add page/script name to log message
$script = isset ( $_SERVER [ 'PHP_SELF' ]) ? basename ( $_SERVER [ 'PHP_SELF' ], '.php' ) . ' ' : '' ;
$message = $script . $message ;
2008-10-23 19:10:35 +02:00
// Add user to log message
2009-11-09 15:41:35 +01:00
$login = 'nologin' ;
2008-08-06 16:50:06 +02:00
if ( is_object ( $user ) && $user -> id ) $login = $user -> login ;
$message = sprintf ( " %-8s " , $login ) . " " . $message ;
2008-10-23 19:10:35 +02:00
// Check if log is to a file (SYSLOG_FILE defined) or to syslog
2008-08-06 16:50:06 +02:00
if ( defined ( " SYSLOG_FILE " ) && SYSLOG_FILE )
{
2009-04-29 20:14:13 +02:00
$filelog = SYSLOG_FILE ;
2009-10-21 18:50:15 +02:00
$filelog = preg_replace ( '/DOL_DATA_ROOT/i' , DOL_DATA_ROOT , $filelog );
2010-04-27 01:01:19 +02:00
//print "filelog=".$filelog."\n";
2008-08-06 16:50:06 +02:00
if ( defined ( " SYSLOG_FILE_NO_ERROR " )) $file =@ fopen ( $filelog , " a+ " );
else $file = fopen ( $filelog , " a+ " );
2009-02-02 19:33:44 +01:00
2008-08-06 16:50:06 +02:00
if ( $file )
{
2009-11-09 15:41:35 +01:00
$ip = '???' ; // $ip contains information to identify computer that run the code
2009-11-09 15:17:53 +01:00
if ( ! empty ( $_SERVER [ " REMOTE_ADDR " ])) $ip = $_SERVER [ " REMOTE_ADDR " ]; // In most cases.
else if ( ! empty ( $_SERVER [ 'SERVER_ADDR' ])) $ip = $_SERVER [ 'SERVER_ADDR' ]; // This is when PHP session is ran inside a web server but not inside a client request (example: init code of apache)
2009-11-09 15:41:35 +01:00
else if ( ! empty ( $_SERVER [ 'COMPUTERNAME' ])) $ip = $_SERVER [ 'COMPUTERNAME' ] . ( empty ( $_SERVER [ 'USERNAME' ]) ? '' : '@' . $_SERVER [ 'USERNAME' ]); // This is when PHP session is ran outside a web server, like from Windows command line (Not always defined, but usefull if OS defined it).
2009-11-09 15:43:19 +01:00
else if ( ! empty ( $_SERVER [ 'LOGNAME' ])) $ip = '???@' . $_SERVER [ 'LOGNAME' ]; // 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).
2008-08-06 16:50:06 +02:00
$liblevelarray = array ( LOG_ERR => 'ERROR' , LOG_WARNING => 'WARN' , LOG_INFO => 'INFO' , LOG_DEBUG => 'DEBUG' );
$liblevel = $liblevelarray [ $level ];
if ( ! $liblevel ) $liblevel = 'UNDEF' ;
2010-09-04 14:26:09 +02:00
$message = dol_print_date ( time (), " %Y-%m-%d %H:%M:%S " ) . " " . sprintf ( " %-5s " , $liblevel ) . " " . sprintf ( " %-15s " , $ip ) . " " . $message ;
2008-08-06 16:50:06 +02:00
fwrite ( $file , $message . " \n " );
fclose ( $file );
2008-10-21 23:27:20 +02:00
// This is for log file, we do not change permissions
2008-08-06 16:50:06 +02: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 " ;
}
}
elseif ( ! defined ( " SYSLOG_FILE_NO_ERROR " ))
{
2009-02-02 19:33:44 +01:00
// Do not use call to functions that make call to dol_syslog, so no call to langs.
print " Error, failed to open file " . $filelog . " \n " ;
2008-08-06 16:50:06 +02:00
}
}
else
{
2008-10-23 19:10:35 +02:00
if ( function_exists ( 'openlog' )) // This function does not exists on some ISP (Ex: Free in France)
2008-08-06 16:50:06 +02:00
{
2009-01-21 15:09:42 +01:00
$facility = LOG_USER ;
if ( defined ( " SYSLOG_FACILITY " ) && SYSLOG_FACILITY )
2008-10-23 19:10:35 +02:00
{
// Exemple: SYSLOG_FACILITY vaut LOG_USER qui vaut 8. On a besoin de 8 dans $facility.
$facility = constant ( " SYSLOG_FACILITY " );
}
2009-01-21 15:09:42 +01:00
openlog ( " dolibarr " , LOG_PID | LOG_PERROR , ( int ) $facility ); // (int) is required to avoid error parameter 3 expected to be long
2008-11-09 01:08:52 +01:00
2008-10-23 19:10:35 +02:00
if ( ! $level )
{
syslog ( LOG_ERR , $message );
}
else
{
syslog ( $level , $message );
}
2008-11-09 01:08:52 +01:00
2008-10-23 19:10:35 +02:00
closelog ();
2008-08-06 16:50:06 +02:00
}
}
}
}
2009-01-07 11:57:36 +01:00
/* For backward compatibility */
function dolibarr_fiche_head ( $links , $active = '0' , $title = '' , $notab = 0 )
{
return dol_fiche_head ( $links , $active , $title , $notab );
}
2008-08-06 16:50:06 +02:00
/**
2010-12-28 01:10:13 +01:00
* Show tab header of a card
* @ param links Array of tabs
* @ param active Active tab name
* @ param title Title
* @ param notab 0 = Add tab header , 1 = no tab header
* @ param picto Add a picto on tab titel
2008-08-06 16:50:06 +02:00
*/
2009-07-28 15:37:28 +02:00
function dol_fiche_head ( $links , $active = '0' , $title = '' , $notab = 0 , $picto = '' )
2008-08-06 16:50:06 +02:00
{
print " \n " . '<div class="tabs">' . " \n " ;
// Affichage titre
if ( $title )
{
$limittitle = 30 ;
print '<a class="tabTitle">' ;
2009-07-28 15:37:28 +02:00
if ( $picto ) print img_object ( '' , $picto ) . ' ' ;
2009-12-15 23:13:43 +01:00
print dol_trunc ( $title , $limittitle );
2008-08-06 16:50:06 +02:00
print '</a>' ;
}
// Affichage onglets
for ( $i = 0 ; $i < sizeof ( $links ) ; $i ++ )
{
if ( $links [ $i ][ 2 ] == 'image' )
{
2010-10-06 11:07:59 +02:00
if ( ! empty ( $links [ $i ][ 0 ]))
{
print '<a class="tabimage" href="' . $links [ $i ][ 0 ] . '">' . $links [ $i ][ 1 ] . '</a>' . " \n " ;
}
else
{
2010-10-06 11:28:45 +02:00
print '<span class="tabspan">' . $links [ $i ][ 1 ] . '</span>' . " \n " ;
2010-10-06 11:07:59 +02:00
}
2008-08-06 16:50:06 +02:00
}
else
{
//print "x $i $active ".$links[$i][2]." z";
if (( is_numeric ( $active ) && $i == $active )
|| ( ! is_numeric ( $active ) && $active == $links [ $i ][ 2 ]))
{
print '<a id="active" class="tab" href="' . $links [ $i ][ 0 ] . '">' . $links [ $i ][ 1 ] . '</a>' . " \n " ;
}
else
{
2010-10-06 11:07:59 +02:00
print '<a id="' . $links [ $i ][ 2 ] . '" class="tab" href="' . $links [ $i ][ 0 ] . '">' . $links [ $i ][ 1 ] . '</a>' . " \n " ;
2008-08-06 16:50:06 +02:00
}
}
}
print " </div> \n " ;
2009-12-30 15:45:55 +01:00
if ( ! $notab ) print " \n " . '<div class="tabBar">' . " \n " ;
2008-08-06 16:50:06 +02:00
}
2010-05-26 16:52:32 +02:00
/**
2010-10-02 01:37:36 +02:00
* Show tab footer of a card
* @ param notab 0 = Add tab footer , 1 = no tab footer
2010-05-26 16:52:32 +02:00
*/
function dol_fiche_end ( $notab = 0 )
{
if ( ! $notab ) print " \n </div> \n " ;
}
2008-08-06 16:50:06 +02:00
2009-01-07 11:57:36 +01:00
/* For backward compatibility */
2009-02-14 01:00:24 +01:00
function dolibarr_print_date ( $time , $format = '' , $to_gmt = false , $outputlangs = '' , $encodetooutput = false )
2009-01-07 11:57:36 +01:00
{
2009-02-14 01:00:24 +01:00
return dol_print_date ( $time , $format , $to_gmt , $outputlangs , $encodetooutput );
2009-01-07 11:57:36 +01:00
}
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 ) .
* Return charset is always UTF - 8 , except if encodetoouput is defined . In this cas charset is output charset .
2010-12-08 14:13:17 +01:00
* @ param time GM Timestamps date ( or deprecated strings 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS' )
2010-11-21 17:11:52 +01:00
* @ param format Output date format
2008-10-17 02:24:44 +02:00
* " %d %b %Y " ,
* " %d/%m/%Y %H:%M " ,
* " %d/%m/%Y %H:%M:%S " ,
* " day " , " daytext " , " dayhour " , " dayhourldap " , " dayhourtext "
2010-12-08 14:13:17 +01:00
* @ param tzoutput true = output 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
2010-11-21 17:11:52 +01:00
* @ param outputlangs Object lang that contains language for text translation .
* @ return string Formated date or '' if time is null
* @ see dol_mktime , dol_stringtotime
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
{
2008-10-17 02:24:44 +02:00
global $conf , $langs ;
2008-08-06 16:50:06 +02:00
2010-12-08 14:13:17 +01:00
$to_gmt = false ;
$offset = 0 ;
if ( $tzoutput )
{
$to_gmt = true ; // For backward compatibility
2010-12-18 03:33:13 +01:00
$offset = 0 ;
2010-12-08 14:13:17 +01:00
if ( is_string ( $tzoutput ))
{
if ( $tzoutput == 'tzserver' )
{
$to_gmt = false ;
$offset = 0 ;
}
if ( $tzoutput == 'tzuser' )
{
$to_gmt = true ;
$offset = ( empty ( $_SESSION [ 'dol_tz' ]) ? 0 : $_SESSION [ 'dol_tz' ]) * 60 * 60 ;
}
if ( $tzoutput == 'tzcompany' )
{
$to_gmt = false ;
$offset = 0 ; // TODO Define this and use it later
}
2010-12-18 03:33:13 +01:00
}
2010-12-08 14:13:17 +01:00
}
2010-12-18 03:33:13 +01:00
2010-07-23 02:17:46 +02:00
if ( ! is_object ( $outputlangs )) $outputlangs = $langs ;
2008-08-06 16:50:06 +02:00
// 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' );
2010-07-23 02:17:46 +02:00
// 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
2009-02-07 01:38:29 +01:00
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' ;
2010-11-20 00:50:27 +01:00
if ( $format == 'dayxcard' ) $format = '%Y%m%d' ;
2008-08-06 16:50:06 +02:00
2008-12-06 12:28:40 +01:00
// If date undefined or "", we return ""
2010-08-24 16:42:18 +02:00
if ( dol_strlen ( $time ) == 0 ) return '' ; // $time=0 allowed (it means 01/01/1970 00:00:00)
2009-01-15 00:36:51 +01:00
2009-02-03 03:59:16 +01:00
//print 'x'.$time;
2009-02-10 23:07:31 +01:00
2009-10-21 20:21:56 +02:00
if ( preg_match ( '/%b/i' , $format )) // There is some text to translate
2009-02-07 01:38:29 +01:00
{
// We inhibate translation to text made by strftime functions. We will use trans instead later.
2009-10-21 20:21:56 +02:00
$format = str_replace ( '%b' , '__b__' , $format );
$format = str_replace ( '%B' , '__B__' , $format );
2009-02-07 01:38:29 +01:00
}
2009-10-21 20:21:56 +02:00
if ( preg_match ( '/%a/i' , $format )) // There is some text to translate
2009-02-21 01:11:13 +01:00
{
// We inhibate translation to text made by strftime functions. We will use trans instead later.
2009-10-21 20:21:56 +02:00
$format = str_replace ( '%a' , '__a__' , $format );
$format = str_replace ( '%A' , '__A__' , $format );
2009-02-21 01:11:13 +01:00
}
2009-02-10 23:07:31 +01:00
2010-11-21 17:11:52 +01:00
// Analyze date (deprecated) Ex: 1970-01-01, 1970-01-01 01:00:00, 19700101010000
2009-10-21 20:21:56 +02:00
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 ))
2008-08-06 16:50:06 +02:00
{
2008-09-29 02:17:40 +02:00
// This part of code should not be used.
2009-02-20 23:53:15 +01:00
dol_syslog ( " Functions.lib::dol_print_date function call with deprecated value of time in page " . $_SERVER [ " PHP_SELF " ], LOG_WARNING );
2009-02-03 03:59:16 +01:00
// Date has format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS' or 'YYYYMMDDHHMMSS'
2008-08-06 16:50:06 +02:00
$syear = $reg [ 1 ];
$smonth = $reg [ 2 ];
$sday = $reg [ 3 ];
$shour = $reg [ 4 ];
$smin = $reg [ 5 ];
$ssec = $reg [ 6 ];
2010-02-03 03:22:15 +01:00
$time = dol_mktime ( $shour , $smin , $ssec , $smonth , $sday , $syear , true );
2010-12-08 14:13:17 +01:00
$ret = adodb_strftime ( $format , $time + $offset , $to_gmt );
2008-08-06 16:50:06 +02:00
}
else
{
2008-09-29 02:17:40 +02:00
// Date is a timestamps
2009-02-03 03:59:16 +01:00
if ( $time < 100000000000 ) // Protection against bad date values
{
2010-12-08 14:13:17 +01:00
$ret = adodb_strftime ( $format , $time + $offset , $to_gmt );
2009-02-03 03:59:16 +01:00
}
else $ret = 'Bad value ' . $time . ' for date' ;
2008-08-06 16:50:06 +02:00
}
2008-10-21 23:27:20 +02:00
2009-10-21 20:21:56 +02:00
if ( preg_match ( '/__b__/i' , $format ))
2009-02-07 01:38:29 +01:00
{
// Here ret is string in PHP setup language (strftime was used). Now we convert to $outputlangs.
2010-12-08 14:13:17 +01:00
$month = adodb_strftime ( '%m' , $time + $offset );
2009-02-10 23:07:31 +01:00
if ( $encodetooutput )
{
$monthtext = $outputlangs -> transnoentities ( 'Month' . $month );
$monthtextshort = $outputlangs -> transnoentities ( 'MonthShort' . $month );
}
else
{
$monthtext = $outputlangs -> transnoentitiesnoconv ( 'Month' . $month );
$monthtextshort = $outputlangs -> transnoentitiesnoconv ( 'MonthShort' . $month );
}
2009-02-07 01:38:29 +01:00
//print 'monthtext='.$monthtext.' monthtextshort='.$monthtextshort;
2009-10-21 20:21:56 +02:00
$ret = str_replace ( '__b__' , $monthtextshort , $ret );
$ret = str_replace ( '__B__' , $monthtext , $ret );
2009-02-07 01:38:29 +01:00
//print 'x'.$outputlangs->charset_output.'-'.$ret.'x';
//return $ret;
}
2009-10-21 20:21:56 +02:00
if ( preg_match ( '/__a__/i' , $format ))
2009-02-21 01:11:13 +01:00
{
2010-12-08 14:13:17 +01:00
$w = adodb_strftime ( '%w' , $time + $offset );
2009-02-21 01:11:13 +01:00
$dayweek = $outputlangs -> transnoentitiesnoconv ( 'Day' . $w );
2009-10-21 20:21:56 +02:00
$ret = str_replace ( '__A__' , $dayweek , $ret );
$ret = str_replace ( '__a__' , dol_substr ( $dayweek , 0 , 3 ), $ret );
2009-02-21 01:11:13 +01:00
}
2009-02-07 01:38:29 +01:00
return $ret ;
2008-08-06 16:50:06 +02:00
}
/**
2010-11-21 17:11:52 +01:00
* Convert a string date into a GM Timestamps date
* @ param string Date in a string
* YYYYMMDD
* YYYYMMDDHHMMSS
* DD / MM / YY or DD / MM / YYYY ( this format should not be used anymore )
* DD / MM / YY HH : MM : SS or DD / MM / YYYY HH : MM : SS ( this format should not be used anymore )
* 19700101020000 -> 7200
* @ param gm 1 = Input date is GM date , 0 = Input date is local date
* @ return date Date
* @ see dol_print_date , dol_mktime
2008-08-06 16:50:06 +02:00
*/
2010-11-21 17:11:52 +01:00
function dol_stringtotime ( $string , $gm = 1 )
2008-08-06 16:50:06 +02:00
{
2009-10-21 20:21:56 +02:00
if ( preg_match ( '/^([0-9]+)\/([0-9]+)\/([0-9]+)\s?([0-9]+)?:?([0-9]+)?:?([0-9]+)?/i' , $string , $reg ))
2008-08-06 16:50:06 +02:00
{
2008-09-29 02:17:40 +02:00
// This part of code should not be used.
2010-07-15 01:12:49 +02:00
dol_syslog ( " Functions.lib::dol_stringtotime call to function with deprecated parameter " , LOG_WARNING );
2008-08-06 16:50:06 +02:00
// Date est au format 'DD/MM/YY' ou 'DD/MM/YY HH:MM:SS'
// Date est au format 'DD/MM/YYYY' ou 'DD/MM/YYYY HH:MM:SS'
$sday = $reg [ 1 ];
$smonth = $reg [ 2 ];
$syear = $reg [ 3 ];
$shour = $reg [ 4 ];
$smin = $reg [ 5 ];
$ssec = $reg [ 6 ];
if ( $syear < 50 ) $syear += 1900 ;
if ( $syear >= 50 && $syear < 100 ) $syear += 2000 ;
$string = sprintf ( " %04d%02d%02d%02d%02d%02d " , $syear , $smonth , $sday , $shour , $smin , $ssec );
}
2009-10-21 18:50:15 +02:00
$string = preg_replace ( '/([^0-9])/i' , '' , $string );
2008-08-06 16:50:06 +02:00
$tmp = $string . '000000' ;
2010-11-21 17:11:52 +01:00
$date = dol_mktime ( substr ( $tmp , 8 , 2 ), substr ( $tmp , 10 , 2 ), substr ( $tmp , 12 , 2 ), substr ( $tmp , 4 , 2 ), substr ( $tmp , 6 , 2 ), substr ( $tmp , 0 , 4 ), $gm );
2008-08-06 16:50:06 +02:00
return $date ;
}
/**
2008-09-29 02:17:40 +02:00
* \brief Return an array with date info
* \param timestamp Timestamp
* \param fast Fast mode
* \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 )),
* 0 => $origd
* If fast mode :
* 'seconds' => $secs ,
* 'minutes' => $min ,
* 'hours' => $hour ,
* 'mday' => $day ,
* 'mon' => $month ,
* 'year' => $year ,
* 'yday' => floor ( $secsInYear / $_day_power ),
* 'leap' => $leaf ,
* 'ndays' => $ndays
* \remarks PHP getdate is restricted to the years 1901 - 2038 on Unix and 1970 - 2038 on Windows
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
{
$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 ;
}
2009-01-07 11:57:36 +01:00
/* For backward compatibility */
2010-02-03 03:22:15 +01:00
function dolibarr_mktime ( $hour , $minute , $second , $month , $day , $year , $gm = false , $check = 1 )
2009-01-07 11:57:36 +01:00
{
return dol_mktime ( $hour , $minute , $second , $month , $day , $year , $gm , $check );
}
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
* @ param hour Hour ( can be - 1 for undefined )
2008-10-02 00:24:31 +02:00
* @ param minute Minute ( can be - 1 for undefined )
* @ param second Second ( can be - 1 for undefined )
* @ param month Month
* @ param day Day
* @ param year Year
2010-02-03 03:22:15 +01:00
* @ param gm 1 = Input informations are GMT values , otherwise local to server TZ
2009-01-04 23:09:02 +01:00
* @ param check 0 = No check on parameters ( Can use day 32 , etc ... )
2010-02-13 17:28:03 +01:00
* @ return timestamp Date as a timestamp , '' if error
2010-11-21 17:11:52 +01:00
* @ see dol_print_date , dol_stringtotime
2008-08-06 16:50:06 +02:00
*/
2010-02-03 03:22:15 +01:00
function dol_mktime ( $hour , $minute , $second , $month , $day , $year , $gm = false , $check = 1 )
2008-08-06 16:50:06 +02:00
{
//print "- ".$hour.",".$minute.",".$second.",".$month.",".$day.",".$year.",".$_SERVER["WINDIR"]." -";
2008-10-02 00:24:31 +02:00
// Clean parameters
if ( $hour == - 1 ) $hour = 0 ;
if ( $minute == - 1 ) $minute = 0 ;
if ( $second == - 1 ) $second = 0 ;
2008-10-09 19:29:32 +02:00
2008-08-06 16:50:06 +02:00
// Check parameters
if ( $check )
{
if ( ! $month || ! $day ) return '' ;
if ( $day > 31 ) return '' ;
if ( $month > 12 ) return '' ;
2008-10-02 00:24:31 +02:00
if ( $hour < 0 || $hour > 24 ) return '' ;
if ( $minute < 0 || $minute > 60 ) return '' ;
if ( $second < 0 || $second > 60 ) return '' ;
2008-08-06 16:50:06 +02:00
}
$usealternatemethod = false ;
if ( $year <= 1970 ) $usealternatemethod = true ; // <= 1970
if ( $year >= 2038 ) $usealternatemethod = true ; // >= 2038
if ( $usealternatemethod || $gm ) // Si time gm, seule adodb peut convertir
{
/*
// On peut utiliser strtotime pour obtenir la traduction.
2009-03-30 18:33:55 +02:00
// strtotime is ok for range: Friday 13 December 1901 20:45:54 GMT to Tuesday 19 January 2038 03:14:07 GMT.
2008-08-06 16:50:06 +02:00
$montharray = array ( 1 => 'january' , 2 => 'february' , 3 => 'march' , 4 => 'april' , 5 => 'may' , 6 => 'june' ,
7 => 'july' , 8 => 'august' , 9 => 'september' , 10 => 'october' , 11 => 'november' , 12 => 'december' );
$string = $day . " " . $montharray [ 0 + $month ] . " " . $year . " " . $hour . " : " . $minute . " : " . $second . " GMT " ;
$date = strtotime ( $string );
print " - " . $string . " " . $date . " - " ;
*/
$date = adodb_mktime ( $hour , $minute , $second , $month , $day , $year , 0 , $gm );
}
else
{
$date = mktime ( $hour , $minute , $second , $month , $day , $year );
}
return $date ;
}
2009-01-07 11:57:36 +01:00
/* For backward compatibility */
2010-02-03 03:22:15 +01:00
function dolibarr_date ( $fmt , $timestamp , $gm = false )
2009-01-07 11:57:36 +01:00
{
return dol_date ( $fmt , $timestamp , $gm );
}
2008-08-06 16:50:06 +02:00
/**
2010-11-21 17:11:52 +01:00
* Returns formated date
* @ param fmt Format ( Exemple : 'Y-m-d H:i:s' )
* @ param timestamp Date . Example : If timestamp = 0 and gm = 1 , return 01 / 01 / 1970 00 : 00 : 00
* @ param gm 1 if timestamp was built with gmmktime , 0 if timestamp was build with mktime
* @ return string Formated date
* @ deprecated Replaced by dol_print_date
2008-08-06 16:50:06 +02:00
*/
2010-02-03 03:22:15 +01:00
function dol_date ( $fmt , $timestamp , $gm = false )
2008-08-06 16:50:06 +02:00
{
$usealternatemethod = false ;
if ( $timestamp <= 0 ) $usealternatemethod = true ;
if ( $timestamp >= 2145913200 ) $usealternatemethod = true ;
if ( $usealternatemethod || $gm ) // Si time gm, seule adodb peut convertir
{
$string = adodb_date ( $fmt , $timestamp , $gm );
}
else
{
$string = date ( $fmt , $timestamp );
}
return $string ;
}
2009-01-07 13:39:40 +01:00
/**
* \brief Return string with formated size
* \param size Size to print
2009-10-04 17:52:16 +02:00
* \param shortvalue Tell if we want long value to use another unit ( Ex : 1.5 Kb instead of 1500 b )
* \param shortunit Use short value of size unit
2009-01-07 13:39:40 +01:00
* \return string Link
*/
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
{
global $langs ;
2009-10-04 17:52:16 +02:00
$level = 1024 ;
2009-01-07 13:39:40 +01:00
2009-10-04 17:52:16 +02:00
// 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
}
/**
* \brief Show Url link
* \param url Url to show
* \param target Target for link
* \param max Max number of characters to show
* \return string HTML Link
*/
function dol_print_url ( $url , $target = '_blank' , $max = 32 )
{
if ( empty ( $url )) return '' ;
2009-01-15 00:36:51 +01:00
2009-01-07 13:39:40 +01:00
$link = '<a href="' ;
2009-10-23 16:58:33 +02:00
if ( ! preg_match ( '/^http/i' , $url )) $link .= 'http://' ;
2009-01-07 13:39:40 +01:00
$link .= $url ;
if ( $target ) $link .= '" target="' . $target . '">' ;
2009-10-23 16:58:33 +02:00
if ( ! preg_match ( '/^http/i' , $url )) $link .= 'http://' ;
2009-02-20 23:53:15 +01:00
$link .= dol_trunc ( $url , $max );
2009-01-07 13:39:40 +01:00
$link .= '</a>' ;
return $link ;
}
/**
* \brief Show EMail link
2010-04-17 11:47:25 +02:00
* \param email EMail to show ( only email , without < Name of recipient > )
2009-01-07 13:39:40 +01:00
* \param cid Id of contact if known
* \param socid Id of third party if known
* \param addlink 0 = no link to create action
* \param max Max number of characters to show
2010-04-17 11:47:25 +02:00
* \param showinvalid Show warning if syntax email is wrong
2009-01-07 13:39:40 +01:00
* \return string HTML Link
*/
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
{
global $conf , $user , $langs ;
2009-01-15 00:36:51 +01:00
2009-01-07 13:39:40 +01:00
$newemail = $email ;
2009-01-15 00:36:51 +01:00
2009-07-28 20:12:47 +02:00
if ( empty ( $email )) return ' ' ;
2009-01-07 13:39:40 +01:00
if ( ! empty ( $addlink ))
{
$newemail = '<a href="' ;
2009-10-23 16:58:33 +02:00
if ( ! preg_match ( '/^mailto:/i' , $email )) $newemail .= 'mailto:' ;
2009-01-07 13:39:40 +01:00
$newemail .= $email ;
$newemail .= '">' ;
$newemail .= dol_trunc ( $email , $max );
$newemail .= '</a>' ;
2010-01-08 18:33:30 +01:00
if ( $showinvalid && ! isValidEmail ( $email ))
{
$langs -> load ( " errors " );
$newemail .= img_warning ( $langs -> trans ( " ErrorBadEMail " , $email ));
}
2009-01-15 00:36:51 +01:00
2009-01-07 13:39:40 +01:00
if (( $cid || $socid ) && $conf -> agenda -> enabled && $user -> rights -> agenda -> myactions -> create )
{
2011-01-29 19:50:13 +01: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>' ;
2009-01-07 13:39:40 +01:00
$newemail = '<table class="nobordernopadding"><tr><td>' . $newemail . ' </td><td> ' . $link . '</td></tr></table>' ;
}
}
2010-04-17 11:47:25 +02:00
else
{
if ( $showinvalid && ! isValidEmail ( $email ))
{
$langs -> load ( " errors " );
$newemail .= img_warning ( $langs -> trans ( " ErrorBadEMail " , $email ));
}
}
2009-01-07 13:39:40 +01:00
return $newemail ;
}
2009-01-07 11:57:36 +01:00
/* For backward compatibility */
2009-01-07 13:39:40 +01:00
function dolibarr_print_phone ( $phone , $country = " FR " , $cid = 0 , $socid = 0 , $addlink = 0 , $separ = " " )
2009-01-07 11:57:36 +01:00
{
2009-01-07 13:39:40 +01:00
return dol_print_phone ( $phone , $country , $cid , $socid , $addlink , $separ );
2009-01-07 11:57:36 +01:00
}
2008-08-06 16:50:06 +02:00
/**
2010-11-07 12:27:22 +01:00
* Format phone numbers according to country
* @ param phone Phone number to format
* @ param country Country to use for formatting
* @ param cid Id of contact if known
* @ param socid Id of third party if known
* @ param addlink 0 = no link to create action
* @ param 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
{
2009-01-07 13:39:40 +01:00
global $conf , $user , $langs ;
2009-01-15 00:36:51 +01:00
2009-01-07 13:39:40 +01:00
// Clean phone parameter
2009-10-21 20:21:56 +02:00
$phone = preg_replace ( " /[ \ s.-]/ " , " " , trim ( $phone ));
2008-11-01 01:16:34 +01:00
if ( empty ( $phone )) { return '' ; }
2008-08-06 16:50:06 +02:00
2008-11-01 01:16:34 +01:00
$newphone = $phone ;
2008-08-06 16:50:06 +02:00
if ( strtoupper ( $country ) == " FR " )
{
// France
2010-08-24 16:42:18 +02:00
if ( dol_strlen ( $phone ) == 10 ) {
2008-11-09 01:08:52 +01:00
$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 );
2008-08-06 16:50:06 +02:00
}
2010-08-24 16:42:18 +02:00
elseif ( dol_strlen ( $newphone ) == 7 )
2008-08-06 16:50:06 +02:00
{
2008-11-09 01:08:52 +01:00
$newphone = substr ( $newphone , 0 , 3 ) . $separ . substr ( $newphone , 3 , 2 ) . $separ . substr ( $newphone , 5 , 2 );
2008-08-06 16:50:06 +02:00
}
2010-08-24 16:42:18 +02:00
elseif ( dol_strlen ( $newphone ) == 9 )
2008-08-06 16:50:06 +02:00
{
2008-11-09 01:08:52 +01:00
$newphone = substr ( $newphone , 0 , 2 ) . $separ . substr ( $newphone , 2 , 3 ) . $separ . substr ( $newphone , 5 , 2 ) . $separ . substr ( $newphone , 7 , 2 );
2008-08-06 16:50:06 +02:00
}
2010-08-24 16:42:18 +02:00
elseif ( dol_strlen ( $newphone ) == 11 )
2008-08-06 16:50:06 +02:00
{
2008-11-09 01:08:52 +01:00
$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 );
2008-08-06 16:50:06 +02:00
}
2010-08-24 16:42:18 +02:00
elseif ( dol_strlen ( $newphone ) == 12 )
2008-08-06 16:50:06 +02:00
{
2008-11-09 01:08:52 +01:00
$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 );
2008-08-06 16:50:06 +02:00
}
}
2009-01-15 00:36:51 +01:00
2009-01-07 13:39:40 +01:00
if ( ! empty ( $addlink ))
2008-11-01 01:16:34 +01:00
{
2009-01-07 13:39:40 +01:00
if ( $conf -> clicktodial -> enabled )
2008-11-01 01:16:34 +01:00
{
2009-01-09 22:22:58 +01:00
if ( empty ( $user -> clicktodial_loaded )) $user -> fetch_clicktodial ();
2009-01-15 00:36:51 +01:00
if ( empty ( $conf -> global -> CLICKTODIAL_URL )) $urlmask = 'ErrorClickToDialModuleNotConfigured' ;
2009-01-07 13:39:40 +01:00
else $urlmask = $conf -> global -> CLICKTODIAL_URL ;
2010-11-07 12:27:22 +01:00
// This line is for backward compatibility
2009-01-07 13:39:40 +01:00
$url = sprintf ( $urlmask , urlencode ( $phone ), urlencode ( $user -> clicktodial_poste ), urlencode ( $user -> clicktodial_login ), urlencode ( $user -> clicktodial_password ));
2010-11-07 12:27:22 +01:00
// Thoose lines are for substitution
$substitarray = array ( '__PHONEFROM__' => $user -> clicktodial_poste ,
'__PHONETO__' => $phone ,
'__LOGIN__' => $user -> clicktodial_login ,
'__PASS__' => $user -> clicktodial_password );
$url = make_substitutions ( $url , $substitarray , $langs );
$newphonesav = $newphone ;
$newphone = '<a href="' . $url . '"' ;
if ( ! empty ( $conf -> global -> CLICKTODIAL_FORCENEWTARGET )) $newphone .= ' target="_blank"' ;
$newphone .= '>' . $newphonesav . '</a>' ;
2008-11-01 01:16:34 +01:00
}
2009-01-07 13:39:40 +01:00
2009-02-02 03:04:26 +01:00
//if (($cid || $socid) && $conf->agenda->enabled && $user->rights->agenda->myactions->create)
if ( $conf -> agenda -> enabled && $user -> rights -> agenda -> myactions -> create )
2008-11-01 01:16:34 +01:00
{
2011-01-29 19:50:13 +01:00
$type = 'AC_TEL' ; $link = '' ;
2009-01-07 13:39:40 +01:00
if ( $addlink == 'AC_FAX' ) $type = 'AC_FAX' ;
2011-01-29 19:50:13 +01:00
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>' ;
2009-01-07 13:39:40 +01:00
$newphone = '<table class="nobordernopadding"><tr><td>' . $newphone . ' </td><td> ' . $link . '</td></tr></table>' ;
2008-11-01 01:16:34 +01:00
}
}
2008-11-09 01:08:52 +01:00
2008-11-01 01:16:34 +01:00
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
* @ param ip IP
* @ param mode 1 = return only country / flag , 2 = return only IP
* @ 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
{
global $conf , $langs ;
2009-10-14 13:04:09 +02:00
$ret = '' ;
2009-10-17 15:47:17 +02:00
2009-10-14 13:04:09 +02:00
if ( empty ( $mode )) $ret .= $ip ;
2009-10-17 15:47:17 +02:00
2009-10-14 13:04:09 +02:00
if ( ! empty ( $conf -> geoipmaxmind -> enabled ) && $mode != 2 )
2009-10-14 00:01:27 +02:00
{
$datafile = $conf -> global -> GEOIPMAXMIND_COUNTRY_DATAFILE ;
//$ip='24.24.24.24';
2009-10-14 00:10:06 +02:00
//$datafile='E:\Mes Sites\Web\Admin1\awstats\maxmind\GeoIP.dat';
2009-10-14 00:01:27 +02:00
include_once ( DOL_DOCUMENT_ROOT . '/lib/dolgeoip.class.php' );
$geoip = new DolGeoIP ( 'country' , $datafile );
$countrycode = $geoip -> getCountryCodeFromIP ( $ip );
if ( $countrycode ) // If success, countrycode is us, fr, ...
{
if ( file_exists ( DOL_DOCUMENT_ROOT . '/theme/common/flags/' . $countrycode . '.png' ))
{
2009-10-14 13:04:09 +02:00
$ret .= ' ' . img_picto ( $countrycode . ' ' . $langs -> trans ( " AccordingToGeoIPDatabase " ), DOL_URL_ROOT . '/theme/common/flags/' . $countrycode . '.png' , '' , 1 );
2009-10-14 00:01:27 +02:00
}
2009-10-14 13:04:09 +02:00
else $ret .= ' (' . $countrycode . ')' ;
2009-10-14 00:01:27 +02:00
}
}
2009-10-17 15:47:17 +02:00
2009-10-14 13:04:09 +02:00
return $ret ;
2009-10-14 00:01:27 +02:00
}
2009-08-11 19:58:25 +02:00
/**
2010-11-15 20:08:35 +01:00
* Return true if email syntax is ok .
2010-08-21 00:12:05 +02:00
* @ param address email ( Ex : " toto@titi.com " , " John Do <johndo@titi.com> " )
2010-11-15 20:08:35 +01:00
* @ 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 )
{
2009-10-23 16:58:33 +02:00
if ( preg_match ( " /.*<(.+)>/i " , $address , $regs )) {
2009-08-11 19:58:25 +02:00
$address = $regs [ 1 ];
}
// 2 letters domains extensions are for countries
// 3 letters domains extensions: biz|com|edu|gov|int|mil|net|org|pro|...
2009-10-23 16:58:33 +02:00
if ( preg_match ( " /^[^@ \ s \t ]+@([a-zA-Z0-9 \ -]+ \ .)+([a-zA-Z0-9 \ -] { 2,3}|asso|aero|coop|info|name) \$ /i " , $address ))
2009-08-11 19:58:25 +02:00
{
return true ;
}
else
{
return false ;
}
}
2008-08-06 16:50:06 +02:00
2008-11-17 00:43:10 +01:00
/**
2010-07-27 22:52:00 +02:00
* Make a strlen call . Works even if mbstring module not enabled .
2008-11-17 00:43:10 +01:00
*
2010-07-28 00:38:28 +02:00
* @ param $string
* @ param $stringencoding
* @ return int
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
{
2010-08-24 23:29:47 +02:00
// print $stringencoding."xxx";
// $stringencoding='rrr';
if ( function_exists ( 'mb_strlen' )) return mb_strlen ( $string , $stringencoding );
else return strlen ( $string );
2008-11-17 00:43:10 +01:00
}
/**
* Make a substring . Works even in mbstring module not enabled
*
2010-07-28 00:38:28 +02:00
* @ param $string
* @ param $start
* @ param $length
* @ param $stringencoding
* @ return string
2008-11-17 00:43:10 +01:00
*/
function dol_substr ( $string , $start , $length , $stringencoding = '' )
{
global $langs ;
2009-01-15 00:36:51 +01:00
2008-11-17 00:43:10 +01:00
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 ;
}
2009-01-07 11:57:36 +01:00
/* For backward compatibility */
function dolibarr_trunc ( $string , $size = 40 , $trunc = 'right' , $stringencoding = '' )
{
return dol_trunc ( $string , $size , $trunc , $stringencoding );
}
2008-08-06 16:50:06 +02:00
/**
2009-10-10 18:45:37 +02:00
* \brief 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 '...' .
2008-08-06 16:50:06 +02:00
* \param string String to truncate
* \param size Max string size . 0 for no limit .
2010-08-21 20:53:19 +02:00
* \param trunc Where to trunc : right , left , middle , wrap
2009-09-28 02:03:43 +02:00
* \param stringencoding Tell what is source string encoding
2008-08-06 16:50:06 +02:00
* \return string Truncated string
2009-12-15 23:13:43 +01:00
* \remarks MAIN_DISABLE_TRUNC = 1 can disable all truncings
2008-08-06 16:50:06 +02:00
*/
2010-08-24 23:29:47 +02:00
function dol_trunc ( $string , $size = 40 , $trunc = 'right' , $stringencoding = 'UTF-8' )
2008-08-06 16:50:06 +02:00
{
2008-10-21 01:14:50 +02:00
global $conf ;
2008-10-21 23:27:20 +02:00
2008-08-06 16:50:06 +02:00
if ( $size == 0 ) return $string ;
2009-12-15 23:13:43 +01:00
if ( empty ( $conf -> global -> MAIN_DISABLE_TRUNC ))
2008-08-06 16:50:06 +02:00
{
// We go always here
if ( $trunc == 'right' )
{
2009-06-05 14:44:33 +02:00
$newstring = dol_textishtml ( $string ) ? dol_string_nohtmltag ( $string , 1 ) : $string ;
2009-10-10 18:45:37 +02:00
if ( dol_strlen ( $newstring , $stringencoding ) > ( $size + 1 ))
2009-06-05 14:44:33 +02:00
return dol_substr ( $newstring , 0 , $size , $stringencoding ) . '...' ;
2008-08-06 16:50:06 +02:00
else
2008-10-21 23:27:20 +02:00
return $string ;
2008-08-06 16:50:06 +02:00
}
if ( $trunc == 'middle' )
{
2009-06-05 14:44:33 +02:00
$newstring = dol_textishtml ( $string ) ? dol_string_nohtmltag ( $string , 1 ) : $string ;
2009-10-10 18:45:37 +02:00
if ( dol_strlen ( $newstring , $stringencoding ) > 2 && dol_strlen ( $newstring , $stringencoding ) > ( $size + 1 ))
2008-08-06 16:50:06 +02:00
{
$size1 = round ( $size / 2 );
$size2 = round ( $size / 2 );
2009-06-05 14:44:33 +02:00
return dol_substr ( $newstring , 0 , $size1 , $stringencoding ) . '...' . dol_substr ( $newstring , dol_strlen ( $newstring , $stringencoding ) - $size2 , $size2 , $stringencoding );
2008-08-06 16:50:06 +02:00
}
else
return $string ;
}
if ( $trunc == 'left' )
{
2009-06-05 14:44:33 +02:00
$newstring = dol_textishtml ( $string ) ? dol_string_nohtmltag ( $string , 1 ) : $string ;
2009-10-10 18:45:37 +02:00
if ( dol_strlen ( $newstring , $stringencoding ) > ( $size + 1 ))
2009-06-05 14:44:33 +02:00
return '...' . dol_substr ( $newstring , dol_strlen ( $newstring , $stringencoding ) - $size , $size , $stringencoding );
2008-08-06 16:50:06 +02:00
else
return $string ;
}
2010-08-21 20:53:19 +02:00
if ( $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 ;
}
2008-08-06 16:50:06 +02:00
}
else
{
return $string ;
}
}
/**
2010-10-21 21:56:42 +02:00
* Show a picto according to module / object ( generic function )
* @ param alt Text of alt on image
* @ param object Objet pour lequel il faut afficher le logo ( example : user , group , action , bill , contract , propal , product , ... )
2010-03-26 11:13:34 +01:00
* Pour les modules externe utiliser nomimage @ mymodule pour rechercher dans le repertoire " img " du module
2011-01-14 22:30:19 +01:00
* @ param options Add more attribute on img tag
2010-12-18 03:33:13 +01:00
* @ return string Return img tag
2008-08-06 16:50:06 +02:00
*/
2011-01-14 22:30:19 +01:00
function img_object ( $alt , $object , $options = '' )
2008-08-06 16:50:06 +02:00
{
global $conf , $langs ;
2010-11-02 12:22:41 +01:00
2010-12-15 17:43:43 +01:00
$path = 'theme/' . $conf -> theme ;
$url = DOL_URL_ROOT ;
2010-03-29 01:26:14 +02:00
2010-03-26 11:13:34 +01:00
if ( preg_match ( '/^([^@]+)@([^@]+)$/i' , $object , $regs ))
{
2010-12-15 17:43:43 +01:00
$object = $regs [ 1 ];
$path = $regs [ 2 ];
2010-12-18 03:33:13 +01:00
// If img file not into standard path, we use alternate path
2011-01-19 17:43:55 +01:00
if ( defined ( 'DOL_URL_ROOT_ALT' ) && DOL_URL_ROOT_ALT && ! file_exists ( DOL_DOCUMENT_ROOT . '/' . $path . '/img/object_' . $object . '.png' )) $url = DOL_URL_ROOT_ALT ;
2010-03-26 11:13:34 +01:00
}
2010-03-29 01:26:14 +02:00
2011-01-14 22:30:19 +01:00
return '<img src="' . $url . '/' . $path . '/img/object_' . $object . '.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '"' . ( $options ? ' ' . $options : '' ) . '>' ;
2008-08-06 16:50:06 +02:00
}
/**
2010-11-13 17:59:53 +01:00
* Show picto ( generic function )
* @ param alt Text on alt and title of image
* @ param picto Name of image file to show ( If no extension provided , we use '.png' ) . Image must be stored into 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 options Add more attribute on img tag
* @ param pictoisfullpath If 1 , image path is a full path
* @ return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_picto ( $alt , $picto , $options = '' , $pictoisfullpath = 0 )
{
global $conf ;
2010-10-02 01:37:36 +02:00
2010-09-30 18:34:27 +02:00
$path = 'theme/' . $conf -> theme ;
2010-12-15 17:43:43 +01:00
$url = DOL_URL_ROOT ;
2010-10-02 01:37:36 +02:00
2010-09-30 18:34:27 +02:00
if ( preg_match ( '/^([^@]+)@([^@]+)$/i' , $picto , $regs ))
{
$picto = $regs [ 1 ];
$path = $regs [ 2 ];
2011-01-14 22:30:19 +01:00
// If img file not into standard path, we use alternate path
2011-01-19 17:43:55 +01:00
if ( defined ( 'DOL_URL_ROOT_ALT' ) && DOL_URL_ROOT_ALT && ! file_exists ( DOL_DOCUMENT_ROOT . '/' . $path . '/img/' . $picto )) $url = DOL_URL_ROOT_ALT ;
2010-09-30 18:34:27 +02:00
}
2010-10-02 01:37:36 +02:00
2009-10-23 16:58:33 +02:00
if ( ! preg_match ( '/(\.png|\.gif)$/i' , $picto )) $picto .= '.png' ;
2009-09-28 01:22:00 +02:00
if ( $pictoisfullpath ) return '<img src="' . $picto . '" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '"' . ( $options ? ' ' . $options : '' ) . '>' ;
2010-12-15 17:43:43 +01:00
return '<img src="' . $url . '/' . $path . '/img/' . $picto . '" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '"' . ( $options ? ' ' . $options : '' ) . '>' ;
2008-08-06 16:50:06 +02:00
}
2008-09-02 02:27:05 +02:00
/**
2010-11-13 17:59:53 +01:00
* Show picto ( generic function )
* @ param alt Text on alt and title of image
* @ param picto Name of image file to show ( If no extension provided , we use '.png' ) . Image must be stored into htdocs / theme / common directory .
* @ param options Add more attribute on img tag
* @ param pictoisfullpath If 1 , image path is a full path
* @ return string Retourne tag img
2008-09-02 02:27:05 +02:00
*/
function img_picto_common ( $alt , $picto , $options = '' , $pictoisfullpath = 0 )
{
global $conf ;
2009-10-23 16:58:33 +02:00
if ( ! preg_match ( '/(\.png|\.gif)$/i' , $picto )) $picto .= '.png' ;
2009-09-28 01:22:00 +02:00
if ( $pictoisfullpath ) return '<img src="' . $picto . '" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '"' . ( $options ? ' ' . $options : '' ) . '>' ;
return '<img src="' . DOL_URL_ROOT . '/theme/common/' . $picto . '" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '"' . ( $options ? ' ' . $options : '' ) . '>' ;
2008-09-02 02:27:05 +02:00
}
2008-08-06 16:50:06 +02:00
/**
2009-05-27 00:50:36 +02:00
* \brief Show logo action
* \param alt Text for image alt and title
* \param numaction Action to show
* \return string Return an img tag
2008-08-06 16:50:06 +02:00
*/
function img_action ( $alt = " default " , $numaction )
{
global $conf , $langs ;
if ( $alt == " default " ) {
if ( $numaction == - 1 ) $alt = $langs -> trans ( " ChangeDoNotContact " );
if ( $numaction == 0 ) $alt = $langs -> trans ( " ChangeNeverContacted " );
if ( $numaction == 1 ) $alt = $langs -> trans ( " ChangeToContact " );
if ( $numaction == 2 ) $alt = $langs -> trans ( " ChangeContactInProcess " );
if ( $numaction == 3 ) $alt = $langs -> trans ( " ChangeContactDone " );
}
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/stcomm' . $numaction . '.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2009-10-07 19:36:51 +02:00
* \brief Affiche logo fichier
* \param alt Texte sur le alt de l ' image
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_file ( $alt = " default " )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Show " );
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/file.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2009-10-07 19:36:51 +02:00
* \brief Affiche logo refresh
* \param alt Texte sur le alt de l ' image
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_refresh ( $alt = " default " )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Refresh " );
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/refresh.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2008-11-16 02:09:04 +01:00
* \brief Affiche logo dossier
* \param alt Texte sur le alt de l ' image
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_folder ( $alt = " default " )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Dossier " );
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/folder.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2008-11-16 02:09:04 +01:00
* \brief Affiche logo nouveau fichier
* \param alt Texte sur le alt de l ' image
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_file_new ( $alt = " default " )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Show " );
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/filenew.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
\brief Affiche logo pdf
\param alt Texte sur le alt de l ' image
\param $size Taille de l ' icone : 3 = 16 x16px , 2 = 14 x14px
\return string Retourne tag img
*/
function img_pdf ( $alt = " default " , $size = 3 )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Show " );
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/pdf' . $size . '.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2008-11-16 02:09:04 +01:00
* \brief Affiche logo +
* \param alt Texte sur le alt de l ' image
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_edit_add ( $alt = " default " )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Add " );
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/edit_add.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2008-11-16 02:09:04 +01:00
* \brief Affiche logo -
* \param alt Texte sur le alt de l ' image
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_edit_remove ( $alt = " default " )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Remove " );
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/edit_remove.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2008-11-16 02:09:04 +01:00
* \brief Affiche logo editer / modifier fiche
* \param alt Texte sur le alt de l ' image
* \param float Si il faut y mettre le style " float: right "
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_edit ( $alt = " default " , $float = 0 , $other = '' )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Modify " );
2009-10-07 19:36:51 +02:00
$img = '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/edit.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '"' ;
2008-08-06 16:50:06 +02:00
if ( $float ) $img .= ' style="float: right"' ;
if ( $other ) $img .= ' ' . $other ;
$img .= '>' ;
return $img ;
}
2008-10-03 01:08:14 +02:00
/**
2008-11-16 02:09:04 +01:00
* \brief Affiche logo voir fiche
* \param alt Texte sur le alt de l ' image
* \param float Si il faut y mettre le style " float: right "
* \return string Retourne tag img
2008-10-03 01:08:14 +02:00
*/
function img_view ( $alt = " default " , $float = 0 , $other = '' )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " View " );
2009-10-07 19:36:51 +02:00
$img = '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/view.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '"' ;
2008-10-03 01:08:14 +02:00
if ( $float ) $img .= ' style="float: right"' ;
if ( $other ) $img .= ' ' . $other ;
$img .= '>' ;
return $img ;
}
2008-08-06 16:50:06 +02:00
/**
\brief Affiche logo effacer
\param alt Texte sur le alt de l ' image
\return string Retourne tag img
*/
function img_delete ( $alt = " default " )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Delete " );
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/delete.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2008-11-16 02:09:04 +01:00
* \brief Affiche logo help avec curseur " ? "
2009-01-15 00:36:51 +01:00
* \param usehelpcursor
2009-07-19 18:34:13 +02:00
* \param usealttitle Texte to use as alt title
2009-01-12 18:36:14 +01:00
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_help ( $usehelpcursor = 1 , $usealttitle = 1 )
{
global $conf , $langs ;
$s = '<img ' ;
if ( $usehelpcursor ) $s .= 'style="cursor: help;" ' ;
$s .= 'src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/info.png" border="0"' ;
2009-01-15 00:36:51 +01:00
if ( $usealttitle )
2009-01-12 18:36:14 +01:00
{
2009-10-07 19:36:51 +02:00
if ( is_string ( $usealttitle )) $s .= ' alt="' . dol_escape_htmltag ( $usealttitle ) . '" title="' . dol_escape_htmltag ( $usealttitle ) . '"' ;
2009-01-12 18:36:14 +01:00
else $s .= ' alt="' . $langs -> trans ( " Info " ) . '" title="' . $langs -> trans ( " Info " ) . '"' ;
}
2009-07-19 18:34:13 +02:00
else $s .= ' alt=""' ;
2008-08-06 16:50:06 +02:00
$s .= '>' ;
return $s ;
}
/**
2008-11-16 02:09:04 +01:00
* \brief Affiche logo info
* \param alt Texte sur le alt de l ' image
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_info ( $alt = " default " )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Informations " );
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/info.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2008-11-16 02:09:04 +01:00
* \brief Affiche logo warning
* \param alt Texte sur le alt de l ' image
* \param float Si il faut afficher le style " float: right "
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_warning ( $alt = " default " , $float = 0 )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Warning " );
2009-10-07 19:36:51 +02:00
$img = '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/warning.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '"' ;
2008-08-06 16:50:06 +02:00
if ( $float ) $img .= ' style="float: right"' ;
2009-05-04 16:58:40 +02:00
$img .= '>' ;
return $img ;
}
/**
* \brief Affiche logo redstar
* \param alt Texte sur le alt de l ' image
* \param float Si il faut afficher le style " float: right "
* \return string Retourne tag img
*/
function img_redstar ( $alt = " default " , $float = 0 )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " SuperAdministrator " );
2009-10-07 19:36:51 +02:00
$img = '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/redstar.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '"' ;
2009-05-04 16:58:40 +02:00
if ( $float ) $img .= ' style="float: right"' ;
2008-08-06 16:50:06 +02:00
$img .= '>' ;
return $img ;
}
/**
2008-11-16 02:09:04 +01:00
\brief Affiche logo error
2008-08-06 16:50:06 +02:00
\param alt Texte sur le alt de l ' image
\return string Retourne tag img
*/
function img_error ( $alt = " default " )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Error " );
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/error.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2008-11-16 02:09:04 +01:00
* \brief Affiche logo telephone
* \param alt Texte sur le alt de l ' image
* \param option Choose of logo
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_phone ( $alt = " default " , $option = 0 )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Call " );
$img = 'call_out' ;
if ( $option == 1 ) $img = 'call' ;
$img = 'object_commercial' ;
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/' . $img . '.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2009-10-07 19:36:51 +02:00
* \brief Affiche logo suivant
* \param alt Texte sur le alt de l ' image
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_next ( $alt = " default " )
{
global $conf , $langs ;
if ( $alt == " default " ) {
$alt = $langs -> trans ( " Next " );
}
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/next.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2009-10-07 19:36:51 +02:00
* \brief Affiche logo precedent
* \param alt Texte sur le alt de l ' image
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_previous ( $alt = " default " )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Previous " );
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/previous.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2010-09-14 22:27:47 +02:00
* Show logo down arrow
* @ param alt Texte sur le alt de l ' image
* @ param selected Affiche version " selected " du logo
* @ return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_down ( $alt = " default " , $selected = 0 )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Down " );
2010-09-14 22:27:47 +02:00
if ( $selected ) return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/1downarrow_selected.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '" class="imgdown">' ;
else return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/1downarrow.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '" class="imgdown">' ;
2008-08-06 16:50:06 +02:00
}
/**
2010-09-14 22:27:47 +02:00
* Show logo top arrow
* @ param alt Texte sur le alt de l ' image
* @ param selected Affiche version " selected " du logo
* @ return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_up ( $alt = " default " , $selected = 0 )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Up " );
2010-09-14 22:27:47 +02:00
if ( $selected ) return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/1uparrow_selected.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '" class="imgup">' ;
else return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/1uparrow.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '" class="imgup">' ;
2008-08-06 16:50:06 +02:00
}
/**
2009-10-07 19:36:51 +02:00
* \brief Affiche logo gauche
* \param alt Texte sur le alt de l ' image
* \param selected Affiche version " selected " du logo
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_left ( $alt = " default " , $selected = 0 )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Left " );
2009-10-07 19:36:51 +02:00
if ( $selected ) return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/1leftarrow_selected.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
else return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/1leftarrow.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2009-10-07 19:36:51 +02:00
* \brief Affiche logo droite
* \param alt Texte sur le alt de l ' image
* \param selected Affiche version " selected " du logo
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_right ( $alt = " default " , $selected = 0 )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Right " );
2009-10-07 19:36:51 +02:00
if ( $selected ) return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/1rightarrow_selected.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
else return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/1rightarrow.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2009-10-07 19:36:51 +02:00
* \brief Affiche logo tick
* \param alt Texte sur le alt de l ' image
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
function img_tick ( $alt = " default " )
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Active " );
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/tick.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2009-10-07 19:36:51 +02:00
* \brief Affiche le logo tick si allow
* \param allow Authorise ou non
* \return string Retourne tag img
2008-08-06 16:50:06 +02:00
*/
2009-06-05 18:56:01 +02:00
function img_allow ( $allow , $alt = 'default' )
2008-08-06 16:50:06 +02:00
{
global $conf , $langs ;
if ( $alt == " default " ) $alt = $langs -> trans ( " Active " );
if ( $allow == 1 )
{
2009-10-07 19:36:51 +02:00
return '<img src="' . DOL_URL_ROOT . '/theme/' . $conf -> theme . '/img/tick.png" border="0" alt="' . dol_escape_htmltag ( $alt ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2008-08-06 16:50:06 +02:00
}
else
{
return " - " ;
}
}
/**
2010-11-13 15:34:06 +01:00
* Show MIME img of a file
* @ param file Filename
* @ param alt Alternate text to show on img mous hover
* @ return string Return img tag
2008-08-06 16:50:06 +02:00
*/
function img_mime ( $file , $alt = '' )
{
2010-11-19 16:32:09 +01:00
require_once ( DOL_DOCUMENT_ROOT . '/lib/files.lib.php' );
2010-11-20 14:08:44 +01:00
2010-11-13 15:34:06 +01:00
$mimetype = dol_mimetype ( $file , '' , 1 );
$mimeimg = dol_mimetype ( $file , '' , 2 );
2009-05-19 02:14:27 +02:00
2010-11-13 15:34:06 +01:00
if ( empty ( $alt )) $alt = 'Mime type: ' . $mimetype ;
2009-10-23 16:58:33 +02:00
2010-11-13 15:34:06 +01:00
return '<img src="' . DOL_URL_ROOT . '/theme/common/mime/' . $mimeimg . '" border="0" alt="' . $alt . '" title="' . $alt . '">' ;
2008-08-06 16:50:06 +02:00
}
/**
2009-03-03 01:42:40 +01:00
* \brief Show information for admin users
2008-11-16 02:09:04 +01:00
* \param text Text info
2010-08-28 12:55:11 +02:00
* \param infoonimgalt Info is shown only on alt of star picto , otherwise it is show on output after the star picto
2008-11-16 02:09:04 +01:00
* \return string String with info text
2008-08-06 16:50:06 +02:00
*/
function info_admin ( $texte , $infoonimgalt = 0 )
{
global $conf , $langs ;
$s = '' ;
if ( $infoonimgalt )
{
$s .= img_picto ( $texte , 'star' );
}
else
{
$s .= '<div class="info">' ;
$s .= img_picto ( $langs -> trans ( " InfoAdmin " ), 'star' );
$s .= ' ' ;
$s .= $texte ;
$s .= '</div>' ;
}
return $s ;
}
/**
2009-11-29 17:36:01 +01:00
* \brief Check permissions of a user to show a page and an object . Check read permission
2010-11-05 11:48:20 +01:00
* If $_REQUEST [ 'action' ] defined , we also check write and delete permission .
2008-10-13 16:14:44 +02:00
* \param user User to check
2009-10-16 15:09:56 +02:00
* \param features Features to check ( in most cases , it ' s module name )
2010-06-09 20:25:20 +02:00
* \param objectid Object ID if we want to check permission on a particular record ( optionnal )
2009-04-27 22:37:50 +02:00
* \param dbtablename Table name where object is stored . Not used if objectid is null ( optionnal )
2009-11-29 17:36:01 +01:00
* \param feature2 Feature to check ( second level of permission )
2009-05-07 16:33:52 +02:00
* \param dbt_keyfield Field name for socid foreign key if not fk_soc . ( optionnal )
2009-04-27 22:37:50 +02:00
* \param dbt_select Field name for select if not rowid . ( optionnal )
2010-11-07 13:33:32 +01:00
* @ return int Always 1 , die process if not allowed
2008-08-06 16:50:06 +02:00
*/
2009-10-16 15:09:56 +02:00
function restrictedArea ( $user , $features = 'societe' , $objectid = 0 , $dbtablename = '' , $feature2 = '' , $dbt_keyfield = 'fk_soc' , $dbt_select = 'rowid' )
2008-08-06 16:50:06 +02:00
{
2009-04-27 22:37:50 +02:00
global $db , $conf ;
2009-04-29 20:02:50 +02:00
2009-05-04 12:27:35 +02:00
//dol_syslog("functions.lib:restrictedArea $feature, $objectid, $dbtablename,$feature2,$dbt_socfield,$dbt_select");
2009-04-27 22:37:50 +02:00
if ( $dbt_select != 'rowid' ) $objectid = " ' " . $objectid . " ' " ;
2008-08-06 16:50:06 +02:00
2010-11-10 11:53:39 +01:00
//print "user_id=".$user->id.", features=".$features.", feature2=".$feature2.", objectid=".$objectid;
2009-05-07 16:33:52 +02:00
//print ", dbtablename=".$dbtablename.", dbt_socfield=".$dbt_keyfield.", dbt_select=".$dbt_select;
2010-11-10 11:53:39 +01:00
//print ", perm: ".$features."->".$feature2."=".$user->rights->$features->$feature2->lire."<br>";
2009-10-20 17:23:32 +02:00
2009-10-16 15:09:56 +02:00
// More features to check
2009-10-16 15:15:26 +02:00
$features = explode ( " & " , $features );
2010-10-29 10:11:00 +02:00
//var_dump($features);
2009-10-20 17:23:32 +02:00
2008-08-06 16:50:06 +02:00
// Check read permission from module
// TODO Replace "feature" param by permission for reading
$readok = 1 ;
2009-10-16 15:09:56 +02:00
foreach ( $features as $feature )
2008-08-06 16:50:06 +02:00
{
if ( $feature == 'societe' )
{
2009-10-16 15:09:56 +02:00
if ( ! $user -> rights -> societe -> lire && ! $user -> rights -> fournisseur -> lire ) $readok = 0 ;
2008-08-06 16:50:06 +02:00
}
else if ( $feature == 'contact' )
{
2009-10-16 15:09:56 +02:00
if ( ! $user -> rights -> societe -> contact -> lire ) $readok = 0 ;
2008-08-06 16:50:06 +02:00
}
2009-06-08 20:14:37 +02:00
else if ( $feature == 'produit|service' )
{
2009-10-16 15:09:56 +02:00
if ( ! $user -> rights -> produit -> lire && ! $user -> rights -> service -> lire ) $readok = 0 ;
2009-06-08 20:14:37 +02:00
}
2008-08-06 16:50:06 +02:00
else if ( $feature == 'prelevement' )
{
2009-10-16 15:09:56 +02:00
if ( ! $user -> rights -> prelevement -> bons -> lire ) $readok = 0 ;
2008-08-06 16:50:06 +02:00
}
else if ( $feature == 'commande_fournisseur' )
{
2009-10-16 15:09:56 +02:00
if ( ! $user -> rights -> fournisseur -> commande -> lire ) $readok = 0 ;
2008-08-06 16:50:06 +02:00
}
2009-10-16 15:09:56 +02:00
else if ( $feature == 'cheque' )
2008-08-06 16:50:06 +02:00
{
2009-10-16 15:09:56 +02:00
if ( ! $user -> rights -> banque -> cheque ) $readok = 0 ;
2008-08-06 16:50:06 +02:00
}
2009-10-16 15:09:56 +02:00
else if ( $feature == 'ecm' )
2008-08-06 16:50:06 +02:00
{
2009-10-16 15:09:56 +02:00
if ( ! $user -> rights -> ecm -> download ) $readok = 0 ;
2008-08-06 16:50:06 +02:00
}
2010-09-18 15:38:43 +02:00
else if ( $feature == 'projet' )
{
if ( ! $user -> rights -> projet -> lire && ! $user -> rights -> projet -> all -> lire ) $readok = 0 ;
}
2008-08-19 15:35:16 +02:00
else if ( ! empty ( $feature2 )) // This should be used for future changes
{
2009-10-16 15:09:56 +02:00
if ( empty ( $user -> rights -> $feature -> $feature2 -> lire )
&& empty ( $user -> rights -> $feature -> $feature2 -> read )) $readok = 0 ;
2008-08-19 15:35:16 +02:00
}
2009-10-16 15:09:56 +02:00
else if ( ! empty ( $feature ) && ( $feature != 'user' && $feature != 'usergroup' )) // This is for old permissions
2008-08-06 16:50:06 +02:00
{
2009-10-16 15:09:56 +02:00
if ( empty ( $user -> rights -> $feature -> lire )
2010-01-21 23:19:16 +01:00
&& empty ( $user -> rights -> $feature -> read )
&& empty ( $user -> rights -> $feature -> run )) $readok = 0 ;
2008-08-06 16:50:06 +02:00
}
}
2009-10-16 15:09:56 +02:00
if ( ! $readok )
2008-08-06 16:50:06 +02:00
{
2009-10-16 15:09:56 +02:00
//print "Read access is down";
accessforbidden ();
}
//print "Read access is ok";
2009-04-29 20:02:50 +02:00
2009-10-16 15:09:56 +02:00
// Check write permission from module
$createok = 1 ;
2010-10-29 10:11:00 +02:00
if ( GETPOST ( " action " ) && GETPOST ( " action " ) == 'create' )
2009-10-16 15:09:56 +02:00
{
foreach ( $features as $feature )
2008-08-06 16:50:06 +02:00
{
2010-11-05 11:48:20 +01:00
if ( $feature == 'contact' )
2009-04-27 22:37:50 +02:00
{
2009-10-16 15:09:56 +02:00
if ( ! $user -> rights -> societe -> contact -> creer ) $createok = 0 ;
2009-04-27 22:37:50 +02:00
}
2009-10-16 15:09:56 +02:00
else if ( $feature == 'produit|service' )
2008-08-06 16:50:06 +02:00
{
2009-10-16 15:09:56 +02:00
if ( ! $user -> rights -> produit -> creer && ! $user -> rights -> service -> creer ) $createok = 0 ;
2008-08-06 16:50:06 +02:00
}
2009-10-16 15:09:56 +02:00
else if ( $feature == 'prelevement' )
2008-08-06 16:50:06 +02:00
{
2009-10-16 15:09:56 +02:00
if ( ! $user -> rights -> prelevement -> bons -> creer ) $createok = 0 ;
2008-08-06 16:50:06 +02:00
}
2009-10-16 15:09:56 +02:00
else if ( $feature == 'commande_fournisseur' )
2008-08-06 16:50:06 +02:00
{
2009-10-16 15:09:56 +02:00
if ( ! $user -> rights -> fournisseur -> commande -> creer ) $createok = 0 ;
2008-08-06 16:50:06 +02:00
}
2009-10-16 15:09:56 +02:00
else if ( $feature == 'banque' )
2009-04-27 22:37:50 +02:00
{
2009-10-16 15:09:56 +02:00
if ( ! $user -> rights -> banque -> modifier ) $createok = 0 ;
}
else if ( $feature == 'cheque' )
{
if ( ! $user -> rights -> banque -> cheque ) $createok = 0 ;
}
else if ( ! empty ( $feature2 )) // This should be used for future changes
{
if ( empty ( $user -> rights -> $feature -> $feature2 -> creer )
&& empty ( $user -> rights -> $feature -> $feature2 -> write )) $createok = 0 ;
}
else if ( ! empty ( $feature )) // This is for old permissions
{
2010-10-29 10:11:00 +02:00
//print '<br>feature='.$feature.' creer='.$user->rights->$feature->creer.' write='.$user->rights->$feature->write;
2009-10-16 15:09:56 +02:00
if ( empty ( $user -> rights -> $feature -> creer )
&& empty ( $user -> rights -> $feature -> write )) $createok = 0 ;
2009-04-27 22:37:50 +02:00
}
2008-08-06 16:50:06 +02:00
}
2009-10-20 17:23:32 +02:00
2009-10-16 15:09:56 +02:00
if ( ! $createok ) accessforbidden ();
//print "Write access is ok";
}
2010-11-06 18:07:06 +01:00
2010-11-05 16:36:43 +01:00
// Check create user permission
$createuserok = 1 ;
if ( GETPOST ( " action " ) && ( GETPOST ( " action " ) == 'confirm_create_user' && GETPOST ( " confirm " ) == 'yes' ) )
{
if ( ! $user -> rights -> user -> user -> creer ) $createuserok = 0 ;
2010-11-06 18:07:06 +01:00
2010-11-05 16:36:43 +01:00
if ( ! $createuserok ) accessforbidden ();
//print "Create user access is ok";
}
2010-11-06 18:07:06 +01:00
2010-11-05 11:48:20 +01:00
// Check delete permission from module
$deleteok = 1 ;
2010-11-05 16:36:43 +01:00
if ( GETPOST ( " action " ) && ( ( GETPOST ( " action " ) == 'confirm_delete' && GETPOST ( " confirm " ) && GETPOST ( " confirm " ) == 'yes' ) || GETPOST ( " action " ) == 'delete' ) )
2010-11-05 11:48:20 +01:00
{
foreach ( $features as $feature )
{
if ( $feature == 'contact' )
{
if ( ! $user -> rights -> societe -> contact -> supprimer ) $deleteok = 0 ;
}
else if ( $feature == 'produit|service' )
{
if ( ! $user -> rights -> produit -> supprimer && ! $user -> rights -> service -> supprimer ) $deleteok = 0 ;
}
else if ( $feature == 'commande_fournisseur' )
{
if ( ! $user -> rights -> fournisseur -> commande -> supprimer ) $deleteok = 0 ;
}
else if ( $feature == 'banque' )
{
if ( ! $user -> rights -> banque -> modifier ) $deleteok = 0 ;
}
else if ( $feature == 'cheque' )
{
if ( ! $user -> rights -> banque -> cheque ) $deleteok = 0 ;
}
2011-03-28 10:25:54 +02:00
else if ( $feature == 'ecm' )
{
if ( ! $user -> rights -> ecm -> upload ) $deleteok = 0 ;
}
2010-11-05 11:48:20 +01:00
else if ( ! empty ( $feature2 )) // This should be used for future changes
{
if ( empty ( $user -> rights -> $feature -> $feature2 -> supprimer )
&& empty ( $user -> rights -> $feature -> $feature2 -> delete )) $deleteok = 0 ;
}
else if ( ! empty ( $feature )) // This is for old permissions
{
//print '<br>feature='.$feature.' creer='.$user->rights->$feature->supprimer.' write='.$user->rights->$feature->delete;
if ( empty ( $user -> rights -> $feature -> supprimer )
&& empty ( $user -> rights -> $feature -> delete )) $deleteok = 0 ;
}
}
2010-11-10 11:53:39 +01:00
//print "Delete access is ko";
2010-11-05 11:48:20 +01:00
if ( ! $deleteok ) accessforbidden ();
//print "Delete access is ok";
}
2008-08-06 16:50:06 +02:00
2009-11-29 17:36:01 +01:00
// If we have a particular object to check permissions on, we check this object
// is linked to a company allowed to $user.
2009-11-29 17:53:46 +01:00
if ( ! empty ( $objectid ) && $objectid > 0 )
2009-10-16 15:09:56 +02:00
{
foreach ( $features as $feature )
2008-08-06 16:50:06 +02:00
{
2009-10-16 15:09:56 +02:00
$sql = '' ;
2009-11-29 17:36:01 +01:00
2011-03-16 11:57:12 +01:00
$check = array ( 'banque' , 'user' , 'usergroup' , 'produit' , 'service' , 'produit|service' ); // Test on entity only (Objects with no link to company)
2010-09-18 15:38:43 +02:00
$checksoc = array ( 'societe' ); // Test for societe object
$checkother = array ( 'contact' ); // Test on entity and link to societe. Allowed if link is empty (Ex: contacts...).
$checkproject = array ( 'projet' ); // Test for project object
2009-11-29 20:05:22 +01:00
$nocheck = array ( 'categorie' , 'barcode' , 'stock' , 'fournisseur' ); // No test
2010-09-18 15:38:43 +02:00
$checkdefault = 'all other not already defined' ; // Test on entity and link to societe. Not allowed if link is empty (Ex: invoice, orders...).
2009-10-20 17:23:32 +02:00
2009-10-16 15:09:56 +02:00
// If dbtable not defined, we use same name for table than module name
if ( empty ( $dbtablename )) $dbtablename = $feature ;
2009-10-20 17:23:32 +02:00
2009-10-16 15:09:56 +02:00
// Check permission for object with entity
2009-11-28 13:06:11 +01:00
if ( in_array ( $feature , $check ))
2008-08-06 16:50:06 +02:00
{
2009-10-16 15:09:56 +02:00
$sql = " SELECT dbt. " . $dbt_select ;
$sql .= " FROM " . MAIN_DB_PREFIX . $dbtablename . " as dbt " ;
$sql .= " WHERE dbt. " . $dbt_select . " = " . $objectid ;
$sql .= " AND dbt.entity IN (0, " . $conf -> entity . " ) " ;
}
2009-11-29 20:05:22 +01:00
else if ( in_array ( $feature , $checksoc ))
2009-10-16 15:09:56 +02:00
{
// If external user: Check permission for external users
if ( $user -> societe_id > 0 )
{
if ( $user -> societe_id <> $objectid ) accessforbidden ();
}
// If internal user: Check permission for internal users that are restricted on their objects
else if ( ! $user -> rights -> societe -> client -> voir )
{
$sql = " SELECT sc.fk_soc " ;
$sql .= " FROM ( " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " , " . MAIN_DB_PREFIX . " societe as s) " ;
$sql .= " WHERE sc.fk_soc = " . $objectid ;
$sql .= " AND sc.fk_user = " . $user -> id ;
$sql .= " AND sc.fk_soc = s.rowid " ;
$sql .= " AND s.entity = " . $conf -> entity ;
}
// If multicompany and internal users with all permissions, check user is in correct entity
else if ( $conf -> global -> MAIN_MODULE_MULTICOMPANY )
{
$sql = " SELECT s.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " societe as s " ;
$sql .= " WHERE s.rowid = " . $objectid ;
$sql .= " AND s.entity = " . $conf -> entity ;
}
2008-08-06 16:50:06 +02:00
}
2009-11-29 20:05:22 +01:00
else if ( in_array ( $feature , $checkother ))
2009-11-29 17:36:01 +01:00
{
// If external user: Check permission for external users
if ( $user -> societe_id > 0 )
{
2009-11-29 20:05:22 +01:00
$sql = " SELECT dbt.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $dbtablename . " as dbt " ;
$sql .= " WHERE dbt.rowid = " . $objectid ;
$sql .= " AND dbt.fk_soc = " . $user -> societe_id ;
2009-11-29 17:36:01 +01:00
}
// If internal user: Check permission for internal users that are restricted on their objects
else if ( ! $user -> rights -> societe -> client -> voir )
{
2009-11-29 20:05:22 +01:00
$sql = " SELECT dbt.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $dbtablename . " as dbt " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe_commerciaux as sc ON dbt.fk_soc = sc.fk_soc AND sc.fk_user = ' " . $user -> id . " ' " ;
$sql .= " WHERE dbt.rowid = " . $objectid ;
$sql .= " AND (dbt.fk_soc IS NULL OR sc.fk_soc IS NOT NULL) " ; // Contact not linked to a company or to a company of user
$sql .= " AND dbt.entity = " . $conf -> entity ;
2009-11-29 17:36:01 +01:00
}
// If multicompany and internal users with all permissions, check user is in correct entity
else if ( $conf -> global -> MAIN_MODULE_MULTICOMPANY )
{
2009-11-29 20:05:22 +01:00
$sql = " SELECT dbt.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $dbtablename . " as dbt " ;
$sql .= " WHERE dbt.rowid = " . $objectid ;
$sql .= " AND dbt.entity = " . $conf -> entity ;
2009-11-29 17:36:01 +01:00
}
}
2010-09-18 15:38:43 +02:00
else if ( in_array ( $feature , $checkproject ))
{
if ( ! $user -> rights -> projet -> all -> lire )
{
include_once ( DOL_DOCUMENT_ROOT . " /projet/class/project.class.php " );
$projectstatic = new Project ( $db );
$tmps = $projectstatic -> getProjectsAuthorizedForUser ( $user , 0 , 1 , $user -> societe_id );
$tmparray = explode ( ',' , $tmps );
if ( ! in_array ( $objectid , $tmparray )) accessforbidden ();
}
}
2009-11-28 13:06:11 +01:00
else if ( ! in_array ( $feature , $nocheck ))
2008-08-06 16:50:06 +02:00
{
2009-10-16 15:09:56 +02:00
// If external user: Check permission for external users
if ( $user -> societe_id > 0 )
{
2009-11-29 17:36:01 +01:00
$sql = " SELECT dbt. " . $dbt_keyfield ;
2009-10-16 15:09:56 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . $dbtablename . " as dbt " ;
$sql .= " WHERE dbt.rowid = " . $objectid ;
2009-11-29 17:36:01 +01:00
$sql .= " AND dbt. " . $dbt_keyfield . " = " . $user -> societe_id ;
2009-10-16 15:09:56 +02:00
}
// If internal user: Check permission for internal users that are restricted on their objects
else if ( ! $user -> rights -> societe -> client -> voir )
{
$sql = " SELECT sc.fk_soc " ;
2009-11-28 08:30:45 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . $dbtablename . " as dbt " ;
$sql .= " , " . MAIN_DB_PREFIX . " societe as s " ;
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
2009-11-28 13:06:11 +01:00
$sql .= " WHERE dbt. " . $dbt_select . " = " . $objectid ;
2009-11-28 08:30:45 +01:00
$sql .= " AND sc.fk_soc = dbt. " . $dbt_keyfield ;
2009-11-29 17:36:01 +01:00
$sql .= " AND dbt. " . $dbt_keyfield . " = s.rowid " ;
2009-10-16 15:09:56 +02:00
$sql .= " AND s.entity = " . $conf -> entity ;
2009-11-29 17:36:01 +01:00
$sql .= " AND sc.fk_user = " . $user -> id ;
2009-10-16 15:09:56 +02:00
}
// If multicompany and internal users with all permissions, check user is in correct entity
else if ( $conf -> global -> MAIN_MODULE_MULTICOMPANY )
{
$sql = " SELECT dbt. " . $dbt_select ;
$sql .= " FROM " . MAIN_DB_PREFIX . $dbtablename . " as dbt " ;
$sql .= " WHERE dbt. " . $dbt_select . " = " . $objectid ;
$sql .= " AND dbt.entity = " . $conf -> entity ;
}
}
2009-10-20 17:23:32 +02:00
2009-10-16 15:09:56 +02:00
//print $sql."<br>";
if ( $sql )
{
$resql = $db -> query ( $sql );
if ( $resql )
{
if ( $db -> num_rows ( $resql ) == 0 ) accessforbidden ();
}
else
{
dol_syslog ( " functions.lib:restrictedArea sql= " . $sql , LOG_ERR );
accessforbidden ();
}
2008-08-06 16:50:06 +02:00
}
}
}
return 1 ;
}
/**
2010-11-07 13:33:32 +01:00
* Affiche message erreur de type acces interdit et arrete le programme
* L ' appel a cette fonction termine le code .
* @ param message Force error message
* @ param printheader Affiche avant le header
2008-08-06 16:50:06 +02:00
*/
2009-07-15 15:02:51 +02:00
function accessforbidden ( $message = '' , $printheader = 1 , $printfooter = 1 , $showonlymessage = 0 )
2008-08-06 16:50:06 +02:00
{
2010-04-05 03:01:28 +02:00
global $conf , $db , $user , $langs ;
if ( ! is_object ( $langs ))
{
2010-04-28 12:11:41 +02:00
include_once ( DOL_DOCUMENT_ROOT . '/core/class/translate.class.php' );
2010-04-05 03:01:28 +02:00
$langs = new Translate ( '' , $conf );
}
2008-08-06 16:50:06 +02:00
$langs -> load ( " other " );
2009-07-15 15:02:51 +02:00
if ( $printheader )
{
if ( function_exists ( " llxHeader " )) llxHeader ( '' );
else if ( function_exists ( " llxHeaderVierge " )) llxHeaderVierge ( '' );
}
2008-08-06 16:50:06 +02:00
print '<div class="error">' ;
if ( ! $message ) print $langs -> trans ( " ErrorForbidden " );
else print $message ;
print '</div>' ;
print '<br>' ;
2009-07-15 15:02:51 +02:00
if ( empty ( $showonlymessage ))
2008-08-06 16:50:06 +02:00
{
2009-07-15 15:02:51 +02:00
if ( $user -> login )
{
print $langs -> trans ( " CurrentLogin " ) . ': <font class="error">' . $user -> login . '</font><br>' ;
print $langs -> trans ( " ErrorForbidden2 " , $langs -> trans ( " Home " ), $langs -> trans ( " Users " ));
}
else
{
print $langs -> trans ( " ErrorForbidden3 " );
}
2008-08-06 16:50:06 +02:00
}
2009-07-15 15:02:51 +02:00
if ( $printfooter && function_exists ( " llxFooter " )) llxFooter ( '' );
2008-08-06 16:50:06 +02:00
exit ( 0 );
}
2009-04-20 23:05:38 +02:00
/* For backward compatibility */
function dolibarr_print_error ( $db = '' , $error = '' )
{
return dol_print_error ( $db , $error );
}
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 " .
* @ param db Database handler
* @ param error String or array of errors strings to show
2010-12-21 01:44:57 +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
{
global $conf , $langs , $argv ;
2010-10-03 23:43:03 +02:00
global $dolibarr_main_prod ;
2010-10-20 20:55:12 +02:00
2009-08-25 16:01:43 +02:00
$out = '' ;
2008-08-06 16:50:06 +02:00
$syslog = '' ;
// Si erreur intervenue avant chargement langue
if ( ! $langs )
{
2010-04-28 12:11:41 +02:00
require_once ( DOL_DOCUMENT_ROOT . " /core/class/translate.class.php " );
2008-08-06 16:50:06 +02:00
$langs = new Translate ( " " , $conf );
2009-02-21 01:11:13 +01:00
$langs -> load ( " main " );
2008-08-06 16:50:06 +02:00
}
2010-07-28 23:03:08 +02:00
$langs -> load ( " main " );
$langs -> load ( " errors " );
2008-08-06 16:50:06 +02:00
if ( $_SERVER [ 'DOCUMENT_ROOT' ]) // Mode web
{
2009-08-25 16:01:43 +02:00
$out .= $langs -> trans ( " DolibarrHasDetectedError " ) . " .<br> \n " ;
2009-01-15 00:36:51 +01:00
if ( ! empty ( $conf -> global -> MAIN_FEATURES_LEVEL ))
2009-08-25 16:01:43 +02:00
$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 " ;
2010-07-28 23:03:08 +02:00
$out .= " <b> " . $langs -> trans ( " Date " ) . " :</b> " . dol_print_date ( time (), 'dayhourlog' ) . " <br> \n " ;;
2009-08-25 16:01:43 +02:00
$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 " ;;
2010-07-28 23:03:08 +02:00
if ( function_exists ( " phpversion " ))
{
$out .= " <b> " . $langs -> trans ( " PHP " ) . " :</b> " . phpversion () . " <br> \n " ;
2010-07-28 23:58:18 +02:00
//phpinfo(); // This is to show location of php.ini file
2010-07-28 23:03:08 +02:00
}
2009-08-25 16:01:43 +02:00
$out .= " <b> " . $langs -> trans ( " Server " ) . " :</b> " . $_SERVER [ " SERVER_SOFTWARE " ] . " <br> \n " ;;
2010-07-28 23:03:08 +02:00
$out .= " <br> \n " ;
2009-08-25 16:01:43 +02:00
$out .= " <b> " . $langs -> trans ( " RequestedUrl " ) . " :</b> " . $_SERVER [ " REQUEST_URI " ] . " <br> \n " ;;
2010-08-31 23:47:18 +02:00
$out .= " <b> " . $langs -> trans ( " Referer " ) . " :</b> " . ( isset ( $_SERVER [ " HTTP_REFERER " ]) ? $_SERVER [ " HTTP_REFERER " ] : '' ) . " <br> \n " ;;
2010-09-29 12:10:33 +02:00
//$out.="<b>".$langs->trans("MenuManager").":</b> ".$conf->top_menu.($conf->top_menu||$conf->left_menu?'/':'').$conf->left_menu."<br>\n";
$out .= " <b> " . $langs -> trans ( " MenuManager " ) . " :</b> " . $conf -> top_menu . " <br> \n " ;
2009-08-25 16:01:43 +02:00
$out .= " <br> \n " ;
2008-08-06 16:50:06 +02:00
$syslog .= " url= " . $_SERVER [ " REQUEST_URI " ];
$syslog .= " , query_string= " . $_SERVER [ " QUERY_STRING " ];
}
else // Mode CLI
{
2009-08-25 16:01:43 +02:00
$out .= '> ' . $langs -> transnoentities ( " ErrorInternalErrorDetected " ) . " : \n " . $argv [ 0 ] . " \n " ;
2008-08-06 16:50:06 +02:00
$syslog .= " pid= " . getmypid ();
}
if ( is_object ( $db ))
{
if ( $_SERVER [ 'DOCUMENT_ROOT' ]) // Mode web
{
2009-08-25 16:01:43 +02:00
$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 " ;
2008-08-06 16:50:06 +02:00
}
else // Mode CLI
{
2009-08-25 16:01:43 +02:00
$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 " ;
2008-08-06 16:50:06 +02:00
}
$syslog .= " , sql= " . $db -> lastquery ();
$syslog .= " , db_error= " . $db -> lasterror ();
}
if ( $error )
{
$langs -> load ( " errors " );
2009-01-15 00:36:51 +01:00
2008-08-06 16:50:06 +02:00
if ( is_array ( $error )) $errors = $error ;
else $errors = array ( $error );
foreach ( $errors as $msg )
{
$msg = $langs -> trans ( $msg );
if ( $_SERVER [ 'DOCUMENT_ROOT' ]) // Mode web
{
2009-08-25 16:01:43 +02:00
$out .= " <b> " . $langs -> trans ( " Message " ) . " :</b> " . $msg . " <br> \n " ;
2008-08-06 16:50:06 +02:00
}
else // Mode CLI
{
2009-08-25 16:01:43 +02:00
$out .= '> ' . $langs -> transnoentities ( " Message " ) . " : \n " . $msg . " \n " ;
2008-08-06 16:50:06 +02:00
}
$syslog .= " , msg= " . $msg ;
}
}
2010-10-03 23:43:03 +02:00
if ( empty ( $dolibarr_main_prod ) && $_SERVER [ 'DOCUMENT_ROOT' ] && function_exists ( 'xdebug_call_file' ))
2009-02-21 02:04:35 +01:00
{
2009-10-17 15:47:17 +02:00
xdebug_print_function_stack ();
2009-08-25 16:01:43 +02:00
$out .= '<b>XDebug informations:</b>' . " <br> \n " ;
$out .= 'File: ' . xdebug_call_file () . " <br> \n " ;
$out .= 'Line: ' . xdebug_call_line () . " <br> \n " ;
2009-10-17 15:47:17 +02:00
$out .= 'Function: ' . xdebug_call_function () . " <br> \n " ;
2009-08-25 16:01:43 +02:00
$out .= " <br> \n " ;
2009-02-21 02:04:35 +01:00
}
2009-01-15 00:36:51 +01:00
2009-11-11 16:46:56 +01:00
if ( empty ( $dolibarr_main_prod )) print $out ;
2010-10-03 23:43:03 +02:00
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.';
2009-02-20 21:28:16 +01:00
dol_syslog ( " Error " . $syslog , LOG_ERR );
2008-08-06 16:50:06 +02:00
}
2009-10-10 17:32:28 +02:00
/**
* Show email to contact if technical error
*/
function dol_print_error_email ()
{
global $langs , $conf ;
$langs -> load ( " errors " );
2009-10-10 17:45:18 +02:00
print '<br><div class="error">' . $langs -> trans ( " ErrorContactEMail " , $conf -> global -> MAIN_INFO_SOCIETE_MAIL , 'ERRORNEWPAYMENT' . dol_print_date ( mktime (), '%Y%m%d' )) . '</div>' ;
2009-10-10 17:32:28 +02:00
}
2008-08-06 16:50:06 +02:00
/**
2008-11-16 19:55:00 +01:00
* \brief Show title line of an array
* \param name libelle champ
* \param file url pour clic sur tri
* \param field champ de tri
* \param begin ( " " par defaut )
* \param options ( " " par defaut )
* \param td options de l ' attribut td ( " " par defaut )
2009-01-15 00:36:51 +01:00
* \param sortfield field currently used to sort
2008-11-16 19:55:00 +01:00
* \param sortorder ordre du tri
2008-08-06 16:50:06 +02:00
*/
function print_liste_field_titre ( $name , $file , $field , $begin = " " , $options = " " , $td = " " , $sortfield = " " , $sortorder = " " )
{
global $conf ;
//print "$name, $file, $field, $begin, $options, $td, $sortfield, $sortorder<br>\n";
2008-11-16 01:06:26 +01:00
// Le champ de tri est mis en evidence.
2008-08-06 16:50:06 +02:00
// Exemple si (sortfield,field)=("nom","xxx.nom") ou (sortfield,field)=("nom","nom")
2009-10-21 20:21:56 +02:00
if ( $field && ( $sortfield == $field || $sortfield == preg_replace ( " /^[^ \ .]+ \ ./ " , " " , $field )))
2008-08-06 16:50:06 +02:00
{
print '<td class="liste_titre_sel" ' . $td . '>' ;
}
else
{
print '<td class="liste_titre" ' . $td . '>' ;
}
print $name ;
// If this is a sort field
if ( $field )
{
//print " ";
2009-01-25 19:16:09 +01:00
print '<img width="2" src="' . DOL_URL_ROOT . '/theme/common/transparent.png" alt="">' ;
2008-08-06 16:50:06 +02:00
if ( ! $sortorder )
{
print '<a href="' . $file . '?sortfield=' . $field . '&sortorder=asc&begin=' . $begin . $options . '">' . img_down ( " A-Z " , 0 ) . '</a>' ;
print '<a href="' . $file . '?sortfield=' . $field . '&sortorder=desc&begin=' . $begin . $options . '">' . img_up ( " Z-A " , 0 ) . '</a>' ;
}
else
{
if ( $field != $sortfield )
{
print '<a href="' . $file . '?sortfield=' . $field . '&sortorder=asc&begin=' . $begin . $options . '">' . img_down ( " A-Z " , 0 ) . '</a>' ;
print '<a href="' . $file . '?sortfield=' . $field . '&sortorder=desc&begin=' . $begin . $options . '">' . img_up ( " Z-A " , 0 ) . '</a>' ;
}
else {
$sortorder = strtoupper ( $sortorder );
if ( $sortorder == 'DESC' ) {
print '<a href="' . $file . '?sortfield=' . $field . '&sortorder=asc&begin=' . $begin . $options . '">' . img_down ( " A-Z " , 0 ) . '</a>' ;
print '<a href="' . $file . '?sortfield=' . $field . '&sortorder=desc&begin=' . $begin . $options . '">' . img_up ( " Z-A " , 1 ) . '</a>' ;
}
if ( $sortorder == 'ASC' ) {
print '<a href="' . $file . '?sortfield=' . $field . '&sortorder=asc&begin=' . $begin . $options . '">' . img_down ( " A-Z " , 1 ) . '</a>' ;
print '<a href="' . $file . '?sortfield=' . $field . '&sortorder=desc&begin=' . $begin . $options . '">' . img_up ( " Z-A " , 0 ) . '</a>' ;
}
}
}
}
print " </td> " ;
}
/**
2010-12-05 21:33:24 +01:00
* Show a title ( deprecated . use print_fiche_titre instrad )
* @ param titre Title to show
2008-08-06 16:50:06 +02:00
*/
function print_titre ( $titre )
{
print '<div class="titre">' . $titre . '</div>' ;
}
2010-05-02 09:34:08 +02:00
/**
2010-12-05 21:33:24 +01:00
* Show a title with picto
* @ param titre Title to show
* @ param mesg Added message to show on right
* @ param picto Icon to use before title ( should be a 32 x32 transparent png file )
* @ param pictoisfullpath 1 = Icon name is a full absolute url of image
* @ param id To force an id on html objects
2010-05-02 09:34:08 +02:00
*/
function print_fiche_titre ( $titre , $mesg = '' , $picto = 'title.png' , $pictoisfullpath = 0 , $id = '' )
{
print load_fiche_titre ( $titre , $mesg , $picto , $pictoisfullpath , $id );
}
2008-08-06 16:50:06 +02:00
/**
2010-12-05 21:33:24 +01:00
* Load a title with picto
* @ param titre Title to show
* @ param mesg Added message to show on right
* @ param picto Icon to use before title ( should be a 32 x32 transparent png file )
* @ param pictoisfullpath 1 = Icon name is a full absolute url of image
* @ param id To force an id on html objects
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
{
2009-06-14 14:38:45 +02:00
global $conf ;
2010-05-26 16:52:32 +02:00
2010-05-02 06:57:15 +02:00
$return = '' ;
2009-07-26 13:19:11 +02:00
if ( $picto == 'setup' ) $picto = 'title.png' ;
2010-12-31 17:28:05 +01:00
if ( ! empty ( $conf -> browser -> ie ) && $picto == 'title.png' ) $picto = 'title.gif' ;
2009-06-14 14:38:45 +02:00
2010-05-02 06:57:15 +02:00
$return .= " \n " ;
$return .= '<table ' . ( $id ? 'id="' . $id . '" ' : '' ) . 'summary="" width="100%" border="0" class="notopnoleftnoright" style="margin-bottom: 2px;"><tr>' ;
if ( empty ( $conf -> browser -> phone ) && $picto && $titre ) $return .= '<td class="nobordernopadding" 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>' ;
2010-08-24 16:42:18 +02:00
if ( dol_strlen ( $mesg ))
2008-08-06 16:50:06 +02:00
{
2010-05-02 06:57:15 +02:00
$return .= '<td class="nobordernopadding" align="right" valign="middle"><b>' . $mesg . '</b></td>' ;
2008-08-06 16:50:06 +02:00
}
2010-05-02 06:57:15 +02:00
$return .= '</tr></table>' . " \n " ;
2010-05-26 16:52:32 +02:00
2010-05-02 06:57:15 +02:00
return $return ;
}
2008-08-06 16:50:06 +02:00
/**
2008-11-16 19:55:00 +01:00
* \brief Print a title with navigation controls for pagination
* \param titre Title to show ( required )
* \param page Numero of page ( required )
* \param file Url of page ( required )
* \param options parametres complementaires lien ( '' par defaut )
* \param sortfield champ de tri ( '' par defaut )
* \param sortorder ordre de tri ( '' par defaut )
* \param center chaine du centre ( '' par defaut )
* \param num number of records found by select with limit + 1
* \param totalnboflines Total number of records / lines for all pages ( if known )
* \param picto Icon to use before title ( should be a 32 x32 transparent png file )
* \param pictoisfullpath 1 = Icon name is a full absolute url of image
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
{
global $conf , $langs ;
2009-07-26 13:19:11 +02:00
if ( $picto == 'setup' ) $picto = 'title.png' ;
2010-12-31 17:28:05 +01:00
if ( ! empty ( $conf -> browser -> ie ) && $picto == 'title.png' ) $picto = 'title.gif' ;
2008-08-06 16:50:06 +02:00
if ( $num > $conf -> liste_limit or $num == - 1 )
{
$nextpage = 1 ;
}
else
{
$nextpage = 0 ;
}
2009-10-29 18:30:29 +01:00
print " \n " ;
print " <!-- Begin title ' " . $titre . " ' --> \n " ;
2008-11-16 19:55:00 +01:00
print '<table width="100%" border="0" class="notopnoleftnoright" style="margin-bottom: 2px;"><tr>' ;
2009-01-15 00:36:51 +01:00
2008-08-06 16:50:06 +02:00
$pagelist = '' ;
2008-11-16 19:55:00 +01:00
// Left
2008-08-06 16:50:06 +02:00
if ( $page > 0 || $num > $conf -> liste_limit )
{
if ( $totalnboflines )
{
2008-11-16 19:55:00 +01:00
if ( $picto && $titre ) print '<td class="nobordernopadding" width="40" align="left" valign="middle">' . img_picto ( '' , $picto , '' , $pictoisfullpath ) . '</td>' ;
print '<td class="nobordernopadding">' ;
2008-08-06 16:50:06 +02:00
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
{
2010-09-29 12:10:33 +02:00
if ( empty ( $conf -> browser -> phone ) && $picto && $titre ) print '<td class="nobordernopadding" width="40" align="left" valign="middle">' . img_picto ( '' , $picto , '' , $pictoisfullpath ) . '</td>' ;
2008-11-16 19:55:00 +01:00
print '<td class="nobordernopadding">' ;
2008-08-06 16:50:06 +02:00
print '<div class="titre">' . $titre . '</div>' ;
$pagelist .= $langs -> trans ( 'Page' ) . ' ' . ( $page + 1 );
print '</td>' ;
}
}
else
{
2010-09-29 12:10:33 +02:00
if ( empty ( $conf -> browser -> phone ) && $picto && $titre ) print '<td class="nobordernopadding" width="40" align="left" valign="middle">' . img_picto ( '' , $picto , '' , $pictoisfullpath ) . '</td>' ;
2008-11-16 19:55:00 +01:00
print '<td class="nobordernopadding"><div class="titre">' . $titre . '</div></td>' ;
2008-08-06 16:50:06 +02:00
}
2008-11-16 19:55:00 +01:00
// Center
2008-08-06 16:50:06 +02:00
if ( $center )
{
2008-11-16 19:55:00 +01:00
print '<td class="nobordernopadding" align="left" valign="middle">' . $center . '</td>' ;
2008-08-06 16:50:06 +02:00
}
2008-11-16 19:55:00 +01:00
// Right
print '<td class="nobordernopadding" align="right" valign="middle">' ;
2008-08-06 16:50:06 +02:00
if ( $sortfield ) $options .= " &sortfield= " . $sortfield ;
if ( $sortorder ) $options .= " &sortorder= " . $sortorder ;
// Affichage des fleches de navigation
print_fleche_navigation ( $page , $file , $options , $nextpage , $pagelist );
2008-11-16 19:55:00 +01:00
print '</td>' ;
2008-08-06 16:50:06 +02:00
2009-10-29 18:30:29 +01:00
print '</tr></table>' . " \n " ;
print " <!-- End title --> \n \n " ;
2008-08-06 16:50:06 +02:00
}
/**
2008-11-16 19:55:00 +01:00
* \brief Fonction servant a afficher les fleches de navigation dans les pages de listes
* \param page Numero of page
* \param file Lien
* \param options Autres parametres d ' url a propager dans les liens ( " " par defaut )
* \param nextpage Faut - il une page suivante
* \param betweenarraows HTML Content to show between arrows
2008-08-06 16:50:06 +02:00
*/
function print_fleche_navigation ( $page , $file , $options = '' , $nextpage , $betweenarrows = '' )
{
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-11-16 19:55:00 +01:00
/**
2010-09-29 15:14:48 +02:00
* Remove a file or several files with a mask
* @ param file File to delete or mask of file to delete
* @ param disableglob Disable usage of glob like *
* @ param boolean True if file is deleted , False if error
2008-11-16 19:55:00 +01:00
*/
2009-10-04 19:18:09 +02:00
function dol_delete_file ( $file , $disableglob = 0 )
2008-11-16 19:55:00 +01:00
{
2009-11-29 17:36:01 +01:00
//print "x".$file." ".$disableglob;
2008-11-16 19:55:00 +01:00
$ok = true ;
2009-12-15 11:43:05 +01:00
$file_osencoded = dol_osencode ( $file ); // New filename encoded in OS filesystem encoding charset
2009-10-04 19:18:09 +02:00
if ( empty ( $disableglob ))
2008-11-16 19:55:00 +01:00
{
2009-12-15 11:43:05 +01:00
foreach ( glob ( $file_osencoded ) as $filename )
2009-10-04 19:18:09 +02:00
{
$ok = unlink ( $filename ); // The unlink encapsulated by dolibarr
if ( $ok ) dol_syslog ( " Removed file " . $filename , LOG_DEBUG );
2010-01-07 01:06:21 +01:00
else dol_syslog ( " Failed to remove file " . $filename , LOG_WARNING );
2009-10-04 19:18:09 +02:00
}
}
else
{
2009-12-15 11:43:05 +01:00
$ok = unlink ( $file_osencoded ); // The unlink encapsulated by dolibarr
if ( $ok ) dol_syslog ( " Removed file " . $file_osencoded , LOG_DEBUG );
2010-01-07 01:06:21 +01:00
else dol_syslog ( " Failed to remove file " . $file_osencoded , LOG_WARNING );
2008-11-16 19:55:00 +01:00
}
return $ok ;
}
/**
2010-09-29 15:14:48 +02:00
* Remove a directory ( not recursive , so content must be empty ) .
* If directory is not empty , return false
* @ param file Directory to delete
* @ return boolean True if success , false if error
2008-11-16 19:55:00 +01:00
*/
function dol_delete_dir ( $dir )
{
2009-12-15 11:43:05 +01:00
$dir_osencoded = dol_osencode ( $dir );
return rmdir ( $dir_osencoded );
2008-11-16 19:55:00 +01:00
}
/**
2010-09-29 15:14:48 +02:00
* Remove a directory $dir and its subdirectories
* @ param file Dir to delete
* @ param count Counter to count nb of deleted elements
* @ return int Number of files and directory removed
2008-11-16 19:55:00 +01:00
*/
function dol_delete_dir_recursive ( $dir , $count = 0 )
{
2009-12-15 11:43:05 +01:00
dol_syslog ( " functions.lib:dol_delete_dir_recursive " . $dir , LOG_DEBUG );
$dir_osencoded = dol_osencode ( $dir );
if ( $handle = opendir ( " $dir_osencoded " ))
2008-11-16 19:55:00 +01:00
{
while ( false !== ( $item = readdir ( $handle )))
{
2009-12-15 11:43:05 +01:00
if ( ! utf8_check ( $item )) $item = utf8_encode ( $item ); // should be useless
2009-10-12 20:31:45 +02:00
2008-11-16 19:55:00 +01:00
if ( $item != " . " && $item != " .. " )
{
2009-12-15 11:43:05 +01:00
if ( is_dir ( dol_osencode ( " $dir / $item " )))
2008-11-16 19:55:00 +01:00
{
$count = dol_delete_dir_recursive ( " $dir / $item " , $count );
}
else
{
2009-12-15 11:43:05 +01:00
dol_delete_file ( " $dir / $item " , 1 );
2008-11-16 19:55:00 +01:00
$count ++ ;
//echo " removing $dir/$item<br>\n";
}
}
}
closedir ( $handle );
2009-12-15 11:43:05 +01:00
dol_delete_dir ( $dir );
2008-11-16 19:55:00 +01:00
$count ++ ;
//echo "removing $dir<br>\n";
}
//echo "return=".$count;
return $count ;
}
2008-08-06 16:50:06 +02:00
/**
2008-12-06 03:44:59 +01:00
* \brief Fonction qui retourne un taux de tva formate pour visualisation
* \remarks Fonction utilisee dans les pdf et les pages html
2008-08-06 16:50:06 +02:00
* \param rate Rate value to format ( 19.6 19 , 6 19.6 % 19 , 6 % , ... )
* \param foundpercent Add a percent % sign in output
* \param info_bits Miscellanous information on vat
2008-12-06 03:44:59 +01:00
* \return string Chaine avec montant formate ( 19 , 6 ou 19 , 6 % ou 8.5 % * )
2008-08-06 16:50:06 +02:00
*/
function vatrate ( $rate , $addpercent = false , $info_bits = 0 )
{
// Test for compatibility
2009-10-21 18:50:15 +02:00
if ( preg_match ( '/%/' , $rate ))
2008-08-06 16:50:06 +02:00
{
2009-10-21 18:50:15 +02:00
$rate = str_replace ( '%' , '' , $rate );
2008-08-06 16:50:06 +02:00
$addpercent = true ;
}
2009-10-21 18:50:15 +02:00
if ( preg_match ( '/\*/' , $rate ) || preg_match ( '/' . MAIN_LABEL_MENTION_NPR . '/i' , $rate ))
2008-08-06 16:50:06 +02:00
{
2009-10-21 18:50:15 +02:00
$rate = str_replace ( '*' , '' , $rate );
2008-08-06 16:50:06 +02:00
$info_bits |= 1 ;
}
$ret = price ( $rate , 0 , '' , 0 , 0 ) . ( $addpercent ? '%' : '' );
if ( $info_bits & 1 ) $ret .= ' ' . MAIN_LABEL_MENTION_NPR ;
return $ret ;
}
/**
2010-09-30 10:12:03 +02:00
* Fonction qui formate un montant pour visualisation
* Fonction utilisee dans les pdf et les pages html
* @ param amount Montant a formater
* @ param html Type de formatage , html ou pas ( par defaut )
* @ param outlangs Objet langs pour formatage text
* @ param trunc 1 = Tronque affichage si trop de decimales , 0 = Force le non troncage
* @ param rounding Minimum number of decimal . If not defined we use min ( $conf -> global -> MAIN_MAX_DECIMALS_UNIT , $conf -> global -> MAIN_MAX_DECIMALS_TOTAL )
* @ param forcerounding Force the number of decimal
* @ return string Chaine avec montant formate
* @ see price2num Revert function of price
*/
function price ( $amount , $html = 0 , $outlangs = '' , $trunc = 1 , $rounding =- 1 , $forcerounding =- 1 )
2008-08-06 16:50:06 +02:00
{
global $langs , $conf ;
2009-11-18 00:03:39 +01:00
// Clean parameters
if ( empty ( $amount )) $amount = 0 ; // To have a numeric value if amount not defined or = ''
2009-11-16 01:19:30 +01:00
if ( $rounding < 0 ) $rounding = min ( $conf -> global -> MAIN_MAX_DECIMALS_UNIT , $conf -> global -> MAIN_MAX_DECIMALS_TOT );
2009-11-18 00:03:39 +01:00
2008-08-28 01:00:37 +02:00
$nbdecimal = $rounding ;
2008-10-09 19:29:32 +02:00
2008-08-17 23:13:21 +02:00
// Output separators by default (french)
$dec = ',' ; $thousand = ' ' ;
2008-08-06 16:50:06 +02:00
2008-09-08 09:37:53 +02:00
// If $outlangs not forced, we use use language
2008-08-06 16:50:06 +02:00
if ( ! is_object ( $outlangs )) $outlangs = $langs ;
if ( $outlangs -> trans ( " SeparatorDecimal " ) != " SeparatorDecimal " ) $dec = $outlangs -> trans ( " SeparatorDecimal " );
if ( $outlangs -> trans ( " SeparatorThousand " ) != " SeparatorThousand " ) $thousand = $outlangs -> trans ( " SeparatorThousand " );
2010-02-23 21:30:55 +01:00
if ( $thousand == 'None' ) $thousand = '' ;
//print "amount=".$amount." html=".$html." trunc=".$trunc." nbdecimal=".$nbdecimal." dec='".$dec."' thousand='".$thousand."'<br>";
2008-08-06 16:50:06 +02:00
//print "amount=".$amount."-";
2009-10-21 18:50:15 +02:00
$amount = str_replace ( ',' , '.' , $amount ); // should be useless
2008-08-06 16:50:06 +02:00
//print $amount."-";
2009-10-20 15:14:44 +02:00
$datas = explode ( '.' , $amount );
2009-01-21 14:06:34 +01:00
$decpart = isset ( $datas [ 1 ]) ? $datas [ 1 ] : '' ;
2009-10-21 18:50:15 +02:00
$decpart = preg_replace ( '/0+$/i' , '' , $decpart ); // Supprime les 0 de fin de partie decimale
2008-08-06 16:50:06 +02:00
//print "decpart=".$decpart."<br>";
$end = '' ;
2008-09-08 09:37:53 +02:00
// We increase nbdecimal if there is more decimal than asked (to not loose information)
2010-08-24 16:42:18 +02:00
if ( dol_strlen ( $decpart ) > $nbdecimal ) $nbdecimal = dol_strlen ( $decpart );
2008-08-06 16:50:06 +02:00
// Si on depasse max
if ( $trunc && $nbdecimal > $conf -> global -> MAIN_MAX_DECIMALS_SHOWN )
{
$nbdecimal = $conf -> global -> MAIN_MAX_DECIMALS_SHOWN ;
2009-10-21 18:50:15 +02:00
if ( preg_match ( '/\.\.\./i' , $conf -> global -> MAIN_MAX_DECIMALS_SHOWN ))
2008-08-06 16:50:06 +02:00
{
2008-08-17 23:13:21 +02:00
// Si un affichage est tronque, on montre des ...
2008-08-06 16:50:06 +02:00
$end = '...' ;
}
}
2010-10-02 01:37:36 +02:00
2010-09-30 10:12:03 +02:00
// If force rounding
if ( $forcerounding >= 0 ) $nbdecimal = $forcerounding ;
2008-08-06 16:50:06 +02:00
2008-09-08 09:37:53 +02:00
// Format number
2008-08-06 16:50:06 +02:00
if ( $html )
{
2009-10-21 18:50:15 +02:00
$output = preg_replace ( '/\s/' , ' ' , number_format ( $amount , $nbdecimal , $dec , $thousand ));
2008-08-06 16:50:06 +02:00
}
else
{
$output = number_format ( $amount , $nbdecimal , $dec , $thousand );
}
$output .= $end ;
return $output ;
}
/**
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 .
* Function to use on each input amount before any numeric test or database insert .
* @ param amount Amount to convert / clean
* @ param 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 )
2010-09-07 02:38:03 +02:00
* @ param 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
{
2008-08-17 23:13:21 +02:00
global $langs , $conf ;
2008-08-06 16:50:06 +02:00
2008-12-06 12:48:34 +01:00
// Round PHP function does not allow number like '1,234.56' nor '1.234,56' nor '1 234,56'
// Numbers must be '1234.56'
2009-01-25 17:45:04 +01:00
// Decimal delimiter for PHP and database SQL requests must be '.'
2008-08-17 23:13:21 +02:00
$dec = ',' ; $thousand = ' ' ;
if ( $langs -> trans ( " SeparatorDecimal " ) != " SeparatorDecimal " ) $dec = $langs -> trans ( " SeparatorDecimal " );
if ( $langs -> trans ( " SeparatorThousand " ) != " SeparatorThousand " ) $thousand = $langs -> trans ( " SeparatorThousand " );
2010-02-23 21:30:55 +01:00
if ( $thousand == 'None' ) $thousand = '' ;
//print "amount=".$amount." html=".$html." trunc=".$trunc." nbdecimal=".$nbdecimal." dec='".$dec."' thousand='".$thousand."'<br>";
2008-10-21 23:27:20 +02:00
2008-12-06 03:44:59 +01:00
// Convert value to universal number format (no thousand separator, '.' as decimal separator)
2008-08-28 01:00:37 +02:00
if ( $alreadysqlnb != 1 ) // If not a PHP number or unknown, we change format
{
2009-11-16 01:19:30 +01:00
//print 'PP'.$amount.' - '.$dec.' - '.$thousand.'<br>';
2009-01-15 00:36:51 +01:00
2008-12-06 03:44:59 +01:00
// 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.
2009-01-15 00:36:51 +01:00
if ( is_numeric ( $amount ))
2008-12-06 12:28:40 +01:00
{
2009-11-16 01:19:30 +01:00
// 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
2010-08-24 16:42:18 +02:00
$nbofdec = max ( 0 , dol_strlen ( $temps ) - 2 ); // -2 to remove "0."
2008-12-06 12:28:40 +01:00
$amount = number_format ( $amount , $nbofdec , $dec , $thousand );
}
2008-12-06 03:44:59 +01:00
//print "QQ".$amount.'<br>';
2009-01-15 00:36:51 +01:00
2008-12-06 03:44:59 +01:00
// Now make replace (the main goal of function)
2008-08-28 01:00:37 +02:00
if ( $thousand != ',' && $thousand != '.' ) $amount = str_replace ( ',' , '.' , $amount ); // To accept 2 notations for french users
2008-12-06 03:44:59 +01:00
$amount = str_replace ( ' ' , '' , $amount ); // To avoid spaces
2008-08-28 01:00:37 +02:00
$amount = str_replace ( $thousand , '' , $amount ); // Replace of thousand before replace of dec to avoid pb if thousand is .
$amount = str_replace ( $dec , '.' , $amount );
}
2008-12-06 03:44:59 +01:00
2009-01-25 17:45:04 +01:00
// Now, make a rounding if required
2008-08-06 16:50:06 +02:00
if ( $rounding )
{
2008-12-06 03:44:59 +01:00
$nbofdectoround = '' ;
if ( $rounding == 'MU' ) $nbofdectoround = $conf -> global -> MAIN_MAX_DECIMALS_UNIT ;
elseif ( $rounding == 'MT' ) $nbofdectoround = $conf -> global -> MAIN_MAX_DECIMALS_TOT ;
2009-01-15 00:36:51 +01:00
elseif ( $rounding == 'MS' ) $nbofdectoround = $conf -> global -> MAIN_MAX_DECIMALS_SHOWN ;
2008-12-06 12:48:34 +01:00
elseif ( $rounding == '2' ) $nbofdectoround = 2 ; // For admin info page
2009-11-16 01:19:30 +01:00
//print "RR".$amount.' - '.$nbofdectoround.'<br>';
2010-08-24 16:42:18 +02:00
if ( dol_strlen ( $nbofdectoround )) $amount = round ( $amount , $nbofdectoround ); // $nbofdectoround can be 0.
2008-12-06 03:44:59 +01:00
else return 'ErrorBadParameterProvidedToFunction' ;
2009-11-16 01:19:30 +01:00
//print 'SS'.$amount.' - '.$nbofdec.' - '.$dec.' - '.$thousand.' - '.$nbofdectoround.'<br>';
2009-01-15 00:36:51 +01:00
2008-12-06 03:44:59 +01:00
// 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.
2008-12-06 12:28:40 +01:00
if ( is_numeric ( $amount ))
{
2009-11-16 01:19:30 +01:00
// 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
2010-08-24 16:42:18 +02:00
$nbofdec = max ( 0 , dol_strlen ( $temps ) - 2 ); // -2 to remove "0."
2008-12-06 12:28:40 +01:00
$amount = number_format ( $amount , min ( $nbofdec , $nbofdectoround ), $dec , $thousand ); // Convert amount to format with dolibarr dec and thousand
}
2009-11-16 01:19:30 +01:00
//print "TT".$amount.'<br>';
2008-12-06 03:44:59 +01:00
2008-08-28 01:00:37 +02:00
// 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
2008-08-27 19:23:37 +02:00
if ( $thousand != ',' && $thousand != '.' ) $amount = str_replace ( ',' , '.' , $amount ); // To accept 2 notations for french users
2008-12-06 03:44:59 +01:00
$amount = str_replace ( ' ' , '' , $amount ); // To avoid spaces
2008-08-27 19:23:37 +02:00
$amount = str_replace ( $thousand , '' , $amount ); // Replace of thousand before replace of dec to avoid pb if thousand is .
2008-08-17 23:13:21 +02:00
$amount = str_replace ( $dec , '.' , $amount );
2008-08-06 16:50:06 +02:00
}
2009-01-15 00:36:51 +01:00
2008-08-06 16:50:06 +02:00
return $amount ;
}
2010-03-28 17:13:17 +02:00
/**
2010-09-07 02:38:03 +02:00
* Return localtaxe rate for a particular tva
* @ param tva Vat taxe
* @ param local Local taxe to search and return
* @ param societe_acheteuse Object of buying third party
* @ return int 0 if not found , localtax if found
2010-03-28 17:13:17 +02:00
*/
2010-06-17 19:12:34 +02:00
function get_localtax ( $tva , $local , $societe_acheteuse = " " )
2010-03-28 17:13:17 +02:00
{
global $db , $conf , $mysoc ;
2010-06-26 16:35:26 +02:00
2010-06-21 18:49:41 +02:00
$code_pays = $mysoc -> pays_code ;
2010-06-26 16:35:26 +02:00
2010-06-17 19:12:34 +02:00
if ( is_object ( $societe_acheteuse ))
{
2010-06-21 18:49:41 +02:00
if ( $code_pays != $societe_acheteuse -> pays_code ) return 0 ;
2010-06-17 19:12:34 +02:00
if ( $local == 1 && ! $societe_acheteuse -> localtax1_assuj ) return 0 ;
elseif ( $local == 2 && ! $societe_acheteuse -> localtax2_assuj ) return 0 ;
}
2010-06-26 16:35:26 +02:00
2010-03-28 17:13:17 +02:00
// Search local taxes
$sql = " SELECT t.localtax1, t.localtax2 " ;
$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 = ' " . $code_pays . " ' " ;
$sql .= " AND t.taux = " . $tva . " AND t.active = 1 " ;
$sql .= " ORDER BY t.localtax1 ASC, t.localtax2 ASC " ;
2010-06-26 16:35:26 +02:00
2010-03-28 17:13:17 +02:00
$resql = $db -> query ( $sql );
if ( $resql )
{
$obj = $db -> fetch_object ( $resql );
if ( $local == 1 ) return $obj -> localtax1 ;
elseif ( $local == 2 ) return $obj -> localtax2 ;
}
2010-06-26 16:35:26 +02:00
2010-06-18 22:48:16 +02:00
return 0 ;
2010-03-28 17:13:17 +02:00
}
2008-08-06 16:50:06 +02:00
/**
2010-09-07 02:38:03 +02:00
* Return vat rate of a product in a particular selling country or default country
* vat if product is unknown .
* @ param idprod Id of product or 0 if not a predefined product
* @ param countrycode Country code ( FR , US , IT , ... )
* @ return int < 0 if KO , Vat rate if OK
2010-03-29 01:36:52 +02:00
* TODO May be this should be better as a method of product class
2008-08-06 16:50:06 +02:00
*/
function get_product_vat_for_country ( $idprod , $countrycode )
{
2010-08-09 20:20:15 +02:00
global $db , $mysoc ;
$ret = 0 ;
2010-09-07 02:38:03 +02:00
$found = 0 ;
2008-08-06 16:50:06 +02:00
2010-09-07 02:38:03 +02:00
if ( $idprod > 0 )
{
// Load product
$product = new Product ( $db );
$result = $product -> fetch ( $idprod );
2008-08-06 16:50:06 +02:00
2010-09-07 02:38:03 +02:00
if ( $mysoc -> pays_code == $countrycode ) // If selling country is ours
{
$ret = $product -> tva_tx ; // Default vat of product we defined
$found = 1 ;
}
else
{
// TODO Read default product vat according to countrycode and product
2008-08-06 16:50:06 +02:00
2010-08-09 20:20:15 +02:00
2010-09-07 02:38:03 +02:00
}
}
if ( ! $found )
{
2010-08-09 20:20:15 +02:00
// 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=' " . $countrycode . " ' " ;
$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 );
}
2010-09-07 02:38:03 +02:00
dol_syslog ( " get_product_vat_for_country: ret= " . $ret );
2010-08-09 20:20:15 +02:00
return $ret ;
2008-08-06 16:50:06 +02:00
}
2010-03-27 17:18:10 +01:00
/**
2010-07-22 23:16:29 +02:00
* Return localtax rate of a product in a particular selling country
2010-09-07 02:38:03 +02:00
* @ param idprod Id of product
* @ package local 1 for localtax1 , 2 for localtax 2
* @ param countrycode Country code ( FR , US , IT , ... )
* @ return int < 0 if KO , Vat rate if OK
2010-03-29 01:36:52 +02:00
* TODO May be this should be better as a method of product class
2010-03-27 17:18:10 +01:00
*/
function get_product_localtax_for_country ( $idprod , $local , $countrycode )
{
global $db ;
$product = new Product ( $db );
$product -> fetch ( $idprod );
2010-04-18 18:44:25 +02:00
if ( $local == 1 ) return $product -> localtax1_tx ;
elseif ( $local == 2 ) return $product -> localtax2_tx ;
2010-03-29 01:36:52 +02:00
return - 1 ;
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 .
* @ param societe_vendeuse Objet societe vendeuse
* @ param societe_acheteuse Objet societe acheteuse
* @ param idprod Id product
* @ return float Taux de tva a appliquer , - 1 si ne peut etre determine
2008-08-06 16:50:06 +02:00
*/
2010-07-22 23:16:29 +02:00
function get_default_tva ( $societe_vendeuse , $societe_acheteuse , $idprod = 0 )
2008-08-06 16:50:06 +02:00
{
2010-08-09 20:20:15 +02:00
global $conf ;
2008-08-06 16:50:06 +02:00
if ( ! is_object ( $societe_vendeuse )) return - 1 ;
if ( ! is_object ( $societe_acheteuse )) return - 1 ;
2010-09-15 16:11:43 +02:00
dol_syslog ( " get_default_tva: seller use vat= " . $societe_vendeuse -> tva_assuj . " , seller country= " . $societe_vendeuse -> pays_code . " , seller in cee= " . $societe_vendeuse -> isInEEC () . " , buyer country= " . $societe_acheteuse -> pays_code . " , buyer in cee= " . $societe_acheteuse -> isInEEC () . " , idprod= " . $idprod . " , SERVICE_ARE_ECOMMERCE_200238EC= " . $conf -> global -> SERVICES_ARE_ECOMMERCE_200238EC );
2008-08-06 16:50:06 +02:00
// Si vendeur non assujeti a TVA (tva_assuj vaut 0/1 ou franchise/reel)
if ( is_numeric ( $societe_vendeuse -> tva_assuj ) && ! $societe_vendeuse -> tva_assuj ) return 0 ;
if ( ! is_numeric ( $societe_vendeuse -> tva_assuj ) && $societe_vendeuse -> tva_assuj == 'franchise' ) return 0 ;
2009-08-04 09:25:41 +02:00
// Si le (pays vendeur = pays acheteur) alors la TVA par defaut=TVA du produit vendu. Fin de regle.
2008-08-06 16:50:06 +02:00
//if (is_object($societe_acheteuse) && ($societe_vendeuse->pays_id == $societe_acheteuse->pays_id) && ($societe_acheteuse->tva_assuj == 1 || $societe_acheteuse->tva_assuj == 'reel'))
2010-06-29 19:28:01 +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.
2010-07-22 23:16:29 +02:00
if ( $societe_vendeuse -> pays_code == $societe_acheteuse -> pays_code ) // Warning ->pays_id not always defined
2008-08-06 16:50:06 +02:00
{
2010-09-07 02:38:03 +02:00
return get_product_vat_for_country ( $idprod , $societe_vendeuse -> pays_code );
2008-08-06 16:50:06 +02:00
}
2009-08-04 09:25:41 +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
2008-08-06 16:50:06 +02:00
2010-07-01 01:22:00 +02:00
// 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
if (( $societe_vendeuse -> isInEEC () && $societe_acheteuse -> isInEEC ()))
2010-06-29 16:35:47 +02:00
{
2010-08-09 20:20:15 +02:00
$isacompany = $societe_acheteuse -> isACompany ();
2010-07-01 01:22:00 +02:00
if ( $isacompany )
{
return 0 ;
}
else
{
2010-09-07 02:38:03 +02:00
return get_product_vat_for_country ( $idprod , $societe_vendeuse -> pays_code );
2010-07-01 01:22:00 +02:00
}
2010-06-29 16:35:47 +02:00
}
2010-07-01 01:22:00 +02:00
2010-08-09 20:20:15 +02:00
// If services are eServices according to EU Council Directive 2002/38/EC (ec.europa.eu/taxation_customs/taxation/v.../article_1610_en.htm)
// we use the buyer VAT.
2010-09-15 16:11:43 +02:00
if ( ! empty ( $conf -> global -> SERVICE_ARE_ECOMMERCE_200238EC ))
2010-08-09 20:20:15 +02:00
{
//print "eee".$societe_acheteuse->isACompany();exit;
if ( ! $societe_vendeuse -> isInEEC () && $societe_acheteuse -> isInEEC () && ! $societe_acheteuse -> isACompany ())
{
return get_product_vat_for_country ( $idprod , $societe_acheteuse -> pays_code );
}
}
2009-08-04 09:25:41 +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
2008-08-06 16:50:06 +02:00
return 0 ;
}
/**
2009-12-15 12:30:49 +01:00
* \brief Fonction qui renvoie si tva doit etre tva percue recuperable
* \remarks 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 .
* \param societe_vendeuse Objet societe vendeuse
* \param societe_acheteuse Objet societe acheteuse
2010-07-22 23:16:29 +02:00
* \param idprod Id product
2009-12-15 12:30:49 +01:00
* \return float 0 or 1
2008-08-06 16:50:06 +02:00
*/
2010-07-22 23:16:29 +02:00
function get_default_npr ( $societe_vendeuse , $societe_acheteuse , $idprod )
2008-08-06 16:50:06 +02:00
{
return 0 ;
}
2010-03-27 17:18:10 +01:00
/**
2010-03-29 01:36:52 +02:00
* \brief Function that return localtax of a product line ( according to seller , buyer and product vat rate )
2010-03-27 17:18:10 +01:00
* \param societe_vendeuse Objet societe vendeuse
* \param societe_acheteuse Objet societe acheteuse
2010-07-22 23:16:29 +02:00
* \param local Localtax to process ( 1 or 2 )
* \param idprod Id product
2010-03-27 17:18:10 +01:00
* \return float Taux de localtax appliquer , - 1 si ne peut etre determine
*/
function get_default_localtax ( $societe_vendeuse , $societe_acheteuse , $local , $idprod = 0 )
{
if ( ! is_object ( $societe_vendeuse )) return - 1 ;
if ( ! is_object ( $societe_acheteuse )) return - 1 ;
2010-03-29 01:26:14 +02:00
2010-03-29 01:36:52 +02:00
if ( $societe_vendeuse -> pays_id == 'ES' )
2010-03-27 17:18:10 +01:00
{
2010-04-18 14:18:09 +02:00
if ( $local == 1 ) //RE
2010-03-27 17:18:10 +01:00
{
// Si achatteur non assujeti a RE, localtax1 par default=0
if ( is_numeric ( $societe_acheteuse -> localtax1_assuj ) && ! $societe_acheteuse -> localtax1_assuj ) return 0 ;
if ( ! is_numeric ( $societe_acheteuse -> localtax1_assuj ) && $societe_acheteuse -> localtax1_assuj == 'localtax1off' ) return 0 ;
2010-03-29 01:26:14 +02:00
}
2010-04-18 14:18:09 +02:00
elseif ( $local == 2 ) //IRPF
2010-03-27 17:18:10 +01:00
{
// Si vendeur non assujeti a IRPF, localtax2 par default=0
if ( is_numeric ( $societe_vendeuse -> localtax2_assuj ) && ! $societe_vendeuse -> localtax2_assuj ) return 0 ;
if ( ! is_numeric ( $societe_vendeuse -> localtax2_assuj ) && $societe_vendeuse -> localtax2_assuj == 'localtax2off' ) return 0 ;
} else return - 1 ;
2010-03-29 01:26:14 +02:00
2010-03-27 17:18:10 +01:00
if ( $idprod ) return get_product_localtax_for_country ( $idprod , $local , $societe_vendeuse -> pays_code );
2010-04-18 18:44:25 +02:00
else return - 1 ;
2010-03-27 17:18:10 +01:00
}
return 0 ;
}
2008-08-06 16:50:06 +02:00
/**
2009-08-07 02:49:43 +02:00
* \brief Return yes or no in current language
* \param yesno Value to test ( 1 , 'yes' , 'true' or 0 , 'no' , 'false' )
2009-02-02 03:04:26 +01:00
* \param case 1 = Yes / No , 0 = yes / no
2009-06-16 23:11:25 +02:00
* \param color 0 = texte only , 1 = Text is formated with a color font style ( 'ok' or 'error' ), 2 = Text is formated with 'ok' color .
2008-08-06 16:50:06 +02:00
*/
function yn ( $yesno , $case = 1 , $color = 0 )
{
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 " ));
2010-04-09 09:55:37 +02:00
$classname = 'ok' ;
2008-08-06 16:50:06 +02:00
}
elseif ( $yesno == 0 || strtolower ( $yesno ) == 'no' || strtolower ( $yesno ) == 'false' )
{
$result = ( $case ? $langs -> trans ( " No " ) : $langs -> trans ( " no " ));
2010-04-09 09:55:37 +02:00
if ( $color == 2 ) $classname = 'ok' ;
else $classname = 'error' ;
2008-08-06 16:50:06 +02:00
}
2010-04-09 09:55:37 +02:00
if ( $color ) return '<font class="' . $classname . '">' . $result . '</font>' ;
2008-08-06 16:50:06 +02:00
return $result ;
}
/**
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/ "
* @ param $num Id to develop
* @ param $level Level of development ( 1 , 2 or 3 level )
* @ param $alpha Use alpha ref
* @ param withoutslash 0 = With slash at end , 1 = without slash at end
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
{
2009-04-13 11:36:43 +02:00
$path = '' ;
2009-10-21 18:50:15 +02:00
if ( empty ( $alpha )) $num = preg_replace ( '/([^0-9])/i' , '' , $num );
else $num = preg_replace ( '/^.*\-/i' , '' , $num );
2008-08-06 16:50:06 +02:00
$num = substr ( " 000 " . $num , - $level );
2010-02-13 23:20:32 +01:00
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 .= '/' ;
2009-04-13 11:36:43 +02:00
return $path ;
2008-08-06 16:50:06 +02:00
}
2010-11-06 18:07:06 +01:00
// For backward compatibility
function create_exdir ( $dir )
{
dol_mkdir ( $dir );
}
2008-08-06 16:50:06 +02:00
/**
2010-11-06 18:07:06 +01:00
* Creation of a directory ( recursive )
* @ param $dir Directory to create
* @ return int < 0 if KO , 0 = already exists , > 0 if OK
2008-08-06 16:50:06 +02:00
*/
2010-11-06 18:07:06 +01:00
function dol_mkdir ( $dir )
2008-08-06 16:50:06 +02:00
{
2010-11-06 18:07:06 +01:00
global $conf ;
2008-11-09 01:08:52 +01:00
2009-02-20 21:28:16 +01:00
dol_syslog ( " functions.lib::create_exdir: dir= " . $dir , LOG_INFO );
2008-08-06 16:50:06 +02:00
2009-12-15 12:30:49 +01:00
$dir_osencoded = dol_osencode ( $dir );
if ( @ is_dir ( $dir_osencoded )) return 0 ;
2008-08-06 16:50:06 +02:00
$nberr = 0 ;
$nbcreated = 0 ;
$ccdir = '' ;
$cdir = explode ( " / " , $dir );
for ( $i = 0 ; $i < sizeof ( $cdir ) ; $i ++ )
{
if ( $i > 0 ) $ccdir .= '/' . $cdir [ $i ];
else $ccdir = $cdir [ $i ];
2009-10-23 16:58:33 +02:00
if ( preg_match ( " /^.: $ / " , $ccdir , $regs )) continue ; // Si chemin Windows incomplet, on poursuit par rep suivant
2008-08-06 16:50:06 +02:00
2008-08-28 02:53:47 +02:00
// Attention, le is_dir() peut echouer bien que le rep existe.
2008-08-06 16:50:06 +02:00
// (ex selon config de open_basedir)
if ( $ccdir )
{
2009-12-15 12:30:49 +01:00
$ccdir_osencoded = dol_osencode ( $ccdir );
if ( ! @ is_dir ( $ccdir_osencoded ))
2008-08-06 16:50:06 +02:00
{
2009-02-20 21:28:16 +01:00
dol_syslog ( " functions.lib::create_exdir: Directory ' " . $ccdir . " ' does not exists or is outside open_basedir PHP setting. " , LOG_DEBUG );
2008-10-21 23:27:20 +02:00
umask ( 0 );
$dirmaskdec = octdec ( '0755' );
2008-11-09 01:08:52 +01:00
if ( ! empty ( $conf -> global -> MAIN_UMASK )) $dirmaskdec = octdec ( $conf -> global -> MAIN_UMASK );
2008-10-21 23:27:20 +02:00
$dirmaskdec |= octdec ( '0110' );
2009-12-15 12:30:49 +01:00
if ( ! @ mkdir ( $ccdir_osencoded , $dirmaskdec ))
2008-10-21 23:27:20 +02:00
{
// Si le is_dir a renvoye une fausse info, alors on passe ici.
2009-02-20 21:28:16 +01:00
dol_syslog ( " functions.lib::create_exdir: Fails to create directory ' " . $ccdir . " ' or directory already exists. " , LOG_WARNING );
2008-10-21 23:27:20 +02:00
$nberr ++ ;
}
else
{
2009-02-20 21:28:16 +01:00
dol_syslog ( " functions.lib::create_exdir: Directory ' " . $ccdir . " ' created " , LOG_DEBUG );
2009-04-06 10:34:42 +02:00
$nberr = 0 ; // On remet a zero car si on arrive ici, cela veut dire que les echecs precedents peuvent etre ignore
2008-10-21 23:27:20 +02:00
$nbcreated ++ ;
}
2008-08-06 16:50:06 +02:00
}
else
{
2009-04-06 10:34:42 +02:00
$nberr = 0 ; // On remet a zero car si on arrive ici, cela veut dire que les echecs precedents peuvent etre ignores
2008-08-06 16:50:06 +02:00
}
}
}
return ( $nberr ? - $nberr : $nbcreated );
}
/**
2008-11-16 02:09:04 +01:00
* \brief Retourne le picto champ obligatoire
* \return string Chaine avec picto obligatoire
2008-08-06 16:50:06 +02:00
*/
function picto_required ()
{
2010-02-04 20:54:58 +01: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
* @ param StringHtml String to clean
* @ param removelinefeed Replace also all lines feeds by a space
* @ return string String cleaned
2008-08-06 16:50:06 +02:00
*/
2008-11-06 02:31:57 +01:00
function dol_string_nohtmltag ( $StringHtml , $removelinefeed = 1 )
2008-08-06 16:50:06 +02:00
{
2009-10-21 20:21:56 +02:00
$pattern = " /<[^>]+>/ " ;
2008-08-06 16:50:06 +02:00
$temp = dol_entity_decode ( $StringHtml );
2009-10-21 20:21:56 +02:00
$temp = preg_replace ( $pattern , " " , $temp );
2008-08-06 16:50:06 +02:00
// Supprime aussi les retours
if ( $removelinefeed ) $temp = str_replace ( " \n " , " " , $temp );
// et les espaces doubles
2008-08-25 09:16:16 +02:00
while ( strpos ( $temp , " " ))
2008-08-06 16:50:06 +02:00
{
2008-08-25 09:16:16 +02:00
$temp = str_replace ( " " , " " , $temp );
2008-08-06 16:50:06 +02:00
}
2009-03-09 00:09:45 +01:00
$CleanString = trim ( $temp );
2008-08-06 16:50:06 +02:00
return $CleanString ;
}
/**
2010-08-29 14:54:39 +02:00
* Replace CRLF in string with a HTML BR tag .
* @ param string2encode String to encode
* @ param nl2brmode 0 = Adding br before \n , 1 = Replacing \n by br
* @ param 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
{
2010-09-04 15:11:57 +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 );
}
2008-08-06 16:50:06 +02:00
else
{
2009-10-21 20:21:56 +02:00
$ret = str_replace ( " \r " , " " , $stringtoencode );
2010-08-21 20:53:19 +02:00
$ret = str_replace ( " \n " ,( $forxml ? '<br />' : '<br>' ), $ret );
2008-08-06 16:50:06 +02:00
return $ret ;
}
}
/**
2009-10-13 19:23:26 +02:00
* \brief 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 .
2009-01-29 21:56:14 +01:00
* This function also remove last CR / BR .
2008-08-06 16:50:06 +02:00
* \param stringtoencode String to encode
* \param nl2brmode 0 = Adding br before \n , 1 = Replacing \n by br ( for use with FPDF writeHTMLCell function for example )
* \remarks 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
* is used to build PDF , nl2brmode must be 1.
*/
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
{
2008-10-09 19:29:32 +02:00
if ( dol_textishtml ( $stringtoencode ))
2008-08-06 16:50:06 +02:00
{
2010-08-29 14:54:39 +02:00
$newstring = preg_replace ( '/<br(\s[\sa-zA-Z_="]*)?\/?>/i' , '<br>' , $stringtoencode ); // Replace "<br type="_moz" />" by "<br>". It's same and avoid pb with FPDF.
2009-10-21 18:50:15 +02:00
$newstring = preg_replace ( '/<br>$/i' , '' , $newstring ); // Replace "<br type="_moz" />" by "<br>". It's same and avoid pb with FPDF.
2009-01-31 03:55:32 +01:00
$newstring = strtr ( $newstring , array ( '&' => '__and__' , '<' => '__lt__' , '>' => '__gt__' , '"' => '__dquot__' ));
2010-08-10 01:57:45 +02:00
$newstring = dol_htmlentities ( $newstring , ENT_COMPAT , $pagecodefrom ); // Make entity encoding
2009-01-31 03:55:32 +01:00
$newstring = strtr ( $newstring , array ( '__and__' => '&' , '__lt__' => '<' , '__gt__' => '>' , '__dquot__' => '"' ));
2009-01-29 02:57:56 +01:00
// If already HTML, CR should be <br> so we don't change \n
2008-08-06 16:50:06 +02:00
}
else {
2010-08-10 01:57:45 +02:00
$newstring = dol_nl2br ( dol_htmlentities ( $stringtoencode , ENT_COMPAT , $pagecodefrom ), $nl2brmode );
2008-08-06 16:50:06 +02:00
}
2009-01-29 02:57:56 +01:00
// Other substitutions that htmlentities does not do
2009-01-28 16:09:37 +01:00
$newstring = str_replace ( chr ( 128 ), '€' , $newstring ); // 128 = 0x80. Not in html entity table.
return $newstring ;
2008-08-06 16:50:06 +02:00
}
2008-10-24 03:01:56 +02:00
/**
* \brief This function is called to decode a HTML string ( it decodes entities and br tags )
2008-08-06 16:50:06 +02:00
* \param stringtodecode String to decode
*/
2008-10-24 03:01:56 +02:00
function dol_htmlentitiesbr_decode ( $stringtodecode , $pagecodeto = 'UTF-8' )
2008-08-06 16:50:06 +02:00
{
2010-08-10 01:57:45 +02:00
$ret = dol_html_entity_decode ( $stringtodecode , ENT_COMPAT , $pagecodeto );
2009-10-21 18:50:15 +02:00
$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 );
2009-10-21 19:42:31 +02:00
$ret = preg_replace ( '/<br(\s[\sa-zA-Z_="]*)?\/?>' . " \n " . '/i' , " \n " , $ret );
2009-10-21 18:50:15 +02:00
$ret = preg_replace ( '/<br(\s[\sa-zA-Z_="]*)?\/?>/i' , " \n " , $ret );
2008-08-06 16:50:06 +02:00
return $ret ;
}
2008-10-25 19:27:15 +02:00
/**
* \brief This function remove all ending \n and br at end
* \param stringtodecode String to decode
*/
function dol_htmlcleanlastbr ( $stringtodecode )
{
2009-10-21 18:50:15 +02:00
$ret = preg_replace ( '/(<br>|<br(\s[\sa-zA-Z_="]*)?\/?>|' . " \n " . '|' . " \r " . ')+$/i' , " " , $stringtodecode );
2008-10-25 19:27:15 +02:00
return $ret ;
}
2008-08-06 16:50:06 +02:00
/**
2008-11-06 02:31:57 +01:00
* \brief This function is called to decode a string with HTML entities ( it decodes entities tags )
2008-10-24 03:01:56 +02:00
* \param string stringhtml
* \return string decodestring
2008-08-06 16:50:06 +02:00
*/
2008-10-24 03:01:56 +02:00
function dol_entity_decode ( $stringhtml , $pagecodeto = 'UTF-8' )
2008-08-06 16:50:06 +02:00
{
2010-08-10 01:57:45 +02:00
$ret = dol_html_entity_decode ( $stringhtml , ENT_COMPAT , $pagecodeto );
2008-10-24 03:01:56 +02:00
return $ret ;
2008-08-06 16:50:06 +02:00
}
2010-08-10 01:57:45 +02:00
/**
* Replace html_entity_decode functions to manage errors
* @ param unknown_type $a
* @ param unknown_type $b
* @ param unknown_type $c
*/
function dol_html_entity_decode ( $a , $b , $c )
{
// We use @ to avoid warning on PHP4 that does not support entity decoding to UTF8;
$ret =@ html_entity_decode ( $a , $b , $c );
return $ret ;
}
/**
* Replace htmlentities functions to manage errors
* @ param unknown_type $a
* @ param unknown_type $b
* @ param unknown_type $c
*/
function dol_htmlentities ( $a , $b , $c )
{
// We use @ to avoid warning on PHP4 that does not support entity decoding to UTF8;
$ret =@ htmlentities ( $a , $b , $c );
return $ret ;
}
2008-08-06 16:50:06 +02:00
/**
2009-08-11 22:16:58 +02:00
* \brief Check if a string is a correct iso string
* If not , it will we considered not HTML encoded even if it is by FPDF .
* \remarks Example , if string contains euro symbol that has ascii code 128.
* \param 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 )
{
2010-08-24 16:42:18 +02:00
$len = dol_strlen ( $s );
2008-08-06 16:50:06 +02:00
$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 ;
}
/**
2009-01-12 18:23:12 +01:00
* \brief Return nb of lines of a clear text
2008-08-06 16:50:06 +02:00
* \param s String to check
* \param maxchar Not yet used
2009-01-12 18:23:12 +01:00
* \return int Number of lines
2008-08-06 16:50:06 +02:00
*/
function dol_nboflines ( $s , $maxchar = 0 )
{
2009-01-12 18:23:12 +01:00
if ( $s == '' ) return 0 ;
2009-10-20 15:14:44 +02:00
$arraystring = explode ( " \n " , $s );
2008-08-06 16:50:06 +02:00
$nb = sizeof ( $arraystring );
2008-10-09 19:29:32 +02:00
2008-08-06 16:50:06 +02:00
return $nb ;
}
/**
2009-08-02 20:05:16 +02:00
* \brief Return nb of lines of a formated text with \n and < br >
* \param texte Text
* \param maxlinesize Largeur de ligne en caracteres ( ou 0 si pas de limite - defaut )
* \param charset Give the charset used to encode the $texte variable in memory .
* \return int Number of lines
2008-08-06 16:50:06 +02:00
*/
2009-08-02 20:05:16 +02:00
function dol_nboflines_bis ( $texte , $maxlinesize = 0 , $charset = 'UTF-8' )
2008-08-06 16:50:06 +02:00
{
2009-08-07 02:49:43 +02:00
//print $texte;
2008-08-06 16:50:06 +02:00
$repTable = array ( " \t " => " " , " \n " => " <br> " , " \r " => " " , " \0 " => " " , " \x0B " => " " );
$texte = strtr ( $texte , $repTable );
2009-08-02 20:05:16 +02:00
if ( $charset == 'UTF-8' ) { $pattern = '/(<[^>]+>)/Uu' ; } // /U is to have UNGREEDY regex to limit to one html tag. /u is for UTF8 support
else $pattern = '/(<[^>]+>)/U' ; // /U is to have UNGREEDY regex to limit to one html tag.
2008-08-06 16:50:06 +02:00
$a = preg_split ( $pattern , $texte , - 1 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
2009-01-29 21:56:14 +01:00
$nblines = floor (( count ( $a ) + 1 ) / 2 );
2008-10-09 19:29:32 +02:00
// count possible auto line breaks
if ( $maxlinesize )
{
foreach ( $a as $line )
{
2010-08-24 16:42:18 +02:00
if ( dol_strlen ( $line ) > $maxlinesize )
2008-10-09 19:29:32 +02:00
{
//$line_dec = html_entity_decode(strip_tags($line));
$line_dec = html_entity_decode ( $line );
2010-08-24 16:42:18 +02:00
if ( dol_strlen ( $line_dec ) > $maxlinesize )
2008-10-09 19:29:32 +02:00
{
$line_dec = wordwrap ( $line_dec , $maxlinesize , '\n' , true );
$nblines += substr_count ( $line_dec , '\n' );
}
}
}
}
2008-08-06 16:50:06 +02:00
return $nblines ;
}
/**
* \brief Fonction simple identique a microtime de PHP 5 mais compatible PHP 4
* \return float Time en millisecondes avec decimal pour microsecondes
*/
function dol_microtime_float ()
{
list ( $usec , $sec ) = explode ( " " , microtime ());
return (( float ) $usec + ( float ) $sec );
}
/*
* \brief Return if a text is a html content
* \param msg Content to check
* \param option 0 = Full detection , 1 = Fast check
* \return boolean true / false
*/
function dol_textishtml ( $msg , $option = 0 )
{
if ( $option == 1 )
{
2009-10-23 16:58:33 +02:00
if ( preg_match ( '/<html/i' , $msg )) return true ;
elseif ( preg_match ( '/<body/i' , $msg )) return true ;
elseif ( preg_match ( '/<br/i' , $msg )) return true ;
2008-08-06 16:50:06 +02:00
return false ;
}
else
{
2009-10-23 16:58:33 +02:00
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 ( '/<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 ;
2008-08-06 16:50:06 +02:00
return false ;
}
}
2008-11-16 02:09:04 +01:00
/**
2010-09-01 12:22:11 +02: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
2010-08-29 14:54:39 +02:00
* @ param chaine Source string in which we must do substitution
* @ param substitutionarray Array substitution old value => new value value
* @ param outputlangs If we want substitution from special constants , we provide a language
* @ param object If we want substitution from special constants , we provide data in a source object
* @ return string Output string after subsitutions
2008-08-06 16:50:06 +02:00
*/
2009-11-01 16:26:56 +01:00
function make_substitutions ( $chaine , $substitutionarray , $outputlangs , $object = '' )
2008-08-06 16:50:06 +02:00
{
2009-08-25 22:57:54 +02:00
global $conf , $user ;
2010-08-29 14:54:39 +02:00
$dir = $dirroot . " /includes/modules/substitutions " ;
$listfonc = array ( 'numberwords' ); // For the moment only one substitution module to search
foreach ( $listfonc as $fonc )
{
// Check if there is external substitution to do asked by plugins
// We look files into the includes/modules/substitutions directory
// By default, there is no such external plugins.
foreach ( $conf -> file -> dol_document_root as $dirroot )
{
// If module enabled and complete
2010-11-17 22:29:22 +01:00
if ( ! empty ( $conf -> $fonc -> enabled ) && file_exists ( $dirroot . '/includes/modules/substitutions/functions_' . $fonc . '.lib.php' ))
2010-08-29 14:54:39 +02:00
{
2010-11-17 22:29:22 +01:00
dol_syslog ( " Library functions_ " . $fonc . " .lib.php found into " . $dirroot );
require_once ( $dirroot . " /includes/modules/substitutions/functions_ " . $fonc . " .lib.php " );
2010-08-29 14:54:39 +02:00
numberwords_completesubstitutionarray ( $substitutionarray , $outputlangs , $object );
break ;
}
2009-08-25 22:57:54 +02:00
}
}
// Make substitition
2008-08-06 16:50:06 +02:00
foreach ( $substitutionarray as $key => $value )
{
2009-10-21 20:21:56 +02:00
$chaine = str_replace ( " $key " , " $value " , $chaine ); // We must keep the " to work when value is 123.5 for example
2008-08-06 16:50:06 +02:00
}
return $chaine ;
}
2008-10-17 02:24:44 +02:00
/**
2010-08-29 14:54:39 +02:00
* Format output for start and end date
* @ param date_start Start date
* @ param date_end End date
* @ param format Output format
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
{
print get_date_range ( $date_start , $date_end , $format , $outputlangs );
}
/**
* Format output for start and end date
* @ param date_start Start date
* @ param date_end End date
* @ param format Output format
*/
function get_date_range ( $date_start , $date_end , $format = '' , $outputlangs = '' )
2008-08-06 16:50:06 +02:00
{
global $langs ;
2010-12-18 03:33:13 +01:00
2010-12-17 11:08:31 +01:00
$out = '' ;
2008-08-06 16:50:06 +02:00
2008-10-29 00:36:36 +01:00
if ( ! is_object ( $outputlangs )) $outputlangs = $langs ;
2008-11-09 01:08:52 +01:00
2008-08-06 16:50:06 +02:00
if ( $date_start && $date_end )
{
2010-12-17 11:08:31 +01:00
$out .= ' (' . $outputlangs -> trans ( 'DateFromTo' , dol_print_date ( $date_start , $format , false , $outputlangs ), dol_print_date ( $date_end , $format , false , $outputlangs )) . ')' ;
2008-08-06 16:50:06 +02:00
}
if ( $date_start && ! $date_end )
{
2010-12-17 11:08:31 +01:00
$out .= ' (' . $outputlangs -> trans ( 'DateFrom' , dol_print_date ( $date_start , $format , false , $outputlangs )) . ')' ;
2008-08-06 16:50:06 +02:00
}
if ( ! $date_start && $date_end )
{
2010-12-17 11:08:31 +01:00
$out .= ' (' . $outputlangs -> trans ( 'DateUntil' , dol_print_date ( $date_end , $format , false , $outputlangs )) . ')' ;
2008-08-06 16:50:06 +02:00
}
2010-12-18 03:33:13 +01:00
2010-12-17 11:08:31 +01:00
return $out ;
2008-08-06 16:50:06 +02:00
}
/**
2008-11-16 02:09:04 +01:00
* \brief Retourne un tableau des mois ou le mois selectionne
* \param selected Mois a selectionner ou - 1
* \return string or array Month string or array if selected < 0
2008-08-06 16:50:06 +02:00
*/
function monthArrayOrSelected ( $selected = 0 )
{
global $langs ;
$langs -> load ( " main " );
$month = array ( 1 => $langs -> trans ( " January " ),
2 => $langs -> trans ( " February " ),
3 => $langs -> trans ( " March " ),
4 => $langs -> trans ( " April " ),
5 => $langs -> trans ( " May " ),
6 => $langs -> trans ( " June " ),
7 => $langs -> trans ( " July " ),
8 => $langs -> trans ( " August " ),
9 => $langs -> trans ( " September " ),
10 => $langs -> trans ( " October " ),
11 => $langs -> trans ( " November " ),
12 => $langs -> trans ( " December " )
);
if ( $selected >= 0 )
{
$return = '' ;
foreach ( $month as $key => $val )
{
if ( $selected == $key )
{
$return = $val ;
}
}
return $return ;
}
else
{
return $month ;
}
}
/**
2010-09-02 14:18:02 +02:00
* Returns formated errors messages
* @ param mesgstring Error message
* @ param mesgarray Error messages array
* @ return html Return html output
2010-12-21 01:44:57 +01:00
* @ see dol_print_error
2008-08-06 16:50:06 +02:00
*/
function dol_htmloutput_errors ( $mesgstring = '' , $mesgarray = '' )
{
global $langs ;
2011-02-26 02:57:00 +01:00
$ret = '' ;
2008-08-06 16:50:06 +02:00
$langs -> load ( " errors " );
if ( is_array ( $mesgarray ) && sizeof ( $mesgarray ))
{
print '<div class="error">' ;
foreach ( $mesgarray as $message )
{
$ret ++ ;
print $langs -> trans ( $message ) . " <br> \n " ;
}
print '</div>' ;
}
if ( $mesgstring )
{
$ret ++ ;
print '<div class="error">' ;
2010-12-21 01:44:57 +01:00
print $langs -> trans ( $mesgstring );
2008-08-06 16:50:06 +02:00
print '</div>' ;
}
return $ret ;
}
/**
* \brief This function output memory used by PHP and exit everything . Used for debugging purpose .
*/
function stopwithmem ()
{
print memory_get_usage ();
llxFooter ();
exit ;
}
/**
* \brief 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 ) .
*/
2009-01-30 22:21:22 +01:00
function dol_sort_array ( & $array , $index , $order = 'asc' , $natsort , $case_sensitive )
2008-08-06 16:50:06 +02:00
{
// Clean parameters
$order = strtolower ( $order );
if ( is_array ( $array ) && count ( $array ) > 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-10-09 19:29:32 +02:00
/**
2010-09-29 10:05:22 +02:00
* Check if a string is in UTF8
* @ param $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
{
2010-09-29 10:05:22 +02:00
// We must use here a binary strlen function (so not dol_strlen)
for ( $i = 0 ; $i < strlen ( $str ); $i ++ )
2008-10-09 19:29:32 +02:00
{
2010-09-29 10:05:22 +02:00
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
2008-10-09 19:29:32 +02:00
else return false ; # Does not match any model
for ( $j = 0 ; $j < $n ; $j ++ ) { # n bytes matching 10bbbbbb follow ?
2010-09-29 10:05:22 +02:00
if (( ++ $i == strlen ( $str )) || (( ord ( $str [ $i ]) & 0xC0 ) != 0x80 ))
2008-10-09 19:29:32 +02:00
return false ;
}
}
return true ;
}
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
2009-12-14 23:17:50 +01:00
* value to pass to filesystem PHP functions .
2010-10-02 12:48:28 +02:00
* @ param $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 )
{
$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 ;
if ( $tmp == 'iso-8859-1' ) return utf8_decode ( $str );
return $str ;
}
2009-10-20 17:23:32 +02:00
/**
2010-11-02 12:22:41 +01:00
* Return an id from a Code . Store Code - Id in a cache .
* @ param db Database handler
* @ param key Code to get Id
* @ param tablename Table name without prefix
* @ param fieldkey Field for code
* @ param 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' )
{
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
{
2009-10-22 02:27:13 +02:00
dol_syslog ( " dol_getIdFromCode error= " . $db -> lasterror (), LOG_ERR );
2009-10-20 17:23:32 +02:00
return - 1 ;
}
}
2010-03-25 12:16:42 +01:00
/**
* Verify if condition in string is ok or not
*
* @ param string $strRights
* @ return boolean true or false
*/
function verifCond ( $strRights )
{
global $user , $conf , $langs , $leftmenu ;
global $rights ; // To export to dol_eval function
//print $strRights."<br>\n";
if ( $strRights != " " )
{
$rights = true ;
$tab_rights = explode ( " || " , $strRights );
$i = 0 ;
while (( $i < count ( $tab_rights )) && ( $rights == true )) {
$str = 'if(!(' . $strRights . ')) { $rights = false; }' ;
dol_eval ( $str );
$i ++ ;
}
}
else
{
$rights = true ;
}
return $rights ;
}
/**
* Replace eval function to add more security
*
* @ param string $s
* @ return int 1
*/
function dol_eval ( $s )
{
// Only global variables can be changed by eval function and returned to caller
global $langs , $user , $conf ;
global $rights ;
global $leftmenu ;
// \todo
// Warning. We must add code to exclude test if it contains = (affectation) that is not == (compare)
//print $s."<br>\n";
eval ( $s );
return 1 ;
}
2010-08-29 19:43:51 +02:00
2010-09-21 21:10:07 +02:00
if ( ! function_exists ( 'glob' ) && ! is_callable ( 'glob' ))
2010-05-10 20:26:34 +02:00
{
2010-08-29 19:43:51 +02:00
/**
* \brief For replace glob () function
*/
function glob ( $pattern )
2010-05-10 20:26:34 +02:00
{
#get pathname (everything up until the last / or \)
$path = $output = null ;
if ( PHP_OS == 'WIN32' ) $slash = '\\' ;
else $slash = '/' ;
$lastpos = strrpos ( $pattern , $slash );
2010-05-26 16:52:32 +02:00
2010-05-10 20:26:34 +02:00
if ( ! ( $lastpos === false ))
{
$path = substr ( $pattern , 0 , - $lastpos - 1 );
$pattern = substr ( $pattern , $lastpos );
}
else
{
#no dir info, use current dir
$path = getcwd ();
}
$handle =@ opendir ( $path );
if ( $handle === false ) return false ;
2010-05-26 16:52:32 +02:00
2010-05-10 20:26:34 +02:00
while ( $dir = readdir ( $handle ))
{
if ( pattern_match ( $pattern , $dir )) $output [] = $dir ;
}
2010-05-26 16:52:32 +02:00
2010-05-10 20:26:34 +02:00
closedir ( $handle );
2010-05-26 16:52:32 +02:00
2010-05-10 20:26:34 +02:00
if ( is_array ( $output )) return $output ;
return false ;
}
}
/**
* \brief For dol_glob () function
*/
function pattern_match ( $pattern , $string )
{
#basically prepare a regular expression
$out = null ;
$chunks = explode ( ';' , $pattern );
foreach ( $chunks as $pattern )
{
$escape = array ( '$' , '^' , '.' , '{' , '}' , '(' , ')' , '[' , ']' , '|' );
while ( strpos ( $pattern , '**' ) !== false ) $pattern = str_replace ( '**' , '*' , $pattern );
2010-05-26 16:52:32 +02:00
2010-05-10 20:26:34 +02:00
foreach ( $escape as $probe ) $pattern = str_replace ( $probe , " \\ $probe " , $pattern );
2010-05-26 16:52:32 +02:00
2010-05-10 20:26:34 +02:00
$pattern = str_replace ( '?*' , '*' , str_replace ( '*?' , '*' , str_replace ( '*' , " .* " , str_replace ( '?' , '.{1,1}' , $pattern ))));
$out [] = $pattern ;
}
2010-05-26 16:52:32 +02:00
2010-05-10 20:26:34 +02:00
if ( count ( $out ) == 1 )
{
return ( preg_match ( '/^' . $out [ 0 ] . '$/i' , $string ));
}
else
{
foreach ( $out as $tester )
{
if ( preg_match ( '/^' . $tester . '$/i' , $string )) return true ;
return false ;
}
}
}
2010-03-25 12:16:42 +01: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
* @ param 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 )
{
$ret = '' ;
if ( ! empty ( $codelang ))
{
if ( $codelang == 'auto' ) $ret = img_picto ( '' , DOL_URL_ROOT . '/theme/common/flags/int.png' , '' , 1 );
else {
//print $codelang;
2010-08-28 14:06:33 +02:00
$langtocountryflag = array ( 'ar_AR' => '' , 'ca_ES' => 'catalonia' , 'da_DA' => 'dk' , 'fr_CA' => 'mq' , 'sv_SV' => 'se' );
2010-06-26 16:35:26 +02:00
$tmpcode = '' ;
if ( isset ( $langtocountryflag [ $codelang ])) $tmpcode = $langtocountryflag [ $codelang ];
else
{
$tmparray = explode ( '_' , $codelang );
2010-07-10 01:49:41 +02:00
$tmpcode = empty ( $tmparray [ 1 ]) ? $tmparray [ 0 ] : $tmparray [ 1 ];
2010-06-26 16:35:26 +02:00
}
if ( $tmpcode ) $ret .= img_picto ( $codelang , DOL_URL_ROOT . '/theme/common/flags/' . strtolower ( $tmpcode ) . '.png' , '' , 1 );
}
}
return $ret ;
}
2008-08-06 16:50:06 +02:00
?>