IPv4 Fremnet Logo
TOOLS, TINKERINGS & CODE

Do you use my code?

Object Orientated LDAP for PHP4 · Jun 23, 10:49 by Shannon Wynter

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]
Download: 235

And an example for it’s usage.

  1. <html>
  2. <head>
  3. <title> class.ldap.php4: Example1 </title>
  4. </head>
  5. <body>
  6. <?php
  7.  
  8. include('class.ldap.php4');
  9.  
  10. // The hostname of our LDAP server
  11. $ServerHost = 'localhost';
  12.  
  13. // The base DN we'll be querying (saves lots of typing)
  14. $BaseDN = 'ou=staff,dc=example,dc=org';
  15.  
  16. // The user we're going to try to log in as
  17. $Username = 'fred';
  18.  
  19. // The user's password
  20. $Password = 'barny';
  21.  
  22. // Create an ldap object
  23. $ld = new ldap($ServerHost);
  24.  
  25. // Connect to the server
  26. if (!$ld->connect()) {
  27. die("Error connecting: ".$ld->ldapError."\n");
  28. }
  29.  
  30. // We'll try to log in as a user
  31. if ($ld->bind("uid=$Username,$BaseDN",$Password)) {
  32. // Example of the added 'children' function
  33. // Get a list of child nodes under our username
  34. if ($children = $ld->children("uid=$Username,$BaseDN")) {
  35.   print "<b>Children under our $Username's node</b><pre>\n";
  36.   var_dump($children);
  37.   print "</pre>\n";
  38. } else {
  39.   print "Error getting children: ".$ld->ldapError."<br>\n";
  40. }
  41.  
  42. // Find an entry under the address book
  43. // All search functions return ldapresult objects
  44. if ($sr = $ld->searchSubtree("ou=AddressBook,dc=example,dc=org","givenName=wilma",array('cn','givenName','sn','homePhone'))) {
  45.   // Get the first entry that the search returns
  46.   // ldapresult->firstEntry returns ldapresultentry objects
  47.   if ($entry = $sr->firstEntry()) {
  48.   printEntry($entry);
  49.   // More then one result?
  50.   // ldapresultentry->nextEntry() simply updates the existing entry
  51.   while ($entry->nextEntry()) {
  52.     printEntry($entry);
  53.   }
  54.   } else {
  55.   die("Error fetching entry: ".$sr->ldapError."\n");
  56.   }
  57.   // Good idea if you're doing lots of large queries, but not required
  58.   $sr->free();
  59. } else {
  60.   die("Error performing search: ".$ld->ldapError."\n");
  61. }
  62. } else {
  63. die("Error binding: ".$ld->ldapError."\n");
  64. }
  65.  
  66.  
  67. function printEntry($entry) {
  68. print "<b>DN:</b>".$entry->getDN();
  69. // Fetch all the attributes
  70. if ($attrs = $entry->getAttributes()) {
  71.   print "<b>cn:</b> ".$attrs['cn']."<br>\n";
  72.   print "<b>givenName:</b> ".$attrs['givenName']."<br>\n";
  73.   print "<b>sn:</b> ".$attrs['sn']."<br>\n";
  74.   print "<b>homePhone</b> ".$attrs['homePhone']."<br>\n";
  75. } else {
  76.   print "Error while fetching attributes: ".$entry->ldapError."<br>\n";
  77. }
  78. }
  79. ?>
  80. </body>
  81. </html>
  82. Download this code: class.ldap.example1.php (Downloaded 202 time(s))
Comments

Commenting is closed for this article.

---== Copyright Shannon Wynter - All rights reserved - All wrongs avenged ==---