Commit | Line | Data |
---|---|---|
9f8ebb9f VZ |
1 | <?php |
2 | /*************************************************************************** | |
9f5bd98e | 3 | * Copyright (C) 2003-2010 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 | ||
956608bc RB |
27 | // Additional fields (non core) |
28 | protected $promo = null; | |
29 | ||
70232020 | 30 | // Implementation of the login to uid method. |
b1719b13 VZ |
31 | protected function getLogin($login) |
32 | { | |
33 | global $globals; | |
34 | ||
f6c58d14 VZ |
35 | if (!$login) { |
36 | throw new UserNotFoundException(); | |
37 | } | |
38 | ||
455ea0c9 FB |
39 | if ($login instanceof User) { |
40 | $machin->id(); | |
41 | } | |
42 | ||
e7b93962 | 43 | if ($login instanceof Profile) { |
3e53a496 FB |
44 | $this->_profile = $login; |
45 | $this->_profile_fetched = true; | |
e7b93962 FB |
46 | $res = XDB::query('SELECT ap.uid |
47 | FROM account_profiles AS ap | |
48 | WHERE ap.pid = {?} AND FIND_IN_SET(\'owner\', perms)', | |
49 | $login->id()); | |
50 | if ($res->numRows()) { | |
51 | return $res->fetchOneCell(); | |
52 | } | |
53 | throw new UserNotFoundException(); | |
54 | } | |
55 | ||
b1719b13 VZ |
56 | // If $data is an integer, fetches directly the result. |
57 | if (is_numeric($login)) { | |
e7b93962 FB |
58 | $res = XDB::query('SELECT a.uid |
59 | FROM accounts AS a | |
60 | WHERE a.uid = {?}', $login); | |
b1719b13 | 61 | if ($res->numRows()) { |
70232020 | 62 | return $res->fetchOneCell(); |
b1719b13 VZ |
63 | } |
64 | ||
65 | throw new UserNotFoundException(); | |
66 | } | |
67 | ||
68 | // Checks whether $login is a valid hruid or not. | |
e7b93962 FB |
69 | $res = XDB::query('SELECT a.uid |
70 | FROM accounts AS a | |
71 | WHERE a.hruid = {?}', $login); | |
b1719b13 | 72 | if ($res->numRows()) { |
70232020 | 73 | return $res->fetchOneCell(); |
b1719b13 VZ |
74 | } |
75 | ||
76 | // From now, $login can only by an email alias, or an email redirection. | |
77 | // If it doesn't look like a valid address, appends the plat/al's main domain. | |
78 | $login = trim(strtolower($login)); | |
79 | if (strstr($login, '@') === false) { | |
80 | $login = $login . '@' . $globals->mail->domain; | |
81 | } | |
82 | ||
83 | // Checks if $login is a valid alias on the main domains. | |
84 | list($mbox, $fqdn) = explode('@', $login); | |
85 | if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) { | |
e7b93962 FB |
86 | $res = XDB::query('SELECT a.uid |
87 | FROM accounts AS a | |
fe13bc1d | 88 | INNER JOIN aliases AS al ON (al.uid = a.uid AND al.type IN (\'alias\', \'a_vie\')) |
e7b93962 | 89 | WHERE al.alias = {?}', $mbox); |
b1719b13 | 90 | if ($res->numRows()) { |
70232020 | 91 | return $res->fetchOneCell(); |
b1719b13 VZ |
92 | } |
93 | ||
e7b93962 | 94 | /** TODO: implements this by inspecting the profile. |
b1719b13 | 95 | if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) { |
e7b93962 FB |
96 | $res = XDB::query('SELECT a.uid |
97 | FROM accounts AS a | |
98 | INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie')) | |
99 | WHERE al.alias = {?} AND a.promo = {?}', $matches[1], $matches[2]); | |
b1719b13 | 100 | if ($res->numRows() == 1) { |
70232020 | 101 | return $res->fetchOneCell(); |
b1719b13 | 102 | } |
e7b93962 | 103 | }*/ |
b1719b13 VZ |
104 | |
105 | throw new UserNotFoundException(); | |
106 | } | |
107 | ||
108 | // Looks for $login as an email alias from the dedicated alias domain. | |
109 | if ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) { | |
110 | $res = XDB::query("SELECT redirect | |
111 | FROM virtual_redirect | |
112 | INNER JOIN virtual USING(vid) | |
113 | WHERE alias = {?}", $mbox . '@' . $globals->mail->alias_dom); | |
114 | if ($redir = $res->fetchOneCell()) { | |
115 | // We now have a valid alias, which has to be translated to an hruid. | |
116 | list($alias, $alias_fqdn) = explode('@', $redir); | |
e7b93962 FB |
117 | $res = XDB::query("SELECT a.uid |
118 | FROM accounts AS a | |
fe13bc1d | 119 | LEFT JOIN aliases AS al ON (al.uid = a.uid AND al.type IN ('alias', 'a_vie')) |
e7b93962 | 120 | WHERE al.alias = {?}", $alias); |
b1719b13 | 121 | if ($res->numRows()) { |
70232020 | 122 | return $res->fetchOneCell(); |
b1719b13 VZ |
123 | } |
124 | } | |
125 | ||
126 | throw new UserNotFoundException(); | |
127 | } | |
128 | ||
cb8a8977 FB |
129 | // Looks for an account with the given email. |
130 | $res = XDB::query('SELECT a.uid | |
131 | FROM accounts AS a | |
132 | WHERE a.email = {?}', $login); | |
133 | if ($res->numRows() == 1) { | |
134 | return $res->fetchOneCell(); | |
135 | } | |
136 | ||
b1719b13 | 137 | // Otherwise, we do suppose $login is an email redirection. |
e7b93962 FB |
138 | $res = XDB::query("SELECT a.uid |
139 | FROM accounts AS a | |
140 | LEFT JOIN emails AS e ON (e.uid = a.uid) | |
b1719b13 VZ |
141 | WHERE e.email = {?}", $login); |
142 | if ($res->numRows() == 1) { | |
70232020 | 143 | return $res->fetchOneCell(); |
b1719b13 VZ |
144 | } |
145 | ||
146 | throw new UserNotFoundException($res->fetchColumn(1)); | |
147 | } | |
148 | ||
0d906109 | 149 | protected static function loadMainFieldsFromUIDs(array $uids, $respect_order = true) |
832e6fcb | 150 | { |
45dcd6dd | 151 | global $globals; |
832e6fcb | 152 | $joins = ''; |
45dcd6dd | 153 | $fields = array(); |
45dcd6dd | 154 | if ($globals->asso('id')) { |
eb41eda9 | 155 | $joins .= XDB::format("LEFT JOIN group_members AS gpm ON (gpm.uid = a.uid AND gpm.asso_id = {?})\n", $globals->asso('id')); |
45dcd6dd | 156 | $fields[] = 'gpm.perms AS group_perms'; |
a6761ca9 | 157 | $fields[] = 'gpm.comm AS group_comm'; |
45dcd6dd FB |
158 | } |
159 | if (count($fields) > 0) { | |
160 | $fields = ', ' . implode(', ', $fields); | |
a3118782 FB |
161 | } else { |
162 | $fields = ''; | |
45dcd6dd | 163 | } |
0d906109 RB |
164 | |
165 | if ($respect_order) { | |
166 | $order = 'ORDER BY ' . XDB::formatCustomOrder('a.uid', $uids); | |
167 | } else { | |
168 | $order = ''; | |
169 | } | |
170 | ||
45dcd6dd | 171 | $uids = array_map(array('XDB', 'escape'), $uids); |
0d906109 | 172 | |
777c5910 | 173 | return XDB::iterator('SELECT a.uid, a.hruid, a.registration_date, ah.alias AS homonym, |
832e6fcb | 174 | CONCAT(af.alias, \'@' . $globals->mail->domain . '\') AS forlife, |
c82aa04c | 175 | CONCAT(af.alias, \'@' . $globals->mail->domain2 . '\') AS forlife_alternate, |
832e6fcb | 176 | CONCAT(ab.alias, \'@' . $globals->mail->domain . '\') AS bestalias, |
c82aa04c | 177 | CONCAT(ab.alias, \'@' . $globals->mail->domain2 . '\') AS bestalias_alternate, |
832e6fcb FB |
178 | a.full_name, a.display_name, a.sex = \'female\' AS gender, |
179 | IF(a.state = \'active\', at.perms, \'\') AS perms, | |
180 | a.email_format, a.is_admin, a.state, a.type, a.skin, | |
181 | FIND_IN_SET(\'watch\', a.flags) AS watch, a.comment, | |
182 | a.weak_password IS NOT NULL AS weak_access, | |
2c411733 FB |
183 | a.token IS NOT NULL AS token_access, |
184 | (e.email IS NULL AND NOT FIND_IN_SET(\'googleapps\', eo.storage)) AND a.state != \'pending\' AS lost | |
185 | ' . $fields . ' | |
832e6fcb FB |
186 | FROM accounts AS a |
187 | INNER JOIN account_types AS at ON (at.type = a.type) | |
fe13bc1d FB |
188 | LEFT JOIN aliases AS af ON (af.uid = a.uid AND af.type = \'a_vie\') |
189 | LEFT JOIN aliases AS ab ON (ab.uid = a.uid AND FIND_IN_SET(\'bestalias\', ab.flags)) | |
777c5910 | 190 | LEFT JOIN aliases AS ah ON (ah.uid = a.uid AND ah.type = \'homonyme\') |
2c411733 FB |
191 | LEFT JOIN emails AS e ON (e.uid = a.uid AND e.flags = \'active\') |
192 | LEFT JOIN email_options AS eo ON (eo.uid = a.uid) | |
d865c296 | 193 | ' . $joins . ' |
832e6fcb | 194 | WHERE a.uid IN (' . implode(', ', $uids) . ') |
0d906109 RB |
195 | GROUP BY a.uid |
196 | ' . $order); | |
832e6fcb FB |
197 | } |
198 | ||
70232020 VZ |
199 | // Implementation of the data loader. |
200 | protected function loadMainFields() | |
201 | { | |
c4012d9b VZ |
202 | if ($this->hruid !== null && $this->forlife !== null |
203 | && $this->bestalias !== null && $this->display_name !== null | |
8f2104cb | 204 | && $this->full_name !== null && $this->perms !== null |
c4012d9b | 205 | && $this->gender !== null && $this->email_format !== null) { |
70232020 VZ |
206 | return; |
207 | } | |
832e6fcb | 208 | $this->fillFromArray(self::loadMainFieldsFromUIDs(array($this->user_id))->next()); |
70232020 VZ |
209 | } |
210 | ||
211 | // Specialization of the fillFromArray method, to implement hacks to enable | |
212 | // lazy loading of user's main properties from the session. | |
c4012d9b VZ |
213 | // TODO(vzanotti): remove the conversion hacks once the old codebase will |
214 | // stop being used actively. | |
70232020 VZ |
215 | protected function fillFromArray(array $values) |
216 | { | |
217 | // It might happen that the 'user_id' field is called uid in some places | |
218 | // (eg. in sessions), so we hard link uid to user_id to prevent useless | |
219 | // SQL requests. | |
220 | if (!isset($values['user_id']) && isset($values['uid'])) { | |
221 | $values['user_id'] = $values['uid']; | |
222 | } | |
223 | ||
224 | // Also, if display_name and full_name are not known, but the user's | |
225 | // surname and last name are, we can construct the former two. | |
226 | if (isset($values['prenom']) && isset($values['nom'])) { | |
227 | if (!isset($values['display_name'])) { | |
228 | $values['display_name'] = ($values['prenom'] ? $values['prenom'] : $values['nom']); | |
229 | } | |
230 | if (!isset($values['full_name'])) { | |
231 | $values['full_name'] = $values['prenom'] . ' ' . $values['nom']; | |
232 | } | |
233 | } | |
234 | ||
c4012d9b VZ |
235 | // We also need to convert the gender (usually named "femme"), and the |
236 | // email format parameter (valued "texte" instead of "text"). | |
237 | if (isset($values['femme'])) { | |
238 | $values['gender'] = (bool) $values['femme']; | |
239 | } | |
240 | if (isset($values['mail_fmt'])) { | |
241 | $values['email_format'] = $values['mail_fmt']; | |
242 | } | |
c4012d9b | 243 | |
70232020 VZ |
244 | parent::fillFromArray($values); |
245 | } | |
246 | ||
50d5ec0b FB |
247 | // Specialization of the buildPerms method |
248 | // This function build 'generic' permissions for the user. It does not take | |
249 | // into account page specific permissions (e.g X.net group permissions) | |
250 | protected function buildPerms() | |
251 | { | |
252 | if (!is_null($this->perm_flags)) { | |
253 | return; | |
254 | } | |
255 | if ($this->perms === null) { | |
256 | $this->loadMainFields(); | |
257 | } | |
365ba8c3 | 258 | $this->perm_flags = self::makePerms($this->perms, $this->is_admin); |
50d5ec0b FB |
259 | } |
260 | ||
7f1ff426 FB |
261 | // We do not want to store the password in the object. |
262 | // So, fetch it 'on demand' | |
263 | public function password() | |
264 | { | |
265 | return XDB::fetchOneCell('SELECT a.password | |
266 | FROM accounts AS a | |
267 | WHERE a.uid = {?}', $this->id()); | |
268 | } | |
269 | ||
8f2104cb FB |
270 | /** Overload PlUser::promo(): there no promo defined for a user in the current |
271 | * schema. The promo is a field from the profile. | |
272 | */ | |
273 | public function promo() | |
274 | { | |
275 | if (!$this->hasProfile()) { | |
276 | return ''; | |
277 | } | |
278 | return $this->profile()->promo(); | |
279 | } | |
280 | ||
a6761ca9 FB |
281 | public function firstName() |
282 | { | |
283 | if (!$this->hasProfile()) { | |
284 | return $this->displayName(); | |
285 | } | |
286 | return $this->profile()->firstName(); | |
287 | } | |
288 | ||
289 | public function lastName() | |
290 | { | |
291 | if (!$this->hasProfile()) { | |
292 | return ''; | |
293 | } | |
294 | return $this->profile()->lastName(); | |
295 | } | |
296 | ||
e7b93962 FB |
297 | /** Return the main profile attached with this account if any. |
298 | */ | |
299 | public function profile() | |
300 | { | |
3e53a496 FB |
301 | if (!$this->_profile_fetched) { |
302 | $this->_profile_fetched = true; | |
303 | $this->_profile = Profile::get($this); | |
304 | } | |
305 | return $this->_profile; | |
306 | } | |
307 | ||
308 | /** Return true if the user has an associated profile. | |
309 | */ | |
310 | public function hasProfile() | |
311 | { | |
312 | return !is_null($this->profile()); | |
313 | } | |
314 | ||
3af21f99 FB |
315 | /** Check if the user can edit to given profile. |
316 | */ | |
317 | public function canEdit(Profile $profile) | |
318 | { | |
319 | // XXX: Check permissions (e.g. secretary permission) | |
320 | // and flags from the profile | |
321 | return XDB::fetchOneCell('SELECT pid | |
322 | FROM account_profiles | |
323 | WHERE uid = {?} AND pid = {?}', | |
324 | $this->id(), $profile->id()); | |
325 | } | |
326 | ||
3e53a496 FB |
327 | /** Get the email alias of the user. |
328 | */ | |
329 | public function emailAlias() | |
330 | { | |
331 | global $globals; | |
8f2104cb FB |
332 | $data = $this->emailAliases($globals->mail->alias_dom); |
333 | if (count($data) > 0) { | |
334 | return array_pop($data); | |
335 | } | |
336 | return null; | |
337 | } | |
338 | ||
339 | /** Get all the aliases the user belongs to. | |
340 | */ | |
a6761ca9 | 341 | public function emailAliases($domain = null, $type = 'user', $sub_state = false) |
8f2104cb | 342 | { |
a6761ca9 FB |
343 | $join = XDB::format('(vr.redirect = {?} OR vr.redirect = {?}) ', |
344 | $this->forlifeEmail(), $this->m4xForlifeEmail()); | |
8f2104cb FB |
345 | $where = ''; |
346 | if (!is_null($domain)) { | |
a6761ca9 FB |
347 | $where = XDB::format('WHERE v.alias LIKE CONCAT("%@", {?})', $domain); |
348 | } | |
349 | if (!is_null($type)) { | |
350 | if (empty($where)) { | |
351 | $where = XDB::format('WHERE v.type = {?}', $type); | |
352 | } else { | |
353 | $where .= XDB::format(' AND v.type = {?}', $type); | |
354 | } | |
355 | } | |
356 | if ($sub_state) { | |
357 | return XDB::fetchAllAssoc('alias', 'SELECT v.alias, vr.redirect IS NOT NULL AS sub | |
358 | FROM virtual AS v | |
359 | LEFT JOIN virtual_redirect AS vr ON (v.vid = vr.vid AND ' . $join . ') | |
360 | ' . $where); | |
361 | } else { | |
362 | return XDB::fetchColumn('SELECT v.alias | |
363 | FROM virtual AS v | |
364 | INNER JOIN virtual_redirect AS vr ON (v.vid = vr.vid AND ' . $join . ') | |
365 | ' . $where); | |
8f2104cb | 366 | } |
3e53a496 FB |
367 | } |
368 | ||
369 | /** Get the alternative forlife email | |
370 | * TODO: remove this uber-ugly hack. The issue is that you need to remove | |
371 | * all @m4x.org addresses in virtual_redirect first. | |
372 | * XXX: This is juste to make code more readable, to be remove as soon as possible | |
373 | */ | |
374 | public function m4xForlifeEmail() | |
375 | { | |
376 | global $globals; | |
377 | trigger_error('USING M4X FORLIFE', E_USER_NOTICE); | |
378 | return $this->login() . '@' . $globals->mail->domain2; | |
e7b93962 FB |
379 | } |
380 | ||
38c6fe96 FB |
381 | |
382 | /** Get marketing informations | |
383 | */ | |
384 | private function fetchMarketingData() | |
385 | { | |
386 | if (isset($this->last_known_email)) { | |
387 | return; | |
388 | } | |
389 | $infos = XDB::fetchOneAssoc('SELECT IF (MAX(m.last) > p.relance, MAX(m.last), p.relance) AS last_relance, | |
390 | p.email AS last_known_email | |
391 | FROM register_pending AS p | |
392 | LEFT JOIN register_marketing AS m ON (p.uid = m.uid) | |
393 | WHERE p.uid = {?} | |
394 | GROUP BY p.uid', $this->id()); | |
395 | if (!$infos) { | |
396 | $infos = array('last_relance' => null, 'last_known_email' => null); | |
397 | } | |
398 | $this->fillFromArray($infos); | |
399 | } | |
400 | ||
401 | public function lastMarketingRelance() | |
402 | { | |
403 | $this->fetchMarketingData(); | |
404 | return $this->last_relance; | |
405 | } | |
406 | ||
407 | public function lastKnownEmail() | |
408 | { | |
409 | $this->fetchMarketingData(); | |
410 | return $this->last_known_email; | |
411 | } | |
412 | ||
009b8ab7 FB |
413 | |
414 | /** Get watch informations | |
415 | */ | |
416 | private function fetchWatchData() | |
417 | { | |
418 | if (isset($this->watch_actions)) { | |
419 | return; | |
420 | } | |
421 | $watch = XDB::fetchOneAssoc('SELECT flags AS watch_flags, actions AS watch_actions, | |
422 | UNIX_TIMESTAMP(last) AS watch_last | |
423 | FROM watch | |
424 | WHERE uid = {?}', $this->id()); | |
425 | $watch['watch_flags'] = new PlFlagSet($watch['watch_flags']); | |
426 | $watch['watch_actions'] = new PlFlagSet($watch['watch_actions']); | |
427 | $watch['watch_promos'] = XDB::fetchColumn('SELECT promo | |
428 | FROM watch_promo | |
429 | WHERE uid = {?}', $this->id()); | |
430 | $watch['watch_users'] = XDB::fetchColumn('SELECT ni_id | |
431 | FROM watch_nonins | |
432 | WHERE uid = {?}', $this->id()); | |
433 | $this->fillFromArray($watch); | |
434 | } | |
435 | ||
a87530ea | 436 | public function watchType($type) |
009b8ab7 FB |
437 | { |
438 | $this->fetchWatchData(); | |
439 | return $this->watch_actions->hasFlag($type); | |
440 | } | |
441 | ||
442 | public function watchContacts() | |
443 | { | |
444 | $this->fetchWatchData(); | |
445 | return $this->watch_flags->hasFlag('contacts'); | |
446 | } | |
447 | ||
448 | public function watchEmail() | |
449 | { | |
450 | $this->fetchWatchData(); | |
451 | return $this->watch_flags->hasFlag('mail'); | |
452 | } | |
453 | ||
454 | public function watchPromos() | |
455 | { | |
456 | $this->fetchWatchData(); | |
457 | return $this->watch_promos; | |
458 | } | |
459 | ||
460 | public function watchUsers() | |
461 | { | |
462 | $this->fetchWatchData(); | |
463 | return $this->watch_users; | |
464 | } | |
465 | ||
466 | public function watchLast() | |
467 | { | |
468 | $this->fetchWatchData(); | |
469 | return $this->watch_last; | |
470 | } | |
471 | ||
c350577b FB |
472 | |
473 | // Contacts | |
474 | private $contacts = null; | |
48e683dd | 475 | private function fetchContacts() |
c350577b | 476 | { |
76cbe885 | 477 | if (is_null($this->contacts)) { |
c350577b FB |
478 | $this->contacts = XDB::fetchAllAssoc('contact', 'SELECT * |
479 | FROM contacts | |
480 | WHERE uid = {?}', | |
481 | $this->id()); | |
482 | } | |
48e683dd FB |
483 | } |
484 | ||
485 | public function iterContacts() | |
486 | { | |
487 | $this->fetchContacts(); | |
488 | return self::iterOverUIDs(array_keys($this->contacts)); | |
489 | } | |
490 | ||
491 | public function getContacts() | |
492 | { | |
493 | $this->fetchContacts(); | |
494 | return self::getBulkUsersWithUIDs(array_keys($this->contacts)); | |
495 | } | |
496 | ||
497 | public function isContact(PlUser &$user) | |
498 | { | |
499 | $this->fetchContacts(); | |
c350577b FB |
500 | return isset($this->contacts[$user->id()]); |
501 | } | |
502 | ||
f5ef8b57 RB |
503 | // Groupes X |
504 | private $groups = null; | |
505 | public function groups() | |
506 | { | |
507 | if (is_null($this->groups)) { | |
508 | $this->groups = XDB::fetchAllAssoc('asso_id', 'SELECT asso_id, perms, comm | |
185d4ea1 | 509 | FROM group_members |
f5ef8b57 RB |
510 | WHERE uid = {?}', |
511 | $this->id()); | |
512 | } | |
513 | return $this->groups; | |
514 | } | |
515 | ||
50d5ec0b | 516 | // Return permission flags for a given permission level. |
365ba8c3 | 517 | public static function makePerms($perms, $is_admin) |
50d5ec0b | 518 | { |
365ba8c3 | 519 | $flags = new PlFlagSet($perms); |
50d5ec0b | 520 | $flags->addFlag(PERMS_USER); |
365ba8c3 | 521 | if ($is_admin) { |
50d5ec0b FB |
522 | $flags->addFlag(PERMS_ADMIN); |
523 | } | |
524 | return $flags; | |
525 | } | |
526 | ||
b1719b13 VZ |
527 | // Implementation of the default user callback. |
528 | public static function _default_user_callback($login, $results) | |
529 | { | |
b1719b13 | 530 | $result_count = count($results); |
dd70cd28 | 531 | if ($result_count == 0 || !S::admin()) { |
70232020 | 532 | Platal::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login"); |
b1719b13 | 533 | } else { |
70232020 | 534 | Platal::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results)); |
b1719b13 VZ |
535 | } |
536 | } | |
70232020 VZ |
537 | |
538 | // Implementation of the static email locality checker. | |
539 | public static function isForeignEmailAddress($email) | |
540 | { | |
541 | global $globals; | |
542 | if (strpos($email, '@') === false) { | |
543 | return false; | |
544 | } | |
545 | ||
546 | list($user, $dom) = explode('@', $email); | |
547 | return $dom != $globals->mail->domain && | |
548 | $dom != $globals->mail->domain2 && | |
549 | $dom != $globals->mail->alias_dom && | |
550 | $dom != $globals->mail->alias_dom2; | |
551 | } | |
832e6fcb | 552 | |
aa21c568 FB |
553 | public static function isVirtualEmailAddress($email) |
554 | { | |
555 | global $globals; | |
556 | if (strpos($email, '@') === false) { | |
557 | return false; | |
558 | } | |
559 | ||
560 | list($user, $dom) = explode('@', $email); | |
561 | return $dom == $globals->mail->alias_dom | |
562 | || $dom == $globals->mail->alias_dom2; | |
563 | } | |
564 | ||
0d906109 RB |
565 | public static function iterOverUIDs($uids, $respect_order = true) |
566 | { | |
567 | return new UserIterator(self::loadMainFieldsFromUIDs($uids, $respect_order)); | |
568 | } | |
569 | ||
570 | /** Fetch a set of users from a list of UIDs | |
571 | * @param $data The list of uids to fetch, or an array of arrays | |
572 | * @param $orig If $data is an array of arrays, the subfield where uids are stored | |
573 | * @param $dest If $data is an array of arrays, the subfield to fill with Users | |
574 | * @param $fetchProfile Whether to fetch Profiles as well | |
575 | * @return either an array of $uid => User, or $data with $data[$i][$dest] = User | |
576 | */ | |
b774ddab | 577 | public static function getBulkUsersWithUIDs(array $data, $orig = null, $dest = null, $fetchProfile = true) |
832e6fcb | 578 | { |
07eb5b0e FB |
579 | // Fetch the list of uids |
580 | if (is_null($orig)) { | |
581 | $uids = $data; | |
582 | } else { | |
583 | if (is_null($dest)) { | |
584 | $dest = $orig; | |
585 | } | |
586 | $uids = array(); | |
587 | foreach ($data as $key=>$entry) { | |
588 | if (isset($entry[$orig])) { | |
589 | $uids[] = $entry[$orig]; | |
590 | } | |
591 | } | |
592 | } | |
593 | ||
594 | // Fetch users | |
38c6fe96 | 595 | if (count($uids) == 0) { |
07eb5b0e | 596 | return $data; |
38c6fe96 | 597 | } |
0d906109 RB |
598 | $users = self::iterOverUIDs($uids, true); |
599 | ||
d865c296 | 600 | $table = array(); |
b774ddab | 601 | if ($fetchProfile) { |
0d906109 RB |
602 | $profiles = Profile::iterOverUIDS($uids, true); |
603 | $profile = $profiles->next(); | |
b774ddab | 604 | } |
0d906109 RB |
605 | |
606 | /** We iterate through the users, moving in | |
607 | * profiles when they match the user ID : | |
608 | * there can be users without a profile, but not | |
609 | * the other way around. | |
610 | */ | |
611 | while (($user = $users->next())) { | |
b774ddab | 612 | if ($fetchProfile) { |
0d906109 RB |
613 | if ($profile->owner_id == $user->id()) { |
614 | $user->_profile = $profile; | |
615 | $profile = $profiles->next(); | |
b774ddab FB |
616 | } |
617 | $user->_profile_fetched = true; | |
618 | } | |
0d906109 | 619 | $table[$user->id()] = $user; |
d865c296 | 620 | } |
07eb5b0e FB |
621 | |
622 | // Build the result with respect to input order. | |
623 | if (is_null($orig)) { | |
0d906109 | 624 | return $table; |
07eb5b0e FB |
625 | } else { |
626 | foreach ($data as $key=>$entry) { | |
627 | if (isset($entry[$orig])) { | |
628 | $entry[$dest] = $table[$entry[$orig]]; | |
629 | $data[$key] = $entry; | |
630 | } | |
631 | } | |
632 | return $data; | |
832e6fcb | 633 | } |
07eb5b0e FB |
634 | } |
635 | ||
b774ddab | 636 | public static function getBulkUsersFromDB($fetchProfile = true) |
07eb5b0e FB |
637 | { |
638 | $args = func_get_args(); | |
639 | $uids = call_user_func_array(array('XDB', 'fetchColumn'), $args); | |
b774ddab | 640 | return self::getBulkUsersWithUIDs($uids, null, null, $fetchProfile); |
832e6fcb | 641 | } |
9f8ebb9f VZ |
642 | } |
643 | ||
0d906109 RB |
644 | /** Iterator over a set of Users |
645 | * @param an XDB::Iterator obtained from a User::loadMainFieldsFromUIDs | |
646 | */ | |
647 | class UserIterator implements PlIterator | |
648 | { | |
649 | private $dbiter; | |
650 | ||
651 | public function __construct($dbiter) | |
652 | { | |
653 | $this->dbiter = $dbiter; | |
654 | } | |
655 | ||
656 | public function next() | |
657 | { | |
658 | $data = $this->dbiter->next(); | |
659 | if ($data == null) { | |
660 | return null; | |
661 | } else { | |
662 | return User::getSilentWithValues(null, $data); | |
663 | } | |
664 | } | |
665 | ||
666 | public function total() | |
667 | { | |
668 | return $this->dbiter->total(); | |
669 | } | |
670 | ||
671 | public function first() | |
672 | { | |
673 | return $this->dbiter->first(); | |
674 | } | |
675 | ||
676 | public function last() | |
677 | { | |
678 | return $this->dbiter->last(); | |
679 | } | |
680 | } | |
681 | ||
9f8ebb9f VZ |
682 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
683 | ?> |