2007-03-29 18:10:14 +02:00
< ? php
/*******************************************************************************
* Copyright ( C ) 2007 Easter - eggs
2021-04-13 18:04:19 +02:00
* https :// ldapsaisie . org
2007-03-29 18:10:14 +02:00
*
* Author : See AUTHORS file in top - level directory .
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation .
*
* 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 .
******************************************************************************/
/**
2008-04-25 16:09:27 +02:00
* Construction d ' une chaine formatée
2007-03-29 18:10:14 +02:00
*
2008-04-25 16:09:27 +02:00
* Cette fonction retourne la valeur d ' une chaine formatée selon le format
* et les données passés en paramètre .
2007-03-29 18:10:14 +02:00
*
* @ author Benjamin Renard < brenard @ easter - eggs . com >
*
2022-12-31 05:52:31 +01:00
* @ param string $format Format de la chaine
* @ param string $format Les données pour composés la chaine
2008-04-25 16:09:27 +02:00
* Ce paramètre peut être un tableau de string , une string ,
2007-03-29 18:10:14 +02:00
* une tableau d ' objet ou un objet .
2022-12-31 05:52:31 +01:00
* @ param string $format Le nom de la methode de / des objet ( s ) à appeler pour
2008-04-25 16:09:27 +02:00
* obtenir la valeur de remplacement dans la chaine formatée .
2020-04-29 15:54:21 +02:00
*
2022-12-31 05:52:31 +01:00
* @ return string La chaine formatée
2007-03-29 18:10:14 +02:00
*/
function getFData ( $format , $data , $meth = NULL ) {
2007-11-15 19:07:24 +01:00
$unique = false ;
2014-07-04 12:41:16 +02:00
/*
2018-09-20 19:28:26 +02:00
* Format : % {[ key name ][ : A ][ : B ][ ! ou _ ][ ~ ][ % }}
2014-07-04 12:41:16 +02:00
*
* Extracted fields
2018-09-20 19:28:26 +02:00
* - 0 : full string '%{...}'
2014-07-04 12:41:16 +02:00
* - 1 : key name
* - 2 : : A
* - 3 : A
* - 4 : : B
* - 5 : B
2018-09-20 19:28:26 +02:00
* - 6 : " ! " / " _ " / " ~ " / " % "
2014-07-04 12:41:16 +02:00
*/
2018-09-20 19:28:26 +02:00
$expr = " /%[ { (]([A-Za-z0-9]+)( \ :(-?[0-9])+)?( \ :(-?[0-9]+))?([ \ ! \ _~%]*)[})]/ " ;
2007-11-15 19:07:24 +01:00
if ( ! is_array ( $format )) {
$format = array ( $format );
$unique = true ;
}
for ( $i = 0 ; $i < count ( $format ); $i ++ ) {
if ( is_array ( $data )) {
if ( $meth == NULL ) {
2018-01-08 19:08:39 +01:00
while ( preg_match ( $expr , $format [ $i ], $ch )) {
2020-05-08 12:32:46 +02:00
if ( ! isset ( $data [ $ch [ 1 ]])) {
$val = '' ;
}
elseif ( is_array ( $data [ $ch [ 1 ]])) {
2008-11-09 02:06:30 +01:00
$val = $data [ $ch [ 1 ]][ 0 ];
}
else {
$val = $data [ $ch [ 1 ]];
}
2021-06-10 18:45:00 +02:00
$val = _getFData_extractAndModify ( $val , $ch );
2011-04-13 16:44:09 +02:00
$format [ $i ] = str_replace ( $ch [ 0 ], $val , $format [ $i ]);
2007-03-29 18:10:14 +02:00
}
2007-11-15 19:07:24 +01:00
}
else {
2018-01-08 19:08:39 +01:00
while ( preg_match ( $expr , $format [ $i ], $ch )) {
2007-11-15 19:07:24 +01:00
if ( method_exists ( $data [ $ch [ 1 ]], $meth )) {
2008-07-29 15:45:02 +02:00
$value = $data [ $ch [ 1 ]] -> $meth ();
if ( is_array ( $value )) {
$value = $value [ 0 ];
}
2021-06-10 18:45:00 +02:00
$value = _getFData_extractAndModify ( $value , $ch );
2011-04-13 16:44:09 +02:00
$format [ $i ] = str_replace ( $ch [ 0 ], $value , $format [ $i ]);
2007-11-15 19:07:24 +01:00
}
else {
2009-01-24 18:45:14 +01:00
LSerror :: addErrorCode ( 'fct_getFData_01' , array ( 'meth' => $meth , 'obj' => $ch [ 1 ]));
2007-11-15 19:07:24 +01:00
break ;
}
2007-03-29 18:10:14 +02:00
}
}
}
2009-10-26 00:44:11 +01:00
elseif ( is_object ( $data )) {
2007-11-15 19:07:24 +01:00
if ( $meth == NULL ) {
2018-01-08 19:08:39 +01:00
while ( preg_match ( $expr , $format [ $i ], $ch )) {
2009-10-26 00:44:11 +01:00
$value = $data -> $ch [ 1 ];
if ( is_array ( $value )) {
$value = $value [ 0 ];
}
2020-08-04 10:31:02 +02:00
$value = _getFData_extractAndModify ( $value , $ch );
2011-04-13 16:44:09 +02:00
$format [ $i ] = str_replace ( $ch [ 0 ], $value , $format [ $i ]);
2008-10-27 15:06:49 +01:00
}
2007-11-15 19:07:24 +01:00
}
else {
2018-01-08 19:08:39 +01:00
while ( preg_match ( $expr , $format [ $i ], $ch )) {
2007-11-15 19:07:24 +01:00
if ( method_exists ( $data , $meth )) {
2008-07-29 15:45:02 +02:00
$value = $data -> $meth ( $ch [ 1 ]);
if ( is_array ( $value )) {
$value = $value [ 0 ];
}
2021-06-10 18:45:00 +02:00
$value = _getFData_extractAndModify ( $value , $ch );
2011-04-13 16:44:09 +02:00
$format [ $i ] = str_replace ( $ch [ 0 ], $value , $format [ $i ]);
2007-11-15 19:07:24 +01:00
}
else {
2009-01-25 15:37:03 +01:00
LSerror :: addErrorCode ( 0 , getFData ( _ ( " Function 'getFData' : The method % { meth} of the object % { obj} doesn't exist. " ), array ( 'meth' => $meth , 'obj' => get_class ( $data ))));
2007-11-15 19:07:24 +01:00
break ;
}
2007-03-29 18:10:14 +02:00
}
}
}
2009-10-26 00:44:11 +01:00
else {
2018-01-08 19:08:39 +01:00
while ( preg_match ( $expr , $format [ $i ], $ch )) {
2021-06-10 18:45:00 +02:00
$val = _getFData_extractAndModify ( $data , $ch );
2011-04-13 16:44:09 +02:00
$format [ $i ] = str_replace ( $ch [ 0 ], $val , $format [ $i ]);
2009-10-26 00:44:11 +01:00
}
}
2007-03-29 18:10:14 +02:00
}
2007-11-15 19:07:24 +01:00
if ( $unique ) {
return $format [ 0 ];
}
2007-03-29 18:10:14 +02:00
return $format ;
}
2010-03-04 16:47:37 +01:00
function _getFData_extractAndModify ( $data , $ch ) {
2018-09-20 19:28:26 +02:00
/*
* Format : % {[ key name ][ : A ][ : B ][ - ][ ! ou _ ][ ~ ][ % }}
*
* Extracted fields
* - 0 : full string '%{...}'
* - 1 : key name
* - 2 : : A
* - 3 : A
* - 4 : : B
* - 5 : B
* - 6 : " ! " / " _ " / " ~ " / " % "
*/
// If A
if ( $ch [ 3 ] != " " ) {
// If A and B
if ( $ch [ 5 ] != " " ) {
// If A and B=0
if ( $ch [ 5 ] == 0 ) {
// If A<0 and B=0
if ( $ch [ 3 ] < 0 ) {
$s = strlen (( string ) $data ) - ( - 1 * $ch [ 3 ]);
$l = strlen (( string ) $data );
2010-03-04 16:47:37 +01:00
}
2018-09-20 19:28:26 +02:00
// If A >= 0 and B
2010-03-04 16:47:37 +01:00
else {
2018-09-20 19:28:26 +02:00
$s = $ch [ 3 ];
$l = strlen (( string ) $data );
2010-03-04 16:47:37 +01:00
}
}
2018-09-20 19:28:26 +02:00
// If A and B > 0
elseif ( $ch [ 5 ] > 0 ) {
// If A < 0 and B > 0 or A >= 0 and B > 0
2010-03-04 16:47:37 +01:00
$s = $ch [ 3 ];
$l = $ch [ 5 ];
}
2018-09-20 19:28:26 +02:00
// If A and B < 0
else {
// If A < 0 and B < 0
if ( $ch [ 3 ] < 0 ) {
$s = $ch [ 5 ];
$l = false ;
}
// If A >= 0 and B < 0
else {
$s = $ch [ 3 ] + $ch [ 5 ];
$l = abs ( $ch [ 5 ]);
}
}
2010-03-04 16:47:37 +01:00
}
2018-09-20 19:28:26 +02:00
// If only A
else {
2014-07-04 12:41:16 +02:00
if ( $ch [ 3 ] < 0 ) {
2018-09-20 19:28:26 +02:00
$s = $ch [ 3 ];
$l = false ;
2014-07-04 12:41:16 +02:00
}
else {
2018-09-20 19:28:26 +02:00
$s = 0 ;
$l = $ch [ 3 ];
2014-07-04 12:41:16 +02:00
}
}
2018-09-20 19:28:26 +02:00
if ( $l == false ) {
$val = mb_substr (( string ) $data , $s );
}
2010-03-04 16:47:37 +01:00
else {
2018-09-20 19:28:26 +02:00
$val = mb_substr (( string ) $data , $s , abs ( $l ));
2010-03-04 16:47:37 +01:00
}
}
else {
2014-11-20 12:00:26 +01:00
try {
$val = strval ( $data );
}
catch ( Exception $e ) {
$val = _ ( '[not string value]' );
}
2010-03-04 16:47:37 +01:00
}
2018-09-20 19:28:26 +02:00
if ( $ch [ 6 ]) {
# Without Accent
if ( strpos ( $ch [ 6 ], '~' ) !== false ) {
$val = withoutAccents ( $val );
}
2020-04-29 15:54:21 +02:00
2018-09-20 19:28:26 +02:00
# Upper / Lower case
if ( strpos ( $ch [ 6 ], '!' ) !== false ) {
$val = mb_strtoupper ( $val );
}
elseif ( strpos ( $ch [ 6 ], '_' ) !== false ) {
$val = mb_strtolower ( $val );
}
# Escape HTML entities
if ( strpos ( $ch [ 6 ], '%' ) !== false ) {
$val = htmlentities ( $val );
}
2010-03-04 16:47:37 +01:00
}
return $val ;
}
2008-11-09 01:57:50 +01:00
function getFieldInFormat ( $format ) {
$fields = array ();
2018-09-20 19:28:26 +02:00
$expr = '/%[{(]([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9]+))?(-)?(\!|\_)?(~)?(%)?[})]/' ;
2018-01-08 19:08:39 +01:00
while ( preg_match ( $expr , $format , $ch )) {
2008-11-09 01:57:50 +01:00
$fields [] = $ch [ 1 ];
2011-04-13 16:44:09 +02:00
$format = str_replace ( $ch [ 0 ], '' , $format );
2008-11-09 01:57:50 +01:00
}
return $fields ;
}
2018-01-08 19:08:39 +01:00
function loadDir ( $dir , $regexpr = '/^.*\.php$/' ) {
2008-02-08 18:39:24 +01:00
if ( $handle = opendir ( $dir )) {
while ( false !== ( $file = readdir ( $handle ))) {
2018-01-08 19:08:39 +01:00
if ( preg_match ( $regexpr , $file )) {
2008-02-08 18:39:24 +01:00
require_once ( $dir . '/' . $file );
}
}
}
else {
2009-03-12 19:01:31 +01:00
die ( _ ( 'Folder not found' ) . ' : ' . $dir );
2008-02-08 18:39:24 +01:00
}
return true ;
2008-02-05 17:11:21 +01:00
}
2007-03-29 18:10:14 +02:00
function valid ( $obj ) {
2009-03-09 13:42:03 +01:00
LSdebug ( 'function valid() : ok' );
2007-03-29 18:10:14 +02:00
return true ;
}
2009-01-21 18:08:09 +01:00
function validPas ( $obj = null ) {
2009-03-09 13:42:03 +01:00
LSdebug ( 'function valid() : nok' );
2009-01-21 18:08:09 +01:00
return false ;
}
2007-03-29 18:10:14 +02:00
function return_data ( $data ) {
return $data ;
}
2009-11-11 20:53:49 +01:00
function varDump ( $data ) {
ob_start ();
var_dump ( $data );
2020-04-29 15:54:21 +02:00
$data = ob_get_contents ();
2009-11-11 20:53:49 +01:00
ob_end_clean ();
return $data ;
}
2021-08-25 16:50:29 +02:00
/*
* LSdebug
*/
$GLOBALS [ 'LSdebug_fields' ] = array ();
function LSdebug ( $data , $dump = false ) {
if ( $dump )
$data = varDump ( $data );
2020-05-13 15:54:52 +02:00
if ( class_exists ( 'LSlog' ))
LSlog :: debug ( $data );
2019-06-28 18:00:37 +02:00
2021-08-25 16:50:29 +02:00
$GLOBALS [ 'LSdebug_fields' ][] = htmlentities ( strval ( $data ));
2008-02-08 18:39:24 +01:00
return true ;
2007-11-15 19:07:24 +01:00
}
2021-08-25 16:50:29 +02:00
function LSdebug_print ( $return = false ) {
$result = array ();
if ( LSdebugDefined ())
$result = $GLOBALS [ 'LSdebug_fields' ];
// Reset
$GLOBALS [ 'LSdebug_fields' ] = array ();
if ( $return )
return $result ;
LStemplate :: assign (
'LSdebug_content' ,
base64_encode (
json_encode (
$result
)
)
);
2008-06-05 15:21:18 +02:00
return ;
2007-11-15 19:07:24 +01:00
}
2008-10-13 12:58:48 +02:00
function LSdebugDefined () {
2009-03-25 13:26:32 +01:00
if ( ! LSdebug )
2008-10-13 12:58:48 +02:00
return ;
2021-08-25 16:50:29 +02:00
return (
isset ( $GLOBALS [ 'LSdebug_fields' ]) &&
is_array ( $GLOBALS [ 'LSdebug_fields' ]) &&
! empty ( $GLOBALS [ 'LSdebug_fields' ])
);
2008-10-13 12:58:48 +02:00
}
2021-08-25 18:02:37 +02:00
/**
* Vérifie la compatibilite des DN
*
* Vérifie que les DNs sont dans la même branche de l ' annuaire .
*
2022-12-31 05:52:31 +01:00
* @ param string $dn1 Un premier DN .
* @ param string $dn2 Un deuxième DN .
2021-08-25 18:02:37 +02:00
*
* @ author Benjamin Renard < brenard @ easter - eggs . com >
*
2022-12-31 05:52:31 +01:00
* @ return boolean true si les DN sont compatibles , false sinon .
2021-08-25 18:02:37 +02:00
*/
function isCompatibleDNs ( $dn1 , $dn2 ) {
$infos1 = ldap_explode_dn ( $dn1 , 0 );
if ( ! $infos1 )
return ;
$infos2 = ldap_explode_dn ( $dn2 , 0 );
if ( ! $infos2 )
return ;
if ( $infos2 [ 'count' ] > $infos1 [ 'count' ]) {
$tmp = $infos1 ;
$infos1 = $infos2 ;
$infos2 = $tmp ;
}
$infos1 = array_reverse ( $infos1 );
$infos2 = array_reverse ( $infos2 );
2020-04-29 15:54:21 +02:00
2021-08-25 18:02:37 +02:00
for ( $i = 0 ; $i < $infos1 [ 'count' ]; $i ++ ) {
if ( ! isset ( $infos2 [ $i ])) continue ;
if ( $infos1 [ $i ] == $infos2 [ $i ]) continue ;
return false ;
2008-02-08 18:39:24 +01:00
}
2021-08-25 18:02:37 +02:00
return true ;
}
2008-02-08 18:39:24 +01:00
2021-08-25 18:02:37 +02:00
/**
* Fait la somme de DN
*
* Retourne un DN qui correspond au point de séparation des DN si les DN
* ne sont pas dans la meme dans la meme branche ou le dn le plus long sinon .
*
2022-12-31 05:52:31 +01:00
* @ param string $dn1 Un premier DN .
* @ param string $dn2 Un deuxième DN .
2021-08-25 18:02:37 +02:00
*
* @ author Benjamin Renard < brenard @ easter - eggs . com >
*
2022-12-31 05:52:31 +01:00
* @ return string Un DN ( ou false si les DN ne sont pas valide )
2021-08-25 18:02:37 +02:00
*/
function sumDn ( $dn1 , $dn2 ) {
$infos1 = ldap_explode_dn ( $dn1 , 0 );
if ( ! $infos1 )
return ;
$infos2 = ldap_explode_dn ( $dn2 , 0 );
if ( ! $infos2 )
return ;
if ( $infos2 [ 'count' ] > $infos1 [ 'count' ]) {
$tmp = $infos1 ;
$infos1 = $infos2 ;
$infos2 = $tmp ;
}
$infos1 = array_reverse ( $infos1 );
$infos2 = array_reverse ( $infos2 );
$first = true ;
$basedn = '' ;
for ( $i = 0 ; $i < $infos1 [ 'count' ]; $i ++ ) {
if (( $infos1 [ $i ] == $infos2 [ $i ]) || ( ! isset ( $infos2 [ $i ]))) {
if ( $first ) {
$basedn = $infos1 [ $i ];
$first = false ;
2008-02-08 18:39:24 +01:00
}
2021-08-25 18:02:37 +02:00
else
$basedn = $infos1 [ $i ] . ',' . $basedn ;
}
else {
return $basedn ;
2008-02-08 18:39:24 +01:00
}
}
2021-08-25 18:02:37 +02:00
return $basedn ;
}
2020-04-29 15:54:21 +02:00
2021-08-25 18:02:37 +02:00
function checkEmail ( $value , $domain = NULL , $checkDns = true ) {
$log = LSlog :: get_logger ( 'checkEmail' );
$regex = '/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/' ;
2008-06-05 15:21:18 +02:00
2021-08-25 18:02:37 +02:00
if ( ! preg_match ( $regex , $value )) {
$log -> debug ( " ' $value ': regex fail " );
return false ;
}
2008-06-05 15:21:18 +02:00
2021-08-25 18:02:37 +02:00
$nd = explode ( '@' , $value );
$nd = $nd [ 1 ];
2020-04-29 15:54:21 +02:00
2021-08-25 18:02:37 +02:00
if ( $domain ) {
if ( is_array ( $domain )) {
if ( ! in_array ( $nd , $domain )) {
$log -> debug ( " ' $value ': domain ' $nd ' not authorized. Allowed domains: ' " . implode ( " ', ' " , $domain ) . " ' " );
return false ;
2009-02-12 15:39:44 +01:00
}
}
2021-08-25 18:02:37 +02:00
else {
if ( $nd != $domain ) {
$log -> debug ( " ' $value ': domain ' $nd ' not authorized. Allowed domains: ' $domain ' " );
2008-06-05 15:21:18 +02:00
return false ;
}
}
}
2020-04-29 15:54:21 +02:00
2021-08-25 18:02:37 +02:00
if ( $checkDns && function_exists ( 'checkdnsrr' )) {
if ( ! ( checkdnsrr ( $nd , 'MX' ) || checkdnsrr ( $nd , 'A' ))) {
$log -> debug ( " ' $value ': DNS check fail " );
return false ;
2008-12-06 00:42:47 +01:00
}
2021-08-25 18:02:37 +02:00
}
$log -> debug ( " ' $value ': validated " );
return true ;
}
function generatePassword ( $chars = NULL , $lenght = NULL ) {
if ( ! $lenght ) {
$lenght = 8 ;
}
if ( is_array ( $chars )) {
$retval = '' ;
foreach ( $chars as $chs ) {
if ( ! is_array ( $chs )) {
$chs = array ( 'chars' => $chs );
2008-12-06 00:42:47 +01:00
}
2021-08-25 18:02:37 +02:00
if ( ! isset ( $chs [ 'nb' ]) || ! is_int ( $chs [ 'nb' ])) {
$chs [ 'nb' ] = 1 ;
2008-12-06 00:42:47 +01:00
}
2021-08-25 18:02:37 +02:00
$retval .= aleaChar ( $chs [ 'chars' ], $chs [ 'nb' ]);
}
$add = ( $lenght - strlen ( $retval ));
if ( $add > 0 ) {
$retval .= aleaChar ( $chars , $add );
2008-12-06 00:42:47 +01:00
}
2021-08-25 18:02:37 +02:00
return str_shuffle ( $retval );
} else {
return aleaChar ( $chars , $lenght );
2008-12-06 00:42:47 +01:00
}
2021-08-25 18:02:37 +02:00
}
2020-04-29 15:54:21 +02:00
2021-08-25 18:02:37 +02:00
function aleaChar ( $chars = NULL , $lenght = 1 ) {
if ( is_array ( $chars )) {
$nchars = " " ;
foreach ( $chars as $chs ) {
if ( is_string ( $chs )) {
$nchars .= $chs ;
2008-12-06 00:42:47 +01:00
}
2021-08-25 18:02:37 +02:00
else if ( is_string ( $chs [ 'chars' ])) {
$nchars .= $chs [ 'chars' ];
2008-12-06 00:42:47 +01:00
}
}
2021-08-25 18:02:37 +02:00
if ( is_string ( $chars ) && strlen ( $chars ) > 0 ) {
$chars = $nchars ;
2008-06-05 15:21:18 +02:00
}
2021-08-25 18:02:37 +02:00
else {
$chars = NULL ;
2008-06-05 15:21:18 +02:00
}
}
2021-08-25 18:02:37 +02:00
if ( ! $chars ) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-' ;
2008-06-18 14:27:35 +02:00
}
2021-08-25 18:02:37 +02:00
$nbChars = strlen ( $chars );
$retval = " " ;
if ( is_int ( $lenght )) {
for ( $i = 0 ; $i < $lenght ; $i ++ ) {
$retval .= $chars [ rand ( 0 , $nbChars - 1 )];
2009-03-19 18:42:51 +01:00
}
}
2021-08-25 18:02:37 +02:00
return $retval ;
}
2020-04-29 15:54:21 +02:00
2021-08-25 18:02:37 +02:00
function compareDn ( $a , $b ) {
if ( substr_count ( $a , ',' ) > substr_count ( $b , ',' ))
return - 1 ;
else
return 1 ;
}
/**
* Translate message by using LSlang or Gettext methods
*
2022-12-31 05:52:31 +01:00
* @ param string $msg The message
2021-08-25 18:02:37 +02:00
*
2022-12-31 05:52:31 +01:00
* @ return string The translated message if translation available , the original message otherwise
2021-08-25 18:02:37 +02:00
**/
function __ ( $msg ) {
if ( empty ( $msg )) return $msg ;
if ( isset ( $GLOBALS [ 'LSlang' ][ $msg ])) {
return $GLOBALS [ 'LSlang' ][ $msg ];
2020-05-04 18:02:39 +02:00
}
2021-08-25 18:02:37 +02:00
return _ ( $msg );
}
/**
* Non - translate message
*
* Just - return the input message . This function permit the detection of message
* that will be translated only at display time and not at declare time .
*
2022-12-31 05:52:31 +01:00
* @ param string $msg The message
2021-08-25 18:02:37 +02:00
*
2022-12-31 05:52:31 +01:00
* @ return string The message ( unchanged )
2021-08-25 18:02:37 +02:00
**/
function ___ ( $msg ) {
return $msg ;
}
2020-05-04 18:02:39 +02:00
2021-06-28 16:22:26 +02:00
// Try to load unidecode library
2021-10-21 20:19:45 +02:00
// Note: unidecode lib use mb_ord function only available since PHP 7.2.
if ( ! function_exists ( 'unidecode' ) && function_exists ( 'mb_ord' )) {
2021-06-28 16:22:26 +02:00
if ( file_exists ( LS_LIB_DIR . " /unidecode/unidecode.php " ))
@ include ( LS_LIB_DIR . " /unidecode/unidecode.php " );
if ( ! function_exists ( 'unidecode' ) && stream_resolve_include_path ( " unidecode/unidecode.php " ))
@ include ( " unidecode/unidecode.php " );
}
2010-03-04 16:47:37 +01:00
/**
* Supprime les accents d ' une chaine
2020-04-29 15:54:21 +02:00
*
2022-12-31 05:52:31 +01:00
* @ param string $string La chaine originale
2020-04-29 15:54:21 +02:00
*
2022-12-31 05:52:31 +01:00
* @ return string La chaine sans les accents
2010-03-04 16:47:37 +01:00
*/
2021-06-28 16:22:26 +02:00
function withoutAccents ( $string ){
// Use unidecode lib if available
if ( function_exists ( 'unidecode' ))
return unidecode ( $string );
// Otherwise, use historical method
$replaceAccent = array (
" à " => " a " , " á " => " a " , " â " => " a " , " ã " => " a " , " ä " => " a " , " ç " => " c " ,
" è " => " e " , " é " => " e " , " ê " => " e " , " ë " => " e " , " ì " => " i " , " í " => " i " ,
" î " => " i " , " ï " => " i " , " ñ " => " n " , " ò " => " o " , " ó " => " o " , " ô " => " o " ,
" õ " => " o " , " ö " => " o " , " ù " => " u " , " ú " => " u " , " û " => " u " , " ü " => " u " ,
" ý " => " y " , " ÿ " => " y " , " À " => " A " , " Á " => " A " , " Â " => " A " , " Ã " => " A " ,
" Ä " => " A " , " Ç " => " C " , " È " => " E " , " É " => " E " , " Ê " => " E " , " Ë " => " E " ,
" Ì " => " I " , " Í " => " I " , " Î " => " I " , " Ï " => " I " , " Ñ " => " N " , " Ò " => " O " ,
" Ó " => " O " , " Ô " => " O " , " Õ " => " O " , " Ö " => " O " , " Ù " => " U " , " Ú " => " U " ,
" Û " => " U " , " Ü " => " U " , " Ý " => " Y "
);
return strtr ( $string , $replaceAccent );
}
2010-03-04 16:47:37 +01:00
2010-10-01 17:30:28 +02:00
/**
* Supprime les espaces d ' une chaine en les remplacant par un motif ou non
*
2022-12-31 05:52:31 +01:00
* @ param string $str La chaine originale
* @ param string $to Le motif de remplacement . S 'il n' est pas spécifié , les espaces seront simplement supprimés
2010-10-01 17:30:28 +02:00
*
2022-12-31 05:52:31 +01:00
* @ return string La chaine sans les espaces
2010-10-01 17:30:28 +02:00
**/
function replaceSpaces ( $str , $to = '' ) {
return strtr ( $str , array (
' ' => $to ,
" \t " => $to
)
);
}
2011-02-03 17:30:55 +01:00
/**
* List files in a directory corresponding to a regex
*
2022-12-31 05:52:31 +01:00
* @ param string $dir The path of the directory
* @ param string $regex The regex apply on filename
2020-04-29 15:54:21 +02:00
*
2022-12-31 05:52:31 +01:00
* @ return array () List of file name
2011-02-03 17:30:55 +01:00
**/
function listFiles ( $dir , $regex ) {
$retval = array ();
if ( is_dir ( $dir )) {
$d = dir ( $dir );
while ( false !== ( $file = $d -> read ())) {
if ( is_file ( " $dir / $file " )) {
2020-06-14 23:42:11 +02:00
if ( preg_match ( $regex , $file , $m )) {
$retval [] = (( is_array ( $m ) && count ( $m ) > 1 ) ? $m : $file );
2011-02-03 17:30:55 +01:00
}
}
}
}
return $retval ;
}
2014-09-30 13:48:50 +02:00
/**
* Return current date in LDAP format
*
2022-12-31 05:52:31 +01:00
* @ param mixed $mixed Anything ( to permit using as generated function )
2014-09-30 13:48:50 +02:00
*
2022-12-31 05:52:31 +01:00
* @ return string The current date in LDAP format ( YYYYMMDDHHMMSSZ )
2014-09-30 13:48:50 +02:00
**/
function now ( $mixed = Null ) {
return date ( 'YmdHis' ) . 'Z' ;
}
2020-08-17 20:04:21 +02:00
/**
* Check if a path is absolute
*
2022-12-31 05:52:31 +01:00
* @ param string $path The path
2020-08-17 20:04:21 +02:00
*
2022-12-31 05:52:31 +01:00
* @ return boolean True if path is absolute , False otherwise
2020-08-17 20:04:21 +02:00
*/
function isAbsolutePath ( $path ) {
return strStartWith ( $path , '/' ) || strStartWith ( $path , './' ) || strStartWith ( $path , '../' );
}
/**
* Check if a string start with another specified string
*
2022-12-31 05:52:31 +01:00
* @ param string $string The string to search in
* @ param string $start_string The starting string to check
2020-08-17 20:04:21 +02:00
*
2022-12-31 05:52:31 +01:00
* @ return boolean True if string start by specified one , False otherwise
2020-08-17 20:04:21 +02:00
*/
function strStartWith ( $string , $start_string ) {
if ( strlen ( $start_string ) > strlen ( $string ))
return false ;
return substr ( $string , 0 , strlen ( $start_string )) === $start_string ;
}
2020-05-02 17:49:15 +02:00
/**
* Dump file content
*
2022-12-31 05:52:31 +01:00
* @ param string $file_path The file path to dump
* @ param string | null $mime_type The MIME type return as Content - type ( optional , default : auto - detected )
* @ param int $max_age The cache max_age value , as return in Cache - Control HTTP header
2020-05-02 17:49:15 +02:00
* ( optional , default : 3600 )
2022-12-31 05:52:31 +01:00
* @ param boolean $force_download Set to true to force download ( optional , default : false )
* @ param string | null $filename Specific filename in case of force download ( optional , default : orignal filename )
2020-05-02 17:49:15 +02:00
*
2022-12-31 05:52:31 +01:00
* @ return void
2020-05-02 17:49:15 +02:00
**/
2020-08-13 16:20:34 +02:00
function dumpFile ( $file_path , $mime_type = null , $max_age = 3600 , $force_download = false , $filename = null ) {
2020-05-02 17:49:15 +02:00
if ( is_file ( $file_path )) {
2020-05-05 12:48:52 +02:00
header ( 'Content-Type: ' . ( is_null ( $mime_type ) ? mime_content_type ( $file_path ) : $mime_type ));
2020-08-13 16:20:34 +02:00
if ( $force_download )
header ( " Content-disposition: attachment; filename= \" " . ( $filename ? $filename : basename ( $file_path )) . " \" " );
2020-05-02 17:49:15 +02:00
$last_modified_time = filemtime ( $file_path );
$etag = md5_file ( $file_path );
header ( " Cache-Control: max-age= $max_age , must-revalidate " );
header ( " Last-Modified: " . gmdate ( " D, d M Y H:i:s " , $last_modified_time ) . " GMT " );
header ( " Etag: $etag " );
if (
(
isset ( $_SERVER [ 'HTTP_IF_MODIFIED_SINCE' ]) &&
@ strtotime ( $_SERVER [ 'HTTP_IF_MODIFIED_SINCE' ]) == $last_modified_time
)
||
(
isset ( $_SERVER [ 'HTTP_IF_NONE_MATCH' ]) &&
trim ( $_SERVER [ 'HTTP_IF_NONE_MATCH' ]) == $etag
)
) {
header ( " HTTP/1.1 304 Not Modified " );
exit ();
}
header ( 'Pragma: public' );
header ( 'Content-Length: ' . filesize ( $file_path ));
readfile ( $file_path );
exit ();
}
// File not found, Trigger error 404 (via LSurl if defined)
if ( class_exists ( 'LSurl' )) {
LSurl :: error_404 ();
}
else {
header ( " HTTP/1.1 404 Not found " );
exit ();
}
}
2020-08-26 18:08:14 +02:00
/**
* Format a callable object for logging
* @ param callable $callable The callable object
* @ return string The callable object string representation
*/
function format_callable ( $callable ) {
2022-12-31 05:52:31 +01:00
if ( is_string ( $callable ))
return $callable . " () " ;
if ( is_array ( $callable ) && count ( $callable ) == 2 )
if ( is_string ( $callable [ 0 ]))
return $callable [ 0 ] . " :: " . $callable [ 1 ] . " () " ;
elseif ( is_object ( $callable [ 0 ]))
return get_class ( $callable [ 0 ]) . " -> " . $callable [ 1 ] . " () " ;
else
return " Unkown-> " . $callable [ 1 ] . " () " ;
return varDump ( $callable );
2020-08-26 18:08:14 +02:00
}
2020-09-11 13:34:42 +02:00
function is_empty ( $val ) {
switch ( gettype ( $val )) {
case " boolean " :
case " integer " :
case " double " :
case " object " :
case " resource " :
return False ;
case " array " :
case " string " :
if ( $val == " 0 " ) return false ;
return empty ( $val );
case " NULL " :
return True ;
}
return empty ( $val );
}
2020-11-30 19:37:44 +01:00
function ensureIsArray ( $value ) {
if ( is_array ( $value ))
return $value ;
if ( is_empty ( $value ))
return array ();
return array ( $value );
}
2021-06-10 17:56:34 +02:00
function ldapDate2DateTime ( $value , $naive = False , $format = null ) {
if ( is_null ( $format ))
$format = ( $naive ? 'YmdHis*' : 'YmdHisO' );
$datetime = date_create_from_format ( $format , $value );
if ( $datetime instanceof DateTime )
return $datetime ;
return False ;
}
function ldapDate2Timestamp ( $value , $naive = False , $format = null ) {
$datetime = ldapDate2DateTime ( $value , $naive , $format );
if ( $datetime instanceof DateTime )
return $datetime -> format ( 'U' );
return False ;
}
function dateTime2LdapDate ( $datetime , $timezone = null , $format = null ) {
if ( $timezone != 'naive' && $timezone != 'local' ) {
$datetime -> setTimezone ( timezone_open ( is_null ( $timezone ) ? 'UTC' : $timezone ));
}
if ( is_null ( $format ))
2022-12-31 02:01:17 +01:00
$format = ( $timezone == 'naive' ? 'YmdHis' : 'YmdHisO' );
2021-06-10 17:56:34 +02:00
$datetime_string = $datetime -> format ( $format );
// Replace +0000 or -0000 end by Z
$datetime_string = preg_replace ( '/[\+\-]0000$/' , 'Z' , $datetime_string );
return $datetime_string ;
}
function timestamp2LdapDate ( $value , $timezone = null , $format = null ) {
$datetime = date_create ( " @ $value " );
if ( $datetime instanceof DateTime )
return dateTime2LdapDate ( $datetime , $timezone , $format );
return false ;
}