Replace copy_tables.php by a shell script (database alteration require admin privileges)
[platal.git] / modules / xnet.php
CommitLineData
bd4be95d 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 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(
020c8ed0
SJ
27 'index' => $this->make_hook('index', AUTH_PUBLIC),
28 'exit' => $this->make_hook('exit', AUTH_PUBLIC),
71fe935c 29
020c8ed0
SJ
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),
bd4be95d 36 );
37 }
38
deb09f59 39 function handler_photo(&$page, $x = null)
40 {
41 if (is_null($x)) {
42 return PL_NOT_FOUND;
43 }
44
5d83c3be
SJ
45 $res = XDB::query("SELECT pp.attachmime, pp.attach
46 FROM aliases AS a
47 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.flags))
48 INNER JOIN profile_photos AS pp ON (ap.pid = pp.pid)
49 WHERE a.alias = {?}", $x);
deb09f59 50
51 if ((list($type, $data) = $res->fetchOneRow())) {
3cb500d5 52 pl_cached_dynamic_content_headers("image/$type");
deb09f59 53 echo $data;
54 } else {
3cb500d5 55 pl_cached_dynamic_content_headers("image/png");
deb09f59 56 echo file_get_contents(dirname(__FILE__).'/../htdocs/images/none.png');
57 }
58 exit;
59 }
eaf30d86 60
bd4be95d 61 function handler_index(&$page)
62 {
d83a1b04 63 $page->nomenu = true;
bd4be95d 64 $page->changeTpl('xnet/index.tpl');
bd4be95d 65 }
badfc7ee 66
71fe935c 67 function handler_exit(&$page)
68 {
ab694eb5
FB
69 Platal::session()->stopSUID();
70 Platal::session()->destroy();
b8e265bf 71 $page->changeTpl('xnet/deconnexion.tpl');
71fe935c 72 }
73
d55888e6 74 function handler_admin(&$page)
75 {
1490093c 76 $page->changeTpl('xnet/admin.tpl');
d55888e6 77
78 if (Get::has('del')) {
08cce2ff 79 $res = XDB::query('SELECT id, nom, mail_domain
eb41eda9 80 FROM groups WHERE diminutif={?}',
5e2307dc 81 Get::v('del'));
d55888e6 82 list($id, $nom, $domain) = $res->fetchOneRow();
83 $page->assign('nom', $nom);
40d428d8
VZ
84 if ($id && Post::has('del')) {
85 S::assert_xsrf_token();
86
eb41eda9 87 XDB::query('DELETE FROM group_members WHERE asso_id={?}', $id);
a7d35093 88 $page->trigSuccess('membres supprimés');
d55888e6 89
90 if ($domain) {
08cce2ff 91 XDB::query('DELETE FROM virtual_domains WHERE domain={?}', $domain);
92 XDB::query('DELETE FROM virtual, virtual_redirect
d55888e6 93 USING virtual INNER JOIN virtual_redirect USING (vid)
94 WHERE alias LIKE {?}', '%@'.$domain);
a7d35093 95 $page->trigSuccess('suppression des alias mails');
d55888e6 96
9bb8bf21 97 $mmlist = new MMList(S::v('uid'), S::v('password'), $domain);
98 if ($listes = $mmlist->get_lists()) {
d55888e6 99 foreach ($listes as $l) {
9bb8bf21 100 $mmlist->delete_list($l['list'], true);
d55888e6 101 }
a7d35093 102 $page->trigSuccess('mail lists surpprimées');
d55888e6 103 }
104 }
105
eb41eda9 106 XDB::query('DELETE FROM groups WHERE id={?}', $id);
a7d35093 107 $page->trigSuccess("Groupe $nom supprimé");
d55888e6 108 Get::kill('del');
109 }
110 if (!$id) {
111 Get::kill('del');
112 }
113 }
114
8d11b755 115 if (Post::has('diminutif') && Post::v('diminutif') != "") {
40d428d8
VZ
116 S::assert_xsrf_token();
117
3af5a31e 118 $res = XDB::query('SELECT COUNT(*)
eb41eda9 119 FROM groups
3af5a31e
SJ
120 WHERE diminutif = {?}',
121 Post::v('diminutif'));
122
123 if ($res->fetchOneCell() == 0) {
eb41eda9 124 XDB::execute('INSERT INTO groups (id, diminutif)
3af5a31e
SJ
125 VALUES (NULL, {?})',
126 Post::v('diminutif'));
127 pl_redirect('../' . Post::v('diminutif') . '/edit');
128 } else {
129 $page->trigError('Le diminutif demandé est déjà pris.');
130 }
d55888e6 131 }
132
3af5a31e 133 $res = XDB::query('SELECT nom, diminutif
eb41eda9 134 FROM groups
3af5a31e 135 ORDER BY nom');
d55888e6 136 $page->assign('assos', $res->fetchAllAssoc());
137 }
138
71fe935c 139 function handler_plan(&$page)
140 {
71fe935c 141 $page->changeTpl('xnet/plan.tpl');
142
143 $page->setType('plan');
144
08cce2ff 145 $res = XDB::iterator(
e1406965
FB
146 'SELECT dom.id, dom.nom as domnom, groups.diminutif, groups.nom
147 FROM group_dom AS dom
148 INNER JOIN groups ON dom.id = groups.dom
149 WHERE FIND_IN_SET("GroupesX", dom.cat) AND FIND_IN_SET("GroupesX", groups.cat)
150 ORDER BY dom.nom, groups.nom');
71fe935c 151 $groupesx = array();
152 while ($tmp = $res->next()) { $groupesx[$tmp['id']][] = $tmp; }
153 $page->assign('groupesx', $groupesx);
154
08cce2ff 155 $res = XDB::iterator(
e1406965
FB
156 'SELECT dom.id, dom.nom as domnom, groups.diminutif, groups.nom
157 FROM group_dom AS dom
158 INNER JOIN groups ON dom.id = groups.dom
159 WHERE FIND_IN_SET("Binets", dom.cat) AND FIND_IN_SET("Binets", groups.cat)
160 ORDER BY dom.nom, groups.nom');
71fe935c 161 $binets = array();
162 while ($tmp = $res->next()) { $binets[$tmp['id']][] = $tmp; }
163 $page->assign('binets', $binets);
164
08cce2ff 165 $res = XDB::iterator(
e1406965 166 'SELECT diminutif, nom
eb41eda9 167 FROM groups
71fe935c 168 WHERE cat LIKE "%Promotions%"
169 ORDER BY diminutif');
170 $page->assign('promos', $res);
171
08cce2ff 172 $res = XDB::iterator(
e1406965 173 'SELECT diminutif, nom
eb41eda9 174 FROM groups
71fe935c 175 WHERE FIND_IN_SET("Institutions", cat)
176 ORDER BY diminutif');
177 $page->assign('inst', $res);
045a1522 178 }
0e02493e 179
180 function handler_groups2(&$page)
181 {
5e2307dc 182 $this->handler_groups(&$page, Get::v('cat'), Get::v('dom'));
0e02493e 183 }
184
185 function handler_groups(&$page, $cat = null, $dom = null)
186 {
0e02493e 187 if (!$cat) {
188 $this->handler_index(&$page);
189 }
190
a953f7e7 191 $cat = mb_strtolower($cat);
0e02493e 192
193 $page->changeTpl('xnet/groupes.tpl');
194 $page->assign('cat', $cat);
195 $page->assign('dom', $dom);
196
00112b2e 197 $res = XDB::query("SELECT id,nom
eb41eda9 198 FROM group_dom
00112b2e
VZ
199 WHERE FIND_IN_SET({?}, cat)
200 ORDER BY nom", $cat);
0e02493e 201 $doms = $res->fetchAllAssoc();
202 $page->assign('doms', $doms);
203
204 if (empty($doms)) {
00112b2e 205 $res = XDB::query("SELECT diminutif, nom, site
eb41eda9 206 FROM groups
00112b2e
VZ
207 WHERE FIND_IN_SET({?}, cat)
208 ORDER BY nom", $cat);
f0430dc7 209 $page->assign('gps', $res->fetchAllAssoc());
0e02493e 210 } elseif (!is_null($dom)) {
00112b2e 211 $res = XDB::query("SELECT diminutif, nom, site
eb41eda9 212 FROM groups
00112b2e
VZ
213 WHERE FIND_IN_SET({?}, cat) AND dom={?}
214 ORDER BY nom", $cat, $dom);
f0430dc7 215 $page->assign('gps', $res->fetchAllAssoc());
0e02493e 216 }
0e02493e 217
0e02493e 218 $page->setType($cat);
219 }
eaf30d86 220
4a8a1e0a 221 function handler_autologin(&$page)
222 {
8c5c6d64 223 $allkeys = func_get_args();
224 unset($allkeys[0]);
225 $url = join('/',$allkeys);
3cb500d5 226 pl_content_headers("text/javascript");
8c5c6d64 227 echo '$.ajax({ url: "'.$url.'?forceXml=1", dataType: "xml", success: function(xml) { $("body",xml).insertBefore("body"); $("body:eq(1)").remove(); }});';
4a8a1e0a 228 exit;
229 }
bd4be95d 230}
231
a7de4ef7 232// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
bd4be95d 233?>