Commit | Line | Data |
---|---|---|
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 | 22 | class 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 FB |
141 | a.full_name, a.display_name, a.sex = 'female' AS gender, |
142 | a.email_format, a.state AS perms | |
143 | FROM accounts AS a | |
144 | INNER JOIN profile_display AS d ON (d.uid = a.uid) | |
145 | LEFT JOIN aliases AS af ON (af.id = a.uid AND af.type = 'a_vie') | |
146 | LEFT JOIN aliases AS ab ON (ab.id = a.uid AND FIND_IN_SET('bestalias', ab.flags)) | |
147 | WHERE a.uid = {?}", $this->user_id); | |
70232020 VZ |
148 | $this->fillFromArray($res->fetchOneAssoc()); |
149 | } | |
150 | ||
151 | // Specialization of the fillFromArray method, to implement hacks to enable | |
152 | // lazy loading of user's main properties from the session. | |
c4012d9b VZ |
153 | // TODO(vzanotti): remove the conversion hacks once the old codebase will |
154 | // stop being used actively. | |
70232020 VZ |
155 | protected function fillFromArray(array $values) |
156 | { | |
157 | // It might happen that the 'user_id' field is called uid in some places | |
158 | // (eg. in sessions), so we hard link uid to user_id to prevent useless | |
159 | // SQL requests. | |
160 | if (!isset($values['user_id']) && isset($values['uid'])) { | |
161 | $values['user_id'] = $values['uid']; | |
162 | } | |
163 | ||
164 | // Also, if display_name and full_name are not known, but the user's | |
165 | // surname and last name are, we can construct the former two. | |
166 | if (isset($values['prenom']) && isset($values['nom'])) { | |
167 | if (!isset($values['display_name'])) { | |
168 | $values['display_name'] = ($values['prenom'] ? $values['prenom'] : $values['nom']); | |
169 | } | |
170 | if (!isset($values['full_name'])) { | |
171 | $values['full_name'] = $values['prenom'] . ' ' . $values['nom']; | |
172 | } | |
173 | } | |
174 | ||
c4012d9b VZ |
175 | // We also need to convert the gender (usually named "femme"), and the |
176 | // email format parameter (valued "texte" instead of "text"). | |
177 | if (isset($values['femme'])) { | |
178 | $values['gender'] = (bool) $values['femme']; | |
179 | } | |
180 | if (isset($values['mail_fmt'])) { | |
181 | $values['email_format'] = $values['mail_fmt']; | |
182 | } | |
c4012d9b | 183 | |
70232020 VZ |
184 | parent::fillFromArray($values); |
185 | } | |
186 | ||
50d5ec0b FB |
187 | // Specialization of the buildPerms method |
188 | // This function build 'generic' permissions for the user. It does not take | |
189 | // into account page specific permissions (e.g X.net group permissions) | |
190 | protected function buildPerms() | |
191 | { | |
192 | if (!is_null($this->perm_flags)) { | |
193 | return; | |
194 | } | |
195 | if ($this->perms === null) { | |
196 | $this->loadMainFields(); | |
197 | } | |
198 | $this->perm_flags = self::makePerms($this->perms); | |
199 | } | |
200 | ||
e7b93962 FB |
201 | /** Return the main profile attached with this account if any. |
202 | */ | |
203 | public function profile() | |
204 | { | |
205 | return Profile::get($this); | |
206 | } | |
207 | ||
50d5ec0b FB |
208 | // Return permission flags for a given permission level. |
209 | public static function makePerms($perms) | |
210 | { | |
211 | $flags = new PlFlagSet(); | |
212 | if (is_null($flags) || $perms == 'disabled' || $perms == 'ext') { | |
213 | return $flags; | |
214 | } | |
215 | $flags->addFlag(PERMS_USER); | |
216 | if ($perms == 'admin') { | |
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 | ?> |