Better username-Handling

Martin Eitzenberger martin at psiplus.at
Thu Sep 28 15:08:50 CEST 2006


Hi!

I somehow "improved" the way how roundcubes handles virtual users... This is, because my server panel (ispconfig) somehow creates an ugly virtual user table, therefore i want strictly the real mail username as common-name for accounts, so I moved the email2user BEFORE the first mysql-query

// PART OF main.inc / rcmail_login(...) //

    $imap_port = $CONFIG['default_port'];

  // try to resolve email address from virtuser table
   if (!empty($CONFIG['virtuser_file']) && strstr($user, '@'))
    $user = rcmail_email2user($user);

  // query if user already registered
  $sql_result = $DB->query("SELECT user_id, username, language, preferences
                            FROM ".get_table_name('users')."
                            WHERE  mail_host=? AND (username=? OR alias=?)",

// END OF PART //

This way, the database is forced to be on a real-username basis and doesn't create different addressbooks and settings by logging in with the different aliases of the same mailbox (wich makes no sense at all...)

Another thing is, that I created faster mail2user and user2mail functions, use them if you like, GPL Code as usual ;-), mention me in some author/history/changelog if you like, I'm not one of these Super-EGO people who need to be mentioned anywhere, but I'm not against it either ;-)

The functions are currently named "rcmail_getvlist", "rcmail_email2user" and "rcmail_user2email":

// SNIPPET for main.inc //

function rcmail_getvlist()
{
    global $CONFIG,$x_listcache;
    if(!is_array($x_listcache))
    {
        if(!is_file($CONFIG['virtuser_file']))return(array());
        $f=file($CONFIG['virtuser_file']);
        while(list(,$t)=each($f))if(($t=trim($t))&&$t[0]!="#")
        {
            $t=str_replace("\t"," ",$t);
            while(strstr($t,"  "))$t=str_replace("  "," ",$t);
            list($x,$y)=explode(" ",$t);
            $e[$x]=$y;
            $u[$y]=$x;
        }
        $x_listcache=array($e,$u);
    }
    return($x_listcache);
}

function rcmail_email2user($email)
{
    $l=rcmail_getvlist();
    $x=$l[0][$email]
    return((($x)?$x:$email));
}

function rcmail_user2email($user)
{
    $l=rcmail_getvlist();
    $x=$l[1][$user]
    return((($x)?$x:$user));
}

// END OF SNIPPED //

Basically, the getvlist returns (cached run, for future usage in sessions and/or multiple usage) a multi-dimensional array, value 0 contains an assoc-list with mail2user and 1 a list of user2mail. so you easily get the user: $user=$l[0][$email] ... the vlist basically works stable on all sendmail-style mailtab files, space or tab seperated (postfix, sendmail, exim, etc.) with single user to alias associations.

Yeah, one other thing: I strongly recommend that you don't rely on any user2email functions, because often there are many aliases for the same user, and there is no rule to tell wich is the prefered one...

Okay, hope I could help you, thanx for the only free webmailer I >want< to use ;-)
And if anyone of you speaks german, please visit my RPG: http://pb.exw.at/

Have Fun!
Martin alias PSIplus





More information about the Dev mailing list