Updates core version.
[platal.git] / classes / user.php
CommitLineData
9f8ebb9f
VZ
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
2d96cf7b 22class User extends PlUser
9f8ebb9f 23{
b1719b13
VZ
24 // Implementation of properties accessors.
25 public function bestEmail()
26 {
27 if (!isset($this->bestalias)) {
28 global $globals;
29 $res = XDB::query("SELECT CONCAT(alias, '@{$globals->mail->domain}')
30 FROM aliases
31 WHERE FIND_IN_SET('bestalias', flags)
32 AND id = {?}", $this->user_id);
33 $this->bestalias = $res->numRows() ? $res->fetchOneCell() : false;
34 }
35 return $this->bestalias;
36 }
37
38 public function forlifeEmail()
39 {
40 if (!isset($this->forlife)) {
41 global $globals;
42 $res = XDB::query("SELECT CONCAT(alias, '@{$globals->mail->domain}')
43 FROM aliases
44 WHERE type = 'a_vie' AND id = {?}", $this->user_id);
45 $this->forlife = $res->numRows() ? $res->fetchOneCell() : false;
46 }
47 return $this->forlife;
48 }
49
50 // Implementation of the login to array(user_id, hruid) function.
51 protected function getLogin($login)
52 {
53 global $globals;
54
55 // If $data is an integer, fetches directly the result.
56 if (is_numeric($login)) {
57 $res = XDB::query("SELECT user_id, hruid FROM auth_user_md5 WHERE user_id = {?}", $login);
58 if ($res->numRows()) {
59 return $res->fetchOneRow();
60 }
61
62 throw new UserNotFoundException();
63 }
64
65 // Checks whether $login is a valid hruid or not.
66 $res = XDB::query("SELECT user_id, hruid FROM auth_user_md5 WHERE hruid = {?}", $login);
67 if ($res->numRows()) {
68 return $res->fetchOneRow();
69 }
70
71 // From now, $login can only by an email alias, or an email redirection.
72 // If it doesn't look like a valid address, appends the plat/al's main domain.
73 $login = trim(strtolower($login));
74 if (strstr($login, '@') === false) {
75 $login = $login . '@' . $globals->mail->domain;
76 }
77
78 // Checks if $login is a valid alias on the main domains.
79 list($mbox, $fqdn) = explode('@', $login);
80 if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) {
81 $res = XDB::query("SELECT u.user_id, u.hruid
82 FROM auth_user_md5 AS u
83 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
84 WHERE a.alias = {?}", $mbox);
85 if ($res->numRows()) {
86 return $res->fetchOneRow();
87 }
88
89 if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) {
90 $res = XDB::query("SELECT u.user_id, u.hruid
91 FROM auth_user_md5 AS u
92 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
93 WHERE a.alias = {?} AND u.promo = {?}", $matches[1], $matches[2]);
94 if ($res->numRows() == 1) {
95 return $res->fetchOneRow();
96 }
97 }
98
99 throw new UserNotFoundException();
100 }
101
102 // Looks for $login as an email alias from the dedicated alias domain.
103 if ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) {
104 $res = XDB::query("SELECT redirect
105 FROM virtual_redirect
106 INNER JOIN virtual USING(vid)
107 WHERE alias = {?}", $mbox . '@' . $globals->mail->alias_dom);
108 if ($redir = $res->fetchOneCell()) {
109 // We now have a valid alias, which has to be translated to an hruid.
110 list($alias, $alias_fqdn) = explode('@', $redir);
111 $res = XDB::query("SELECT u.user_id, u.hruid
112 FROM auth_user_md5 AS u
113 LEFT JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
114 WHERE a.alias = {?}", $alias);
115 if ($res->numRows()) {
116 return $res->fetchOneRow();
117 }
118 }
119
120 throw new UserNotFoundException();
121 }
122
123 // Otherwise, we do suppose $login is an email redirection.
124 $res = XDB::query("SELECT u.user_id, u.hruid
125 FROM auth_user_md5 AS u
126 LEFT JOIN emails AS e ON (e.uid = u.user_id)
127 WHERE e.email = {?}", $login);
128 if ($res->numRows() == 1) {
129 return $res->fetchOneRow();
130 }
131
132 throw new UserNotFoundException($res->fetchColumn(1));
133 }
134
135 // Implementation of the default user callback.
136 public static function _default_user_callback($login, $results)
137 {
138 global $page;
139
140 $result_count = count($results);
141 if ($result_count == 0 || !S::has_perms()) {
142 $page->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login");
143 } else {
144 $page->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results));
145 }
146 }
9f8ebb9f
VZ
147}
148
149// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
150?>