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