Outils pour utilisateurs

Outils du site


braveo:classeldap

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
braveo:classeldap [2012/01/04 11:33] – modification externe 127.0.0.1braveo:classeldap [2024/04/16 22:20] (Version actuelle) – modification externe 127.0.0.1
Ligne 1: Ligne 1:
 +====  LDAP.classe.php ====
 +Cette classe PHP nous servira pour le différente opération à effectuer sur le serveur LDAP. 
 + 
  
 +<code PHP>
 +
 +<?php
 +/******************************************************************************/
 +/*                                                                            */
 +/*      _ ____ _ ____ ____ _  _    ___  _  _    _    _ ___  ____ ____       */
 +/*     |\/| |__| | [__  |  | |\ |    |  \ |  |    |    | |__] |__/ |___       */
 +/*      | |  | | ___] |__| | \|    |__/ |__|    |___ | |__] |  \ |___       */
 +/*                                                                            */
 +/******************************************************************************/
 +/*                                                                            */
 +/* Titre          : Classe LDAP                                               */
 +/*                                                                            */
 +/* URL            : http://mdl29.net                                          */
 +/* Auteur :       : Arnaud                                                    */
 +/* Date édition   : Février 2010                                              */
 +/*                                                                            */
 +/* Licence         : GNU General Public License                               */
 +/******************************************************************************/
 +
 +class LDAP
 +{
 + // Adresse du serveur LDAP
 + private $serveur;
 +
 + // Port du serveur 
 + private $port;
 +
 + // Base Dn pour l'interrogation du ldap
 + private $basedndn;
 +
 + // Base Dn de connexion pour les modifications au niveau du ldap 
 + private $rootdn;
 +
 + // Mot de passe du LDAP
 + private  $rootpwd;
 +
 + // Attribut de connexion
 + private $connexion;
 +
 + // Attribut d'identification
 + private $identification;
 +
 +        // Attribut profil utilisateur 
 +        private $info_user;
 +        
 +       
 +
 +
 +
 + // Définition du constructeur
 + public function __construct($serveur,$port,$basedn,$rootdn,$rootpwd)
 +   {       
 +                
 + // Vérification des variables
 + if (empty ($serveur) || empty ($port) || empty ($basedn)  || empty ($rootdn)  || empty ($rootpwd)  ){
 + exit;
 + }
 + else {
 + // Initialisation des attributs
 + $this->serveur =  $serveur; 
 +                        $this->port    =  $port;
 +                        $this->basedn  =  $basedn;
 +                        $this->rootdn  =  $rootdn;
 + $this->rootpwd =  $rootpwd;
 +                        
 + }
 + }  // fin de la méthode
 +
 +
 + // Méthode de connexion
 + public function Connexion()
 + {
 + // Connexion au LDAP
 + $this->connexion = @ldap_connect($this->serveur,$this->port);
 +
 + ldap_set_option($this->connexion, LDAP_OPT_PROTOCOL_VERSION, 3);  // version du protocole : 3
 +
 + // Vérification de la connexion
 + if (!$this->connexion)
 + {
 + // si erreur 
 + return false;
 +
 + else 
 + {
 + return true;
 +       }
 + }
 +
 + // Méthode Identification()
 + public function Identification ()
 + {
 + // Identification sur le serveur 
 + $ident = @ldap_bind($this->connexion, $this->rootdn, $this->rootpwd);
 +
 +                //Vérification de la connexion
 + if ($ident) 
 + {
 + $this->identification = $ident;
 + return true;
 +     } 
 + else 
 + {
 + return false;   // Echec d'identification
 +     }
 +    }
 +
 +
 +
 +
 + /* Méthode qui retourne true si un utilisateur est enregistré dans le ldap
 +    sinon elle renvoie false   */
 + 
 + public function Verif_utilisateur($utilisateur)
 + {
 +                $champ_recherche = "uid=$utilisateur";
 +
 + $search = ldap_search($this->connexion, $this->basedn, $champ_recherche);  
 + if ($search != false ) 
 +                 {
 +                    $info = ldap_get_entries($this->connexion, $search); 
 +                   
 +                   if ( $info["count"] == 1 )
 +
 +                    {  
 +                       $this->info_user = $info;
 +                       return true;
 +                    }
 +                }
 +         else {
 + 
 +          return false;
 +         }
 +
 +      } // fin de la Méthode
 +
 +
 + /* Méthode qui récupère les informations  utilisateur 
 +    sinon elle renvoie false   */
 + 
 + public function Informations_utilisateur($utilisateur)
 + {
 +             // si l'utilisateur existe 
 +
 +                   if ($this->Verif_utilisateur($utilisateur))
 +                    { 
 +                    $profil = array (
 +     'identifiant' =>  $this->info_user[0]["cn"][0],
 +     'nom' => $this->info_user[0]["sn"][0],
 +     'prenom' => $this->info_user[0]["givenname"][0],
 +                    'pwd' => "",
 +                    'mail' => $this->info_user[0]["mail"][0],
 +     'mail_contact' => $this->info_user[0]["mailalternateaddress"][0],
 +     'blog' => $this->info_user[0]["labeleduri"][0],
 + 'chimere' => $this->info_user[0]["labeleduri"][1],
 +     'date_creation' => $this->info_user[0]["accountstatus"][0]);
 +                    
 +                    return $profil;   // on retourne un tableau
 +                   }
 +                  else {
 + 
 +              return false;
 +                       }
 +
 +      } // fin de la Méthode
 +
 +
 +
 +
 + /* Méthode qui renvoie le nombre d'inscrits dans le ldap */
 + 
 + public function Nombre_utilisateurs()
 + {
 +             $champ_recherche = "uid=*";
 +
 +             $search = ldap_search($this->connexion, $this->basedn, $champ_recherche);  
 +             if ($search != false )
 + {
 +                  $nombre = ldap_count_entries($this->connexion, $search); 
 +                    
 +                 if ($nombre != false ) 
 +                   { return $nombre;  
 +                   }
 +               }
 +               
 +          return false;
 +        } // fin de la Méthode
 +
 +
 + // Méthode qui vérifie le mot de passe d'un utilisateur
 +
 +   public function Verifier_mot_de_passe($utilisateur,$pwd)
 +    {
 +
 +       // on vérifie si le mot de passe est bon
 +       if (@ldap_bind($this->connexion,"uid=".$utilisateur.",".$this->basedn,$pwd))
 +         
 +           {  
 +            // @ldap_bind($this->connexion, $this->rootdn, $this->rootpwd); 
 +             return  true;
 +           }
 +
 +      
 +             return false;
 +      
 +    }
 +
 +
 + // Méthode qui permet de changer son mot de passe
 +    
 +  public function Modifier_mot_de_passe($utilisateur,$ancien_pwd,$nouveau_pwd)
 +     {  
 +
 +        if ( $this->Verifier_mot_de_passe($utilisateur,$ancien_pwd) || $ancien_pwd===true ) // $ancien_pwd vaut true si l'utilisateur a oublié son pwd et est autorisé à le changer
 +            {
 +             // si le mot de passe actuel est valide, on peut le changer
 +             if($ancien_pwd===true){ $this->Identification();} //A cause du bind qui bug sinon
 +             $entry['userPassword']=utf8_encode('{SHA}'.base64_encode(pack('H*',sha1($nouveau_pwd))));
 +
 +             // on ajoute le nouveau mot de passe dans le ldap
 +            if (@ldap_modify($this->connexion,utf8_encode("uid=".$utilisateur.",".$this->basedn),$entry))
 +
 +               
 +             return  true;
 +          }
 +         return false;       
 +
 +     }
 +
 +
 +     public function Ajouter_url_blog($utilisateur, $url_blog)
 +     {
 +  
 +       $entry['labeledURI']=utf8_encode($url_blog);
 +              
 +       // on modifie l'attribut dans le ldap
 +     if (ldap_modify($this->connexion,utf8_encode("uid=".$utilisateur.",".$this->basedn),$entry))
 +          {
 +            return true;
 +          }
 +
 +      return false;
 +  }
 +
 +
 +
 +      // Ajout d'un utilisateur dans le ldap avec uid = cn
 +
 +     public function Ajout_membre($prenom, $nom, $identifiant, $motdepasse, $mail, $url_blog)
 +      {    
 + $espace = " ";
 + $nom_prenom = $prenom . $espace . $nom;     // concaténation
 + $les_initials = $prenom[0].$nom[0];        // On crée les initials 
 + $mdl29 = "@mdl29.net";
 + $adr_mail = $identifiant.$mdl29;
 +
 + $entry['cn']=utf8_encode($identifiant);
 + $entry['displayName']=utf8_encode($nom_prenom);
 + $entry['givenName']=utf8_encode($prenom);
 + $entry['initials']=utf8_encode($les_initials);
 + $entry['labeledURI']=utf8_encode($url_blog);
 + $entry['mail']=utf8_encode($adr_mail);
 + $entry['mailhost']=utf8_encode("mail.mdl29.net");
 + $entry['mailAlternateAddress']=utf8_encode($mail);
 + $entry['preferredlanguage']=utf8_encode("fr, en-gb;q=0.8, en;q=0.7");
 + $entry['ObjectClass'][1]=utf8_encode("person");
 + $entry['ObjectClass'][2]=utf8_encode("organizationalPerson");
 + $entry['ObjectClass'][3]=utf8_encode("inetOrgPerson");
 + $entry['ObjectClass'][0]=utf8_encode("top");
 + $entry['ObjectClass'][4]=utf8_encode("qmailUser");
 + $entry['sn']=utf8_encode($nom);
 + $entry['uid']=utf8_encode($identifiant);
 + $entry['userPassword']=utf8_encode('{SHA}'.base64_encode(pack('H*',sha1($motdepasse))));
 +                $entry['accountstatus']=utf8_encode(strftime(" %d/%m/%y "));
 +
 +              // on ajoute le compte dans le ldap
 +              $res=@ldap_add($this->connexion,utf8_encode("uid=".$identifiant.",".$this->basedn),$entry); 
 +              return $res;
 +        }   
 +
 +   // on passe en paramètre $attribut qui est un tableau
 +   public function Modifier_profil($utilisateur, $attribut) {
 +
 +
 +     
 +    // on modifie le prénom
 +    if (array_key_exists('prenom', $attribut))
 +      { 
 +        $entry['givenName']=utf8_encode($attribut['prenom']);
 +      }
 +
 +
 +    // on modifie le nom
 +    if (array_key_exists('nom', $attribut))
 +      { 
 +
 +        $entry['sn']=utf8_encode($attribut['nom']);  
 +      }
 +
 +   // on modifie le displayname
 +  if (array_key_exists('nom_prenom', $attribut))
 +       
 +        $entry['displayName']=utf8_encode($attribut['nom_prenom']);    
 +       }
 +
 +
 +    // on modifie les initials
 +  if (array_key_exists('initials', $attribut))
 +       
 +        $entry['initials']=utf8_encode($attribut['initials']);    
 +       }
 +
 +
 +     // on modifie l'email de contact
 +
 +  if (array_key_exists('mail_contact', $attribut))
 +      { 
 +        $entry['mailAlternateAddress']=utf8_encode($attribut['mail_contact']);   
 +       }
 +
 +   // url du blog et chimere
 +  if (array_key_exists('blog', $attribut) || array_key_exists('chimere', $attribut))
 +      { 
 +        $entry['labeledURI']=Array( (array_key_exists('blog', $attribut)) ? utf8_encode($attribut['blog']) : $_SESSION['profil_utilisateur']['blog'] ,
 +        (array_key_exists('chimere', $attribut)) ? utf8_encode($attribut['chimere']) :  $_SESSION['profil_utilisateur']['chimere']
 +        );
 +       }
 +
 +   if (@ldap_modify($this->connexion,utf8_encode("uid=".$utilisateur.",".$this->basedn),$entry))
 +          { 
 +            return true;
 +          }
 +
 +   return false;
 +
 +   } // fin méthode
 +
 +
 +
 +
 +      // Méthode Deconnexion()
 +     
 + public function Deconnexion(){
 +
 + // Déconnexion du serveur
 + $deconnexion = @ldap_unbind($this->connexion);
 + if(!$deconnexion)
 + @ldap_close($this->connexion);
 + } //fin de déconnexion
 +
 + } //fin de la classe
 +?>
 +
 +</code>
braveo/classeldap.txt · Dernière modification : 2024/04/16 22:20 de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki