7b9d64a8 |
1 | <?php |
2 | /*************************************************************************** |
5ddeb07c |
3 | * Copyright (C) 2003-2007 Polytechnique.org * |
7b9d64a8 |
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 | require_once dirname(__FILE__).'/lists.php'; |
23 | |
24 | class XnetListsModule extends ListsModule |
25 | { |
26 | var $client; |
27 | |
28 | function handlers() |
29 | { |
30 | return array( |
1490093c |
31 | '%grp/lists' => $this->make_hook('lists', AUTH_MDP, 'groupmember'), |
32 | '%grp/lists/create' => $this->make_hook('create', AUTH_MDP, 'groupmember'), |
7b9d64a8 |
33 | |
d1ebc57a |
34 | '%grp/lists/members' => $this->make_hook('members', AUTH_COOKIE), |
1cc0afe7 |
35 | '%grp/lists/annu' => $this->make_hook('annu', AUTH_COOKIE), |
d1ebc57a |
36 | '%grp/lists/archives' => $this->make_hook('archives', AUTH_COOKIE), |
7b9d64a8 |
37 | |
d1ebc57a |
38 | '%grp/lists/moderate' => $this->make_hook('moderate', AUTH_MDP), |
39 | '%grp/lists/admin' => $this->make_hook('admin', AUTH_MDP), |
40 | '%grp/lists/options' => $this->make_hook('options', AUTH_MDP), |
41 | '%grp/lists/delete' => $this->make_hook('delete', AUTH_MDP), |
7b9d64a8 |
42 | |
d1ebc57a |
43 | '%grp/lists/soptions' => $this->make_hook('soptions', AUTH_MDP), |
44 | '%grp/lists/check' => $this->make_hook('check', AUTH_MDP), |
45 | '%grp/lists/sync' => $this->make_hook('sync', AUTH_MDP), |
7b9d64a8 |
46 | |
c6c10b6b |
47 | '%grp/alias/admin' => $this->make_hook('aadmin', AUTH_MDP, 'groupadmin'), |
48 | '%grp/alias/create' => $this->make_hook('acreate', AUTH_MDP, 'groupadmin'), |
d1fcf09c |
49 | |
7b9d64a8 |
50 | /* hack: lists uses that */ |
51 | 'profile' => $this->make_hook('profile', AUTH_PUBLIC), |
52 | ); |
53 | } |
54 | |
55 | function prepare_client(&$page) |
56 | { |
57 | global $globals; |
58 | |
9bb8bf21 |
59 | require_once dirname(__FILE__).'/lists/lists.inc.php'; |
7b9d64a8 |
60 | |
9bb8bf21 |
61 | $this->client = new MMList(S::v('uid'), S::v('password'), |
62 | $globals->asso('mail_domain')); |
7b9d64a8 |
63 | |
7b9d64a8 |
64 | $page->assign('asso', $globals->asso()); |
65 | $page->setType($globals->asso('cat')); |
092945b4 |
66 | |
67 | return $globals->asso('mail_domain'); |
7b9d64a8 |
68 | } |
69 | |
9fdacf8d |
70 | function handler_lists(&$page) |
71 | { |
72 | global $globals; |
73 | |
c77e45f1 |
74 | if (!$globals->asso('mail_domain')) { |
75 | return PL_NOT_FOUND; |
76 | } |
9fdacf8d |
77 | $this->prepare_client($page); |
1490093c |
78 | $page->changeTpl('xnetlists/index.tpl'); |
9fdacf8d |
79 | |
80 | if (Get::has('del')) { |
5e2307dc |
81 | $this->client->unsubscribe(Get::v('del')); |
8b00e0e0 |
82 | pl_redirect('lists'); |
9fdacf8d |
83 | } |
84 | if (Get::has('add')) { |
5e2307dc |
85 | $this->client->subscribe(Get::v('add')); |
8b00e0e0 |
86 | pl_redirect('lists'); |
9fdacf8d |
87 | } |
88 | |
89 | if (Post::has('del_alias') && may_update()) { |
5e2307dc |
90 | $alias = Post::v('del_alias'); |
9fdacf8d |
91 | // prevent group admin from erasing aliases from other groups |
92 | $alias = substr($alias, 0, strpos($alias, '@')).'@'.$globals->asso('mail_domain'); |
08cce2ff |
93 | XDB::query( |
9fdacf8d |
94 | 'DELETE FROM x4dat.virtual_redirect, x4dat.virtual |
95 | USING x4dat.virtual AS v |
96 | LEFT JOIN x4dat.virtual_redirect USING(vid) |
97 | WHERE v.alias={?}', $alias); |
a7de4ef7 |
98 | $page->trig(Post::v('del_alias')." supprimé !"); |
9fdacf8d |
99 | } |
100 | |
101 | $listes = $this->client->get_lists(); |
102 | $page->assign('listes',$listes); |
103 | |
08cce2ff |
104 | $alias = XDB::iterator( |
9fdacf8d |
105 | 'SELECT alias,type |
106 | FROM x4dat.virtual |
107 | WHERE alias |
108 | LIKE {?} AND type="user" |
109 | ORDER BY alias', '%@'.$globals->asso('mail_domain')); |
110 | $page->assign('alias', $alias); |
111 | |
112 | $page->assign('may_update', may_update()); |
113 | } |
114 | |
80f44cfe |
115 | function handler_create(&$page) |
116 | { |
117 | global $globals; |
118 | |
c77e45f1 |
119 | if (!$globals->asso('mail_domain')) { |
120 | return PL_NOT_FOUND; |
121 | } |
80f44cfe |
122 | $this->prepare_client($page); |
3fe218e4 |
123 | $page->changeTpl('xnetlists/create.tpl'); |
80f44cfe |
124 | |
125 | if (!Post::has('submit')) { |
126 | return; |
127 | } |
128 | |
129 | if (!Post::has('liste')) { |
a7de4ef7 |
130 | $page->trig('champs «addresse souhaitée» vide'); |
c9110c6c |
131 | return; |
80f44cfe |
132 | } |
133 | |
88f7a3f1 |
134 | $liste = strtolower(Post::v('liste')); |
80f44cfe |
135 | |
136 | if (!preg_match("/^[a-zA-Z0-9\-]*$/", $liste)) { |
1b5b2b79 |
137 | $page->trig('le nom de la liste ne doit contenir que des lettres non accentuées, chiffres et tirets'); |
c9110c6c |
138 | return; |
80f44cfe |
139 | } |
140 | |
141 | $new = $liste.'@'.$globals->asso('mail_domain'); |
08cce2ff |
142 | $res = XDB::query('SELECT COUNT(*) FROM x4dat.virtual WHERE alias={?}', $new); |
80f44cfe |
143 | $n = $res->fetchOneCell(); |
144 | |
c9110c6c |
145 | if ($n) { |
a7de4ef7 |
146 | $page->trig('cet alias est déjà pris'); |
c9110c6c |
147 | return; |
80f44cfe |
148 | } |
5e2307dc |
149 | if (!Post::v('desc')) { |
c9110c6c |
150 | $page->trig('le sujet est vide'); |
151 | return; |
80f44cfe |
152 | } |
153 | |
80f44cfe |
154 | $ret = $this->client->create_list( |
5e2307dc |
155 | $liste, Post::v('desc'), Post::v('advertise'), |
156 | Post::v('modlevel'), Post::v('inslevel'), |
cab08090 |
157 | array(S::v('forlife')), array()); |
80f44cfe |
158 | |
159 | $dom = strtolower($globals->asso("mail_domain")); |
160 | $red = $dom.'_'.$liste; |
161 | |
162 | if (!$ret) { |
a7de4ef7 |
163 | $page->kill("Un problème est survenu, contacter " |
80f44cfe |
164 | ."<a href='mailto:support@m4x.org'>support@m4x.org</a>"); |
165 | return; |
166 | } |
08cce2ff |
167 | XDB::execute('INSERT INTO x4dat.virtual (alias,type) |
80f44cfe |
168 | VALUES({?},{?})', $liste.'@'.$dom, 'list'); |
08cce2ff |
169 | XDB::execute('INSERT INTO x4dat.virtual_redirect (vid,redirect) |
8b83a166 |
170 | VALUES ({?}, {?})', XDB::insertId(), |
80f44cfe |
171 | "$red+post@listes.polytechnique.org"); |
08cce2ff |
172 | XDB::execute('INSERT INTO x4dat.virtual (alias,type) |
80f44cfe |
173 | VALUES({?},{?})', $liste.'-owner@'.$dom, 'list'); |
08cce2ff |
174 | XDB::execute('INSERT INTO x4dat.virtual_redirect (vid,redirect) |
8b83a166 |
175 | VALUES ({?}, {?})', XDB::insertId(), |
80f44cfe |
176 | "$red+owner@listes.polytechnique.org"); |
08cce2ff |
177 | XDB::execute('INSERT INTO x4dat.virtual (alias,type) |
80f44cfe |
178 | VALUES({?},{?})', $liste.'-admin@'.$dom, 'list'); |
08cce2ff |
179 | XDB::execute('INSERT INTO x4dat.virtual_redirect (vid,redirect) |
8b83a166 |
180 | VALUES ({?}, {?})', XDB::insertId(), |
80f44cfe |
181 | "$red+admin@listes.polytechnique.org"); |
08cce2ff |
182 | XDB::execute('INSERT INTO x4dat.virtual (alias,type) |
80f44cfe |
183 | VALUES({?},{?})', $liste.'-bounces@'.$dom, 'list'); |
08cce2ff |
184 | XDB::execute('INSERT INTO x4dat.virtual_redirect (vid,redirect) |
8b83a166 |
185 | VALUES ({?}, {?})', XDB::insertId(), |
80f44cfe |
186 | "$red+bounces@listes.polytechnique.org"); |
187 | |
8b00e0e0 |
188 | pl_redirect('lists/admin/'.$liste); |
80f44cfe |
189 | } |
190 | |
9fdacf8d |
191 | function handler_sync(&$page, $liste = null) |
192 | { |
193 | global $globals; |
194 | |
c77e45f1 |
195 | if (!$globals->asso('mail_domain')) { |
196 | return PL_NOT_FOUND; |
197 | } |
9fdacf8d |
198 | $this->prepare_client($page); |
3fe218e4 |
199 | $page->changeTpl('xnetlists/sync.tpl'); |
9fdacf8d |
200 | |
201 | if (Env::has('add')) { |
5e2307dc |
202 | $this->client->mass_subscribe($liste, array_keys(Env::v('add'))); |
9fdacf8d |
203 | } |
204 | |
205 | list(,$members) = $this->client->get_members($liste); |
206 | $mails = array_map(create_function('$arr', 'return $arr[1];'), $members); |
cf5e8ef1 |
207 | $subscribers = array_unique($mails); |
9fdacf8d |
208 | |
209 | $not_in_group_x = array(); |
210 | $not_in_group_ext = array(); |
211 | |
08cce2ff |
212 | $ann = XDB::iterator( |
c9110c6c |
213 | "SELECT if (m.origine='X',if (u.nom_usage<>'', u.nom_usage, u.nom) ,m.nom) AS nom, |
214 | if (m.origine='X',u.prenom,m.prenom) AS prenom, |
a7de4ef7 |
215 | if (m.origine='X',u.promo,'extérieur') AS promo, |
c9110c6c |
216 | if (m.origine='X',CONCAT(a.alias, '@polytechnique.org'),m.email) AS email, |
217 | if (m.origine='X',FIND_IN_SET('femme', u.flags),0) AS femme, |
9fdacf8d |
218 | m.perms='admin' AS admin, |
219 | m.origine='X' AS x |
220 | FROM groupex.membres AS m |
221 | LEFT JOIN auth_user_md5 AS u ON ( u.user_id = m.uid ) |
222 | LEFT JOIN aliases AS a ON ( a.id = m.uid AND a.type='a_vie' ) |
1bd2bc7e |
223 | WHERE m.asso_id = {?} |
224 | ORDER BY promo, nom, prenom", $globals->asso('id')); |
9fdacf8d |
225 | |
226 | $not_in_list = array(); |
227 | |
228 | while ($tmp = $ann->next()) { |
cf5e8ef1 |
229 | if (!in_array(strtolower($tmp['email']), $subscribers)) { |
9fdacf8d |
230 | $not_in_list[] = $tmp; |
231 | } |
232 | } |
233 | |
234 | $page->assign('not_in_list', $not_in_list); |
235 | } |
236 | |
d1fcf09c |
237 | function handler_aadmin(&$page, $lfull = null) |
238 | { |
c77e45f1 |
239 | global $globals; |
240 | |
241 | if (!$globals->asso('mail_domain') || is_null($lfull)) { |
d1fcf09c |
242 | return PL_NOT_FOUND; |
243 | } |
1490093c |
244 | $page->changeTpl('xnetlists/alias-admin.tpl'); |
d1fcf09c |
245 | |
246 | if (Env::has('add_member')) { |
5e2307dc |
247 | $add = Env::v('add_member'); |
d1fcf09c |
248 | if (strstr($add, '@')) { |
249 | list($mbox,$dom) = explode('@', strtolower($add)); |
250 | } else { |
251 | $mbox = $add; |
252 | $dom = 'm4x.org'; |
253 | } |
c9110c6c |
254 | if ($dom == 'polytechnique.org' || $dom == 'm4x.org') { |
08cce2ff |
255 | $res = XDB::query( |
d1fcf09c |
256 | "SELECT a.alias, b.alias |
257 | FROM x4dat.aliases AS a |
258 | LEFT JOIN x4dat.aliases AS b ON (a.id=b.id AND b.type = 'a_vie') |
259 | WHERE a.alias={?} AND a.type!='homonyme'", $mbox); |
260 | if (list($alias, $blias) = $res->fetchOneRow()) { |
261 | $alias = empty($blias) ? $alias : $blias; |
08cce2ff |
262 | XDB::query( |
d1fcf09c |
263 | "INSERT INTO x4dat.virtual_redirect (vid,redirect) |
264 | SELECT vid, {?} |
265 | FROM x4dat.virtual |
266 | WHERE alias={?}", "$alias@m4x.org", $lfull); |
a7de4ef7 |
267 | $page->trig("$alias@m4x.org ajouté"); |
d1fcf09c |
268 | } else { |
269 | $page->trig("$mbox@polytechnique.org n'existe pas."); |
270 | } |
271 | } else { |
08cce2ff |
272 | XDB::query( |
d1fcf09c |
273 | "INSERT INTO x4dat.virtual_redirect (vid,redirect) |
274 | SELECT vid,{?} |
275 | FROM x4dat.virtual |
276 | WHERE alias={?}", "$mbox@$dom", $lfull); |
a7de4ef7 |
277 | $page->trig("$mbox@$dom ajouté"); |
d1fcf09c |
278 | } |
279 | } |
280 | |
281 | if (Env::has('del_member')) { |
08cce2ff |
282 | XDB::query( |
d1fcf09c |
283 | "DELETE FROM x4dat.virtual_redirect |
284 | USING x4dat.virtual_redirect |
285 | INNER JOIN x4dat.virtual USING(vid) |
5e2307dc |
286 | WHERE redirect={?} AND alias={?}", Env::v('del_member'), $lfull); |
8b00e0e0 |
287 | pl_redirect('alias/admin/'.$lfull); |
d1fcf09c |
288 | } |
289 | |
7cdecf88 |
290 | global $globals; |
08cce2ff |
291 | $res = XDB::iterator( |
7cdecf88 |
292 | "SELECT redirect, |
293 | IF(u.nom IS NOT NULL, IF(u.nom_usage<>'', u.nom_usage, u.nom), m.nom) AS nom, |
294 | IF(u.prenom IS NOT NULL, u.prenom, m.prenom) AS prenom, |
a7de4ef7 |
295 | IF(u.promo IS NOT NULL, u.promo, 'extérieur') AS promo, |
7cdecf88 |
296 | IF(m2.perms, m2.perms = 'admin', m.perms = 'admin') AS admin, |
297 | a.alias |
d1fcf09c |
298 | FROM x4dat.virtual_redirect AS vr |
299 | INNER JOIN x4dat.virtual AS v USING(vid) |
3ad44e08 |
300 | LEFT JOIN x4dat.aliases AS a ON(vr.redirect = CONCAT(a.alias, '@', {?}) |
301 | OR vr.redirect = CONCAT(a.alias, '@', {?})) |
7cdecf88 |
302 | LEFT JOIN x4dat.auth_user_md5 AS u ON(a.id = u.user_id) |
303 | LEFT JOIN groupex.membres AS m2 ON(u.user_id = m2.uid AND m2.asso_id = {?}) |
304 | LEFT JOIN groupex.membres AS m ON(m.email = vr.redirect AND m.asso_id = {?}) |
d1fcf09c |
305 | WHERE v.alias={?} |
7cdecf88 |
306 | ORDER BY redirect", |
307 | $globals->mail->domain, $globals->mail->domain2, |
308 | $globals->asso('id'), $globals->asso('id'), $lfull); |
d1fcf09c |
309 | $page->assign('mem', $res); |
310 | } |
311 | |
312 | function handler_acreate(&$page) |
313 | { |
314 | global $globals; |
315 | |
c77e45f1 |
316 | if (!$globals->asso('mail_domain')) { |
317 | return PL_NOT_FOUND; |
318 | } |
1490093c |
319 | $page->changeTpl('xnetlists/alias-create.tpl'); |
d1fcf09c |
320 | |
321 | if (!Post::has('submit')) { |
322 | return; |
323 | } |
324 | |
325 | if (!Post::has('liste')) { |
a7de4ef7 |
326 | $page->trig('champs «addresse souhaitée» vide'); |
d1fcf09c |
327 | return; |
328 | } |
5e2307dc |
329 | $liste = Post::v('liste'); |
d1fcf09c |
330 | if (!preg_match("/^[a-zA-Z0-9\-\.]*$/", $liste)) { |
331 | $page->trig('le nom de l\'alias ne doit contenir que des lettres,' |
332 | .' chiffres, tirets et points'); |
333 | return; |
334 | } |
335 | |
336 | $new = $liste.'@'.$globals->asso('mail_domain'); |
08cce2ff |
337 | $res = XDB::query('SELECT COUNT(*) FROM x4dat.virtual WHERE alias={?}', $new); |
d1fcf09c |
338 | $n = $res->fetchOneCell(); |
c9110c6c |
339 | if ($n) { |
a7de4ef7 |
340 | $page->trig('cet alias est déjà pris'); |
d1fcf09c |
341 | return; |
342 | } |
343 | |
08cce2ff |
344 | XDB::query('INSERT INTO x4dat.virtual (alias,type) VALUES({?}, "user")', $new); |
d1fcf09c |
345 | |
8b00e0e0 |
346 | pl_redirect("alias/admin/$new"); |
d1fcf09c |
347 | } |
348 | |
7b9d64a8 |
349 | function handler_profile(&$page, $user = null) |
350 | { |
8b00e0e0 |
351 | http_redirect('https://www.polytechnique.org/profile/'.$user); |
7b9d64a8 |
352 | } |
353 | } |
354 | |
a7de4ef7 |
355 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
7b9d64a8 |
356 | ?> |