Fixes group display permissions.
[platal.git] / modules / xnet.php
... / ...
CommitLineData
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2011 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 XnetModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
27 'index' => $this->make_hook('index', AUTH_PUBLIC),
28 'exit' => $this->make_hook('exit', AUTH_PUBLIC),
29
30 'admin' => $this->make_hook('admin', AUTH_MDP, 'admin'),
31 'groups' => $this->make_hook('groups', AUTH_PUBLIC),
32 'groupes.php' => $this->make_hook('groups2', AUTH_PUBLIC),
33 'plan' => $this->make_hook('plan', AUTH_PUBLIC),
34 'photo' => $this->make_hook('photo', AUTH_MDP),
35 'autologin' => $this->make_hook('autologin', AUTH_MDP),
36 'login/ext' => $this->make_hook('login_ext', AUTH_PUBLIC),
37 'register/ext' => $this->make_hook('register_ext', AUTH_PUBLIC),
38 'edit' => $this->make_hook('edit', AUTH_MDP, 'user'),
39 'password' => $this->make_hook('password', AUTH_MDP, 'user'),
40
41 'Xnet' => $this->make_wiki_hook(),
42 );
43 }
44
45 function handler_photo($page, $x = null)
46 {
47 if (!$x || !($profile = Profile::get($x))) {
48 return PL_NOT_FOUND;
49 }
50
51 // Retrieve the photo and its mime type.
52 $photo = $profile->getPhoto(true, true);
53
54 // Display the photo, or a default one when not available.
55 $photo->send();
56 }
57
58 function handler_index($page)
59 {
60 $page->nomenu = true;
61 $page->changeTpl('xnet/index.tpl');
62 }
63
64 function handler_exit($page)
65 {
66 Platal::session()->stopSUID();
67 Platal::session()->destroy();
68 $page->changeTpl('xnet/deconnexion.tpl');
69 }
70
71 function handler_admin($page)
72 {
73 $page->changeTpl('xnet/admin.tpl');
74
75 if (Get::has('del')) {
76 $res = XDB::query('SELECT id, nom, mail_domain
77 FROM groups WHERE diminutif={?}',
78 Get::v('del'));
79 list($id, $nom, $domain) = $res->fetchOneRow();
80 $page->assign('nom', $nom);
81 if ($id && Post::has('del')) {
82 S::assert_xsrf_token();
83
84 XDB::query('DELETE FROM group_members WHERE asso_id={?}', $id);
85 $page->trigSuccess('membres supprimés');
86
87 if ($domain) {
88 XDB::execute('DELETE v
89 FROM email_virtual AS v
90 INNER JOIN email_virtual_domains AS d ON (v.domain = d.id)
91 WHERE d.name = {?}',
92 $domain);
93 XDB::execute('DELETE FROM email_virtual_domains
94 WHERE name = {?}', $domain);
95 $page->trigSuccess('suppression des alias mails');
96
97 $mmlist = new MMList(S::v('uid'), S::v('password'), $domain);
98 if ($listes = $mmlist->get_lists()) {
99 foreach ($listes as $l) {
100 $mmlist->delete_list($l['list'], true);
101 }
102 $page->trigSuccess('mail lists surpprimées');
103 }
104 }
105
106 XDB::query('DELETE FROM groups WHERE id={?}', $id);
107 $page->trigSuccess("Groupe $nom supprimé");
108 Get::kill('del');
109 }
110 if (!$id) {
111 Get::kill('del');
112 }
113 }
114
115 if (Post::has('diminutif') && Post::v('diminutif') != "") {
116 S::assert_xsrf_token();
117
118 $res = XDB::query('SELECT COUNT(*)
119 FROM groups
120 WHERE diminutif = {?}',
121 Post::v('diminutif'));
122
123 if ($res->fetchOneCell() == 0) {
124 XDB::execute('INSERT INTO groups (id, diminutif)
125 VALUES (NULL, {?})',
126 Post::v('diminutif'));
127 pl_redirect(Post::v('diminutif') . '/edit');
128 } else {
129 $page->trigError('Le diminutif demandé est déjà pris.');
130 }
131 }
132
133 $res = XDB::query('SELECT nom, diminutif
134 FROM groups
135 ORDER BY nom');
136 $page->assign('assos', $res->fetchAllAssoc());
137 }
138
139 function handler_plan($page)
140 {
141 $page->changeTpl('xnet/plan.tpl');
142
143 $page->setType('plan');
144
145 $res = XDB::iterator(
146 'SELECT dom.id, dom.nom as domnom, groups.diminutif, groups.nom
147 FROM group_dom AS dom
148 INNER JOIN groups ON dom.id = groups.dom
149 WHERE FIND_IN_SET("GroupesX", dom.cat) AND FIND_IN_SET("GroupesX", groups.cat)
150 ORDER BY dom.nom, groups.nom');
151 $groupesx = array();
152 while ($tmp = $res->next()) { $groupesx[$tmp['id']][] = $tmp; }
153 $page->assign('groupesx', $groupesx);
154
155 $res = XDB::iterator(
156 'SELECT dom.id, dom.nom as domnom, groups.diminutif, groups.nom
157 FROM group_dom AS dom
158 INNER JOIN groups ON dom.id = groups.dom
159 WHERE FIND_IN_SET("Binets", dom.cat) AND FIND_IN_SET("Binets", groups.cat)
160 ORDER BY dom.nom, groups.nom');
161 $binets = array();
162 while ($tmp = $res->next()) { $binets[$tmp['id']][] = $tmp; }
163 $page->assign('binets', $binets);
164
165 $res = XDB::iterator(
166 'SELECT diminutif, nom
167 FROM groups
168 WHERE cat LIKE "%Promotions%"
169 ORDER BY diminutif');
170 $page->assign('promos', $res);
171
172 $res = XDB::iterator(
173 'SELECT diminutif, nom
174 FROM groups
175 WHERE FIND_IN_SET("Institutions", cat)
176 ORDER BY diminutif');
177 $page->assign('inst', $res);
178 }
179
180 function handler_groups2($page)
181 {
182 $this->handler_groups($page, Get::v('cat'), Get::v('dom'));
183 }
184
185 function handler_groups($page, $cat = null, $dom = null)
186 {
187 if (!$cat) {
188 $this->handler_index($page);
189 }
190
191 $cat = mb_strtolower($cat);
192
193 $page->changeTpl('xnet/groupes.tpl');
194 $page->assign('cat', $cat);
195 $page->assign('dom', $dom);
196
197 $res = XDB::query("SELECT id,nom
198 FROM group_dom
199 WHERE FIND_IN_SET({?}, cat)
200 ORDER BY nom", $cat);
201 $doms = $res->fetchAllAssoc();
202 $page->assign('doms', $doms);
203
204 if (empty($doms)) {
205 $res = XDB::query("SELECT diminutif, nom, site
206 FROM groups
207 WHERE FIND_IN_SET({?}, cat)
208 ORDER BY nom", $cat);
209 $page->assign('gps', $res->fetchAllAssoc());
210 } elseif (!is_null($dom)) {
211 $res = XDB::query("SELECT diminutif, nom, site
212 FROM groups
213 WHERE FIND_IN_SET({?}, cat) AND dom={?}
214 ORDER BY nom", $cat, $dom);
215 $page->assign('gps', $res->fetchAllAssoc());
216 }
217
218 $page->setType($cat);
219 }
220
221 function handler_autologin($page)
222 {
223 $allkeys = func_get_args();
224 unset($allkeys[0]);
225 $url = join('/',$allkeys);
226 pl_content_headers("text/javascript");
227 echo '$.ajax({ url: "'.$url.'?forceXml=1", dataType: "xml", success: function(xml) { $("body",xml).insertBefore("body"); $("body:eq(1)").remove(); }});';
228 exit;
229 }
230
231 function handler_login_ext($page)
232 {
233 if (!S::logged()) {
234 $page->changeTpl('xnet/login.tpl');
235 } else {
236 pl_redirect('');
237 }
238 }
239
240 function handler_register_ext($page, $hash = null)
241 {
242 XDB::execute('DELETE FROM register_pending_xnet
243 WHERE DATE_SUB(NOW(), INTERVAL 1 MONTH) > date');
244 $res = XDB::fetchOneAssoc('SELECT uid, hruid
245 FROM register_pending_xnet
246 WHERE hash = {?}',
247 $hash);
248
249 if (is_null($hash) || is_null($res)) {
250 $page->trigErrorRedirect('Cette adresse n\'existe pas ou n\'existe plus sur le serveur.', '');
251 }
252
253 if (Post::has('pwhash') && Post::t('pwhash')) {
254 XDB::query('UPDATE accounts
255 SET password = {?}, state = \'active\'
256 WHERE uid = {?} AND state = \'pending\' AND type = \'xnet\'',
257 Post::t('pwhash'), $res['uid']);
258 XDB::query('DELETE FROM register_pending_xnet
259 WHERE uid = {?}',
260 $res['uid']);
261
262 S::logger($res['uid'])->log('passwd', '');
263
264 // Try to start a session (so the user don't have to log in); we will use
265 // the password available in Post:: to authenticate the user.
266 Post::kill('wait');
267 Platal::session()->startAvailableAuth();
268
269 $page->changeTpl('xnet/register.success.tpl');
270 $page->assign('hruid', $res['hruid']);
271 } else {
272 $page->changeTpl('platal/password.tpl');
273 $page->assign('xnet', true);
274 $page->assign('hruid', $res['hruid']);
275 $page->assign('do_auth', true);
276 }
277 }
278
279 function handler_edit($page)
280 {
281 global $globals;
282
283 $user = S::user();
284 if (empty($user)) {
285 return PL_NOT_FOUND;
286 }
287 if ($user->type != 'xnet') {
288 pl_redirect('index');
289 }
290
291 $page->changeTpl('xnet/edit.tpl');
292 if (Post::has('change')) {
293 S::assert_xsrf_token();
294
295 // Convert user status to X
296 if (!Post::blank('login_X')) {
297 $forlife = $this->changeLogin($page, $user, Post::t('login_X'));
298 if ($forlife) {
299 pl_redirect('index');
300 }
301 }
302
303 // Update user info
304 XDB::query('UPDATE accounts
305 SET full_name = {?}, directory_name = {?}, display_name = {?},
306 sex = {?}, email = {?}
307 WHERE uid = {?}',
308 Post::t('full_name'), Post::t('directory_name'), Post::t('display_name'),
309 (Post::t('sex') == 'male') ? 'male' : 'female', Post::t('email'), $user->id());
310 if (XDB::affectedRows()) {
311 $user = User::getWithUID($user->id());
312 S::set('user', $user);
313 $page->trigSuccess('Données mises à jour.');
314 }
315 }
316
317 $page->addJsLink('password.js');
318 $page->assign('user', $user);
319 }
320
321 function handler_password ($page)
322 {
323 if (Post::has('pwhash') && Post::t('pwhash')) {
324 S::assert_xsrf_token();
325
326 S::set('password', $password = Post::t('pwhash'));
327 XDB::execute('UPDATE accounts
328 SET password = {?}
329 WHERE uid={?}', $password,
330 S::i('uid'));
331 S::logger()->log('passwd');
332 Platal::session()->setAccessCookie(true);
333 $page->changeTpl('platal/password.success.tpl');
334 $page->run();
335 }
336
337 $page->changeTpl('platal/password.tpl');
338 $page->assign('xnet_reset', true);
339 $page->assign('do_auth', false);
340 }
341}
342
343// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
344?>