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