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
30 // {{{ function XorgSession()
32 function XorgSession()
34 $this->challenge
= md5(uniqid(rand(), 1));
36 if (!Session
::has('uid')) {
47 if (!Session
::has('session')) {
48 $_SESSION['session'] = new XorgSession
;
53 // {{{ function destroy()
62 // {{{ function doAuth()
64 /** Try to do an authentication.
66 * @param page the calling page (by reference)
68 function doAuth(&$page,$new_name=false
)
71 if (identified()) { // ok, c'est bon, on n'a rien à faire
75 if (Session
::has('session')) {
76 $session =& Session
::getMixed('session');
78 if (Env
::has('username') && Env
::has('response') && isset($session->challenge
))
80 // si on vient de recevoir une identification par passwordpromptscreen.tpl
81 // ou passwordpromptscreenlogged.tpl
82 $uname = Env
::get('username');
84 if (Env
::get('domain') == "alias") {
89 INNER JOIN virtual_redirect USING(vid)
90 WHERE alias LIKE {?}", $uname."@".$globals->mail
->alias_dom
);
91 $redirect = $res->fetchOneCell();
93 $login = substr($redirect, 0, strpos($redirect, '@'));
101 $field = (!$redirect && preg_match('/^\d*$/', $uname)) ?
'id' : 'alias';
103 "SELECT u.user_id, u.password
104 FROM auth_user_md5 AS u
105 INNER JOIN aliases AS a ON ( a.id=u.user_id AND type!='homonyme' )
106 WHERE a.$field = {?} AND u.perms IN('admin','user')", $login);
108 $logger =& Session
::getMixed('log');
109 if (list($uid, $password) = $res->fetchOneRow()) {
110 require_once('secure_hash.inc.php');
111 $expected_response=hash_encrypt("$uname:$password:{$session->challenge}");
112 // le password de la base est peut-être encore encodé en md5
113 if (Env
::get('response') != $expected_response) {
114 $new_password = hash_xor(Env
::get('xorpass'), $password);
115 $expected_response = hash_encrypt("$uname:$new_password:{$session->challenge}");
116 if (Env
::get('response') == $expected_response) {
117 XDB
::execute("UPDATE auth_user_md5 SET password = {?} WHERE user_id = {?}", $new_password, $uid);
120 if (Env
::get('response') == $expected_response) {
121 if (Env
::has('domain')) {
122 if (($domain = Env
::get('domain', 'login')) == 'alias') {
123 setcookie('ORGdomain', "alias", (time()+
25920000), '/', '', 0);
125 setcookie('ORGdomain', '', (time()-3600), '/', '', 0);
127 // pour que la modification soit effective dans le reste de la page
128 $_COOKIE['ORGdomain'] = $domain;
131 unset($session->challenge
);
133 $logger->log('auth_ok');
135 start_connexion($uid, true
);
136 if (Env
::get('remember', 'false') == 'true') {
137 $cookie = hash_encrypt(Session
::get('password'));
138 setcookie('ORGaccess',$cookie,(time()+
25920000),'/','',0);
140 $logger->log("cookie_on");
143 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
146 $logger->log("cookie_off");
151 $logger->log('auth_fail','bad password');
154 $logger->log('auth_fail','bad login');
157 $this->doLogin($page,$new_name);
161 // {{{ function doAuthCookie()
163 /** Try to do a cookie-based authentication.
165 * @param page the calling page (by reference)
167 function doAuthCookie(&$page)
173 if (Env
::has('username') and Env
::has('response')) {
174 return $this->doAuth($page);
177 if ($r = try_cookie()) {
178 return $this->doAuth($page,($r>0));
183 // {{{ function doLogin()
185 /** Display login screen.
187 function doLogin(&$page, $new_name=false
)
189 if (logged() and !$new_name) {
190 $page->changeTpl('password_prompt_logged.tpl');
191 $page->addJsLink('javascript/do_challenge_response_logged.js');
192 $page->assign("xorg_tpl", "password_prompt_logged.tpl");
195 $page->changeTpl('password_prompt.tpl');
196 $page->addJsLink('javascript/do_challenge_response.js');
197 $page->assign("xorg_tpl", "password_prompt.tpl");
200 if ($globals->mail
->alias_dom
) {
201 $page->assign("domains", Array(
202 $globals->mail
->domain
."/".$globals->mail
->domain2
,
203 $globals->mail
->alias_dom
."/".$globals->mail
->alias_dom2
));
204 $page->assign("domains_value", Array("login", "alias"));
205 $page->assign("r_domain", Cookie
::get('ORGdomain', 'login'));
216 // {{{ function try_cookie()
218 /** réalise la récupération de $_SESSION pour qqn avec cookie
219 * @return int 0 if all OK, -1 if no cookie, 1 if cookie with bad hash,
220 * -2 should not happen
222 function try_cookie()
225 if (Cookie
::get('ORGaccess') == '' or !Cookie
::has('ORGuid')) {
230 "SELECT user_id,password FROM auth_user_md5 WHERE user_id = {?} AND perms IN('admin','user')",
231 Cookie
::getInt('ORGuid')
233 if ($res->numRows() != 0) {
234 list($uid, $password) = $res->fetchOneRow();
235 require_once('secure_hash.inc.php');
236 $expected_value = hash_encrypt($password);
237 if ($expected_value == Cookie
::get('ORGaccess')) {
238 start_connexion($uid, false
);
249 // {{{ function start_connexion()
251 /** place les variables de session dépendants de auth_user_md5
252 * et met à jour les dates de dernière connexion si nécessaire
254 * @see controlpermanent.inc.php controlauthentication.inc.php
256 function start_connexion ($uid, $identified)
260 SELECT u.user_id AS uid, prenom, nom, perms, promo, matricule, password, FIND_IN_SET('femme', u.flags) AS femme,
261 UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, a.alias AS forlife, a2.alias AS bestalias,
262 q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash
263 FROM auth_user_md5 AS u
264 INNER JOIN auth_user_quick AS q USING(user_id)
265 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
266 INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET('bestalias',a2.flags))
267 LEFT JOIN logger.sessions AS s ON (s.uid=u.user_id AND s.suid=0)
268 WHERE u.user_id = {?} AND u.perms IN('admin','user')
269 ORDER BY s.start DESC
271 $sess = $res->fetchOneAssoc();
272 $suid = Session
::getMixed('suid');
275 $logger = new DiogenesCoreLogger($uid, $suid);
276 $logger->log("suid_start", Session
::get('forlife')." by {$suid['uid']}");
277 $sess['suid'] = $suid;
279 $logger = Session
::getMixed('log', new DiogenesCoreLogger($uid));
280 $logger->log("connexion", $_SERVER['PHP_SELF']);
281 setcookie('ORGuid', $uid, (time()+
25920000), '/', '', 0);
285 $_SESSION['log'] = $logger;
286 $_SESSION['auth'] = ($identified ? AUTH_MDP
: AUTH_COOKIE
);
291 // {{{ function set_skin()
296 if (logged() && $globals->skin
->enable
) {
297 $uid = Session
::getInt('uid');
298 $res = XDB
::query("SELECT skin,skin_tpl
299 FROM auth_user_quick AS a
300 INNER JOIN skins AS s ON a.skin=s.id
301 WHERE user_id = {?} AND skin_tpl != ''", $uid);
302 if (list($_SESSION['skin_id'], $_SESSION['skin']) = $res->fetchOneRow()) {
306 if ($globals->skin
->enable
) {
307 $_SESSION['skin'] = $globals->skin
->def_tpl
;
308 $_SESSION['skin_id'] = $globals->skin
->def_id
;
310 $_SESSION['skin'] = 'default.tpl';
311 $_SESSION['skin_id'] = -1;
317 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: