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