Edirectory PHP Class
Views:
Contents |
There are three classes: eDirectory, which is basically an abstract class; eDirectorySearch, which allows you to search for various objects in the tree; and eDirectoryAppObject, which knows about Application Objects (ZEN) types in eDirectory
The Classes
<?php
/* Copyright Kieran Whitbread 2006 */
/* This file is released under the following license: http://opensource.org/licenses/lgpl-2.1.php GNU Lesser General Public License */
class eDirectory
{
var $connection = NULL;
var $binding = NULL;
var $server = NULL;
function eDirectory($server='localhost')
{
$this->__construct($server);
}
function __construct($server='localhost')
{
$this->server = $server;
$this->connection = ldap_connect($this->server);
if ($this->connection)
{
$this->binding = ldap_bind($this->connection);
if ($this->binding)
{
ldap_set_option($this->connection, LDAP_OPT_DEREF, LDAP_DEREF_ALWAYS);
ldap_set_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, 3);
}
else
{
error_log('Could not bind to server '.get_class().', '.__FILE__);
}
}
}
function __destruct()
{
ldap_unbind($this->connection);
}
function GetErrorMessage()
{
return ldap_error($this->connection);
}
}
class eDirectorySearch extends eDirectory
{
var $filter = NULL;
var $baseDn = NULL;
var $ldapObjectAttributes = NULL;
var $ldapSearchResultsResource = NULL;
var $currentResult = false;
var $currentResultAttributes = NULL;
function eDirectorySearch($filter, $baseDn, $ldapObjectAttributes)
{
$this->__construct($filter, $baseDn, $ldapObjectAttributes);
}
function __construct($filter, $baseDn, $ldapObjectAttributes)
{
parent::__construct();
$this->SetFilter($filter);
$this->SetBaseDn($baseDn);
$this->ldapObjectAttributes = $ldapObjectAttributes;
}
function __destruct()
{
parent::__destruct();
}
function GetAttribute($attribute, $index=false)
{
if (!isset($this->currentResultAttributes) || !isset($this->currentResultAttributes[$attribute]))
{
return NULL;
}
else
if ($index)
{
return $this->currentResultAttributes[$attribute][$index];
}
else
{
return $this->currentResultAttributes[$attribute];
}
}
function NextResult()
{
if ($this->connection && $this->ldapSearchResultsResource)
{
if (!$this->currentResult) $this->currentResult = ldap_first_entry($this->connection, $this->ldapSearchResultsResource);
else $this->currentResult = ldap_next_entry($this->connection, $this->ldapSearchResultsResource);
if ($this->currentResult)
{
$this->currentResultAttributes = ldap_get_attributes($this->connection, $this->currentResult);
return true;
}
}
$this->currentResultAttributes = NULL;
$this->currentResult = NULL;
$this->ldapSearchResultsResource = NULL;
return false;
}
function Search()
{
$this->ldapSearchResultsResource = ldap_search($this->connection, $this->baseDn, $this->filter, $this->ldapObjectAttributes);
}
function SetBaseDn($baseDn)
{
$this->baseDn = $baseDn;
}
function SetFilter($filter)
{
$this->filter = $filter;
}
function SetLdapObjectAttributes($ldapObjectAttributes)
{
if (is_array($ldapObjectAttributes))
{
$this->ldapObjectAttributes = $ldapObjectAttributes;
}
}
}
class eDirectoryAppObjects extends eDirectorySearch
{
var $applications = array();
function eDirectoryAppObjects($baseDn)
{
$this->__construct($baseDn);
}
function __construct($baseDn)
{
$filter = '(objectclass=AppApplication)';
$ldapObjectAttributes = array(
'appdescription',
'appadministratornotes',
'appcaption',
'appfolderobjects',
'creatorsname',
);
parent::__construct($filter, $baseDn, $ldapObjectAttributes);
}
function GetApplications()
{
$this->Search();
if ($application = ldap_first_entry($this->connection, $this->ldapSearchResultsResource))
{
$count = 0;
do
{
$caption = @ldap_get_values($this->connection, $application, 'appcaption');
$caption = $caption[0];
$description = @ldap_get_values($this->connection, $application, 'appdescription');
$description = $description[0];
$adminnotes = @ldap_get_values($this->connection, $application, 'appadministratornotes');
$adminnotes = $adminnotes[0];
$appfolders = @ldap_get_values($this->connection, $application, 'appfolderobjects');
$creator = @ldap_get_values($this->connection, $application, 'creatorsname');
$creator = $creator[0];
$dn = ldap_get_dn($this->connection, $application);
$dn = ldap_explode_dn($dn, 0);
$this->applications[$count] = array(
'Distinguished Name' => $dn,
'Caption' => $caption,
'Description' => $description,
'Administrator Notes' => $adminnotes,
'Application Folders' => $appfolders,
'Creator' => $creator,
);
$count++;
}
while ($application = ldap_next_entry($this->connection, $application));
}
}
}
Usage Example
Here's a really simple script which generates a report of application objects in eDirectory:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Application Objects Report - prototype</title>
</head>
<body>
<?php
require_once 'edir.classes.inc.php';
$appObjects = new eDirectoryAppObjects('ou=learner,o=vsfc');
$appObjects->GetApplications();
echo '<pre>';
print_r($appObjects->applications);
echo '</pre>';
if ($appObjects->binding) echo '<p>OK</p>';
?>
</body>
</html>
</nowiki>
