\documentclass{article}
\usepackage{ulem}
\usepackage{graphicx}
\usepackage{hyperref}
\pagestyle{headings}
\begin{document}
+Default Preferences - Full Name and Email Address LDAP 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 in hooks.php.dist, but I had some issues with that so it's a bit tweaked. Runs on my <a href="https://wiki.horde.org/CentOS">CentOS</a> 6 system. Rather than specifying LDAP server details in the hook, it uses the system's ability to 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:

\begin{itemize}
\item LDAP cn entry(ies) with Full Name (the script uses the first one as the default Full Name)


\item LDAP mail entry(ies) with email addresses (the script uses the first one as the default email address)


\item As ldapsearch is set to use TLS (using the -ZZ option), you will need to have a functional TLS LDAP setup for this to work. If you are happy to not use TLS, drop that option. Again, make sure it works from the command line if you are having issues.


\item To set your LDAP domain in searchBase


\end{itemize}
First enable the prefs to look for the hooks in horde/config/prefs.local.php:

<pre><code class="language-php">
<?php
 \$\_prefs['from\_addr']['hook'] = true;
 \$\_prefs['fullname']['hook'] = true;
</code></pre>
Then you will need to create a horde/config/hooks.local.php:

<pre><code class="language-php">
<?php
class Horde\_Hooks
\{
    public function prefs\_init(\$pref, \$value, \$username, \$scope\_ob)
    \{
        switch (\$pref) \{
        case 'from\_addr':
            if (is\_null(\$username)) \{
                return \$value;
            \}
            \$searchBase = 'ou=users,dc=yourdomain,dc=lan';
            \$cmd = '/usr/bin/ldapsearch -ZZ -x -b ' . \$searchBase . ' uid=' . escapeshellcmd(\$username) . ' mail | /bin/grep mail: | /usr/bin/awk \textbackslash\{\}'\{print \$2\}\textbackslash\{\}'';
// remember use -h for use another ldap host
	     \$mails = `\$cmd`;
            \$mail\_array = explode("\textbackslash\{\}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) . ' cn | /bin/grep cn: | /usr/bin/cut -c5-';
// remember use -h for use another ldap host
		\$cns = `\$cmd`;
		\$cn\_array = explode("\textbackslash\{\}n", \$cns);
		\$cn = \$cn\_array['0'];
		return empty(\$cn)
			? \$username
			: \$cn;
        \}
    \}
\}
</code></pre>
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:

<pre><code class="language-php">
\$searchBase = 'ou=users,dc=yourdomain,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 \textbackslash\{\}'\{print \$2\}\textbackslash\{\}'';
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("\textbackslash\{\}n", \$cns);
\$cn = \$cn\_array['0'];
</code></pre>

\noindent\rule{\textwidth}{1pt}
Simon Wilson

\end{document}
