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