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