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 ***************************************************************************/
24 // {{{ public static function init
26 public static function init() {
34 // {{{ public static function destroy()
36 public static function destroy() {
42 // {{{ public static function doAuth()
44 public static function doAuth($new_name = false
)
47 if (S
::identified()) { // ok, c'est bon, on n'a rien à faire
51 if (!Env
::has('username') ||
!Env
::has('response')
52 ||
!S
::has('challenge'))
57 // si on vient de recevoir une identification par passwordpromptscreen.tpl
58 // ou passwordpromptscreenlogged.tpl
59 $uname = Env
::v('username');
61 if (Env
::v('domain') == "alias") {
66 INNER JOIN virtual_redirect USING(vid)
67 WHERE alias LIKE {?}", $uname."@".$globals->mail
->alias_dom
);
68 $redirect = $res->fetchOneCell();
70 $login = substr($redirect, 0, strpos($redirect, '@'));
79 $field = (!$redirect && preg_match('/^\d*$/', $uname)) ?
'id' : 'alias';
81 "SELECT u.user_id, u.password
82 FROM auth_user_md5 AS u
83 INNER JOIN aliases AS a ON ( a.id=u.user_id AND type!='homonyme' )
84 WHERE a.$field = {?} AND u.perms IN('admin','user')", $login);
86 $logger = S
::v('log');
87 if (list($uid, $password) = $res->fetchOneRow()) {
88 require_once('secure_hash.inc.php');
89 $expected_response = hash_encrypt("$uname:$password:".S
::v('challenge'));
90 // le password de la base est peut-être encore encodé en md5
91 if (Env
::v('response') != $expected_response) {
92 $new_password = hash_xor(Env
::v('xorpass'), $password);
93 $expected_response = hash_encrypt("$uname:$new_password:".S
::v('challenge'));
94 if (Env
::v('response') == $expected_response) {
95 XDB
::execute("UPDATE auth_user_md5 SET password = {?} WHERE user_id = {?}",
99 if (Env
::v('response') == $expected_response) {
100 if (Env
::has('domain')) {
101 if (($domain = Env
::v('domain', 'login')) == 'alias') {
102 setcookie('ORGdomain', "alias", (time()+
25920000), '/', '', 0);
104 setcookie('ORGdomain', '', (time()-3600), '/', '', 0);
106 // pour que la modification soit effective dans le reste de la page
107 $_COOKIE['ORGdomain'] = $domain;
110 S
::kill('challenge');
112 $logger->log('auth_ok');
114 start_connexion($uid, true
);
115 if (Env
::v('remember', 'false') == 'true') {
116 $cookie = hash_encrypt(S
::v('password'));
117 setcookie('ORGaccess',$cookie,(time()+
25920000),'/','',0);
119 $logger->log("cookie_on");
122 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
125 $logger->log("cookie_off");
130 $logger->log('auth_fail','bad password');
133 $logger->log('auth_fail','bad login');
140 // {{{ public static function doAuthCookie()
142 /** Try to do a cookie-based authentication.
144 * @param page the calling page (by reference)
146 public static function doAuthCookie()
152 if (Env
::has('username') and Env
::has('response')) {
153 return XorgSession
::doAuth();
156 if ($r = try_cookie()) {
157 return XorgSession
::doAuth(($r > 0));
166 // {{{ function try_cookie()
168 /** réalise la récupération de $_SESSION pour qqn avec cookie
169 * @return int 0 if all OK, -1 if no cookie, 1 if cookie with bad hash,
170 * -2 should not happen
172 function try_cookie()
174 if (Cookie
::v('ORGaccess') == '' or !Cookie
::has('ORGuid')) {
179 "SELECT user_id,password FROM auth_user_md5
180 WHERE user_id = {?} AND perms IN('admin','user')",
181 Cookie
::i('ORGuid'));
183 if ($res->numRows() != 0) {
184 list($uid, $password) = $res->fetchOneRow();
185 require_once('secure_hash.inc.php');
186 $expected_value = hash_encrypt($password);
187 if ($expected_value == Cookie
::v('ORGaccess')) {
188 start_connexion($uid, false
);
199 // {{{ function start_connexion()
201 /** place les variables de session dépendants de auth_user_md5
202 * et met à jour les dates de dernière connexion si nécessaire
204 * @see controlpermanent.inc.php controlauthentication.inc.php
206 function start_connexion ($uid, $identified)
209 SELECT u.user_id AS uid, prenom, nom, perms, promo, matricule, password, FIND_IN_SET('femme', u.flags) AS femme,
210 UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, a.alias AS forlife, a2.alias AS bestalias,
211 q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash
212 FROM auth_user_md5 AS u
213 INNER JOIN auth_user_quick AS q USING(user_id)
214 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
215 INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET('bestalias',a2.flags))
216 LEFT JOIN logger.sessions AS s ON (s.uid=u.user_id AND s.suid=0)
217 WHERE u.user_id = {?} AND u.perms IN('admin','user')
218 ORDER BY s.start DESC
220 $sess = $res->fetchOneAssoc();
221 $suid = S
::v('suid');
224 $logger = new CoreLogger($uid, $suid);
225 $logger->log("suid_start", S
::v('forlife')." by {$suid['uid']}");
226 $sess['suid'] = $suid;
229 $logger = S
::v('log', new CoreLogger($uid));
230 $logger->log("connexion", $platal->path
);
231 setcookie('ORGuid', $uid, (time()+
25920000), '/', '', 0);
234 $_SESSION = array_merge($_SESSION, $sess);
235 $_SESSION['log'] = $logger;
236 $_SESSION['auth'] = ($identified ? AUTH_MDP
: AUTH_COOKIE
);
245 if (S
::logged() && !S
::has('skin')) {
247 $res = XDB
::query("SELECT skin_tpl
248 FROM auth_user_quick AS a
249 INNER JOIN skins AS s ON a.skin = s.id
250 WHERE user_id = {?} AND skin_tpl != ''", $uid);
251 if ($_SESSION['skin'] = $res->fetchOneCell()) {
257 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: