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');
76 if (Env
::has('username') && Env
::has('response') && isset($session->challenge
))
78 // si on vient de recevoir une identification par passwordpromptscreen.tpl
79 // ou passwordpromptscreenlogged.tpl
80 $uname = Env
::get('username');
82 if (Env
::get('domain') == "alias") {
84 $res = $globals->xdb
->query(
87 INNER JOIN virtual_redirect USING(vid)
88 WHERE alias LIKE {?}", $uname."@".$globals->mail
->alias_dom
);
89 $redirect = $res->fetchOneCell();
91 $login = substr($redirect, 0, strpos($redirect, '@'));
99 $field = (!$redirect && preg_match('/^\d*$/', $uname)) ?
'id' : 'alias';
100 $res = $globals->xdb
->query(
101 "SELECT u.user_id, u.password
102 FROM auth_user_md5 AS u
103 INNER JOIN aliases AS a ON ( a.id=u.user_id AND type!='homonyme' )
104 WHERE a.$field = {?} AND u.perms IN('admin','user')", $login);
106 $logger =& Session
::getMixed('log');
107 if (list($uid, $password) = $res->fetchOneRow()) {
108 require_once('secure_hash.inc.php');
109 $expected_response=hash_encrypt("$uname:$password:{$session->challenge}");
110 // le password de la base est peut-être encore encodé en md5
111 if (Env
::get('response') != $expected_response) {
112 $new_password = hash_xor(Env
::get('xorpass'), $password);
113 $expected_response = hash_encrypt("$uname:$new_password:{$session->challenge}");
114 if (Env
::get('response') == $expected_response) {
115 $globals->xdb
->execute("UPDATE auth_user_md5 SET password = {?} WHERE user_id = {?}", $new_password, $uid);
118 if (Env
::get('response') == $expected_response) {
119 if (Env
::has('domain')) {
120 if (($domain = Env
::get('domain', 'login')) == 'alias') {
121 setcookie('ORGdomain', "alias", (time()+
25920000), '/', '', 0);
123 setcookie('ORGdomain', '', (time()-3600), '/', '', 0);
125 // pour que la modification soit effective dans le reste de la page
126 $_COOKIE['ORGdomain'] = $domain;
129 unset($session->challenge
);
131 $logger->log('auth_ok');
133 start_connexion($uid, true
);
134 if (Env
::get('remember', 'false') == 'true') {
135 $cookie = hash_encrypt(Session
::get('password'));
136 setcookie('ORGaccess',$cookie,(time()+
25920000),'/','',0);
138 $logger->log("cookie_on");
141 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
144 $logger->log("cookie_off");
149 $logger->log('auth_fail','bad password');
152 $logger->log('auth_fail','bad login');
155 $this->doLogin($page,$new_name);
159 // {{{ function doAuthCookie()
161 /** Try to do a cookie-based authentication.
163 * @param page the calling page (by reference)
165 function doAuthCookie(&$page)
171 if (Env
::has('username') and Env
::has('response')) {
172 return $this->doAuth($page);
175 if ($r = try_cookie()) {
176 return $this->doAuth($page,($r>0));
181 // {{{ function doLogin()
183 /** Display login screen.
185 function doLogin(&$page, $new_name=false
)
187 $page->addJsLink('javascript/secure_hash.js');
188 if (logged() and !$new_name) {
189 $page->changeTpl('password_prompt_logged.tpl');
190 $page->addJsLink('javascript/do_challenge_response_logged.js');
191 $page->assign("xorg_tpl", "password_prompt_logged.tpl");
194 $page->changeTpl('password_prompt.tpl');
195 $page->addJsLink('javascript/do_challenge_response.js');
196 $page->assign("xorg_tpl", "password_prompt.tpl");
199 if ($globals->mail
->alias_dom
) {
200 $page->assign("domains", Array(
201 $globals->mail
->domain
."/".$globals->mail
->domain2
,
202 $globals->mail
->alias_dom
."/".$globals->mail
->alias_dom2
));
203 $page->assign("domains_value", Array("login", "alias"));
204 $page->assign("r_domain", Cookie
::get('ORGdomain', 'login'));
212 // {{{ function getUserId()
214 function getUserId($auth,$username)
217 $res = $globals->xdb
->query("SELECT id FROM aliases WHERE alias = {?}",$username);
218 return $res->fetchOneCell();
222 // {{{ function getUsername()
224 function getUsername($auth,$uid)
227 $res = $globals->xdb
->query("SELECT alias FROM aliases WHERE id = {?} AND type='a_vie'", $uid);
228 return $res->fetchOneCell();
235 // {{{ function try_cookie()
237 /** réalise la récupération de $_SESSION pour qqn avec cookie
238 * @return int 0 if all OK, -1 if no cookie, 1 if cookie with bad hash,
239 * -2 should not happen
241 function try_cookie()
244 if (Cookie
::get('ORGaccess') == '' or !Cookie
::has('ORGuid')) {
248 $res = @$globals->xdb
->query(
249 "SELECT user_id,password FROM auth_user_md5 WHERE user_id = {?} AND perms IN('admin','user')",
250 Cookie
::getInt('ORGuid')
252 if ($res->numRows() != 0) {
253 list($uid, $password) = $res->fetchOneRow();
254 require_once('secure_hash.inc.php');
255 $expected_value = hash_encrypt($password);
256 if ($expected_value == Cookie
::get('ORGaccess')) {
257 start_connexion($uid, false
);
268 // {{{ function start_connexion()
270 /** place les variables de session dépendants de auth_user_md5
271 * et met à jour les dates de dernière connexion si nécessaire
273 * @see controlpermanent.inc.php controlauthentication.inc.php
275 function start_connexion ($uid, $identified)
278 $res = $globals->xdb
->query("
279 SELECT u.user_id AS uid, prenom, nom, perms, promo, matricule, password, FIND_IN_SET('femme', u.flags) AS femme,
280 UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, a.alias AS forlife, a2.alias AS bestalias,
281 q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash
282 FROM auth_user_md5 AS u
283 INNER JOIN auth_user_quick AS q USING(user_id)
284 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
285 INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET('bestalias',a2.flags))
286 LEFT JOIN logger.sessions AS s ON (s.uid=u.user_id AND s.suid=0)
287 WHERE u.user_id = {?} AND u.perms IN('admin','user')
288 ORDER BY s.start DESC
290 $sess = $res->fetchOneAssoc();
291 $suid = Session
::getMixed('suid');
294 $logger = new DiogenesCoreLogger($uid, $suid);
295 $logger->log("suid_start", Session
::get('forlife')." by {$suid['uid']}");
296 $sess['suid'] = $suid;
298 $logger = Session
::getMixed('log', new DiogenesCoreLogger($uid));
299 $logger->log("connexion", $_SERVER['PHP_SELF']);
300 setcookie('ORGuid', $uid, (time()+
25920000), '/', '', 0);
304 $_SESSION['log'] = $logger;
305 $_SESSION['auth'] = ($identified ? AUTH_MDP
: AUTH_COOKIE
);
310 // {{{ function set_skin()
315 if (logged() && $globals->skin
->enable
) {
316 $uid = Session
::getInt('uid');
317 $res = $globals->xdb
->query("SELECT skin,skin_tpl
318 FROM auth_user_quick AS a
319 INNER JOIN skins AS s ON a.skin=s.id
320 WHERE user_id = {?} AND skin_tpl != ''", $uid);
321 if (list($_SESSION['skin_id'], $_SESSION['skin']) = $res->fetchOneRow()) {
325 if ($globals->skin
->enable
) {
326 $_SESSION['skin'] = $globals->skin
->def_tpl
;
327 $_SESSION['skin_id'] = $globals->skin
->def_id
;
329 $_SESSION['skin'] = 'default.tpl';
330 $_SESSION['skin_id'] = -1;
336 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: