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