++ Introduction
++ The SQL table
There are many ways to do this of course. In this example, we'll just be using a table with two rows:
---------------------------------------------------------
| username | cyrus |
---------------------------------------------------------
Your table can be constructed however you like. It can even be a part of your existing Horde DB. The important point is that you need the table to be constructed in such a way as to be able to query a username and have the lookup return a server name from {{imp/config/servers.php}}.
++ Writing a hook
Here's some sample code for a hook placed inside horde/config/hooks.php:
if (!function_exists('_horde_hook_preauthenticate')) {
function _horde_hook_preauthenticate($userID, $credential, $realm)
{
require dirname(__FILE__) . '/../imp/config/servers.php';
// Strip domain part from user.
$userID = substr($userID, 0, strpos($userID, '@'));
// Connect to database server.
$db = mysql_connect('hostname', 'dbuser', 'dbpasswd') or die('Can not connect to database.');
// Select database.
mysql_select_db('dbname') or die('Can not select database.');
// Execute the query
$sth = mysql_query('SELECT server_name FROM users WHERE user=\'' . mysql_real_escape_string($userID) . '\'') or die ('Can not query database.');
// Fetch the server name.
$server = mysql_fetch_row($sth);
if ($server === false) {
die('Can not read user from database.');
}
// Set IMAP server values.
foreach (array('server', 'folders', 'namespace') as $key) {
$_SESSION['imp'][$key] = $servers[$server[0]][$key];
}
return true;
}
}
++ IMAP server selection by realmed username
+++ Requirements
++++ Examplary snippet of {{imp/config/servers.php}}
/* Example configuration: */
$servers['imap'] = array(
'name' => 'IMAP Server',
'server' => 'imap.example.com',
'hordeauth' => true,
'protocol' => 'imap/notls',
'port' => 143,
'maildomain' => 'example.com',
'smtphost' => 'smtp.example.com',
'smtpport' => 25,
'realm' => '',
'preferred' => '',
);
$servers['imap1'] = array(
'name' => 'IMAP Server 1',
'server' => 'imap1.example.com',
'hordeauth' => true,
'protocol' => 'imap/ssl/novalidate-cert',
'port' => 993,
'maildomain' => 'example.com',
'smtphost' => 'smtp.example.com',
'smtpport' => 25,
'realm' => 'imap1.example.com',
'preferred' => '',
);
++++ Functionality of the hook
Entering the username {{smith}} in the login screen selects the necessary values given by {{$servers['imap']}}:
Entering the username {{smith@imap1.example.com}} instead selects the values of {{$servers['imap1']}}:
//Note://
+++ The Preauthenticate-Hook
The following hook has to be inserted in {{config/hooks.php}}:
if (!function_exists('_horde_hook_preauthenticate')) {
function _horde_hook_preauthenticate($userID, $credential, $realm)
{
require dirname(FILE) . '/../imp/config/servers.php';
// Convert all to lower chars (even the possible domain part)
$userID=strtolower($userID);
// Strip domain part from user, if it exists.
if (($domainpart = strpos($userID, '@'))) {
$server=substr($userID, $domainpart+1);
$userID=substr($userID, 0, $domainpart);
// Change the values only if a domain part was found ...
if (!empty($server)) {
foreach ($servers as $serverkey => $curServer) {
if (!empty($curServer['realm']) && $server == $curServer['realm']) {
// We found an entry, now set IMAP server values.
foreach (array('server', 'folders', 'namespace',
'protocol', 'port', 'smtphost', 'smtpport', 'maildomain') as $key) {
if (isset($servers[$serverkey][$key])) {
$_SESSION['imp'][$key] = $servers[$serverkey][$key];
}
}
// Now use only the stripped version of the userID to logon to the server
$_SESSION['imp']['user'] = $userID;
// Setup the correct base_protocol
$_SESSION['imp']['base_protocol'] = $_SESSION['imp']['protocol'];
if (($pos = strpos($_SESSION['imp']['protocol'], '/'))) {
$_SESSION['imp']['base_protocol'] = substr($_SESSION['imp']['protocol'], 0, $pos);
}
}
}
}
}
return true;
}
}
Like in the first example, the hook has to be activated inside {{config/conf.php}}.
...
$conf['hooks']['preauthenticate'] = true;
...