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