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