Getting Horde logged-in username & full name

A customer wanted to be able to pull the current logged in username AND the full name of the user to use outside of Horde to populate some on-the-fly flyers.

Getting Horde to tell us the current logged in user was no problem. Three lines of code. However, the trick was getting the full name of the user which is set in the Mail Preferences under “Identities” when you log into Horde. Horde has a built in hook to retrieve the full name, but only if you’re using LDAP authentication, or /etc/passwd authentication. Because the mail accounts are setup using Plesk, we had to set his Horde Auth to use simple IMAP authentication, which doesn’t maintain the full name of the user account, just the user account and password and then the standard plesk qmail /var/qmail/mailnames message store. Everything else about the user is stored in the Horde database. Took us a few hours, because we tried everything from trying to manipulate session data, to inserting code in Horde, then it dawned on us to why not just pull the query based on the username from the database. But because the place where Horde stores the full name is not like in a constant table row thats says something like “full name”.. the data looks like this:

:startcode:

a:1:{i:0;a:13:{s:2:"id";s:16:"Default Identity";s:8:"fullname";s:6:"Mark D";s:9:"from_addr";s:15:"markd-notreal@none.com";s:12:"replyto_addr";s:0:"";s:10:"alias_addr";a:0:{}s:10:"tieto_addr";a:0:{}s:8:"bcc_addr";a:0:{}s:9:"signature";s:0:"";s:9:"sig_first";i:0;s:10:"sig_dashes";i:0;s:14:"save_sent_mail";i:1;s:16:"sent_mail_folder";s:4:"Sent";s:16:"default_identity";s:1:"0";}}

:endcode:

That’s one field (prefs_value) that contains that information. And the next problem was that “s:6″ doesn’t always precede every full name. Sometimes it was “s:9″, or “s:13″, etc. The only thing that was constant before the full name was the :”fullname”;s:. So I had to write a function to get whatever was after “fullname”;s and the following ;s:, then strip out the characters before the full name and remove the quotes. And here ya go!

:startcode:


<?php

require_once("lib/base.php");
$current = (Auth::getAuth());
echo "Horde says this user is on: <span style="color: blue;">" . $current . "</span>";

$link = $link = mysql_connect('localhost', 'admin', 'apassword');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db('newhorde', $link);
$query = sprintf("SELECT pref_uid, pref_scope, pref_name, pref_value FROM horde_prefs WHERE pref_uid='$current' AND pref_scope='horde' AND pref_name='identities'");
$result = mysql_query($query);
mysql_close($link);
while ($row = mysql_fetch_assoc($result)) {
$workit = $row['pref_value'];
}

function get_string_between($string, $start, $end) {
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
} 

echo "

This is the full string from the pref_value row from table horde_prefs for user <span style="color: red;"> " . $current . "</span>: <span style="color: blue;"> " . $workit . "</span>";

$parsed = get_string_between($workit, 'fullname";s:', ';s:'); 

echo "

First passed parsing to strip out everything we can using 'fullname\";s:' as a constant base: <span style="color: blue;">" . $parsed . "</span>";

$parsed2 = get_string_between($parsed, ':"', '"'); 

echo "

Second and final parsing to strip out other characters other than the actual user name, giving us end result: <span style="color: blue;">" . $parsed2 . "</span>";
?>

:endcode:

The end result is this:
——–
Horde says this user is on: markd

This is the full string from the pref_value row from table horde_prefs for user markd:

a:1:{i:0;a:13:{s:2:"id";s:16:"Default Identity";s:8:"fullname";s:6:"Mark D";s:9:"from_addr";s:15:"markd-notereal@none.com";s:12:"replyto_addr";s:0:"";s:10:"alias_addr";a:0:{}s:10:"tieto_addr";a:0:{}s:8:"bcc_addr";a:0:{}s:9:"signature";s:0:"";s:9:"sig_first";i:0;s:10:"sig_dashes";i:0;s:14:"save_sent_mail";i:1;s:16:"sent_mail_folder";s:4:"Sent";s:16:"default_identity";s:1:"0";}}

First passed parsing to strip out everything we can using ‘fullname”;s:’ as a constant base: 6:”Mark D”

Second and final parsing to strip out other characters other than the actual user name, giving us end result: Mark D

-nw

This entry was posted on Thursday, January 14th, 2010 at 11:29 pm and is filed under Horde / IMP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply