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