Import singletons from auth_user_quick.
[platal.git] / classes / user.php
CommitLineData
9f8ebb9f
VZ
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
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{
70232020 24 // Implementation of the login to uid method.
b1719b13
VZ
25 protected function getLogin($login)
26 {
27 global $globals;
28
e7b93962
FB
29 if ($login instanceof Profile) {
30 $res = XDB::query('SELECT ap.uid
31 FROM account_profiles AS ap
32 WHERE ap.pid = {?} AND FIND_IN_SET(\'owner\', perms)',
33 $login->id());
34 if ($res->numRows()) {
35 return $res->fetchOneCell();
36 }
37 throw new UserNotFoundException();
38 }
39
b1719b13
VZ
40 // If $data is an integer, fetches directly the result.
41 if (is_numeric($login)) {
e7b93962
FB
42 $res = XDB::query('SELECT a.uid
43 FROM accounts AS a
44 WHERE a.uid = {?}', $login);
b1719b13 45 if ($res->numRows()) {
70232020 46 return $res->fetchOneCell();
b1719b13
VZ
47 }
48
49 throw new UserNotFoundException();
50 }
51
52 // Checks whether $login is a valid hruid or not.
e7b93962
FB
53 $res = XDB::query('SELECT a.uid
54 FROM accounts AS a
55 WHERE a.hruid = {?}', $login);
b1719b13 56 if ($res->numRows()) {
70232020 57 return $res->fetchOneCell();
b1719b13
VZ
58 }
59
60 // From now, $login can only by an email alias, or an email redirection.
61 // If it doesn't look like a valid address, appends the plat/al's main domain.
62 $login = trim(strtolower($login));
63 if (strstr($login, '@') === false) {
64 $login = $login . '@' . $globals->mail->domain;
65 }
66
67 // Checks if $login is a valid alias on the main domains.
68 list($mbox, $fqdn) = explode('@', $login);
69 if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) {
e7b93962
FB
70 $res = XDB::query('SELECT a.uid
71 FROM accounts AS a
72 INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN (\'alias\', \'a_vie\'))
73 WHERE al.alias = {?}', $mbox);
b1719b13 74 if ($res->numRows()) {
70232020 75 return $res->fetchOneCell();
b1719b13
VZ
76 }
77
e7b93962 78 /** TODO: implements this by inspecting the profile.
b1719b13 79 if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) {
e7b93962
FB
80 $res = XDB::query('SELECT a.uid
81 FROM accounts AS a
82 INNER JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie'))
83 WHERE al.alias = {?} AND a.promo = {?}', $matches[1], $matches[2]);
b1719b13 84 if ($res->numRows() == 1) {
70232020 85 return $res->fetchOneCell();
b1719b13 86 }
e7b93962 87 }*/
b1719b13
VZ
88
89 throw new UserNotFoundException();
90 }
91
92 // Looks for $login as an email alias from the dedicated alias domain.
93 if ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) {
94 $res = XDB::query("SELECT redirect
95 FROM virtual_redirect
96 INNER JOIN virtual USING(vid)
97 WHERE alias = {?}", $mbox . '@' . $globals->mail->alias_dom);
98 if ($redir = $res->fetchOneCell()) {
99 // We now have a valid alias, which has to be translated to an hruid.
100 list($alias, $alias_fqdn) = explode('@', $redir);
e7b93962
FB
101 $res = XDB::query("SELECT a.uid
102 FROM accounts AS a
103 LEFT JOIN aliases AS al ON (al.id = a.uid AND al.type IN ('alias', 'a_vie'))
104 WHERE al.alias = {?}", $alias);
b1719b13 105 if ($res->numRows()) {
70232020 106 return $res->fetchOneCell();
b1719b13
VZ
107 }
108 }
109
110 throw new UserNotFoundException();
111 }
112
113 // Otherwise, we do suppose $login is an email redirection.
e7b93962
FB
114 $res = XDB::query("SELECT a.uid
115 FROM accounts AS a
116 LEFT JOIN emails AS e ON (e.uid = a.uid)
b1719b13
VZ
117 WHERE e.email = {?}", $login);
118 if ($res->numRows() == 1) {
70232020 119 return $res->fetchOneCell();
b1719b13
VZ
120 }
121
122 throw new UserNotFoundException($res->fetchColumn(1));
123 }
124
70232020
VZ
125 // Implementation of the data loader.
126 protected function loadMainFields()
127 {
c4012d9b
VZ
128 if ($this->hruid !== null && $this->forlife !== null
129 && $this->bestalias !== null && $this->display_name !== null
130 && $this->full_name !== null && $this->promo !== null && $this->perms !== null
131 && $this->gender !== null && $this->email_format !== null) {
70232020
VZ
132 return;
133 }
134
135 global $globals;
e7b93962
FB
136 /** TODO: promo stuff again */
137 /** TODO: fix perms field to fit new perms system */
138 $res = XDB::query("SELECT a.hruid, d.promo_display AS promo,
70232020
VZ
139 CONCAT(af.alias, '@{$globals->mail->domain}') AS forlife,
140 CONCAT(ab.alias, '@{$globals->mail->domain}') AS bestalias,
e7b93962 141 a.full_name, a.display_name, a.sex = 'female' AS gender,
8c12f931 142 a.email_format, a.password,
365ba8c3
FB
143 IF (a.state = 'active', at.perms, '') AS perms,
144 a.is_admin
e7b93962 145 FROM accounts AS a
365ba8c3 146 INNER JOIN account_types AS at ON (at.type = a.type)
e7b93962
FB
147 INNER JOIN profile_display AS d ON (d.uid = a.uid)
148 LEFT JOIN aliases AS af ON (af.id = a.uid AND af.type = 'a_vie')
149 LEFT JOIN aliases AS ab ON (ab.id = a.uid AND FIND_IN_SET('bestalias', ab.flags))
150 WHERE a.uid = {?}", $this->user_id);
70232020
VZ
151 $this->fillFromArray($res->fetchOneAssoc());
152 }
153
154 // Specialization of the fillFromArray method, to implement hacks to enable
155 // lazy loading of user's main properties from the session.
c4012d9b
VZ
156 // TODO(vzanotti): remove the conversion hacks once the old codebase will
157 // stop being used actively.
70232020
VZ
158 protected function fillFromArray(array $values)
159 {
160 // It might happen that the 'user_id' field is called uid in some places
161 // (eg. in sessions), so we hard link uid to user_id to prevent useless
162 // SQL requests.
163 if (!isset($values['user_id']) && isset($values['uid'])) {
164 $values['user_id'] = $values['uid'];
165 }
166
167 // Also, if display_name and full_name are not known, but the user's
168 // surname and last name are, we can construct the former two.
169 if (isset($values['prenom']) && isset($values['nom'])) {
170 if (!isset($values['display_name'])) {
171 $values['display_name'] = ($values['prenom'] ? $values['prenom'] : $values['nom']);
172 }
173 if (!isset($values['full_name'])) {
174 $values['full_name'] = $values['prenom'] . ' ' . $values['nom'];
175 }
176 }
177
c4012d9b
VZ
178 // We also need to convert the gender (usually named "femme"), and the
179 // email format parameter (valued "texte" instead of "text").
180 if (isset($values['femme'])) {
181 $values['gender'] = (bool) $values['femme'];
182 }
183 if (isset($values['mail_fmt'])) {
184 $values['email_format'] = $values['mail_fmt'];
185 }
c4012d9b 186
70232020
VZ
187 parent::fillFromArray($values);
188 }
189
50d5ec0b
FB
190 // Specialization of the buildPerms method
191 // This function build 'generic' permissions for the user. It does not take
192 // into account page specific permissions (e.g X.net group permissions)
193 protected function buildPerms()
194 {
195 if (!is_null($this->perm_flags)) {
196 return;
197 }
198 if ($this->perms === null) {
199 $this->loadMainFields();
200 }
365ba8c3 201 $this->perm_flags = self::makePerms($this->perms, $this->is_admin);
50d5ec0b
FB
202 }
203
e7b93962
FB
204 /** Return the main profile attached with this account if any.
205 */
206 public function profile()
207 {
208 return Profile::get($this);
209 }
210
50d5ec0b 211 // Return permission flags for a given permission level.
365ba8c3 212 public static function makePerms($perms, $is_admin)
50d5ec0b 213 {
365ba8c3 214 $flags = new PlFlagSet($perms);
50d5ec0b 215 $flags->addFlag(PERMS_USER);
365ba8c3 216 if ($is_admin) {
50d5ec0b
FB
217 $flags->addFlag(PERMS_ADMIN);
218 }
219 return $flags;
220 }
221
b1719b13
VZ
222 // Implementation of the default user callback.
223 public static function _default_user_callback($login, $results)
224 {
b1719b13
VZ
225 $result_count = count($results);
226 if ($result_count == 0 || !S::has_perms()) {
70232020 227 Platal::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login");
b1719b13 228 } else {
70232020 229 Platal::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results));
b1719b13
VZ
230 }
231 }
70232020
VZ
232
233 // Implementation of the static email locality checker.
234 public static function isForeignEmailAddress($email)
235 {
236 global $globals;
237 if (strpos($email, '@') === false) {
238 return false;
239 }
240
241 list($user, $dom) = explode('@', $email);
242 return $dom != $globals->mail->domain &&
243 $dom != $globals->mail->domain2 &&
244 $dom != $globals->mail->alias_dom &&
245 $dom != $globals->mail->alias_dom2;
246 }
9f8ebb9f
VZ
247}
248
249// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
250?>