Commit | Line | Data |
---|---|---|
9f8ebb9f VZ |
1 | <?php |
2 | /*************************************************************************** | |
832e6fcb | 3 | * Copyright (C) 2003-2008 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 | { |
3e53a496 FB |
24 | private $_profile_fetched = false; |
25 | private $_profile = null; | |
26 | ||
70232020 | 27 | // Implementation of the login to uid method. |
b1719b13 VZ |
28 | protected function getLogin($login) |
29 | { | |
30 | global $globals; | |
31 | ||
e7b93962 | 32 | if ($login instanceof Profile) { |
3e53a496 FB |
33 | $this->_profile = $login; |
34 | $this->_profile_fetched = true; | |
e7b93962 FB |
35 | $res = XDB::query('SELECT ap.uid |
36 | FROM account_profiles AS ap | |
37 | WHERE ap.pid = {?} AND FIND_IN_SET(\'owner\', perms)', | |
38 | $login->id()); | |
39 | if ($res->numRows()) { | |
40 | return $res->fetchOneCell(); | |
41 | } | |
42 | throw new UserNotFoundException(); | |
43 | } | |
44 | ||
b1719b13 VZ |
45 | // If $data is an integer, fetches directly the result. |
46 | if (is_numeric($login)) { | |
e7b93962 FB |
47 | $res = XDB::query('SELECT a.uid |
48 | FROM accounts AS a | |
49 | WHERE a.uid = {?}', $login); | |
b1719b13 | 50 | if ($res->numRows()) { |
70232020 | 51 | return $res->fetchOneCell(); |
b1719b13 VZ |
52 | } |
53 | ||
54 | throw new UserNotFoundException(); | |
55 | } | |
56 | ||
57 | // Checks whether $login is a valid hruid or not. | |
e7b93962 FB |
58 | $res = XDB::query('SELECT a.uid |
59 | FROM accounts AS a | |
60 | WHERE a.hruid = {?}', $login); | |
b1719b13 | 61 | if ($res->numRows()) { |
70232020 | 62 | return $res->fetchOneCell(); |
b1719b13 VZ |
63 | } |
64 | ||
65 | // From now, $login can only by an email alias, or an email redirection. | |
66 | // If it doesn't look like a valid address, appends the plat/al's main domain. | |
67 | $login = trim(strtolower($login)); | |
68 | if (strstr($login, '@') === false) { | |
69 | $login = $login . '@' . $globals->mail->domain; | |
70 | } | |
71 | ||
72 | // Checks if $login is a valid alias on the main domains. | |
73 | list($mbox, $fqdn) = explode('@', $login); | |
74 | if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) { | |
e7b93962 FB |
75 | $res = XDB::query('SELECT a.uid |
76 | FROM accounts AS a | |
77 | INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN (\'alias\', \'a_vie\')) | |
78 | WHERE al.alias = {?}', $mbox); | |
b1719b13 | 79 | if ($res->numRows()) { |
70232020 | 80 | return $res->fetchOneCell(); |
b1719b13 VZ |
81 | } |
82 | ||
e7b93962 | 83 | /** TODO: implements this by inspecting the profile. |
b1719b13 | 84 | if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) { |
e7b93962 FB |
85 | $res = XDB::query('SELECT a.uid |
86 | FROM accounts AS a | |
87 | INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie')) | |
88 | WHERE al.alias = {?} AND a.promo = {?}', $matches[1], $matches[2]); | |
b1719b13 | 89 | if ($res->numRows() == 1) { |
70232020 | 90 | return $res->fetchOneCell(); |
b1719b13 | 91 | } |
e7b93962 | 92 | }*/ |
b1719b13 VZ |
93 | |
94 | throw new UserNotFoundException(); | |
95 | } | |
96 | ||
97 | // Looks for $login as an email alias from the dedicated alias domain. | |
98 | if ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) { | |
99 | $res = XDB::query("SELECT redirect | |
100 | FROM virtual_redirect | |
101 | INNER JOIN virtual USING(vid) | |
102 | WHERE alias = {?}", $mbox . '@' . $globals->mail->alias_dom); | |
103 | if ($redir = $res->fetchOneCell()) { | |
104 | // We now have a valid alias, which has to be translated to an hruid. | |
105 | list($alias, $alias_fqdn) = explode('@', $redir); | |
e7b93962 FB |
106 | $res = XDB::query("SELECT a.uid |
107 | FROM accounts AS a | |
108 | LEFT JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie')) | |
109 | WHERE al.alias = {?}", $alias); | |
b1719b13 | 110 | if ($res->numRows()) { |
70232020 | 111 | return $res->fetchOneCell(); |
b1719b13 VZ |
112 | } |
113 | } | |
114 | ||
115 | throw new UserNotFoundException(); | |
116 | } | |
117 | ||
cb8a8977 FB |
118 | // Looks for an account with the given email. |
119 | $res = XDB::query('SELECT a.uid | |
120 | FROM accounts AS a | |
121 | WHERE a.email = {?}', $login); | |
122 | if ($res->numRows() == 1) { | |
123 | return $res->fetchOneCell(); | |
124 | } | |
125 | ||
b1719b13 | 126 | // Otherwise, we do suppose $login is an email redirection. |
e7b93962 FB |
127 | $res = XDB::query("SELECT a.uid |
128 | FROM accounts AS a | |
129 | LEFT JOIN emails AS e ON (e.uid = a.uid) | |
b1719b13 VZ |
130 | WHERE e.email = {?}", $login); |
131 | if ($res->numRows() == 1) { | |
70232020 | 132 | return $res->fetchOneCell(); |
b1719b13 VZ |
133 | } |
134 | ||
135 | throw new UserNotFoundException($res->fetchColumn(1)); | |
136 | } | |
137 | ||
832e6fcb FB |
138 | protected static function loadMainFieldsFromUIDs(array $uids, $sorted = null) |
139 | { | |
45dcd6dd | 140 | global $globals; |
832e6fcb FB |
141 | $joins = ''; |
142 | $orderby = ''; | |
45dcd6dd | 143 | $fields = array(); |
832e6fcb FB |
144 | if (!is_null($sorted)) { |
145 | $order = array(); | |
146 | $with_ap = false; | |
147 | $with_pd = false; | |
148 | foreach (explode(',', $sorted) as $part) { | |
149 | $desc = ($part[0] == '-'); | |
150 | if ($desc) { | |
151 | $part = substr($desc, 1); | |
152 | } | |
153 | switch ($part) { | |
154 | case 'promo': | |
155 | $with_pd = true; | |
156 | $with_ap = true; | |
157 | $part = 'IF (pd.promo IS NULL, \'ext\', pd.promo)'; | |
158 | break; | |
159 | case 'full_name': | |
160 | $part = 'a.full_name'; | |
161 | break; | |
162 | case 'display_name': | |
163 | $part = 'a.display_name'; | |
164 | break; | |
165 | default: | |
166 | $part = null; | |
167 | } | |
168 | if (!is_null($part)) { | |
169 | if ($desc) { | |
170 | $part .= ' DESC'; | |
171 | } | |
172 | $order[] = $part; | |
173 | } | |
174 | } | |
175 | if (count($order) > 0) { | |
176 | if ($with_ap) { | |
177 | $joins .= "LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))\n"; | |
178 | } | |
179 | if ($with_pd) { | |
180 | $joins .= "LEFT JOIN profile_display AS pd ON (pd.pid = ap.pid)\n"; | |
181 | } | |
182 | $orderby = 'ORDER BY ' . implode(', ', $order); | |
183 | } | |
184 | } | |
45dcd6dd FB |
185 | if ($globals->asso('id')) { |
186 | $joins .= XDB::format("LEFT JOIN groupex.membres AS gpm ON (gpm.uid = a.uid AND gpm.asso_id = {?})\n", $globals->asso('id')); | |
187 | $fields[] = 'gpm.perms AS group_perms'; | |
188 | } | |
189 | if (count($fields) > 0) { | |
190 | $fields = ', ' . implode(', ', $fields); | |
a3118782 FB |
191 | } else { |
192 | $fields = ''; | |
45dcd6dd FB |
193 | } |
194 | $uids = array_map(array('XDB', 'escape'), $uids); | |
832e6fcb FB |
195 | return XDB::iterator('SELECT a.uid, a.hruid, a.registration_date, |
196 | CONCAT(af.alias, \'@' . $globals->mail->domain . '\') AS forlife, | |
197 | CONCAT(ab.alias, \'@' . $globals->mail->domain . '\') AS bestalias, | |
198 | a.full_name, a.display_name, a.sex = \'female\' AS gender, | |
199 | IF(a.state = \'active\', at.perms, \'\') AS perms, | |
200 | a.email_format, a.is_admin, a.state, a.type, a.skin, | |
201 | FIND_IN_SET(\'watch\', a.flags) AS watch, a.comment, | |
202 | a.weak_password IS NOT NULL AS weak_access, | |
45dcd6dd | 203 | a.token IS NOT NULL AS token_access ' . $fields . ' |
832e6fcb FB |
204 | FROM accounts AS a |
205 | INNER JOIN account_types AS at ON (at.type = a.type) | |
206 | LEFT JOIN aliases AS af ON (af.id = a.uid AND af.type = \'a_vie\') | |
207 | LEFT JOIN aliases AS ab ON (ab.id = a.uid AND FIND_IN_SET(\'bestalias\', ab.flags)) | |
208 | ' . $joins . ' | |
209 | WHERE a.uid IN (' . implode(', ', $uids) . ') | |
210 | ' . $orderby); | |
211 | } | |
212 | ||
70232020 VZ |
213 | // Implementation of the data loader. |
214 | protected function loadMainFields() | |
215 | { | |
c4012d9b VZ |
216 | if ($this->hruid !== null && $this->forlife !== null |
217 | && $this->bestalias !== null && $this->display_name !== null | |
8f2104cb | 218 | && $this->full_name !== null && $this->perms !== null |
c4012d9b | 219 | && $this->gender !== null && $this->email_format !== null) { |
70232020 VZ |
220 | return; |
221 | } | |
832e6fcb | 222 | $this->fillFromArray(self::loadMainFieldsFromUIDs(array($this->user_id))->next()); |
70232020 VZ |
223 | } |
224 | ||
225 | // Specialization of the fillFromArray method, to implement hacks to enable | |
226 | // lazy loading of user's main properties from the session. | |
c4012d9b VZ |
227 | // TODO(vzanotti): remove the conversion hacks once the old codebase will |
228 | // stop being used actively. | |
70232020 VZ |
229 | protected function fillFromArray(array $values) |
230 | { | |
231 | // It might happen that the 'user_id' field is called uid in some places | |
232 | // (eg. in sessions), so we hard link uid to user_id to prevent useless | |
233 | // SQL requests. | |
234 | if (!isset($values['user_id']) && isset($values['uid'])) { | |
235 | $values['user_id'] = $values['uid']; | |
236 | } | |
237 | ||
238 | // Also, if display_name and full_name are not known, but the user's | |
239 | // surname and last name are, we can construct the former two. | |
240 | if (isset($values['prenom']) && isset($values['nom'])) { | |
241 | if (!isset($values['display_name'])) { | |
242 | $values['display_name'] = ($values['prenom'] ? $values['prenom'] : $values['nom']); | |
243 | } | |
244 | if (!isset($values['full_name'])) { | |
245 | $values['full_name'] = $values['prenom'] . ' ' . $values['nom']; | |
246 | } | |
247 | } | |
248 | ||
c4012d9b VZ |
249 | // We also need to convert the gender (usually named "femme"), and the |
250 | // email format parameter (valued "texte" instead of "text"). | |
251 | if (isset($values['femme'])) { | |
252 | $values['gender'] = (bool) $values['femme']; | |
253 | } | |
254 | if (isset($values['mail_fmt'])) { | |
255 | $values['email_format'] = $values['mail_fmt']; | |
256 | } | |
c4012d9b | 257 | |
70232020 VZ |
258 | parent::fillFromArray($values); |
259 | } | |
260 | ||
50d5ec0b FB |
261 | // Specialization of the buildPerms method |
262 | // This function build 'generic' permissions for the user. It does not take | |
263 | // into account page specific permissions (e.g X.net group permissions) | |
264 | protected function buildPerms() | |
265 | { | |
266 | if (!is_null($this->perm_flags)) { | |
267 | return; | |
268 | } | |
269 | if ($this->perms === null) { | |
270 | $this->loadMainFields(); | |
271 | } | |
365ba8c3 | 272 | $this->perm_flags = self::makePerms($this->perms, $this->is_admin); |
50d5ec0b FB |
273 | } |
274 | ||
7f1ff426 FB |
275 | // We do not want to store the password in the object. |
276 | // So, fetch it 'on demand' | |
277 | public function password() | |
278 | { | |
279 | return XDB::fetchOneCell('SELECT a.password | |
280 | FROM accounts AS a | |
281 | WHERE a.uid = {?}', $this->id()); | |
282 | } | |
283 | ||
8f2104cb FB |
284 | /** Overload PlUser::promo(): there no promo defined for a user in the current |
285 | * schema. The promo is a field from the profile. | |
286 | */ | |
287 | public function promo() | |
288 | { | |
289 | if (!$this->hasProfile()) { | |
290 | return ''; | |
291 | } | |
292 | return $this->profile()->promo(); | |
293 | } | |
294 | ||
e7b93962 FB |
295 | /** Return the main profile attached with this account if any. |
296 | */ | |
297 | public function profile() | |
298 | { | |
3e53a496 FB |
299 | if (!$this->_profile_fetched) { |
300 | $this->_profile_fetched = true; | |
301 | $this->_profile = Profile::get($this); | |
302 | } | |
303 | return $this->_profile; | |
304 | } | |
305 | ||
306 | /** Return true if the user has an associated profile. | |
307 | */ | |
308 | public function hasProfile() | |
309 | { | |
310 | return !is_null($this->profile()); | |
311 | } | |
312 | ||
3af21f99 FB |
313 | /** Check if the user can edit to given profile. |
314 | */ | |
315 | public function canEdit(Profile $profile) | |
316 | { | |
317 | // XXX: Check permissions (e.g. secretary permission) | |
318 | // and flags from the profile | |
319 | return XDB::fetchOneCell('SELECT pid | |
320 | FROM account_profiles | |
321 | WHERE uid = {?} AND pid = {?}', | |
322 | $this->id(), $profile->id()); | |
323 | } | |
324 | ||
3e53a496 FB |
325 | /** Get the email alias of the user. |
326 | */ | |
327 | public function emailAlias() | |
328 | { | |
329 | global $globals; | |
8f2104cb FB |
330 | $data = $this->emailAliases($globals->mail->alias_dom); |
331 | if (count($data) > 0) { | |
332 | return array_pop($data); | |
333 | } | |
334 | return null; | |
335 | } | |
336 | ||
337 | /** Get all the aliases the user belongs to. | |
338 | */ | |
339 | public function emailAliases($domain = null) | |
340 | { | |
341 | $where = ''; | |
342 | if (!is_null($domain)) { | |
343 | $where = XDB::format(' AND alias LIKE CONCAT("%@", {?})', $domain); | |
344 | } | |
345 | return XDB::fetchColumn('SELECT v.alias | |
346 | FROM virtual AS v | |
347 | INNER JOIN virtual_redirect AS vr ON (v.vid = vr.vid) | |
348 | WHERE (vr.redirect = {?} OR vr.redirect = {?}) | |
349 | ' . $where, | |
350 | $this->forlifeEmail(), $this->m4xForlifeEmail()); | |
3e53a496 FB |
351 | } |
352 | ||
353 | /** Get the alternative forlife email | |
354 | * TODO: remove this uber-ugly hack. The issue is that you need to remove | |
355 | * all @m4x.org addresses in virtual_redirect first. | |
356 | * XXX: This is juste to make code more readable, to be remove as soon as possible | |
357 | */ | |
358 | public function m4xForlifeEmail() | |
359 | { | |
360 | global $globals; | |
361 | trigger_error('USING M4X FORLIFE', E_USER_NOTICE); | |
362 | return $this->login() . '@' . $globals->mail->domain2; | |
e7b93962 FB |
363 | } |
364 | ||
50d5ec0b | 365 | // Return permission flags for a given permission level. |
365ba8c3 | 366 | public static function makePerms($perms, $is_admin) |
50d5ec0b | 367 | { |
365ba8c3 | 368 | $flags = new PlFlagSet($perms); |
50d5ec0b | 369 | $flags->addFlag(PERMS_USER); |
365ba8c3 | 370 | if ($is_admin) { |
50d5ec0b FB |
371 | $flags->addFlag(PERMS_ADMIN); |
372 | } | |
373 | return $flags; | |
374 | } | |
375 | ||
b1719b13 VZ |
376 | // Implementation of the default user callback. |
377 | public static function _default_user_callback($login, $results) | |
378 | { | |
b1719b13 VZ |
379 | $result_count = count($results); |
380 | if ($result_count == 0 || !S::has_perms()) { | |
70232020 | 381 | Platal::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login"); |
b1719b13 | 382 | } else { |
70232020 | 383 | Platal::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results)); |
b1719b13 VZ |
384 | } |
385 | } | |
70232020 VZ |
386 | |
387 | // Implementation of the static email locality checker. | |
388 | public static function isForeignEmailAddress($email) | |
389 | { | |
390 | global $globals; | |
391 | if (strpos($email, '@') === false) { | |
392 | return false; | |
393 | } | |
394 | ||
395 | list($user, $dom) = explode('@', $email); | |
396 | return $dom != $globals->mail->domain && | |
397 | $dom != $globals->mail->domain2 && | |
398 | $dom != $globals->mail->alias_dom && | |
399 | $dom != $globals->mail->alias_dom2; | |
400 | } | |
832e6fcb FB |
401 | |
402 | // Fetch a set of users from a list of UIDs | |
403 | public static function getBuildUsersWithUIDs(array $uids, $sortby = null) | |
404 | { | |
405 | $fields = self::loadMainFieldsFromUIDs($uids, $sortby); | |
406 | $users = array(); | |
407 | while (($list = $fields->next())) { | |
408 | $users[] = User::getSilentWithValues(null, $list); | |
409 | } | |
410 | return $users; | |
411 | } | |
9f8ebb9f VZ |
412 | } |
413 | ||
414 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
415 | ?> |