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