Commit | Line | Data |
---|---|---|
9f8ebb9f VZ |
1 | <?php |
2 | /*************************************************************************** | |
c441aabe | 3 | * Copyright (C) 2003-2014 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 | { |
16d2c883 | 24 | const PERM_API_USER_READONLY = 'api_user_readonly'; |
30962fae FB |
25 | const PERM_DIRECTORY_AX = 'directory_ax'; |
26 | const PERM_DIRECTORY_PRIVATE = 'directory_private'; | |
7f217324 | 27 | const PERM_DIRECTORY_HIDDEN = 'directory_hidden'; |
30962fae FB |
28 | const PERM_EDIT_DIRECTORY = 'edit_directory'; |
29 | const PERM_FORUMS = 'forums'; | |
16d2c883 | 30 | const PERM_GROUPS = 'groups'; |
30962fae | 31 | const PERM_LISTS = 'lists'; |
16d2c883 | 32 | const PERM_MAIL = 'mail'; |
30962fae FB |
33 | const PERM_PAYMENT = 'payment'; |
34 | ||
f036c896 SJ |
35 | public static $sub_mail_domains = array( |
36 | 'x' => '', | |
37 | 'master' => 'master.', | |
38 | 'phd' => 'doc.', | |
39 | 'all' => 'alumni.' | |
40 | ); | |
41 | ||
3e53a496 FB |
42 | private $_profile_fetched = false; |
43 | private $_profile = null; | |
44 | ||
956608bc RB |
45 | // Additional fields (non core) |
46 | protected $promo = null; | |
47 | ||
70232020 | 48 | // Implementation of the login to uid method. |
b1719b13 VZ |
49 | protected function getLogin($login) |
50 | { | |
51 | global $globals; | |
52 | ||
f6c58d14 VZ |
53 | if (!$login) { |
54 | throw new UserNotFoundException(); | |
55 | } | |
56 | ||
455ea0c9 | 57 | if ($login instanceof User) { |
14da7ef4 | 58 | return $login->id(); |
455ea0c9 FB |
59 | } |
60 | ||
e7b93962 | 61 | if ($login instanceof Profile) { |
3e53a496 FB |
62 | $this->_profile = $login; |
63 | $this->_profile_fetched = true; | |
e7b93962 FB |
64 | $res = XDB::query('SELECT ap.uid |
65 | FROM account_profiles AS ap | |
66 | WHERE ap.pid = {?} AND FIND_IN_SET(\'owner\', perms)', | |
67 | $login->id()); | |
68 | if ($res->numRows()) { | |
69 | return $res->fetchOneCell(); | |
70 | } | |
71 | throw new UserNotFoundException(); | |
72 | } | |
73 | ||
b1719b13 VZ |
74 | // If $data is an integer, fetches directly the result. |
75 | if (is_numeric($login)) { | |
06a548e5 SJ |
76 | $res = XDB::query('SELECT uid |
77 | FROM accounts | |
78 | WHERE uid = {?}', $login); | |
b1719b13 | 79 | if ($res->numRows()) { |
70232020 | 80 | return $res->fetchOneCell(); |
b1719b13 VZ |
81 | } |
82 | ||
83 | throw new UserNotFoundException(); | |
84 | } | |
85 | ||
86 | // Checks whether $login is a valid hruid or not. | |
bfc34dc5 | 87 | $res = XDB::query('SELECT uid |
06a548e5 | 88 | FROM accounts |
c846452b RB |
89 | WHERE hruid = {?}', $login); |
90 | if ($res->numRows()) { | |
70232020 | 91 | return $res->fetchOneCell(); |
b1719b13 VZ |
92 | } |
93 | ||
94 | // From now, $login can only by an email alias, or an email redirection. | |
b1719b13 VZ |
95 | $login = trim(strtolower($login)); |
96 | if (strstr($login, '@') === false) { | |
ac57076f SJ |
97 | $res = XDB::fetchOneCell('SELECT uid |
98 | FROM email_source_account | |
99 | WHERE email = {?}', | |
100 | $login); | |
06a548e5 SJ |
101 | } else { |
102 | list($email, $domain) = explode('@', $login); | |
ac57076f SJ |
103 | $res = XDB::fetchOneCell('SELECT s.uid |
104 | FROM email_source_account AS s | |
105 | INNER JOIN email_virtual_domains AS m ON (s.domain = m.id) | |
106 | INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id) | |
107 | WHERE s.email = {?} AND d.name = {?}', | |
108 | $email, $domain); | |
b1719b13 VZ |
109 | } |
110 | ||
06a548e5 SJ |
111 | if ($res) { |
112 | return $res; | |
b1719b13 VZ |
113 | } |
114 | ||
cb8a8977 | 115 | // Looks for an account with the given email. |
06a548e5 SJ |
116 | $res = XDB::query('SELECT uid |
117 | FROM accounts | |
118 | WHERE email = {?}', $login); | |
cb8a8977 FB |
119 | if ($res->numRows() == 1) { |
120 | return $res->fetchOneCell(); | |
121 | } | |
122 | ||
b1719b13 | 123 | // Otherwise, we do suppose $login is an email redirection. |
06a548e5 SJ |
124 | $res = XDB::query('SELECT uid |
125 | FROM email_redirect_account | |
126 | WHERE redirect = {?}', $login); | |
b1719b13 | 127 | if ($res->numRows() == 1) { |
70232020 | 128 | return $res->fetchOneCell(); |
b1719b13 VZ |
129 | } |
130 | ||
131 | throw new UserNotFoundException($res->fetchColumn(1)); | |
132 | } | |
133 | ||
0d906109 | 134 | protected static function loadMainFieldsFromUIDs(array $uids, $respect_order = true) |
832e6fcb | 135 | { |
6d33f1d3 FB |
136 | if (empty($uids)) { |
137 | return PlIteratorUtils::emptyIterator(); | |
138 | } | |
139 | ||
45dcd6dd | 140 | global $globals; |
832e6fcb | 141 | $joins = ''; |
45dcd6dd | 142 | $fields = array(); |
45dcd6dd | 143 | if ($globals->asso('id')) { |
eb41eda9 | 144 | $joins .= XDB::format("LEFT JOIN group_members AS gpm ON (gpm.uid = a.uid AND gpm.asso_id = {?})\n", $globals->asso('id')); |
45dcd6dd | 145 | $fields[] = 'gpm.perms AS group_perms'; |
a6761ca9 | 146 | $fields[] = 'gpm.comm AS group_comm'; |
33fcb12c | 147 | $fields[] = 'gpm.position AS group_position'; |
45dcd6dd FB |
148 | } |
149 | if (count($fields) > 0) { | |
150 | $fields = ', ' . implode(', ', $fields); | |
a3118782 FB |
151 | } else { |
152 | $fields = ''; | |
45dcd6dd | 153 | } |
0d906109 RB |
154 | |
155 | if ($respect_order) { | |
156 | $order = 'ORDER BY ' . XDB::formatCustomOrder('a.uid', $uids); | |
157 | } else { | |
158 | $order = ''; | |
159 | } | |
160 | ||
45dcd6dd | 161 | $uids = array_map(array('XDB', 'escape'), $uids); |
0d906109 | 162 | |
b7753795 | 163 | return XDB::iterator('SELECT a.uid, a.hruid, a.registration_date, h.uid IS NOT NULL AS homonym, a.firstname, a.lastname, |
f036c896 SJ |
164 | IF(ef.email IS NULL, NULL, CONCAT(ef.email, \'@\', mf.name)) AS forlife, |
165 | IF(ef.email IS NULL, NULL, CONCAT(ef.email, \'@\', df.name)) AS forlife_alternate, | |
166 | IF(eb.email IS NULL, NULL, CONCAT(eb.email, \'@\', mb.name)) AS bestalias, | |
dac6e2c6 | 167 | (er.redirect IS NULL AND a.state = \'active\' AND FIND_IN_SET(\'mail\', at.perms)) AS lost, |
27492fde | 168 | a.email, a.full_name, a.directory_name, a.display_name, a.sort_name, a.sex = \'female\' AS gender, |
7fd6dbb3 | 169 | IF(a.state = \'active\', CONCAT(at.perms, \',\', IF(a.user_perms IS NULL, \'\', a.user_perms)), \'\') AS perms, |
f60c3d1f | 170 | a.user_perms, a.email_format, a.is_admin, a.state, a.type, at.description AS type_description, a.skin, |
832e6fcb | 171 | FIND_IN_SET(\'watch\', a.flags) AS watch, a.comment, |
2ab3486b SJ |
172 | a.weak_password IS NOT NULL AS weak_access, g.g_account_name IS NOT NULL AS googleapps, |
173 | a.token IS NOT NULL AS token_access, a.token, a.last_version, | |
1b78b883 | 174 | s.start AS lastlogin, s.host, fp.last_seen AS banana_last |
2c411733 | 175 | ' . $fields . ' |
06a548e5 SJ |
176 | FROM accounts AS a |
177 | INNER JOIN account_types AS at ON (at.type = a.type) | |
178 | LEFT JOIN email_source_account AS ef ON (ef.uid = a.uid AND ef.type = \'forlife\') | |
179 | LEFT JOIN email_virtual_domains AS mf ON (ef.domain = mf.id) | |
f036c896 SJ |
180 | LEFT JOIN email_virtual_domains AS df ON (df.aliasing = mf.id AND |
181 | df.name LIKE CONCAT(\'%\', {?}) AND df.name NOT LIKE \'alumni.%\') | |
f67c8d9a | 182 | LEFT JOIN email_source_account AS eb ON (eb.uid = a.uid AND FIND_IN_SET(\'bestalias\',eb.flags)) |
9f3f87bc | 183 | LEFT JOIN email_virtual_domains AS mb ON (a.best_domain = mb.id) |
06a548e5 SJ |
184 | LEFT JOIN email_redirect_account AS er ON (er.uid = a.uid AND er.flags = \'active\' AND er.broken_level < 3 |
185 | AND er.type != \'imap\' AND er.type != \'homonym\') | |
186 | LEFT JOIN homonyms_list AS h ON (h.uid = a.uid) | |
187 | LEFT JOIN gapps_accounts AS g ON (a.uid = g.l_userid AND g.g_status = \'active\') | |
188 | LEFT JOIN log_last_sessions AS ls ON (ls.uid = a.uid) | |
189 | LEFT JOIN log_sessions AS s ON (s.id = ls.id) | |
190 | LEFT JOIN forum_profiles AS fp ON (fp.uid = a.uid) | |
d865c296 | 191 | ' . $joins . ' |
832e6fcb | 192 | WHERE a.uid IN (' . implode(', ', $uids) . ') |
0d906109 | 193 | GROUP BY a.uid |
f036c896 | 194 | ' . $order, $globals->mail->domain2, $globals->mail->domain2); |
832e6fcb FB |
195 | } |
196 | ||
70232020 VZ |
197 | // Implementation of the data loader. |
198 | protected function loadMainFields() | |
199 | { | |
c4012d9b VZ |
200 | if ($this->hruid !== null && $this->forlife !== null |
201 | && $this->bestalias !== null && $this->display_name !== null | |
8f2104cb | 202 | && $this->full_name !== null && $this->perms !== null |
c4012d9b | 203 | && $this->gender !== null && $this->email_format !== null) { |
70232020 VZ |
204 | return; |
205 | } | |
1bf36cd1 | 206 | $this->fillFromArray(self::loadMainFieldsFromUIDs(array($this->uid))->next()); |
70232020 VZ |
207 | } |
208 | ||
50d5ec0b FB |
209 | // Specialization of the buildPerms method |
210 | // This function build 'generic' permissions for the user. It does not take | |
211 | // into account page specific permissions (e.g X.net group permissions) | |
212 | protected function buildPerms() | |
213 | { | |
214 | if (!is_null($this->perm_flags)) { | |
215 | return; | |
216 | } | |
217 | if ($this->perms === null) { | |
218 | $this->loadMainFields(); | |
219 | } | |
365ba8c3 | 220 | $this->perm_flags = self::makePerms($this->perms, $this->is_admin); |
50d5ec0b FB |
221 | } |
222 | ||
20b087ff FB |
223 | public function setPerms($perms) |
224 | { | |
225 | $this->perms = $perms; | |
226 | $this->perm_flags = null; | |
227 | } | |
228 | ||
38a86f60 RB |
229 | /** Retrieve the 'general' read visibility. |
230 | * This is the maximum level of fields that may be viewed by the current user on other profiles. | |
231 | * | |
232 | * Rules are: | |
233 | * - Everyone can view 'public' | |
7f217324 | 234 | * - directory_ax gives access to 'AX' level, ie. the printed directory |
38a86f60 | 235 | * - directory_private gives access to 'private' level |
7f217324 | 236 | * - admin and directory_hidden gives access to 'hidden' level |
38a86f60 RB |
237 | */ |
238 | public function readVisibility() | |
239 | { | |
22771578 | 240 | $level = Visibility::VIEW_NONE; |
7f217324 | 241 | if ($this->is_admin || $this->checkPerms('directory_hidden')) { |
22771578 | 242 | $level = Visibility::VIEW_ADMIN; |
38a86f60 | 243 | } elseif ($this->checkPerms('directory_private')) { |
22771578 | 244 | $level = Visibility::VIEW_PRIVATE; |
38a86f60 | 245 | } elseif ($this->checkPerms('directory_ax')) { |
22771578 | 246 | $level = Visibility::VIEW_AX; |
38a86f60 | 247 | } else { |
22771578 | 248 | $level = Visibility::VIEW_PUBLIC; |
38a86f60 | 249 | } |
22771578 | 250 | return Visibility::get($level); |
38a86f60 RB |
251 | } |
252 | ||
253 | /** Retrieve the 'general' edit visibility. | |
254 | * This is the maximum level of fields that may be edited by the current user on other profiles. | |
255 | * | |
256 | * Rules are: | |
257 | * - Only admins can edit the 'hidden' fields | |
22771578 | 258 | * - If someone has 'directory_edit' (which is actually directory_ax_edit): AX level |
38a86f60 RB |
259 | * - Otherwise, nothing. |
260 | */ | |
261 | public function editVisibility() | |
262 | { | |
22771578 | 263 | $level = Visibility::VIEW_NONE; |
38a86f60 | 264 | if ($this->is_admin) { |
22771578 | 265 | $level = Visibility::VIEW_ADMIN; |
38a86f60 | 266 | } elseif ($this->checkPerms('directory_edit')) { |
22771578 | 267 | $level = Visibility::VIEW_AX; |
38a86f60 | 268 | } |
22771578 | 269 | return Visibility::get($level); |
38a86f60 RB |
270 | } |
271 | ||
7f1ff426 FB |
272 | // We do not want to store the password in the object. |
273 | // So, fetch it 'on demand' | |
274 | public function password() | |
275 | { | |
276 | return XDB::fetchOneCell('SELECT a.password | |
277 | FROM accounts AS a | |
278 | WHERE a.uid = {?}', $this->id()); | |
279 | } | |
280 | ||
280806d9 SJ |
281 | public function isActive() |
282 | { | |
283 | return $this->state == 'active'; | |
284 | } | |
285 | ||
8f2104cb FB |
286 | /** Overload PlUser::promo(): there no promo defined for a user in the current |
287 | * schema. The promo is a field from the profile. | |
288 | */ | |
289 | public function promo() | |
290 | { | |
291 | if (!$this->hasProfile()) { | |
292 | return ''; | |
293 | } | |
294 | return $this->profile()->promo(); | |
295 | } | |
296 | ||
f60c3d1f FB |
297 | public function category() |
298 | { | |
299 | $promo = $this->promo(); | |
300 | if (!empty($promo)) { | |
301 | return $promo; | |
302 | } else { | |
303 | return $this->type_description; | |
304 | } | |
305 | } | |
306 | ||
a6761ca9 FB |
307 | public function firstName() |
308 | { | |
309 | if (!$this->hasProfile()) { | |
94b6106c | 310 | return $this->firstname; |
a6761ca9 FB |
311 | } |
312 | return $this->profile()->firstName(); | |
313 | } | |
314 | ||
315 | public function lastName() | |
316 | { | |
317 | if (!$this->hasProfile()) { | |
94b6106c | 318 | return $this->lastname; |
a6761ca9 FB |
319 | } |
320 | return $this->profile()->lastName(); | |
321 | } | |
322 | ||
2ab3486b SJ |
323 | public function displayName() |
324 | { | |
325 | if (!$this->hasProfile()) { | |
326 | return $this->display_name; | |
327 | } | |
328 | return $this->profile()->yourself; | |
329 | } | |
330 | ||
09e54905 SJ |
331 | public function fullName($with_promo = false) |
332 | { | |
333 | if (!$this->hasProfile()) { | |
334 | return $this->full_name; | |
335 | } | |
336 | return $this->profile()->fullName($with_promo); | |
337 | } | |
338 | ||
fc1227ef SJ |
339 | public function shortName($with_promo = false) |
340 | { | |
341 | if (!$this->hasProfile()) { | |
342 | return $this->full_name; | |
343 | } | |
344 | return $this->profile()->shortName($with_promo); | |
345 | } | |
346 | ||
09e54905 SJ |
347 | public function directoryName() |
348 | { | |
349 | if (!$this->hasProfile()) { | |
d081acb2 | 350 | return $this->directory_name; |
09e54905 SJ |
351 | } |
352 | return $this->profile()->directory_name; | |
353 | } | |
354 | ||
27492fde AL |
355 | public function sortName() |
356 | { | |
357 | if (!$this->hasProfile()) { | |
358 | return $this->sort_name; | |
359 | } | |
360 | return $this->profile()->sort_name; | |
361 | } | |
362 | ||
52366335 SJ |
363 | static public function compareDirectoryName($a, $b) |
364 | { | |
365 | return strcasecmp(replace_accent($a->directoryName()), replace_accent($b->directoryName())); | |
366 | } | |
367 | ||
e7b93962 FB |
368 | /** Return the main profile attached with this account if any. |
369 | */ | |
4ac2e2ba | 370 | public function profile($forceFetch = false, $fields = 0x0000, $visibility = null) |
e7b93962 | 371 | { |
4e698dc9 | 372 | if (!$this->_profile_fetched || $forceFetch) { |
3e53a496 | 373 | $this->_profile_fetched = true; |
4ac2e2ba | 374 | $this->_profile = Profile::get($this, $fields, $visibility); |
6178c3f3 | 375 | } else if ($this->_profile !== null && $visibility !== null && !$this->_profile->visibility->equals($visibility)) { |
22771578 | 376 | return Profile::get($this, $fields, $visibility); |
3e53a496 FB |
377 | } |
378 | return $this->_profile; | |
379 | } | |
380 | ||
83040cf5 RB |
381 | public function setPrefetchedProfile(Profile $profile) |
382 | { | |
383 | $this->_profile_fetched = true; | |
384 | $this->_profile = $profile; | |
385 | } | |
386 | ||
3e53a496 FB |
387 | /** Return true if the user has an associated profile. |
388 | */ | |
389 | public function hasProfile() | |
390 | { | |
391 | return !is_null($this->profile()); | |
392 | } | |
393 | ||
fceed6ea FB |
394 | /** Return true if given a reference to the profile of this user. |
395 | */ | |
396 | public function isMyProfile($other) | |
397 | { | |
398 | if (!$other) { | |
399 | return false; | |
400 | } else if ($other instanceof Profile) { | |
401 | $profile = $this->profile(); | |
402 | return $profile && $profile->id() == $other->id(); | |
403 | } | |
404 | return false; | |
405 | } | |
406 | ||
3af21f99 FB |
407 | /** Check if the user can edit to given profile. |
408 | */ | |
409 | public function canEdit(Profile $profile) | |
410 | { | |
a81ee987 FB |
411 | if ($this->checkPerms(User::PERM_EDIT_DIRECTORY)) { |
412 | return true; | |
413 | } | |
3af21f99 FB |
414 | return XDB::fetchOneCell('SELECT pid |
415 | FROM account_profiles | |
416 | WHERE uid = {?} AND pid = {?}', | |
417 | $this->id(), $profile->id()); | |
418 | } | |
419 | ||
f036c896 | 420 | /** Determines main email domain for this user. |
3e53a496 | 421 | */ |
f036c896 | 422 | public function mainEmailDomain() |
3e53a496 | 423 | { |
f036c896 SJ |
424 | if (array_key_exists($this->type, self::$sub_mail_domains)) { |
425 | return self::$sub_mail_domains[$this->type] . Platal::globals()->mail->domain; | |
8f2104cb | 426 | } |
8f2104cb FB |
427 | } |
428 | ||
f036c896 | 429 | /** Determines alternate email domain for this user. |
8f2104cb | 430 | */ |
f036c896 SJ |
431 | public function alternateEmailDomain() |
432 | { | |
433 | if (array_key_exists($this->type, self::$sub_mail_domains)) { | |
434 | return self::$sub_mail_domains[$this->type] . Platal::globals()->mail->domain2; | |
a6761ca9 | 435 | } |
f036c896 SJ |
436 | } |
437 | ||
72e12532 SJ |
438 | public function forlifeEmailAlternate() |
439 | { | |
440 | if (!empty($this->forlife_alternate)) { | |
441 | return $this->forlife_alternate; | |
8f2104cb | 442 | } |
72e12532 | 443 | return $this->email; |
3e53a496 FB |
444 | } |
445 | ||
08d33afc | 446 | /** Fetch existing auxiliary alias. |
3e53a496 | 447 | */ |
3e53a496 | 448 | public function emailAlias() |
3e53a496 | 449 | { |
06a548e5 SJ |
450 | $aliases = $this->emailAliases(); |
451 | if (count($aliases)) { | |
452 | return $aliases[0]; | |
8f2104cb FB |
453 | } |
454 | return null; | |
e7b93962 FB |
455 | } |
456 | ||
08d33afc | 457 | /** Fetch existing auxiliary aliases. |
8f2104cb | 458 | */ |
06a548e5 SJ |
459 | public function emailAliases() |
460 | { | |
461 | return XDB::fetchColumn('SELECT CONCAT(s.email, \'@\', d.name) | |
462 | FROM email_source_account AS s | |
463 | INNER JOIN email_virtual_domains AS m ON (s.domain = m.id) | |
464 | INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id) | |
08d33afc | 465 | WHERE s.uid = {?} AND s.type = \'alias_aux\' |
06a548e5 | 466 | ORDER BY d.name', |
08d33afc | 467 | $this->id()); |
3e53a496 FB |
468 | } |
469 | ||
06a548e5 | 470 | /** Get all group aliases the user belongs to. |
3e53a496 | 471 | */ |
06a548e5 SJ |
472 | public function emailGroupAliases($domain = null) |
473 | { | |
474 | if (is_null($domain)) { | |
475 | return XDB::fetchColumn('SELECT CONCAT(v.email, \'@\', dv.name) AS alias | |
476 | FROM email_virtual AS v | |
477 | INNER JOIN email_virtual_domains AS dv ON (v.domain = dv.id) | |
478 | INNER JOIN email_source_account AS s ON (s.uid = {?}) | |
479 | INNER JOIN email_virtual_domains AS ms ON (s.domain = ms.id) | |
480 | INNER JOIN email_virtual_domains AS ds ON (ds.aliasing = ms.id) | |
51c2c63a | 481 | WHERE v.redirect = CONCAT(s.email, \'@\', ds.name) AND v.type = \'alias\'', |
06a548e5 SJ |
482 | $this->id()); |
483 | } else { | |
a94fdc4e SJ |
484 | return XDB::fetchAllAssoc('alias', |
485 | 'SELECT CONCAT(v.email, \'@\', dv.name) AS alias, MAX(v.redirect = CONCAT(s.email, \'@\', ds.name)) AS sub | |
06a548e5 SJ |
486 | FROM email_virtual AS v |
487 | INNER JOIN email_virtual_domains AS dv ON (v.domain = dv.id AND dv.name = {?}) | |
488 | INNER JOIN email_source_account AS s ON (s.uid = {?}) | |
489 | INNER JOIN email_virtual_domains AS ms ON (s.domain = ms.id) | |
490 | INNER JOIN email_virtual_domains AS ds ON (ds.aliasing = ms.id) | |
51c2c63a | 491 | WHERE v.type = \'alias\' |
a94fdc4e SJ |
492 | GROUP BY v.email |
493 | ORDER BY v.email', | |
06a548e5 SJ |
494 | $domain, $this->id()); |
495 | } | |
e7b93962 | 496 | } |
38c6fe96 FB |
497 | |
498 | /** Get marketing informations | |
499 | */ | |
500 | private function fetchMarketingData() | |
501 | { | |
db8432d5 | 502 | if (isset($this->pending_registration_date)) { |
38c6fe96 FB |
503 | return; |
504 | } | |
db8432d5 SJ |
505 | $infos = XDB::fetchOneAssoc('SELECT rp.date AS pending_registration_date, rp.email AS pending_registration_email, |
506 | rm.last AS last_marketing_date, rm.email AS last_marketing_email | |
507 | FROM accounts AS a | |
508 | LEFT JOIN register_pending AS rp ON (rp.uid = a.uid) | |
509 | LEFT JOIN register_marketing AS rm ON (rm.uid = a.uid AND rm.last != \'0000-00-00\') | |
510 | WHERE a.uid = {?} | |
511 | ORDER BY rm.last DESC', $this->id()); | |
512 | if (is_null($infos)) { | |
513 | $infos = array( | |
514 | 'pending_registration_date' => null, | |
515 | 'pending_registration_email' => null, | |
516 | 'last_marketing_date' => null, | |
517 | 'last_marketing_email' => null | |
518 | ); | |
38c6fe96 FB |
519 | } |
520 | $this->fillFromArray($infos); | |
521 | } | |
522 | ||
db8432d5 | 523 | public function pendingRegistrationDate() |
38c6fe96 FB |
524 | { |
525 | $this->fetchMarketingData(); | |
db8432d5 SJ |
526 | return $this->pending_registration_date; |
527 | } | |
528 | ||
529 | public function pendingRegistrationEmail() | |
530 | { | |
531 | $this->fetchMarketingData(); | |
532 | return $this->pending_registration_email; | |
533 | } | |
534 | ||
535 | public function lastMarketingDate() | |
536 | { | |
537 | $this->fetchMarketingData(); | |
538 | return $this->last_marketing_date; | |
539 | } | |
540 | ||
541 | public function lastMarketingEmail() | |
542 | { | |
543 | $this->fetchMarketingData(); | |
544 | return $this->last_marketing_email; | |
38c6fe96 FB |
545 | } |
546 | ||
547 | public function lastKnownEmail() | |
548 | { | |
549 | $this->fetchMarketingData(); | |
db8432d5 SJ |
550 | if ($this->pending_registration_email > $this->last_marketing_date) { |
551 | return $this->pending_registration_email; | |
552 | } | |
553 | return $this->last_marketing_email; | |
38c6fe96 FB |
554 | } |
555 | ||
009b8ab7 | 556 | |
8d308ee4 FB |
557 | /** Format of the emails sent by the site |
558 | */ | |
559 | public function setEmailFormat($format) | |
560 | { | |
561 | Platal::assert($format == self::FORMAT_HTML || $format == self::FORMAT_TEXT, | |
562 | "Invalid email format \"$format\""); | |
563 | XDB::execute("UPDATE accounts | |
564 | SET email_format = {?} | |
565 | WHERE uid = {?}", | |
566 | $format, $this->uid); | |
567 | $this->email_format = $format; | |
568 | } | |
569 | ||
009b8ab7 FB |
570 | /** Get watch informations |
571 | */ | |
572 | private function fetchWatchData() | |
573 | { | |
574 | if (isset($this->watch_actions)) { | |
575 | return; | |
576 | } | |
577 | $watch = XDB::fetchOneAssoc('SELECT flags AS watch_flags, actions AS watch_actions, | |
578 | UNIX_TIMESTAMP(last) AS watch_last | |
579 | FROM watch | |
580 | WHERE uid = {?}', $this->id()); | |
581 | $watch['watch_flags'] = new PlFlagSet($watch['watch_flags']); | |
582 | $watch['watch_actions'] = new PlFlagSet($watch['watch_actions']); | |
583 | $watch['watch_promos'] = XDB::fetchColumn('SELECT promo | |
584 | FROM watch_promo | |
585 | WHERE uid = {?}', $this->id()); | |
fe286e5f SJ |
586 | $watch['watch_groups'] = XDB::fetchColumn("SELECT w.groupid |
587 | FROM watch_group AS w | |
588 | INNER JOIN groups AS g ON (w.groupid = g.id AND NOT FIND_IN_SET('private', pub)) | |
589 | WHERE w.uid = {?}", $this->id()); | |
009b8ab7 FB |
590 | $watch['watch_users'] = XDB::fetchColumn('SELECT ni_id |
591 | FROM watch_nonins | |
592 | WHERE uid = {?}', $this->id()); | |
593 | $this->fillFromArray($watch); | |
594 | } | |
595 | ||
a87530ea | 596 | public function watchType($type) |
009b8ab7 FB |
597 | { |
598 | $this->fetchWatchData(); | |
599 | return $this->watch_actions->hasFlag($type); | |
600 | } | |
601 | ||
602 | public function watchContacts() | |
603 | { | |
604 | $this->fetchWatchData(); | |
605 | return $this->watch_flags->hasFlag('contacts'); | |
606 | } | |
607 | ||
608 | public function watchEmail() | |
609 | { | |
610 | $this->fetchWatchData(); | |
611 | return $this->watch_flags->hasFlag('mail'); | |
612 | } | |
613 | ||
614 | public function watchPromos() | |
615 | { | |
616 | $this->fetchWatchData(); | |
617 | return $this->watch_promos; | |
618 | } | |
619 | ||
fe286e5f SJ |
620 | public function watchGroups() |
621 | { | |
622 | $this->fetchWatchData(); | |
623 | return $this->watch_groups; | |
624 | } | |
625 | ||
009b8ab7 FB |
626 | public function watchUsers() |
627 | { | |
628 | $this->fetchWatchData(); | |
629 | return $this->watch_users; | |
630 | } | |
631 | ||
632 | public function watchLast() | |
633 | { | |
634 | $this->fetchWatchData(); | |
635 | return $this->watch_last; | |
636 | } | |
637 | ||
069ddda8 FB |
638 | public function invalidWatchCache() |
639 | { | |
640 | unset($this->watch_actions); | |
641 | unset($this->watch_users); | |
642 | unset($this->watch_last); | |
643 | unset($this->watch_promos); | |
fe286e5f | 644 | unset($this->watch_groups); |
069ddda8 FB |
645 | } |
646 | ||
c350577b FB |
647 | |
648 | // Contacts | |
649 | private $contacts = null; | |
48e683dd | 650 | private function fetchContacts() |
c350577b | 651 | { |
76cbe885 | 652 | if (is_null($this->contacts)) { |
c350577b FB |
653 | $this->contacts = XDB::fetchAllAssoc('contact', 'SELECT * |
654 | FROM contacts | |
655 | WHERE uid = {?}', | |
656 | $this->id()); | |
657 | } | |
48e683dd FB |
658 | } |
659 | ||
660 | public function iterContacts() | |
661 | { | |
662 | $this->fetchContacts(); | |
a289e967 | 663 | return Profile::iterOverPIDs(array_keys($this->contacts)); |
48e683dd FB |
664 | } |
665 | ||
666 | public function getContacts() | |
667 | { | |
668 | $this->fetchContacts(); | |
a289e967 | 669 | return Profile::getBulkProfilesWithPIDs(array_keys($this->contacts)); |
48e683dd FB |
670 | } |
671 | ||
26ba053e | 672 | public function isContact(Profile $profile) |
48e683dd FB |
673 | { |
674 | $this->fetchContacts(); | |
a289e967 | 675 | return isset($this->contacts[$profile->id()]); |
c350577b FB |
676 | } |
677 | ||
26ba053e | 678 | public function isWatchedUser(Profile $profile) |
958def08 PC |
679 | { |
680 | return in_array($profile->id(), $this->watchUsers()); | |
681 | } | |
682 | ||
f5ef8b57 RB |
683 | // Groupes X |
684 | private $groups = null; | |
56cd7aee | 685 | public function groups($institutions = false, $onlyPublic = false) |
f5ef8b57 RB |
686 | { |
687 | if (is_null($this->groups)) { | |
56cd7aee FB |
688 | $this->groups = XDB::fetchAllAssoc('asso_id', 'SELECT gm.asso_id, gm.perms, gm.comm, |
689 | g.diminutif, g.nom, g.site, g.cat, | |
690 | g.pub | |
691 | FROM group_members AS gm | |
692 | INNER JOIN groups AS g ON (g.id = gm.asso_id) | |
f5ef8b57 RB |
693 | WHERE uid = {?}', |
694 | $this->id()); | |
695 | } | |
56cd7aee FB |
696 | if (!$institutions && !$onlyPublic) { |
697 | return $this->groups; | |
fa589f90 | 698 | } else { |
56cd7aee FB |
699 | $result = array(); |
700 | foreach ($this->groups as $id=>$data) { | |
701 | if ($institutions) { | |
1d889ac4 | 702 | if ($data['cat'] != Group::CAT_GROUPESX && $data['cat'] != Group::CAT_INSTITUTIONS) { |
56cd7aee FB |
703 | continue; |
704 | } | |
705 | } | |
706 | if ($onlyPublic) { | |
707 | if ($data['pub'] != 'public') { | |
708 | continue; | |
709 | } | |
710 | } | |
711 | $result[$id] = $data; | |
712 | } | |
713 | return $result; | |
fa589f90 | 714 | } |
fa589f90 RB |
715 | } |
716 | ||
9a7f3d8e | 717 | public function groupCount() |
4257b11a | 718 | { |
9a7f3d8e SJ |
719 | return XDB::fetchOneCell('SELECT COUNT(DISTINCT(asso_id)) |
720 | FROM group_members | |
721 | WHERE uid = {?}', | |
722 | $this->id()); | |
4257b11a SJ |
723 | } |
724 | ||
7ae5c545 SJ |
725 | public function inGroup($asso_id) |
726 | { | |
727 | $res = XDB::fetchOneCell('SELECT COUNT(*) | |
728 | FROM group_members | |
729 | WHERE uid = {?} AND asso_id = {?}', | |
730 | $this->id(), $asso_id); | |
731 | return ($res > 0); | |
732 | } | |
733 | ||
6150f591 SJ |
734 | /** |
735 | * Clears a user. | |
736 | * *always deletes in: account_lost_passwords, register_marketing, | |
fe286e5f | 737 | * register_pending, register_subs, watch_nonins, watch, watch_promo, watch_group, |
06a548e5 SJ |
738 | * *always keeps in: account_types, accounts, email_virtual, carvas, |
739 | * group_members, homonyms_list, newsletter_ins, register_mstats, email_source_account | |
6150f591 | 740 | * *deletes if $clearAll: account_auth_openid, announce_read, contacts, |
06a548e5 | 741 | * email_redirect_account, email_redirect_account, email_send_save, forum_innd, forum_profiles, |
6150f591 SJ |
742 | * forum_subs, gapps_accounts, gapps_nicknames, group_announces_read, |
743 | * group_member_sub_requests, reminder, requests, requests_hidden, | |
06a548e5 | 744 | * email_virtual, ML |
6150f591 SJ |
745 | * *modifies if $clearAll: accounts |
746 | * | |
747 | * Use cases: | |
748 | * *$clearAll == false: when a user dies, her family still needs to keep in | |
749 | * touch with the community. | |
750 | * *$clearAll == true: in every other case we want the account to be fully | |
751 | * deleted so that it can not be used anymore. | |
752 | */ | |
753 | public function clear($clearAll = true) | |
754 | { | |
405d70cc RB |
755 | $tables = array('account_lost_passwords', 'register_marketing', |
756 | 'register_pending', 'register_subs', 'watch_nonins', | |
fe286e5f | 757 | 'watch', 'watch_promo', 'watch_group'); |
405d70cc RB |
758 | |
759 | foreach ($tables as $t) { | |
760 | XDB::execute('DELETE FROM ' . $t . ' | |
761 | WHERE uid = {?}', | |
762 | $this->id()); | |
763 | } | |
6150f591 SJ |
764 | |
765 | if ($clearAll) { | |
c79e28fc SJ |
766 | global $globals; |
767 | ||
0e5b3438 SJ |
768 | $groupIds = XDB::iterator('SELECT asso_id |
769 | FROM group_members | |
770 | WHERE uid = {?}', | |
771 | $this->id()); | |
772 | while ($groupId = $groupIds->next()) { | |
773 | $group = Group::get($groupId); | |
c79e28fc | 774 | if (!empty($group) && $group->notif_unsub) { |
0e5b3438 SJ |
775 | $mailer = new PlMailer('xnetgrp/unsubscription-notif.mail.tpl'); |
776 | $admins = $group->iterAdmins(); | |
777 | while ($admin = $admins->next()) { | |
778 | $mailer->addTo($admin); | |
779 | } | |
780 | $mailer->assign('group', $group->shortname); | |
781 | $mailer->assign('user', $this); | |
782 | $mailer->assign('selfdone', false); | |
783 | $mailer->send(); | |
784 | } | |
785 | } | |
786 | ||
c79e28fc | 787 | $tables = array('account_auth_openid', 'announce_read', 'contacts', |
13b45814 | 788 | 'email_send_save', |
c79e28fc SJ |
789 | 'forum_innd', 'forum_profiles', 'forum_subs', |
790 | 'group_announces_read', 'group_members', | |
791 | 'group_member_sub_requests', 'reminder', 'requests', | |
06a548e5 | 792 | 'requests_hidden'); |
405d70cc RB |
793 | foreach ($tables as $t) { |
794 | XDB::execute('DELETE FROM ' . $t . ' | |
795 | WHERE uid = {?}', | |
c79e28fc SJ |
796 | $this->id()); |
797 | } | |
06a548e5 SJ |
798 | XDB::execute('DELETE FROM email_redirect_account |
799 | WHERE uid = {?} AND type != \'homonym\'', | |
800 | $this->id()); | |
13b45814 SJ |
801 | XDB::execute('DELETE FROM email_virtual |
802 | WHERE redirect = {?}', | |
803 | $this->forlifeEmail()); | |
c79e28fc SJ |
804 | |
805 | foreach (array('gapps_accounts', 'gapps_nicknames') as $t) { | |
806 | XDB::execute('DELETE FROM ' . $t . ' | |
807 | WHERE l_userid = {?}', | |
808 | $this->id()); | |
405d70cc RB |
809 | } |
810 | ||
6150f591 SJ |
811 | XDB::execute("UPDATE accounts |
812 | SET registration_date = 0, state = 'pending', password = NULL, | |
813 | weak_password = NULL, token = NULL, is_admin = 0 | |
814 | WHERE uid = {?}", | |
815 | $this->id()); | |
816 | ||
6150f591 SJ |
817 | if ($globals->mailstorage->googleapps_domain) { |
818 | require_once 'googleapps.inc.php'; | |
819 | ||
c79e28fc SJ |
820 | if (GoogleAppsAccount::account_status($this->id())) { |
821 | $account = new GoogleAppsAccount($this); | |
6150f591 SJ |
822 | $account->suspend(); |
823 | } | |
824 | } | |
825 | } | |
826 | ||
dec84555 | 827 | $mmlist = new MMList(S::user()); |
a85562a0 | 828 | $mmlist->kill($this->hruid, $clearAll); |
6150f591 SJ |
829 | } |
830 | ||
ab06182d | 831 | // Merge all infos in other user and then clean this one |
26ba053e | 832 | public function mergeIn(User $newuser) { |
33a4f3f9 SJ |
833 | if ($this->profile()) { |
834 | // Don't disable user with profile in this way. | |
835 | global $globals; | |
836 | Platal::page()->trigError('Impossible de fusionner les comptes ' . $this->hruid . ' et ' . $newuser->hruid . | |
837 | '. Contacte support@' . $globals->mail->domain . '.'); | |
ab06182d PC |
838 | return false; |
839 | } | |
ab06182d | 840 | |
33a4f3f9 SJ |
841 | if ($this->forlifeEmail()) { |
842 | // If the new user is not registered and does not have already an email address, | |
843 | // we need to give him the old user's email address if he has any. | |
844 | if (!$newuser->perms) { | |
845 | XDB::execute('UPDATE accounts | |
846 | SET email = {?} | |
847 | WHERE uid = {?} AND email IS NULL', | |
848 | $this->forlifeEmail(), $newuser->id()); | |
06a548e5 SJ |
849 | |
850 | // Reftech new user so its forlifeEmail will be correct. | |
cb8b7945 | 851 | $newuser = self::getSilentWithUID($newuser->id()); |
ab06182d | 852 | } |
33a4f3f9 | 853 | |
06a548e5 SJ |
854 | // Change email used in mailing lists. |
855 | if ($this->forlifeEmail() != $newuser->forlifeEmail()) { | |
f7d43ed5 SJ |
856 | // The super user is the user who has the right to do the modification. |
857 | $super_user = S::user(); | |
33a4f3f9 SJ |
858 | // group mailing lists |
859 | $group_domains = XDB::fetchColumn('SELECT g.mail_domain | |
860 | FROM groups AS g | |
861 | INNER JOIN group_members AS gm ON(g.id = gm.asso_id) | |
862 | WHERE g.mail_domain != \'\' AND gm.uid = {?}', | |
863 | $this->id()); | |
864 | foreach ($group_domains as $mail_domain) { | |
f7d43ed5 | 865 | $mmlist = new MMList($super_user, $mail_domain); |
06a548e5 | 866 | $mmlist->replace_email_in_all($this->forlifeEmail(), $newuser->forlifeEmail()); |
33a4f3f9 SJ |
867 | } |
868 | // main domain lists | |
f7d43ed5 | 869 | $mmlist = new MMList($super_user); |
06a548e5 | 870 | $mmlist->replace_email_in_all($this->forlifeEmail(), $newuser->forlifeEmail()); |
33a4f3f9 SJ |
871 | } |
872 | } | |
873 | ||
874 | // Updates user in following tables. | |
7f376ae0 | 875 | foreach (array('group_announces', 'payment_transactions', 'log_sessions', 'group_events') as $table) { |
33a4f3f9 SJ |
876 | XDB::execute('UPDATE ' . $table . ' |
877 | SET uid = {?} | |
878 | WHERE uid = {?}', | |
879 | $newuser->id(), $this->id()); | |
880 | } | |
33a4f3f9 SJ |
881 | |
882 | // Merges user in following tables, ie updates when possible, then deletes remaining occurences of the old user. | |
06a548e5 | 883 | foreach (array('group_announces_read', 'group_event_participants', 'group_member_sub_requests', 'group_members', 'email_redirect_account') as $table) { |
33a4f3f9 SJ |
884 | XDB::execute('UPDATE IGNORE ' . $table . ' |
885 | SET uid = {?} | |
886 | WHERE uid = {?}', | |
887 | $newuser->id(), $this->id()); | |
888 | XDB::execute('DELETE FROM ' . $table . ' | |
889 | WHERE uid = {?}', | |
890 | $this->id()); | |
891 | } | |
ab06182d | 892 | |
33a4f3f9 SJ |
893 | // Eventually updates last session id and deletes old user's accounts entry. |
894 | $lastSession = XDB::fetchOneCell('SELECT id | |
895 | FROM log_sessions | |
896 | WHERE uid = {?} | |
897 | ORDER BY start DESC | |
898 | LIMIT 1', | |
899 | $newuser->id()); | |
900 | XDB::execute('UPDATE log_last_sessions | |
901 | SET id = {?} | |
902 | WHERE uid = {?}', | |
be281b31 | 903 | $lastSession, $newuser->id()); |
33a4f3f9 SJ |
904 | XDB::execute('DELETE FROM accounts |
905 | WHERE uid = {?}', | |
906 | $this->id()); | |
ab06182d PC |
907 | |
908 | return true; | |
909 | } | |
910 | ||
50d5ec0b | 911 | // Return permission flags for a given permission level. |
365ba8c3 | 912 | public static function makePerms($perms, $is_admin) |
50d5ec0b | 913 | { |
365ba8c3 | 914 | $flags = new PlFlagSet($perms); |
365ba8c3 | 915 | if ($is_admin) { |
50d5ec0b FB |
916 | $flags->addFlag(PERMS_ADMIN); |
917 | } | |
7fd6dbb3 FB |
918 | |
919 | // Access to private directory implies access to 'less'-private version. | |
920 | if ($flags->hasFlag('directory_private')) { | |
921 | $flags->addFlag('directory_ax'); | |
922 | } | |
50d5ec0b FB |
923 | return $flags; |
924 | } | |
925 | ||
b1719b13 VZ |
926 | // Implementation of the default user callback. |
927 | public static function _default_user_callback($login, $results) | |
928 | { | |
b1719b13 | 929 | $result_count = count($results); |
dd70cd28 | 930 | if ($result_count == 0 || !S::admin()) { |
70232020 | 931 | Platal::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login"); |
b1719b13 | 932 | } else { |
70232020 | 933 | Platal::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results)); |
b1719b13 VZ |
934 | } |
935 | } | |
70232020 | 936 | |
f88d9154 SJ |
937 | public static function makeHomonymHrmid($alias) |
938 | { | |
939 | return 'h.' . $alias . '.' . Platal::globals()->mail->domain; | |
940 | } | |
941 | ||
f036c896 | 942 | public static function isMainMailDomain($domain) |
70232020 VZ |
943 | { |
944 | global $globals; | |
70232020 | 945 | |
f036c896 SJ |
946 | $is_main_domain = false; |
947 | foreach (self::$sub_mail_domains as $sub_domain) { | |
ca2f19b8 | 948 | $is_main_domain = $is_main_domain || $domain == ($sub_domain . $globals->mail->domain) || $domain == ($sub_domain . $globals->mail->domain2); |
f036c896 SJ |
949 | } |
950 | return $is_main_domain; | |
70232020 | 951 | } |
832e6fcb | 952 | |
f036c896 | 953 | public static function isAliasMailDomain($domain) |
aa21c568 FB |
954 | { |
955 | global $globals; | |
f036c896 SJ |
956 | |
957 | return $domain == $globals->mail->alias_dom || $domain == $globals->mail->alias_dom2; | |
958 | } | |
959 | ||
960 | // Implementation of the static email locality checker. | |
961 | public static function isForeignEmailAddress($email) | |
962 | { | |
aa21c568 FB |
963 | if (strpos($email, '@') === false) { |
964 | return false; | |
965 | } | |
966 | ||
f036c896 SJ |
967 | list(, $domain) = explode('@', $email); |
968 | return !(self::isMainMailDomain($domain) || self::isAliasMailDomain($domain)); | |
aa21c568 FB |
969 | } |
970 | ||
61a7d279 SJ |
971 | /* Tries to find pending accounts with an hruid close to $login. */ |
972 | public static function getPendingAccounts($login, $iterator = false) | |
973 | { | |
61a7d279 SJ |
974 | if (strpos($login, '@') === false) { |
975 | return null; | |
976 | } | |
977 | ||
978 | list($login, $domain) = explode('@', $login); | |
979 | ||
f036c896 | 980 | if ($domain && !self::isMainMailDomain($domain)) { |
61a7d279 SJ |
981 | return null; |
982 | } | |
983 | ||
984 | $sql = "SELECT uid, full_name | |
985 | FROM accounts | |
986 | WHERE state = 'pending' AND REPLACE(hruid, '-', '') LIKE | |
987 | CONCAT('%', REPLACE(REPLACE(REPLACE({?}, ' ', ''), '-', ''), '\'', ''), '%') | |
988 | ORDER BY full_name"; | |
989 | if ($iterator) { | |
990 | return XDB::iterator($sql, $login); | |
991 | } else { | |
992 | $res = XDB::query($sql, $login); | |
993 | return $res->fetchAllAssoc(); | |
994 | } | |
995 | } | |
996 | ||
997 | ||
0d906109 RB |
998 | public static function iterOverUIDs($uids, $respect_order = true) |
999 | { | |
1000 | return new UserIterator(self::loadMainFieldsFromUIDs($uids, $respect_order)); | |
1001 | } | |
1002 | ||
1003 | /** Fetch a set of users from a list of UIDs | |
1004 | * @param $data The list of uids to fetch, or an array of arrays | |
1005 | * @param $orig If $data is an array of arrays, the subfield where uids are stored | |
1006 | * @param $dest If $data is an array of arrays, the subfield to fill with Users | |
1007 | * @param $fetchProfile Whether to fetch Profiles as well | |
1008 | * @return either an array of $uid => User, or $data with $data[$i][$dest] = User | |
1009 | */ | |
b774ddab | 1010 | public static function getBulkUsersWithUIDs(array $data, $orig = null, $dest = null, $fetchProfile = true) |
832e6fcb | 1011 | { |
07eb5b0e FB |
1012 | // Fetch the list of uids |
1013 | if (is_null($orig)) { | |
1014 | $uids = $data; | |
1015 | } else { | |
1016 | if (is_null($dest)) { | |
1017 | $dest = $orig; | |
1018 | } | |
1019 | $uids = array(); | |
1020 | foreach ($data as $key=>$entry) { | |
1021 | if (isset($entry[$orig])) { | |
1022 | $uids[] = $entry[$orig]; | |
1023 | } | |
1024 | } | |
1025 | } | |
1026 | ||
1027 | // Fetch users | |
38c6fe96 | 1028 | if (count($uids) == 0) { |
07eb5b0e | 1029 | return $data; |
38c6fe96 | 1030 | } |
0d906109 RB |
1031 | $users = self::iterOverUIDs($uids, true); |
1032 | ||
d865c296 | 1033 | $table = array(); |
b774ddab | 1034 | if ($fetchProfile) { |
0d906109 | 1035 | $profiles = Profile::iterOverUIDS($uids, true); |
7a8da8e8 PC |
1036 | if ($profiles != null) { |
1037 | $profile = $profiles->next(); | |
1038 | } else { | |
1039 | $profile = null; | |
1040 | } | |
b774ddab | 1041 | } |
0d906109 RB |
1042 | |
1043 | /** We iterate through the users, moving in | |
1044 | * profiles when they match the user ID : | |
1045 | * there can be users without a profile, but not | |
1046 | * the other way around. | |
1047 | */ | |
1048 | while (($user = $users->next())) { | |
b774ddab | 1049 | if ($fetchProfile) { |
7a8da8e8 | 1050 | if ($profile != null && $profile->owner_id == $user->id()) { |
0d906109 RB |
1051 | $user->_profile = $profile; |
1052 | $profile = $profiles->next(); | |
b774ddab FB |
1053 | } |
1054 | $user->_profile_fetched = true; | |
1055 | } | |
0d906109 | 1056 | $table[$user->id()] = $user; |
d865c296 | 1057 | } |
07eb5b0e FB |
1058 | |
1059 | // Build the result with respect to input order. | |
1060 | if (is_null($orig)) { | |
0d906109 | 1061 | return $table; |
07eb5b0e FB |
1062 | } else { |
1063 | foreach ($data as $key=>$entry) { | |
1064 | if (isset($entry[$orig])) { | |
1065 | $entry[$dest] = $table[$entry[$orig]]; | |
1066 | $data[$key] = $entry; | |
1067 | } | |
1068 | } | |
1069 | return $data; | |
832e6fcb | 1070 | } |
07eb5b0e FB |
1071 | } |
1072 | ||
b774ddab | 1073 | public static function getBulkUsersFromDB($fetchProfile = true) |
07eb5b0e FB |
1074 | { |
1075 | $args = func_get_args(); | |
1076 | $uids = call_user_func_array(array('XDB', 'fetchColumn'), $args); | |
b774ddab | 1077 | return self::getBulkUsersWithUIDs($uids, null, null, $fetchProfile); |
832e6fcb | 1078 | } |
9f8ebb9f VZ |
1079 | } |
1080 | ||
0d906109 RB |
1081 | /** Iterator over a set of Users |
1082 | * @param an XDB::Iterator obtained from a User::loadMainFieldsFromUIDs | |
1083 | */ | |
1084 | class UserIterator implements PlIterator | |
1085 | { | |
1086 | private $dbiter; | |
1087 | ||
1088 | public function __construct($dbiter) | |
1089 | { | |
1090 | $this->dbiter = $dbiter; | |
1091 | } | |
1092 | ||
1093 | public function next() | |
1094 | { | |
1095 | $data = $this->dbiter->next(); | |
1096 | if ($data == null) { | |
1097 | return null; | |
1098 | } else { | |
1099 | return User::getSilentWithValues(null, $data); | |
1100 | } | |
1101 | } | |
1102 | ||
1103 | public function total() | |
1104 | { | |
1105 | return $this->dbiter->total(); | |
1106 | } | |
1107 | ||
1108 | public function first() | |
1109 | { | |
1110 | return $this->dbiter->first(); | |
1111 | } | |
1112 | ||
1113 | public function last() | |
1114 | { | |
1115 | return $this->dbiter->last(); | |
1116 | } | |
1117 | } | |
1118 | ||
448c8cdc | 1119 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: |
9f8ebb9f | 1120 | ?> |