9d783e268168906e50743855ac0db0c12aea350b
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
36 $_SESSION['session'] = new XorgSession
;
40 // {{{ function destroy()
48 // {{{ function doAuth()
50 /** Try to do an authentication.
52 * @param page the calling page (by reference)
54 function doAuth(&$page,$new_name=false
)
57 if (S
::identified()) { // ok, c'est bon, on n'a rien à faire
61 if (Env
::has('username') && Env
::has('response') && S
::has('challenge'))
63 // si on vient de recevoir une identification par passwordpromptscreen.tpl
64 // ou passwordpromptscreenlogged.tpl
65 $uname = Env
::get('username');
67 if (Env
::get('domain') == "alias") {
72 INNER JOIN virtual_redirect USING(vid)
73 WHERE alias LIKE {?}", $uname."@".$globals->mail
->alias_dom
);
74 $redirect = $res->fetchOneCell();
76 $login = substr($redirect, 0, strpos($redirect, '@'));
84 $field = (!$redirect && preg_match('/^\d*$/', $uname)) ?
'id' : 'alias';
86 "SELECT u.user_id, u.password
87 FROM auth_user_md5 AS u
88 INNER JOIN aliases AS a ON ( a.id=u.user_id AND type!='homonyme' )
89 WHERE a.$field = {?} AND u.perms IN('admin','user')", $login);
91 $logger =& S
::v('log');
92 if (list($uid, $password) = $res->fetchOneRow()) {
93 require_once('secure_hash.inc.php');
94 $expected_response=hash_encrypt("$uname:$password:".S
::v('challenge'));
95 // le password de la base est peut-être encore encodé en md5
96 if (Env
::get('response') != $expected_response) {
97 $new_password = hash_xor(Env
::get('xorpass'), $password);
98 $expected_response = hash_encrypt("$uname:$new_password:".S
::v('challenge'));
99 if (Env
::get('response') == $expected_response) {
100 XDB
::execute("UPDATE auth_user_md5 SET password = {?} WHERE user_id = {?}", $new_password, $uid);
103 if (Env
::get('response') == $expected_response) {
104 if (Env
::has('domain')) {
105 if (($domain = Env
::get('domain', 'login')) == 'alias') {
106 setcookie('ORGdomain', "alias", (time()+
25920000), '/', '', 0);
108 setcookie('ORGdomain', '', (time()-3600), '/', '', 0);
110 // pour que la modification soit effective dans le reste de la page
111 $_COOKIE['ORGdomain'] = $domain;
114 S
::kill('challenge');
116 $logger->log('auth_ok');
118 start_connexion($uid, true
);
119 if (Env
::get('remember', 'false') == 'true') {
120 $cookie = hash_encrypt(S
::v('password'));
121 setcookie('ORGaccess',$cookie,(time()+
25920000),'/','',0);
123 $logger->log("cookie_on");
126 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
129 $logger->log("cookie_off");
134 $logger->log('auth_fail','bad password');
137 $logger->log('auth_fail','bad login');
140 XorgSession
::doLogin($page,$new_name);
144 // {{{ function doAuthCookie()
146 /** Try to do a cookie-based authentication.
148 * @param page the calling page (by reference)
150 function doAuthCookie(&$page)
156 if (Env
::has('username') and Env
::has('response')) {
157 return XorgSession
::doAuth($page);
160 if ($r = try_cookie()) {
161 return XorgSession
::doAuth($page,($r>0));
166 // {{{ function doLogin()
168 /** Display login screen.
170 function doLogin(&$page, $new_name=false
)
172 if (S
::logged() and !$new_name) {
173 $page->changeTpl('password_prompt_logged.tpl');
174 $page->addJsLink('javascript/do_challenge_response_logged.js');
175 $page->assign("xorg_tpl", "password_prompt_logged.tpl");
178 $page->changeTpl('password_prompt.tpl');
179 $page->addJsLink('javascript/do_challenge_response.js');
180 $page->assign("xorg_tpl", "password_prompt.tpl");
183 if ($globals->mail
->alias_dom
) {
184 $page->assign("domains", Array(
185 $globals->mail
->domain
."/".$globals->mail
->domain2
,
186 $globals->mail
->alias_dom
."/".$globals->mail
->alias_dom2
));
187 $page->assign("domains_value", Array("login", "alias"));
188 $page->assign("r_domain", Cookie
::get('ORGdomain', 'login'));
199 // {{{ function try_cookie()
201 /** réalise la récupération de $_SESSION pour qqn avec cookie
202 * @return int 0 if all OK, -1 if no cookie, 1 if cookie with bad hash,
203 * -2 should not happen
205 function try_cookie()
207 if (Cookie
::get('ORGaccess') == '' or !Cookie
::has('ORGuid')) {
212 "SELECT user_id,password FROM auth_user_md5 WHERE user_id = {?} AND perms IN('admin','user')",
213 Cookie
::getInt('ORGuid')
215 if ($res->numRows() != 0) {
216 list($uid, $password) = $res->fetchOneRow();
217 require_once('secure_hash.inc.php');
218 $expected_value = hash_encrypt($password);
219 if ($expected_value == Cookie
::get('ORGaccess')) {
220 start_connexion($uid, false
);
231 // {{{ function start_connexion()
233 /** place les variables de session dépendants de auth_user_md5
234 * et met à jour les dates de dernière connexion si nécessaire
236 * @see controlpermanent.inc.php controlauthentication.inc.php
238 function start_connexion ($uid, $identified)
241 SELECT u.user_id AS uid, prenom, nom, perms, promo, matricule, password, FIND_IN_SET('femme', u.flags) AS femme,
242 UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, a.alias AS forlife, a2.alias AS bestalias,
243 q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash
244 FROM auth_user_md5 AS u
245 INNER JOIN auth_user_quick AS q USING(user_id)
246 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
247 INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET('bestalias',a2.flags))
248 LEFT JOIN logger.sessions AS s ON (s.uid=u.user_id AND s.suid=0)
249 WHERE u.user_id = {?} AND u.perms IN('admin','user')
250 ORDER BY s.start DESC
252 $sess = $res->fetchOneAssoc();
253 $suid = S
::v('suid');
256 $logger = new DiogenesCoreLogger($uid, $suid);
257 $logger->log("suid_start", S
::v('forlife')." by {$suid['uid']}");
258 $sess['suid'] = $suid;
260 $logger = S
::v('log', new DiogenesCoreLogger($uid));
261 $logger->log("connexion", $_SERVER['PHP_SELF']);
262 setcookie('ORGuid', $uid, (time()+
25920000), '/', '', 0);
266 $_SESSION['log'] = $logger;
267 $_SESSION['auth'] = ($identified ? AUTH_MDP
: AUTH_COOKIE
);
272 // {{{ function set_skin()
277 if (S
::logged() && $globals->skin
->enable
) {
279 $res = XDB
::query("SELECT skin,skin_tpl
280 FROM auth_user_quick AS a
281 INNER JOIN skins AS s ON a.skin=s.id
282 WHERE user_id = {?} AND skin_tpl != ''", $uid);
283 if (list($_SESSION['skin_id'], $_SESSION['skin']) = $res->fetchOneRow()) {
287 if ($globals->skin
->enable
) {
288 $_SESSION['skin'] = $globals->skin
->def_tpl
;
289 $_SESSION['skin_id'] = $globals->skin
->def_id
;
291 $_SESSION['skin'] = 'default.tpl';
292 $_SESSION['skin_id'] = -1;
298 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: