Some small improvements on the skin
[platal.git] / include / xorg / session.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 require_once dirname(__FILE__).'/../../classes/Session.php';
23
24 class XorgSession
25 {
26 // {{{ function init
27
28 function init() {
29 S::init();
30 if (!S::has('uid')) {
31 try_cookie();
32 }
33 }
34
35 // }}}
36 // {{{ function destroy()
37
38 function destroy() {
39 S::destroy();
40 XorgSession::init();
41 }
42
43 // }}}
44 // {{{ function doAuth()
45
46 function doAuth($new_name = false)
47 {
48 global $globals;
49 if (S::identified()) { // ok, c'est bon, on n'a rien à faire
50 return true;
51 }
52
53 if (!Env::has('username') || !Env::has('response')
54 || !S::has('challenge'))
55 {
56 return false;
57 }
58
59 // si on vient de recevoir une identification par passwordpromptscreen.tpl
60 // ou passwordpromptscreenlogged.tpl
61 $uname = Env::v('username');
62
63 if (Env::v('domain') == "alias") {
64
65 $res = XDB::query(
66 "SELECT redirect
67 FROM virtual
68 INNER JOIN virtual_redirect USING(vid)
69 WHERE alias LIKE {?}", $uname."@".$globals->mail->alias_dom);
70 $redirect = $res->fetchOneCell();
71 if ($redirect) {
72 $login = substr($redirect, 0, strpos($redirect, '@'));
73 } else {
74 $login = "";
75 }
76 } else {
77 $login = $uname;
78 }
79
80 $field = (!$redirect && preg_match('/^\d*$/', $uname)) ? 'id' : 'alias';
81 $res = XDB::query(
82 "SELECT u.user_id, u.password
83 FROM auth_user_md5 AS u
84 INNER JOIN aliases AS a ON ( a.id=u.user_id AND type!='homonyme' )
85 WHERE a.$field = {?} AND u.perms IN('admin','user')", $login);
86
87 $logger =& S::v('log');
88 if (list($uid, $password) = $res->fetchOneRow()) {
89 require_once('secure_hash.inc.php');
90 $expected_response=hash_encrypt("$uname:$password:".S::v('challenge'));
91 // le password de la base est peut-être encore encodé en md5
92 if (Env::v('response') != $expected_response) {
93 $new_password = hash_xor(Env::v('xorpass'), $password);
94 $expected_response = hash_encrypt("$uname:$new_password:".S::v('challenge'));
95 if (Env::v('response') == $expected_response) {
96 XDB::execute("UPDATE auth_user_md5 SET password = {?} WHERE user_id = {?}", $new_password, $uid);
97 }
98 }
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);
103 } else {
104 setcookie('ORGdomain', '', (time()-3600), '/', '', 0);
105 }
106 // pour que la modification soit effective dans le reste de la page
107 $_COOKIE['ORGdomain'] = $domain;
108 }
109
110 S::kill('challenge');
111 if ($logger) {
112 $logger->log('auth_ok');
113 }
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);
118 if ($logger) {
119 $logger->log("cookie_on");
120 }
121 } else {
122 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
123
124 if ($logger) {
125 $logger->log("cookie_off");
126 }
127 }
128 return true;
129 } elseif ($logger) {
130 $logger->log('auth_fail','bad password');
131 }
132 } elseif ($logger) {
133 $logger->log('auth_fail','bad login');
134 }
135
136 return false;
137 }
138
139 // }}}
140 // {{{ function doAuthCookie()
141
142 /** Try to do a cookie-based authentication.
143 *
144 * @param page the calling page (by reference)
145 */
146 function doAuthCookie()
147 {
148 if (S::logged()) {
149 return true;
150 }
151
152 if (Env::has('username') and Env::has('response')) {
153 return XorgSession::doAuth();
154 }
155
156 if ($r = try_cookie()) {
157 return XorgSession::doAuth(($r > 0));
158 }
159
160 return false;
161 }
162
163 // }}}
164 }
165
166 // {{{ function try_cookie()
167
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
171 */
172 function try_cookie()
173 {
174 if (Cookie::v('ORGaccess') == '' or !Cookie::has('ORGuid')) {
175 return -1;
176 }
177
178 $res = @XDB::query(
179 "SELECT user_id,password FROM auth_user_md5 WHERE user_id = {?} AND perms IN('admin','user')",
180 Cookie::i('ORGuid')
181 );
182 if ($res->numRows() != 0) {
183 list($uid, $password) = $res->fetchOneRow();
184 require_once('secure_hash.inc.php');
185 $expected_value = hash_encrypt($password);
186 if ($expected_value == Cookie::v('ORGaccess')) {
187 start_connexion($uid, false);
188 return 0;
189 } else {
190 return 1;
191 }
192 }
193
194 return -2;
195 }
196
197 // }}}
198 // {{{ function start_connexion()
199
200 /** place les variables de session dépendants de auth_user_md5
201 * et met à jour les dates de dernière connexion si nécessaire
202 * @return void
203 * @see controlpermanent.inc.php controlauthentication.inc.php
204 */
205 function start_connexion ($uid, $identified)
206 {
207 $res = XDB::query("
208 SELECT u.user_id AS uid, prenom, nom, perms, promo, matricule, password, FIND_IN_SET('femme', u.flags) AS femme,
209 UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, a.alias AS forlife, a2.alias AS bestalias,
210 q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash
211 FROM auth_user_md5 AS u
212 INNER JOIN auth_user_quick AS q USING(user_id)
213 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
214 INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET('bestalias',a2.flags))
215 LEFT JOIN logger.sessions AS s ON (s.uid=u.user_id AND s.suid=0)
216 WHERE u.user_id = {?} AND u.perms IN('admin','user')
217 ORDER BY s.start DESC
218 LIMIT 1", $uid);
219 $sess = $res->fetchOneAssoc();
220 $suid = S::v('suid');
221
222 if ($suid) {
223 $logger = new CoreLogger($uid, $suid);
224 $logger->log("suid_start", S::v('forlife')." by {$suid['uid']}");
225 $sess['suid'] = $suid;
226 } else {
227 $logger = S::v('log', new CoreLogger($uid));
228 $logger->log("connexion", $_SERVER['PHP_SELF']);
229 setcookie('ORGuid', $uid, (time()+25920000), '/', '', 0);
230 }
231
232 $_SESSION = $sess;
233 $_SESSION['log'] = $logger;
234 $_SESSION['auth'] = ($identified ? AUTH_MDP : AUTH_COOKIE);
235 set_skin();
236 }
237
238 // }}}
239
240 function set_skin()
241 {
242 global $globals;
243 if (S::logged() && !S::has('skin')) {
244 $uid = S::v('uid');
245 $res = XDB::query("SELECT skin_tpl
246 FROM auth_user_quick AS a
247 INNER JOIN skins AS s ON a.skin = s.id
248 WHERE user_id = {?} AND skin_tpl != ''", $uid);
249 if ($_SESSION['skin'] = $res->fetchOneCell()) {
250 return;
251 }
252 }
253 }
254
255 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
256 ?>