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