2 /***************************************************************************
3 * Copyright (C) 2003-2014 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class User
extends PlUser
24 const PERM_API_USER_READONLY
= 'api_user_readonly';
25 const PERM_DIRECTORY_AX
= 'directory_ax';
26 const PERM_DIRECTORY_PRIVATE
= 'directory_private';
27 const PERM_DIRECTORY_HIDDEN
= 'directory_hidden';
28 const PERM_EDIT_DIRECTORY
= 'edit_directory';
29 const PERM_FORUMS
= 'forums';
30 const PERM_GROUPS
= 'groups';
31 const PERM_LISTS
= 'lists';
32 const PERM_MAIL
= 'mail';
33 const PERM_PAYMENT
= 'payment';
35 public static $sub_mail_domains = array(
37 'master' => 'master.',
42 private $_profile_fetched = false
;
43 private $_profile = null
;
45 // Additional fields (non core)
46 protected $promo = null
;
48 // Implementation of the login to uid method.
49 protected function getLogin($login)
54 throw new UserNotFoundException();
57 if ($login instanceof User
) {
61 if ($login instanceof Profile
) {
62 $this->_profile
= $login;
63 $this->_profile_fetched
= true
;
64 $res = XDB
::query('SELECT ap.uid
65 FROM account_profiles AS ap
66 WHERE ap.pid = {?} AND FIND_IN_SET(\'owner\', perms)',
68 if ($res->numRows()) {
69 return $res->fetchOneCell();
71 throw new UserNotFoundException();
74 // If $data is an integer, fetches directly the result.
75 if (is_numeric($login)) {
76 $res = XDB
::query('SELECT uid
78 WHERE uid = {?}', $login);
79 if ($res->numRows()) {
80 return $res->fetchOneCell();
83 throw new UserNotFoundException();
86 // Checks whether $login is a valid hruid or not.
87 $res = XDB
::query('SELECT uid
89 WHERE hruid = {?}', $login);
90 if ($res->numRows()) {
91 return $res->fetchOneCell();
94 // From now, $login can only by an email alias, or an email redirection.
95 $login = trim(strtolower($login));
96 if (strstr($login, '@') === false
) {
97 $res = XDB
::fetchOneCell('SELECT uid
98 FROM email_source_account
102 list($email, $domain) = explode('@', $login);
103 $res = XDB
::fetchOneCell('SELECT s.uid
104 FROM email_source_account AS s
105 INNER JOIN email_virtual_domains AS m ON (s.domain = m.id)
106 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
107 WHERE s.email = {?} AND d.name = {?}',
115 // Looks for an account with the given email.
116 $res = XDB
::query('SELECT uid
118 WHERE email = {?}', $login);
119 if ($res->numRows() == 1) {
120 return $res->fetchOneCell();
123 // Otherwise, we do suppose $login is an email redirection.
124 $res = XDB
::query('SELECT uid
125 FROM email_redirect_account
126 WHERE redirect = {?}', $login);
127 if ($res->numRows() == 1) {
128 return $res->fetchOneCell();
131 throw new UserNotFoundException($res->fetchColumn(1));
134 protected static function loadMainFieldsFromUIDs(array $uids, $respect_order = true
)
137 return PlIteratorUtils
::emptyIterator();
143 if ($globals->asso('id')) {
144 $joins .= XDB
::format("LEFT JOIN group_members AS gpm ON (gpm.uid = a.uid AND gpm.asso_id = {?})\n", $globals->asso('id'));
145 $fields[] = 'gpm.perms AS group_perms';
146 $fields[] = 'gpm.comm AS group_comm';
147 $fields[] = 'gpm.position AS group_position';
149 if (count($fields) > 0) {
150 $fields = ', ' . implode(', ', $fields);
155 if ($respect_order) {
156 $order = 'ORDER BY ' . XDB
::formatCustomOrder('a.uid', $uids);
161 $uids = array_map(array('XDB', 'escape'), $uids);
163 return XDB
::iterator('SELECT a.uid, a.hruid, a.registration_date, h.uid IS NOT NULL AS homonym, a.firstname, a.lastname,
164 IF(ef.email IS NULL, NULL, CONCAT(ef.email, \'@\', mf.name)) AS forlife,
165 IF(ef.email IS NULL, NULL, CONCAT(ef.email, \'@\', df.name)) AS forlife_alternate,
166 IF(eb.email IS NULL, NULL, CONCAT(eb.email, \'@\', mb.name)) AS bestalias,
167 (er.redirect IS NULL AND a.state = \'active\' AND FIND_IN_SET(\'mail\', at.perms)) AS lost,
168 a.email, a.full_name, a.directory_name, a.display_name, a.sex = \'female\' AS gender,
169 IF(a.state = \'active\', CONCAT(at.perms, \',\', IF(a.user_perms IS NULL, \'\', a.user_perms)), \'\') AS perms,
170 a.user_perms, a.email_format, a.is_admin, a.state, a.type, at.description AS type_description, a.skin,
171 FIND_IN_SET(\'watch\', a.flags) AS watch, a.comment,
172 a.weak_password IS NOT NULL AS weak_access, g.g_account_name IS NOT NULL AS googleapps,
173 a.token IS NOT NULL AS token_access, a.token, a.last_version,
174 s.start AS lastlogin, s.host, fp.last_seen AS banana_last
177 INNER JOIN account_types AS at ON (at.type = a.type)
178 LEFT JOIN email_source_account AS ef ON (ef.uid = a.uid AND ef.type = \'forlife\')
179 LEFT JOIN email_virtual_domains AS mf ON (ef.domain = mf.id)
180 LEFT JOIN email_virtual_domains AS df ON (df.aliasing = mf.id AND
181 df.name LIKE CONCAT(\'%\', {?}) AND df.name NOT LIKE \'alumni.%\')
182 LEFT JOIN email_source_account AS eb ON (eb.uid = a.uid AND FIND_IN_SET(\'bestalias\',eb.flags))
183 LEFT JOIN email_virtual_domains AS mb ON (a.best_domain = mb.id)
184 LEFT JOIN email_redirect_account AS er ON (er.uid = a.uid AND er.flags = \'active\' AND er.broken_level < 3
185 AND er.type != \'imap\' AND er.type != \'homonym\')
186 LEFT JOIN homonyms_list AS h ON (h.uid = a.uid)
187 LEFT JOIN gapps_accounts AS g ON (a.uid = g.l_userid AND g.g_status = \'active\')
188 LEFT JOIN log_last_sessions AS ls ON (ls.uid = a.uid)
189 LEFT JOIN log_sessions AS s ON (s.id = ls.id)
190 LEFT JOIN forum_profiles AS fp ON (fp.uid = a.uid)
192 WHERE a.uid IN (' . implode(', ', $uids) . ')
194 ' . $order, $globals->mail
->domain2
, $globals->mail
->domain2
);
197 // Implementation of the data loader.
198 protected function loadMainFields()
200 if ($this->hruid
!== null
&& $this->forlife
!== null
201 && $this->bestalias
!== null
&& $this->display_name
!== null
202 && $this->full_name
!== null
&& $this->perms
!== null
203 && $this->gender
!== null
&& $this->email_format
!== null
) {
206 $this->fillFromArray(self
::loadMainFieldsFromUIDs(array($this->uid
))->next());
209 // Specialization of the buildPerms method
210 // This function build 'generic' permissions for the user. It does not take
211 // into account page specific permissions (e.g X.net group permissions)
212 protected function buildPerms()
214 if (!is_null($this->perm_flags
)) {
217 if ($this->perms
=== null
) {
218 $this->loadMainFields();
220 $this->perm_flags
= self
::makePerms($this->perms
, $this->is_admin
);
223 public function setPerms($perms)
225 $this->perms
= $perms;
226 $this->perm_flags
= null
;
229 /** Retrieve the 'general' read visibility.
230 * This is the maximum level of fields that may be viewed by the current user on other profiles.
233 * - Everyone can view 'public'
234 * - directory_ax gives access to 'AX' level, ie. the printed directory
235 * - directory_private gives access to 'private' level
236 * - admin and directory_hidden gives access to 'hidden' level
238 public function readVisibility()
240 $level = Visibility
::VIEW_NONE
;
241 if ($this->is_admin ||
$this->checkPerms('directory_hidden')) {
242 $level = Visibility
::VIEW_ADMIN
;
243 } elseif ($this->checkPerms('directory_private')) {
244 $level = Visibility
::VIEW_PRIVATE
;
245 } elseif ($this->checkPerms('directory_ax')) {
246 $level = Visibility
::VIEW_AX
;
248 $level = Visibility
::VIEW_PUBLIC
;
250 return Visibility
::get($level);
253 /** Retrieve the 'general' edit visibility.
254 * This is the maximum level of fields that may be edited by the current user on other profiles.
257 * - Only admins can edit the 'hidden' fields
258 * - If someone has 'directory_edit' (which is actually directory_ax_edit): AX level
259 * - Otherwise, nothing.
261 public function editVisibility()
263 $level = Visibility
::VIEW_NONE
;
264 if ($this->is_admin
) {
265 $level = Visibility
::VIEW_ADMIN
;
266 } elseif ($this->checkPerms('directory_edit')) {
267 $level = Visibility
::VIEW_AX
;
269 return Visibility
::get($level);
272 // We do not want to store the password in the object.
273 // So, fetch it 'on demand'
274 public function password()
276 return XDB
::fetchOneCell('SELECT a.password
278 WHERE a.uid = {?}', $this->id());
281 public function isActive()
283 return $this->state
== 'active';
286 /** Overload PlUser::promo(): there no promo defined for a user in the current
287 * schema. The promo is a field from the profile.
289 public function promo()
291 if (!$this->hasProfile()) {
294 return $this->profile()->promo();
297 public function category()
299 $promo = $this->promo();
300 if (!empty($promo)) {
303 return $this->type_description
;
307 public function firstName()
309 if (!$this->hasProfile()) {
310 return $this->firstname
;
312 return $this->profile()->firstName();
315 public function lastName()
317 if (!$this->hasProfile()) {
318 return $this->lastname
;
320 return $this->profile()->lastName();
323 public function displayName()
325 if (!$this->hasProfile()) {
326 return $this->display_name
;
328 return $this->profile()->yourself
;
331 public function fullName($with_promo = false
)
333 if (!$this->hasProfile()) {
334 return $this->full_name
;
336 return $this->profile()->fullName($with_promo);
339 public function shortName($with_promo = false
)
341 if (!$this->hasProfile()) {
342 return $this->full_name
;
344 return $this->profile()->shortName($with_promo);
347 public function directoryName()
349 if (!$this->hasProfile()) {
350 return $this->directory_name
;
352 return $this->profile()->directory_name
;
355 static public function compareDirectoryName($a, $b)
357 return strcasecmp(replace_accent($a->directoryName()), replace_accent($b->directoryName()));
360 /** Return the main profile attached with this account if any.
362 public function profile($forceFetch = false
, $fields = 0x0000, $visibility = null
)
364 if (!$this->_profile_fetched ||
$forceFetch) {
365 $this->_profile_fetched
= true
;
366 $this->_profile
= Profile
::get($this, $fields, $visibility);
367 } else if ($this->_profile
!== null
&& $visibility !== null
&& !$this->_profile
->visibility
->equals($visibility)) {
368 return Profile
::get($this, $fields, $visibility);
370 return $this->_profile
;
373 public function setPrefetchedProfile(Profile
$profile)
375 $this->_profile_fetched
= true
;
376 $this->_profile
= $profile;
379 /** Return true if the user has an associated profile.
381 public function hasProfile()
383 return !is_null($this->profile());
386 /** Return true if given a reference to the profile of this user.
388 public function isMyProfile($other)
392 } else if ($other instanceof Profile
) {
393 $profile = $this->profile();
394 return $profile && $profile->id() == $other->id();
399 /** Check if the user can edit to given profile.
401 public function canEdit(Profile
$profile)
403 if ($this->checkPerms(User
::PERM_EDIT_DIRECTORY
)) {
406 return XDB
::fetchOneCell('SELECT pid
407 FROM account_profiles
408 WHERE uid = {?} AND pid = {?}',
409 $this->id(), $profile->id());
412 /** Determines main email domain for this user.
414 public function mainEmailDomain()
416 if (array_key_exists($this->type
, self
::$sub_mail_domains)) {
417 return self
::$sub_mail_domains[$this->type
] . Platal
::globals()->mail
->domain
;
421 /** Determines alternate email domain for this user.
423 public function alternateEmailDomain()
425 if (array_key_exists($this->type
, self
::$sub_mail_domains)) {
426 return self
::$sub_mail_domains[$this->type
] . Platal
::globals()->mail
->domain2
;
430 public function forlifeEmailAlternate()
432 if (!empty($this->forlife_alternate
)) {
433 return $this->forlife_alternate
;
438 /** Fetch existing auxiliary alias.
440 public function emailAlias()
442 $aliases = $this->emailAliases();
443 if (count($aliases)) {
449 /** Fetch existing auxiliary aliases.
451 public function emailAliases()
453 return XDB
::fetchColumn('SELECT CONCAT(s.email, \'@\', d.name)
454 FROM email_source_account AS s
455 INNER JOIN email_virtual_domains AS m ON (s.domain = m.id)
456 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
457 WHERE s.uid = {?} AND s.type = \'alias_aux\'
462 /** Get all group aliases the user belongs to.
464 public function emailGroupAliases($domain = null
)
466 if (is_null($domain)) {
467 return XDB
::fetchColumn('SELECT CONCAT(v.email, \'@\', dv.name) AS alias
468 FROM email_virtual AS v
469 INNER JOIN email_virtual_domains AS dv ON (v.domain = dv.id)
470 INNER JOIN email_source_account AS s ON (s.uid = {?})
471 INNER JOIN email_virtual_domains AS ms ON (s.domain = ms.id)
472 INNER JOIN email_virtual_domains AS ds ON (ds.aliasing = ms.id)
473 WHERE v.redirect = CONCAT(s.email, \'@\', ds.name) AND v.type = \'alias\'',
476 return XDB
::fetchAllAssoc('alias',
477 'SELECT CONCAT(v.email, \'@\', dv.name) AS alias, MAX(v.redirect = CONCAT(s.email, \'@\', ds.name)) AS sub
478 FROM email_virtual AS v
479 INNER JOIN email_virtual_domains AS dv ON (v.domain = dv.id AND dv.name = {?})
480 INNER JOIN email_source_account AS s ON (s.uid = {?})
481 INNER JOIN email_virtual_domains AS ms ON (s.domain = ms.id)
482 INNER JOIN email_virtual_domains AS ds ON (ds.aliasing = ms.id)
483 WHERE v.type = \'alias\'
486 $domain, $this->id());
490 /** Get marketing informations
492 private function fetchMarketingData()
494 if (isset($this->pending_registration_date
)) {
497 $infos = XDB
::fetchOneAssoc('SELECT rp.date AS pending_registration_date, rp.email AS pending_registration_email,
498 rm.last AS last_marketing_date, rm.email AS last_marketing_email
500 LEFT JOIN register_pending AS rp ON (rp.uid = a.uid)
501 LEFT JOIN register_marketing AS rm ON (rm.uid = a.uid AND rm.last != \'0000-00-00\')
503 ORDER BY rm.last DESC', $this->id());
504 if (is_null($infos)) {
506 'pending_registration_date' => null
,
507 'pending_registration_email' => null
,
508 'last_marketing_date' => null
,
509 'last_marketing_email' => null
512 $this->fillFromArray($infos);
515 public function pendingRegistrationDate()
517 $this->fetchMarketingData();
518 return $this->pending_registration_date
;
521 public function pendingRegistrationEmail()
523 $this->fetchMarketingData();
524 return $this->pending_registration_email
;
527 public function lastMarketingDate()
529 $this->fetchMarketingData();
530 return $this->last_marketing_date
;
533 public function lastMarketingEmail()
535 $this->fetchMarketingData();
536 return $this->last_marketing_email
;
539 public function lastKnownEmail()
541 $this->fetchMarketingData();
542 if ($this->pending_registration_email
> $this->last_marketing_date
) {
543 return $this->pending_registration_email
;
545 return $this->last_marketing_email
;
549 /** Format of the emails sent by the site
551 public function setEmailFormat($format)
553 Platal
::assert($format == self
::FORMAT_HTML ||
$format == self
::FORMAT_TEXT
,
554 "Invalid email format \"$format\"");
555 XDB
::execute("UPDATE accounts
556 SET email_format = {?}
558 $format, $this->uid
);
559 $this->email_format
= $format;
562 /** Get watch informations
564 private function fetchWatchData()
566 if (isset($this->watch_actions
)) {
569 $watch = XDB
::fetchOneAssoc('SELECT flags AS watch_flags, actions AS watch_actions,
570 UNIX_TIMESTAMP(last) AS watch_last
572 WHERE uid = {?}', $this->id());
573 $watch['watch_flags'] = new PlFlagSet($watch['watch_flags']);
574 $watch['watch_actions'] = new PlFlagSet($watch['watch_actions']);
575 $watch['watch_promos'] = XDB
::fetchColumn('SELECT promo
577 WHERE uid = {?}', $this->id());
578 $watch['watch_groups'] = XDB
::fetchColumn("SELECT w.groupid
579 FROM watch_group AS w
580 INNER JOIN groups AS g ON (w.groupid = g.id AND NOT FIND_IN_SET('private', pub))
581 WHERE w.uid = {?}", $this->id());
582 $watch['watch_users'] = XDB
::fetchColumn('SELECT ni_id
584 WHERE uid = {?}', $this->id());
585 $this->fillFromArray($watch);
588 public function watchType($type)
590 $this->fetchWatchData();
591 return $this->watch_actions
->hasFlag($type);
594 public function watchContacts()
596 $this->fetchWatchData();
597 return $this->watch_flags
->hasFlag('contacts');
600 public function watchEmail()
602 $this->fetchWatchData();
603 return $this->watch_flags
->hasFlag('mail');
606 public function watchPromos()
608 $this->fetchWatchData();
609 return $this->watch_promos
;
612 public function watchGroups()
614 $this->fetchWatchData();
615 return $this->watch_groups
;
618 public function watchUsers()
620 $this->fetchWatchData();
621 return $this->watch_users
;
624 public function watchLast()
626 $this->fetchWatchData();
627 return $this->watch_last
;
630 public function invalidWatchCache()
632 unset($this->watch_actions
);
633 unset($this->watch_users
);
634 unset($this->watch_last
);
635 unset($this->watch_promos
);
636 unset($this->watch_groups
);
641 private $contacts = null
;
642 private function fetchContacts()
644 if (is_null($this->contacts
)) {
645 $this->contacts
= XDB
::fetchAllAssoc('contact', 'SELECT *
652 public function iterContacts()
654 $this->fetchContacts();
655 return Profile
::iterOverPIDs(array_keys($this->contacts
));
658 public function getContacts()
660 $this->fetchContacts();
661 return Profile
::getBulkProfilesWithPIDs(array_keys($this->contacts
));
664 public function isContact(Profile
$profile)
666 $this->fetchContacts();
667 return isset($this->contacts
[$profile->id()]);
670 public function isWatchedUser(Profile
$profile)
672 return in_array($profile->id(), $this->watchUsers());
676 private $groups = null
;
677 public function groups($institutions = false
, $onlyPublic = false
)
679 if (is_null($this->groups
)) {
680 $this->groups
= XDB
::fetchAllAssoc('asso_id', 'SELECT gm.asso_id, gm.perms, gm.comm,
681 g.diminutif, g.nom, g.site, g.cat,
683 FROM group_members AS gm
684 INNER JOIN groups AS g ON (g.id = gm.asso_id)
688 if (!$institutions && !$onlyPublic) {
689 return $this->groups
;
692 foreach ($this->groups
as $id=>$data) {
694 if ($data['cat'] != Group
::CAT_GROUPESX
&& $data['cat'] != Group
::CAT_INSTITUTIONS
) {
699 if ($data['pub'] != 'public') {
703 $result[$id] = $data;
709 public function groupCount()
711 return XDB
::fetchOneCell('SELECT COUNT(DISTINCT(asso_id))
717 public function inGroup($asso_id)
719 $res = XDB
::fetchOneCell('SELECT COUNT(*)
721 WHERE uid = {?} AND asso_id = {?}',
722 $this->id(), $asso_id);
728 * *always deletes in: account_lost_passwords, register_marketing,
729 * register_pending, register_subs, watch_nonins, watch, watch_promo, watch_group,
730 * *always keeps in: account_types, accounts, email_virtual, carvas,
731 * group_members, homonyms_list, newsletter_ins, register_mstats, email_source_account
732 * *deletes if $clearAll: account_auth_openid, announce_read, contacts,
733 * email_redirect_account, email_redirect_account, email_send_save, forum_innd, forum_profiles,
734 * forum_subs, gapps_accounts, gapps_nicknames, group_announces_read,
735 * group_member_sub_requests, reminder, requests, requests_hidden,
737 * *modifies if $clearAll: accounts
740 * *$clearAll == false: when a user dies, her family still needs to keep in
741 * touch with the community.
742 * *$clearAll == true: in every other case we want the account to be fully
743 * deleted so that it can not be used anymore.
745 public function clear($clearAll = true
)
747 $tables = array('account_lost_passwords', 'register_marketing',
748 'register_pending', 'register_subs', 'watch_nonins',
749 'watch', 'watch_promo', 'watch_group');
751 foreach ($tables as $t) {
752 XDB
::execute('DELETE FROM ' . $t . '
760 $groupIds = XDB
::iterator('SELECT asso_id
764 while ($groupId = $groupIds->next()) {
765 $group = Group
::get($groupId);
766 if (!empty($group) && $group->notif_unsub
) {
767 $mailer = new PlMailer('xnetgrp/unsubscription-notif.mail.tpl');
768 $admins = $group->iterAdmins();
769 while ($admin = $admins->next()) {
770 $mailer->addTo($admin);
772 $mailer->assign('group', $group->shortname
);
773 $mailer->assign('user', $this);
774 $mailer->assign('selfdone', false
);
779 $tables = array('account_auth_openid', 'announce_read', 'contacts',
781 'forum_innd', 'forum_profiles', 'forum_subs',
782 'group_announces_read', 'group_members',
783 'group_member_sub_requests', 'reminder', 'requests',
785 foreach ($tables as $t) {
786 XDB
::execute('DELETE FROM ' . $t . '
790 XDB
::execute('DELETE FROM email_redirect_account
791 WHERE uid = {?} AND type != \'homonym\'',
793 XDB
::execute('DELETE FROM email_virtual
794 WHERE redirect = {?}',
795 $this->forlifeEmail());
797 foreach (array('gapps_accounts', 'gapps_nicknames') as $t) {
798 XDB
::execute('DELETE FROM ' . $t . '
799 WHERE l_userid = {?}',
803 XDB
::execute("UPDATE accounts
804 SET registration_date = 0, state = 'pending', password = NULL,
805 weak_password = NULL, token = NULL, is_admin = 0
809 if ($globals->mailstorage
->googleapps_domain
) {
810 require_once 'googleapps.inc.php';
812 if (GoogleAppsAccount
::account_status($this->id())) {
813 $account = new GoogleAppsAccount($this);
819 $mmlist = new MMList(S
::user());
820 $mmlist->kill($this->hruid
, $clearAll);
823 // Merge all infos in other user and then clean this one
824 public function mergeIn(User
$newuser) {
825 if ($this->profile()) {
826 // Don't disable user with profile in this way.
828 Platal
::page()->trigError('Impossible de fusionner les comptes ' . $this->hruid
. ' et ' . $newuser->hruid
.
829 '. Contacte support@' . $globals->mail
->domain
. '.');
833 if ($this->forlifeEmail()) {
834 // If the new user is not registered and does not have already an email address,
835 // we need to give him the old user's email address if he has any.
836 if (!$newuser->perms
) {
837 XDB
::execute('UPDATE accounts
839 WHERE uid = {?} AND email IS NULL',
840 $this->forlifeEmail(), $newuser->id());
842 // Reftech new user so its forlifeEmail will be correct.
843 $newuser = self
::getSilentWithUID($newuser->id());
846 // Change email used in mailing lists.
847 if ($this->forlifeEmail() != $newuser->forlifeEmail()) {
848 // The super user is the user who has the right to do the modification.
849 $super_user = S
::user();
850 // group mailing lists
851 $group_domains = XDB
::fetchColumn('SELECT g.mail_domain
853 INNER JOIN group_members AS gm ON(g.id = gm.asso_id)
854 WHERE g.mail_domain != \'\' AND gm.uid = {?}',
856 foreach ($group_domains as $mail_domain) {
857 $mmlist = new MMList($super_user, $mail_domain);
858 $mmlist->replace_email_in_all($this->forlifeEmail(), $newuser->forlifeEmail());
861 $mmlist = new MMList($super_user);
862 $mmlist->replace_email_in_all($this->forlifeEmail(), $newuser->forlifeEmail());
866 // Updates user in following tables.
867 foreach (array('group_announces', 'payment_transactions', 'log_sessions', 'group_events') as $table) {
868 XDB
::execute('UPDATE ' . $table . '
871 $newuser->id(), $this->id());
874 // Merges user in following tables, ie updates when possible, then deletes remaining occurences of the old user.
875 foreach (array('group_announces_read', 'group_event_participants', 'group_member_sub_requests', 'group_members', 'email_redirect_account') as $table) {
876 XDB
::execute('UPDATE IGNORE ' . $table . '
879 $newuser->id(), $this->id());
880 XDB
::execute('DELETE FROM ' . $table . '
885 // Eventually updates last session id and deletes old user's accounts entry.
886 $lastSession = XDB
::fetchOneCell('SELECT id
892 XDB
::execute('UPDATE log_last_sessions
895 $lastSession, $newuser->id());
896 XDB
::execute('DELETE FROM accounts
903 // Return permission flags for a given permission level.
904 public static function makePerms($perms, $is_admin)
906 $flags = new PlFlagSet($perms);
908 $flags->addFlag(PERMS_ADMIN
);
911 // Access to private directory implies access to 'less'-private version.
912 if ($flags->hasFlag('directory_private')) {
913 $flags->addFlag('directory_ax');
918 // Implementation of the default user callback.
919 public static function _default_user_callback($login, $results)
921 $result_count = count($results);
922 if ($result_count == 0 ||
!S
::admin()) {
923 Platal
::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login");
925 Platal
::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results));
929 public static function makeHomonymHrmid($alias)
931 return 'h.' . $alias . '.' . Platal
::globals()->mail
->domain
;
934 public static function isMainMailDomain($domain)
938 $is_main_domain = false
;
939 foreach (self
::$sub_mail_domains as $sub_domain) {
940 $is_main_domain = $is_main_domain ||
$domain == ($sub_domain . $globals->mail
->domain
) ||
$domain == ($sub_domain . $globals->mail
->domain2
);
942 return $is_main_domain;
945 public static function isAliasMailDomain($domain)
949 return $domain == $globals->mail
->alias_dom ||
$domain == $globals->mail
->alias_dom2
;
952 // Implementation of the static email locality checker.
953 public static function isForeignEmailAddress($email)
955 if (strpos($email, '@') === false
) {
959 list(, $domain) = explode('@', $email);
960 return !(self
::isMainMailDomain($domain) || self
::isAliasMailDomain($domain));
963 /* Tries to find pending accounts with an hruid close to $login. */
964 public static function getPendingAccounts($login, $iterator = false
)
966 if (strpos($login, '@') === false
) {
970 list($login, $domain) = explode('@', $login);
972 if ($domain && !self
::isMainMailDomain($domain)) {
976 $sql = "SELECT uid, full_name
978 WHERE state = 'pending' AND REPLACE(hruid, '-', '') LIKE
979 CONCAT('%', REPLACE(REPLACE(REPLACE({?}, ' ', ''), '-', ''), '\'', ''), '%')
982 return XDB
::iterator($sql, $login);
984 $res = XDB
::query($sql, $login);
985 return $res->fetchAllAssoc();
990 public static function iterOverUIDs($uids, $respect_order = true
)
992 return new UserIterator(self
::loadMainFieldsFromUIDs($uids, $respect_order));
995 /** Fetch a set of users from a list of UIDs
996 * @param $data The list of uids to fetch, or an array of arrays
997 * @param $orig If $data is an array of arrays, the subfield where uids are stored
998 * @param $dest If $data is an array of arrays, the subfield to fill with Users
999 * @param $fetchProfile Whether to fetch Profiles as well
1000 * @return either an array of $uid => User, or $data with $data[$i][$dest] = User
1002 public static function getBulkUsersWithUIDs(array $data, $orig = null
, $dest = null
, $fetchProfile = true
)
1004 // Fetch the list of uids
1005 if (is_null($orig)) {
1008 if (is_null($dest)) {
1012 foreach ($data as $key=>$entry) {
1013 if (isset($entry[$orig])) {
1014 $uids[] = $entry[$orig];
1020 if (count($uids) == 0) {
1023 $users = self
::iterOverUIDs($uids, true
);
1026 if ($fetchProfile) {
1027 $profiles = Profile
::iterOverUIDS($uids, true
);
1028 if ($profiles != null
) {
1029 $profile = $profiles->next();
1035 /** We iterate through the users, moving in
1036 * profiles when they match the user ID :
1037 * there can be users without a profile, but not
1038 * the other way around.
1040 while (($user = $users->next())) {
1041 if ($fetchProfile) {
1042 if ($profile != null
&& $profile->owner_id
== $user->id()) {
1043 $user->_profile
= $profile;
1044 $profile = $profiles->next();
1046 $user->_profile_fetched
= true
;
1048 $table[$user->id()] = $user;
1051 // Build the result with respect to input order.
1052 if (is_null($orig)) {
1055 foreach ($data as $key=>$entry) {
1056 if (isset($entry[$orig])) {
1057 $entry[$dest] = $table[$entry[$orig]];
1058 $data[$key] = $entry;
1065 public static function getBulkUsersFromDB($fetchProfile = true
)
1067 $args = func_get_args();
1068 $uids = call_user_func_array(array('XDB', 'fetchColumn'), $args);
1069 return self
::getBulkUsersWithUIDs($uids, null
, null
, $fetchProfile);
1073 /** Iterator over a set of Users
1074 * @param an XDB::Iterator obtained from a User::loadMainFieldsFromUIDs
1076 class UserIterator
implements PlIterator
1080 public function __construct($dbiter)
1082 $this->dbiter
= $dbiter;
1085 public function next()
1087 $data = $this->dbiter
->next();
1088 if ($data == null
) {
1091 return User
::getSilentWithValues(null
, $data);
1095 public function total()
1097 return $this->dbiter
->total();
1100 public function first()
1102 return $this->dbiter
->first();
1105 public function last()
1107 return $this->dbiter
->last();
1111 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: