<?
//Looking User Information into the Windows 2003 Active Directory
//Configurarion Section
$ldap_server="some.ip.addrr"; //The Active Directory Server
$ad_domainNB="someNBdomain"; //The Netbios name of the domain
$ad_domain="domain.com";
$ad_user="user1"; //Active Directory Valid User
$ad_userPass="pass"; //Active Directory Valid User Password
$user="user2"; //Active Directory Valid User looking for
$domain_parts = explode(".",$ad_domain); //Extract every part of the domain
$ad_base_search="";
for ($x=0;$x<sizeof($domain_parts);$x++) //Upper Case Parts and Build a base search string
{
$domain_parts[$x] = strtoupper($domain_parts[$x]);
if ($x==sizeof($domain_parts)-1)
{
$ad_base_search .= "dc=" . $domain_parts[$x];
}
else
{
$ad_base_search .= "dc=" . $domain_parts[$x] . ",";
}
}
$ds=ldap_connect($ldap_server);//Conecting to the Server
if ($ds) //if Conection is ok we continue
{
//Setting the Ldap Query options
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
//Setting the Ldap Query options
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
//Validation with the Active Directory
@$r=ldap_bind($ds, $ad_domainNB . '\\' . $ad_user,$ad_userPass);
//Querying the Active Directory
@$sr=ldap_search($ds,$ad_base_search, "cn=" . $user);
//Fetching the result
@$info = ldap_get_entries($ds, $sr);
//Extracting the data
list($cn,$ou,$dc) = split(",",$info[0]["dn"]);
//Extracting the data
list($basura,$depto) = split("=",$ou);
//Formating Data Some Latin chars may not propertly shown
$nombre = utf8_decode($info[0]["displayname"][0]);
//Formating Data Some Latin chars may not propertly shown
$departamento = utf8_decode($depto);
}
// Closing LDAP Conection
@ldap_close($ds);