Commit | Line | Data |
---|---|---|
9f8ebb9f VZ |
1 | <?php |
2 | /*************************************************************************** | |
8d84c630 | 3 | * Copyright (C) 2003-2009 Polytechnique.org * |
9f8ebb9f VZ |
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 | 22 | class User extends PlUser |
9f8ebb9f | 23 | { |
956608bc RB |
24 | // Additional fields (non core) |
25 | protected $promo = null; | |
26 | ||
6c41ada0 RB |
27 | public function promo() |
28 | { | |
29 | return $this->promo; | |
30 | } | |
31 | ||
70232020 | 32 | // Implementation of the login to uid method. |
b1719b13 VZ |
33 | protected function getLogin($login) |
34 | { | |
35 | global $globals; | |
36 | ||
f6c58d14 VZ |
37 | if (!$login) { |
38 | throw new UserNotFoundException(); | |
39 | } | |
40 | ||
b1719b13 VZ |
41 | // If $data is an integer, fetches directly the result. |
42 | if (is_numeric($login)) { | |
70232020 | 43 | $res = XDB::query("SELECT user_id FROM auth_user_md5 WHERE user_id = {?}", $login); |
b1719b13 | 44 | if ($res->numRows()) { |
70232020 | 45 | return $res->fetchOneCell(); |
b1719b13 VZ |
46 | } |
47 | ||
48 | throw new UserNotFoundException(); | |
49 | } | |
50 | ||
51 | // Checks whether $login is a valid hruid or not. | |
70232020 | 52 | $res = XDB::query("SELECT user_id FROM auth_user_md5 WHERE hruid = {?}", $login); |
b1719b13 | 53 | if ($res->numRows()) { |
70232020 | 54 | return $res->fetchOneCell(); |
b1719b13 VZ |
55 | } |
56 | ||
57 | // From now, $login can only by an email alias, or an email redirection. | |
58 | // If it doesn't look like a valid address, appends the plat/al's main domain. | |
59 | $login = trim(strtolower($login)); | |
60 | if (strstr($login, '@') === false) { | |
61 | $login = $login . '@' . $globals->mail->domain; | |
62 | } | |
63 | ||
64 | // Checks if $login is a valid alias on the main domains. | |
65 | list($mbox, $fqdn) = explode('@', $login); | |
66 | if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) { | |
70232020 | 67 | $res = XDB::query("SELECT u.user_id |
b1719b13 VZ |
68 | FROM auth_user_md5 AS u |
69 | INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie')) | |
70 | WHERE a.alias = {?}", $mbox); | |
71 | if ($res->numRows()) { | |
70232020 | 72 | return $res->fetchOneCell(); |
b1719b13 VZ |
73 | } |
74 | ||
75 | if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) { | |
70232020 | 76 | $res = XDB::query("SELECT u.user_id |
b1719b13 VZ |
77 | FROM auth_user_md5 AS u |
78 | INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie')) | |
79 | WHERE a.alias = {?} AND u.promo = {?}", $matches[1], $matches[2]); | |
80 | if ($res->numRows() == 1) { | |
70232020 | 81 | return $res->fetchOneCell(); |
b1719b13 VZ |
82 | } |
83 | } | |
84 | ||
85 | throw new UserNotFoundException(); | |
86 | } | |
87 | ||
88 | // Looks for $login as an email alias from the dedicated alias domain. | |
89 | if ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) { | |
90 | $res = XDB::query("SELECT redirect | |
91 | FROM virtual_redirect | |
92 | INNER JOIN virtual USING(vid) | |
93 | WHERE alias = {?}", $mbox . '@' . $globals->mail->alias_dom); | |
94 | if ($redir = $res->fetchOneCell()) { | |
95 | // We now have a valid alias, which has to be translated to an hruid. | |
96 | list($alias, $alias_fqdn) = explode('@', $redir); | |
70232020 | 97 | $res = XDB::query("SELECT u.user_id |
b1719b13 VZ |
98 | FROM auth_user_md5 AS u |
99 | LEFT JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie')) | |
100 | WHERE a.alias = {?}", $alias); | |
101 | if ($res->numRows()) { | |
70232020 | 102 | return $res->fetchOneCell(); |
b1719b13 VZ |
103 | } |
104 | } | |
105 | ||
106 | throw new UserNotFoundException(); | |
107 | } | |
108 | ||
109 | // Otherwise, we do suppose $login is an email redirection. | |
70232020 | 110 | $res = XDB::query("SELECT u.user_id |
b1719b13 VZ |
111 | FROM auth_user_md5 AS u |
112 | LEFT JOIN emails AS e ON (e.uid = u.user_id) | |
113 | WHERE e.email = {?}", $login); | |
114 | if ($res->numRows() == 1) { | |
70232020 | 115 | return $res->fetchOneCell(); |
b1719b13 VZ |
116 | } |
117 | ||
118 | throw new UserNotFoundException($res->fetchColumn(1)); | |
119 | } | |
120 | ||
70232020 VZ |
121 | // Implementation of the data loader. |
122 | protected function loadMainFields() | |
123 | { | |
c4012d9b VZ |
124 | if ($this->hruid !== null && $this->forlife !== null |
125 | && $this->bestalias !== null && $this->display_name !== null | |
126 | && $this->full_name !== null && $this->promo !== null && $this->perms !== null | |
127 | && $this->gender !== null && $this->email_format !== null) { | |
70232020 VZ |
128 | return; |
129 | } | |
130 | ||
131 | global $globals; | |
132 | $res = XDB::query("SELECT u.hruid, u.promo, | |
133 | CONCAT(af.alias, '@{$globals->mail->domain}') AS forlife, | |
134 | CONCAT(ab.alias, '@{$globals->mail->domain}') AS bestalias, | |
77057a5c | 135 | CONCAT(u.prenom, ' ', IF(u.nom_usage <> '', u.nom_usage, u.nom)) AS full_name, |
50d5ec0b | 136 | IF(u.prenom != '', u.prenom, u.nom) AS display_name, |
c4012d9b VZ |
137 | FIND_IN_SET('femme', u.flags) AS gender, |
138 | q.core_mail_fmt AS email_format, | |
50d5ec0b | 139 | u.perms |
70232020 | 140 | FROM auth_user_md5 AS u |
c4012d9b | 141 | LEFT JOIN auth_user_quick AS q ON (q.user_id = u.user_id) |
70232020 VZ |
142 | LEFT JOIN aliases AS af ON (af.id = u.user_id AND af.type = 'a_vie') |
143 | LEFT JOIN aliases AS ab ON (ab.id = u.user_id AND FIND_IN_SET('bestalias', ab.flags)) | |
144 | WHERE u.user_id = {?}", $this->user_id); | |
145 | $this->fillFromArray($res->fetchOneAssoc()); | |
146 | } | |
147 | ||
148 | // Specialization of the fillFromArray method, to implement hacks to enable | |
149 | // lazy loading of user's main properties from the session. | |
c4012d9b VZ |
150 | // TODO(vzanotti): remove the conversion hacks once the old codebase will |
151 | // stop being used actively. | |
70232020 VZ |
152 | protected function fillFromArray(array $values) |
153 | { | |
154 | // It might happen that the 'user_id' field is called uid in some places | |
155 | // (eg. in sessions), so we hard link uid to user_id to prevent useless | |
156 | // SQL requests. | |
157 | if (!isset($values['user_id']) && isset($values['uid'])) { | |
158 | $values['user_id'] = $values['uid']; | |
159 | } | |
160 | ||
161 | // Also, if display_name and full_name are not known, but the user's | |
162 | // surname and last name are, we can construct the former two. | |
163 | if (isset($values['prenom']) && isset($values['nom'])) { | |
164 | if (!isset($values['display_name'])) { | |
165 | $values['display_name'] = ($values['prenom'] ? $values['prenom'] : $values['nom']); | |
166 | } | |
167 | if (!isset($values['full_name'])) { | |
168 | $values['full_name'] = $values['prenom'] . ' ' . $values['nom']; | |
169 | } | |
170 | } | |
171 | ||
c4012d9b VZ |
172 | // We also need to convert the gender (usually named "femme"), and the |
173 | // email format parameter (valued "texte" instead of "text"). | |
174 | if (isset($values['femme'])) { | |
175 | $values['gender'] = (bool) $values['femme']; | |
176 | } | |
177 | if (isset($values['mail_fmt'])) { | |
178 | $values['email_format'] = $values['mail_fmt']; | |
179 | } | |
180 | if (isset($values['email_format'])) { | |
181 | $values['email_format'] = ($values['email_format'] ? self::FORMAT_HTML : self::FORMAT_TEXT); | |
182 | } | |
183 | ||
70232020 VZ |
184 | parent::fillFromArray($values); |
185 | } | |
186 | ||
50d5ec0b FB |
187 | // Specialization of the buildPerms method |
188 | // This function build 'generic' permissions for the user. It does not take | |
189 | // into account page specific permissions (e.g X.net group permissions) | |
190 | protected function buildPerms() | |
191 | { | |
192 | if (!is_null($this->perm_flags)) { | |
193 | return; | |
194 | } | |
195 | if ($this->perms === null) { | |
196 | $this->loadMainFields(); | |
197 | } | |
198 | $this->perm_flags = self::makePerms($this->perms); | |
199 | } | |
200 | ||
d69477d3 FB |
201 | // Return the password of the user |
202 | public function password() | |
203 | { | |
204 | return XDB::fetchOneCell('SELECT u.password | |
205 | FROM auth_user_md5 AS u | |
206 | WHERE u.user_id = {?}', $this->id()); | |
207 | } | |
208 | ||
50d5ec0b FB |
209 | // Return permission flags for a given permission level. |
210 | public static function makePerms($perms) | |
211 | { | |
212 | $flags = new PlFlagSet(); | |
213 | if (is_null($flags) || $perms == 'disabled' || $perms == 'ext') { | |
214 | return $flags; | |
215 | } | |
216 | $flags->addFlag(PERMS_USER); | |
217 | if ($perms == 'admin') { | |
218 | $flags->addFlag(PERMS_ADMIN); | |
219 | } | |
220 | return $flags; | |
221 | } | |
222 | ||
b1719b13 VZ |
223 | // Implementation of the default user callback. |
224 | public static function _default_user_callback($login, $results) | |
225 | { | |
b1719b13 | 226 | $result_count = count($results); |
dd70cd28 | 227 | if ($result_count == 0 || !S::admin()) { |
70232020 | 228 | Platal::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login"); |
b1719b13 | 229 | } else { |
70232020 | 230 | Platal::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results)); |
b1719b13 VZ |
231 | } |
232 | } | |
70232020 VZ |
233 | |
234 | // Implementation of the static email locality checker. | |
235 | public static function isForeignEmailAddress($email) | |
236 | { | |
237 | global $globals; | |
238 | if (strpos($email, '@') === false) { | |
239 | return false; | |
240 | } | |
241 | ||
242 | list($user, $dom) = explode('@', $email); | |
243 | return $dom != $globals->mail->domain && | |
244 | $dom != $globals->mail->domain2 && | |
245 | $dom != $globals->mail->alias_dom && | |
246 | $dom != $globals->mail->alias_dom2; | |
247 | } | |
9f8ebb9f VZ |
248 | } |
249 | ||
250 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
251 | ?> |