Fix address<->phone association on profile.
[platal.git] / classes / xorgsession.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 Polytechnique.org *
0337d704 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
c0799142 22class XorgSession extends PlSession
0337d704 23{
45dd32db
FB
24 const INVALID_USER = -2;
25 const NO_COOKIE = -1;
26 const COOKIE_SUCCESS = 0;
27 const INVALID_COOKIE = 1;
28
c0799142
FB
29 public function __construct()
30 {
31 parent::__construct();
abde67b1
FB
32 }
33
c0799142 34 public function startAvailableAuth()
5480a216 35 {
c0799142 36 if (!S::logged()) {
45dd32db
FB
37 switch ($this->tryCookie()) {
38 case self::COOKIE_SUCCESS:
39 if (!$this->start(AUTH_COOKIE)) {
40 return false;
41 }
42 break;
43
44 case self::INVALID_USER:
45 case self::INVALID_COOKIE:
c0799142
FB
46 return false;
47 }
5480a216 48 }
0be07aa6 49 if ((check_ip('dangerous') && S::has('uid')) || check_account()) {
9d214e8e 50 S::logger()->log("view_page", $_SERVER['REQUEST_URI']);
0337d704 51 }
c0799142 52 return true;
0337d704 53 }
54
1bf36cd1 55 /** Check the cookie and set the associated uid in the auth_by_cookie session variable.
c0799142
FB
56 */
57 private function tryCookie()
58 {
59 S::kill('auth_by_cookie');
40565fa2 60 if (Cookie::v('access') == '' || !Cookie::has('uid')) {
45dd32db 61 return self::NO_COOKIE;
c0799142 62 }
cab08090 63
c67ba12a
FB
64 $res = XDB::query('SELECT uid, password
65 FROM accounts
66 WHERE uid = {?} AND state = \'active\'',
8cd3dccc 67 Cookie::i('uid'));
c0799142
FB
68 if ($res->numRows() != 0) {
69 list($uid, $password) = $res->fetchOneRow();
45dd32db 70 if (sha1($password) == Cookie::v('access')) {
c0799142 71 S::set('auth_by_cookie', $uid);
45dd32db 72 return self::COOKIE_SUCCESS;
c0799142 73 } else {
45dd32db 74 return self::INVALID_COOKIE;
c0799142
FB
75 }
76 }
45dd32db 77 return self::INVALID_USER;
c0799142
FB
78 }
79
80 private function checkPassword($uname, $login, $response, $login_type)
5480a216 81 {
675f3174
FB
82 if ($login_type == 'alias') {
83 $res = XDB::query('SELECT a.uid, a.password
84 FROM accounts AS a
85 INNER JOIN aliases AS l ON (l.uid = a.uid AND l.type != \'homonyme\')
86 WHERE l.alias = {?} AND a.state = \'active\'',
87 $login);
88 } else {
89 $res = XDB::query('SELECT uid, password
90 FROM accounts
91 WHERE ' . $login_type . ' = {?}',
92 $login);
93 }
c0799142 94 if (list($uid, $password) = $res->fetchOneRow()) {
c67ba12a 95 $expected_response = sha1("$uname:$password:" . S::v('challenge'));
7a12b2ca 96 /* Deprecates len(password) > 10 conversion. */
c0799142 97 if ($response != $expected_response) {
86777849
FB
98 if (!S::logged()) {
99 Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide');
100 } else {
101 Platal::page()->trigError('Mot de passe invalide');
102 }
732e5855 103 S::logger($uid)->log('auth_fail', 'bad password');
c0799142
FB
104 return null;
105 }
106 return $uid;
107 }
c6bad0e7 108 Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide');
c0799142 109 return null;
0337d704 110 }
cab08090 111
0337d704 112
c0799142
FB
113 /** Check auth.
114 */
115 protected function doAuth($level)
0337d704 116 {
e74411f7 117 global $globals;
c0799142
FB
118
119 /* Cookie authentication
120 */
121 if ($level == AUTH_COOKIE && !S::has('auth_by_cookie')) {
122 $this->tryCookie();
123 }
124 if ($level == AUTH_COOKIE && S::has('auth_by_cookie')) {
125 if (!S::logged()) {
126 S::set('auth', AUTH_COOKIE);
127 }
19fdac5d 128 return User::getSilentWithUID(S::i('auth_by_cookie'));
e74411f7 129 }
0337d704 130
c0799142
FB
131
132 /* We want to do auth... we must have infos from a form.
133 */
e64c8b61 134 if (!Post::has('username') || !Post::has('response') || !S::has('challenge')) {
c0799142 135 return null;
63528107 136 }
137
c0799142
FB
138 /** We come from an authentication form.
139 */
0c02607e
FB
140 if (S::suid()) {
141 $login = $uname = S::suid('uid');
675f3174 142 $loginType = 'uid';
e74411f7 143 $redirect = false;
144 } else {
675f3174
FB
145 $uname = Post::v('username');
146 if (Post::s('domain') == "alias") {
c0799142
FB
147 $res = XDB::query('SELECT redirect
148 FROM virtual
149 INNER JOIN virtual_redirect USING(vid)
150 WHERE alias LIKE {?}',
151 $uname . '@' . $globals->mail->alias_dom);
e74411f7 152 $redirect = $res->fetchOneCell();
153 if ($redirect) {
154 $login = substr($redirect, 0, strpos($redirect, '@'));
155 } else {
c0799142 156 $login = '';
e74411f7 157 }
675f3174
FB
158 $loginType = 'alias';
159 } else if (Post::s('domain') == "ax") {
160 $login = $uname;
161 $redirect = false;
162 $loginType = 'hruid';
0337d704 163 } else {
e74411f7 164 $login = $uname;
165 $redirect = false;
20edd399 166 $loginType = is_numeric($uname) ? 'uid' : 'alias';
0337d704 167 }
63528107 168 }
cab08090 169
675f3174 170 $uid = $this->checkPassword($uname, $login, Post::v('response'), $loginType);
0c02607e
FB
171 if (!is_null($uid) && S::suid()) {
172 if (S::suid('uid') == $uid) {
888465dd
FB
173 $uid = S::i('uid');
174 } else {
175 $uid = null;
176 }
177 }
c0799142
FB
178 if (!is_null($uid)) {
179 S::set('auth', AUTH_MDP);
0c02607e 180 if (!S::suid()) {
888465dd 181 if (Post::has('domain')) {
675f3174
FB
182 $domain = Post::v('domain', 'login');
183 if ($domain == 'alias') {
40565fa2 184 Cookie::set('domain', 'alias', 300);
675f3174
FB
185 } else if ($domain == 'ax') {
186 Cookie::set('domain', 'ax', 300);
888465dd 187 } else {
40565fa2 188 Cookie::kill('domain');
888465dd 189 }
e74411f7 190 }
191 }
c0799142 192 S::kill('challenge');
732e5855 193 S::logger($uid)->log('auth_ok');
c0799142 194 }
19fdac5d 195 return User::getSilentWithUID($uid);
c0799142 196 }
cab08090 197
e2cb093d 198 protected function startSessionAs($user, $level)
c0799142 199 {
b281eb7b 200 if ((!is_null(S::user()) && S::user()->id() != $user->id())
e2cb093d 201 || (S::has('uid') && S::i('uid') != $user->id())) {
c0799142
FB
202 return false;
203 } else if (S::has('uid')) {
204 return true;
205 }
888465dd
FB
206 if ($level == AUTH_SUID) {
207 S::set('auth', AUTH_MDP);
c0799142 208 }
70232020 209
2ab3486b 210 // Loads uid and hruid into the session for developement conveniance.
f98f5111 211 $_SESSION = array_merge($_SESSION, array('uid' => $user->id(), 'hruid' => $user->hruid, 'token' => $user->token));
90c33700 212 unset($_SESSION['perms']);
6672b29b
VZ
213
214 // Starts the session's logger, and sets up the permanent cookie.
0c02607e
FB
215 if (S::suid()) {
216 S::logger()->log("suid_start", S::v('hruid') . ' by ' . S::suid('hruid'));
c0799142 217 } else {
e2cb093d
FB
218 S::logger()->saveLastSession();
219 Cookie::set('uid', $user->id(), 300);
a90cde48 220
e2cb093d
FB
221 if (S::i('auth_by_cookie') == $user->id() || Post::v('remember', 'false') == 'true') {
222 $this->setAccessCookie(false, S::i('auth_by_cookie') != $user->id());
c0799142 223 } else {
604dfd58 224 $this->killAccessCookie();
0337d704 225 }
63528107 226 }
b0b937fd 227
6672b29b 228 // Finalizes the session setup.
2ab3486b 229 $this->makePerms($user->perms, $user->is_admin);
c0799142
FB
230 $this->securityChecks();
231 $this->setSkin();
ebfdf077 232 $this->updateNbNotifs();
c0799142 233 check_redirect();
d0621f88
FB
234
235 // We should not have to use this private data anymore
236 S::kill('auth_by_cookie');
c0799142 237 return true;
0337d704 238 }
239
c0799142 240 private function securityChecks()
0337d704 241 {
c0799142
FB
242 $mail_subject = array();
243 if (check_account()) {
244 $mail_subject[] = 'Connexion d\'un utilisateur surveillé';
0337d704 245 }
c0799142
FB
246 if (check_ip('unsafe')) {
247 $mail_subject[] = 'Une IP surveillee a tente de se connecter';
248 if (check_ip('ban')) {
249 send_warning_mail(implode(' - ', $mail_subject));
250 $this->destroy();
251 Platal::page()->kill('Une erreur est survenue lors de la procédure d\'authentification. '
252 . 'Merci de contacter au plus vite '
253 . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>');
254 return false;
255 }
0337d704 256 }
c0799142
FB
257 if (count($mail_subject)) {
258 send_warning_mail(implode(' - ', $mail_subject));
0337d704 259 }
260 }
261
0d44ce42
FB
262 public function tokenAuth($login, $token)
263 {
1bf36cd1 264 $res = XDB::query('SELECT a.uid, a.hruid
c3e97f0c
FB
265 FROM accounts AS a
266 WHERE a.token = {?} AND a.hruid = {?} AND a.state = \'active\'',
267 $token, $login);
0d44ce42 268 if ($res->numRows() == 1) {
8c12f931 269 return new User(null, $res->fetchOneAssoc());
0d44ce42
FB
270 }
271 return null;
272 }
273
365ba8c3 274 protected function makePerms($perm, $is_admin)
bf517daf 275 {
365ba8c3 276 S::set('perms', User::makePerms($perm, $is_admin));
bf517daf 277 }
278
c0799142
FB
279 public function setSkin()
280 {
0c02607e 281 if (S::logged() && (!S::has('skin') || S::suid())) {
c67ba12a
FB
282 $res = XDB::query('SELECT skin_tpl
283 FROM accounts AS a
284 INNER JOIN skins AS s on (a.skin = s.id)
285 WHERE a.uid = {?} AND skin_tpl != \'\'', S::i('uid'));
c0799142 286 S::set('skin', $res->fetchOneCell());
5480a216 287 }
288 }
0337d704 289
faa06583
FB
290 public function loggedLevel()
291 {
292 return AUTH_COOKIE;
293 }
294
c0799142
FB
295 public function sureLevel()
296 {
297 return AUTH_MDP;
0337d704 298 }
ebfdf077
FB
299
300
301 public function updateNbNotifs()
302 {
009b8ab7
FB
303 require_once 'notifs.inc.php';
304 $user = S::user();
069ddda8 305 $n = Watch::getCount($user);
009b8ab7 306 S::set('notifs', $n);
ebfdf077 307 }
604dfd58
FB
308
309 public function setAccessCookie($replace = false, $log = true) {
0c02607e 310 if (S::suid() || ($replace && !Cookie::blank('access'))) {
604dfd58
FB
311 return;
312 }
45dd32db 313 Cookie::set('access', sha1(S::user()->password()), 300, true);
604dfd58
FB
314 if ($log) {
315 S::logger()->log('cookie_on');
316 }
317 }
318
319 public function killAccessCookie($log = true) {
320 Cookie::kill('access');
321 if ($log) {
322 S::logger()->log('cookie_off');
323 }
324 }
325
326 public function killLoginFormCookies() {
327 Cookie::kill('uid');
328 Cookie::kill('domain');
329 }
0337d704 330}
331
a7de4ef7 332// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 333?>