Merge branch 'xorg/maint' into xorg/master
[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, 'groups'),
35 'autologin' => $this->make_hook('autologin', AUTH_MDP, 'groups'),
36 'register/ext' => $this->make_hook('register_ext', AUTH_PUBLIC),
37 'edit' => $this->make_hook('edit', AUTH_MDP, 'groups'),
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::execute('DELETE v
86 FROM email_virtual AS v
87 INNER JOIN email_virtual_domains AS d ON (v.domain = d.id)
88 WHERE d.name = {?}',
89 $domain);
90 XDB::execute('DELETE FROM email_virtual_domains
91 WHERE name = {?}', $domain);
92 $page->trigSuccess('suppression des alias mails');
93
94 $mmlist = new MMList(S::v('uid'), S::v('password'), $domain);
95 if ($listes = $mmlist->get_lists()) {
96 foreach ($listes as $l) {
97 $mmlist->delete_list($l['list'], true);
98 }
99 $page->trigSuccess('mail lists surpprimées');
100 }
101 }
102
103 XDB::query('DELETE FROM groups WHERE id={?}', $id);
104 $page->trigSuccess("Groupe $nom supprimé");
105 Get::kill('del');
106 }
107 if (!$id) {
108 Get::kill('del');
109 }
110 }
111
112 if (Post::has('diminutif') && Post::v('diminutif') != "") {
113 S::assert_xsrf_token();
114
115 $res = XDB::query('SELECT COUNT(*)
116 FROM groups
117 WHERE diminutif = {?}',
118 Post::v('diminutif'));
119
120 if ($res->fetchOneCell() == 0) {
121 XDB::execute('INSERT INTO groups (id, diminutif)
122 VALUES (NULL, {?})',
123 Post::v('diminutif'));
124 pl_redirect(Post::v('diminutif') . '/edit');
125 } else {
126 $page->trigError('Le diminutif demandé est déjà pris.');
127 }
128 }
129
130 $res = XDB::query('SELECT nom, diminutif
131 FROM groups
132 ORDER BY nom');
133 $page->assign('assos', $res->fetchAllAssoc());
134 }
135
136 function handler_plan($page)
137 {
138 $page->changeTpl('xnet/plan.tpl');
139
140 $page->setType('plan');
141
142 $res = XDB::iterator(
143 'SELECT dom.id, dom.nom as domnom, groups.diminutif, groups.nom
144 FROM group_dom AS dom
145 INNER JOIN groups ON dom.id = groups.dom
146 WHERE FIND_IN_SET("GroupesX", dom.cat) AND FIND_IN_SET("GroupesX", groups.cat)
147 ORDER BY dom.nom, groups.nom');
148 $groupesx = array();
149 while ($tmp = $res->next()) { $groupesx[$tmp['id']][] = $tmp; }
150 $page->assign('groupesx', $groupesx);
151
152 $res = XDB::iterator(
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("Binets", dom.cat) AND FIND_IN_SET("Binets", groups.cat)
157 ORDER BY dom.nom, groups.nom');
158 $binets = array();
159 while ($tmp = $res->next()) { $binets[$tmp['id']][] = $tmp; }
160 $page->assign('binets', $binets);
161
162 $res = XDB::iterator(
163 'SELECT diminutif, nom
164 FROM groups
165 WHERE cat LIKE "%Promotions%"
166 ORDER BY diminutif');
167 $page->assign('promos', $res);
168
169 $res = XDB::iterator(
170 'SELECT diminutif, nom
171 FROM groups
172 WHERE FIND_IN_SET("Institutions", cat)
173 ORDER BY diminutif');
174 $page->assign('inst', $res);
175 }
176
177 function handler_groups2($page)
178 {
179 $this->handler_groups($page, Get::v('cat'), Get::v('dom'));
180 }
181
182 function handler_groups($page, $cat = null, $dom = null)
183 {
184 if (!$cat) {
185 $this->handler_index($page);
186 }
187
188 $cat = mb_strtolower($cat);
189
190 $page->changeTpl('xnet/groupes.tpl');
191 $page->assign('cat', $cat);
192 $page->assign('dom', $dom);
193
194 $res = XDB::query("SELECT id,nom
195 FROM group_dom
196 WHERE FIND_IN_SET({?}, cat)
197 ORDER BY nom", $cat);
198 $doms = $res->fetchAllAssoc();
199 $page->assign('doms', $doms);
200
201 if (empty($doms)) {
202 $res = XDB::query("SELECT diminutif, nom, site
203 FROM groups
204 WHERE FIND_IN_SET({?}, cat)
205 ORDER BY nom", $cat);
206 $page->assign('gps', $res->fetchAllAssoc());
207 } elseif (!is_null($dom)) {
208 $res = XDB::query("SELECT diminutif, nom, site
209 FROM groups
210 WHERE FIND_IN_SET({?}, cat) AND dom={?}
211 ORDER BY nom", $cat, $dom);
212 $page->assign('gps', $res->fetchAllAssoc());
213 }
214
215 $page->setType($cat);
216 }
217
218 function handler_autologin($page)
219 {
220 $allkeys = func_get_args();
221 unset($allkeys[0]);
222 $url = join('/',$allkeys);
223 pl_content_headers("text/javascript");
224 echo '$.ajax({ url: "'.$url.'?forceXml=1", dataType: "xml", success: function(xml) { $("body",xml).insertBefore("body"); $("body:eq(1)").remove(); }});';
225 exit;
226 }
227
228 function handler_edit($page)
229 {
230 global $globals;
231
232 $user = S::user();
233 if (empty($user)) {
234 return PL_NOT_FOUND;
235 }
236 if ($user->type != 'xnet') {
237 pl_redirect('index');
238 }
239
240 $page->changeTpl('xnet/edit.tpl');
241 if (Post::has('change')) {
242 S::assert_xsrf_token();
243
244 // Convert user status to X
245 if (!Post::blank('login_X')) {
246 $forlife = $this->changeLogin($page, $user, Post::t('login_X'));
247 if ($forlife) {
248 pl_redirect('index');
249 }
250 }
251
252 // Update user info
253 $full_name = Post::t('firstname') . ' ' . Post::t('lastname');
254 $directory_name = mb_strtoupper(Post::t('lastname')) . ' ' . Post::t('firstname');
255 XDB::query('UPDATE accounts
256 SET full_name = {?}, directory_name = {?}, display_name = {?},
257 firstname = {?}, lastname = {?}, sex = {?}, email = {?}
258 WHERE uid = {?}',
259 $full_name, $directory_name, Post::t('display_name'),
260 Post::t('firstname'), Post::t('lastname'),
261 (Post::t('sex') == 'male') ? 'male' : 'female', Post::t('email'), $user->id());
262 if (XDB::affectedRows()) {
263 require_once 'emails.inc.php';
264 if (require_email_update($user, Post::t('email'))) {
265 $listClient = new MMList(S::user());
266 $listClient->change_user_email($user->forlifeEmail(), Post::t('email'));
267 update_alias_user($user->forlifeEmail(), Post::t('email'));
268 }
269 $user = User::getWithUID($user->id());
270 S::set('user', $user);
271 $page->trigSuccess('Données mises à jour.');
272 }
273 }
274
275 $page->addJsLink('password.js');
276 $page->assign('user', $user);
277 }
278
279 }
280
281 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
282 ?>