2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 require_once('platal/session.inc.php');
24 // {{{ class XorgSession
26 class XorgSession
extends DiogenesCoreSession
28 // {{{ function XorgSession()
30 function XorgSession()
32 $this->DiogenesCoreSession();
33 if (!Session
::has('uid')) {
44 if (!Session
::has('session')) {
45 $_SESSION['session'] = new XorgSession
;
50 // {{{ function destroy()
59 // {{{ function doAuth()
61 /** Try to do an authentication.
63 * @param page the calling page (by reference)
65 function doAuth(&$page,$new_name=false
)
68 if (identified()) { // ok, c'est bon, on n'a rien à faire
72 if (Session
::has('session')) {
73 $session =& Session
::getMixed('session');
75 if (Env
::has('username') && Env
::has('response') && isset($session->challenge
))
77 // si on vient de recevoir une identification par passwordpromptscreen.tpl
78 // ou passwordpromptscreenlogged.tpl
79 $uname = Env
::get('username');
81 if (Env
::get('domain') == "alias") {
83 $res = $globals->xdb
->query(
86 INNER JOIN virtual_redirect USING(vid)
87 WHERE alias LIKE {?}", $uname."@".$globals->mail
->alias_dom
);
88 $redirect = $res->fetchOneCell();
90 $login = substr($redirect, 0, strpos($redirect, '@'));
98 $field = (!$redirect && preg_match('/^\d*$/', $uname)) ?
'id' : 'alias';
99 $res = $globals->xdb
->query(
100 "SELECT u.user_id, u.password
101 FROM auth_user_md5 AS u
102 INNER JOIN aliases AS a ON ( a.id=u.user_id AND type!='homonyme' )
103 WHERE a.$field = {?} AND u.perms IN('admin','user')", $login);
105 $logger =& Session
::getMixed('log');
106 if (list($uid, $password) = $res->fetchOneRow()) {
107 require_once('secure_hash.inc.php');
108 $expected_response=hash_encrypt("$uname:$password:{$session->challenge}");
109 // le password de la base est peut-être encore encodé en md5
110 if (Env
::get('response') != $expected_response) {
111 $new_password = hash_xor(Env
::get('xorpass'), $password);
112 $expected_response = hash_encrypt("$uname:$new_password:{$session->challenge}");
113 if (Env
::get('response') == $expected_response) {
114 $globals->xdb
->execute("UPDATE auth_user_md5 SET password = {?} WHERE user_id = {?}", $new_password, $uid);
117 if (Env
::get('response') == $expected_response) {
118 if (Env
::has('domain')) {
119 if (($domain = Env
::get('domain', 'login')) == 'alias') {
120 setcookie('ORGdomain', "alias", (time()+
25920000), '/', '', 0);
122 setcookie('ORGdomain', '', (time()-3600), '/', '', 0);
124 // pour que la modification soit effective dans le reste de la page
125 $_COOKIE['ORGdomain'] = $domain;
128 unset($session->challenge
);
130 $logger->log('auth_ok');
132 start_connexion($uid, true
);
133 if (Env
::get('remember', 'false') == 'true') {
134 $cookie = hash_encrypt(Session
::get('password'));
135 setcookie('ORGaccess',$cookie,(time()+
25920000),'/','',0);
137 $logger->log("cookie_on");
140 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
143 $logger->log("cookie_off");
148 $logger->log('auth_fail','bad password');
151 $logger->log('auth_fail','bad login');
154 $this->doLogin($page,$new_name);
158 // {{{ function doAuthCookie()
160 /** Try to do a cookie-based authentication.
162 * @param page the calling page (by reference)
164 function doAuthCookie(&$page)
170 if (Env
::has('username') and Env
::has('response')) {
171 return $this->doAuth($page);
174 if ($r = try_cookie()) {
175 return $this->doAuth($page,($r>0));
180 // {{{ function doLogin()
182 /** Display login screen.
184 function doLogin(&$page, $new_name=false
)
186 if (logged() and !$new_name) {
187 $page->changeTpl('password_prompt_logged.tpl');
188 $page->addJsLink('javascript/do_challenge_response_logged.js');
189 $page->assign("xorg_tpl", "password_prompt_logged.tpl");
192 $page->changeTpl('password_prompt.tpl');
193 $page->addJsLink('javascript/do_challenge_response.js');
194 $page->assign("xorg_tpl", "password_prompt.tpl");
197 if ($globals->mail
->alias_dom
) {
198 $page->assign("domains", Array(
199 $globals->mail
->domain
."/".$globals->mail
->domain2
,
200 $globals->mail
->alias_dom
."/".$globals->mail
->alias_dom2
));
201 $page->assign("domains_value", Array("login", "alias"));
202 $page->assign("r_domain", Cookie
::get('ORGdomain', 'login'));
210 // {{{ function getUserId()
212 function getUserId($auth,$username)
215 $res = $globals->xdb
->query("SELECT id FROM aliases WHERE alias = {?}",$username);
216 return $res->fetchOneCell();
220 // {{{ function getUsername()
222 function getUsername($auth,$uid)
225 $res = $globals->xdb
->query("SELECT alias FROM aliases WHERE id = {?} AND type='a_vie'", $uid);
226 return $res->fetchOneCell();
233 // {{{ function try_cookie()
235 /** réalise la récupération de $_SESSION pour qqn avec cookie
236 * @return int 0 if all OK, -1 if no cookie, 1 if cookie with bad hash,
237 * -2 should not happen
239 function try_cookie()
242 if (Cookie
::get('ORGaccess') == '' or !Cookie
::has('ORGuid')) {
246 $res = @$globals->xdb
->query(
247 "SELECT user_id,password FROM auth_user_md5 WHERE user_id = {?} AND perms IN('admin','user')",
248 Cookie
::getInt('ORGuid')
250 if ($res->numRows() != 0) {
251 list($uid, $password) = $res->fetchOneRow();
252 require_once('secure_hash.inc.php');
253 $expected_value = hash_encrypt($password);
254 if ($expected_value == Cookie
::get('ORGaccess')) {
255 start_connexion($uid, false
);
266 // {{{ function start_connexion()
268 /** place les variables de session dépendants de auth_user_md5
269 * et met à jour les dates de dernière connexion si nécessaire
271 * @see controlpermanent.inc.php controlauthentication.inc.php
273 function start_connexion ($uid, $identified)
276 $res = $globals->xdb
->query("
277 SELECT u.user_id AS uid, prenom, nom, perms, promo, matricule, password, FIND_IN_SET('femme', u.flags) AS femme,
278 UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, a.alias AS forlife, a2.alias AS bestalias,
279 q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash
280 FROM auth_user_md5 AS u
281 INNER JOIN auth_user_quick AS q USING(user_id)
282 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
283 INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET('bestalias',a2.flags))
284 LEFT JOIN logger.sessions AS s ON (s.uid=u.user_id AND s.suid=0)
285 WHERE u.user_id = {?} AND u.perms IN('admin','user')
286 ORDER BY s.start DESC
288 $sess = $res->fetchOneAssoc();
289 $suid = Session
::getMixed('suid');
292 $logger = new DiogenesCoreLogger($uid, $suid);
293 $logger->log("suid_start", Session
::get('forlife')." by {$suid['uid']}");
294 $sess['suid'] = $suid;
296 $logger = Session
::getMixed('log', new DiogenesCoreLogger($uid));
297 $logger->log("connexion", $_SERVER['PHP_SELF']);
298 setcookie('ORGuid', $uid, (time()+
25920000), '/', '', 0);
302 $_SESSION['log'] = $logger;
303 $_SESSION['auth'] = ($identified ? AUTH_MDP
: AUTH_COOKIE
);
308 // {{{ function set_skin()
313 if (logged() && $globals->skin
->enable
) {
314 $uid = Session
::getInt('uid');
315 $res = $globals->xdb
->query("SELECT skin,skin_tpl
316 FROM auth_user_quick AS a
317 INNER JOIN skins AS s ON a.skin=s.id
318 WHERE user_id = {?} AND skin_tpl != ''", $uid);
319 if (list($_SESSION['skin_id'], $_SESSION['skin']) = $res->fetchOneRow()) {
323 if ($globals->skin
->enable
) {
324 $_SESSION['skin'] = $globals->skin
->def_tpl
;
325 $_SESSION['skin_id'] = $globals->skin
->def_id
;
327 $_SESSION['skin'] = 'default.tpl';
328 $_SESSION['skin_id'] = -1;
334 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: