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