Horde LDAP hook file

Hi Ralph,

I adapted the hook file from the horde howto for LDAP. To check the AD/LDAP fields you may use phpldapadmin or lam. I am going to integrate this into the horde module so AD/LDAP fullname instead of username will be used.

<?php
class Horde_Hooks
{
    public function prefs_init($pref, $value, $username, $scope_ob)
    {
        // LDAP connection parameters
        $ldapServer = 'localhost';
        $ldapPort = 389;
        $searchBase = 'ou=People,dc=directory,dc=nh';
        $binddn = 'cn=ldapservice,dc=directory,dc=nh';
        $bindpw = 'YOURBINDPW';

        switch ($pref)  {

            case 'from_addr':

                if (is_null($username)) {
                    return $value;
                }

                $ds = @ldap_connect($ldapServer, $ldapPort);
                if (ldap_start_tls($ds)) {
                   if (@ldap_bind($ds, $binddn, $bindpw)) {
                      $searchResult = @ldap_search($ds, $searchBase, 'uid=' . $username);
                      $information = @ldap_get_entries($ds, $searchResult);
                      if (($information === false) || ($information['count'] == 0)) {
                          $user = $username . '@domain.local';
                      } else {
                          $user = ($information[0]['Email'][0] != '')
                          ? $information[0]['Email'][0]
                          : $information[0]['mail'][0];
                      }
                  }
                }

                ldap_close($ds);
                return empty($user) ? $username : $user;


            case 'fullname':

                if (is_null($username)) {
                    return $value;
                }
                $ds = @ldap_connect($ldapServer, $ldapPort);

                if (ldap_start_tls($ds)) {
                  if (@ldap_bind($ds, $binddn, $bindpw)) {
                      $searchResult = @ldap_search($ds, $searchBase, 'uid=' . $username);
                      $information = @ldap_get_entries($ds, $searchResult);
                      if (($information === false) || ($information['count'] == 0)) {
                          $name = '';
                      } else {
                          $name = ($information[0]['cn'][0] != '')
                          ? $information[0]['cn'][0]
                          : $information[0]['gecos'][0];
                      }
                  }
                }
                ldap_close($ds);
                return empty($name) ? $username : $name;
        }
    }
}
3 Likes