Sunday, January 6, 2013

Function To Find Largest UID Number In LDAP


 I got most of this function from http://bakery.cakephp.org/articles/UncleBill/2006/10/15/using-ldap-as-a-database. It will find all the uidNumbers, sorting them largest to smallest, then return the largest number.

$ds = ldap_connect("localhost");  // assuming the LDAP server is on this host

    function findLargestUidNumber($ds)
    {
      $s = ldap_search($ds, "ou=people,dc=yourdomain,dc=com", 'uidnumber=*');
      if ($s)
      {
         // there must be a better way to get the largest uidnumber, but I can't find a way to reverse sort.
         ldap_sort($ds, $s, "uidnumber");

         $result = ldap_get_entries($ds, $s);
         $count = $result['count'];
         $biguid = $result[$count-1]['uidnumber'][0];
         return $biguid;
      }
      return null;
    }

$largestUID = findLargestUidNumber($ds);

1 comment: