- private function getLogin($login)
- {
- global $globals;
-
- // If $data is an integer, fetches directly the result.
- if (is_numeric($login)) {
- $res = XDB::query("SELECT user_id, hruid FROM auth_user_md5 WHERE user_id = {?}", $login);
- if ($res->numRows()) {
- return $res->fetchOneRow();
- }
-
- throw new UserNotFoundException();
- }
-
- // Checks whether $login is a valid hruid or not.
- $res = XDB::query("SELECT user_id, hruid FROM auth_user_md5 WHERE hruid = {?}", $login);
- if ($res->numRows()) {
- return $res->fetchOneRow();
- }
-
- // From now, $login can only by an email alias, or an email redirection.
- // If it doesn't look like a valid address, appends the plat/al's main domain.
- $login = trim(strtolower($login));
- if (strstr($login, '@') === false) {
- $login = $login . '@' . $globals->mail->domain;
- }
-
- // Checks if $login is a valid alias on the main domains.
- list($mbox, $fqdn) = explode('@', $login);
- if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) {
- $res = XDB::query("SELECT u.user_id, u.hruid
- FROM auth_user_md5 AS u
- INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
- WHERE a.alias = {?}", $mbox);
- if ($res->numRows()) {
- return $res->fetchOneRow();
- }
-
- if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) {
- $res = XDB::query("SELECT u.user_id, u.hruid
- FROM auth_user_md5 AS u
- INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
- WHERE a.alias = {?} AND u.promo = {?}", $matches[1], $matches[2]);
- if ($res->numRows() == 1) {
- return $res->fetchOneRow();
- }
- }
-
- throw new UserNotFoundException();
- }
-
- // Looks for $login as an email alias from the dedicated alias domain.
- if ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) {
- $res = XDB::query("SELECT redirect
- FROM virtual_redirect
- INNER JOIN virtual USING(vid)
- WHERE alias = {?}", $mbox . '@' . $globals->mail->alias_dom);
- if ($redir = $res->fetchOneCell()) {
- // We now have a valid alias, which has to be translated to an hruid.
- list($alias, $alias_fqdn) = explode('@', $redir);
- $res = XDB::query("SELECT u.user_id, u.hruid
- FROM auth_user_md5 AS u
- LEFT JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
- WHERE a.alias = {?}", $alias);
- if ($res->numRows()) {
- return $res->fetchOneRow();
- }
- }
-
- throw new UserNotFoundException();
- }
-
- // Otherwise, we do suppose $login is an email redirection.
- $res = XDB::query("SELECT u.user_id, u.hruid
- FROM auth_user_md5 AS u
- LEFT JOIN emails AS e ON (e.uid = u.user_id)
- WHERE e.email = {?}", $login);
- if ($res->numRows() == 1) {
- return $res->fetchOneRow();
- }
-
- throw new UserNotFoundException($res->fetchColumn(1));
- }