2 /***************************************************************************
3 * Copyright (C) 2003-2011 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_EDIT_DIRECTORY
= 'edit_directory';
28 const PERM_FORUMS
= 'forums';
29 const PERM_GROUPS
= 'groups';
30 const PERM_LISTS
= 'lists';
31 const PERM_MAIL
= 'mail';
32 const PERM_PAYMENT
= 'payment';
34 public static $sub_mail_domains = array(
36 'master' => 'master.',
41 private $_profile_fetched = false
;
42 private $_profile = null
;
44 // Additional fields (non core)
45 protected $promo = null
;
47 // Implementation of the login to uid method.
48 protected function getLogin($login)
53 throw new UserNotFoundException();
56 if ($login instanceof User
) {
60 if ($login instanceof Profile
) {
61 $this->_profile
= $login;
62 $this->_profile_fetched
= true
;
63 $res = XDB
::query('SELECT ap.uid
64 FROM account_profiles AS ap
65 WHERE ap.pid = {?} AND FIND_IN_SET(\'owner\', perms)',
67 if ($res->numRows()) {
68 return $res->fetchOneCell();
70 throw new UserNotFoundException();
73 // If $data is an integer, fetches directly the result.
74 if (is_numeric($login)) {
75 $res = XDB
::query('SELECT uid
77 WHERE uid = {?}', $login);
78 if ($res->numRows()) {
79 return $res->fetchOneCell();
82 throw new UserNotFoundException();
85 // Checks whether $login is a valid hruid or not.
86 $res = XDB
::query('SELECT uid
88 WHERE hruid = {?}', $login);
89 if ($res->numRows()) {
90 return $res->fetchOneCell();
93 // From now, $login can only by an email alias, or an email redirection.
94 $login = trim(strtolower($login));
95 if (strstr($login, '@') === false
) {
96 $res = XDB
::fetchOneCell('SELECT uid
97 FROM email_source_account
101 list($email, $domain) = explode('@', $login);
102 $res = XDB
::fetchOneCell('SELECT s.uid
103 FROM email_source_account AS s
104 INNER JOIN email_virtual_domains AS m ON (s.domain = m.id)
105 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
106 WHERE s.email = {?} AND d.name = {?}',
114 // Looks for an account with the given email.
115 $res = XDB
::query('SELECT uid
117 WHERE email = {?}', $login);
118 if ($res->numRows() == 1) {
119 return $res->fetchOneCell();
122 // Otherwise, we do suppose $login is an email redirection.
123 $res = XDB
::query('SELECT uid
124 FROM email_redirect_account
125 WHERE redirect = {?}', $login);
126 if ($res->numRows() == 1) {
127 return $res->fetchOneCell();
130 throw new UserNotFoundException($res->fetchColumn(1));
133 protected static function loadMainFieldsFromUIDs(array $uids, $respect_order = true
)
136 return PlIteratorUtils
::emptyIterator();
142 if ($globals->asso('id')) {
143 $joins .= XDB
::format("LEFT JOIN group_members AS gpm ON (gpm.uid = a.uid AND gpm.asso_id = {?})\n", $globals->asso('id'));
144 $fields[] = 'gpm.perms AS group_perms';
145 $fields[] = 'gpm.comm AS group_comm';
146 $fields[] = 'gpm.position AS group_position';
148 if (count($fields) > 0) {
149 $fields = ', ' . implode(', ', $fields);
154 if ($respect_order) {
155 $order = 'ORDER BY ' . XDB
::formatCustomOrder('a.uid', $uids);
160 $uids = array_map(array('XDB', 'escape'), $uids);
162 return XDB
::iterator('SELECT a.uid, a.hruid, a.registration_date, h.uid IS NOT NULL AS homonym, a.firstname, a.lastname,
163 IF(ef.email IS NULL, NULL, CONCAT(ef.email, \'@\', mf.name)) AS forlife,
164 IF(ef.email IS NULL, NULL, CONCAT(ef.email, \'@\', df.name)) AS forlife_alternate,
165 IF(eb.email IS NULL, NULL, CONCAT(eb.email, \'@\', mb.name)) AS bestalias,
166 (er.redirect IS NULL AND a.state = \'active\' AND FIND_IN_SET(\'mail\', at.perms)) AS lost,
167 a.email, a.full_name, a.directory_name, a.display_name, a.sex = \'female\' AS gender,
168 IF(a.state = \'active\', CONCAT(at.perms, \',\', IF(a.user_perms IS NULL, \'\', a.user_perms)), \'\') AS perms,
169 a.user_perms, a.email_format, a.is_admin, a.state, a.type, at.description AS type_description, a.skin,
170 FIND_IN_SET(\'watch\', a.flags) AS watch, a.comment,
171 a.weak_password IS NOT NULL AS weak_access, g.g_account_name IS NOT NULL AS googleapps,
172 a.token IS NOT NULL AS token_access, a.token, a.last_version,
173 UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, UNIX_TIMESTAMP(fp.last_seen) AS banana_last
176 INNER JOIN account_types AS at ON (at.type = a.type)
177 LEFT JOIN email_source_account AS ef ON (ef.uid = a.uid AND ef.type = \'forlife\')
178 LEFT JOIN email_virtual_domains AS mf ON (ef.domain = mf.id)
179 LEFT JOIN email_virtual_domains AS df ON (df.aliasing = mf.id AND
180 df.name LIKE CONCAT(\'%\', {?}) AND df.name NOT LIKE \'alumni.%\')
181 LEFT JOIN email_source_account AS eb ON (eb.uid = a.uid AND FIND_IN_SET(\'bestalias\',eb.flags))
182 LEFT JOIN email_virtual_domains AS mb ON (a.best_domain = mb.id)
183 LEFT JOIN email_redirect_account AS er ON (er.uid = a.uid AND er.flags = \'active\' AND er.broken_level < 3
184 AND er.type != \'imap\' AND er.type != \'homonym\')
185 LEFT JOIN homonyms_list AS h ON (h.uid = a.uid)
186 LEFT JOIN gapps_accounts AS g ON (a.uid = g.l_userid AND g.g_status = \'active\')
187 LEFT JOIN log_last_sessions AS ls ON (ls.uid = a.uid)
188 LEFT JOIN log_sessions AS s ON (s.id = ls.id)
189 LEFT JOIN forum_profiles AS fp ON (fp.uid = a.uid)
191 WHERE a.uid IN (' . implode(', ', $uids) . ')
193 ' . $order, $globals->mail
->domain2
, $globals->mail
->domain2
);
196 // Implementation of the data loader.
197 protected function loadMainFields()
199 if ($this->hruid
!== null
&& $this->forlife
!== null
200 && $this->bestalias
!== null
&& $this->display_name
!== null
201 && $this->full_name
!== null
&& $this->perms
!== null
202 && $this->gender
!== null
&& $this->email_format
!== null
) {
205 $this->fillFromArray(self
::loadMainFieldsFromUIDs(array($this->uid
))->next());
208 // Specialization of the buildPerms method
209 // This function build 'generic' permissions for the user. It does not take
210 // into account page specific permissions (e.g X.net group permissions)
211 protected function buildPerms()
213 if (!is_null($this->perm_flags
)) {
216 if ($this->perms
=== null
) {
217 $this->loadMainFields();
219 $this->perm_flags
= self
::makePerms($this->perms
, $this->is_admin
);
222 public function setPerms($perms)
224 $this->perms
= $perms;
225 $this->perm_flags
= null
;
228 // We do not want to store the password in the object.
229 // So, fetch it 'on demand'
230 public function password()
232 return XDB
::fetchOneCell('SELECT a.password
234 WHERE a.uid = {?}', $this->id());
237 public function isActive()
239 return $this->state
== 'active';
242 /** Overload PlUser::promo(): there no promo defined for a user in the current
243 * schema. The promo is a field from the profile.
245 public function promo()
247 if (!$this->hasProfile()) {
250 return $this->profile()->promo();
253 public function category()
255 $promo = $this->promo();
256 if (!empty($promo)) {
259 return $this->type_description
;
263 public function firstName()
265 if (!$this->hasProfile()) {
266 return $this->displayName();
268 return $this->profile()->firstName();
271 public function lastName()
273 if (!$this->hasProfile()) {
276 return $this->profile()->lastName();
279 public function displayName()
281 if (!$this->hasProfile()) {
282 return $this->display_name
;
284 return $this->profile()->yourself
;
287 public function fullName($with_promo = false
)
289 if (!$this->hasProfile()) {
290 return $this->full_name
;
292 return $this->profile()->fullName($with_promo);
295 public function directoryName()
297 if (!$this->hasProfile()) {
298 return $this->directory_name
;
300 return $this->profile()->directory_name
;
303 static public function compareDirectoryName($a, $b)
305 return strcasecmp(replace_accent($a->directoryName()), replace_accent($b->directoryName()));
308 /** Return the main profile attached with this account if any.
310 public function profile($forceFetch = false
, $fields = 0x0000, $visibility = null
)
312 if (!$this->_profile_fetched ||
$forceFetch) {
313 $this->_profile_fetched
= true
;
314 $this->_profile
= Profile
::get($this, $fields, $visibility);
316 return $this->_profile
;
319 /** Return true if the user has an associated profile.
321 public function hasProfile()
323 return !is_null($this->profile());
326 /** Return true if given a reference to the profile of this user.
328 public function isMyProfile($other)
332 } else if ($other instanceof Profile
) {
333 $profile = $this->profile();
334 return $profile && $profile->id() == $other->id();
339 /** Check if the user can edit to given profile.
341 public function canEdit(Profile
$profile)
343 if ($this->checkPerms(User
::PERM_EDIT_DIRECTORY
)) {
346 return XDB
::fetchOneCell('SELECT pid
347 FROM account_profiles
348 WHERE uid = {?} AND pid = {?}',
349 $this->id(), $profile->id());
352 /** Determines main email domain for this user.
354 public function mainEmailDomain()
356 if (array_key_exists($this->type
, self
::$sub_mail_domains)) {
357 return self
::$sub_mail_domains[$this->type
] . Platal
::globals()->mail
->domain
;
361 /** Determines alternate email domain for this user.
363 public function alternateEmailDomain()
365 if (array_key_exists($this->type
, self
::$sub_mail_domains)) {
366 return self
::$sub_mail_domains[$this->type
] . Platal
::globals()->mail
->domain2
;
370 public function forlifeEmailAlternate()
372 if (!empty($this->forlife_alternate
)) {
373 return $this->forlife_alternate
;
378 /** Fetch existing auxiliary alias.
380 public function emailAlias()
382 $aliases = $this->emailAliases();
383 if (count($aliases)) {
389 /** Fetch existing auxiliary aliases.
391 public function emailAliases()
393 return XDB
::fetchColumn('SELECT CONCAT(s.email, \'@\', d.name)
394 FROM email_source_account AS s
395 INNER JOIN email_virtual_domains AS m ON (s.domain = m.id)
396 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
397 WHERE s.uid = {?} AND s.type = \'alias_aux\'
402 /** Get all group aliases the user belongs to.
404 public function emailGroupAliases($domain = null
)
406 if (is_null($domain)) {
407 return XDB
::fetchColumn('SELECT CONCAT(v.email, \'@\', dv.name) AS alias
408 FROM email_virtual AS v
409 INNER JOIN email_virtual_domains AS dv ON (v.domain = dv.id)
410 INNER JOIN email_source_account AS s ON (s.uid = {?})
411 INNER JOIN email_virtual_domains AS ms ON (s.domain = ms.id)
412 INNER JOIN email_virtual_domains AS ds ON (ds.aliasing = ms.id)
413 WHERE v.redirect = CONCAT(s.email, \'@\', ds.name) AND v.type = \'alias\'',
416 return XDB
::fetchAllAssoc('alias',
417 'SELECT CONCAT(v.email, \'@\', dv.name) AS alias, MAX(v.redirect = CONCAT(s.email, \'@\', ds.name)) AS sub
418 FROM email_virtual AS v
419 INNER JOIN email_virtual_domains AS dv ON (v.domain = dv.id AND dv.name = {?})
420 INNER JOIN email_source_account AS s ON (s.uid = {?})
421 INNER JOIN email_virtual_domains AS ms ON (s.domain = ms.id)
422 INNER JOIN email_virtual_domains AS ds ON (ds.aliasing = ms.id)
423 WHERE v.type = \'alias\'
426 $domain, $this->id());
430 /** Get marketing informations
432 private function fetchMarketingData()
434 if (isset($this->pending_registration_date
)) {
437 $infos = XDB
::fetchOneAssoc('SELECT rp.date AS pending_registration_date, rp.email AS pending_registration_email,
438 rm.last AS last_marketing_date, rm.email AS last_marketing_email
440 LEFT JOIN register_pending AS rp ON (rp.uid = a.uid)
441 LEFT JOIN register_marketing AS rm ON (rm.uid = a.uid AND rm.last != \'0000-00-00\')
443 ORDER BY rm.last DESC', $this->id());
444 if (is_null($infos)) {
446 'pending_registration_date' => null
,
447 'pending_registration_email' => null
,
448 'last_marketing_date' => null
,
449 'last_marketing_email' => null
452 $this->fillFromArray($infos);
455 public function pendingRegistrationDate()
457 $this->fetchMarketingData();
458 return $this->pending_registration_date
;
461 public function pendingRegistrationEmail()
463 $this->fetchMarketingData();
464 return $this->pending_registration_email
;
467 public function lastMarketingDate()
469 $this->fetchMarketingData();
470 return $this->last_marketing_date
;
473 public function lastMarketingEmail()
475 $this->fetchMarketingData();
476 return $this->last_marketing_email
;
479 public function lastKnownEmail()
481 $this->fetchMarketingData();
482 if ($this->pending_registration_email
> $this->last_marketing_date
) {
483 return $this->pending_registration_email
;
485 return $this->last_marketing_email
;
489 /** Format of the emails sent by the site
491 public function setEmailFormat($format)
493 Platal
::assert($format == self
::FORMAT_HTML ||
$format == self
::FORMAT_TEXT
,
494 "Invalid email format \"$format\"");
495 XDB
::execute("UPDATE accounts
496 SET email_format = {?}
498 $format, $this->uid
);
499 $this->email_format
= $format;
502 /** Get watch informations
504 private function fetchWatchData()
506 if (isset($this->watch_actions
)) {
509 $watch = XDB
::fetchOneAssoc('SELECT flags AS watch_flags, actions AS watch_actions,
510 UNIX_TIMESTAMP(last) AS watch_last
512 WHERE uid = {?}', $this->id());
513 $watch['watch_flags'] = new PlFlagSet($watch['watch_flags']);
514 $watch['watch_actions'] = new PlFlagSet($watch['watch_actions']);
515 $watch['watch_promos'] = XDB
::fetchColumn('SELECT promo
517 WHERE uid = {?}', $this->id());
518 $watch['watch_users'] = XDB
::fetchColumn('SELECT ni_id
520 WHERE uid = {?}', $this->id());
521 $this->fillFromArray($watch);
524 public function watchType($type)
526 $this->fetchWatchData();
527 return $this->watch_actions
->hasFlag($type);
530 public function watchContacts()
532 $this->fetchWatchData();
533 return $this->watch_flags
->hasFlag('contacts');
536 public function watchEmail()
538 $this->fetchWatchData();
539 return $this->watch_flags
->hasFlag('mail');
542 public function watchPromos()
544 $this->fetchWatchData();
545 return $this->watch_promos
;
548 public function watchUsers()
550 $this->fetchWatchData();
551 return $this->watch_users
;
554 public function watchLast()
556 $this->fetchWatchData();
557 return $this->watch_last
;
560 public function invalidWatchCache()
562 unset($this->watch_actions
);
563 unset($this->watch_users
);
564 unset($this->watch_last
);
565 unset($this->watch_promos
);
570 private $contacts = null
;
571 private function fetchContacts()
573 if (is_null($this->contacts
)) {
574 $this->contacts
= XDB
::fetchAllAssoc('contact', 'SELECT *
581 public function iterContacts()
583 $this->fetchContacts();
584 return Profile
::iterOverPIDs(array_keys($this->contacts
));
587 public function getContacts()
589 $this->fetchContacts();
590 return Profile
::getBulkProfilesWithPIDs(array_keys($this->contacts
));
593 public function isContact(Profile
$profile)
595 $this->fetchContacts();
596 return isset($this->contacts
[$profile->id()]);
599 public function isWatchedUser(Profile
$profile)
601 return in_array($profile->id(), $this->watchUsers());
605 private $groups = null
;
606 public function groups($institutions = false
, $onlyPublic = false
)
608 if (is_null($this->groups
)) {
609 $this->groups
= XDB
::fetchAllAssoc('asso_id', 'SELECT gm.asso_id, gm.perms, gm.comm,
610 g.diminutif, g.nom, g.site, g.cat,
612 FROM group_members AS gm
613 INNER JOIN groups AS g ON (g.id = gm.asso_id)
617 if (!$institutions && !$onlyPublic) {
618 return $this->groups
;
621 foreach ($this->groups
as $id=>$data) {
623 if ($data['cat'] != Group
::CAT_GROUPESX
&& $data['cat'] != Group
::CAT_INSTITUTIONS
) {
628 if ($data['pub'] != 'public') {
632 $result[$id] = $data;
638 public function groupCount()
640 return XDB
::fetchOneCell('SELECT COUNT(DISTINCT(asso_id))
646 public function inGroup($asso_id)
648 $res = XDB
::fetchOneCell('SELECT COUNT(*)
650 WHERE uid = {?} AND asso_id = {?}',
651 $this->id(), $asso_id);
657 * *always deletes in: account_lost_passwords, register_marketing,
658 * register_pending, register_subs, watch_nonins, watch, watch_promo
659 * *always keeps in: account_types, accounts, email_virtual, carvas,
660 * group_members, homonyms_list, newsletter_ins, register_mstats, email_source_account
661 * *deletes if $clearAll: account_auth_openid, announce_read, contacts,
662 * email_redirect_account, email_redirect_account, email_send_save, forum_innd, forum_profiles,
663 * forum_subs, gapps_accounts, gapps_nicknames, group_announces_read,
664 * group_member_sub_requests, reminder, requests, requests_hidden,
666 * *modifies if $clearAll: accounts
669 * *$clearAll == false: when a user dies, her family still needs to keep in
670 * touch with the community.
671 * *$clearAll == true: in every other case we want the account to be fully
672 * deleted so that it can not be used anymore.
674 public function clear($clearAll = true
)
676 $tables = array('account_lost_passwords', 'register_marketing',
677 'register_pending', 'register_subs', 'watch_nonins',
678 'watch', 'watch_promo');
680 foreach ($tables as $t) {
681 XDB
::execute('DELETE FROM ' . $t . '
689 $groupIds = XDB
::iterator('SELECT asso_id
693 while ($groupId = $groupIds->next()) {
694 $group = Group
::get($groupId);
695 if (!empty($group) && $group->notif_unsub
) {
696 $mailer = new PlMailer('xnetgrp/unsubscription-notif.mail.tpl');
697 $admins = $group->iterAdmins();
698 while ($admin = $admins->next()) {
699 $mailer->addTo($admin);
701 $mailer->assign('group', $group->shortname
);
702 $mailer->assign('user', $this);
703 $mailer->assign('selfdone', false
);
708 $tables = array('account_auth_openid', 'announce_read', 'contacts',
710 'forum_innd', 'forum_profiles', 'forum_subs',
711 'group_announces_read', 'group_members',
712 'group_member_sub_requests', 'reminder', 'requests',
714 foreach ($tables as $t) {
715 XDB
::execute('DELETE FROM ' . $t . '
719 XDB
::execute('DELETE FROM email_redirect_account
720 WHERE uid = {?} AND type != \'homonym\'',
722 XDB
::execute('DELETE FROM email_virtual
723 WHERE redirect = {?}',
724 $this->forlifeEmail());
726 foreach (array('gapps_accounts', 'gapps_nicknames') as $t) {
727 XDB
::execute('DELETE FROM ' . $t . '
728 WHERE l_userid = {?}',
732 XDB
::execute("UPDATE accounts
733 SET registration_date = 0, state = 'pending', password = NULL,
734 weak_password = NULL, token = NULL, is_admin = 0
738 if ($globals->mailstorage
->googleapps_domain
) {
739 require_once 'googleapps.inc.php';
741 if (GoogleAppsAccount
::account_status($this->id())) {
742 $account = new GoogleAppsAccount($this);
748 $mmlist = new MMList(S
::user());
749 $mmlist->kill($this->hruid
, $clearAll);
752 // Merge all infos in other user and then clean this one
753 public function mergeIn(User
$newuser) {
754 if ($this->profile()) {
755 // Don't disable user with profile in this way.
757 Platal
::page()->trigError('Impossible de fusionner les comptes ' . $this->hruid
. ' et ' . $newuser->hruid
.
758 '. Contacte support@' . $globals->mail
->domain
. '.');
762 if ($this->forlifeEmail()) {
763 // If the new user is not registered and does not have already an email address,
764 // we need to give him the old user's email address if he has any.
765 if (!$newuser->perms
) {
766 XDB
::execute('UPDATE accounts
768 WHERE uid = {?} AND email IS NULL',
769 $this->forlifeEmail(), $newuser->id());
771 // Reftech new user so its forlifeEmail will be correct.
772 $newuser = getSilentWithUID($newuser->id());
775 // Change email used in mailing lists.
776 if ($this->forlifeEmail() != $newuser->forlifeEmail()) {
777 // group mailing lists
778 $group_domains = XDB
::fetchColumn('SELECT g.mail_domain
780 INNER JOIN group_members AS gm ON(g.id = gm.asso_id)
781 WHERE g.mail_domain != \'\' AND gm.uid = {?}',
783 foreach ($group_domains as $mail_domain) {
784 $mmlist = new MMList($this, $mail_domain);
785 $mmlist->replace_email_in_all($this->forlifeEmail(), $newuser->forlifeEmail());
788 $mmlist = new MMList($this);
789 $mmlist->replace_email_in_all($this->forlifeEmail(), $newuser->forlifeEmail());
793 // Updates user in following tables.
794 foreach (array('group_announces', 'payment_transactions', 'log_sessions', 'group_events') as $table) {
795 XDB
::execute('UPDATE ' . $table . '
798 $newuser->id(), $this->id());
801 // Merges user in following tables, ie updates when possible, then deletes remaining occurences of the old user.
802 foreach (array('group_announces_read', 'group_event_participants', 'group_member_sub_requests', 'group_members', 'email_redirect_account') as $table) {
803 XDB
::execute('UPDATE IGNORE ' . $table . '
806 $newuser->id(), $this->id());
807 XDB
::execute('DELETE FROM ' . $table . '
812 // Eventually updates last session id and deletes old user's accounts entry.
813 $lastSession = XDB
::fetchOneCell('SELECT id
819 XDB
::execute('UPDATE log_last_sessions
822 $lastSession, $newuser->id());
823 XDB
::execute('DELETE FROM accounts
830 // Return permission flags for a given permission level.
831 public static function makePerms($perms, $is_admin)
833 $flags = new PlFlagSet($perms);
834 $flags->addFlag(PERMS_USER
);
836 $flags->addFlag(PERMS_ADMIN
);
839 // Access to private directory implies access to 'less'-private version.
840 if ($flags->hasFlag('directory_private')) {
841 $flags->addFlag('directory_ax');
846 // Implementation of the default user callback.
847 public static function _default_user_callback($login, $results)
849 $result_count = count($results);
850 if ($result_count == 0 ||
!S
::admin()) {
851 Platal
::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login");
853 Platal
::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results));
857 public static function makeHomonymHrmid($alias)
859 return 'h.' . $alias . '.' . Platal
::globals()->mail
->domain
;
862 public static function isMainMailDomain($domain)
866 $is_main_domain = false
;
867 foreach (self
::$sub_mail_domains as $sub_domain) {
868 $is_main_domain = $is_main_domain ||
$domain == ($sub_domain . $globals->mail
->domain
) ||
$domain == ($sub_domain . $globals->mail
->domain2
);
870 return $is_main_domain;
873 public static function isAliasMailDomain($domain)
877 return $domain == $globals->mail
->alias_dom ||
$domain == $globals->mail
->alias_dom2
;
880 // Implementation of the static email locality checker.
881 public static function isForeignEmailAddress($email)
883 if (strpos($email, '@') === false
) {
887 list(, $domain) = explode('@', $email);
888 return !(self
::isMainMailDomain($domain) || self
::isAliasMailDomain($domain));
891 /* Tries to find pending accounts with an hruid close to $login. */
892 public static function getPendingAccounts($login, $iterator = false
)
894 if (strpos($login, '@') === false
) {
898 list($login, $domain) = explode('@', $login);
900 if ($domain && !self
::isMainMailDomain($domain)) {
904 $sql = "SELECT uid, full_name
906 WHERE state = 'pending' AND REPLACE(hruid, '-', '') LIKE
907 CONCAT('%', REPLACE(REPLACE(REPLACE({?}, ' ', ''), '-', ''), '\'', ''), '%')
910 return XDB
::iterator($sql, $login);
912 $res = XDB
::query($sql, $login);
913 return $res->fetchAllAssoc();
918 public static function iterOverUIDs($uids, $respect_order = true
)
920 return new UserIterator(self
::loadMainFieldsFromUIDs($uids, $respect_order));
923 /** Fetch a set of users from a list of UIDs
924 * @param $data The list of uids to fetch, or an array of arrays
925 * @param $orig If $data is an array of arrays, the subfield where uids are stored
926 * @param $dest If $data is an array of arrays, the subfield to fill with Users
927 * @param $fetchProfile Whether to fetch Profiles as well
928 * @return either an array of $uid => User, or $data with $data[$i][$dest] = User
930 public static function getBulkUsersWithUIDs(array $data, $orig = null
, $dest = null
, $fetchProfile = true
)
932 // Fetch the list of uids
933 if (is_null($orig)) {
936 if (is_null($dest)) {
940 foreach ($data as $key=>$entry) {
941 if (isset($entry[$orig])) {
942 $uids[] = $entry[$orig];
948 if (count($uids) == 0) {
951 $users = self
::iterOverUIDs($uids, true
);
955 $profiles = Profile
::iterOverUIDS($uids, true
);
956 if ($profiles != null
) {
957 $profile = $profiles->next();
963 /** We iterate through the users, moving in
964 * profiles when they match the user ID :
965 * there can be users without a profile, but not
966 * the other way around.
968 while (($user = $users->next())) {
970 if ($profile != null
&& $profile->owner_id
== $user->id()) {
971 $user->_profile
= $profile;
972 $profile = $profiles->next();
974 $user->_profile_fetched
= true
;
976 $table[$user->id()] = $user;
979 // Build the result with respect to input order.
980 if (is_null($orig)) {
983 foreach ($data as $key=>$entry) {
984 if (isset($entry[$orig])) {
985 $entry[$dest] = $table[$entry[$orig]];
986 $data[$key] = $entry;
993 public static function getBulkUsersFromDB($fetchProfile = true
)
995 $args = func_get_args();
996 $uids = call_user_func_array(array('XDB', 'fetchColumn'), $args);
997 return self
::getBulkUsersWithUIDs($uids, null
, null
, $fetchProfile);
1001 /** Iterator over a set of Users
1002 * @param an XDB::Iterator obtained from a User::loadMainFieldsFromUIDs
1004 class UserIterator
implements PlIterator
1008 public function __construct($dbiter)
1010 $this->dbiter
= $dbiter;
1013 public function next()
1015 $data = $this->dbiter
->next();
1016 if ($data == null
) {
1019 return User
::getSilentWithValues(null
, $data);
1023 public function total()
1025 return $this->dbiter
->total();
1028 public function first()
1030 return $this->dbiter
->first();
1033 public function last()
1035 return $this->dbiter
->last();
1039 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: