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