Fix login on X.net with only 'groups' perm.
[platal.git] / modules / xnet.php
CommitLineData
bd4be95d 1<?php
2/***************************************************************************
c441aabe 3 * Copyright (C) 2003-2014 Polytechnique.org *
bd4be95d 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(
5ae3e9a9
SJ
27 'index' => $this->make_hook('index', AUTH_PUBLIC),
28 'exit' => $this->make_hook('exit', AUTH_PUBLIC),
71fe935c 29
66ac1590 30 'login' => $this->make_hook('login', AUTH_COOKIE, 'groups'),
bfe9f4c7 31 'admin' => $this->make_hook('admin', AUTH_PASSWD, 'admin'),
5ae3e9a9
SJ
32 'groups' => $this->make_hook('groups', AUTH_PUBLIC),
33 'groupes.php' => $this->make_hook('groups2', AUTH_PUBLIC),
34 'plan' => $this->make_hook('plan', AUTH_PUBLIC),
250915b8
SJ
35 // Should be removed in a future release as links will have expired anyway.
36 'register/ext' => $this->make_hook('register_ext', AUTH_PUBLIC),
bfe9f4c7
SJ
37 'photo' => $this->make_hook('photo', AUTH_PASSWD, 'groups'),
38 'autologin' => $this->make_hook('autologin', AUTH_PASSWD, 'groups'),
39 'edit' => $this->make_hook('edit', AUTH_PASSWD, 'groups'),
5ae3e9a9 40 'Xnet' => $this->make_wiki_hook(),
bd4be95d 41 );
42 }
43
66ac1590
RB
44 function handler_login()
45 {
46 // We require different credentials for '/login/' ("groups" instead of "user").
47 // We have to redirect the call to the actual CoreModule->handler_login.
48 $args = func_get_args();
49 return call_user_func_array(array("CoreModule", "handler_login"), $args);
50 }
51
26ba053e 52 function handler_photo($page, $x = null)
deb09f59 53 {
706f830d 54 if (!$x || !($profile = Profile::get($x))) {
deb09f59 55 return PL_NOT_FOUND;
56 }
57
706f830d 58 // Retrieve the photo and its mime type.
470d14f6 59 $photo = $profile->getPhoto(true, true);
deb09f59 60
706f830d
FB
61 // Display the photo, or a default one when not available.
62 $photo->send();
deb09f59 63 }
eaf30d86 64
26ba053e 65 function handler_index($page)
bd4be95d 66 {
d83a1b04 67 $page->nomenu = true;
bd4be95d 68 $page->changeTpl('xnet/index.tpl');
bd4be95d 69 }
badfc7ee 70
26ba053e 71 function handler_exit($page)
71fe935c 72 {
ab694eb5
FB
73 Platal::session()->stopSUID();
74 Platal::session()->destroy();
b8e265bf 75 $page->changeTpl('xnet/deconnexion.tpl');
71fe935c 76 }
77
26ba053e 78 function handler_admin($page)
d55888e6 79 {
1490093c 80 $page->changeTpl('xnet/admin.tpl');
d55888e6 81
82 if (Get::has('del')) {
08cce2ff 83 $res = XDB::query('SELECT id, nom, mail_domain
eb41eda9 84 FROM groups WHERE diminutif={?}',
5e2307dc 85 Get::v('del'));
d55888e6 86 list($id, $nom, $domain) = $res->fetchOneRow();
87 $page->assign('nom', $nom);
40d428d8
VZ
88 if ($id && Post::has('del')) {
89 S::assert_xsrf_token();
90
eb41eda9 91 XDB::query('DELETE FROM group_members WHERE asso_id={?}', $id);
a7d35093 92 $page->trigSuccess('membres supprimés');
d55888e6 93
94 if ($domain) {
831aa467
SJ
95 XDB::execute('DELETE v
96 FROM email_virtual AS v
97 INNER JOIN email_virtual_domains AS d ON (v.domain = d.id)
98 WHERE d.name = {?}',
99 $domain);
100 XDB::execute('DELETE FROM email_virtual_domains
101 WHERE name = {?}', $domain);
a7d35093 102 $page->trigSuccess('suppression des alias mails');
d55888e6 103
9bb8bf21 104 $mmlist = new MMList(S::v('uid'), S::v('password'), $domain);
105 if ($listes = $mmlist->get_lists()) {
d55888e6 106 foreach ($listes as $l) {
9bb8bf21 107 $mmlist->delete_list($l['list'], true);
d55888e6 108 }
a7d35093 109 $page->trigSuccess('mail lists surpprimées');
d55888e6 110 }
111 }
112
eb41eda9 113 XDB::query('DELETE FROM groups WHERE id={?}', $id);
a7d35093 114 $page->trigSuccess("Groupe $nom supprimé");
d55888e6 115 Get::kill('del');
116 }
117 if (!$id) {
118 Get::kill('del');
119 }
120 }
121
8d11b755 122 if (Post::has('diminutif') && Post::v('diminutif') != "") {
40d428d8
VZ
123 S::assert_xsrf_token();
124
3af5a31e 125 $res = XDB::query('SELECT COUNT(*)
eb41eda9 126 FROM groups
3af5a31e
SJ
127 WHERE diminutif = {?}',
128 Post::v('diminutif'));
129
130 if ($res->fetchOneCell() == 0) {
eb41eda9 131 XDB::execute('INSERT INTO groups (id, diminutif)
3af5a31e
SJ
132 VALUES (NULL, {?})',
133 Post::v('diminutif'));
fc4714d6 134 pl_redirect(Post::v('diminutif') . '/edit');
3af5a31e
SJ
135 } else {
136 $page->trigError('Le diminutif demandé est déjà pris.');
137 }
d55888e6 138 }
139
3af5a31e 140 $res = XDB::query('SELECT nom, diminutif
eb41eda9 141 FROM groups
3af5a31e 142 ORDER BY nom');
d55888e6 143 $page->assign('assos', $res->fetchAllAssoc());
144 }
145
26ba053e 146 function handler_plan($page)
71fe935c 147 {
71fe935c 148 $page->changeTpl('xnet/plan.tpl');
149
150 $page->setType('plan');
151
08cce2ff 152 $res = XDB::iterator(
e1406965
FB
153 'SELECT dom.id, dom.nom as domnom, groups.diminutif, groups.nom
154 FROM group_dom AS dom
155 INNER JOIN groups ON dom.id = groups.dom
156 WHERE FIND_IN_SET("GroupesX", dom.cat) AND FIND_IN_SET("GroupesX", groups.cat)
157 ORDER BY dom.nom, groups.nom');
71fe935c 158 $groupesx = array();
159 while ($tmp = $res->next()) { $groupesx[$tmp['id']][] = $tmp; }
160 $page->assign('groupesx', $groupesx);
161
08cce2ff 162 $res = XDB::iterator(
e1406965
FB
163 'SELECT dom.id, dom.nom as domnom, groups.diminutif, groups.nom
164 FROM group_dom AS dom
165 INNER JOIN groups ON dom.id = groups.dom
166 WHERE FIND_IN_SET("Binets", dom.cat) AND FIND_IN_SET("Binets", groups.cat)
167 ORDER BY dom.nom, groups.nom');
71fe935c 168 $binets = array();
169 while ($tmp = $res->next()) { $binets[$tmp['id']][] = $tmp; }
170 $page->assign('binets', $binets);
171
08cce2ff 172 $res = XDB::iterator(
e1406965 173 'SELECT diminutif, nom
eb41eda9 174 FROM groups
71fe935c 175 WHERE cat LIKE "%Promotions%"
176 ORDER BY diminutif');
177 $page->assign('promos', $res);
178
08cce2ff 179 $res = XDB::iterator(
e1406965 180 'SELECT diminutif, nom
eb41eda9 181 FROM groups
71fe935c 182 WHERE FIND_IN_SET("Institutions", cat)
183 ORDER BY diminutif');
184 $page->assign('inst', $res);
045a1522 185 }
0e02493e 186
26ba053e 187 function handler_groups2($page)
0e02493e 188 {
26ba053e 189 $this->handler_groups($page, Get::v('cat'), Get::v('dom'));
0e02493e 190 }
191
26ba053e 192 function handler_groups($page, $cat = null, $dom = null)
0e02493e 193 {
0e02493e 194 if (!$cat) {
26ba053e 195 $this->handler_index($page);
0e02493e 196 }
197
a953f7e7 198 $cat = mb_strtolower($cat);
0e02493e 199
200 $page->changeTpl('xnet/groupes.tpl');
201 $page->assign('cat', $cat);
202 $page->assign('dom', $dom);
203
aab2ffdd 204 $res = XDB::query("SELECT id,nom
eb41eda9 205 FROM group_dom
00112b2e
VZ
206 WHERE FIND_IN_SET({?}, cat)
207 ORDER BY nom", $cat);
0e02493e 208 $doms = $res->fetchAllAssoc();
209 $page->assign('doms', $doms);
210
211 if (empty($doms)) {
00112b2e 212 $res = XDB::query("SELECT diminutif, nom, site
eb41eda9 213 FROM groups
00112b2e
VZ
214 WHERE FIND_IN_SET({?}, cat)
215 ORDER BY nom", $cat);
f0430dc7 216 $page->assign('gps', $res->fetchAllAssoc());
0e02493e 217 } elseif (!is_null($dom)) {
00112b2e 218 $res = XDB::query("SELECT diminutif, nom, site
eb41eda9 219 FROM groups
00112b2e
VZ
220 WHERE FIND_IN_SET({?}, cat) AND dom={?}
221 ORDER BY nom", $cat, $dom);
f0430dc7 222 $page->assign('gps', $res->fetchAllAssoc());
0e02493e 223 }
0e02493e 224
0e02493e 225 $page->setType($cat);
226 }
eaf30d86 227
26ba053e 228 function handler_autologin($page)
4a8a1e0a 229 {
8c5c6d64 230 $allkeys = func_get_args();
231 unset($allkeys[0]);
232 $url = join('/',$allkeys);
3cb500d5 233 pl_content_headers("text/javascript");
8c5c6d64 234 echo '$.ajax({ url: "'.$url.'?forceXml=1", dataType: "xml", success: function(xml) { $("body",xml).insertBefore("body"); $("body:eq(1)").remove(); }});';
4a8a1e0a 235 exit;
236 }
f15d1307 237
2a1cd4ab 238 function handler_edit($page)
f15d1307
SJ
239 {
240 global $globals;
241
242 $user = S::user();
243 if (empty($user)) {
244 return PL_NOT_FOUND;
245 }
246 if ($user->type != 'xnet') {
247 pl_redirect('index');
248 }
249
250 $page->changeTpl('xnet/edit.tpl');
251 if (Post::has('change')) {
252 S::assert_xsrf_token();
253
254 // Convert user status to X
255 if (!Post::blank('login_X')) {
256 $forlife = $this->changeLogin($page, $user, Post::t('login_X'));
257 if ($forlife) {
258 pl_redirect('index');
259 }
260 }
261
75e295ff
SJ
262 require_once 'emails.inc.php';
263 require_once 'name.func.inc.php';
264
f15d1307 265 // Update user info
75e295ff
SJ
266 $lastname = capitalize_name(Post::t('lastname'));
267 $firstname = capitalize_name(Post::t('firstname'));
3ba2fdf6
SJ
268 $full_name = build_full_name($firstname, $lastname);
269 $directory_name = build_directory_name($firstname, $lastname);
b0788bf4 270 $sort_name = build_sort_name($firstname, $lastname);
f15d1307 271 XDB::query('UPDATE accounts
b0788bf4 272 SET full_name = {?}, directory_name = {?}, sort_name = {?}, display_name = {?},
8a41e7f9 273 firstname = {?}, lastname = {?}, sex = {?}
f15d1307 274 WHERE uid = {?}',
b0788bf4 275 $full_name, $directory_name, $sort_name, Post::t('display_name'),
b7753795 276 Post::t('firstname'), Post::t('lastname'),
8a41e7f9
SJ
277 (Post::t('sex') == 'male') ? 'male' : 'female', $user->id());
278
279 // Updates email.
8a41e7f9
SJ
280 $new_email = strtolower(Post::t('email'));
281 if (require_email_update($user, $new_email)) {
282 XDB::query('UPDATE accounts
283 SET email = {?}
284 WHERE uid = {?}',
285 $new_email, $user->id());
ca6980dc 286 $listClient = new MMList(S::user());
8a41e7f9
SJ
287 $listClient->change_user_email($user->forlifeEmail(), $new_email);
288 update_alias_user($user->forlifeEmail(), $new_email);
f15d1307 289 }
8a41e7f9
SJ
290 $user = User::getWithUID($user->id());
291 S::set('user', $user);
292 $page->trigSuccess('Données mises à jour.');
f15d1307
SJ
293 }
294
295 $page->addJsLink('password.js');
296 $page->assign('user', $user);
297 }
73f2bb48 298
250915b8
SJ
299 function handler_register_ext($page, $hash = null)
300 {
301 http_redirect(Platal::globals()->xnet->xorg_baseurl . 'register/ext/' . $hash);
302 }
bd4be95d 303}
304
448c8cdc 305// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
bd4be95d 306?>