Use 'changeTpl' for xnet pages and use handler permission instead of page restriction
[platal.git] / include / xnet / session.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 Polytechnique.org *
0337d704 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
4869f665 22class XnetSession
0337d704 23{
0337d704 24 // {{{ function init
cab08090 25
1490093c 26 public static function init()
27 {
0337d704 28 global $globals;
29
cab08090 30 S::init();
31
cab08090 32 if (!S::logged()) {
0337d704 33 // prevent connexion to be linked to deconnexion
71fe935c 34 if (($i = strpos($_SERVER['REQUEST_URI'], 'exit')) !== false)
0337d704 35 $returl = "http://{$_SERVER['SERVER_NAME']}".substr($_SERVER['REQUEST_URI'], 0, $i);
36 else
37 $returl = "http://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}";
38 $url = "https://www.polytechnique.org/auth-groupex.php";
39 $url .= "?session=" . session_id();
cab08090 40 $url .= "&challenge=" . S::v('challenge');
41 $url .= "&pass=" . md5(S::v('challenge') . $globals->xnet->secret);
0337d704 42 $url .= "&url=".urlencode($returl);
cab08090 43 $_SESSION['loginX'] = $url;
0337d704 44 }
bf517daf 45
46 if (S::logged() && $globals->asso()) {
47 $perms = S::v('perms');
48 $perms->rmFlag('groupadmin');
49 $perms->rmFlag('groupmember');
50 if (may_update()) {
51 $perms->addFlag('groupadmin');
52 $perms->addFlag('groupmember');
53 }
54 if (is_member()) {
55 $perms->addFlag('groupmember');
56 }
57 $_SESSION['perms'] = $perms;
58 }
0337d704 59 }
cab08090 60
0337d704 61 // }}}
6995a9b9 62 // {{{ public static function destroy()
cab08090 63
6995a9b9 64 public static function destroy() {
cab08090 65 S::destroy();
0337d704 66 XnetSession::init();
67 }
cab08090 68
0337d704 69 // }}}
6995a9b9 70 // {{{ public static function doAuth()
0337d704 71
72 /** Try to do an authentication.
73 *
74 * @param page the calling page (by reference)
75 */
6995a9b9 76 public static function doAuth()
0337d704 77 {
a7de4ef7 78 if (S::identified()) { // ok, c'est bon, on n'a rien à faire
0deaff1d 79 return true;
80 }
0337d704 81
82 if (Get::has('auth')) {
b0b937fd 83 return XnetSession::doAuthX();
0337d704 84 }
63528107 85
86 return false;
0337d704 87 }
88
89 // }}}
c0d6753f 90 // {{{ doAuthCookie
91
6995a9b9 92 public static function doAuthCookie() {
c0d6753f 93 return XnetSession::doAuth();
94 }
95
96 // }}}
0337d704 97 // {{{ doAuthX
98
bf517daf 99 public static function doAuthX()
100 {
b0b937fd 101 global $globals, $page;
0337d704 102
5e2307dc 103 if (md5('1'.S::v('challenge').$globals->xnet->secret.Get::i('uid').'1') != Get::v('auth')) {
0337d704 104 $page->kill("Erreur d'authentification avec polytechnique.org !");
105 }
106
08cce2ff 107 $res = XDB::query("
0337d704 108 SELECT u.user_id AS uid, prenom, nom, perms, promo, password, FIND_IN_SET('femme', u.flags) AS femme,
109 a.alias AS forlife, a2.alias AS bestalias, q.core_mail_fmt AS mail_fmt, q.core_rss_hash
110 FROM auth_user_md5 AS u
111 INNER JOIN auth_user_quick AS q USING(user_id)
112 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
113 INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET('bestalias',a2.flags))
114 WHERE u.user_id = {?} AND u.perms IN('admin','user')
5e2307dc 115 LIMIT 1", Get::i('uid'));
0337d704 116 $_SESSION = array_merge($_SESSION, $res->fetchOneAssoc());
117 $_SESSION['auth'] = AUTH_MDP;
bf517daf 118 require_once 'xorg/session.inc.php';
119 $_SESSION['perms'] =& XorgSession::make_perms(S::v('perms'));
cab08090 120 S::kill('challenge');
121 S::kill('loginX');
0afc7e1e 122 S::kill('may_update');
123 S::kill('is_member');
0337d704 124 Get::kill('auth');
125 Get::kill('uid');
27472b85 126 $path = Get::v('n');
127 Get::kill('n');
63528107 128 Get::kill('PHPSESSID');
b0b937fd 129
fd834b4b 130 $args = array();
b0b937fd 131 foreach($_GET as $key => $val) {
0337d704 132 $args[] = urlencode($key).'='.urlencode($val);
133 }
63528107 134
fd834b4b 135 http_redirect($globals->baseurl . '/' . $path, join('&', $args));
0337d704 136 }
137
138 // }}}
0deaff1d 139 // {{{ doSelfSuid
b8e265bf 140
0deaff1d 141 public static function doSelfSuid()
142 {
143 if (!S::has('suid')) {
144 $_SESSION['suid'] = $_SESSION;
145 }
bf517daf 146 require_once 'xorg/session.inc.php';
147 $_SESSION['perms'] =& XorgSession::make_perms('user');
b8e265bf 148 }
b8e265bf 149
0deaff1d 150 // }}}
151 // {{{ killSuid
b8e265bf 152
0deaff1d 153 public static function killSuid()
154 {
155 if (!S::has('suid')) {
156 return;
157 }
158 $suid = S::v('suid');
159 S::kill('suid');
160 S::kill('may_update');
161 S::kill('is_member');
162 $_SESSION['perms'] = $suid['perms'];
b8e265bf 163 }
0deaff1d 164
165 // }}}
b8e265bf 166}
167
168// }}}
0deaff1d 169// {{{ function may_update
0337d704 170
0deaff1d 171/** Return administration rights for the current asso
172 * @param force Force administration rights to be read from database
173 * @param lose Force administration rights to be false
174 */
b8e265bf 175function may_update($force = false, $lose = false)
176{
177 if (!isset($_SESSION['may_update'])) {
178 $_SESSION['may_update'] = array();
179 }
180 $may_update =& $_SESSION['may_update'];
181
0337d704 182 global $globals;
b8e265bf 183 $asso_id = $globals->asso('id');
0deaff1d 184 if (!$asso_id) {
185 return false;
186 } elseif ($lose) {
187 $may_update[$asso_id] = false;
188 } elseif (S::has_perms() || (S::has('suid') && $force)) {
189 $may_update[$asso_id] = true;
190 } elseif (!isset($may_update[$asso_id]) || $force) {
b8e265bf 191 $res = XDB::query("SELECT perms
192 FROM groupex.membres
193 WHERE uid={?} AND asso_id={?}",
a14159bf 194 S::v('uid'), $asso_id);
b8e265bf 195 $may_update[$asso_id] = ($res->fetchOneCell() == 'admin');
b8e265bf 196 }
197 return $may_update[$asso_id];
0337d704 198}
199
200// }}}
0deaff1d 201// {{{ function is_member
0337d704 202
0deaff1d 203/** Get membership informations for the current asso
204 * @param force Force membership to be read from database
205 * @param lose Force membership to be false
206 */
b8e265bf 207function is_member($force = false, $lose = false)
c1863ee9 208{
b8e265bf 209 if (!isset($_SESSION['is_member'])) {
210 $_SESSION['is_member'] = array();
211 }
212 $is_member =& $_SESSION['is_member'];
213
0337d704 214 global $globals;
258b9710 215 $asso_id = $globals->asso('id');
0deaff1d 216 if (!$asso_id) {
217 return false;
b8e265bf 218 } elseif ($lose) {
219 $is_member[$asso_id] = false;
0deaff1d 220 } elseif (S::has('suid') && $force) {
221 $is_member[$asso_id] = true;
222 } elseif (!isset($is_member[$asso_id]) || $force) {
223 $res = XDB::query("SELECT COUNT(*)
224 FROM groupex.membres
225 WHERE uid={?} AND asso_id={?}",
226 S::v('uid'), $asso_id);
227 $is_member[$asso_id] = ($res->fetchOneCell() == 1);
258b9710 228 }
229 return $is_member[$asso_id];
0337d704 230}
231
232// }}}
a7de4ef7 233// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 234?>