Here is where I am at so far. Anyone care to help or critique:
Base Class File: wldap.class.php
Full Name: wrapper ldap class
<?php
// Define Installed Class
define('INSTALLED_WLDAP', 1);
// Check Dependancies
if( !(defined('LDAP_USER')) ) define('LDAP_USER', '');
if( !(defined('LDAP_PASS')) ) define('LDAP_PASS', '');
if( !(defined('LDAP_DOMAIN')) ) define('LDAP_DOMAIN', '');
if( !(defined('LDAP_PROTOCOL')) ) define('LDAP_PROTOCOL', 3);
if( !(isset($path_root)) ) $path_root = "./../";
if( !(defined('INSTALLED_REDIRECTION')) ) require_once("$path_root/common/redirection.php");
// Class wrapper for base dn
class wBASEDN {
var $ou;
var $cn;
var $dc;
var $dom;
function wBASEDN(){
$this->dc = array();
$this->ou = array();
$this->cn = array();
}
function add_element($type, $element){
switch($type) {
case "dc":
$this->dc[count($this->dc)] = $element;
break;
case "ou":
$this->ou[count($this->ou)] = $element;
break;
case "cn":
$this->cn[count($this->cn)] = $element;
break;
default:
redirect_user("Invalid Distinguished Name element element.", "error");
}
}
function _get(){
$base_dn = "";
for($i=0; $i<count($this->dc); $i++){
if(($i) > 0) $base_dn += ",";
$base_dn += "dc=$this->dc[$i]";
}
for($j=0; $j<count($this->cn); $j++){
if(($i+$j) > 0) $base_dn += ",";
$base_dn += "ou=$this->ou[$j]";
}
for($k=0; $k<count($this->cn); $k++){
if(($i+$j+$k) > 0) $base_dn += ",";
$base_dn += "cn=$this->cn[$i]";
}
return $base_dn;
}
}
// LDAP Wrapper Class and functionality
class wLDAP {
var $queue;
var $top;
var $bottom;
var $rlink;
var $is_open;
var $base_dn;
var $filter;
var $columns;
function wLDAP() {
$this->is_open = false;
$this->base_dn = new wBASEDN();
$this->filter = new wBASEDN();
$this->columns = array();
}
function &get_array_from_group($group) {
if( !($this->is_open) ) $this->_connect();
if( !($search = ldap_search($this->rlink, $this->base_dn->_get(), $this->filter->_get(), $this->columns)) ){
redirect_user("Unable to search LDAP Server.", "error");
}
if( !($group_array = ldap_get_entries($this->rlink, $search)) ){
redirect_user("Unable to get entries from LDAP search", "error");
}
return $group_array;
}
function _connect($ldap_domain=LDAP_DOMAIN, $ldap_protocol=LDAP_PROTOCOL, $ldap_user=LDAP_USER, $ldap_pass=LDAP_PASS){
if( !($resource_link = ldap_connect(LDAP_DOMAIN)) )
redirect_user("Unable to connect to LDAP Server.", "error");
if( !(ldap_set_option($resource_link, LDAP_OPT_PROTOCOL_VERSION, LDAP_PROTOCOL)) )
redirect_user("Unable to set LDAP Protocol Version.", "error");
if( !(ldap_bind($resource_link, LDAP_USER, LDAP_PASS)) )
redirect_user("Unable to bind to LDAP Server.", "error");
$this->rlink = $resource_link;
}
function _disconnect(){
ldap_unbind($this->rlink);
}
}
?>
testing file: ldap_test.php
<?php
ob_start();
$path_root = "./..";
require_once('wldap.class.php');
$ldap = new wLDAP();
$ldap->base_dn->add_element('dc', 'com');
$ldap->base_dn->add_element('dc', 'mysite');
$ldap->base_dn->add_element('dc', 'subdomain');
$ldap->base_dn->add_element('ou', 'Security Groups');
$ldap->_connect();
$ldap_return =& $ldap->get_array_from_group("All Homeland");
echo (count($ldap_return) . " entries in array<br>\n");
$ldap->_disconnect();
ob_end_flush();
?>
Both Files are stored in a sub directory of root application called common (just for reference if you see the $path_root variable.
redirection function use headers to redirect to main index.php file where error message is displayed
I found a primer that explains base_dn (base distinguished name) for active directory which helped.
http://www.windowsnetworking.com/kbase/WindowsTips/Windows2000/AdminTips/ActiveDirectory/ActiveDirectoryNamingStandard.html
Anyway I can't get the get entries to work so I think I have to bind to the active directory server with domain credentials and not anonymously. Is there an SSL consideration here? Am I missing anything huge.
BTW the output buffer is needed to allow redirection as the warning messages generated by the failed ldap_get_entries would cause headers to be sent to the browser before the redirection function moves the user with the error message (graceful death)
Thanks Ahead,
Geoffrey