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