2 /*******************************************************************************
3 * Copyright (C) 2007 Easter-eggs
4 * http://ldapsaisie.labs.libre-entreprise.org
6 * Author: See AUTHORS file in top-level directory.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 ******************************************************************************/
24 * Construction d'une chaine formatée
26 * Cette fonction retourne la valeur d'une chaine formatée selon le format
27 * et les données passés en paramètre.
29 * @author Benjamin Renard <brenard@easter-eggs.com>
31 * @param[in] $format string Format de la chaine
32 * @param[in] $data mixed Les données pour composés la chaine
33 * Ce paramètre peut être un tableau de string, une string,
34 * une tableau d'objet ou un objet.
35 * @param[in] $meth string Le nom de la methode de/des objet(s) à appeler pour
36 * obtenir la valeur de remplacement dans la chaine formatée.
38 * @retval string La chaine formatée
40 function getFData($format,$data,$meth=NULL) {
42 $expr="%{([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9])+)?}";
43 if(!is_array($format)) {
44 $format=array($format);
47 for($i=0;$i<count($format);$i++) {
50 while (ereg($expr,$format[$i],$ch)) {
51 if (is_array($data[$ch[1]])) {
52 $val = $data[$ch[1]][0];
66 $val=substr((string)$val,$s,$l);
68 $format[$i]=ereg_replace($ch[0],$val,$format[$i]);
72 while (ereg($expr,$format[$i],$ch)) {
73 if (method_exists($data[$ch[1]],$meth)) {
74 $value = $data[$ch[1]] -> $meth();
75 if (is_array($value)) {
87 $value=substr((string)$value,$s,$l);
89 $format[$i]=ereg_replace($ch[0],$value,$format[$i]);
92 LSerror :: addErrorCode('fct_getFData_01',array('meth' => $meth,'obj' => $ch[1]));
100 while (ereg($expr,$format[$i],$ch)) {
110 $val=substr((string)$data,$s,$l);
115 $format[$i]=ereg_replace($ch[0],$val,$format[$i]);
119 while (ereg($expr,$format[$i],$ch)) {
120 if (method_exists($data,$meth)) {
121 $value = $data -> $meth($ch[1]);
122 if (is_array($value)) {
134 $value=substr((string)$value,$s,$l);
136 $format[$i]=ereg_replace($ch[0],$value,$format[$i]);
139 LSerror :: addErrorCode(0,getFData(_("Function 'getFData' : The method %{meth} of the object %{obj} doesn't exist."),array('meth' => $meth,'obj' => get_class($data))));
152 function getFieldInFormat($format) {
154 $expr="%{([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9])+)?}";
155 while (ereg($expr,$format,$ch)) {
157 $format=ereg_replace($ch[0],'',$format);
162 function loadDir($dir,$regexpr='^.*\.php$') {
163 if ($handle = opendir($dir)) {
164 while (false !== ($file = readdir($handle))) {
165 if (ereg($regexpr,$file)) {
166 require_once($dir.'/'.$file);
171 die(_('Folder not found').' : '.$dir);
177 function valid($obj) {
178 LSdebug('function valid() : ok');
182 function validPas($obj=null) {
183 LSdebug('function valid() : nok');
187 function return_data($data) {
191 $GLOBALS['LSdebug_fields']=array();
192 function LSdebug($data,$dump=false) {
196 $GLOBALS['LSdebug_fields'][]=ob_get_contents();
200 if (is_array($data)||is_object($data)) {
201 $GLOBALS['LSdebug_fields'][]=$data;
204 $GLOBALS['LSdebug_fields'][]="[$data]";
210 function LSdebug_print($return=false) {
211 if (( $GLOBALS['LSdebug_fields'] ) && (LSdebug)) {
213 foreach($GLOBALS['LSdebug_fields'] as $debug) {
214 if (is_array($debug)||is_object($debug)) {
215 $txt.='<li><pre>'.print_r($debug,true).'</pre></li>';
218 $txt.='<li>'.$debug.'</li>';
222 $GLOBALS['Smarty'] -> assign('LSdebug',$txt);
230 function LSdebugDefined() {
233 return (!empty($GLOBALS['LSdebug_fields']));
237 * Vérifie la compatibilite des DN
239 * Vérifie que les DNs sont dans la même branche de l'annuaire.
241 * @param[in] $dn Un premier DN.
242 * @param[in] $dn Un deuxième DN.
244 * @author Benjamin Renard <brenard@easter-eggs.com>
246 * @retval boolean true si les DN sont compatibles, false sinon.
248 function isCompatibleDNs($dn1,$dn2) {
249 $infos1=ldap_explode_dn($dn1,0);
252 $infos2=ldap_explode_dn($dn2,0);
255 if($infos2['count']>$infos1['count']) {
260 $infos1=array_reverse($infos1);
261 $infos2=array_reverse($infos2);
263 for($i=0;$i<$infos1['count'];$i++) {
264 if(($infos1[$i]==$infos2[$i])||(!isset($infos2[$i])))
273 * Fait la somme de DN
275 * Retourne un DN qui correspond au point de séparation des DN si les DN
276 * ne sont pas dans la meme dans la meme branche ou le dn le plus long sinon.
278 * @param[in] $dn Un premier DN.
279 * @param[in] $dn Un deuxième DN.
281 * @author Benjamin Renard <brenard@easter-eggs.com>
283 * @retval string Un DN (ou false si les DN ne sont pas valide)
285 function sumDn($dn1,$dn2) {
286 $infos1=ldap_explode_dn($dn1,0);
289 $infos2=ldap_explode_dn($dn2,0);
292 if($infos2['count']>$infos1['count']) {
297 $infos1=array_reverse($infos1);
298 $infos2=array_reverse($infos2);
302 for($i=0;$i<$infos1['count'];$i++) {
303 if(($infos1[$i]==$infos2[$i])||(!isset($infos2[$i]))) {
309 $basedn=$infos1[$i].','.$basedn;
318 function checkEmail($value,$domain=NULL,$checkDns=true) {
319 $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\-]+))$/';
321 if (!preg_match($regex, $value)) {
322 LSdebug('checkEmail : regex fail');
326 $nd = explode('@', $value);
330 if(is_array($domain)) {
331 if (!in_array($nd,$domain)) {
342 if ($checkDns && function_exists('checkdnsrr')) {
343 if (!(checkdnsrr($nd, 'MX') || checkdnsrr($nd, 'A'))) {
344 LSdebug('checkEmail : DNS fail');
352 function generatePassword($chars=NULL,$lenght=NULL) {
356 if (is_array($chars)) {
358 foreach($chars as $chs) {
359 if (!is_array($chs)) {
360 $chs=array('chars' => $chs);
362 if (!is_int($chs['nb'])) {
365 $retval.=aleaChar($chs['chars'],$chs['nb']);
367 $add = ($lenght-strlen($retval));
369 $retval .= aleaChar($chars,$add);
371 return str_shuffle($retval);
373 return aleaChar($chars,$lenght);
377 function aleaChar($chars=NULL,$lenght=1) {
378 if (is_array($chars)) {
380 foreach($chars as $chs) {
381 if (is_string($chs)) {
384 else if (is_string($chs['chars'])) {
385 $nchars.=$chs['chars'];
388 if(strlen($chars)>0) {
396 $chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-';
398 $nbChars=strlen($chars);
400 if(is_int($lenght)) {
401 for ($i=0;$i<$lenght;$i++) {
402 $retval.=$chars[rand(0,$nbChars-1)];
408 function compareDn($a,$b) {
409 if (substr_count($a,',') > substr_count($b,','))
415 function LSlog($msg) {
416 if ($GLOBALS['LSlog']['enable']) {
419 $LSlogFile=fopen($GLOBALS['LSlog']['filename'],'a');
421 fwrite($LSlogFile,$_SERVER['REQUEST_URI']." : ".$msg."\n");
426 if (isset($GLOBALS['LSlang'][$msg])) {
427 return $GLOBALS['LSlang'][$msg];
432 function tr($msg,$key=null) {
433 $val = $GLOBALS['Smarty']->get_template_vars($msg);
436 if (is_array($val)) {