Hello everyone,
I've been trying to get my users to validate against an IMAP4 server and I've found the imap_auth hook very useful. The only problem is that it doesn't work against the IMAP server I use, since the usernames ought to have a suffix. Because of this, and because I found the code of imap_auth rather unreadable, I've decided to hack it a bit. So, now I've got this:
function imap_auth_auth($username, $password, $server) {
if (variable_get('imap_auth_enabled', 0) == 0) {
return FALSE;
}
if ( strpos ( $username, 'domainname.nl' ) === false )
{
$username = $username . '.domainname.nl'; // this is the suffix.
}
if ( @imap_open ( '{imap4.myimapserver.nl}/INBOX', $username, $password ) )
{
watchdog ( 'user', 'User ' . $username . ' validated.' ); // For debugging purposes.
global $user;
return true;
}
else
{
watchdog ( 'user', "nope for user $username, $password on $server." );
return false;
}
}
This actually works. It will open a connection to imap4.myimapserver.nl, with the username username.domainname.nl and the password.
The logs (watchdog) however tell me a different story:
user 2006-08-18 08:27 Login attempt failed for username@domainname.nl
user 2006-08-18 08:27 succeeded.
As you can see the string 'succeeded' is written to the log, but still the login attempt has failed, while I specifically return true. Does someone know why exactly this doesn't work? Perhaps a global variable like $loggedin or something like this should be set? I've been thinking about that quite a while, but the original imap_auth doesn't seem to do anything else.