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