Don’t get me wrong, there’s nothing wrong with procedural coding, but when you’re working on a massive application you can’t go past OOP.
Recently I’ve had to integrate LDAP into our existing systems and web applications at work – Most of these are written with OO(Object Orientated) code as any other way would be impossible to maintain (Think 70,000 lines+)
After looking into PHP’s support for ldap and finding that it’s not only unstructured, but some of it just doesn’t make much sense (procedure names don’t fit the task for example) I decided to knock up a LDAP class.
The class is fully documented with phpdoc docblocks so that should make it easier for you to work out what it does
I also added a function to get the immediate children of a DN as it’s something I needed for my work. The function simply returns an array of child DN’s
Here it is.
File: class.ldap.php4 [21.48 kB]And an example for it’s usage.
<html>
<head>
<title> class.ldap.php4: Example1 </title>
</head>
<body>
<?php
include('class.ldap.php4');
// The hostname of our LDAP server
$ServerHost = 'localhost';
// The base DN we'll be querying (saves lots of typing)
$BaseDN = 'ou=staff,dc=example,dc=org';
// The user we're going to try to log in as
$Username = 'fred';
// The user's password
$Password = 'barny';
// Create an ldap object
$ld = new ldap($ServerHost);
// Connect to the server
if (!$ld->connect()) {
die("Error connecting: ".$ld->ldapError."\n");
}
// We'll try to log in as a user
if ($ld->bind("uid=$Username,$BaseDN",$Password)) {
// Example of the added 'children' function
// Get a list of child nodes under our username
if ($children = $ld->children("uid=$Username,$BaseDN")) {
print "<b>Children under our $Username's node</b><pre>\n";
var_dump($children);
print "</pre>\n";
} else {
print "Error getting children: ".$ld->ldapError."<br>\n";
}
// Find an entry under the address book
// All search functions return ldapresult objects
if ($sr = $ld->searchSubtree("ou=AddressBook,dc=example,dc=org","givenName=wilma",array('cn','givenName','sn','homePhone'))) {
// Get the first entry that the search returns
// ldapresult->firstEntry returns ldapresultentry objects
if ($entry = $sr->firstEntry()) {
printEntry($entry);
// More then one result?
// ldapresultentry->nextEntry() simply updates the existing entry
while ($entry->nextEntry()) {
printEntry($entry);
}
} else {
die("Error fetching entry: ".$sr->ldapError."\n");
}
// Good idea if you're doing lots of large queries, but not required
$sr->free();
} else {
die("Error performing search: ".$ld->ldapError."\n");
}
} else {
die("Error binding: ".$ld->ldapError."\n");
}
function printEntry($entry) {
print "<b>DN:</b>".$entry->getDN();
// Fetch all the attributes
if ($attrs = $entry->getAttributes()) {
print "<b>cn:</b> ".$attrs['cn']."<br>\n";
print "<b>givenName:</b> ".$attrs['givenName']."<br>\n";
print "<b>sn:</b> ".$attrs['sn']."<br>\n";
print "<b>homePhone</b> ".$attrs['homePhone']."<br>\n";
} else {
print "Error while fetching attributes: ".$entry->ldapError."<br>\n";
}
}
?>
</body>
</html>
Commenting is closed for this article.