Oops, wrong core version.
[platal.git] / classes / xorgsession.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 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{
c0799142
FB
24 public function __construct()
25 {
26 parent::__construct();
abde67b1
FB
27 }
28
c0799142 29 public function startAvailableAuth()
5480a216 30 {
c0799142
FB
31 if (!S::logged()) {
32 $cookie = $this->tryCookie();
33 if ($cookie == 0) {
34 return $this->start(AUTH_COOKIE);
998c070f 35 } else if ($cookie == 1 || $cookie == -2) {
c0799142
FB
36 return false;
37 }
5480a216 38 }
0be07aa6 39 if ((check_ip('dangerous') && S::has('uid')) || check_account()) {
9d214e8e 40 S::logger()->log("view_page", $_SERVER['REQUEST_URI']);
0337d704 41 }
c0799142 42 return true;
0337d704 43 }
44
c0799142
FB
45 /** Check the cookie and set the associated user_id in the auth_by_cookie session variable.
46 */
47 private function tryCookie()
48 {
49 S::kill('auth_by_cookie');
50 if (Cookie::v('ORGaccess') == '' || !Cookie::has('ORGuid')) {
51 return -1;
52 }
cab08090 53
c67ba12a
FB
54 $res = XDB::query('SELECT uid, password
55 FROM accounts
56 WHERE uid = {?} AND state = \'active\'',
c0799142
FB
57 Cookie::i('ORGuid'));
58 if ($res->numRows() != 0) {
59 list($uid, $password) = $res->fetchOneRow();
c67ba12a 60 $expected_value = sha1($password);
c0799142
FB
61 if ($expected_value == Cookie::v('ORGaccess')) {
62 S::set('auth_by_cookie', $uid);
63 return 0;
64 } else {
65 return 1;
66 }
67 }
68 return -2;
69 }
70
71 private function checkPassword($uname, $login, $response, $login_type)
5480a216 72 {
c67ba12a
FB
73 $res = XDB::query('SELECT a.uid, a.password
74 FROM accounts AS a
75 INNER JOIN aliases AS l ON (l.id = a.uid AND l.type != \'homonyme\')
76 WHERE l.' . $login_type . ' = {?} AND a.state = \'active\'',
c0799142
FB
77 $login);
78 if (list($uid, $password) = $res->fetchOneRow()) {
c67ba12a
FB
79 $expected_response = sha1("$uname:$password:" . S::v('challenge'));
80 /* XXX: Deprecates len(password) > 10 conversion */
c0799142 81 if ($response != $expected_response) {
732e5855 82 S::logger($uid)->log('auth_fail', 'bad password');
c0799142
FB
83 return null;
84 }
85 return $uid;
86 }
87 return null;
0337d704 88 }
cab08090 89
0337d704 90
c0799142
FB
91 /** Check auth.
92 */
93 protected function doAuth($level)
0337d704 94 {
e74411f7 95 global $globals;
c0799142
FB
96
97 /* Cookie authentication
98 */
99 if ($level == AUTH_COOKIE && !S::has('auth_by_cookie')) {
100 $this->tryCookie();
101 }
102 if ($level == AUTH_COOKIE && S::has('auth_by_cookie')) {
103 if (!S::logged()) {
104 S::set('auth', AUTH_COOKIE);
105 }
106 return S::i('auth_by_cookie');
e74411f7 107 }
0337d704 108
c0799142
FB
109
110 /* We want to do auth... we must have infos from a form.
111 */
e64c8b61 112 if (!Post::has('username') || !Post::has('response') || !S::has('challenge')) {
c0799142 113 return null;
63528107 114 }
115
c0799142
FB
116 /** We come from an authentication form.
117 */
e74411f7 118 if (S::has('suid')) {
c0799142 119 $suid = S::v('suid');
888465dd 120 $login = $uname = $suid['uid'];
e74411f7 121 $redirect = false;
122 } else {
123 $uname = Env::v('username');
e74411f7 124 if (Env::v('domain') == "alias") {
c0799142
FB
125 $res = XDB::query('SELECT redirect
126 FROM virtual
127 INNER JOIN virtual_redirect USING(vid)
128 WHERE alias LIKE {?}',
129 $uname . '@' . $globals->mail->alias_dom);
e74411f7 130 $redirect = $res->fetchOneCell();
131 if ($redirect) {
132 $login = substr($redirect, 0, strpos($redirect, '@'));
133 } else {
c0799142 134 $login = '';
e74411f7 135 }
0337d704 136 } else {
e74411f7 137 $login = $uname;
138 $redirect = false;
0337d704 139 }
63528107 140 }
cab08090 141
888465dd
FB
142 $uid = $this->checkPassword($uname, $login, Post::v('response'), (!$redirect && is_numeric($uname)) ? 'id' : 'alias');
143 if (!is_null($uid) && S::has('suid')) {
144 $suid = S::v('suid');
145 if ($suid['uid'] == $uid) {
146 $uid = S::i('uid');
147 } else {
148 $uid = null;
149 }
150 }
c0799142
FB
151 if (!is_null($uid)) {
152 S::set('auth', AUTH_MDP);
888465dd
FB
153 if (!S::has('suid')) {
154 if (Post::has('domain')) {
155 if (($domain = Post::v('domain', 'login')) == 'alias') {
156 setcookie('ORGdomain', "alias", (time() + 25920000), '/', '', 0);
157 } else {
158 setcookie('ORGdomain', '', (time() - 3600), '/', '', 0);
159 }
160 // pour que la modification soit effective dans le reste de la page
161 $_COOKIE['ORGdomain'] = $domain;
e74411f7 162 }
163 }
c0799142 164 S::kill('challenge');
732e5855 165 S::logger($uid)->log('auth_ok');
c0799142
FB
166 }
167 return $uid;
168 }
cab08090 169
c0799142
FB
170 protected function startSessionAs($uid, $level)
171 {
172 if ((!is_null(S::v('user')) && S::i('user') != $uid) || (S::has('uid') && S::i('uid') != $uid)) {
173 return false;
174 } else if (S::has('uid')) {
175 return true;
176 }
888465dd
FB
177 if ($level == AUTH_SUID) {
178 S::set('auth', AUTH_MDP);
9d214e8e 179 unset($_SESSION['log']);
c0799142 180 }
70232020 181
6672b29b 182 // Retrieves main user properties.
4182c6b5
FB
183 /** TODO: Move needed informations to account tables */
184 /** TODO: Currently suppressed data are matricule, promo */
185 /** TODO: Data to move are: banana_last, watch_last, last_version */
186 /** TODO: Switch to new permission system */
187 $res = XDB::query("SELECT a.uid, a.hruid, a.display_name, a.full_name, a.password,
188 a.sex = 'female' AS femme, a.mail_format as mail_fmt,
189 a.token, FIND_IN_SET('watch', a.flags) AS watch_account,
190 UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last,
191 q.last_version, g.g_account_name IS NOT NULL AS googleapps,
192 UNIX_TIMESTAMP(s.start) AS lastlogin, s.host,
193 IF(a.is_admin, 'admin', 'user') AS perms
194 FROM accounts AS a
195 INNER JOIN auth_user_quick AS q ON(a.uid = q.user_id)
196 LEFT JOIN gapps_accounts AS g ON(a.uid = g.l_userid AND g.g_status = 'active')
197 LEFT JOIN logger.last_sessions AS ls ON (ls.uid = a.uid)
6a61c46d 198 LEFT JOIN logger.sessions AS s ON(s.id = ls.id)
4182c6b5 199 WHERE a.uid = {?} AND a.state = 'active'", $uid);
c0799142
FB
200 $sess = $res->fetchOneAssoc();
201 $perms = $sess['perms'];
202 unset($sess['perms']);
6672b29b 203
6672b29b
VZ
204 // Loads the data into the real session.
205 $_SESSION = array_merge($_SESSION, $sess);
206
207 // Starts the session's logger, and sets up the permanent cookie.
208 if (S::has('suid')) {
209 $suid = S::v('suid');
e4501b51 210 $logger = S::logger($uid);
d0b64586 211 $logger->log("suid_start", S::v('hruid') . " by " . $suid['hruid']);
c0799142 212 } else {
e4501b51 213 $logger = S::logger($uid);
4ea44525 214 $logger->saveLastSession();
c0799142 215 setcookie('ORGuid', $uid, (time() + 25920000), '/', '', 0);
a90cde48 216
d0621f88 217 if (S::i('auth_by_cookie') == $uid || Post::v('remember', 'false') == 'true') {
c67ba12a 218 $cookie = sha1($sess['password']);
c0799142 219 setcookie('ORGaccess', $cookie, (time() + 25920000), '/', '', 0);
9d214e8e 220 if (S::i('auth_by_cookie') != $uid) {
c0799142 221 $logger->log("cookie_on");
63528107 222 }
c0799142
FB
223 } else {
224 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
9d214e8e 225 $logger->log("cookie_off");
0337d704 226 }
63528107 227 }
b0b937fd 228
6672b29b 229 // Finalizes the session setup.
50d5ec0b 230 S::set('perms', User::makePerms($perms));
c0799142
FB
231 $this->securityChecks();
232 $this->setSkin();
ebfdf077 233 $this->updateNbNotifs();
c0799142 234 check_redirect();
d0621f88
FB
235
236 // We should not have to use this private data anymore
237 S::kill('auth_by_cookie');
c0799142 238 return true;
0337d704 239 }
240
c0799142 241 private function securityChecks()
0337d704 242 {
c0799142
FB
243 $mail_subject = array();
244 if (check_account()) {
245 $mail_subject[] = 'Connexion d\'un utilisateur surveillé';
0337d704 246 }
c0799142
FB
247 if (check_ip('unsafe')) {
248 $mail_subject[] = 'Une IP surveillee a tente de se connecter';
249 if (check_ip('ban')) {
250 send_warning_mail(implode(' - ', $mail_subject));
251 $this->destroy();
252 Platal::page()->kill('Une erreur est survenue lors de la procédure d\'authentification. '
253 . 'Merci de contacter au plus vite '
254 . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>');
255 return false;
256 }
0337d704 257 }
c0799142
FB
258 if (count($mail_subject)) {
259 send_warning_mail(implode(' - ', $mail_subject));
0337d704 260 }
261 }
262
0d44ce42
FB
263 public function tokenAuth($login, $token)
264 {
c67ba12a
FB
265 $res = XDB::query('SELECT a.hruid
266 FROM aliases AS l
267 INNER JOIN accounts AS a ON (l.id = a.uid AND a.state = \'active\')
268 WHERE a.token = {?} AND l.alias = {?} AND l.type != \'homonyme\'',
269 $token, $login);
0d44ce42 270 if ($res->numRows() == 1) {
50d5ec0b
FB
271 $data = $res->fetchOneAssoc();
272 return new User($data['hruid'], $data);
0d44ce42
FB
273 }
274 return null;
275 }
276
732e5855 277 public function makePerms($perm)
bf517daf 278 {
113f6de8 279 $flags = new PlFlagSet();
bf517daf 280 if ($perm == 'disabled' || $perm == 'ext') {
ab694eb5 281 S::set('perms', $flags);
ab694eb5 282 return;
bf517daf 283 }
284 $flags->addFlag(PERMS_USER);
285 if ($perm == 'admin') {
286 $flags->addFlag(PERMS_ADMIN);
287 }
c0799142 288 S::set('perms', $flags);
bf517daf 289 }
290
c0799142
FB
291 public function setSkin()
292 {
c0799142
FB
293 if (S::logged() && (!S::has('skin') || S::has('suid'))) {
294 $uid = S::v('uid');
c67ba12a
FB
295 $res = XDB::query('SELECT skin_tpl
296 FROM accounts AS a
297 INNER JOIN skins AS s on (a.skin = s.id)
298 WHERE a.uid = {?} AND skin_tpl != \'\'', S::i('uid'));
c0799142 299 S::set('skin', $res->fetchOneCell());
5480a216 300 }
301 }
0337d704 302
faa06583
FB
303 public function loggedLevel()
304 {
305 return AUTH_COOKIE;
306 }
307
c0799142
FB
308 public function sureLevel()
309 {
310 return AUTH_MDP;
0337d704 311 }
ebfdf077
FB
312
313
314 public function updateNbNotifs()
315 {
316 require_once 'notifs.inc.php';
317 $n = select_notifs(false, S::i('uid'), S::v('watch_last'), false);
318 S::set('notifs', $n->numRows());
319 }
0337d704 320}
321
a7de4ef7 322// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 323?>