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