123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- class ActiveDirectory
- {
- public $server,$port,$login,$password,$userRoot,$groupRoot,$domain,$datasource,$protocolVersion;
-
-
-
- public function connect($login=false,$password=false){
- putenv('LDAPTLS_REQCERT=never');
- if($this->server==null || $this->port==null || $this->userRoot==null) throw new Exception('Paramètres de connexion manquants',400);
- $this->datasource = ldap_connect($this->server,$this->port);
- if(!$this->datasource) throw new Exception('Connexion échouée', 400);
- ldap_set_option($this->datasource,LDAP_OPT_PROTOCOL_VERSION,$this->protocolVersion);
- ldap_set_option($this->datasource, LDAP_OPT_REFERRALS, 0);
- if(@ldap_bind($this->datasource,$login,$password) == false) throw new Exception('Identifiant ou mot de passe incorrect', 401);
- }
-
-
- public function userFromCn($cn){
- $entries= $this->search($this->userRoot,"(distinguishedname=$cn)");
- return $entries;
- }
-
- public function cnFromLogin($login){
- $entries= $this->search($this->userRoot,'(samAccountName='.$login.')');
- return (isset($entries[0])) ? $entries[0]['dn'] : false;
- }
-
- function recursiveGroups(&$groups,$groupCN){
- $entries = $this->search($this->groupRoot,"(member=".$groupCN.")");
- if(count($entries)!=0 && $entries['count']!=0) {
- if(isset($entries[0])){
- $groups[] = $entries[0]['name'][0];
- $parentCN = $entries[0]['distinguishedname'][0];
- $this->recursiveGroups($groups,$parentCN);
- }
- }
- }
-
-
- public function search($dn,$filter="(objectClass=user)"){
- $roots = (substr_count($dn, ';') > 0) ? explode(';', $dn) : array($dn);
- $infos = array('count' => 0);
- foreach ($roots as $root) {
- if(empty($root)) continue;
- $sr = ldap_search($this->datasource, $root, $filter);
- if(!$sr) throw new Exception("Erreur lors de la recherche. Vérifiez que les racines existent");
- $info = ldap_get_entries($this->datasource, $sr);
- if($info['count'] == 0) continue;
- $infos['count'] += $info['count'];
- for($i=0;$i<$info['count'];$i++){
- array_push($infos, $info[$i]);
- }
- }
- $infos = array_unique($infos,SORT_REGULAR);
- return $infos;
- }
-
-
- public function populate($dn){
- return $this->search($dn,"(&(samAccountName=*)(objectClass=user))");
- }
- public function get_domain_name($login){
- $user = $this->search($this->userRoot,'(samAccountName='.$login.')');
- return $user[0]['dn'];
- }
- public function set($dn,$entry,$value){
- putenv('LDAPTLS_REQCERT=never');
- if($value != ''){
- $attributes[$entry][0] = $value;
- return ldap_modify($this->datasource,$dn,$attributes);
- }else{
- $attributes[$entry] = array();
- return ldap_mod_del($this->datasource,$dn,$attributes);
- }
- }
- public function change_password( $userDn , $newPassword ) {
- if (!ldap_mod_replace($this->datasource, $userDn , self::encrypt_password($newPassword))) throw new Exception("Impossible de modifier le mot de passe : ".ldap_error($this->datasource));
- }
- public static function encrypt_password( $newPassword ) {
- $newPassword = "\"" . $newPassword . "\"";
- $len = strlen( $newPassword );
- $newPassw = "";
- for ( $i = 0; $i < $len; $i++ ){
- $newPassw .= "{$newPassword{$i}}\000";
- }
- return array("unicodePwd" => $newPassw);
- }
-
-
- public function disconnect(){
- if($this->datasource!=null){
- @ldap_close($this->datasource);
- }
- }
-
-
- }
- ?>
|