7edb674891fcec2f4a15ff24d66e772785130478
[platal.git] / modules / xnet.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 );
37 }
38
39 function handler_photo(&$page, $x = null)
40 {
41 if (!$x || !($profile = Profile::get($x))) {
42 return PL_NOT_FOUND;
43 }
44
45 // Retrieve the photo and its mime type.
46 $photo = $profile->getPhoto(true);
47
48 // Display the photo, or a default one when not available.
49 $photo->send();
50 }
51
52 function handler_index(&$page)
53 {
54 $page->nomenu = true;
55 $page->changeTpl('xnet/index.tpl');
56 }
57
58 function handler_exit(&$page)
59 {
60 Platal::session()->stopSUID();
61 Platal::session()->destroy();
62 $page->changeTpl('xnet/deconnexion.tpl');
63 }
64
65 function handler_admin(&$page)
66 {
67 $page->changeTpl('xnet/admin.tpl');
68
69 if (Get::has('del')) {
70 $res = XDB::query('SELECT id, nom, mail_domain
71 FROM groups WHERE diminutif={?}',
72 Get::v('del'));
73 list($id, $nom, $domain) = $res->fetchOneRow();
74 $page->assign('nom', $nom);
75 if ($id && Post::has('del')) {
76 S::assert_xsrf_token();
77
78 XDB::query('DELETE FROM group_members WHERE asso_id={?}', $id);
79 $page->trigSuccess('membres supprimés');
80
81 if ($domain) {
82 XDB::query('DELETE FROM virtual_domains WHERE domain={?}', $domain);
83 XDB::query('DELETE FROM virtual, virtual_redirect
84 USING virtual INNER JOIN virtual_redirect USING (vid)
85 WHERE alias LIKE {?}', '%@'.$domain);
86 $page->trigSuccess('suppression des alias mails');
87
88 $mmlist = new MMList(S::v('uid'), S::v('password'), $domain);
89 if ($listes = $mmlist->get_lists()) {
90 foreach ($listes as $l) {
91 $mmlist->delete_list($l['list'], true);
92 }
93 $page->trigSuccess('mail lists surpprimées');
94 }
95 }
96
97 XDB::query('DELETE FROM groups WHERE id={?}', $id);
98 $page->trigSuccess("Groupe $nom supprimé");
99 Get::kill('del');
100 }
101 if (!$id) {
102 Get::kill('del');
103 }
104 }
105
106 if (Post::has('diminutif') && Post::v('diminutif') != "") {
107 S::assert_xsrf_token();
108
109 $res = XDB::query('SELECT COUNT(*)
110 FROM groups
111 WHERE diminutif = {?}',
112 Post::v('diminutif'));
113
114 if ($res->fetchOneCell() == 0) {
115 XDB::execute('INSERT INTO groups (id, diminutif)
116 VALUES (NULL, {?})',
117 Post::v('diminutif'));
118 pl_redirect('../' . Post::v('diminutif') . '/edit');
119 } else {
120 $page->trigError('Le diminutif demandé est déjà pris.');
121 }
122 }
123
124 $res = XDB::query('SELECT nom, diminutif
125 FROM groups
126 ORDER BY nom');
127 $page->assign('assos', $res->fetchAllAssoc());
128 }
129
130 function handler_plan(&$page)
131 {
132 $page->changeTpl('xnet/plan.tpl');
133
134 $page->setType('plan');
135
136 $res = XDB::iterator(
137 'SELECT dom.id, dom.nom as domnom, groups.diminutif, groups.nom
138 FROM group_dom AS dom
139 INNER JOIN groups ON dom.id = groups.dom
140 WHERE FIND_IN_SET("GroupesX", dom.cat) AND FIND_IN_SET("GroupesX", groups.cat)
141 ORDER BY dom.nom, groups.nom');
142 $groupesx = array();
143 while ($tmp = $res->next()) { $groupesx[$tmp['id']][] = $tmp; }
144 $page->assign('groupesx', $groupesx);
145
146 $res = XDB::iterator(
147 'SELECT dom.id, dom.nom as domnom, groups.diminutif, groups.nom
148 FROM group_dom AS dom
149 INNER JOIN groups ON dom.id = groups.dom
150 WHERE FIND_IN_SET("Binets", dom.cat) AND FIND_IN_SET("Binets", groups.cat)
151 ORDER BY dom.nom, groups.nom');
152 $binets = array();
153 while ($tmp = $res->next()) { $binets[$tmp['id']][] = $tmp; }
154 $page->assign('binets', $binets);
155
156 $res = XDB::iterator(
157 'SELECT diminutif, nom
158 FROM groups
159 WHERE cat LIKE "%Promotions%"
160 ORDER BY diminutif');
161 $page->assign('promos', $res);
162
163 $res = XDB::iterator(
164 'SELECT diminutif, nom
165 FROM groups
166 WHERE FIND_IN_SET("Institutions", cat)
167 ORDER BY diminutif');
168 $page->assign('inst', $res);
169 }
170
171 function handler_groups2(&$page)
172 {
173 $this->handler_groups(&$page, Get::v('cat'), Get::v('dom'));
174 }
175
176 function handler_groups(&$page, $cat = null, $dom = null)
177 {
178 if (!$cat) {
179 $this->handler_index(&$page);
180 }
181
182 $cat = mb_strtolower($cat);
183
184 $page->changeTpl('xnet/groupes.tpl');
185 $page->assign('cat', $cat);
186 $page->assign('dom', $dom);
187
188 $res = XDB::query("SELECT id,nom
189 FROM group_dom
190 WHERE FIND_IN_SET({?}, cat)
191 ORDER BY nom", $cat);
192 $doms = $res->fetchAllAssoc();
193 $page->assign('doms', $doms);
194
195 if (empty($doms)) {
196 $res = XDB::query("SELECT diminutif, nom, site
197 FROM groups
198 WHERE FIND_IN_SET({?}, cat)
199 ORDER BY nom", $cat);
200 $page->assign('gps', $res->fetchAllAssoc());
201 } elseif (!is_null($dom)) {
202 $res = XDB::query("SELECT diminutif, nom, site
203 FROM groups
204 WHERE FIND_IN_SET({?}, cat) AND dom={?}
205 ORDER BY nom", $cat, $dom);
206 $page->assign('gps', $res->fetchAllAssoc());
207 }
208
209 $page->setType($cat);
210 }
211
212 function handler_autologin(&$page)
213 {
214 $allkeys = func_get_args();
215 unset($allkeys[0]);
216 $url = join('/',$allkeys);
217 pl_content_headers("text/javascript");
218 echo '$.ajax({ url: "'.$url.'?forceXml=1", dataType: "xml", success: function(xml) { $("body",xml).insertBefore("body"); $("body:eq(1)").remove(); }});';
219 exit;
220 }
221 }
222
223 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
224 ?>