6.0.0-git
2021-11-27

Diff for LDAPHooksHorde5 between 4 and 5

+Hooks in Horde5

These are the hooks I set up in my Horde 5 setup to provide default Full Name and email addresses, retrieved from LDAP. It's based on the one onin hooks.php.dist, but I had some issues with that so it's a bit tweaked. Runs on my CentOS 6 system. Rather than specifying LDAP server details in thethe hook, it uses the system's ability toto ldapsearch - which makes troubleshooting easier too, just strip out the command and make it work on the command line. It does mean that you have to have LDAP functional from your system for ldapsearch calls. 

It uses awk to get the email address from the returned grep'ed value, and cut to remove "cn: " from the Full Name. You may need to tweak those bits so it does what you need on your system.

You will need:
* LDAP cn entry(ies) with Full Name (the script uses the first one as the default Full Name)
* LDAP mail entry(ies) with email addresses (the script uses the first one as the default email address)
* As ldapsearch is set to use TLS (using the -ZZ option). Youoption), you will need toto have a functional TLS LDAP setup for this toto work. If youyou are happy to not use TLS, drop that option. Again, make sure it works from the command line if you are having issues.
* To set your LDAP domain in searchBase


<code type="php">
<?php
class Horde_Hooks
{
    public function prefs_init($pref, $value, $username, $scope_ob)
    {
        switch ($pref) {
        case 'from_addr':

                return $value;
            }
            $searchBase = 'ou=users,dc=yourdomain,dc=lan';
            $cmd = '/usr/bin/ldapsearch -ZZ -x -b ' . $searchBase . ' uid=' . escapeshellcmd($username) . ' | /bin/grep mail | /usr/bin/awk \'{print $2}\'';
	     $mails = `$cmd`;
            $mail_array = explode("\n", $mails);
            $mail = $mail_array['0'];
            return empty($mail)
                ? ''
                : $mail;

        case 'fullname':
            if (is_null($username)) {
                return $value;
            }
		$searchBase = 'ou=users,dc=yourdomain,dc=lan';
		$cmd = '/usr/bin/ldapsearch -ZZ -x -b ' . $searchBase . ' uid=' . escapeshellcmd($username) . ' | /bin/grep cn: | /usr/bin/cut -c5-';
		$cns = `$cmd`;
		$cn_array = explode("\n", $cns);
		$cn = $cn_array['0'];
		return empty($cn)
			? $username
			: $cn;
        }
    }
}
</code>

For troubleshooting I used the php file_put_contents function. The following is an extract of what I used as a guide to help you see what the variables are doing:

<code type="php">
$searchBase = 'ou=users,dc=simonandkate,dc=lan';
file_put_contents('/var/www/horde/output.txt', 'Variable $searchBase is ' . $searchBase . PHP_EOL);
$cmd = '/usr/bin/ldapsearch -ZZ -x -b ' . $searchBase . ' uid=' . escapeshellcmd($username) . ' | /bin/grep cn: | /usr/bin/awk \'{print $2}\'';
file_put_contents('/var/www/horde/output.txt', 'Variable $cmd is ' . $cmd . PHP_EOL, FILE_APPEND);
$cns = `$cmd`;
file_put_contents('/var/www/horde/output.txt', 'Variable $cns is ' . $cns . PHP_EOL, FILE_APPEND);
$cn_array = explode("\n", $cns);
$cn = $cn_array['0'];
</code>