2008-06-20 23:10:24 +02:00
< ? php
2014-10-11 23:44:50 +02:00
/* Copyright ( C ) 2008 - 2014 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-12-30 15:11:07 +01:00
* Copyright ( C ) 2008 - 2012 Regis Houssin < regis . houssin @ capnetworks . com >
2011-10-27 10:15:49 +02:00
* Copyright ( C ) 2008 Raphael Bertrand ( Resultic ) < raphael . bertrand @ resultic . fr >
2014-07-17 21:12:50 +02:00
* Copyright ( C ) 2014 Marcos García < marcosgdf @ gmail . com >
2015-05-04 09:22:55 +02:00
* Copyright ( C ) 2015 Ferran Marcet < fmarcet @ 2 byte . es >
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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2008-06-20 23:10:24 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2011-08-01 01:24:38 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2008-06-20 23:10:24 +02:00
* or see http :// www . gnu . org /
*/
/**
2011-10-24 10:45:06 +02:00
* \file htdocs / core / 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-06-20 23:10:24 +02:00
*/
2013-06-16 23:15:20 +02:00
// Enable this line to trace path when function is called.
//print xdebug_print_function_stack('Functions2.lib was called');exit;
2015-05-19 00:44:05 +02:00
/**
* Return first line of text . Cut will depends if content is HTML or not .
*
* @ param string $text Input text
* @ return string Output text
* @ see dol_nboflines_bis
*/
function dolGetFirstLineOfText ( $text )
{
if ( dol_textishtml ( $text ))
{
$firstline = preg_replace ( '/<br[^>]*>.*$/s' , '' , $text ); // The s pattern modifier means the . can match newline characters
}
else
{
$firstline = preg_replace ( '/[\n\r].*/' , '' , $text );
}
return $firstline . (( strlen ( $firstline ) != strlen ( $text )) ? '...' : '' );
}
2013-06-16 23:15:20 +02:00
2012-06-24 19:32:19 +02:00
/**
* Same function than javascript unescape () function but in PHP .
*
2012-07-25 11:37:43 +02:00
* @ param string $source String to decode
* @ return string Unescaped string
2012-06-24 19:32:19 +02:00
*/
function jsUnEscape ( $source )
{
$decodedStr = " " ;
$pos = 0 ;
2012-07-25 11:37:43 +02:00
$len = strlen ( $source );
2012-06-24 19:32:19 +02:00
while ( $pos < $len ) {
2012-07-25 11:37:43 +02:00
$charAt = substr ( $source , $pos , 1 );
2012-06-24 19:32:19 +02:00
if ( $charAt == '%' ) {
$pos ++ ;
2012-07-25 11:37:43 +02:00
$charAt = substr ( $source , $pos , 1 );
2012-06-24 19:32:19 +02:00
if ( $charAt == 'u' ) {
// we got a unicode character
$pos ++ ;
2012-07-25 11:37:43 +02:00
$unicodeHexVal = substr ( $source , $pos , 4 );
$unicode = hexdec ( $unicodeHexVal );
2012-06-24 19:32:19 +02:00
$entity = " &# " . $unicode . ';' ;
2012-07-25 11:37:43 +02:00
$decodedStr .= utf8_encode ( $entity );
2012-06-24 19:32:19 +02:00
$pos += 4 ;
}
else {
// we have an escaped ascii character
2012-07-25 11:37:43 +02:00
$hexVal = substr ( $source , $pos , 2 );
$decodedStr .= chr ( hexdec ( $hexVal ));
2012-06-24 19:32:19 +02:00
$pos += 2 ;
}
} else {
$decodedStr .= $charAt ;
$pos ++ ;
}
}
return dol_html_entity_decode ( $decodedStr , ENT_COMPAT );
}
2008-06-20 23:10:24 +02:00
2012-05-08 22:49:03 +02:00
/**
* Return list of modules directories
*
2012-05-09 16:40:54 +02:00
* @ param string $subdir Sub directory ( Example : '/mailings' )
* @ return array Array of directories that can contains module descriptors
2012-05-08 22:49:03 +02:00
*/
2012-05-09 16:40:54 +02:00
function dolGetModulesDirs ( $subdir = '' )
2012-05-08 22:49:03 +02:00
{
global $conf ;
$modulesdir = array ();
foreach ( $conf -> file -> dol_document_root as $type => $dirroot )
{
// Default core/modules dir
2012-05-09 16:40:54 +02:00
$modulesdir [ $dirroot . '/core/modules' . $subdir . '/' ] = $dirroot . '/core/modules' . $subdir . '/' ;
2012-05-08 22:49:03 +02:00
// Scan dir from external modules
$handle =@ opendir ( $dirroot );
if ( is_resource ( $handle ))
{
while (( $file = readdir ( $handle )) !== false )
{
if ( is_dir ( $dirroot . '/' . $file ) && substr ( $file , 0 , 1 ) <> '.' && substr ( $file , 0 , 3 ) <> 'CVS' && $file != 'includes' )
{
2012-05-09 16:40:54 +02:00
if ( is_dir ( $dirroot . '/' . $file . '/core/modules' . $subdir . '/' ))
2012-05-08 22:49:03 +02:00
{
2012-05-09 16:40:54 +02:00
$modulesdir [ $dirroot . '/' . $file . '/core/modules' . $subdir . '/' ] = $dirroot . '/' . $file . '/core/modules' . $subdir . '/' ;
2012-05-08 22:49:03 +02:00
}
}
}
closedir ( $handle );
}
}
return $modulesdir ;
}
2011-08-11 03:34:54 +02:00
/**
2011-08-31 14:24:47 +02:00
* Try to guess default paper format according to language into $langs
*
2013-04-29 09:25:42 +02:00
* @ param Translate $outputlangs Output lang to use to autodetect output format if setup not done
* @ return string Default paper format code
2011-08-11 03:34:54 +02:00
*/
2013-04-29 09:25:42 +02:00
function dol_getDefaultFormat ( $outputlangs = '' )
2011-08-11 03:34:54 +02:00
{
global $langs ;
2013-04-29 09:25:42 +02:00
2011-08-11 03:34:54 +02:00
$selected = 'EUA4' ;
2013-04-29 09:25:42 +02:00
if ( empty ( $outputlangs ) || ! is_object ( $outputlangs )) $outputlangs = $langs ;
if ( $outputlangs -> defaultlang == 'ca_CA' ) $selected = 'CAP4' ; // Canada
if ( $outputlangs -> defaultlang == 'en_US' ) $selected = 'USLetter' ; // US
2011-08-11 03:34:54 +02:00
return $selected ;
}
2009-02-20 21:28:16 +01:00
/**
2010-09-29 10:09:17 +02:00
* Output content of a file $filename in version of current language ( otherwise may use an alternate language )
2011-08-31 14:24:47 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param Translate $langs Object language to use for output
* @ param string $filename Relative filename to output
* @ param int $searchalt 1 = Search also in alternative languages
* @ return boolean true if OK , false if KO
2009-02-20 21:28:16 +01:00
*/
function dol_print_file ( $langs , $filename , $searchalt = 0 )
{
2010-12-14 00:50:51 +01:00
global $conf ;
// Test if file is in lang directory
foreach ( $langs -> dir as $searchdir )
{
2011-11-08 10:18:45 +01:00
$formfile = ( $searchdir . " /langs/ " . $langs -> defaultlang . " / " . $filename );
dol_syslog ( 'functions2::dol_print_file search file ' . $formfile , LOG_DEBUG );
if ( is_readable ( $formfile ))
2010-12-14 00:50:51 +01:00
{
2011-11-08 10:18:45 +01:00
$content = file_get_contents ( $formfile );
2010-12-14 00:50:51 +01:00
$isutf8 = utf8_check ( $content );
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 );
else print $content ;
return true ;
}
else dol_syslog ( 'functions2::dol_print_file not found' , LOG_DEBUG );
if ( $searchalt ) {
// Test si fichier dans repertoire de la langue alternative
2011-11-08 10:18:45 +01:00
if ( $langs -> defaultlang != " en_US " ) $formfilealt = $searchdir . " /langs/en_US/ " . $filename ;
else $formfilealt = $searchdir . " /langs/fr_FR/ " . $filename ;
dol_syslog ( 'functions2::dol_print_file search alt file ' . $formfilealt , LOG_DEBUG );
//print 'getcwd='.getcwd().' htmlfilealt='.$formfilealt.' X '.file_exists(getcwd().'/'.$formfilealt);
if ( is_readable ( $formfilealt ))
2010-12-14 00:50:51 +01:00
{
2011-11-08 10:18:45 +01:00
$content = file_get_contents ( $formfilealt );
2010-12-14 00:50:51 +01:00
$isutf8 = utf8_check ( $content );
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 );
else print $content ;
return true ;
}
else dol_syslog ( 'functions2::dol_print_file not found' , LOG_DEBUG );
}
}
return false ;
2009-02-20 21:28:16 +01:00
}
2009-02-19 20:06:12 +01:00
/**
2011-08-31 14:24:47 +02:00
* Show informations on an object
2012-05-12 16:27:20 +02:00
* TODO Move this into html . formother
2011-08-31 14:24:47 +02:00
*
2013-11-07 21:23:29 +01:00
* @ param object $object Objet to show
2012-02-04 14:39:47 +01:00
* @ return void
2009-02-19 20:06:12 +01:00
*/
function dol_print_object_info ( $object )
{
2010-12-14 00:50:51 +01:00
global $langs , $db ;
$langs -> load ( " other " );
2012-05-12 18:30:49 +02:00
$langs -> load ( " admin " );
2010-12-14 00:50:51 +01:00
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
2012-05-14 16:46:48 +02:00
$deltadateforserver = getServerTimeZoneInt ( 'now' );
$deltadateforclient = (( int ) $_SESSION [ 'dol_tz' ] + ( int ) $_SESSION [ 'dol_dst' ]);
2012-05-12 16:27:20 +02:00
//$deltadateforcompany=((int) $_SESSION['dol_tz'] + (int) $_SESSION['dol_dst']);
2012-05-14 16:46:48 +02:00
$deltadateforuser = round ( $deltadateforclient - $deltadateforserver );
2012-07-02 19:30:37 +02:00
//print "x".$deltadateforserver." - ".$deltadateforclient." - ".$deltadateforuser;
2012-05-12 16:27:20 +02:00
2010-12-14 00:50:51 +01:00
// Import key
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> import_key ))
2012-07-28 20:20:56 +02:00
print $langs -> trans ( " ImportedWithSet " ) . ': ' . $object -> import_key . '<br>' ;
2010-12-14 00:50:51 +01:00
// User creation
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> user_creation ))
2010-12-14 00:50:51 +01:00
{
2012-07-28 20:16:54 +02:00
print $langs -> trans ( " CreatedBy " ) . ': ' ;
2010-12-14 00:50:51 +01:00
if ( is_object ( $object -> user_creation ))
{
2013-11-05 14:24:37 +01:00
if ( $object -> user_creation -> id ) print $object -> user_creation -> getNomUrl ( 1 );
else print $langs -> trans ( " Unknown " );
2010-12-14 00:50:51 +01:00
}
else
{
$userstatic = new User ( $db );
$userstatic -> fetch ( $object -> user_creation );
2013-11-05 14:24:37 +01:00
if ( $userstatic -> id ) print $userstatic -> getNomUrl ( 1 );
else print $langs -> trans ( " Unknown " );
2010-12-14 00:50:51 +01:00
}
print '<br>' ;
}
2012-04-16 11:43:19 +02:00
// Date creation
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> date_creation ))
2012-05-12 16:27:20 +02:00
{
2012-07-28 20:16:54 +02:00
print $langs -> trans ( " DateCreation " ) . ': ' . dol_print_date ( $object -> date_creation , 'dayhour' );
2012-05-14 16:46:48 +02:00
if ( $deltadateforuser ) print ' ' . $langs -> trans ( " CurrentHour " ) . ' / ' . dol_print_date ( $object -> date_creation + ( $deltadateforuser * 3600 ), " dayhour " ) . ' ' . $langs -> trans ( " ClientHour " );
2012-05-12 16:27:20 +02:00
print '<br>' ;
}
2010-12-14 00:50:51 +01:00
// User change
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> user_modification ))
2010-12-14 00:50:51 +01:00
{
2012-07-28 20:16:54 +02:00
print $langs -> trans ( " ModifiedBy " ) . ': ' ;
2010-12-14 00:50:51 +01:00
if ( is_object ( $object -> user_modification ))
{
2013-11-05 14:24:37 +01:00
if ( $object -> user_modification -> id ) print $object -> user_modification -> getNomUrl ( 1 );
else print $langs -> trans ( " Unknown " );
2010-12-14 00:50:51 +01:00
}
else
{
$userstatic = new User ( $db );
$userstatic -> fetch ( $object -> user_modification );
2013-11-05 14:24:37 +01:00
if ( $userstatic -> id ) print $userstatic -> getNomUrl ( 1 );
else print $langs -> trans ( " Unknown " );
2010-12-14 00:50:51 +01:00
}
print '<br>' ;
}
2012-04-16 11:43:19 +02:00
// Date change
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> date_modification ))
2012-05-12 16:27:20 +02:00
{
2012-07-28 20:16:54 +02:00
print $langs -> trans ( " DateLastModification " ) . ': ' . dol_print_date ( $object -> date_modification , 'dayhour' );
2012-05-14 16:46:48 +02:00
if ( $deltadateforuser ) print ' ' . $langs -> trans ( " CurrentHour " ) . ' / ' . dol_print_date ( $object -> date_modification + ( $deltadateforuser * 3600 ), " dayhour " ) . ' ' . $langs -> trans ( " ClientHour " );
2012-05-12 16:27:20 +02:00
print '<br>' ;
}
2010-12-14 00:50:51 +01:00
// User validation
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> user_validation ))
2010-12-14 00:50:51 +01:00
{
2012-07-28 20:16:54 +02:00
print $langs -> trans ( " ValidatedBy " ) . ': ' ;
2010-12-14 00:50:51 +01:00
if ( is_object ( $object -> user_validation ))
{
2013-11-05 14:24:37 +01:00
if ( $object -> user_validation -> id ) print $object -> user_validation -> getNomUrl ( 1 );
else print $langs -> trans ( " Unknown " );
2010-12-14 00:50:51 +01:00
}
else
{
$userstatic = new User ( $db );
$userstatic -> fetch ( $object -> user_validation );
2013-11-05 14:24:37 +01:00
if ( $userstatic -> id ) print $userstatic -> getNomUrl ( 1 );
else print $langs -> trans ( " Unknown " );
2010-12-14 00:50:51 +01:00
}
print '<br>' ;
}
2012-04-16 11:43:19 +02:00
// Date validation
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> date_validation ))
2012-05-12 16:27:20 +02:00
{
2012-07-28 20:16:54 +02:00
print $langs -> trans ( " DateValidation " ) . ': ' . dol_print_date ( $object -> date_validation , 'dayhour' );
2012-05-14 16:46:48 +02:00
if ( $deltadateforuser ) print ' ' . $langs -> trans ( " CurrentHour " ) . ' / ' . dol_print_date ( $object -> date_validation + ( $deltadateforuser * 3600 ), " dayhour " ) . ' ' . $langs -> trans ( " ClientHour " );
2012-05-12 16:27:20 +02:00
print '<br>' ;
}
2010-12-14 00:50:51 +01:00
2012-04-16 11:43:19 +02:00
// User approve
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> user_approve ))
2012-04-16 11:43:19 +02:00
{
2012-07-28 20:16:54 +02:00
print $langs -> trans ( " ApprovedBy " ) . ': ' ;
2012-04-16 11:43:19 +02:00
if ( is_object ( $object -> user_approve ))
{
2013-11-05 14:24:37 +01:00
if ( $object -> user_approve -> id ) print $object -> user_approve -> getNomUrl ( 1 );
else print $langs -> trans ( " Unknown " );
2012-04-16 11:43:19 +02:00
}
else
{
$userstatic = new User ( $db );
$userstatic -> fetch ( $object -> user_approve );
2013-11-05 14:24:37 +01:00
if ( $userstatic -> id ) print $userstatic -> getNomUrl ( 1 );
else print $langs -> trans ( " Unknown " );
2012-04-16 11:43:19 +02:00
}
print '<br>' ;
}
// Date approve
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> date_approve ))
2012-05-12 16:27:20 +02:00
{
2012-07-28 20:16:54 +02:00
print $langs -> trans ( " DateApprove " ) . ': ' . dol_print_date ( $object -> date_approve , 'dayhour' );
2012-05-14 16:46:48 +02:00
if ( $deltadateforuser ) print ' ' . $langs -> trans ( " CurrentHour " ) . ' / ' . dol_print_date ( $object -> date_approve + ( $deltadateforuser * 3600 ), " dayhour " ) . ' ' . $langs -> trans ( " ClientHour " );
2012-05-12 16:27:20 +02:00
print '<br>' ;
}
2012-04-16 11:43:19 +02:00
2010-12-14 00:50:51 +01:00
// User close
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> user_cloture ))
2010-12-14 00:50:51 +01:00
{
2012-07-28 20:16:54 +02:00
print $langs -> trans ( " ClosedBy " ) . ': ' ;
2010-12-14 00:50:51 +01:00
if ( is_object ( $object -> user_cloture ))
{
2013-11-05 14:24:37 +01:00
if ( $object -> user_cloture -> id ) print $object -> user_cloture -> getNomUrl ( 1 );
else print $langs -> trans ( " Unknown " );
2010-12-14 00:50:51 +01:00
}
else
{
$userstatic = new User ( $db );
$userstatic -> fetch ( $object -> user_cloture );
2013-11-05 14:24:37 +01:00
if ( $userstatic -> id ) print $userstatic -> getNomUrl ( 1 );
else print $langs -> trans ( " Unknown " );
2010-12-14 00:50:51 +01:00
}
print '<br>' ;
}
2012-04-16 11:43:19 +02:00
// Date close
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> date_cloture ))
2012-05-12 16:27:20 +02:00
{
2012-07-28 20:16:54 +02:00
print $langs -> trans ( " DateClosing " ) . ': ' . dol_print_date ( $object -> date_cloture , 'dayhour' );
2012-05-14 16:46:48 +02:00
if ( $deltadateforuser ) print ' ' . $langs -> trans ( " CurrentHour " ) . ' / ' . dol_print_date ( $object -> date_cloture + ( $deltadateforuser * 3600 ), " dayhour " ) . ' ' . $langs -> trans ( " ClientHour " );
2012-05-12 16:27:20 +02:00
print '<br>' ;
}
2010-12-14 00:50:51 +01:00
// User conciliate
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> user_rappro ))
2010-12-14 00:50:51 +01:00
{
2012-07-28 20:16:54 +02:00
print $langs -> trans ( " ConciliatedBy " ) . ': ' ;
2010-12-14 00:50:51 +01:00
if ( is_object ( $object -> user_rappro ))
{
2013-11-05 14:24:37 +01:00
if ( $object -> user_rappro -> id ) print $object -> user_rappro -> getNomUrl ( 1 );
else print $langs -> trans ( " Unknown " );
2010-12-14 00:50:51 +01:00
}
else
{
$userstatic = new User ( $db );
$userstatic -> fetch ( $object -> user_rappro );
2013-11-05 14:24:37 +01:00
if ( $userstatic -> id ) print $userstatic -> getNomUrl ( 1 );
else print $langs -> trans ( " Unknown " );
2010-12-14 00:50:51 +01:00
}
print '<br>' ;
}
2012-04-16 11:43:19 +02:00
// Date conciliate
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> date_rappro ))
2012-05-12 16:27:20 +02:00
{
2012-07-28 20:16:54 +02:00
print $langs -> trans ( " DateConciliating " ) . ': ' . dol_print_date ( $object -> date_rappro , 'dayhour' );
2012-05-14 16:46:48 +02:00
if ( $deltadateforuser ) print ' ' . $langs -> trans ( " CurrentHour " ) . ' / ' . dol_print_date ( $object -> date_rappro + ( $deltadateforuser * 3600 ), " dayhour " ) . ' ' . $langs -> trans ( " ClientHour " );
2012-05-12 16:27:20 +02:00
print '<br>' ;
}
2010-12-14 00:50:51 +01:00
2012-04-16 11:43:19 +02:00
// Date send
2013-04-12 14:46:48 +02:00
if ( ! empty ( $object -> date_envoi ))
2012-05-12 16:27:20 +02:00
{
2012-07-28 20:16:54 +02:00
print $langs -> trans ( " DateLastSend " ) . ': ' . dol_print_date ( $object -> date_envoi , 'dayhour' );
2012-05-14 16:46:48 +02:00
if ( $deltadateforuser ) print ' ' . $langs -> trans ( " CurrentHour " ) . ' / ' . dol_print_date ( $object -> date_envoi + ( $deltadateforuser * 3600 ), " dayhour " ) . ' ' . $langs -> trans ( " ClientHour " );
2012-05-12 16:27:20 +02:00
print '<br>' ;
}
2009-02-19 20:06:12 +01:00
}
2015-03-03 18:33:53 +01:00
/**
* Return an email formatted to include a tracking id
* For example myemail @ mydomain . com becom myemail + trackingid @ mydomain . com
*
* @ param string $email Email address ( Ex : " toto@titi.com " , " John Do <johndo@titi.com> " )
* @ param string $trackingid Tracking id ( Ex : thi123 for thirdparty with id 123 )
* @ return boolean True if domain email is OK , False if KO
*/
function dolAddEmailTrackId ( $email , $trackingid )
{
$tmp = explode ( '@' , $email );
return $tmp [ 0 ] . '+' . $trackingid . '@' . ( isset ( $tmp [ 1 ]) ? $tmp [ 1 ] : '' );
}
2009-02-19 19:57:54 +01:00
/**
2011-08-31 14:24:47 +02:00
* Return true if email has a domain name that can ' t be resolved
*
2012-02-04 14:39:47 +01:00
* @ param string $mail Email address ( Ex : " toto@titi.com " , " John Do <johndo@titi.com> " )
* @ 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
{
2010-12-14 00:50:51 +01:00
list ( $user , $domain ) = explode ( " @ " , $mail , 2 );
if ( checkdnsrr ( $domain , " MX " ))
{
return true ;
}
else
{
return false ;
}
2009-02-19 19:57:54 +01:00
}
2009-04-06 10:34:42 +02:00
/**
2011-08-31 14:24:47 +02:00
* Url string validation
* < http [ s ] > :// [ user [ : pass ] @ ] hostname [ port ] [ / path ] [ ? getquery ] [ anchor ]
*
2012-02-04 14:39:47 +01:00
* @ param string $url Url
2014-08-01 01:31:52 +02:00
* @ param int $http 1 : verify http is provided , 0 : not verify http
* @ param int $pass 1 : verify user and pass is provided , 0 : not verify user and pass
* @ param int $port 1 : verify port is provided , 0 : not verify port
* @ param int $path 1 : verify a path is provided " / " or " /... " or " /.../ " , 0 : not verify path
* @ param int $query 1 : verify query is provided , 0 : not verify query
* @ param int $anchor 1 : verify anchor is provided , 0 : not verify anchor
2012-02-04 14:39:47 +01:00
* @ return int 1 = Check is OK , 0 = Check is KO
2009-04-06 10:34:42 +02:00
*/
function isValidUrl ( $url , $http = 0 , $pass = 0 , $port = 0 , $path = 0 , $query = 0 , $anchor = 0 )
{
2010-12-14 00:50:51 +01:00
$ValidUrl = 0 ;
$urlregex = '' ;
// SCHEME
if ( $http ) $urlregex .= " ^(http: \ / \ /|https: \ / \ /) " ;
// USER AND PASS
if ( $pass ) $urlregex .= " ([a-z0-9+!*(),;?&= \$ _.-]+( \ :[a-z0-9+!*(),;?&= \$ _.-]+)?@) " ;
// HOSTNAME OR IP
2014-08-01 01:31:52 +02:00
//$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*"; // x allowed (ex. http://localhost, http://routerlogin)
//$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)+"; // x.x
$urlregex .= " ([a-z0-9+ \$ _ \\ \ :-])+( \ .[a-z0-9+ \$ _-][a-z0-9+ \$ _-]+)* " ; // x ou x.xx (2 x ou plus)
2010-12-14 00:50:51 +01:00
//use only one of the above
// PORT
if ( $port ) $urlregex .= " ( \ :[0-9] { 2,5}) " ;
// PATH
if ( $path ) $urlregex .= " ( \ /([a-z0-9+ \$ _-] \ .?)+)* \ / " ;
// GET Query
if ( $query ) $urlregex .= " ( \ ?[a-z+& \$ _.-][a-z0-9;:@ \ /&%=+ \$ _.-]*) " ;
// ANCHOR
if ( $anchor ) $urlregex .= " (#[a-z_.-][a-z0-9+ \$ _.-]*) $ " ;
// check
if ( preg_match ( '/' . $urlregex . '/i' , $url ))
{
$ValidUrl = 1 ;
}
2014-08-02 17:27:45 +02:00
//print $urlregex.' - '.$url.' - '.$ValidUrl;
2010-12-14 00:50:51 +01:00
return $ValidUrl ;
2009-04-06 10:34:42 +02:00
}
/**
2011-08-31 14:24:47 +02:00
* Clean an url string
*
2012-02-04 14:39:47 +01:00
* @ param string $url Url
2013-11-15 11:43:16 +01:00
* @ param string $http 1 = keep both http :// and https :// , 0 : remove http :// but not https ://
2012-02-04 14:39:47 +01:00
* @ return string Cleaned url
2009-04-06 10:34:42 +02:00
*/
function clean_url ( $url , $http = 1 )
{
2010-12-14 00:50:51 +01:00
// 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
if ( preg_match ( '/^(https?:[\\/]+)?([0-9A-Z.-]+\.[A-Z]{2,4})(:[0-9]+)?/i' , $url , $regs ))
{
$proto = $regs [ 1 ];
$domain = $regs [ 2 ];
2014-05-01 19:17:45 +02:00
$port = isset ( $regs [ 3 ]) ? $regs [ 3 ] : '' ;
2010-12-14 00:50:51 +01:00
//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 )
{
if ( preg_match ( '/^http:[\\/]+/i' , $url ))
{
$url = preg_replace ( '/^http:[\\/]+/i' , '' , $url );
$newproto = '' ;
}
}
// On passe le nom de domaine en minuscule
$CleanUrl = preg_replace ( '/^' . preg_quote ( $proto . $domain , '/' ) . '/i' , $newproto . strtolower ( $domain ), $url );
return $CleanUrl ;
}
2012-06-15 11:24:09 +02:00
else return $url ;
2009-04-06 10:34:42 +02:00
}
2014-10-11 23:44:50 +02:00
2014-10-11 23:46:49 +02:00
/**
* Returns an email value with obfuscated parts .
*
* @ param string $mail Email
* @ param string $replace Replacement character ( defaul : * )
* @ param int $nbreplace Number of replacement character ( default : 8 )
* @ param int $nbdisplaymail Number of character unchanged ( default : 4 )
* @ param int $nbdisplaydomain Number of character unchanged of domain ( default : 3 )
* @ param bool $displaytld Display tld ( default : true )
* @ return string Return email with hidden parts or '' ;
*/
function dolObfuscateEmail ( $mail , $replace = " * " , $nbreplace = 8 , $nbdisplaymail = 4 , $nbdisplaydomain = 3 , $displaytld = true )
{
if ( ! isValidEmail ( $mail )) return '' ;
$tab = explode ( '@' , $mail );
$tab2 = explode ( '.' , $tab [ 1 ]);
$string_replace = '' ;
$mail_name = $tab [ 0 ];
$mail_domaine = $tab2 [ 0 ];
$mail_tld = '' ;
for ( $i = 1 ; $i < count ( $tab2 ) && $displaytld ; $i ++ )
{
$mail_tld .= '.' . $tab2 [ $i ];
}
for ( $i = 0 ; $i < $nbreplace ; $i ++ ){
$string_replace .= $replace ;
}
if ( strlen ( $mail_name ) > $nbdisplaymail ){
$mail_name = substr ( $mail_name , 0 , $nbdisplaymail );
}
if ( strlen ( $mail_domaine ) > $nbdisplaydomain ){
$mail_domaine = substr ( $mail_domaine , strlen ( $mail_domaine ) - $nbdisplaydomain );
}
return $mail_name . $string_replace . $mail_domaine . $mail_tld ;
}
2014-10-11 23:44:50 +02:00
2008-11-16 02:09:04 +01:00
/**
2011-08-31 14:24:47 +02:00
* Return lines of an html table from an array
* Used by array2table function only
2012-02-04 14:39:47 +01:00
*
* @ param array $data Array of data
* @ param string $troptions Options for tr
* @ param string $tdoptions Options for td
* @ return string
2008-11-16 02:09:04 +01:00
*/
2012-02-04 14:39:47 +01:00
function array2tr ( $data , $troptions = '' , $tdoptions = '' )
{
2010-12-14 00:50:51 +01:00
$text = '<tr ' . $troptions . '>' ;
foreach ( $data as $key => $item ){
$text .= '<td ' . $tdoptions . '>' . $item . '</td>' ;
}
$text .= '</tr>' ;
return $text ;
2008-11-16 02:09:04 +01:00
}
/**
2011-08-31 14:24:47 +02:00
* Return an html table from an array
2012-02-04 14:39:47 +01:00
*
* @ param array $data Array of data
* @ param int $tableMarkup Table markup
2012-02-15 13:55:00 +01:00
* @ param string $tableoptions Options for table
2012-02-04 14:39:47 +01:00
* @ param string $troptions Options for tr
* @ param string $tdoptions Options for td
* @ return string
2008-11-16 02:09:04 +01:00
*/
2012-02-04 14:39:47 +01:00
function array2table ( $data , $tableMarkup = 1 , $tableoptions = '' , $troptions = '' , $tdoptions = '' )
{
2010-12-14 00:50:51 +01:00
$text = '' ;
if ( $tableMarkup ) $text = '<table ' . $tableoptions . '>' ;
foreach ( $data as $key => $item ){
if ( is_array ( $item )){
2011-09-20 15:32:16 +02:00
$text .= array2tr ( $item , $troptions , $tdoptions );
2010-12-14 00:50:51 +01:00
} else {
$text .= '<tr ' . $troptions . '>' ;
$text .= '<td ' . $tdoptions . '>' . $key . '</td>' ;
$text .= '<td ' . $tdoptions . '>' . $item . '</td>' ;
$text .= '</tr>' ;
}
}
if ( $tableMarkup ) $text .= '</table>' ;
return $text ;
2008-11-16 02:09:04 +01:00
}
2008-06-20 23:10:24 +02:00
/**
2013-04-16 15:00:41 +02:00
* Return last or next value for a mask ( according to area we should not reset )
2008-06-20 23:10:24 +02:00
*
2014-12-26 04:44:15 +01:00
* @ param DoliDB $db Database handler
2012-02-04 14:39:47 +01:00
* @ param string $mask Mask to use
* @ param string $table Table containing field with counter
* @ param string $field Field containing already used values of counter
* @ param string $where To add a filter on selection ( for exemple to filter on invoice types )
* @ param Societe $objsoc The company that own the object we need a counter for
* @ param string $date Date to use for the { y },{ m },{ d } tags .
2014-10-17 10:26:34 +02:00
* @ param string $mode 'next' for next value or 'last' for last value
2014-12-26 04:44:15 +01:00
* @ param bool $bentityon Activate the entity filter . Default is true ( for modules not compatible with multicompany )
* @ return string New value ( numeric ) or error message
2008-06-20 23:10:24 +02:00
*/
2014-10-17 10:26:34 +02:00
function get_next_value ( $db , $mask , $table , $field , $where = '' , $objsoc = '' , $date = '' , $mode = 'next' , $bentityon = true )
2008-06-20 23:10:24 +02:00
{
2010-12-14 00:50:51 +01:00
global $conf ;
if ( ! is_object ( $objsoc )) $valueforccc = $objsoc ;
2015-05-04 09:22:55 +02:00
else if ( $table == " commande_fournisseur " || $table == " facture_fourn " ) $valueforccc = $objsoc -> code_fournisseur ;
2010-12-14 00:50:51 +01:00
else $valueforccc = $objsoc -> code_client ;
// Clean parameters
2012-04-30 15:01:25 +02:00
if ( $date == '' ) $date = dol_now (); // We use local year and month of PHP server to search numbers
2010-12-14 00:50:51 +01:00
// but we should use local year and month of user
2012-09-21 11:23:31 +02:00
// For debugging
//include_once(DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php');
2014-01-21 13:48:47 +01:00
//$mask='FA{yy}{mm}-{0000@99}';
2012-09-24 09:30:37 +02:00
//$date=dol_mktime(12, 0, 0, 1, 1, 1900);
2014-01-21 13:48:47 +01:00
//$date=dol_stringtotime('20130101');
2012-09-21 11:23:31 +02:00
2015-05-25 14:56:37 +02:00
$hasglobalcounter = false ;
2010-12-14 00:50:51 +01:00
// Extract value for mask counter, mask raz and mask offset
2015-05-25 14:56:37 +02:00
if ( preg_match ( '/\{(0+)([@\+][0-9\-\+\=]+)?([@\+][0-9\-\+\=]+)?\}/i' , $mask , $reg ))
{
$masktri = $reg [ 1 ] . ( ! empty ( $reg [ 2 ]) ? $reg [ 2 ] : '' ) . ( ! empty ( $reg [ 3 ]) ? $reg [ 3 ] : '' );
$maskcounter = $reg [ 1 ];
$hasglobalcounter = true ;
}
else
{
// setting some defaults so the rest of the code won't fail if there is a third party counter
$masktri = '00000' ;
$maskcounter = '00000' ;
}
2010-12-14 00:50:51 +01:00
$maskraz =- 1 ;
$maskoffset = 0 ;
2013-09-11 14:18:56 +02:00
$resetEveryMonth = false ;
2015-01-28 00:02:57 +01:00
if ( dol_strlen ( $maskcounter ) < 3 && empty ( $conf -> global -> MAIN_COUNTER_WITH_LESS_3_DIGITS )) return 'ErrorCounterMustHaveMoreThan3Digits' ;
2010-12-14 00:50:51 +01:00
// Extract value for third party mask counter
if ( preg_match ( '/\{(c+)(0*)\}/i' , $mask , $regClientRef ))
{
$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 , dol_strlen ( $maskrefclient_maskclientcode )); //get n first characters of client code where n is length in mask
$maskrefclient_clientcode = str_pad ( $maskrefclient_clientcode , dol_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
2015-01-11 11:31:53 +01:00
if ( dol_strlen ( $maskrefclient_maskcounter ) > 0 && dol_strlen ( $maskrefclient_maskcounter ) < 3 ) return 'ErrorCounterMustHaveMoreThan3Digits' ;
2010-12-14 00:50:51 +01:00
}
else $maskrefclient = '' ;
2015-05-25 14:56:37 +02:00
// fail if there is neither a global nor a third party counter
if ( ! $hasglobalcounter && ( $maskrefclient_maskcounter == '' ))
{
return 'ErrorBadMask' ;
}
2010-12-14 00:50:51 +01:00
// Extract value for third party type
if ( preg_match ( '/\{(t+)\}/i' , $mask , $regType ))
{
$masktype = $regType [ 1 ];
2015-01-13 22:13:15 +01:00
$masktype_value = substr ( preg_replace ( '/^TE_/' , '' , $objsoc -> typent_code ), 0 , dol_strlen ( $regType [ 1 ])); // get n first characters of thirdpaty typent_code (where n is length in mask)
$masktype_value = str_pad ( $masktype_value , dol_strlen ( $regType [ 1 ]), " # " , STR_PAD_RIGHT ); // we fill on right with # to have same number of char than into mask
2010-12-14 00:50:51 +01:00
}
2012-07-10 18:57:27 +02:00
else
{
$masktype = '' ;
$masktype_value = '' ;
}
2010-12-14 00:50:51 +01:00
$maskwithonlyymcode = $mask ;
2012-09-21 11:23:31 +02:00
$maskwithonlyymcode = preg_replace ( '/\{(0+)([@\+][0-9\-\+\=]+)?([@\+][0-9\-\+\=]+)?\}/i' , $maskcounter , $maskwithonlyymcode );
2010-12-14 00:50:51 +01:00
$maskwithonlyymcode = preg_replace ( '/\{dd\}/i' , 'dd' , $maskwithonlyymcode );
$maskwithonlyymcode = preg_replace ( '/\{(c+)(0*)\}/i' , $maskrefclient , $maskwithonlyymcode );
$maskwithonlyymcode = preg_replace ( '/\{(t+)\}/i' , $masktype_value , $maskwithonlyymcode );
$maskwithnocode = $maskwithonlyymcode ;
$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 );
// Now maskwithnocode = 0000ddmmyyyyccc for example
// and maskcounter = 0000 for example
//print "maskwithonlyymcode=".$maskwithonlyymcode." maskwithnocode=".$maskwithnocode."\n<br>";
2015-01-11 11:31:53 +01:00
//var_dump($reg);
2010-12-14 00:50:51 +01:00
// If an offset is asked
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 ]);
// Define $sqlwhere
2012-07-28 12:35:11 +02:00
$sqlwhere = '' ;
2012-09-23 16:25:08 +02:00
$yearoffset = 0 ; // Use year of current $date by default
$yearoffsettype = false ; // false: no reset, 0,-,=,+: reset at offset SOCIETE_FISCAL_MONTH_START, x=reset at offset x
2010-12-14 00:50:51 +01:00
// If a restore to zero after a month is asked we check if there is already a value for this year.
2012-09-21 11:23:31 +02:00
if ( ! empty ( $reg [ 2 ]) && preg_match ( '/^@/' , $reg [ 2 ])) $yearoffsettype = preg_replace ( '/^@/' , '' , $reg [ 2 ]);
if ( ! empty ( $reg [ 3 ]) && preg_match ( '/^@/' , $reg [ 3 ])) $yearoffsettype = preg_replace ( '/^@/' , '' , $reg [ 3 ]);
2012-09-23 16:25:08 +02:00
//print "yearoffset=".$yearoffset." yearoffsettype=".$yearoffsettype;
if ( is_numeric ( $yearoffsettype ) && $yearoffsettype >= 1 )
2015-01-11 11:31:53 +01:00
$maskraz = $yearoffsettype ; // For backward compatibility
2012-09-21 11:23:31 +02:00
else if ( $yearoffsettype === '0' || ( ! empty ( $yearoffsettype ) && ! is_numeric ( $yearoffsettype ) && $conf -> global -> SOCIETE_FISCAL_MONTH_START > 1 ))
2015-01-11 11:31:53 +01:00
$maskraz = $conf -> global -> SOCIETE_FISCAL_MONTH_START ;
2013-06-06 21:18:50 +02:00
//print "maskraz=".$maskraz; // -1=no reset
2012-09-21 11:23:31 +02:00
2015-01-11 11:31:53 +01:00
if ( $maskraz > 0 ) { // A reset is required
if ( $maskraz == 99 ) {
$maskraz = date ( 'm' , $date );
$resetEveryMonth = true ;
}
2010-12-14 00:50:51 +01:00
if ( $maskraz > 12 ) return 'ErrorBadMaskBadRazMonth' ;
2011-03-18 16:51:25 +01:00
// Define posy, posm and reg
2013-09-11 14:18:56 +02:00
if ( $maskraz > 1 ) // if reset is not first month, we need month and year into mask
2011-03-18 16:51:25 +01:00
{
2011-06-10 22:33:48 +02:00
if ( preg_match ( '/^(.*)\{(y+)\}\{(m+)\}/i' , $maskwithonlyymcode , $reg )) { $posy = 2 ; $posm = 3 ; }
elseif ( preg_match ( '/^(.*)\{(m+)\}\{(y+)\}/i' , $maskwithonlyymcode , $reg )) { $posy = 3 ; $posm = 2 ; }
2013-09-11 14:18:56 +02:00
else return 'ErrorCantUseRazInStartedYearIfNoYearMonthInMask' ;
2011-06-10 22:33:48 +02:00
if ( dol_strlen ( $reg [ $posy ]) < 2 ) return 'ErrorCantUseRazWithYearOnOneDigit' ;
2011-04-03 20:02:18 +02:00
}
2013-09-11 14:18:56 +02:00
else // if reset is for a specific month in year, we need year
2011-03-18 16:51:25 +01:00
{
2013-09-11 14:18:56 +02:00
if ( preg_match ( '/^(.*)\{(m+)\}\{(y+)\}/i' , $maskwithonlyymcode , $reg )) { $posy = 3 ; $posm = 2 ; }
else if ( preg_match ( '/^(.*)\{(y+)\}\{(m+)\}/i' , $maskwithonlyymcode , $reg )) { $posy = 2 ; $posm = 3 ; }
else if ( preg_match ( '/^(.*)\{(y+)\}/i' , $maskwithonlyymcode , $reg )) { $posy = 2 ; $posm = 0 ; }
else return 'ErrorCantUseRazIfNoYearInMask' ;
2011-03-18 16:51:25 +01:00
}
2013-09-11 14:18:56 +02:00
// Define length
$yearlen = $posy ? dol_strlen ( $reg [ $posy ]) : 0 ;
$monthlen = $posm ? dol_strlen ( $reg [ $posm ]) : 0 ;
// Define pos
$yearpos = ( dol_strlen ( $reg [ 1 ]) + 1 );
$monthpos = ( $yearpos + $yearlen );
if ( $posy == 3 && $posm == 2 ) { // if month is before year
$monthpos = ( dol_strlen ( $reg [ 1 ]) + 1 );
$yearpos = ( $monthpos + $monthlen );
}
//print "xxx ".$maskwithonlyymcode." maskraz=".$maskraz." posy=".$posy." yearlen=".$yearlen." yearpos=".$yearpos." posm=".$posm." monthlen=".$monthlen." monthpos=".$monthpos." yearoffsettype=".$yearoffsettype." resetEveryMonth=".$resetEveryMonth."\n";
2011-03-18 16:51:25 +01:00
2010-12-14 00:50:51 +01:00
// Define $yearcomp and $monthcomp (that will be use in the select where to search max number)
$monthcomp = $maskraz ;
$yearcomp = 0 ;
2012-09-21 11:23:31 +02:00
2012-09-23 16:25:08 +02:00
if ( ! empty ( $yearoffsettype ) && ! is_numeric ( $yearoffsettype ) && $yearoffsettype != '=' ) // $yearoffsettype is - or +
2012-09-22 09:03:38 +02:00
{
2012-09-22 08:53:10 +02:00
$currentyear = date ( " Y " , $date );
$fiscaldate = dol_mktime ( '0' , '0' , '0' , $maskraz , '1' , $currentyear );
$newyeardate = dol_mktime ( '0' , '0' , '0' , '1' , '1' , $currentyear );
$nextnewyeardate = dol_mktime ( '0' , '0' , '0' , '1' , '1' , $currentyear + 1 );
//echo 'currentyear='.$currentyear.' date='.dol_print_date($date, 'day').' fiscaldate='.dol_print_date($fiscaldate, 'day').'<br>';
2012-09-22 09:18:36 +02:00
// If after or equal of current fiscal date
if ( $date >= $fiscaldate )
2012-09-22 08:53:10 +02:00
{
2012-09-22 09:18:36 +02:00
// If before of next new year date
if ( $date < $nextnewyeardate && $yearoffsettype == '+' ) $yearoffset = 1 ;
2012-09-22 08:53:10 +02:00
}
2012-09-22 09:18:36 +02:00
// If after or equal of current new year date
else if ( $date >= $newyeardate && $yearoffsettype == '-' ) $yearoffset =- 1 ;
2012-09-21 11:23:31 +02:00
}
2012-09-22 09:03:38 +02:00
// For backward compatibility
2013-05-24 11:23:56 +02:00
else if ( date ( " m " , $date ) < $maskraz && empty ( $resetEveryMonth )) { $yearoffset =- 1 ; } // If current month lower that month of return to zero, year is previous year
2012-09-22 08:53:10 +02:00
2012-09-22 10:40:12 +02:00
if ( $yearlen == 4 ) $yearcomp = sprintf ( " %04d " , date ( " Y " , $date ) + $yearoffset );
elseif ( $yearlen == 2 ) $yearcomp = sprintf ( " %02d " , date ( " y " , $date ) + $yearoffset );
elseif ( $yearlen == 1 ) $yearcomp = substr ( date ( " y " , $date ), 2 , 1 ) + $yearoffset ;
2013-05-24 11:23:56 +02:00
if ( $monthcomp > 1 && empty ( $resetEveryMonth )) // Test with month is useless if monthcomp = 0 or 1 (0 is same as 1) (regis: $monthcomp can't equal 0)
2010-12-14 00:50:51 +01:00
{
2012-09-22 10:40:12 +02:00
if ( $yearlen == 4 ) $yearcomp1 = sprintf ( " %04d " , date ( " Y " , $date ) + $yearoffset + 1 );
elseif ( $yearlen == 2 ) $yearcomp1 = sprintf ( " %02d " , date ( " y " , $date ) + $yearoffset + 1 );
2012-04-15 17:55:45 +02:00
2012-09-22 10:33:25 +02:00
$sqlwhere .= " ( " ;
$sqlwhere .= " (SUBSTRING( " . $field . " , " . $yearpos . " , " . $yearlen . " ) = ' " . $yearcomp . " ' " ;
$sqlwhere .= " AND SUBSTRING( " . $field . " , " . $monthpos . " , " . $monthlen . " ) >= ' " . str_pad ( $monthcomp , $monthlen , '0' , STR_PAD_LEFT ) . " ') " ;
$sqlwhere .= " OR " ;
$sqlwhere .= " (SUBSTRING( " . $field . " , " . $yearpos . " , " . $yearlen . " ) = ' " . $yearcomp1 . " ' " ;
$sqlwhere .= " AND SUBSTRING( " . $field . " , " . $monthpos . " , " . $monthlen . " ) < ' " . str_pad ( $monthcomp , $monthlen , '0' , STR_PAD_LEFT ) . " ') " ;
2012-04-15 17:55:45 +02:00
$sqlwhere .= ')' ;
2010-12-14 00:50:51 +01:00
}
2013-09-11 14:18:56 +02:00
else if ( $resetEveryMonth )
{
$sqlwhere .= " (SUBSTRING( " . $field . " , " . $yearpos . " , " . $yearlen . " ) = ' " . $yearcomp . " ' " ;
$sqlwhere .= " AND SUBSTRING( " . $field . " , " . $monthpos . " , " . $monthlen . " ) = ' " . str_pad ( $monthcomp , $monthlen , '0' , STR_PAD_LEFT ) . " ') " ;
2013-05-24 11:23:56 +02:00
}
2012-04-15 17:55:45 +02:00
else // reset is done on january
2010-12-14 00:50:51 +01:00
{
2013-09-11 14:18:56 +02:00
$sqlwhere .= '(SUBSTRING(' . $field . ', ' . $yearpos . ', ' . $yearlen . " ) = ' " . $yearcomp . " ') " ;
2010-12-14 00:50:51 +01:00
}
}
2013-06-06 21:18:50 +02:00
//print "sqlwhere=".$sqlwhere." yearcomp=".$yearcomp."<br>\n"; // sqlwhere and yearcomp defined only if we ask a reset
//print "masktri=".$masktri." maskcounter=".$maskcounter." maskraz=".$maskraz." maskoffset=".$maskoffset."<br>\n";
2010-12-14 00:50:51 +01:00
// Define $sqlstring
2014-03-14 23:18:46 +01:00
$posnumstart = strrpos ( $maskwithnocode , $maskcounter ); // Pos of counter in final string (from 0 to ...)
2010-12-14 00:50:51 +01:00
if ( $posnumstart < 0 ) return 'ErrorBadMaskFailedToLocatePosOfSequence' ;
$sqlstring = 'SUBSTRING(' . $field . ', ' . ( $posnumstart + 1 ) . ', ' . dol_strlen ( $maskcounter ) . ')' ;
// Define $maskLike
$maskLike = dol_string_nospecial ( $mask );
$maskLike = str_replace ( " % " , " _ " , $maskLike );
// Replace protected special codes with matching number of _ as wild card caracter
$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 );
$maskLike = str_replace ( dol_string_nospecial ( '{' . $masktri . '}' ), str_pad ( " " , dol_strlen ( $maskcounter ), " _ " ), $maskLike );
if ( $maskrefclient ) $maskLike = str_replace ( dol_string_nospecial ( '{' . $maskrefclient . '}' ), str_pad ( " " , dol_strlen ( $maskrefclient ), " _ " ), $maskLike );
if ( $masktype ) $maskLike = str_replace ( dol_string_nospecial ( '{' . $masktype . '}' ), $masktype_value , $maskLike );
// Get counter in database
$counter = 0 ;
$sql = " SELECT MAX( " . $sqlstring . " ) as val " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $table ;
$sql .= " WHERE " . $field . " LIKE ' " . $maskLike . " ' " ;
2014-12-24 14:31:17 +01:00
$sql .= " AND " . $field . " NOT LIKE '(PROV%)' " ;
2014-10-17 17:10:12 +02:00
if ( $bentityon ) // only if entity enable
2014-10-17 09:53:17 +02:00
$sql .= " AND entity IN ( " . getEntity ( $table , 1 ) . " ) " ;
2014-12-24 14:31:17 +01:00
2010-12-14 00:50:51 +01:00
if ( $where ) $sql .= $where ;
if ( $sqlwhere ) $sql .= ' AND ' . $sqlwhere ;
//print $sql.'<br>';
2014-06-12 11:31:53 +02:00
dol_syslog ( " functions2::get_next_value mode= " . $mode . " " , LOG_DEBUG );
2010-12-14 00:50:51 +01:00
$resql = $db -> query ( $sql );
if ( $resql )
{
$obj = $db -> fetch_object ( $resql );
$counter = $obj -> val ;
}
else dol_print_error ( $db );
2013-06-06 21:18:50 +02:00
// Check if we must force counter to maskoffset
2010-12-14 00:50:51 +01:00
if ( empty ( $counter ) || preg_match ( '/[^0-9]/i' , $counter )) $counter = $maskoffset ;
2013-06-06 21:18:50 +02:00
else if ( $counter < $maskoffset && empty ( $conf -> global -> MAIN_NUMBERING_OFFSET_ONLY_FOR_FIRST )) $counter = $maskoffset ;
2010-12-14 00:50:51 +01:00
2013-04-16 15:00:41 +02:00
if ( $mode == 'last' ) // We found value for counter = last counter value. Now need to get corresponding ref of invoice.
2010-12-14 00:50:51 +01:00
{
$counterpadded = str_pad ( $counter , dol_strlen ( $maskcounter ), " 0 " , STR_PAD_LEFT );
2013-06-05 16:24:32 +02:00
// Define $maskLike
$maskLike = dol_string_nospecial ( $mask );
$maskLike = str_replace ( " % " , " _ " , $maskLike );
// Replace protected special codes with matching number of _ as wild card caracter
$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 );
$maskLike = str_replace ( dol_string_nospecial ( '{' . $masktri . '}' ), $counterpadded , $maskLike );
if ( $maskrefclient ) $maskLike = str_replace ( dol_string_nospecial ( '{' . $maskrefclient . '}' ), str_pad ( " " , dol_strlen ( $maskrefclient ), " _ " ), $maskLike );
if ( $masktype ) $maskLike = str_replace ( dol_string_nospecial ( '{' . $masktype . '}' ), $masktype_value , $maskLike );
2013-04-29 09:25:42 +02:00
2010-12-14 00:50:51 +01:00
$ref = '' ;
2013-04-16 15:00:41 +02:00
$sql = " SELECT " . $field . " as ref " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $table ;
$sql .= " WHERE " . $field . " LIKE ' " . $maskLike . " ' " ;
$sql .= " AND " . $field . " NOT LIKE '%PROV%' " ;
2014-10-17 17:10:12 +02:00
if ( $bentityon ) // only if entity enable
$sql .= " AND entity IN ( " . getEntity ( $table , 1 ) . " ) " ;
2013-06-05 16:24:32 +02:00
if ( $where ) $sql .= $where ;
if ( $sqlwhere ) $sql .= ' AND ' . $sqlwhere ;
2013-04-29 09:25:42 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( " functions2::get_next_value " , LOG_DEBUG );
2010-12-14 00:50:51 +01:00
$resql = $db -> query ( $sql );
if ( $resql )
{
$obj = $db -> fetch_object ( $resql );
if ( $obj ) $ref = $obj -> ref ;
}
else dol_print_error ( $db );
$numFinal = $ref ;
}
else if ( $mode == 'next' )
{
$counter ++ ;
2014-05-21 19:44:53 +02:00
// If value for $counter has a length higher than $maskcounter chars
if ( $counter >= pow ( 10 , dol_strlen ( $maskcounter )))
{
$counter = 'ErrorMaxNumberReachForThisMask' ;
2014-05-21 19:54:40 +02:00
}
2014-05-21 19:44:53 +02:00
2012-07-28 12:35:11 +02:00
if ( ! empty ( $maskrefclient_maskcounter ))
2010-12-14 00:50:51 +01:00
{
//print "maskrefclient_maskcounter=".$maskrefclient_maskcounter." maskwithnocode=".$maskwithnocode." maskrefclient=".$maskrefclient."\n<br>";
// 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 ) . ', ' . dol_strlen ( $maskrefclient_maskcounter ) . ')' ;
//print "x".$sqlstring;
// Define $maskrefclient_maskLike
$maskrefclient_maskLike = dol_string_nospecial ( $mask );
$maskrefclient_maskLike = str_replace ( " % " , " _ " , $maskrefclient_maskLike );
// Replace protected special codes with matching number of _ as wild card caracter
$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 ( " " , dol_strlen ( $maskcounter ), " _ " ), $maskrefclient_maskLike );
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{' . $maskrefclient . '}' ), $maskrefclient_clientcode . str_pad ( " " , dol_strlen ( $maskrefclient_maskcounter ), " _ " ), $maskrefclient_maskLike );
// 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 '(%'";
$maskrefclient_sql .= " WHERE " . $field . " LIKE ' " . $maskrefclient_maskLike . " ' " ;
2014-10-17 17:10:12 +02:00
if ( $bentityon ) // only if entity enable
$maskrefclient_sql .= " AND entity IN ( " . getEntity ( $table , 1 ) . " ) " ;
2010-12-14 00:50:51 +01:00
if ( $where ) $maskrefclient_sql .= $where ; //use the same optional where as general mask
if ( $sqlwhere ) $maskrefclient_sql .= ' AND ' . $sqlwhere ; //use the same sqlwhere as general mask
$maskrefclient_sql .= ' AND (SUBSTRING(' . $field . ', ' . ( strpos ( $maskwithnocode , $maskrefclient ) + 1 ) . ', ' . dol_strlen ( $maskrefclient_maskclientcode ) . " )=' " . $maskrefclient_clientcode . " ') " ;
2014-07-02 21:29:07 +02:00
dol_syslog ( " functions2::get_next_value maskrefclient " , LOG_DEBUG );
2010-12-14 00:50:51 +01:00
$maskrefclient_resql = $db -> query ( $maskrefclient_sql );
if ( $maskrefclient_resql )
{
$maskrefclient_obj = $db -> fetch_object ( $maskrefclient_resql );
$maskrefclient_counter = $maskrefclient_obj -> val ;
}
else dol_print_error ( $db );
2014-05-21 19:44:53 +02:00
2010-12-14 00:50:51 +01:00
if ( empty ( $maskrefclient_counter ) || preg_match ( '/[^0-9]/i' , $maskrefclient_counter )) $maskrefclient_counter = $maskrefclient_maskoffset ;
2013-06-06 21:18:50 +02:00
$maskrefclient_counter ++ ;
2010-12-14 00:50:51 +01:00
}
// Build numFinal
$numFinal = $mask ;
// We replace special codes except refclient
2012-09-23 22:09:54 +02:00
if ( ! empty ( $yearoffsettype ) && ! is_numeric ( $yearoffsettype ) && $yearoffsettype != '=' ) // yearoffsettype is - or +, so we don't want current year
{
2012-09-23 16:25:08 +02:00
$numFinal = preg_replace ( '/\{yyyy\}/i' , date ( " Y " , $date ) + $yearoffset , $numFinal );
$numFinal = preg_replace ( '/\{yy\}/i' , date ( " y " , $date ) + $yearoffset , $numFinal );
$numFinal = preg_replace ( '/\{y\}/i' , substr ( date ( " y " , $date ), 2 , 1 ) + $yearoffset , $numFinal );
2012-09-23 22:09:54 +02:00
}
2012-09-23 16:25:08 +02:00
else // we want yyyy to be current year
{
$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 );
2012-09-23 22:09:54 +02:00
}
2012-02-04 15:20:32 +01:00
$numFinal = preg_replace ( '/\{mm\}/i' , date ( " m " , $date ), $numFinal );
$numFinal = preg_replace ( '/\{dd\}/i' , date ( " d " , $date ), $numFinal );
2010-12-14 00:50:51 +01:00
// Now we replace the counter
$maskbefore = '{' . $masktri . '}' ;
$maskafter = str_pad ( $counter , dol_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 , dol_strlen ( $maskrefclient_maskcounter ), " 0 " , STR_PAD_LEFT );
$numFinal = str_replace ( $maskrefclient_maskbefore , $maskrefclient_maskafter , $numFinal );
}
// Now we replace the type
if ( $masktype )
{
$masktype_maskbefore = '{' . $masktype . '}' ;
$masktype_maskafter = $masktype_value ;
$numFinal = str_replace ( $masktype_maskbefore , $masktype_maskafter , $numFinal );
}
}
dol_syslog ( " functions2::get_next_value return " . $numFinal , LOG_DEBUG );
return $numFinal ;
2008-11-16 02:09:04 +01:00
}
2009-01-31 16:23:05 +01:00
/**
* Check value
2011-08-31 14:24:47 +02:00
*
2012-02-15 13:55:00 +01:00
* @ param string $mask Mask to use
* @ param string $value Value
* @ return int < 0 if KO , 0 if OK
2009-01-31 16:23:05 +01:00
*/
function check_value ( $mask , $value )
{
2010-12-14 00:50:51 +01:00
$result = 0 ;
2015-05-25 14:56:37 +02:00
$hasglobalcounter = false ;
2010-12-14 00:50:51 +01:00
// Extract value for mask counter, mask raz and mask offset
2015-05-25 14:56:37 +02:00
if ( preg_match ( '/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i' , $mask , $reg ))
{
$masktri = $reg [ 1 ] . ( isset ( $reg [ 2 ]) ? $reg [ 2 ] : '' ) . ( isset ( $reg [ 3 ]) ? $reg [ 3 ] : '' );
$maskcounter = $reg [ 1 ];
$hasglobalcounter = true ;
}
else
{
// setting some defaults so the rest of the code won't fail if there is a third party counter
$masktri = '00000' ;
$maskcounter = '00000' ;
}
2010-12-14 00:50:51 +01:00
$maskraz =- 1 ;
$maskoffset = 0 ;
2015-01-11 11:31:53 +01:00
if ( dol_strlen ( $maskcounter ) < 3 ) return 'ErrorCounterMustHaveMoreThan3Digits' ;
2010-12-14 00:50:51 +01:00
// Extract value for third party mask counter
if ( preg_match ( '/\{(c+)(0*)\}/i' , $mask , $regClientRef ))
{
$maskrefclient = $regClientRef [ 1 ] . $regClientRef [ 2 ];
$maskrefclient_maskclientcode = $regClientRef [ 1 ];
$maskrefclient_maskcounter = $regClientRef [ 2 ];
$maskrefclient_maskoffset = 0 ; //default value of maskrefclient_counter offset
2011-12-05 19:03:36 +01:00
$maskrefclient_clientcode = substr ( '' , 0 , dol_strlen ( $maskrefclient_maskclientcode )); //get n first characters of client code to form maskrefclient_clientcode
2010-12-14 00:50:51 +01:00
$maskrefclient_clientcode = str_pad ( $maskrefclient_clientcode , dol_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
2015-01-11 11:31:53 +01:00
if ( dol_strlen ( $maskrefclient_maskcounter ) > 0 && dol_strlen ( $maskrefclient_maskcounter ) < 3 ) return 'ErrorCounterMustHaveMoreThan3Digits' ;
2010-12-14 00:50:51 +01:00
}
else $maskrefclient = '' ;
2015-05-25 14:56:37 +02:00
// fail if there is neither a global nor a third party counter
if ( ! $hasglobalcounter && ( $maskrefclient_maskcounter == '' ))
{
return 'ErrorBadMask' ;
}
2010-12-14 00:50:51 +01:00
$maskwithonlyymcode = $mask ;
$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 );
$maskwithnocode = $maskwithonlyymcode ;
$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 );
// Now maskwithnocode = 0000ddmmyyyyccc for example
// and maskcounter = 0000 for example
//print "maskwithonlyymcode=".$maskwithonlyymcode." maskwithnocode=".$maskwithnocode."\n<br>";
// If an offset is asked
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 ]);
// Define $sqlwhere
// If a restore to zero after a month is asked we check if there is already a value for this year.
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 ]);
if ( $maskraz >= 0 )
{
if ( $maskraz > 12 ) return 'ErrorBadMaskBadRazMonth' ;
// Define reg
if ( $maskraz > 1 && ! preg_match ( '/^(.*)\{(y+)\}\{(m+)\}/i' , $maskwithonlyymcode , $reg )) return 'ErrorCantUseRazInStartedYearIfNoYearMonthInMask' ;
if ( $maskraz <= 1 && ! preg_match ( '/^(.*)\{(y+)\}/i' , $maskwithonlyymcode , $reg )) return 'ErrorCantUseRazIfNoYearInMask' ;
//print "x".$maskwithonlyymcode." ".$maskraz;
}
//print "masktri=".$masktri." maskcounter=".$maskcounter." maskraz=".$maskraz." maskoffset=".$maskoffset."<br>\n";
// Check we have a number in ($posnumstart+1).', '.dol_strlen($maskcounter)
//
// Check length
$len = dol_strlen ( $maskwithnocode );
if ( dol_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 ( " " , dol_strlen ( $maskcounter ), " _ " ), $maskLike );
if ( $maskrefclient ) $maskLike = str_replace ( dol_string_nospecial ( '{' . $maskrefclient . '}' ), str_pad ( " " , strlen ( $maskrefclient ), " _ " ), $maskLike );
dol_syslog ( " functions2::check_value result= " . $result , LOG_DEBUG );
return $result ;
2009-01-31 16:23:05 +01:00
}
2008-11-16 02:09:04 +01:00
/**
2011-07-04 10:38:51 +02:00
* Convert a binary data to string that represent hexadecimal value
2011-08-31 14:24:47 +02:00
*
2012-02-04 15:20:32 +01:00
* @ param string $bin Value to convert
* @ param boolean $pad Add 0
* @ param boolean $upper Convert to tupper
* @ return string x
2008-11-16 02:09:04 +01:00
*/
function binhex ( $bin , $pad = false , $upper = false )
{
2010-12-14 00:50:51 +01:00
$last = dol_strlen ( $bin ) - 1 ;
for ( $i = 0 ; $i <= $last ; $i ++ ){ $x += $bin [ $last - $i ] * pow ( 2 , $i ); }
$x = dechex ( $x );
if ( $pad ){ while ( dol_strlen ( $x ) < intval ( dol_strlen ( $bin )) / 4 ){ $x = " 0 $x " ; } }
if ( $upper ){ $x = strtoupper ( $x ); }
return $x ;
2008-11-16 02:09:04 +01:00
}
/**
2011-07-04 10:38:51 +02:00
* Convert an hexadecimal string into a binary string
2011-08-31 14:24:47 +02:00
*
2012-02-04 14:39:47 +01:00
* @ param string $hexa Hexadecimal string to convert ( example : 'FF' )
* @ return string bin
2008-11-16 02:09:04 +01:00
*/
function hexbin ( $hexa )
{
2010-12-14 00:50:51 +01:00
$bin = '' ;
2011-11-06 10:00:45 +01:00
$strLength = dol_strlen ( $hexa );
for ( $i = 0 ; $i < $strLength ; $i ++ )
2010-12-14 00:50:51 +01:00
{
$bin .= str_pad ( decbin ( hexdec ( $hexa { $i })), 4 , '0' , STR_PAD_LEFT );
}
return $bin ;
2008-11-16 02:09:04 +01:00
}
/**
2011-07-04 10:38:51 +02:00
* Retourne le numero de la semaine par rapport a une date
2011-08-31 14:24:47 +02:00
*
2012-02-04 14:39:47 +01:00
* @ param string $time Date au format 'timestamp'
* @ return int Number of week
2008-11-16 02:09:04 +01:00
*/
function numero_semaine ( $time )
{
2010-12-14 00:50:51 +01:00
$stime = strftime ( '%Y-%m-%d' , $time );
if ( preg_match ( '/^([0-9]+)\-([0-9]+)\-([0-9]+)\s?([0-9]+)?:?([0-9]+)?/i' , $stime , $reg ))
{
// 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 :
* - 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.
* - Le 1 er jour de la semaine est le Lundi
*/
// Definition du Jeudi de la semaine
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 );
// Definition du premier Jeudi de l'annee
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 ));
}
// Definition du numero de semaine: nb de jours entre "premier Jeudi de l'annee" et "Jeudi de la semaine";
$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 )
{
// Les annees qui commence un Jeudi et les annees bissextiles commencant un Mercredi en possede 53
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 );
2008-11-16 02:09:04 +01:00
}
/**
2011-07-04 10:38:51 +02:00
* Convertit une masse d ' une unite vers une autre unite
2011-08-31 14:24:47 +02:00
*
2012-02-04 14:39:47 +01:00
* @ param float $weight Masse a convertir
2014-09-27 16:00:11 +02:00
* @ param int $from_unit Unite originale en puissance de 10
2012-02-04 14:39:47 +01:00
* @ param int $to_unit Nouvelle unite en puissance de 10
* @ return float Masse convertie
2008-11-16 02:09:04 +01:00
*/
function weight_convert ( $weight , & $from_unit , $to_unit )
{
2010-12-14 00:50:51 +01:00
/* 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 )
{
$weight = $weight * 10 ;
$from_unit = $from_unit - 1 ;
$weight = weight_convert ( $weight , $from_unit , $to_unit );
}
if ( $from_unit < $to_unit )
{
$weight = $weight / 10 ;
$from_unit = $from_unit + 1 ;
$weight = weight_convert ( $weight , $from_unit , $to_unit );
}
}
return $weight ;
2008-11-16 02:09:04 +01:00
}
2009-01-07 11:57:36 +01:00
/**
2011-07-04 10:38:51 +02:00
* Save personnal parameter
2011-08-31 14:24:47 +02:00
*
2012-02-04 14:39:47 +01:00
* @ param DoliDB $db Handler database
* @ param Conf $conf Object conf
2014-10-04 17:20:17 +02:00
* @ param User $user Object user
* @ param array $tab Array ( key => value ) with all parameters to save
2012-02-04 14:39:47 +01:00
* @ return int < 0 if KO , > 0 if OK
2012-01-15 19:07:01 +01:00
*
* @ see dolibarr_get_const , dolibarr_set_const , dolibarr_del_const
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
{
2010-12-14 00:50:51 +01:00
// Verification parametres
2011-09-17 21:49:50 +02:00
if ( count ( $tab ) < 1 ) return - 1 ;
2010-12-14 00:50:51 +01:00
$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 ;
$sql .= " AND entity = " . $conf -> entity ;
$sql .= " AND param in ( " ;
$i = 0 ;
foreach ( $tab as $key => $value )
{
if ( $i > 0 ) $sql .= ',' ;
$sql .= " ' " . $key . " ' " ;
$i ++ ;
}
$sql .= " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( " functions2.lib::dol_set_user_param " , LOG_DEBUG );
2010-12-14 00:50:51 +01:00
$resql = $db -> query ( $sql );
if ( ! $resql )
{
dol_print_error ( $db );
$db -> rollback ();
return - 1 ;
}
foreach ( $tab as $key => $value )
{
// Set new parameters
2011-12-05 17:13:48 +01:00
if ( $value )
2010-12-14 00:50:51 +01:00
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " user_param(fk_user,entity,param,value) " ;
$sql .= " VALUES ( " . $user -> id . " , " . $conf -> entity . " , " ;
2012-07-28 11:28:37 +02:00
$sql .= " ' " . $key . " ',' " . $db -> escape ( $value ) . " ') " ;
2010-12-14 00:50:51 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( " functions2.lib::dol_set_user_param " , LOG_DEBUG );
2010-12-14 00:50:51 +01:00
$result = $db -> query ( $sql );
if ( ! $result )
{
dol_print_error ( $db );
$db -> rollback ();
return - 1 ;
}
2012-01-15 19:07:01 +01:00
$user -> conf -> $key = $value ;
//print "key=".$key." user->conf->key=".$user->conf->$key;
2010-12-14 00:50:51 +01:00
}
2012-01-15 23:48:25 +01:00
else
{
unset ( $user -> conf -> $key );
}
2010-12-14 00:50:51 +01:00
}
$db -> commit ();
return 1 ;
2009-01-07 11:57:36 +01:00
}
/**
2011-07-04 10:38:51 +02:00
* Returns formated reduction
2011-08-31 14:24:47 +02:00
*
2012-02-04 14:39:47 +01:00
* @ param int $reduction Reduction percentage
* @ param Translate $langs Output language
* @ return string Formated reduction
2009-01-07 11:57:36 +01:00
*/
2012-02-04 14:39:47 +01:00
function dol_print_reduction ( $reduction , $langs )
2009-01-07 11:57:36 +01:00
{
2010-12-14 00:50:51 +01:00
$string = '' ;
if ( $reduction == 100 )
{
2014-09-17 17:01:59 +02:00
$string = $langs -> transnoentities ( " Offered " );
2010-12-14 00:50:51 +01:00
}
else
{
$string = $reduction . '%' ;
}
return $string ;
2009-01-07 11:57:36 +01:00
}
2009-03-09 22:59:29 +01:00
/**
2011-10-05 22:54:39 +02:00
* Return OS version .
* Note that PHP_OS returns only OS ( not version ) and OS PHP was built on , not
* necessarly OS PHP runs on .
2011-08-31 14:24:47 +02:00
*
* @ return string OS version
2009-03-09 22:59:29 +01:00
*/
function version_os ()
{
2011-10-05 22:54:39 +02:00
$osversion = php_uname ();
2010-12-14 00:50:51 +01:00
return $osversion ;
2009-03-09 22:59:29 +01:00
}
/**
2011-08-31 14:24:47 +02:00
* Return PHP version
*
* @ return string PHP version
2009-03-09 22:59:29 +01:00
*/
function version_php ()
{
2010-12-14 00:50:51 +01:00
return phpversion ();
2009-03-09 22:59:29 +01:00
}
/**
2011-08-31 14:24:47 +02:00
* Return Dolibarr version
*
* @ return string Dolibarr version
2009-03-09 22:59:29 +01:00
*/
function version_dolibarr ()
{
2010-12-14 00:50:51 +01:00
return DOL_VERSION ;
2009-03-09 22:59:29 +01:00
}
/**
2011-08-31 14:24:47 +02:00
* Return web server version
*
* @ return string Web server version
2009-03-09 22:59:29 +01:00
*/
function version_webserver ()
{
2010-12-14 00:50:51 +01:00
return $_SERVER [ " SERVER_SOFTWARE " ];
2009-03-09 22:59:29 +01:00
}
2010-02-27 11:11:00 +01:00
2010-02-27 23:53:27 +01:00
/**
2011-08-31 14:24:47 +02:00
* Return list of activated modules usable for document generation
*
2012-02-04 14:39:47 +01:00
* @ param DoliDB $db Database handler
* @ param string $type Type of models ( company , invoice , ... )
* @ param int $maxfilenamelength Max length of value to show
* @ return mixed 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
*/
2011-06-08 19:37:21 +02:00
function getListOfModels ( $db , $type , $maxfilenamelength = 0 )
2010-02-27 23:53:27 +01:00
{
2010-12-14 00:50:51 +01:00
global $conf , $langs ;
$liste = array ();
$found = 0 ;
$dirtoscan = '' ;
$sql = " SELECT nom as id, nom as lib, libelle as label, description as description " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " document_model " ;
$sql .= " WHERE type = ' " . $type . " ' " ;
2013-01-14 12:01:44 +01:00
$sql .= " AND entity IN (0, " . ( ! empty ( $conf -> multicompany -> enabled ) && ! empty ( $conf -> multicompany -> transverse_mode ) ? " 1, " : " " ) . $conf -> entity . " ) " ;
2014-10-27 19:37:30 +01:00
$sql .= " ORDER BY description DESC " ;
2010-12-14 00:50:51 +01:00
2014-06-13 01:34:39 +02:00
dol_syslog ( '/core/lib/function2.lib.php::getListOfModels' , LOG_DEBUG );
2010-12-14 00:50:51 +01:00
$resql = $db -> query ( $sql );
if ( $resql )
{
$num = $db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$found = 1 ;
$obj = $db -> fetch_object ( $resql );
// If this generation module needs to scan a directory, then description field is filled
2011-02-20 20:01:24 +01:00
// with the constant that contains list of directories to scan (COMPANY_ADDON_PDF_ODT_PATH, ...).
2010-12-14 00:50:51 +01:00
if ( ! empty ( $obj -> description )) // List of directories to scan is defined
{
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2011-02-22 09:46:36 +01:00
2010-12-14 00:50:51 +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 ))
{
2014-09-17 17:49:39 +02:00
$tmpfiles = dol_dir_list ( $tmpdir , 'files' , 0 , '\.od(s|t)$' , '' , 'name' , SORT_ASC , 0 );
2011-09-17 21:49:50 +02:00
if ( count ( $tmpfiles )) $listoffiles = array_merge ( $listoffiles , $tmpfiles );
2010-12-14 00:50:51 +01:00
}
}
2011-09-17 21:49:50 +02:00
if ( count ( $listoffiles ))
2010-12-14 00:50:51 +01:00
{
foreach ( $listoffiles as $record )
{
2011-06-08 19:37:21 +02:00
$max = ( $maxfilenamelength ? $maxfilenamelength : 28 );
$liste [ $obj -> id . ':' . $record [ 'fullname' ]] = dol_trunc ( $record [ 'name' ], $max , 'middle' );
2010-12-14 00:50:51 +01:00
}
}
else
{
$liste [ 0 ] = $obj -> label . ': ' . $langs -> trans ( " None " );
}
}
else
{
$liste [ $obj -> id ] = $obj -> label ? $obj -> label : $obj -> lib ;
}
$i ++ ;
}
}
else
{
2010-10-18 21:29:15 +02:00
dol_print_error ( $db );
2010-12-14 00:50:51 +01:00
return - 1 ;
}
2010-02-27 23:53:27 +01:00
2010-12-14 00:50:51 +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
/**
2011-01-05 18:43:58 +01:00
* This function evaluates a string that should be a valid IPv4
2011-08-31 14:24:47 +02:00
*
2014-07-17 21:19:03 +02:00
* @ param string $ip IP Address
2014-07-26 14:16:09 +02:00
* @ return int 0 if not valid or reserved range , 1 if valid and public IP , 2 if valid and private range IP
2010-04-13 21:56:24 +02:00
*/
function is_ip ( $ip )
{
2014-07-26 14:16:09 +02:00
// First we test if it is a valid IPv4
2014-07-17 21:12:50 +02:00
if ( filter_var ( $ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )) {
2014-07-26 14:16:09 +02:00
// Then we test if it is a private range
if ( ! filter_var ( $ip , FILTER_VALIDATE_IP , FILTER_FLAG_NO_PRIV_RANGE )) return 2 ;
// Then we test if it is a reserved range
if ( ! filter_var ( $ip , FILTER_VALIDATE_IP , FILTER_FLAG_NO_RES_RANGE )) return 0 ;
2014-07-17 21:12:50 +02:00
2014-07-26 14:16:09 +02:00
return 1 ;
2014-07-17 21:12:50 +02:00
}
return 0 ;
2010-04-13 21:56:24 +02:00
}
2011-01-05 18:43:58 +01:00
/**
* Build a login from lastname , firstname
2011-08-31 14:24:47 +02:00
*
2012-02-04 14:39:47 +01:00
* @ param string $lastname Lastname
* @ param string $firstname Firstname
* @ return string Login
2011-01-05 18:43:58 +01:00
*/
function dol_buildlogin ( $lastname , $firstname )
{
2011-06-10 22:33:48 +02:00
$login = strtolower ( dol_string_unaccent ( $firstname ));
$login .= ( $login ? '.' : '' );
$login .= strtolower ( dol_string_unaccent ( $lastname ));
$login = dol_string_nospecial ( $login , '' ); // For special names
return $login ;
2011-04-03 20:02:18 +02:00
}
/**
* Return array to use for SoapClient constructor
2011-08-31 14:24:47 +02:00
*
2011-04-03 20:02:18 +02:00
* @ return param
*/
function getSoapParams ()
{
global $conf ;
$params = array ();
2011-06-09 01:10:42 +02:00
$proxyuse = ( empty ( $conf -> global -> MAIN_PROXY_USE ) ? false : true );
$proxyhost = ( empty ( $conf -> global -> MAIN_PROXY_USE ) ? false : $conf -> global -> MAIN_PROXY_HOST );
$proxyport = ( empty ( $conf -> global -> MAIN_PROXY_USE ) ? false : $conf -> global -> MAIN_PROXY_PORT );
$proxyuser = ( empty ( $conf -> global -> MAIN_PROXY_USE ) ? false : $conf -> global -> MAIN_PROXY_USER );
$proxypass = ( empty ( $conf -> global -> MAIN_PROXY_USE ) ? false : $conf -> global -> MAIN_PROXY_PASS );
$timeout = ( empty ( $conf -> global -> MAIN_USE_CONNECT_TIMEOUT ) ? 10 : $conf -> global -> MAIN_USE_CONNECT_TIMEOUT ); // Connection timeout
2011-04-03 20:02:18 +02:00
$response_timeout = ( empty ( $conf -> global -> MAIN_USE_RESPONSE_TIMEOUT ) ? 30 : $conf -> global -> MAIN_USE_RESPONSE_TIMEOUT ); // Response timeout
//print extension_loaded('soap');
if ( $proxyuse )
{
$params = array ( 'connection_timeout' => $timeout ,
'response_timeout' => $response_timeout ,
'proxy_use' => 1 ,
'proxy_host' => $proxyhost ,
'proxy_port' => $proxyport ,
'proxy_login' => $proxyuser ,
2011-10-18 00:28:01 +02:00
'proxy_password' => $proxypass ,
'trace' => 1
2011-06-10 22:33:48 +02:00
);
2011-04-03 20:02:18 +02:00
}
else
{
$params = array ( 'connection_timeout' => $timeout ,
'response_timeout' => $response_timeout ,
'proxy_use' => 0 ,
'proxy_host' => false ,
'proxy_port' => false ,
'proxy_login' => false ,
2011-10-18 00:28:01 +02:00
'proxy_password' => false ,
'trace' => 1
2011-06-10 22:33:48 +02:00
);
2011-04-03 20:02:18 +02:00
}
return $params ;
}
2013-06-16 23:15:20 +02:00
2013-06-19 09:26:20 +02:00
/**
* List urls of element
*
* @ param int $objectid Id of record
* @ param string $objecttype Type of object ( 'invoice' , 'order' , 'expedition_bon' , ... )
* @ param int $withpicto Picto to show
* @ param string $option More options
* @ return string URL of link to object id / type
*/
function dolGetElementUrl ( $objectid , $objecttype , $withpicto = 0 , $option = '' )
{
global $db , $conf ;
2013-06-16 23:15:20 +02:00
$ret = '' ;
2013-06-19 09:26:20 +02:00
// Parse element/subelement (ex: project_task)
$module = $element = $subelement = $objecttype ;
if ( preg_match ( '/^([^_]+)_([^_]+)/i' , $objecttype , $regs ))
{
$module = $element = $regs [ 1 ];
$subelement = $regs [ 2 ];
}
$classpath = $element . '/class' ;
// To work with non standard path
if ( $objecttype == 'facture' || $objecttype == 'invoice' ) {
2013-09-11 14:18:56 +02:00
$classpath = 'compta/facture/class' ;
$module = 'facture' ;
2013-08-09 13:48:44 +02:00
$subelement = 'facture' ;
2013-06-19 09:26:20 +02:00
}
if ( $objecttype == 'commande' || $objecttype == 'order' ) {
2013-09-11 14:18:56 +02:00
$classpath = 'commande/class' ;
$module = 'commande' ;
2013-08-09 13:48:44 +02:00
$subelement = 'commande' ;
2013-06-19 09:26:20 +02:00
}
if ( $objecttype == 'propal' ) {
$classpath = 'comm/propal/class' ;
}
2015-02-09 11:57:30 +01:00
if ( $objecttype == 'askpricesupplier' ) {
$classpath = 'comm/askpricesupplier/class' ;
}
2013-06-19 09:26:20 +02:00
if ( $objecttype == 'shipping' ) {
2013-09-11 14:18:56 +02:00
$classpath = 'expedition/class' ;
$subelement = 'expedition' ;
2013-08-09 13:48:44 +02:00
$module = 'expedition_bon' ;
2013-06-19 09:26:20 +02:00
}
if ( $objecttype == 'delivery' ) {
2013-09-11 14:18:56 +02:00
$classpath = 'livraison/class' ;
$subelement = 'livraison' ;
2013-08-09 13:48:44 +02:00
$module = 'livraison_bon' ;
2013-06-19 09:26:20 +02:00
}
if ( $objecttype == 'contract' ) {
2013-09-11 14:18:56 +02:00
$classpath = 'contrat/class' ;
$module = 'contrat' ;
2013-08-09 13:48:44 +02:00
$subelement = 'contrat' ;
2013-06-19 09:26:20 +02:00
}
if ( $objecttype == 'member' ) {
2013-09-11 14:18:56 +02:00
$classpath = 'adherents/class' ;
$module = 'adherent' ;
2013-08-09 13:48:44 +02:00
$subelement = 'adherent' ;
2013-06-19 09:26:20 +02:00
}
if ( $objecttype == 'cabinetmed_cons' ) {
2013-09-11 14:18:56 +02:00
$classpath = 'cabinetmed/class' ;
$module = 'cabinetmed' ;
2013-08-09 13:48:44 +02:00
$subelement = 'cabinetmedcons' ;
2013-06-19 09:26:20 +02:00
}
if ( $objecttype == 'fichinter' ) {
2013-09-11 14:18:56 +02:00
$classpath = 'fichinter/class' ;
$module = 'ficheinter' ;
2013-08-09 13:48:44 +02:00
$subelement = 'fichinter' ;
2013-06-19 09:26:20 +02:00
}
//print "objecttype=".$objecttype." module=".$module." subelement=".$subelement;
$classfile = strtolower ( $subelement ); $classname = ucfirst ( $subelement );
if ( $objecttype == 'invoice_supplier' ) {
2013-09-11 14:18:56 +02:00
$classfile = 'fournisseur.facture' ;
2013-08-09 13:48:44 +02:00
$classname = 'FactureFournisseur' ;
$classpath = 'fourn/class' ;
$module = 'fournisseur' ;
2013-06-19 09:26:20 +02:00
}
if ( $objecttype == 'order_supplier' ) {
2013-09-11 14:18:56 +02:00
$classfile = 'fournisseur.commande' ;
2013-08-09 13:48:44 +02:00
$classname = 'CommandeFournisseur' ;
$classpath = 'fourn/class' ;
$module = 'fournisseur' ;
2013-06-19 09:26:20 +02:00
}
if ( ! empty ( $conf -> $module -> enabled ))
{
$res = dol_include_once ( '/' . $classpath . '/' . $classfile . '.class.php' );
if ( $res )
{
$object = new $classname ( $db );
$res = $object -> fetch ( $objectid );
2013-06-16 23:15:20 +02:00
if ( $res > 0 ) $ret = $object -> getNomUrl ( $withpicto , $option );
unset ( $object );
}
}
2013-06-19 09:26:20 +02:00
return $ret ;
2013-06-24 20:02:02 +02:00
}
2014-04-26 21:36:58 +02:00
/**
* Clean corrupted tree ( orphelins linked to a not existing parent ), record linked to themself and child - parent loop
*
2014-04-26 22:23:46 +02:00
* @ param DoliDB $db Database handler
2014-04-26 21:36:58 +02:00
* @ param string $tabletocleantree Table to clean
* @ param string $fieldfkparent Field name that contains id of parent
* @ return int Nb of records fixed / deleted
*/
function cleanCorruptedTree ( $db , $tabletocleantree , $fieldfkparent )
{
$totalnb = 0 ;
$listofid = array ();
$listofparentid = array ();
// Get list of all id in array listofid and all parents in array listofparentid
$sql = 'SELECT rowid, ' . $fieldfkparent . ' as parent_id FROM ' . MAIN_DB_PREFIX . $tabletocleantree ;
$resql = $db -> query ( $sql );
if ( $resql )
{
$num = $db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$obj = $db -> fetch_object ( $resql );
$listofid [] = $obj -> rowid ;
if ( $obj -> parent_id > 0 ) $listofparentid [ $obj -> rowid ] = $obj -> parent_id ;
$i ++ ;
}
}
else
{
dol_print_error ( $db );
}
if ( count ( $listofid ))
{
print 'Code requested to clean tree (may be to solve data corruption), so we check/clean orphelins and loops.' . " <br> \n " ;
// Check loops on each other
$sql = " UPDATE " . MAIN_DB_PREFIX . $tabletocleantree . " SET " . $fieldfkparent . " = 0 WHERE " . $fieldfkparent . " = rowid " ; // So we update only records linked to themself
$resql = $db -> query ( $sql );
if ( $resql )
{
$nb = $db -> affected_rows ( $sql );
if ( $nb > 0 )
{
print '<br>Some record that were parent of themself were cleaned.' ;
}
$totalnb += $nb ;
}
//else dol_print_error($db);
// Check other loops
$listofidtoclean = array ();
foreach ( $listofparentid as $id => $pid )
{
// Check depth
//print 'Analyse record id='.$id.' with parent '.$pid.'<br>';
$cursor = $id ; $arrayidparsed = array (); // We start from child $id
while ( $cursor > 0 )
{
$arrayidparsed [ $cursor ] = 1 ;
if ( $arrayidparsed [ $listofparentid [ $cursor ]]) // We detect a loop. A record with a parent that was already into child
{
print 'Found a loop between id ' . $id . ' - ' . $cursor . '<br>' ;
unset ( $arrayidparsed );
$listofidtoclean [ $cursor ] = $id ;
break ;
}
$cursor = $listofparentid [ $cursor ];
}
if ( count ( $listofidtoclean )) break ;
}
$sql = " UPDATE " . MAIN_DB_PREFIX . $tabletocleantree ;
$sql .= " SET " . $fieldfkparent . " = 0 " ;
$sql .= " WHERE rowid IN ( " . join ( ',' , $listofidtoclean ) . " ) " ; // So we update only records detected wrong
$resql = $db -> query ( $sql );
if ( $resql )
{
$nb = $db -> affected_rows ( $sql );
if ( $nb > 0 )
{
// Removed orphelins records
print '<br>Some records were detected to have parent that is a child, we set them as root record for id: ' ;
print join ( ',' , $listofidtoclean );
}
$totalnb += $nb ;
}
//else dol_print_error($db);
// Check and clean orphelins
$sql = " UPDATE " . MAIN_DB_PREFIX . $tabletocleantree ;
$sql .= " SET " . $fieldfkparent . " = 0 " ;
$sql .= " WHERE " . $fieldfkparent . " NOT IN ( " . join ( ',' , $listofid ) . " ) " ; // So we update only records linked to a non existing parent
$resql = $db -> query ( $sql );
if ( $resql )
{
$nb = $db -> affected_rows ( $sql );
if ( $nb > 0 )
{
// Removed orphelins records
print '<br>Some orphelins were found and modified to be parent so records are visible again for id: ' ;
print join ( ',' , $listofid );
}
$totalnb += $nb ;
}
//else dol_print_error($db);
print '<br>We fixed ' . $totalnb . ' record(s). Some records may still be corrupted. New check may be required.' ;
return $totalnb ;
}
}
2014-03-15 16:29:33 +01:00
/**
* Get an array with properties of an element
*
* @ param string $element_type Element type . ex : project_task or object @ modulext or object_under @ module
* @ return array ( module , classpath , element , subelement , classfile , classname )
*/
function getElementProperties ( $element_type )
{
// Parse element/subelement (ex: project_task)
$module = $element = $subelement = $element_type ;
// If we ask an resource form external module (instead of default path)
if ( preg_match ( '/^([^@]+)@([^@]+)$/i' , $element_type , $regs ))
{
$element = $subelement = $regs [ 1 ];
$module = $regs [ 2 ];
}
//print '<br />1. element : '.$element.' - module : '.$module .'<br />';
if ( preg_match ( '/^([^_]+)_([^_]+)/i' , $element , $regs ))
{
$module = $element = $regs [ 1 ];
$subelement = $regs [ 2 ];
}
$classfile = strtolower ( $subelement );
$classname = ucfirst ( $subelement );
$classpath = $module . '/class' ;
// For compat
if ( $element_type == " action " ) {
$classpath = 'comm/action/class' ;
$subelement = 'Actioncomm' ;
$module = 'agenda' ;
}
// To work with non standard path
if ( $element_type == 'facture' || $element_type == 'invoice' ) {
$classpath = 'compta/facture/class' ;
$module = 'facture' ;
$subelement = 'facture' ;
}
if ( $element_type == 'commande' || $element_type == 'order' ) {
$classpath = 'commande/class' ;
$module = 'commande' ;
$subelement = 'commande' ;
}
if ( $element_type == 'propal' ) {
$classpath = 'comm/propal/class' ;
}
2015-02-09 11:57:30 +01:00
if ( $element_type == 'askpricesupplier' ) {
$classpath = 'comm/askpricesupplier/class' ;
}
2014-03-15 16:29:33 +01:00
if ( $element_type == 'shipping' ) {
$classpath = 'expedition/class' ;
$subelement = 'expedition' ;
$module = 'expedition_bon' ;
}
if ( $element_type == 'delivery' ) {
$classpath = 'livraison/class' ;
$subelement = 'livraison' ;
$module = 'livraison_bon' ;
}
if ( $element_type == 'contract' ) {
$classpath = 'contrat/class' ;
$module = 'contrat' ;
$subelement = 'contrat' ;
}
if ( $element_type == 'member' ) {
$classpath = 'adherents/class' ;
$module = 'adherent' ;
$subelement = 'adherent' ;
}
if ( $element_type == 'cabinetmed_cons' ) {
$classpath = 'cabinetmed/class' ;
$module = 'cabinetmed' ;
$subelement = 'cabinetmedcons' ;
}
if ( $element_type == 'fichinter' ) {
$classpath = 'fichinter/class' ;
$module = 'ficheinter' ;
$subelement = 'fichinter' ;
}
$classfile = strtolower ( $subelement );
$classname = ucfirst ( $subelement );
$element_properties = array (
'module' => $module ,
'classpath' => $classpath ,
'element' => $element ,
'subelement' => $subelement ,
'classfile' => $classfile ,
'classname' => $classname
);
return $element_properties ;
}
/**
* Fetch an object with element_type and its id
* Inclusion classes is automatic
*
2014-03-24 00:31:49 +01:00
* @ param int $element_id Element id
* @ param string $element_type Element type
2014-03-15 16:29:33 +01:00
* @ return object || 0 || - 1 if error
*/
function fetchObjectByElement ( $element_id , $element_type ) {
global $conf ;
2014-03-24 00:31:49 +01:00
global $db , $conf ;
2014-03-15 16:29:33 +01:00
$element_prop = getElementProperties ( $element_type );
if ( is_array ( $element_prop ) && $conf -> $element_prop [ 'module' ] -> enabled )
{
dol_include_once ( '/' . $element_prop [ 'classpath' ] . '/' . $element_prop [ 'classfile' ] . '.class.php' );
2014-03-24 00:31:49 +01:00
$objectstat = new $element_prop [ 'classname' ]( $db );
$ret = $objectstat -> fetch ( $element_id );
if ( $ret >= 0 )
{
return $objectstat ;
}
}
return 0 ;
2014-03-15 16:29:33 +01:00
}
2014-12-08 19:05:22 +01:00
/**
* Convert an array with RGB value into hex RGB value
*
* @ param array $arraycolor Array
* @ param string $colorifnotfound Color code to return if entry not defined
* @ return string RGB hex value ( without # before). For example: FF00FF
* @ see Make the opposite of colorStringToArray
*/
function colorArrayToHex ( $arraycolor , $colorifnotfound = '888888' )
{
if ( ! is_array ( $arraycolor )) return $colorifnotfound ;
return dechex ( $arraycolor [ 0 ]) . dechex ( $arraycolor [ 1 ]) . dechex ( $arraycolor [ 2 ]);
}
/**
* Convert a string RGB value ( 'FFFFFF' , '255,255,255' ) into an array RGB array ( 255 , 255 , 255 )
*
* @ param string $stringcolor String with hex ( FFFFFF ) or comma RGB ( '255,255,255' )
* @ param string $colorifnotfound Color code to return if entry not defined
* @ return string RGB hex value ( without # before). For example: FF00FF
* @ see Make the opposite of colorArrayToHex
*/
function colorStringToArray ( $stringcolor , $colorifnotfound = array ( 88 , 88 , 88 ))
{
if ( is_array ( $stringcolor )) return $stringcolor ; // If already into correct output format, we return as is
2015-03-22 18:17:25 +01:00
$tmp = preg_match ( '/^#?([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])$/' , $stringcolor , $reg );
2014-12-08 19:05:22 +01:00
if ( ! $tmp )
{
$tmp = explode ( ',' , $stringcolor );
if ( count ( $tmp ) < 3 ) return $colorifnotfound ;
return $tmp ;
}
return array ( hexdec ( $reg [ 1 ]), hexdec ( $reg [ 2 ]), hexdec ( $reg [ 3 ]));
}