* * @param[in] $format string Format de la chaine * @param[in] $data mixed Les données pour composés la chaine * Ce paramètre peut être un tableau de string, une string, * une tableau d'objet ou un objet. * @param[in] $meth string Le nom de la methode de/des objet(s) à appeler pour * obtenir la valeur de remplacement dans la chaine formatée. * * @retval string La chaine formatée */ function getFData($format,$data,$meth=NULL) { $unique=false; $expr="%{([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9])+)?}"; if(!is_array($format)) { $format=array($format); $unique=true; } for($i=0;$i $meth(); if (is_array($value)) { $value = $value[0]; } if($ch[3]) { if ($ch[5]) { $s=$ch[3]; $l=$ch[5]; } else { $s=0; $l=$ch[3]; } $value=substr((string)$value,$s,$l); } $format[$i]=ereg_replace($ch[0],$value,$format[$i]); } else { $GLOBALS['LSerror'] -> addErrorCode(901,array('meth' => $meth,'obj' => $ch[1])); break; } } } } else { if ($meth==NULL) { while (ereg($expr,$format[$i],$ch)) { if($ch[3]) { if ($ch[5]) { $s=$ch[3]; $l=$ch[5]; } else { $s=0; $l=$ch[3]; } $val=substr((string)$data,$s,$l); } else { $val=$data; } $format[$i]=ereg_replace($ch[0],$val,$format[$i]); } } else { while (ereg($expr,$format[$i],$ch)) { if (method_exists($data,$meth)) { $value = $data -> $meth($ch[1]); if (is_array($value)) { $value = $value[0]; } if($ch[3]) { if ($ch[5]) { $s=$ch[3]; $l=$ch[5]; } else { $s=0; $l=$ch[3]; } $value=substr((string)$value,$s,$l); } $format[$i]=ereg_replace($ch[0],$value,$format[$i]); } else { $GLOBALS['LSerror'] -> addErrorCode(901,array('meth' => $meth,'obj' => get_class($data))); break; } } } } } if($unique) { return $format[0]; } return $format; } function getFieldInFormat($format) { $fields=array(); $expr="%{([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9])+)?}"; while (ereg($expr,$format,$ch)) { $fields[]=$ch[1]; $format=ereg_replace($ch[0],'',$format); } return $fields; } function loadDir($dir,$regexpr='^.*\.php$') { if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if (ereg($regexpr,$file)) { require_once($dir.'/'.$file); } } } else { die(_('Dossier introuvable ('.$dir.').')); } return true; } function valid($obj) { LSdebug('Validation : ok'); return true; } function return_data($data) { return $data; } function LSdebug($data,$dump=false) { if ($dump) { ob_start(); var_dump($data); $GLOBALS['LSdebug']['fields'][]=ob_get_contents(); ob_end_clean(); } else { if (is_array($data)||is_object($data)) { $GLOBALS['LSdebug']['fields'][]=$data; } else { $GLOBALS['LSdebug']['fields'][]="[$data]"; } } return true; } function LSdebug_print($return=false) { if (( $GLOBALS['LSdebug']['fields'] ) && ( $GLOBALS['LSdebug']['active'] )) { $txt=''; $GLOBALS['Smarty'] -> assign('LSdebug',$txt); if ($return) { return $txt; } } return; } function LSdebugDefined() { if (!$GLOBALS['LSdebug']['active']) return; return (!empty($GLOBALS['LSdebug']['fields'])); } /** * Vérifie la compatibilite des DN * * Vérifie que les DNs sont dans la même branche de l'annuaire. * * @param[in] $dn Un premier DN. * @param[in] $dn Un deuxième DN. * * @author Benjamin Renard * * @retval boolean true si les DN sont compatibles, false sinon. */ 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); for($i=0;$i<$infos1['count'];$i++) { if(($infos1[$i]==$infos2[$i])||(!isset($infos2[$i]))) continue; else return false; } return true; } /** * 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. * * @param[in] $dn Un premier DN. * @param[in] $dn Un deuxième DN. * * @author Benjamin Renard * * @retval string Un DN (ou false si les DN ne sont pas valide) */ 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; } else $basedn=$infos1[$i].','.$basedn; } else { return $basedn; } } return $basedn; } function checkEmail($value,$domain=NULL,$checkDns=true) { if (!$domain) { $domain = '((\[(((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\-]+))'; } $regex = '/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@'.$domain.'$/'; if (!preg_match($regex, $value)) { LSdebug('checkEmail : regex fail'); return false; } if ($checkDns && function_exists('checkdnsrr')) { $tokens = explode('@', $value); if (!(checkdnsrr($tokens[1], 'MX') || checkdnsrr($tokens[1], 'A'))) { LSdebug('checkEmail : DNS fail'); return false; } } return true; } function generatePassword($chars=NULL,$lenght=NULL) { if (!$chars) { $chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-'; } $nbChars=strlen($chars); if (!$lenght) { $lenght=8; } $retVal=''; for($i=0;$i<$lenght;$i++){ $retVal.=$chars[rand(0,$nbChars-1)]; } return $retVal; } function compareDn($a,$b) { if (substr_count($a,',') > substr_count($b,',')) return -1; else return 1; } ?>