Jump to content

Cool LDAP class for AD


countnikon

Recommended Posts

I built this class for LDAP authentication and Schema Searches.  I thought you all might like it.  It also handle more than one OU which all the LDAP classes I've seen do not handle.  Here it is for what it's worth.

[code]
<?PHP
/*
******************************************************************************************
LDAP Class                                                                               
Class Created 7-5-06                                                                     
This class allows for LDAP searches and authentication against AD.                       
You can use a foreach loop in your code that calls this class to display all entries     
------------------------------------------------------------------------------------------
Example LDAP Search.                                                                     
------------------------------------------------------------------------------------------
$ouArray = array('acct','sales','purchasing','exec','it');                                       
$ldap = new ldap("dc.domain.com","389","dc=domain,dc=com"); 
$ldap->ldapConn();                                                                       
$bind=$ldap->ldapBind("ldapuser","ldappass");                                         
if($bind==false)                                                                         
  echo "Bind Failed<br>";
//This goes as follows
//ldapSearch('what you want to pull','self explanitory','your ou array','what you are searching against')
//you can search against anything in the schema.                                                               
$description = $ldap->ldapSearch("description","$usrname",$ouArray,"samaccountname");   
if($description)                                                                         
{                                                                                       
  foreach($description as $output)                                                       
    $company=$output;                                                                   
}                                                                                       
------------------------------------------------------------------------------------------
Example Ldap Authenticate                                                               
------------------------------------------------------------------------------------------
$usrname=$_POST['usrname'];                                                             
$pass=$_POST['pass'];                                                                   
$ldap = new ldap("dc.domain.com","389","dc=domain-systems,dc=com"); 
$ldap->ldapConn();                                                                       
$auth = $ldap->ldapAuthenticate("$usrname","$pass");                                     
if($auth)                                                                               
  echo "Successful Authentication<br>";                                                 
else                                                                                     
  echo "Authentication Failed.";                                                         
------------------------------------------------------------------------------------------
Modifications                                                                           
------------------------------------------------------------------------------------------
******************************************************************************************
*/
class ldap
{
  var $ldapConn; //ldap connection storage variable
  var $ldapBind; //ldap bind storage variable
  var $entries;  //ldap entries variable
  var $ldapLookupUser;
  var $ldapLookupPass;
  var $server;
  var $port;
  var $by;
  var $search;
  var $baseDN;
  //Function to create the ldap object
  function ldap($server,$port,$baseDN)
  {
    $this->server=$server; //sets the dc server
    $this->port=$port; //sets the port to connect to AD
    $this->baseDN=$baseDN; //Sets the base DN for LDAP searches
  }
  //connects to the AD server
  function ldapConn()
  {
    //connects to AD server
    $this->ldapConn = @ldap_connect($this->server,$this->port);
    return $this->ldapConn;
  }
  //Binds to the AD server so you can do lookups against it
  function ldapBind($ldapLookupUser,$ldapLookupPass)
  {
    if(@ldap_bind($this->ldapConn,$ldapLookupUser,$ldapLookupPass))
    {
  $this->ldapBind = @ldap_bind($this->ldapconn,$ldapLookupUser,$ldapLookupPass);
  //returns true if you are able to bind
  return true;
}
    else
      return false;
  }
  //Authenticates a User against AD
  function ldapAuthenticate($usrname,$password)
  {
    if(@ldap_bind($this->ldapConn,$username,$password))
      return true;
    else
      return false;
  }
  //Searches the ldap schema
  function ldapSearch($by,$search,$ous,$searchby)
  {
    $c=0;
    foreach($ous as $ou) //This foreach loop allows the searching through multiple OU's'
    {
      /*This line searches the AD Schema. 
      It is setup so that you can search for any schema item by any schema item.
      */
  $read=ldap_search($this->ldapConn,"ou=$ou,$this->baseDN", "$searchby=*$search*");
  //This line reads in the entries for output
      $entries = ldap_get_entries($this->ldapConn, $read);
      //Loops through the entries and puts them in the array values
      for ($i=0; $i<$entries["count"]; $i++)
      {
        if($entries[$i][$by][0])
          $values[$c]=$entries[$i][$by][0];
        $c++;
      }
}
    return $values; //returns the values of the search
  }
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/15257-cool-ldap-class-for-ad/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.