_domain = $domain; $this->_login = $login; $this->_password = $password; $this->_ssl = ($ssl == 'true'); if( !empty($hostname) ) $this->_hostname = $hostname; // Connect to the AD/LDAP server as the username/password if ($this->_ssl) { if( isset($this->_hostname) ) $this->_conn = ldap_connect("ldaps://".$this->_hostname); else $this->_conn = ldap_connect("ldaps://".$this->_domain); } else { if( isset($this->_hostname) ) $this->_conn = ldap_connect($this->_hostname); else $this->_conn = ldap_connect($this->_domain); } // Set some ldap options for talking to AD ldap_set_option($this->_conn, LDAP_OPT_PROTOCOL_VERSION, 3); ldap_set_option($this->_conn, LDAP_OPT_REFERRALS, 0); // Bind as a domain admin if they've set it up if ($this->_login!=NULL && $this->_password!=NULL){ $this->_bind = @ldap_bind($this->_conn,$this->_login."@".$this->_domain,$this->_password); if (!$this->_bind){ if ($this->_ssl) { throw new Exception ('FATAL: AD bind failed. Either the LDAPS connection failed or the login credentials are incorrect.'); } else { throw new Exception ("FATAL: AD bind failed. Check the login credentials."); } } } } /** * Default Destructor * * Closes the LDAP connection * * @return void */ function __destruct(){ ldap_close($this->_conn); } /** * Validate a user's login credentials * * @param string $username A user's AD username * @param string $password A user's AD password * @param bool optional $prevent_rebind * @return bool */ public function authenticate($login,$password,$prevent_rebind=false){ // Prevent null binding if ($login==NULL || $password==NULL){ return (false); } // Bind as the user try{ $this->_bind = @ldap_bind($this->_conn,$login."@".$this->_domain,$password); } catch(Exception $e){} if (!$this->_bind){ return (false); } // Cnce we've checked their details, kick back into admin mode if we have it if ($this->_login!=NULL && !$prevent_rebind){ $this->_bind = @ldap_bind($this->_conn,$this->_login."@".$this->_domain,$this->_password); if (!$this->_bind){ echo ("FATAL: AD rebind failed."); exit(); } // This should never happen in theory } return (true); } //***************************************************************************************************************** // GROUP FUNCTIONS /** * Group Information. Returns an array of information about a group. * The group name is the distinguishedname * * @param string $group_dn The group distinguishedname to retrieve info about * @param array $fields Fields to retrieve * @return array */ public function group_info($group_dn,$fields=array(),$dn='',$filter=''){ if ($group_dn==NULL){ return (false); } if (!$this->_bind){ return (false); } if(count($fields) < 1) $fields[] = "distinguishedname"; if(empty($dn)) $dn="DC=".str_replace(".",",DC=",$this->_domain); $entries = array(); $filter="(&(objectCategory=group)(distinguishedName=".$group_dn.")".$filter.")"; $sr=ldap_search($this->_conn,$dn,$filter,$fields); $entries = ldap_get_entries($this->_conn, $sr); if($entries['count'] != 1) return array(); $ad_info_group = array(); foreach($fields as $fd) { if( $fd == 'memberof') { unset($entries[0][$fd]['count']); $ad_info_group[$fd] = $entries[0][$fd]; } else if( $fd == 'objectguid' && !empty($entries[0][$fd][0]) ) { $ad_info_group[$fd] = bin2hex($entries[0][$fd][0]); } else if( $fd == 'objectguid' && empty($entries[0][$fd][0]) ) { //Le groupe n'a pas de objectguid (pb sur l'annuaire) return array(); } else if( $fd == 'member') { unset($entries[0][$fd]['count']); $ad_info_group[$fd] = $entries[0][$fd]; } else { $ad_info_group[$fd] = $entries[0][$fd][0]; } } return $ad_info_group; } /** * Return a list of all users in AD * * @param bool $include_desc Return a description of the user * @param string $search Search parameter * @param bool $sorted Sort the user accounts * @return array */ public function all_users($fields=array(),$dn='',$filter=''){ if(empty($dn)) $dn="DC=".str_replace(".",",DC=",$this->_domain); if (!$this->_bind){ return (false); } if(count($fields) < 1) $fields[] = "distinguishedname"; $entries = array(); $filter = "(&(objectClass=user)(objectCategory=person)".$filter.")"; $sr=ldap_search($this->_conn,$dn,$filter,$fields); $entries = array_merge(ldap_get_entries($this->_conn, $sr),$entries); $ad_users = array(); for ($i=0; $i < (count($entries)-1); $i++) { foreach($fields as $fd) { if( $fd == 'objectguid' && !empty($entries[$i][$fd][0]) ) { $ad_users[$i][$fd] = bin2hex($entries[$i][$fd][0]); } else if( $fd == 'objectguid' && empty($entries[$i][$fd][0]) ) { //L'utilisateur n'a pas de objectguid (pb sur l'annuaire) unset($ad_users[$i]); break; } else if( $fd == 'memberof') { unset($entries[$i][$fd]['count']); $ad_users[$i][$fd] = $entries[$i][$fd]; } else if( $fd == 'useraccountcontrol') { if( ($entries[$i][$fd][0] & 2) == 0) $ad_users[$i][$fd] = 'Y'; else $ad_users[$i][$fd] = 'N'; } else { $ad_users[$i][$fd] = $entries[$i][$fd][0]; } } } return $ad_users; } public function all_groups($fields=array(),$dn='',$filter=''){ if(empty($dn)) $dn="DC=".str_replace(".",",DC=",$this->_domain); if (!$this->_bind){ return (false); } if(count($fields) < 1) $fields[]="distinguishedname"; $entries = array(); //Search for each filter $filter = "(&(objectClass=group)".$filter.")"; $sr=ldap_search($this->_conn,$dn,$filter,$fields); $entries = ldap_get_entries($this->_conn, $sr); for ($i=0; $i< ( count($entries) -1); $i++) { foreach($fields as $fd) { if( $fd == 'objectguid' && !empty($entries[$i][$fd][0]) ) $ad_groups[$i][$fd] = bin2hex($entries[$i][$fd][0]); else if( $fd == 'objectguid' && empty($entries[$i][$fd][0]) ) { //Le groupe n'a pas de objectguid (pb sur l'annuaire) unset($ad_groups[$i]); break; } else if( $fd == 'memberof') { unset($entries[$i][$fd]['count']); $ad_groups[$i][$fd] = $entries[$i][$fd]; } else if( $fd == 'member') { unset($entries[$i][$fd]['count']); $ad_groups[$i][$fd] = $entries[$i][$fd]; } else { $ad_groups[$i][$fd] = $entries[$i][$fd][0]; } } } return ($ad_groups); } } ?>