From 942069969cfd95784f872b3bef2cc6323fc55c3d Mon Sep 17 00:00:00 2001 From: alecpl Date: Fri, 10 Apr 2009 09:26:56 +0000 Subject: - Support several e-mail addresses per user from virtuser_file (#1485678) - return NULL from rcube_user::email2user if user not found (#1485801) --- program/include/rcmail.php | 7 ++-- program/include/rcube_user.php | 91 +++++++++++++++++++++--------------------- 2 files changed, 49 insertions(+), 49 deletions(-) (limited to 'program') diff --git a/program/include/rcmail.php b/program/include/rcmail.php index f109c16fd..71601b526 100644 --- a/program/include/rcmail.php +++ b/program/include/rcmail.php @@ -453,9 +453,10 @@ class rcmail $username .= '@'.$config['username_domain']; } - // try to resolve email address from virtuser table - if (!empty($config['virtuser_file']) && strpos($username, '@')) - $username = rcube_user::email2user($username); + // try to resolve email address from virtuser table + if (strpos($username, '@')) + if ($virtuser = rcube_user::email2user($username)) + $username = $virtuser; // lowercase username if it's an e-mail address (#1484473) if (strpos($username, '@')) diff --git a/program/include/rcube_user.php b/program/include/rcube_user.php index b8833b3c7..17debeb7a 100644 --- a/program/include/rcube_user.php +++ b/program/include/rcube_user.php @@ -350,10 +350,12 @@ class rcube_user $rcmail = rcmail::get_instance(); $dbh = $rcmail->get_dbh(); - // try to resolve user in virtusertable - if ($rcmail->config->get('virtuser_file') && !strpos($user, '@')) - $user_email = rcube_user::user2email($user); - + // try to resolve user in virtuser table and file + if (!strpos($user, '@')) { + if ($email_list = self::user2email($user, false)) + $user_email = $email_list[0]; + } + $dbh->query( "INSERT INTO ".get_table_name('users')." (created, last_login, username, mail_host, alias, language) @@ -372,35 +374,21 @@ class rcube_user $user_name = $user != $user_email ? $user : ''; - // try to resolve the e-mail address from the virtuser table - if (($virtuser_query = $rcmail->config->get('virtuser_query')) - && ($sql_result = $dbh->query(preg_replace('/%u/', $dbh->escapeSimple($user), $virtuser_query))) - && ($dbh->num_rows() > 0)) - { - $standard = 1; - while ($sql_arr = $dbh->fetch_array($sql_result)) - { - $dbh->query( + if (empty($email_list)) + $email_list[] = strip_newlines($user_email); + + // also create new identity records + $standard = 1; + foreach ($email_list as $email) { + $dbh->query( "INSERT INTO ".get_table_name('identities')." (user_id, del, standard, name, email) VALUES (?, 0, ?, ?, ?)", $user_id, $standard, strip_newlines($user_name), - preg_replace('/^@/', $user . '@', $sql_arr[0])); - $standard = 0; - } - } - else - { - // also create new identity records - $dbh->query( - "INSERT INTO ".get_table_name('identities')." - (user_id, del, standard, name, email) - VALUES (?, 0, 1, ?, ?)", - $user_id, - strip_newlines($user_name), - strip_newlines($user_email)); + preg_replace('/^@/', $user . '@', $email)); + $standard = 0; } } else @@ -418,14 +406,13 @@ class rcube_user /** - * Resolve username using a virtuser table + * Resolve username using a virtuser file * * @param string E-mail address to resolve * @return string Resolved IMAP username */ static function email2user($email) { - $user = $email; $r = self::findinvirtual('^' . quotemeta($email) . '[[:space:]]'); for ($i=0; $i 0) - { - $user = trim($arr[count($arr)-1]); - break; - } + return trim($arr[count($arr)-1]); } - return $user; + return NULL; } /** - * Resolve e-mail address from virtuser table + * Resolve e-mail address from virtuser file/table * * @param string User name - * @return string Resolved e-mail address + * @param boolean If true returns first found entry + * @return mixed Resolved e-mail address string or array of strings */ - static function user2email($user) + static function user2email($user, $first=true) { - $email = ''; - $r = self::findinvirtual('[[:space:]]' . quotemeta($user) . '[[:space:]]*$'); + $result = array(); + $rcmail = rcmail::get_instance(); + $dbh = $rcmail->get_dbh(); + // SQL lookup + if ($virtuser_query = $rcmail->config->get('virtuser_query')) { + $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escapeSimple($user), $virtuser_query)); + while ($sql_arr = $dbh->fetch_array($sql_result)) + if (strpos($sql_arr[0], '@')) { + $result[] = $sql_arr[0]; + if ($first) + return $result[0]; + } + } + // File lookup + $r = self::findinvirtual('[[:space:]]' . quotemeta($user) . '[[:space:]]*$'); for ($i=0; $i 0) + if (count($arr) > 0 && strpos($arr[0], '@')) { - $email = trim(str_replace('\\@', '@', $arr[0])); - break; + $result[] = trim(str_replace('\\@', '@', $arr[0])); + + if ($first) + return $result[0]; } } - - return $email; + + return empty($result) ? NULL : $result; } /** - * Find matches of the given pattern in virtuser table + * Find matches of the given pattern in virtuser file * * @param string Regular expression to search for * @return array Matching entries @@ -500,7 +500,6 @@ class rcube_user return $result; } - } -- cgit v1.2.3