2008-06-20 23:10:24 +02:00
< ? php
/* Copyright ( C ) 2008 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2008 Raphael Bertrand ( Resultic ) < raphael . bertrand @ resultic . fr >
2009-01-15 01:32:10 +01:00
*
2008-06-20 23:10:24 +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 /
*/
/**
2008-09-25 17:54:08 +02:00
* \file htdocs / lib / functions2 . lib . php
2008-11-16 02:09:04 +01:00
* \brief A set of functions for Dolibarr
* This file contains all rare functions .
2008-09-25 17:54:08 +02:00
* \version $Id $
2008-06-20 23:10:24 +02:00
*/
2009-02-20 21:28:16 +01:00
/**
* \brief Renvoi le fichier $filename dans la version de la langue courante , sinon alternative
* \param filename nom du fichier a rechercher
* \param searchalt cherche aussi dans langue alternative
* \return boolean
*/
function dol_print_file ( $langs , $filename , $searchalt = 0 )
{
global $conf ;
// Test if file is in lang directory
foreach ( $langs -> dir as $searchdir )
{
$htmlfile = ( $searchdir . " /langs/ " . $langs -> defaultlang . " / " . $filename );
dol_syslog ( 'Translate::print_file search file ' . $htmlfile , LOG_DEBUG );
if ( is_readable ( $htmlfile ))
{
$content = file_get_contents ( $htmlfile );
$isutf8 = utf8_check ( $content );
2009-05-08 03:23:33 +02:00
if ( ! $isutf8 && $conf -> file -> character_set_client == 'UTF-8' ) print utf8_encode ( $content );
elseif ( $isutf8 && $conf -> file -> character_set_client == 'ISO-8859-1' ) print utf8_decode ( $content );
2009-02-20 21:28:16 +01:00
else print $content ;
return true ;
}
else dol_syslog ( 'Translate::print_file not found' , LOG_DEBUG );
if ( $searchalt ) {
// Test si fichier dans repertoire de la langue alternative
if ( $langs -> defaultlang != " en_US " ) $htmlfilealt = $searchdir . " /langs/en_US/ " . $filename ;
else $htmlfilealt = $searchdir . " /langs/fr_FR/ " . $filename ;
dol_syslog ( 'Translate::print_file search alt file ' . $htmlfilealt , LOG_DEBUG );
//print 'getcwd='.getcwd().' htmlfilealt='.$htmlfilealt.' X '.file_exists(getcwd().'/'.$htmlfilealt);
if ( is_readable ( $htmlfilealt ))
{
$content = file_get_contents ( $htmlfilealt );
$isutf8 = utf8_check ( $content );
2009-05-08 03:23:33 +02:00
if ( ! $isutf8 && $conf -> file -> character_set_client == 'UTF-8' ) print utf8_encode ( $content );
elseif ( $isutf8 && $conf -> file -> character_set_client == 'ISO-8859-1' ) print utf8_decode ( $content );
2009-02-20 21:28:16 +01:00
else print $content ;
return true ;
}
else dol_syslog ( 'Translate::print_file not found' , LOG_DEBUG );
}
}
return false ;
}
2009-02-19 20:06:12 +01:00
/**
* \brief Show informations on an object
* \param object Objet to show
*/
function dol_print_object_info ( $object )
{
2009-07-29 20:39:31 +02:00
global $langs , $db ;
2009-02-19 20:06:12 +01:00
$langs -> load ( " other " );
2009-07-29 20:39:31 +02:00
// Import key
2009-05-05 03:09:31 +02:00
if ( isset ( $object -> import_key ))
print $langs -> trans ( " ImportedWithSet " ) . " : " . $object -> import_key . '<br>' ;
2009-02-19 20:06:12 +01:00
2009-07-29 20:39:31 +02:00
// User creation
if ( isset ( $object -> user_creation ))
{
print $langs -> trans ( " CreatedBy " ) . " : " ;
if ( is_object ( $object -> user_creation ))
{
print $object -> user_creation -> getNomUrl ( 1 );
}
else
{
$userstatic = new User ( $db );
$userstatic -> id = $object -> user_creation ;
$userstatic -> fetch ();
print $userstatic -> getNomUrl ( 1 );
}
print '<br>' ;
}
// Date
2009-02-19 20:06:12 +01:00
if ( isset ( $object -> date_creation ))
2009-02-20 23:53:15 +01:00
print $langs -> trans ( " DateCreation " ) . " : " . dol_print_date ( $object -> date_creation , " dayhourtext " ) . '<br>' ;
2009-02-19 20:06:12 +01:00
2009-07-29 20:39:31 +02:00
// User change
if ( isset ( $object -> user_modification ))
{
print $langs -> trans ( " ModifiedBy " ) . " : " ;
if ( is_object ( $object -> user_modification ))
{
print $object -> user_modification -> getNomUrl ( 1 );
}
else
{
$userstatic = new User ( $db );
$userstatic -> id = $object -> user_modification ;
$userstatic -> fetch ();
print $userstatic -> getNomUrl ( 1 );
}
print '<br>' ;
}
2009-02-19 20:06:12 +01:00
2009-07-29 20:39:31 +02:00
// Date
2009-02-19 20:06:12 +01:00
if ( isset ( $object -> date_modification ))
2009-02-20 23:53:15 +01:00
print $langs -> trans ( " DateLastModification " ) . " : " . dol_print_date ( $object -> date_modification , " dayhourtext " ) . '<br>' ;
2009-02-19 20:06:12 +01:00
2009-07-29 20:39:31 +02:00
// User validation
if ( isset ( $object -> user_validation ))
{
print $langs -> trans ( " ValidatedBy " ) . " : " ;
if ( is_object ( $object -> user_validation ))
{
print $object -> user_validation -> getNomUrl ( 1 );
}
else
{
$userstatic = new User ( $db );
$userstatic -> id = $object -> user_validation ;
$userstatic -> fetch ();
print $userstatic -> getNomUrl ( 1 );
}
print '<br>' ;
}
2009-02-19 20:06:12 +01:00
2009-07-29 20:39:31 +02:00
// Date
2009-02-19 20:06:12 +01:00
if ( isset ( $object -> date_validation ))
2009-02-20 23:53:15 +01:00
print $langs -> trans ( " DateValidation " ) . " : " . dol_print_date ( $object -> date_validation , " dayhourtext " ) . '<br>' ;
2009-02-19 20:06:12 +01:00
2009-07-29 20:39:31 +02:00
// User close
if ( isset ( $object -> user_cloture ))
{
print $langs -> trans ( " ClosedBy " ) . " : " ;
if ( is_object ( $object -> user_cloture ))
{
print $object -> user_cloture -> getNomUrl ( 1 );
}
else
{
$userstatic = new User ( $db );
$userstatic -> id = $object -> user_cloture ;
$userstatic -> fetch ();
print $userstatic -> getNomUrl ( 1 );
}
print '<br>' ;
}
2009-02-19 20:06:12 +01:00
2009-07-29 20:39:31 +02:00
// Date
2009-02-19 20:06:12 +01:00
if ( isset ( $object -> date_cloture ))
2009-02-20 23:53:15 +01:00
print $langs -> trans ( " DateClosing " ) . " : " . dol_print_date ( $object -> date_cloture , " dayhourtext " ) . '<br>' ;
2009-02-19 20:06:12 +01:00
2009-07-29 20:39:31 +02:00
// User conciliate
if ( isset ( $object -> user_rappro ))
{
print $langs -> trans ( " ConciliatedBy " ) . " : " ;
if ( is_object ( $object -> user_rappro ))
{
print $object -> user_rappro -> getNomUrl ( 1 );
}
else
{
$userstatic = new User ( $db );
$userstatic -> id = $object -> user_rappro ;
$userstatic -> fetch ();
print $userstatic -> getNomUrl ( 1 );
}
print '<br>' ;
}
2009-02-19 20:06:12 +01:00
2009-07-29 20:39:31 +02:00
// Date
2009-02-19 20:06:12 +01:00
if ( isset ( $object -> date_rappro ))
2009-02-20 23:53:15 +01:00
print $langs -> trans ( " DateConciliating " ) . " : " . dol_print_date ( $object -> date_rappro , " dayhourtext " ) . '<br>' ;
2009-07-29 20:39:31 +02:00
//Date send
if ( isset ( $object -> date_envoi ))
print $langs -> trans ( " DateLastSend " ) . " : " . dol_print_date ( $object -> date_envoi , " dayhourtext " ) . '<br>' ;
2009-02-19 20:06:12 +01:00
}
2009-02-19 19:57:54 +01:00
/**
2009-03-25 20:12:35 +01:00
* \brief Return true if email has a domain name that can ' t be resolved
2009-02-19 19:57:54 +01:00
* \param mail adresse email ( Ex : " toto@titi.com " , " John Do <johndo@titi.com> " )
2009-03-25 20:12:35 +01:00
* \return boolean true if domain email is OK , false if KO
2009-02-19 19:57:54 +01:00
*/
2009-03-25 20:12:35 +01:00
function isValidMailDomain ( $mail )
2009-02-19 19:57:54 +01:00
{
2009-10-20 15:14:44 +02:00
list ( $user , $domain ) = explode ( " @ " , $mail , 2 );
2009-02-19 19:57:54 +01:00
if ( checkdnsrr ( $domain , " MX " ))
{
return true ;
}
else
{
return false ;
}
}
2009-03-25 20:12:35 +01:00
2009-04-06 10:34:42 +02:00
/**
* \brief Url string validation
* \remarks < http [ s ] > :// [ user [ : pass ] @ ] hostname [ port ] [ / path ] [ ? getquery ] [ anchor ]
* \param url Url
* \param http 1 : verify http , 0 : not verify http
* \param pass 1 : verify user and pass , 0 : not verify user and pass
* \param port 1 : verify port , 0 : not verify port
* \param path 1 : verify path , 0 : not verify path
* \param query 1 : verify query , 0 : not verify query
* \param anchor 1 : verify anchor , 0 : not verify anchor
* \return int 1 = Check is OK , 0 = Check is KO
*/
function isValidUrl ( $url , $http = 0 , $pass = 0 , $port = 0 , $path = 0 , $query = 0 , $anchor = 0 )
{
$ValidUrl = 0 ;
$urlregex = '' ;
2009-07-29 20:39:31 +02:00
2009-04-06 10:34:42 +02:00
// SCHEME
2009-10-24 01:01:03 +02:00
if ( $http ) $urlregex .= " ^(http: \ / \ /|https: \ / \ /) " ;
2009-07-29 20:39:31 +02:00
2009-04-06 10:34:42 +02:00
// USER AND PASS
if ( $pass ) $urlregex .= " ([a-z0-9+!*(),;?&= \$ _.-]+( \ :[a-z0-9+!*(),;?&= \$ _.-]+)?@) " ;
2009-07-29 20:39:31 +02:00
2009-04-06 10:34:42 +02:00
// HOSTNAME OR IP
//$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*"; // http://x = allowed (ex. http://localhost, http://routerlogin)
//$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)+"; // http://x.x = minimum
$urlregex .= " ([a-z0-9+ \$ _-]+ \ .)*[a-z0-9+ \$ _-] { 2,3} " ; // http://x.xx(x) = minimum
//use only one of the above
2009-07-29 20:39:31 +02:00
2009-04-06 10:34:42 +02:00
// PORT
if ( $port ) $urlregex .= " ( \ :[0-9] { 2,5}) " ;
// PATH
if ( $path ) $urlregex .= " ( \ /([a-z0-9+ \$ _-] \ .?)+)* \ / " ;
// GET Query
2009-10-24 07:22:40 +02:00
if ( $query ) $urlregex .= " ( \ ?[a-z+& \$ _.-][a-z0-9;:@ \ /&%=+ \$ _.-]*) " ;
2009-04-06 10:34:42 +02:00
// ANCHOR
2009-10-24 07:22:40 +02:00
if ( $anchor ) $urlregex .= " (#[a-z_.-][a-z0-9+ \$ _.-]*) $ " ;
2009-07-29 20:39:31 +02:00
2009-04-06 10:34:42 +02:00
// check
2009-10-24 07:22:40 +02:00
if ( preg_match ( '/' . $urlregex . '/i' , $url ))
2009-04-06 10:34:42 +02:00
{
$ValidUrl = 1 ;
}
2009-10-24 01:24:39 +02:00
//print $urlregex.' - '.$url.' - '.$ValidUrl;exit;
2009-07-29 20:39:31 +02:00
2009-04-06 10:34:42 +02:00
return $ValidUrl ;
}
/**
* \brief Clean an url string
* \param url Url
* \param http 1 : keep http :// , 0 : remove also http ://
* \return string Cleaned url
*/
function clean_url ( $url , $http = 1 )
{
// Fixed by Matelli (see http://matelli.fr/showcases/patchs-dolibarr/fix-cleaning-url.html)
// To include the minus sign in a char class, we must not escape it but put it at the end of the class
// Also, there's no need of escape a dot sign in a class
2009-10-21 19:42:31 +02:00
if ( preg_match ( '/^(https?:[\\/]+)?([0-9A-Z.-]+\.[A-Z]{2,4})(:[0-9]+)?/i' , $url , $regs ))
2009-04-06 10:34:42 +02:00
{
$proto = $regs [ 1 ];
$domain = $regs [ 2 ];
$port = $regs [ 3 ];
//print $url." -> ".$proto." - ".$domain." - ".$port;
//$url = dol_string_nospecial(trim($url));
$url = trim ( $url );
// Si http: defini on supprime le http (Si https on ne supprime pas)
$newproto = $proto ;
if ( $http == 0 )
{
2009-10-21 19:42:31 +02:00
if ( preg_match ( '/^http:[\\/]+/i' , $url ))
2009-04-06 10:34:42 +02:00
{
2009-10-21 19:42:31 +02:00
$url = preg_replace ( '/^http:[\\/]+/i' , '' , $url );
2009-04-06 10:34:42 +02:00
$newproto = '' ;
}
}
2009-10-24 01:01:03 +02:00
2009-04-06 10:34:42 +02:00
// On passe le nom de domaine en minuscule
2009-10-23 17:26:45 +02:00
$CleanUrl = preg_replace ( '/^' . preg_quote ( $proto . $domain , '/' ) . '/i' , $newproto . strtolower ( $domain ), $url );
2009-04-06 10:34:42 +02:00
return $CleanUrl ;
}
}
2008-11-16 02:09:04 +01:00
/**
* \brief Return lines of an html table from an array
* \remarks Used by array2table function only
*/
function array2tr ( $data , $troptions = '' , $tdoptions = '' ){
$text = '<tr ' . $troptions . '>' ;
foreach ( $data as $key => $item ){
$text .= '<td ' . $tdoptions . '>' . $item . '</td>' ;
}
$text .= '</tr>' ;
return $text ;
}
/**
* \brief Return an html table from an array
*/
function array2table ( $data , $tableMarkup = 1 , $tableoptions = '' , $troptions = '' , $tdoptions = '' ){
$text = '' ;
if ( $tableMarkup ) $text = '<table ' . $tableoptions . '>' ;
foreach ( $data as $key => $item ){
if ( is_array ( $item )){
$text .= array2tr ( $item , $troptions , $tdoptions ) ;
} else {
$text .= '<tr ' . $troptions . '>' ;
$text .= '<td ' . $tdoptions . '>' . $key . '</td>' ;
$text .= '<td ' . $tdoptions . '>' . $item . '</td>' ;
$text .= '</tr>' ;
}
}
if ( $tableMarkup ) $text .= '</table>' ;
return $text ;
}
2008-09-25 17:54:08 +02:00
2008-06-20 23:10:24 +02:00
/**
* Return next value for a mask
*
2009-01-31 16:23:05 +01:00
* @ param unknown_type $db Database handler
* @ param $mask Mask to use
* @ param unknown_type $table Table containing field with counter
* @ param unknown_type $field Field containing already used values of counter
* @ param unknown_type $where To add a filter on selection ( for exemple to filter on invoice types )
2009-11-06 20:45:34 +01:00
* @ param unknown_type $objsoc The company that own the object we need a counter for
* @ param unknown_type $date Date to use for the { y },{ m },{ d } tags .
2008-06-20 23:10:24 +02:00
* @ return string New value
*/
2009-11-06 20:45:34 +01:00
function get_next_value ( $db , $mask , $table , $field , $where = '' , $objsoc = '' , $date = '' )
2008-06-20 23:10:24 +02:00
{
2009-04-29 18:54:51 +02:00
global $conf ;
2009-07-29 20:39:31 +02:00
2009-11-06 20:45:34 +01:00
if ( ! is_object ( $objsoc )) $valueforccc = $objsoc ;
else $valueforccc = $objsoc -> code_client ;
2008-07-05 16:20:03 +02:00
// Clean parameters
2009-01-07 16:57:02 +01:00
if ( $date == '' ) $date = mktime (); // We use local year and month of PHP server to search numbers
2009-02-20 21:28:16 +01:00
// but we should use local year and month of user
2009-01-15 01:32:10 +01:00
2008-06-20 23:10:24 +02:00
// Extract value for mask counter, mask raz and mask offset
2009-10-23 17:34:46 +02:00
if ( ! preg_match ( '/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i' , $mask , $reg )) return 'ErrorBadMask' ;
2008-06-20 23:10:24 +02:00
$masktri = $reg [ 1 ] . $reg [ 2 ] . $reg [ 3 ];
$maskcounter = $reg [ 1 ];
$maskraz =- 1 ;
$maskoffset = 0 ;
if ( strlen ( $maskcounter ) < 3 ) return 'CounterMustHaveMoreThan3Digits' ;
// Extract value for third party mask counter
2009-10-23 17:34:46 +02:00
if ( preg_match ( '/\{(c+)(0*)\}/i' , $mask , $regClientRef ))
2008-06-20 23:10:24 +02:00
{
$maskrefclient = $regClientRef [ 1 ] . $regClientRef [ 2 ];
$maskrefclient_maskclientcode = $regClientRef [ 1 ];
$maskrefclient_maskcounter = $regClientRef [ 2 ];
$maskrefclient_maskoffset = 0 ; //default value of maskrefclient_counter offset
2009-12-07 18:27:09 +01:00
$maskrefclient_clientcode = substr ( $valueforccc , 0 , strlen ( $maskrefclient_maskclientcode )); //get n first characters of client code where n is length in mask
2008-06-20 23:10:24 +02:00
$maskrefclient_clientcode = str_pad ( $maskrefclient_clientcode , strlen ( $maskrefclient_maskclientcode ), " # " , STR_PAD_RIGHT ); //padding maskrefclient_clientcode for having exactly n characters in maskrefclient_clientcode
2008-10-25 23:35:27 +02:00
$maskrefclient_clientcode = dol_string_nospecial ( $maskrefclient_clientcode ); //sanitize maskrefclient_clientcode for sql insert and sql select like
2008-06-20 23:10:24 +02:00
if ( strlen ( $maskrefclient_maskcounter ) > 0 && strlen ( $maskrefclient_maskcounter ) < 3 ) return 'CounterMustHaveMoreThan3Digits' ;
}
else $maskrefclient = '' ;
2009-12-07 18:27:09 +01:00
// Extract value for third party type
if ( preg_match ( '/\{(t+)\}/i' , $mask , $regType ))
{
$masktype = $regType [ 1 ];
$masktype_value = substr ( preg_replace ( '/^TE_/' , '' , $objsoc -> typent_code ), 0 , strlen ( $regType [ 1 ])); //get n first characters of client code where n is length in mask
$masktype_value = str_pad ( $masktype_value , strlen ( $regType [ 1 ]), " # " , STR_PAD_RIGHT );
}
else $masktype = '' ;
2008-06-20 23:10:24 +02:00
$maskwithonlyymcode = $mask ;
2009-10-21 19:42:31 +02:00
$maskwithonlyymcode = preg_replace ( '/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i' , $maskcounter , $maskwithonlyymcode );
$maskwithonlyymcode = preg_replace ( '/\{dd\}/i' , 'dd' , $maskwithonlyymcode );
$maskwithonlyymcode = preg_replace ( '/\{(c+)(0*)\}/i' , $maskrefclient , $maskwithonlyymcode );
2009-12-07 18:27:09 +01:00
$maskwithonlyymcode = preg_replace ( '/\{(t+)\}/i' , $masktype_value , $maskwithonlyymcode );
2008-06-20 23:10:24 +02:00
$maskwithnocode = $maskwithonlyymcode ;
2009-10-21 19:42:31 +02:00
$maskwithnocode = preg_replace ( '/\{yyyy\}/i' , 'yyyy' , $maskwithnocode );
$maskwithnocode = preg_replace ( '/\{yy\}/i' , 'yy' , $maskwithnocode );
$maskwithnocode = preg_replace ( '/\{y\}/i' , 'y' , $maskwithnocode );
$maskwithnocode = preg_replace ( '/\{mm\}/i' , 'mm' , $maskwithnocode );
2008-06-20 23:10:24 +02:00
// Now maskwithnocode = 0000ddmmyyyyccc for example
// and maskcounter = 0000 for example
//print "maskwithonlyymcode=".$maskwithonlyymcode." maskwithnocode=".$maskwithnocode."\n<br>";
// If an offset is asked
2009-10-21 19:42:31 +02:00
if ( ! empty ( $reg [ 2 ]) && preg_match ( '/^\+/' , $reg [ 2 ])) $maskoffset = preg_replace ( '/^\+/' , '' , $reg [ 2 ]);
if ( ! empty ( $reg [ 3 ]) && preg_match ( '/^\+/' , $reg [ 3 ])) $maskoffset = preg_replace ( '/^\+/' , '' , $reg [ 3 ]);
2008-06-20 23:10:24 +02:00
// Define $sqlwhere
2009-01-07 16:57:02 +01:00
2008-06-20 23:10:24 +02:00
// If a restore to zero after a month is asked we check if there is already a value for this year.
2009-10-21 19:42:31 +02:00
if ( ! empty ( $reg [ 2 ]) && preg_match ( '/^@/' , $reg [ 2 ])) $maskraz = preg_replace ( '/^@/' , '' , $reg [ 2 ]);
if ( ! empty ( $reg [ 3 ]) && preg_match ( '/^@/' , $reg [ 3 ])) $maskraz = preg_replace ( '/^@/' , '' , $reg [ 3 ]);
2008-06-20 23:10:24 +02:00
if ( $maskraz >= 0 )
{
if ( $maskraz > 12 ) return 'ErrorBadMaskBadRazMonth' ;
// Define reg
2009-10-21 19:42:31 +02:00
if ( $maskraz > 1 && ! preg_match ( '/^(.*)\{(y+)\}\{(m+)\}/i' , $maskwithonlyymcode , $reg )) return 'ErrorCantUseRazInStartedYearIfNoYearMonthInMask' ;
if ( $maskraz <= 1 && ! preg_match ( '/^(.*)\{(y+)\}/i' , $maskwithonlyymcode , $reg )) return 'ErrorCantUseRazIfNoYearInMask' ;
2008-06-20 23:10:24 +02:00
//print "x".$maskwithonlyymcode." ".$maskraz;
// Define $yearcomp and $monthcomp (that will be use in the select where to search max number)
$monthcomp = $maskraz ;
$yearoffset = 0 ;
$yearcomp = 0 ;
2008-07-05 16:20:03 +02:00
if ( date ( " m " , $date ) < $maskraz ) { $yearoffset =- 1 ; } // If current month lower that month of return to zero, year is previous year
if ( strlen ( $reg [ 2 ]) == 4 ) $yearcomp = sprintf ( " %04d " , date ( " Y " , $date ) + $yearoffset );
if ( strlen ( $reg [ 2 ]) == 2 ) $yearcomp = sprintf ( " %02d " , date ( " y " , $date ) + $yearoffset );
if ( strlen ( $reg [ 2 ]) == 1 ) $yearcomp = substr ( date ( " y " , $date ), 2 , 1 ) + $yearoffset ;
2009-01-15 01:32:10 +01:00
2008-06-20 23:10:24 +02:00
$sqlwhere = '' ;
2009-01-07 16:57:02 +01:00
$sqlwhere .= '( (SUBSTRING(' . $field . ', ' . ( strlen ( $reg [ 1 ]) + 1 ) . ', ' . strlen ( $reg [ 2 ]) . ') >= ' . $yearcomp ;
2008-06-20 23:10:24 +02:00
if ( $monthcomp > 1 ) // Test useless if monthcomp = 1 (or 0 is same as 1)
{
2009-01-07 16:57:02 +01:00
$sqlwhere .= ' AND SUBSTRING(' . $field . ', ' . ( strlen ( $reg [ 1 ]) + strlen ( $reg [ 2 ]) + 1 ) . ', ' . strlen ( $reg [ 3 ]) . ') >= ' . $monthcomp . ')' ;
$sqlwhere .= ' OR SUBSTRING(' . $field . ', ' . ( strlen ( $reg [ 1 ]) + 1 ) . ', ' . strlen ( $reg [ 2 ]) . ') >= ' . sprintf ( " %02d " ,( $yearcomp + 1 )) . ' )' ;
}
else
{
$sqlwhere .= ') )' ;
2008-06-20 23:10:24 +02:00
}
}
//print "masktri=".$masktri." maskcounter=".$maskcounter." maskraz=".$maskraz." maskoffset=".$maskoffset."<br>\n";
// Define $sqlstring
$posnumstart = strpos ( $maskwithnocode , $maskcounter ); // Pos of counter in final string (from 0 to ...)
if ( $posnumstart < 0 ) return 'ErrorBadMaskFailedToLocatePosOfSequence' ;
$sqlstring = 'SUBSTRING(' . $field . ', ' . ( $posnumstart + 1 ) . ', ' . strlen ( $maskcounter ) . ')' ;
//print "x".$sqlstring;
// Define $maskLike
2008-10-25 23:35:27 +02:00
$maskLike = dol_string_nospecial ( $mask );
2008-06-20 23:10:24 +02:00
$maskLike = str_replace ( " % " , " _ " , $maskLike );
// Replace protected special codes with matching number of _ as wild card caracter
2010-03-31 08:47:27 +02:00
$maskLike = preg_replace ( '/\{yyyy\}/i' , '____' , $maskLike );
$maskLike = preg_replace ( '/\{yy\}/i' , '__' , $maskLike );
$maskLike = preg_replace ( '/\{y\}/i' , '_' , $maskLike );
$maskLike = preg_replace ( '/\{mm\}/i' , '__' , $maskLike );
$maskLike = preg_replace ( '/\{dd\}/i' , '__' , $maskLike );
2008-10-25 23:35:27 +02:00
$maskLike = str_replace ( dol_string_nospecial ( '{' . $masktri . '}' ), str_pad ( " " , strlen ( $maskcounter ), " _ " ), $maskLike );
if ( $maskrefclient ) $maskLike = str_replace ( dol_string_nospecial ( '{' . $maskrefclient . '}' ), str_pad ( " " , strlen ( $maskrefclient ), " _ " ), $maskLike );
2009-12-07 18:27:09 +01:00
//if ($masktype) $maskLike = str_replace(dol_string_nospecial('{'.$masktype.'}'),str_pad("",strlen($masktype),"_"),$maskLike);
if ( $masktype ) $maskLike = str_replace ( dol_string_nospecial ( '{' . $masktype . '}' ), $masktype_value , $maskLike );
2008-06-20 23:10:24 +02:00
// Get counter in database
$counter = 0 ;
$sql = " SELECT MAX( " . $sqlstring . " ) as val " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $table ;
// $sql.= " WHERE ".$field." not like '(%'";
2009-10-24 16:33:18 +02:00
$sql .= " WHERE " . $field . " LIKE ' " . $maskLike . " ' " ;
$sql .= " AND " . $field . " NOT LIKE '%PROV%' " ;
2009-04-29 18:54:51 +02:00
$sql .= " AND entity = " . $conf -> entity ;
2008-06-20 23:10:24 +02:00
if ( $where ) $sql .= $where ;
if ( $sqlwhere ) $sql .= ' AND ' . $sqlwhere ;
2009-10-24 16:33:18 +02:00
//print $sql.'<br>';
2009-02-20 21:28:16 +01:00
dol_syslog ( " functions2::get_next_value sql= " . $sql , LOG_DEBUG );
2008-06-20 23:10:24 +02:00
$resql = $db -> query ( $sql );
if ( $resql )
{
$obj = $db -> fetch_object ( $resql );
$counter = $obj -> val ;
}
2009-02-20 23:53:15 +01:00
else dol_print_error ( $db );
2009-10-23 17:34:46 +02:00
if ( empty ( $counter ) || preg_match ( '/[^0-9]/i' , $counter )) $counter = $maskoffset ;
2008-06-20 23:10:24 +02:00
$counter ++ ;
if ( $maskrefclient_maskcounter )
{
//print "maskrefclient_maskcounter=".$maskrefclient_maskcounter." maskwithnocode=".$maskwithnocode." maskrefclient=".$maskrefclient."\n<br>";
2009-01-15 01:32:10 +01:00
2008-06-20 23:10:24 +02:00
// Define $sqlstring
$maskrefclient_posnumstart = strpos ( $maskwithnocode , $maskrefclient_maskcounter , strpos ( $maskwithnocode , $maskrefclient )); // Pos of counter in final string (from 0 to ...)
if ( $maskrefclient_posnumstart <= 0 ) return 'ErrorBadMask' ;
$maskrefclient_sqlstring = 'SUBSTRING(' . $field . ', ' . ( $maskrefclient_posnumstart + 1 ) . ', ' . strlen ( $maskrefclient_maskcounter ) . ')' ;
//print "x".$sqlstring;
// Define $maskrefclient_maskLike
2008-10-25 23:35:27 +02:00
$maskrefclient_maskLike = dol_string_nospecial ( $mask );
2008-06-20 23:10:24 +02:00
$maskrefclient_maskLike = str_replace ( " % " , " _ " , $maskrefclient_maskLike );
// Replace protected special codes with matching number of _ as wild card caracter
2008-10-25 23:35:27 +02:00
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{yyyy}' ), '____' , $maskrefclient_maskLike );
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{yy}' ), '__' , $maskrefclient_maskLike );
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{y}' ), '_' , $maskrefclient_maskLike );
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{mm}' ), '__' , $maskrefclient_maskLike );
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{dd}' ), '__' , $maskrefclient_maskLike );
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{' . $masktri . '}' ), str_pad ( " " , strlen ( $maskcounter ), " _ " ), $maskrefclient_maskLike );
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{' . $maskrefclient . '}' ), $maskrefclient_clientcode . str_pad ( " " , strlen ( $maskrefclient_maskcounter ), " _ " ), $maskrefclient_maskLike );
2008-06-20 23:10:24 +02:00
// Get counter in database
$maskrefclient_counter = 0 ;
$maskrefclient_sql = " SELECT MAX( " . $maskrefclient_sqlstring . " ) as val " ;
$maskrefclient_sql .= " FROM " . MAIN_DB_PREFIX . $table ;
//$sql.= " WHERE ".$field." not like '(%'";
2009-10-24 16:33:18 +02:00
$maskrefclient_sql .= " WHERE " . $field . " LIKE ' " . $maskrefclient_maskLike . " ' " ;
$maskrefclient_sql .= " AND entity = " . $conf -> entity ;
2008-07-07 15:03:54 +02:00
if ( $where ) $maskrefclient_sql .= $where ; //use the same optional where as general mask
2008-06-20 23:10:24 +02:00
if ( $sqlwhere ) $maskrefclient_sql .= ' AND ' . $sqlwhere ; //use the same sqlwhere as general mask
$maskrefclient_sql .= ' AND (SUBSTRING(' . $field . ', ' . ( strpos ( $maskwithnocode , $maskrefclient ) + 1 ) . ', ' . strlen ( $maskrefclient_maskclientcode ) . " )=' " . $maskrefclient_clientcode . " ') " ;
2009-01-15 01:32:10 +01:00
2009-02-20 21:28:16 +01:00
dol_syslog ( " functions2::get_next_value maskrefclient_sql= " . $maskrefclient_sql , LOG_DEBUG );
2008-06-20 23:10:24 +02:00
$maskrefclient_resql = $db -> query ( $maskrefclient_sql );
if ( $maskrefclient_resql )
{
$maskrefclient_obj = $db -> fetch_object ( $maskrefclient_resql );
$maskrefclient_counter = $maskrefclient_obj -> val ;
}
2009-02-20 23:53:15 +01:00
else dol_print_error ( $db );
2009-10-23 17:34:46 +02:00
if ( empty ( $maskrefclient_counter ) || preg_match ( '/[^0-9]/i' , $maskrefclient_counter )) $maskrefclient_counter = $maskrefclient_maskoffset ;
2008-06-20 23:10:24 +02:00
$maskrefclient_counter ++ ;
}
// Build numFinal
$numFinal = $mask ;
// We replace special codes except refclient
2009-12-10 17:04:59 +01:00
$numFinal = preg_replace ( '/\{yyyy\}/i' , date ( " Y " , $date ), $numFinal );
$numFinal = preg_replace ( '/\{yy\}/i' , date ( " y " , $date ), $numFinal );
$numFinal = preg_replace ( '/\{y\}/i' , substr ( date ( " y " , $date ), 2 , 1 ), $numFinal );
$numFinal = preg_replace ( '/\{mm\}/i' , date ( " m " , $date ), $numFinal );
$numFinal = preg_replace ( '/\{dd\}/i' , date ( " d " , $date ), $numFinal );
2008-06-20 23:10:24 +02:00
// Now we replace the counter
$maskbefore = '{' . $masktri . '}' ;
$maskafter = str_pad ( $counter , strlen ( $maskcounter ), " 0 " , STR_PAD_LEFT );
//print 'x'.$maskbefore.'-'.$maskafter.'y';
$numFinal = str_replace ( $maskbefore , $maskafter , $numFinal );
// Now we replace the refclient
if ( $maskrefclient )
{
//print "maskrefclient=".$maskrefclient." maskwithonlyymcode=".$maskwithonlyymcode." maskwithnocode=".$maskwithnocode."\n<br>";
$maskrefclient_maskbefore = '{' . $maskrefclient . '}' ;
$maskrefclient_maskafter = $maskrefclient_clientcode . str_pad ( $maskrefclient_counter , strlen ( $maskrefclient_maskcounter ), " 0 " , STR_PAD_LEFT );
$numFinal = str_replace ( $maskrefclient_maskbefore , $maskrefclient_maskafter , $numFinal );
}
2009-12-07 18:27:09 +01:00
// Now we replace the type
if ( $masktype )
{
$masktype_maskbefore = '{' . $masktype . '}' ;
$masktype_maskafter = $masktype_value ;
$numFinal = str_replace ( $masktype_maskbefore , $masktype_maskafter , $numFinal );
}
2009-02-20 21:28:16 +01:00
dol_syslog ( " functions2::get_next_value return " . $numFinal , LOG_DEBUG );
2008-06-20 23:10:24 +02:00
return $numFinal ;
2008-11-16 02:09:04 +01:00
}
2009-01-31 16:23:05 +01:00
/**
* Check value
*
* @ param unknown_type $db Database handler
* @ param $mask Mask to use
* @ param unknown_type $table Table containing field with counter
* @ param unknown_type $field Field containing already used values of counter
* @ param unknown_type $where To add a filter on selection ( for exemple to filter on invoice types )
* @ param unknown_type $valueforccc
* @ param unknown_type $date
* @ return int < 0 if KO , 0 if OK
*/
function check_value ( $mask , $value )
{
$result = 0 ;
// Extract value for mask counter, mask raz and mask offset
2009-10-23 17:34:46 +02:00
if ( ! preg_match ( '/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i' , $mask , $reg )) return 'ErrorBadMask' ;
2009-01-31 16:23:05 +01:00
$masktri = $reg [ 1 ] . $reg [ 2 ] . $reg [ 3 ];
$maskcounter = $reg [ 1 ];
$maskraz =- 1 ;
$maskoffset = 0 ;
if ( strlen ( $maskcounter ) < 3 ) return 'CounterMustHaveMoreThan3Digits' ;
// Extract value for third party mask counter
2009-10-23 17:34:46 +02:00
if ( preg_match ( '/\{(c+)(0*)\}/i' , $mask , $regClientRef ))
2009-01-31 16:23:05 +01:00
{
$maskrefclient = $regClientRef [ 1 ] . $regClientRef [ 2 ];
$maskrefclient_maskclientcode = $regClientRef [ 1 ];
$maskrefclient_maskcounter = $regClientRef [ 2 ];
$maskrefclient_maskoffset = 0 ; //default value of maskrefclient_counter offset
$maskrefclient_clientcode = substr ( $valueforccc , 0 , strlen ( $maskrefclient_maskclientcode )); //get n first characters of client code to form maskrefclient_clientcode
$maskrefclient_clientcode = str_pad ( $maskrefclient_clientcode , strlen ( $maskrefclient_maskclientcode ), " # " , STR_PAD_RIGHT ); //padding maskrefclient_clientcode for having exactly n characters in maskrefclient_clientcode
$maskrefclient_clientcode = dol_string_nospecial ( $maskrefclient_clientcode ); //sanitize maskrefclient_clientcode for sql insert and sql select like
if ( strlen ( $maskrefclient_maskcounter ) > 0 && strlen ( $maskrefclient_maskcounter ) < 3 ) return 'CounterMustHaveMoreThan3Digits' ;
}
else $maskrefclient = '' ;
$maskwithonlyymcode = $mask ;
2009-10-21 19:42:31 +02:00
$maskwithonlyymcode = preg_replace ( '/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i' , $maskcounter , $maskwithonlyymcode );
$maskwithonlyymcode = preg_replace ( '/\{dd\}/i' , 'dd' , $maskwithonlyymcode );
$maskwithonlyymcode = preg_replace ( '/\{(c+)(0*)\}/i' , $maskrefclient , $maskwithonlyymcode );
2009-01-31 16:23:05 +01:00
$maskwithnocode = $maskwithonlyymcode ;
2009-10-21 19:42:31 +02:00
$maskwithnocode = preg_replace ( '/\{yyyy\}/i' , 'yyyy' , $maskwithnocode );
$maskwithnocode = preg_replace ( '/\{yy\}/i' , 'yy' , $maskwithnocode );
$maskwithnocode = preg_replace ( '/\{y\}/i' , 'y' , $maskwithnocode );
$maskwithnocode = preg_replace ( '/\{mm\}/i' , 'mm' , $maskwithnocode );
2009-01-31 16:23:05 +01:00
// Now maskwithnocode = 0000ddmmyyyyccc for example
// and maskcounter = 0000 for example
//print "maskwithonlyymcode=".$maskwithonlyymcode." maskwithnocode=".$maskwithnocode."\n<br>";
// If an offset is asked
2009-10-21 19:42:31 +02:00
if ( ! empty ( $reg [ 2 ]) && preg_match ( '/^\+/' , $reg [ 2 ])) $maskoffset = preg_replace ( '/^\+/' , '' , $reg [ 2 ]);
if ( ! empty ( $reg [ 3 ]) && preg_match ( '^\+' , $reg [ 3 ])) $maskoffset = preg_replace ( '/^\+/' , '' , $reg [ 3 ]);
2009-01-31 16:23:05 +01:00
// Define $sqlwhere
// If a restore to zero after a month is asked we check if there is already a value for this year.
2009-10-21 19:42:31 +02:00
if ( ! empty ( $reg [ 2 ]) && preg_match ( '/^@/' , $reg [ 2 ])) $maskraz = preg_replace ( '/^@/' , '' , $reg [ 2 ]);
if ( ! empty ( $reg [ 3 ]) && preg_match ( '/^@/' , $reg [ 3 ])) $maskraz = preg_replace ( '/^@/' , '' , $reg [ 3 ]);
2009-01-31 16:23:05 +01:00
if ( $maskraz >= 0 )
{
if ( $maskraz > 12 ) return 'ErrorBadMaskBadRazMonth' ;
// Define reg
2009-10-21 19:42:31 +02:00
if ( $maskraz > 1 && ! preg_match ( '/^(.*)\{(y+)\}\{(m+)\}/i' , $maskwithonlyymcode , $reg )) return 'ErrorCantUseRazInStartedYearIfNoYearMonthInMask' ;
if ( $maskraz <= 1 && ! preg_match ( '/^(.*)\{(y+)\}/i' , $maskwithonlyymcode , $reg )) return 'ErrorCantUseRazIfNoYearInMask' ;
2009-01-31 16:23:05 +01:00
//print "x".$maskwithonlyymcode." ".$maskraz;
}
//print "masktri=".$masktri." maskcounter=".$maskcounter." maskraz=".$maskraz." maskoffset=".$maskoffset."<br>\n";
// Check we have a number in ($posnumstart+1).', '.strlen($maskcounter)
//
// Check length
$len = strlen ( $maskwithnocode );
if ( strlen ( $value ) != $len ) $result =- 1 ;
// Define $maskLike
$maskLike = dol_string_nospecial ( $mask );
$maskLike = str_replace ( " % " , " _ " , $maskLike );
// Replace protected special codes with matching number of _ as wild card caracter
$maskLike = str_replace ( dol_string_nospecial ( '{yyyy}' ), '____' , $maskLike );
$maskLike = str_replace ( dol_string_nospecial ( '{yy}' ), '__' , $maskLike );
$maskLike = str_replace ( dol_string_nospecial ( '{y}' ), '_' , $maskLike );
$maskLike = str_replace ( dol_string_nospecial ( '{mm}' ), '__' , $maskLike );
$maskLike = str_replace ( dol_string_nospecial ( '{dd}' ), '__' , $maskLike );
$maskLike = str_replace ( dol_string_nospecial ( '{' . $masktri . '}' ), str_pad ( " " , strlen ( $maskcounter ), " _ " ), $maskLike );
if ( $maskrefclient ) $maskLike = str_replace ( dol_string_nospecial ( '{' . $maskrefclient . '}' ), str_pad ( " " , strlen ( $maskrefclient ), " _ " ), $maskLike );
2009-02-20 21:28:16 +01:00
dol_syslog ( " functions2::check_value result= " . $result , LOG_DEBUG );
2009-01-31 16:23:05 +01:00
return $result ;
}
2008-11-16 02:09:04 +01:00
/**
* \brief Convert a binary data to string that represent hexadecimal value
* \param bin Value to convert
* \param pad Add 0
* \param upper Convert to tupper
* \return string x
*/
function binhex ( $bin , $pad = false , $upper = false )
{
$last = strlen ( $bin ) - 1 ;
for ( $i = 0 ; $i <= $last ; $i ++ ){ $x += $bin [ $last - $i ] * pow ( 2 , $i ); }
$x = dechex ( $x );
if ( $pad ){ while ( strlen ( $x ) < intval ( strlen ( $bin )) / 4 ){ $x = " 0 $x " ; } }
if ( $upper ){ $x = strtoupper ( $x ); }
return $x ;
}
/**
* \brief Convertir de l ' hexadecimal en binaire
* \param string hexa
* \return string bin
*/
function hexbin ( $hexa )
{
$bin = '' ;
for ( $i = 0 ; $i < strlen ( $hexa ); $i ++ )
{
$bin .= str_pad ( decbin ( hexdec ( $hexa { $i })), 4 , '0' , STR_PAD_LEFT );
}
return $bin ;
}
/**
* \brief Return if a filename is file name of a supported image format
* \param file Filename
* \return int - 1 = Not image filename , 0 = Image filename but format not supported by PHP , 1 = Image filename with format supported
*/
function image_format_supported ( $file )
{
// Case filename is not a format image
2009-10-23 17:34:46 +02:00
if ( ! preg_match ( '/(\.gif|\.jpg|\.jpeg|\.png|\.bmp)$/i' , $file , $reg )) return - 1 ;
2008-11-16 02:09:04 +01:00
// Case filename is a format image but not supported by this PHP
$imgfonction = '' ;
if ( strtolower ( $reg [ 1 ]) == '.gif' ) $imgfonction = 'imagecreatefromgif' ;
if ( strtolower ( $reg [ 1 ]) == '.png' ) $imgfonction = 'imagecreatefrompng' ;
if ( strtolower ( $reg [ 1 ]) == '.jpg' ) $imgfonction = 'imagecreatefromjpeg' ;
if ( strtolower ( $reg [ 1 ]) == '.jpeg' ) $imgfonction = 'imagecreatefromjpeg' ;
if ( strtolower ( $reg [ 1 ]) == '.bmp' ) $imgfonction = 'imagecreatefromwbmp' ;
if ( $imgfonction )
{
if ( ! function_exists ( $imgfonction ))
{
// Fonctions de conversion non presente dans ce PHP
return 0 ;
}
}
// Filename is a format image and supported by this PHP
return 1 ;
}
/**
* \brief Retourne le numero de la semaine par rapport a une date
* \param time Date au format 'timestamp'
* \return int Numero de semaine
*/
function numero_semaine ( $time )
{
$stime = strftime ( '%Y-%m-%d' , $time );
2009-10-21 19:42:31 +02:00
if ( preg_match ( '/^([0-9]+)\-([0-9]+)\-([0-9]+)\s?([0-9]+)?:?([0-9]+)?/i' , $stime , $reg ))
2008-11-16 02:09:04 +01:00
{
// Date est au format 'YYYY-MM-DD' ou 'YYYY-MM-DD HH:MM:SS'
$annee = $reg [ 1 ];
$mois = $reg [ 2 ];
$jour = $reg [ 3 ];
}
/*
* Norme ISO - 8601 :
2009-04-29 18:54:51 +02:00
* - La semaine 1 de toute annee est celle qui contient le 4 janvier ou que la semaine 1 de toute annee est celle qui contient le 1 er jeudi de janvier .
* - La majorite des annees ont 52 semaines mais les annees qui commence un jeudi et les annees bissextiles commencant un mercredi en possede 53.
2008-11-16 02:09:04 +01:00
* - Le 1 er jour de la semaine est le Lundi
*/
2009-04-29 18:54:51 +02:00
// Definition du Jeudi de la semaine
2008-11-16 02:09:04 +01:00
if ( date ( " w " , mktime ( 12 , 0 , 0 , $mois , $jour , $annee )) == 0 ) // Dimanche
$jeudiSemaine = mktime ( 12 , 0 , 0 , $mois , $jour , $annee ) - 3 * 24 * 60 * 60 ;
else if ( date ( " w " , mktime ( 12 , 0 , 0 , $mois , $jour , $annee )) < 4 ) // du Lundi au Mercredi
$jeudiSemaine = mktime ( 12 , 0 , 0 , $mois , $jour , $annee ) + ( 4 - date ( " w " , mktime ( 12 , 0 , 0 , $mois , $jour , $annee ))) * 24 * 60 * 60 ;
else if ( date ( " w " , mktime ( 12 , 0 , 0 , $mois , $jour , $annee )) > 4 ) // du Vendredi au Samedi
$jeudiSemaine = mktime ( 12 , 0 , 0 , $mois , $jour , $annee ) - ( date ( " w " , mktime ( 12 , 0 , 0 , $mois , $jour , $annee )) - 4 ) * 24 * 60 * 60 ;
else // Jeudi
$jeudiSemaine = mktime ( 12 , 0 , 0 , $mois , $jour , $annee );
2009-04-29 18:54:51 +02:00
// Definition du premier Jeudi de l'annee
2008-11-16 02:09:04 +01:00
if ( date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ))) == 0 ) // Dimanche
{
$premierJeudiAnnee = mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine )) + 4 * 24 * 60 * 60 ;
}
else if ( date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ))) < 4 ) // du Lundi au Mercredi
{
$premierJeudiAnnee = mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine )) + ( 4 - date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine )))) * 24 * 60 * 60 ;
}
else if ( date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ))) > 4 ) // du Vendredi au Samedi
{
$premierJeudiAnnee = mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine )) + ( 7 - ( date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ))) - 4 )) * 24 * 60 * 60 ;
}
else // Jeudi
{
$premierJeudiAnnee = mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ));
}
2009-10-21 19:42:31 +02:00
// Definition du numero de semaine: nb de jours entre "premier Jeudi de l'annee" et "Jeudi de la semaine";
2008-11-16 02:09:04 +01:00
$numeroSemaine = (
(
date ( " z " , mktime ( 12 , 0 , 0 , date ( " m " , $jeudiSemaine ), date ( " d " , $jeudiSemaine ), date ( " Y " , $jeudiSemaine )))
-
date ( " z " , mktime ( 12 , 0 , 0 , date ( " m " , $premierJeudiAnnee ), date ( " d " , $premierJeudiAnnee ), date ( " Y " , $premierJeudiAnnee )))
) / 7
) + 1 ;
// Cas particulier de la semaine 53
if ( $numeroSemaine == 53 )
{
2009-10-21 19:42:31 +02:00
// Les annees qui commence un Jeudi et les annees bissextiles commencant un Mercredi en possede 53
2008-11-16 02:09:04 +01:00
if ( date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ))) == 4 || ( date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ))) == 3 && date ( " z " , mktime ( 12 , 0 , 0 , 12 , 31 , date ( " Y " , $jeudiSemaine ))) == 365 ))
{
$numeroSemaine = 53 ;
}
else
{
$numeroSemaine = 1 ;
}
}
//echo $jour."-".$mois."-".$annee." (".date("d-m-Y",$premierJeudiAnnee)." - ".date("d-m-Y",$jeudiSemaine).") -> ".$numeroSemaine."<BR>";
return sprintf ( " %02d " , $numeroSemaine );
}
/**
* \brief Convertit une masse d ' une unite vers une autre unite
* \param weight float Masse a convertir
* \param from_unit int Unite originale en puissance de 10
* \param to_unit int Nouvelle unite en puissance de 10
* \return float Masse convertie
*/
function weight_convert ( $weight , & $from_unit , $to_unit )
{
/* Pour convertire 320 gr en Kg appeler
* $f = - 3
* weigh_convert ( 320 , $f , 0 ) retournera 0.32
*
*/
while ( $from_unit <> $to_unit )
{
if ( $from_unit > $to_unit )
{
2009-02-20 21:28:16 +01:00
$weight = $weight * 10 ;
$from_unit = $from_unit - 1 ;
$weight = weight_convert ( $weight , $from_unit , $to_unit );
2008-11-16 02:09:04 +01:00
}
if ( $from_unit < $to_unit )
{
2009-02-20 21:28:16 +01:00
$weight = $weight / 10 ;
$from_unit = $from_unit + 1 ;
$weight = weight_convert ( $weight , $from_unit , $to_unit );
2008-11-16 02:09:04 +01:00
}
}
return $weight ;
}
2009-01-07 11:57:36 +01:00
/**
* \brief Save personnal parameter
* \param db Handler database
* \param user Object user
* \param tab Tableau ( cle => valeur ) des parametres a sauvegarder
2009-02-11 03:18:27 +01:00
* \return int < 0 if KO , > 0 if OK
2009-01-07 11:57:36 +01:00
*/
2009-04-27 22:37:50 +02:00
function dol_set_user_param ( $db , $conf , & $user , $tab )
2009-01-07 11:57:36 +01:00
{
// Verification parametres
if ( sizeof ( $tab ) < 1 ) return - 1 ;
$db -> begin ();
// We remove old parameters for all keys in $tab
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " user_param " ;
$sql .= " WHERE fk_user = " . $user -> id ;
2009-04-27 22:37:50 +02:00
$sql .= " AND entity = " . $conf -> entity ;
2009-01-07 11:57:36 +01:00
$sql .= " AND param in ( " ;
$i = 0 ;
foreach ( $tab as $key => $value )
{
if ( $i > 0 ) $sql .= ',' ;
$sql .= " ' " . $key . " ' " ;
$i ++ ;
}
$sql .= " ) " ;
2009-02-20 21:28:16 +01:00
dol_syslog ( " functions2.lib::dol_set_user_param sql= " . $sql , LOG_DEBUG );
2009-01-07 11:57:36 +01:00
$resql = $db -> query ( $sql );
if ( ! $resql )
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $db );
2009-01-07 11:57:36 +01:00
$db -> rollback ();
2009-02-11 03:18:27 +01:00
return - 1 ;
2009-01-07 11:57:36 +01:00
}
foreach ( $tab as $key => $value )
{
// Set new parameters
if ( $value && ( ! $url || in_array ( $key , array ( 'sortfield' , 'sortorder' , 'begin' , 'page' ))))
{
2009-04-27 22:37:50 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " user_param(fk_user,entity,param,value) " ;
$sql .= " VALUES ( " . $user -> id . " , " . $conf -> entity . " , " ;
2009-01-07 11:57:36 +01:00
$sql .= " ' " . $key . " ',' " . addslashes ( $value ) . " '); " ;
2009-02-20 21:28:16 +01:00
dol_syslog ( " functions2.lib::dol_set_user_param sql= " . $sql , LOG_DEBUG );
2009-01-07 11:57:36 +01:00
$result = $db -> query ( $sql );
if ( ! $result )
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $db );
2009-01-07 11:57:36 +01:00
$db -> rollback ();
2009-02-11 03:18:27 +01:00
return - 1 ;
2009-01-07 11:57:36 +01:00
}
$user -> page_param [ $key ] = $value ;
}
}
$db -> commit ();
return 1 ;
}
/**
* \brief Returns formated reduction
* \param reduction Reduction percentage
* \return string Formated reduction
*/
2009-01-15 01:32:10 +01:00
function dol_print_reduction ( $reduction = 0 , $langs )
2009-01-07 11:57:36 +01:00
{
$string = '' ;
if ( $reduction == 100 )
{
$string = $langs -> trans ( " Offered " );
}
else
{
$string = $reduction . '%' ;
}
return $string ;
}
2009-03-09 22:59:29 +01:00
/**
* \brief Return OS version
* \return string OS version
*/
function version_os ()
{
// Get version of OS
ob_start ();
phpinfo ();
$chaine = ob_get_contents ();
ob_end_clean ();
2009-10-24 08:10:00 +02:00
preg_match ( '/System <\/td><td class="v">([^<]*)<\/td>/i' , $chaine , $reg );
2009-08-20 19:10:47 +02:00
$osversion = $reg [ 1 ];
return $osversion ;
2009-03-09 22:59:29 +01:00
}
/**
* \brief Return PHP version
* \return string PHP version
*/
function version_php ()
{
return phpversion ();
}
/**
* \brief Return Dolibarr version
* \return string Dolibarr version
*/
function version_dolibarr ()
{
return DOL_VERSION ;
}
/**
* \brief Return web server version
* \return string Web server version
*/
function version_webserver ()
{
return $_SERVER [ " SERVER_SOFTWARE " ];
}
2010-02-27 11:11:00 +01:00
/**
* \brief Show picto of country for a language code
* \return string
*/
function picto_from_langcode ( $codelang )
{
$ret = '' ;
if ( ! empty ( $codelang ))
{
2010-02-27 14:41:24 +01:00
if ( $codelang == 'auto' ) $ret = img_picto ( '' , DOL_URL_ROOT . '/theme/common/flags/int.png' , '' , 1 );
2010-02-27 11:11:00 +01:00
else {
//print $codelang;
2010-02-28 01:27:37 +01:00
$langtocountryflag = array ( 'da_DA' => 'dk' , 'fr_CA' => 'mq' , 'ca_ES' => 'catalonia' , 'ar_AR' => '' );
2010-02-27 11:11:00 +01:00
$tmpcode = '' ;
if ( isset ( $langtocountryflag [ $codelang ])) $tmpcode = $langtocountryflag [ $codelang ];
else
{
$tmparray = explode ( '_' , $codelang );
$tmpcode = $tmparray [ 1 ];
}
2010-02-27 14:41:24 +01:00
if ( $tmpcode ) $ret .= img_picto ( $codelang , DOL_URL_ROOT . '/theme/common/flags/' . strtolower ( $tmpcode ) . '.png' , '' , 1 );
2010-02-27 11:11:00 +01:00
}
}
return $ret ;
}
2010-02-27 23:53:27 +01:00
/**
2010-02-28 23:49:06 +01:00
* \brief Return list of activated modules usable for document generation .
2010-03-27 01:33:28 +01:00
* \param $db Database handler
* \param $type Type of models ( company , invoice , ... )
* \return int or array 0 if no module is activated , or array ( key => label ) . For modules that need directory scan , key is completed with " :filename " .
2010-02-27 23:53:27 +01:00
*/
2010-02-28 23:49:06 +01:00
function getListOfModels ( $db , $type )
2010-02-27 23:53:27 +01:00
{
2010-03-27 01:57:49 +01:00
global $conf , $langs ;
2010-02-27 23:53:27 +01:00
$liste = array ();
2010-03-27 01:33:28 +01:00
$found = 0 ;
2010-02-27 23:53:27 +01:00
2010-02-28 23:49:06 +01:00
$dirtoscan = '' ;
$sql = " SELECT nom as id, nom as lib, libelle as label, description as description " ;
2010-02-27 23:53:27 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " document_model " ;
$sql .= " WHERE type = ' " . $type . " ' " ;
$sql .= " AND entity = " . $conf -> entity ;
$resql = $db -> query ( $sql );
if ( $resql )
{
$num = $db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
2010-03-27 01:33:28 +01:00
$found = 1 ;
2010-02-27 23:53:27 +01:00
$obj = $db -> fetch_object ( $resql );
2010-02-28 19:37:29 +01:00
2010-03-27 01:57:49 +01:00
// If this generation module needs to scan a directory, then description field is filled
2010-03-27 01:33:28 +01:00
// with the constant that contains list of directories to scan.
2010-03-27 01:57:49 +01:00
if ( ! empty ( $obj -> description )) // List of directories to scan is defined
2010-02-28 19:37:29 +01:00
{
2010-02-28 23:49:06 +01:00
$const = $obj -> description ;
$dirtoscan .= ( $dirtoscan ? ',' : '' ) . preg_replace ( '/[\r\n]+/' , ',' , trim ( $conf -> global -> $const ));
$listoffiles = array ();
// Now we add models found in directories scanned
$listofdir = explode ( ',' , $dirtoscan );
foreach ( $listofdir as $key => $tmpdir )
{
$tmpdir = trim ( $tmpdir );
$tmpdir = preg_replace ( '/DOL_DATA_ROOT/' , DOL_DATA_ROOT , $tmpdir );
if ( ! $tmpdir ) { unset ( $listofdir [ $key ]); continue ; }
if ( is_dir ( $tmpdir ))
{
$tmpfiles = dol_dir_list ( $tmpdir , 'files' , 0 , '\.odt' );
if ( sizeof ( $tmpfiles )) $listoffiles = array_merge ( $listoffiles , $tmpfiles );
}
}
2010-03-27 01:57:49 +01:00
if ( sizeof ( $listoffiles ))
{
foreach ( $listoffiles as $record )
{
$liste [ $obj -> id . ':' . $record [ 'fullname' ]] = dol_trunc ( $record [ 'name' ], 24 , 'middle' );
}
}
else
2010-02-28 23:49:06 +01:00
{
2010-03-27 01:57:49 +01:00
$liste [ 0 ] = $obj -> label . ': ' . $langs -> trans ( " None " );
2010-02-28 23:49:06 +01:00
}
}
else
{
$liste [ $obj -> id ] = $obj -> label ? $obj -> label : $obj -> lib ;
2010-02-28 19:37:29 +01:00
}
2010-02-27 23:53:27 +01:00
$i ++ ;
}
}
else
{
$this -> error = $db -> error ();
return - 1 ;
}
2010-03-27 01:33:28 +01:00
if ( $found ) return $liste ;
else return 0 ;
2010-02-27 23:53:27 +01:00
}
2010-04-13 21:56:24 +02:00
/**
* \brief This function evaluates a string that should be a valid IPv4
* \return It returns 0 if $ip is not a valid IPv4
* It returns 1 if $ip is a valid IPv4 and is a public IP
* It returns 2 if $ip is a valid IPv4 and is a private lan IP
*/
function is_ip ( $ip )
{
2010-04-13 22:30:25 +02:00
if ( ! preg_match ( " /^([0-9] { 1,3}) \ .([0-9] { 1,3}) \ .([0-9] { 1,3}) \ .([0-9] { 1,3}) $ / " , $ip )) return 0 ;
2010-04-13 21:56:24 +02:00
if ( sprintf ( " %u " , ip2long ( $ip )) == sprintf ( " %u " , ip2long ( '255.255.255.255' ))) return 0 ;
if ( sprintf ( " %u " , ip2long ( '10.0.0.0' )) <= sprintf ( " %u " , ip2long ( $ip )) and sprintf ( " %u " , ip2long ( $ip )) <= sprintf ( " %u " , ip2long ( '10.255.255.255' ))) return 2 ;
if ( sprintf ( " %u " , ip2long ( '172.16.0.0' )) <= sprintf ( " %u " , ip2long ( $ip )) and sprintf ( " %u " , ip2long ( $ip )) <= sprintf ( " %u " , ip2long ( '172.31.255.255' ))) return 2 ;
if ( sprintf ( " %u " , ip2long ( '192.168.0.0' )) <= sprintf ( " %u " , ip2long ( $ip )) and sprintf ( " %u " , ip2long ( $ip )) <= sprintf ( " %u " , ip2long ( '192.168.255.255' ))) return 2 ;
if ( sprintf ( " %u " , ip2long ( '169.254.0.0' )) <= sprintf ( " %u " , ip2long ( $ip )) and sprintf ( " %u " , ip2long ( $ip )) <= sprintf ( " %u " , ip2long ( '169.254.255.255' ))) return 2 ;
return 1 ;
}