Port X.net session to the new schema.
[platal.git] / include / xnet / session.inc.php
... / ...
CommitLineData
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2008 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
22class XnetSession extends PlSession
23{
24 public function __construct()
25 {
26 parent::__construct();
27 S::bootstrap('perms_backup', new PlFlagSet());
28 }
29
30 public function startAvailableAuth()
31 {
32 if (!(S::v('perms') instanceof PlFlagSet)) {
33 S::set('perms', S::v('perms_backup'));
34 }
35
36 if (!S::logged() && Get::has('auth')) {
37 if (!$this->start(AUTH_MDP)) {
38 return false;
39 }
40 }
41
42 global $globals;
43 if (!S::logged()) {
44 // prevent connection to be linked to disconnection
45 if (($i = strpos($_SERVER['REQUEST_URI'], 'exit')) !== false)
46 $returl = "http://{$_SERVER['SERVER_NAME']}".substr($_SERVER['REQUEST_URI'], 0, $i);
47 else
48 $returl = "http://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}";
49 $url = "https://www.polytechnique.org/auth-groupex";
50 $url .= "?session=" . session_id();
51 $url .= "&challenge=" . S::v('challenge');
52 $url .= "&pass=" . md5(S::v('challenge') . $globals->xnet->secret);
53 $url .= "&url=".urlencode($returl);
54 S::set('loginX', $url);
55 }
56
57 if (S::logged() && $globals->asso()) {
58 $perms = S::v('perms');
59 $perms->rmFlag('groupadmin');
60 $perms->rmFlag('groupmember');
61 $perms->rmFlag('groupannu');
62 if (may_update()) {
63 $perms->addFlag('groupadmin');
64 $perms->addFlag('groupmember');
65 $perms->addFlag('groupannu');
66 }
67 if (is_member()) {
68 $perms->addFlag('groupmember');
69 if ($globals->asso('pub') != 'private') {
70 $perms->addFlag('groupannu');
71 }
72 }
73 if ($globals->asso('cat') == 'Promotions') {
74 $perms->addFlag('groupannu');
75 }
76 S::set('perms', $perms);
77 S::set('perms_backup', $perms);
78 }
79 return true;
80 }
81
82 protected function doAuth($level)
83 {
84 if (S::identified()) { // ok, c'est bon, on n'a rien à faire
85 return S::i('uid');
86 }
87 if (!Get::has('auth')) {
88 return null;
89 }
90 global $globals;
91 if (md5('1' . S::v('challenge') . $globals->xnet->secret . Get::i('uid') . '1') != Get::v('auth')) {
92 return null;
93 }
94 Get::kill('auth');
95 S::set('auth', AUTH_MDP);
96 return Get::i('uid');
97 }
98
99 protected function startSessionAs($user, $level)
100 {
101 global $globals;
102
103 if ($level == -1) {
104 S::set('auth', AUTH_MDP);
105 }
106 $res = XDB::query('SELECT u.user_id AS uid, prenom, nom, perms, promo, password, FIND_IN_SET(\'femme\', u.flags) AS femme,
107 a.alias AS forlife, a2.alias AS bestalias, q.core_mail_fmt AS mail_fmt, q.core_rss_hash
108 FROM auth_user_md5 AS u
109 INNER JOIN auth_user_quick AS q USING(user_id)
110 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\')
111 INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET(\'bestalias\', a2.flags))
112 WHERE u.user_id = {?} AND u.perms IN(\'admin\', \'user\')
113 LIMIT 1', $user);
114 $sess = $res->fetchOneAssoc();
115 $perms = $sess['perms'];
116 unset($sess['perms']);
117 $_SESSION = array_merge($_SESSION, $sess);
118 $this->makePerms($perms);
119 S::kill('challenge');
120 S::kill('loginX');
121 S::kill('may_update');
122 S::kill('is_member');
123 Get::kill('uid');
124 Get::kill('PHPSESSID');
125
126 $args = array();
127 foreach($_GET as $key => $val) {
128 $args[] = urlencode($key). '=' .urlencode($val);
129 }
130 return true;
131 }
132
133 public function doSelfSuid()
134 {
135 if (!$this->startSUID(S::i('uid'))) {
136 return false;
137 }
138 $this->makePerms('user');
139 return true;
140 }
141
142 public function stopSUID()
143 {
144 $suid = S::v('suid');
145 if (!parent::stopSUID()) {
146 return false;
147 }
148 S::kill('suid');
149 S::kill('may_update');
150 S::kill('is_member');
151 S::set('perms', $suid['perms']);
152 S::set('perms_backup', $suid['perms_backup']);
153 return true;
154 }
155
156 public function makePerms($perm)
157 {
158 $flags = new PlFlagSet();
159 if ($perm == 'disabled' || $perm == 'ext') {
160 S::set('perms', $flags);
161 S::set('perms_backup', $flags);
162 return;
163 }
164 $flags->addFlag(PERMS_USER);
165 if ($perm == 'admin') {
166 $flags->addFlag(PERMS_ADMIN);
167 }
168 S::set('perms', $flags);
169 S::set('perms_backup', $flags);
170 }
171
172 public function sureLevel()
173 {
174 return AUTH_MDP;
175 }
176}
177
178// {{{ function may_update
179
180/** Return administration rights for the current asso
181 * @param force Force administration rights to be read from database
182 * @param lose Force administration rights to be false
183 */
184function may_update($force = false, $lose = false)
185{
186 if (!isset($_SESSION['may_update'])) {
187 $_SESSION['may_update'] = array();
188 }
189 $may_update =& $_SESSION['may_update'];
190
191 global $globals;
192 $asso_id = $globals->asso('id');
193 if (!$asso_id) {
194 return false;
195 } elseif ($lose) {
196 $may_update[$asso_id] = false;
197 } elseif (S::has_perms() || (S::has('suid') && $force)) {
198 $may_update[$asso_id] = true;
199 } elseif (!isset($may_update[$asso_id]) || $force) {
200 $res = XDB::query("SELECT perms
201 FROM groupex.membres
202 WHERE uid={?} AND asso_id={?}",
203 S::v('uid'), $asso_id);
204 $may_update[$asso_id] = ($res->fetchOneCell() == 'admin');
205 }
206 return $may_update[$asso_id];
207}
208
209// }}}
210// {{{ function is_member
211
212/** Get membership informations for the current asso
213 * @param force Force membership to be read from database
214 * @param lose Force membership to be false
215 */
216function is_member($force = false, $lose = false)
217{
218 if (!isset($_SESSION['is_member'])) {
219 $_SESSION['is_member'] = array();
220 }
221 $is_member =& $_SESSION['is_member'];
222
223 global $globals;
224 $asso_id = $globals->asso('id');
225 if (!$asso_id) {
226 return false;
227 } elseif ($lose) {
228 $is_member[$asso_id] = false;
229 } elseif (S::has('suid') && $force) {
230 $is_member[$asso_id] = true;
231 } elseif (!isset($is_member[$asso_id]) || $force) {
232 $res = XDB::query("SELECT COUNT(*)
233 FROM groupex.membres
234 WHERE uid={?} AND asso_id={?}",
235 S::v('uid'), $asso_id);
236 $is_member[$asso_id] = ($res->fetchOneCell() == 1);
237 }
238 return $is_member[$asso_id];
239}
240
241// }}}
242// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
243?>