2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 private $_profile_fetched = false
;
25 private $_profile = null
;
27 // Implementation of the login to uid method.
28 protected function getLogin($login)
32 if ($login instanceof User
) {
36 if ($login instanceof Profile
) {
37 $this->_profile
= $login;
38 $this->_profile_fetched
= true
;
39 $res = XDB
::query('SELECT ap.uid
40 FROM account_profiles AS ap
41 WHERE ap.pid = {?} AND FIND_IN_SET(\'owner\', perms)',
43 if ($res->numRows()) {
44 return $res->fetchOneCell();
46 throw new UserNotFoundException();
49 // If $data is an integer, fetches directly the result.
50 if (is_numeric($login)) {
51 $res = XDB
::query('SELECT a.uid
53 WHERE a.uid = {?}', $login);
54 if ($res->numRows()) {
55 return $res->fetchOneCell();
58 throw new UserNotFoundException();
61 // Checks whether $login is a valid hruid or not.
62 $res = XDB
::query('SELECT a.uid
64 WHERE a.hruid = {?}', $login);
65 if ($res->numRows()) {
66 return $res->fetchOneCell();
69 // From now, $login can only by an email alias, or an email redirection.
70 // If it doesn't look like a valid address, appends the plat/al's main domain.
71 $login = trim(strtolower($login));
72 if (strstr($login, '@') === false
) {
73 $login = $login . '@' . $globals->mail
->domain
;
76 // Checks if $login is a valid alias on the main domains.
77 list($mbox, $fqdn) = explode('@', $login);
78 if ($fqdn == $globals->mail
->domain ||
$fqdn == $globals->mail
->domain2
) {
79 $res = XDB
::query('SELECT a.uid
81 INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN (\'alias\', \'a_vie\'))
82 WHERE al.alias = {?}', $mbox);
83 if ($res->numRows()) {
84 return $res->fetchOneCell();
87 /** TODO: implements this by inspecting the profile.
88 if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) {
89 $res = XDB::query('SELECT a.uid
91 INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie'))
92 WHERE al.alias = {?} AND a.promo = {?}', $matches[1], $matches[2]);
93 if ($res->numRows() == 1) {
94 return $res->fetchOneCell();
98 throw new UserNotFoundException();
101 // Looks for $login as an email alias from the dedicated alias domain.
102 if ($fqdn == $globals->mail
->alias_dom ||
$fqdn == $globals->mail
->alias_dom2
) {
103 $res = XDB
::query("SELECT redirect
104 FROM virtual_redirect
105 INNER JOIN virtual USING(vid)
106 WHERE alias = {?}", $mbox . '@' . $globals->mail
->alias_dom
);
107 if ($redir = $res->fetchOneCell()) {
108 // We now have a valid alias, which has to be translated to an hruid.
109 list($alias, $alias_fqdn) = explode('@', $redir);
110 $res = XDB
::query("SELECT a.uid
112 LEFT JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie'))
113 WHERE al.alias = {?}", $alias);
114 if ($res->numRows()) {
115 return $res->fetchOneCell();
119 throw new UserNotFoundException();
122 // Looks for an account with the given email.
123 $res = XDB
::query('SELECT a.uid
125 WHERE a.email = {?}', $login);
126 if ($res->numRows() == 1) {
127 return $res->fetchOneCell();
130 // Otherwise, we do suppose $login is an email redirection.
131 $res = XDB
::query("SELECT a.uid
133 LEFT JOIN emails AS e ON (e.uid = a.uid)
134 WHERE e.email = {?}", $login);
135 if ($res->numRows() == 1) {
136 return $res->fetchOneCell();
139 throw new UserNotFoundException($res->fetchColumn(1));
142 protected static function loadMainFieldsFromUIDs(array $uids, $sorted = null
)
148 if (!is_null($sorted)) {
152 foreach (explode(',', $sorted) as $part) {
153 $desc = ($part[0] == '-');
155 $part = substr($desc, 1);
161 $part = 'IF (pd.promo IS NULL, \'ext\', pd.promo)';
164 $part = 'a.full_name';
167 $part = 'a.display_name';
172 if (!is_null($part)) {
179 if (count($order) > 0) {
181 $joins .= "LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))\n";
184 $joins .= "LEFT JOIN profile_display AS pd ON (pd.pid = ap.pid)\n";
186 $orderby = 'ORDER BY ' . implode(', ', $order);
189 if ($globals->asso('id')) {
190 $joins .= XDB
::format("LEFT JOIN groupex.membres AS gpm ON (gpm.uid = a.uid AND gpm.asso_id = {?})\n", $globals->asso('id'));
191 $fields[] = 'gpm.perms AS group_perms';
192 $fields[] = 'gpm.comm AS group_comm';
194 if (count($fields) > 0) {
195 $fields = ', ' . implode(', ', $fields);
199 $uids = array_map(array('XDB', 'escape'), $uids);
200 return XDB
::iterator('SELECT a.uid, a.hruid, a.registration_date,
201 CONCAT(af.alias, \'@' . $globals->mail
->domain
. '\') AS forlife,
202 CONCAT(ab.alias, \'@' . $globals->mail
->domain
. '\') AS bestalias,
203 a.full_name, a.display_name, a.sex = \'female\' AS gender,
204 IF(a.state = \'active\', at.perms, \'\') AS perms,
205 a.email_format, a.is_admin, a.state, a.type, a.skin,
206 FIND_IN_SET(\'watch\', a.flags) AS watch, a.comment,
207 a.weak_password IS NOT NULL AS weak_access,
208 a.token IS NOT NULL AS token_access ' . $fields . '
210 INNER JOIN account_types AS at ON (at.type = a.type)
211 LEFT JOIN aliases AS af ON (af.id = a.uid AND af.type = \'a_vie\')
212 LEFT JOIN aliases AS ab ON (ab.id = a.uid AND FIND_IN_SET(\'bestalias\', ab.flags))
214 WHERE a.uid IN (' . implode(', ', $uids) . ')
218 // Implementation of the data loader.
219 protected function loadMainFields()
221 if ($this->hruid
!== null
&& $this->forlife
!== null
222 && $this->bestalias
!== null
&& $this->display_name
!== null
223 && $this->full_name
!== null
&& $this->perms
!== null
224 && $this->gender
!== null
&& $this->email_format
!== null
) {
227 $this->fillFromArray(self
::loadMainFieldsFromUIDs(array($this->user_id
))->next());
230 // Specialization of the fillFromArray method, to implement hacks to enable
231 // lazy loading of user's main properties from the session.
232 // TODO(vzanotti): remove the conversion hacks once the old codebase will
233 // stop being used actively.
234 protected function fillFromArray(array $values)
236 // It might happen that the 'user_id' field is called uid in some places
237 // (eg. in sessions), so we hard link uid to user_id to prevent useless
239 if (!isset($values['user_id']) && isset($values['uid'])) {
240 $values['user_id'] = $values['uid'];
243 // Also, if display_name and full_name are not known, but the user's
244 // surname and last name are, we can construct the former two.
245 if (isset($values['prenom']) && isset($values['nom'])) {
246 if (!isset($values['display_name'])) {
247 $values['display_name'] = ($values['prenom'] ?
$values['prenom'] : $values['nom']);
249 if (!isset($values['full_name'])) {
250 $values['full_name'] = $values['prenom'] . ' ' . $values['nom'];
254 // We also need to convert the gender (usually named "femme"), and the
255 // email format parameter (valued "texte" instead of "text").
256 if (isset($values['femme'])) {
257 $values['gender'] = (bool
) $values['femme'];
259 if (isset($values['mail_fmt'])) {
260 $values['email_format'] = $values['mail_fmt'];
263 parent
::fillFromArray($values);
266 // Specialization of the buildPerms method
267 // This function build 'generic' permissions for the user. It does not take
268 // into account page specific permissions (e.g X.net group permissions)
269 protected function buildPerms()
271 if (!is_null($this->perm_flags
)) {
274 if ($this->perms
=== null
) {
275 $this->loadMainFields();
277 $this->perm_flags
= self
::makePerms($this->perms
, $this->is_admin
);
280 // We do not want to store the password in the object.
281 // So, fetch it 'on demand'
282 public function password()
284 return XDB
::fetchOneCell('SELECT a.password
286 WHERE a.uid = {?}', $this->id());
289 /** Overload PlUser::promo(): there no promo defined for a user in the current
290 * schema. The promo is a field from the profile.
292 public function promo()
294 if (!$this->hasProfile()) {
297 return $this->profile()->promo();
300 public function firstName()
302 if (!$this->hasProfile()) {
303 return $this->displayName();
305 return $this->profile()->firstName();
308 public function lastName()
310 if (!$this->hasProfile()) {
313 return $this->profile()->lastName();
316 /** Return the main profile attached with this account if any.
318 public function profile()
320 if (!$this->_profile_fetched
) {
321 $this->_profile_fetched
= true
;
322 $this->_profile
= Profile
::get($this);
324 return $this->_profile
;
327 /** Return true if the user has an associated profile.
329 public function hasProfile()
331 return !is_null($this->profile());
334 /** Check if the user can edit to given profile.
336 public function canEdit(Profile
$profile)
338 // XXX: Check permissions (e.g. secretary permission)
339 // and flags from the profile
340 return XDB
::fetchOneCell('SELECT pid
341 FROM account_profiles
342 WHERE uid = {?} AND pid = {?}',
343 $this->id(), $profile->id());
346 /** Get the email alias of the user.
348 public function emailAlias()
351 $data = $this->emailAliases($globals->mail
->alias_dom
);
352 if (count($data) > 0) {
353 return array_pop($data);
358 /** Get all the aliases the user belongs to.
360 public function emailAliases($domain = null
, $type = 'user', $sub_state = false
)
362 $join = XDB
::format('(vr.redirect = {?} OR vr.redirect = {?}) ',
363 $this->forlifeEmail(), $this->m4xForlifeEmail());
365 if (!is_null($domain)) {
366 $where = XDB
::format('WHERE v.alias LIKE CONCAT("%@", {?})', $domain);
368 if (!is_null($type)) {
370 $where = XDB
::format('WHERE v.type = {?}', $type);
372 $where .= XDB
::format(' AND v.type = {?}', $type);
376 return XDB
::fetchAllAssoc('alias', 'SELECT v.alias, vr.redirect IS NOT NULL AS sub
378 LEFT JOIN virtual_redirect AS vr ON (v.vid = vr.vid AND ' . $join . ')
381 return XDB
::fetchColumn('SELECT v.alias
383 INNER JOIN virtual_redirect AS vr ON (v.vid = vr.vid AND ' . $join . ')
388 /** Get the alternative forlife email
389 * TODO: remove this uber-ugly hack. The issue is that you need to remove
390 * all @m4x.org addresses in virtual_redirect first.
391 * XXX: This is juste to make code more readable, to be remove as soon as possible
393 public function m4xForlifeEmail()
396 trigger_error('USING M4X FORLIFE', E_USER_NOTICE
);
397 return $this->login() . '@' . $globals->mail
->domain2
;
400 // Return permission flags for a given permission level.
401 public static function makePerms($perms, $is_admin)
403 $flags = new PlFlagSet($perms);
404 $flags->addFlag(PERMS_USER
);
406 $flags->addFlag(PERMS_ADMIN
);
411 // Implementation of the default user callback.
412 public static function _default_user_callback($login, $results)
414 $result_count = count($results);
415 if ($result_count == 0 ||
!S
::has_perms()) {
416 Platal
::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login");
418 Platal
::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results));
422 // Implementation of the static email locality checker.
423 public static function isForeignEmailAddress($email)
426 if (strpos($email, '@') === false
) {
430 list($user, $dom) = explode('@', $email);
431 return $dom != $globals->mail
->domain
&&
432 $dom != $globals->mail
->domain2
&&
433 $dom != $globals->mail
->alias_dom
&&
434 $dom != $globals->mail
->alias_dom2
;
437 // Fetch a set of users from a list of UIDs
438 public static function getBuildUsersWithUIDs(array $uids, $sortby = null
)
440 $fields = self
::loadMainFieldsFromUIDs($uids, $sortby);
442 while (($list = $fields->next())) {
443 $users[] = User
::getSilentWithValues(null
, $list);
449 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: