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