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