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