hey yeah, here's my ldap abstraction classes. there's a syntax description up near the top. you'll need to
a) take out any refereneces to $sysdefobj, which is my global system defaults thing, and replace it with actual stuff; the naming should explain what the stuff in question is but email me if you still need to know.
b) you need LDAP support loaded and
c) the CN and name filter will vary depending on yr AD configuration, but I use
cn=Users,dc=domain,dc=com
... for the CN and
(&(objectClass=user)(|(userPrincipalName=#N#)(name=#N#)(displayname=#N#)(cn=#N#)))
for the namefilter, where #N# is the user's name (herein subbed in with regexps before the query as you will see in the convenience method queryForName().)
share and enjoy
-fish
<?php
///
///
///
/// LDAP access abstraction library
/// by fish, january 2003
/// includes
/// system defaults
require_once("sysdefaults.php");
/// debugger
require_once("debugger.php");
/// globalized ldap connection interface
global $ldap;
//$ldap = new LDAPConnection();
//$ldap->open();
/// syntax:
/// $ldap = new LDAPConnection();
/// $ldap->open();
/// $result = $ldap->query($dn, $filter);
/// (or:
/// $result = $ldap->queryForName("fish"); /// ???
/// )
/// $san = $result->getAttribute("samaccountname");
/// $result->close();
/// $ldap->close();
class LDAPConnection {
var $ldap_connection;
var $ldap_binding;
var $ldap_server;
var $ldap_username;
var $ldap_domain;
var $ldap_password;
var $ldap_isopen;
function LDAPConnection() {
/// get connection info from session
$this->ldap_server = $_SESSION['ldap_server'];
$this->ldap_username = $_SESSION['ldap_username'];
$this->ldap_domain = $_SESSION['ldap_domain'];
$this->ldap_password = $_SESSION['ldap_password'];
$this->ldap_isopen = false;
}
function open() {
global $debug;
if (!$this->ldap_isopen) {
/// open connection
$this->ldap_connection = @ldap_connect("ldap://".$this->ldap_server);
if ($this->ldap_connection == false) {
$debug->println("ERROR: LDAP Connection no workie. ldap = ".$this->ldap_connection."", 1);
return false;
} else {
/// bind with userID and password
$this->ldap_binding = @ldap_bind($this->ldap_connection, ($this->ldap_username."@".$this->ldap_domain), $this->ldap_password);
if ($this->ldap_binding == false) {
$debug->println("ERROR: LDAP Binding failed. binding = ".$this->ldap_binding.", ldap = ".$this->ldap_connection."", 1);
return false;
} else {
$this->ldap_isopen = true;
return true;
}
}
}
}
function close() {
if ($this->ldap_isopen) {
$out = true;
$out = ldap_close($this->ldap_connection);
unset($this->ldap_connection);
$this->ldap_isopen = false;
return $out;
} else {
return false;
}
}
function query($ldap_dn, $ldap_filter) {
if ($this->ldap_isopen) {
return new LDAPResult($this->ldap_connection, $ldap_dn, $ldap_filter);
} else {
return false;
}
}
function queryForName($namename) {
global $sysdefobj;
$dn = $sysdefobj->getValue("ldap_dn");
$namefilter = $sysdefobj->getValue("ldap_namefilter");
$namefilter = preg_replace("/#N#/", $namename, $namefilter);
return $this->query($dn, $namefilter);
}
}
class LDAPResult {
var $ldap_connection;
var $ldap_dn;
var $ldap_filter;
var $ldap_result;
var $ldap_info;
var $ldap_success;
var $ldap_fieldcount;
function LDAPResult($ldap_connection, $ldap_dn, $ldap_filter) {
global $sysdefobj;
global $debug;
$this->ldap_connection = $ldap_connection;
$this->ldap_dn = $ldap_dn;
$this->ldap_filter = $ldap_filter;
$this->ldap_success = false;
$this->ldap_fieldcount = 0;
$this->ldap_result = ldap_search($this->ldap_connection, $this->ldap_dn, $this->ldap_filter);
if ($this->ldap_result != false) {
$this->ldap_info = ldap_get_entries($this->ldap_connection, $this->ldap_result);
if ($this->ldap_info != false) {
$this->ldap_success = true;
}
} else {
/// SIREN CAUTION WARNING
$debug->println("WARNING: LDAPResult::construct(): ldap_search() returned false", 2);
}
}
function close() {
/// apparantly we don't need to deallocate the output shit here
$this->ldap_connection = false;
$this->ldap_success = false;
}
function getCount() {
if ($this->ldap_success) {
return intval($this->ldap_info['count']);
} else {
return -1;
}
}
function getField($idx, $num = 0) {
$out = false;
if ($this->ldap_success) {
$out = $this->ldap_info[$this->ldap_fieldcount][$idx][$num];
}
return $out;
}
function getNext($idx) {
$this->ldap_fieldcount++;
$out = false;
if ($this->ldap_success) {
$out = $this->ldap_info[$this->ldap_fieldcount][$idx];
}
return $out;
}
function getFields() {
if ($this->ldap_success) {
return $this->ldap_info;
} else {
return false;
}
}
function reset() {
$this->ldap_fieldcount = 0;
}
}
?>