2007-03-29 18:10:14 +02:00
|
|
|
<?php
|
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (C) 2007 Easter-eggs
|
|
|
|
* http://ldapsaisie.labs.libre-entreprise.org
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
*
|
|
|
|
* @param[in] $format string Format de la chaine
|
2008-04-25 16:09:27 +02:00
|
|
|
* @param[in] $data mixed Les données pour composés la chaine
|
|
|
|
* 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.
|
2008-04-25 16:09:27 +02:00
|
|
|
* @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.
|
2007-03-29 18:10:14 +02:00
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* @retval 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;
|
2008-10-27 15:06:49 +01: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) {
|
2008-10-27 15:06:49 +01:00
|
|
|
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[$ch[1]],$s,$l);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$val=$data[$ch[1]];
|
|
|
|
}
|
|
|
|
$format[$i]=ereg_replace($ch[0],$val,$format[$i]);
|
2007-03-29 18:10:14 +02:00
|
|
|
}
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
else {
|
2008-10-27 15:06:49 +01:00
|
|
|
while (ereg($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];
|
|
|
|
}
|
2008-10-27 15:06:49 +01:00
|
|
|
if($ch[3]) {
|
|
|
|
if ($ch[5]) {
|
|
|
|
$s=$ch[3];
|
|
|
|
$l=$ch[5];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$s=0;
|
|
|
|
$l=$ch[3];
|
|
|
|
}
|
|
|
|
$value=substr((string)$value,$s,$l);
|
|
|
|
}
|
2008-07-29 15:45:02 +02:00
|
|
|
$format[$i]=ereg_replace($ch[0],$value,$format[$i]);
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$GLOBALS['LSerror'] -> addErrorCode(901,array('meth' => $meth,'obj' => $ch[1]));
|
|
|
|
break;
|
|
|
|
}
|
2007-03-29 18:10:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2007-11-15 19:07:24 +01:00
|
|
|
if ($meth==NULL) {
|
2008-10-27 15:06:49 +01:00
|
|
|
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]);
|
|
|
|
}
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
else {
|
2008-10-27 15:06:49 +01:00
|
|
|
while (ereg($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];
|
|
|
|
}
|
2008-10-27 15:06:49 +01:00
|
|
|
if($ch[3]) {
|
|
|
|
if ($ch[5]) {
|
|
|
|
$s=$ch[3];
|
|
|
|
$l=$ch[5];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$s=0;
|
|
|
|
$l=$ch[3];
|
|
|
|
}
|
|
|
|
$value=substr((string)$value,$s,$l);
|
|
|
|
}
|
2008-07-29 15:45:02 +02:00
|
|
|
$format[$i]=ereg_replace($ch[0],$value,$format[$i]);
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$GLOBALS['LSerror'] -> addErrorCode(901,array('meth' => $meth,'obj' => get_class($data)));
|
|
|
|
break;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-09 01:57:50 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-02-05 17:11:21 +01:00
|
|
|
function loadDir($dir,$regexpr='^.*\.php$') {
|
2008-02-08 18:39:24 +01:00
|
|
|
if ($handle = opendir($dir)) {
|
|
|
|
while (false !== ($file = readdir($handle))) {
|
|
|
|
if (ereg($regexpr,$file)) {
|
|
|
|
require_once($dir.'/'.$file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
die(_('Dossier introuvable ('.$dir.').'));
|
|
|
|
}
|
|
|
|
return true;
|
2008-02-05 17:11:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-29 18:10:14 +02:00
|
|
|
function valid($obj) {
|
2008-09-26 20:03:56 +02:00
|
|
|
LSdebug('Validation : ok');
|
2007-03-29 18:10:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function return_data($data) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2008-09-26 20:03:56 +02:00
|
|
|
function LSdebug($data,$dump=false) {
|
|
|
|
if ($dump) {
|
|
|
|
ob_start();
|
|
|
|
var_dump($data);
|
|
|
|
$GLOBALS['LSdebug']['fields'][]=ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
}
|
|
|
|
else {
|
2008-06-05 15:21:18 +02:00
|
|
|
if (is_array($data)||is_object($data)) {
|
2008-02-08 18:39:24 +01:00
|
|
|
$GLOBALS['LSdebug']['fields'][]=$data;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$GLOBALS['LSdebug']['fields'][]="[$data]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
|
2008-09-26 20:03:56 +02:00
|
|
|
function LSdebug_print($return=false) {
|
2008-02-08 18:39:24 +01:00
|
|
|
if (( $GLOBALS['LSdebug']['fields'] ) && ( $GLOBALS['LSdebug']['active'] )) {
|
|
|
|
$txt='<ul>';
|
|
|
|
foreach($GLOBALS['LSdebug']['fields'] as $debug) {
|
2008-06-05 15:21:18 +02:00
|
|
|
if (is_array($debug)||is_object($debug)) {
|
2008-02-08 18:39:24 +01:00
|
|
|
$txt.='<li><pre>'.print_r($debug,true).'</pre></li>';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$txt.='<li>'.$debug.'</li>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$txt.='</ul>';
|
|
|
|
$GLOBALS['Smarty'] -> assign('LSdebug',$txt);
|
2008-06-05 15:21:18 +02:00
|
|
|
if ($return) {
|
|
|
|
return $txt;
|
|
|
|
}
|
2008-02-08 18:39:24 +01:00
|
|
|
}
|
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() {
|
|
|
|
if (!$GLOBALS['LSdebug']['active'])
|
|
|
|
return;
|
|
|
|
return (!empty($GLOBALS['LSdebug']['fields']));
|
|
|
|
}
|
|
|
|
|
2008-02-08 18:39:24 +01:00
|
|
|
/**
|
2008-04-25 16:09:27 +02:00
|
|
|
* Vérifie la compatibilite des DN
|
2008-02-08 18:39:24 +01:00
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* Vérifie que les DNs sont dans la même branche de l'annuaire.
|
2008-02-08 18:39:24 +01:00
|
|
|
*
|
|
|
|
* @param[in] $dn Un premier DN.
|
2008-04-25 16:09:27 +02:00
|
|
|
* @param[in] $dn Un deuxième DN.
|
2008-02-08 18:39:24 +01:00
|
|
|
*
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* Retourne un DN qui correspond au point de séparation des DN si les DN
|
2008-02-08 18:39:24 +01:00
|
|
|
* ne sont pas dans la meme dans la meme branche ou le dn le plus long sinon.
|
|
|
|
*
|
|
|
|
* @param[in] $dn Un premier DN.
|
2008-04-25 16:09:27 +02:00
|
|
|
* @param[in] $dn Un deuxième DN.
|
2008-02-08 18:39:24 +01:00
|
|
|
*
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
2008-06-05 15:21:18 +02:00
|
|
|
|
2008-10-07 16:59:23 +02:00
|
|
|
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.'$/';
|
2008-06-05 15:21:18 +02:00
|
|
|
|
|
|
|
if (!preg_match($regex, $value)) {
|
2008-09-26 20:03:56 +02:00
|
|
|
LSdebug('checkEmail : regex fail');
|
2008-06-05 15:21:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($checkDns && function_exists('checkdnsrr')) {
|
|
|
|
$tokens = explode('@', $value);
|
|
|
|
if (!(checkdnsrr($tokens[1], 'MX') || checkdnsrr($tokens[1], 'A'))) {
|
2008-09-26 20:03:56 +02:00
|
|
|
LSdebug('checkEmail : DNS fail');
|
2008-06-05 15:21:18 +02:00
|
|
|
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;
|
|
|
|
}
|
2008-06-18 14:27:35 +02:00
|
|
|
|
|
|
|
function compareDn($a,$b) {
|
|
|
|
if (substr_count($a,',') > substr_count($b,','))
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
2008-02-08 18:39:24 +01:00
|
|
|
|
2007-11-15 19:07:24 +01:00
|
|
|
?>
|