Merge commit 'origin/fusionax' into account
[platal.git] / modules / xnetlists.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
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 Platal::load('lists');
23
24 class XnetListsModule extends ListsModule
25 {
26 var $client;
27
28 function handlers()
29 {
30 return array(
31 '%grp/lists' => $this->make_hook('lists', AUTH_MDP, 'groupmember'),
32 '%grp/lists/create' => $this->make_hook('create', AUTH_MDP, 'groupmember'),
33
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),
39
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),
44
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),
48
49 '%grp/alias/admin' => $this->make_hook('aadmin', AUTH_MDP, 'groupadmin'),
50 '%grp/alias/create' => $this->make_hook('acreate', AUTH_MDP, 'groupadmin'),
51
52 /* hack: lists uses that */
53 'profile' => $this->make_hook('profile', AUTH_PUBLIC),
54 );
55 }
56
57 function prepare_client(&$page, $user = null)
58 {
59 global $globals;
60 Platal::load('lists', 'lists.inc.php');
61
62 if (is_null($user)) {
63 $user =& S::user();
64 }
65 $this->client = new MMList($user, $globals->asso('mail_domain'));
66
67 $page->assign('asso', $globals->asso());
68 $page->setType($globals->asso('cat'));
69
70 return $globals->asso('mail_domain');
71 }
72
73 function handler_lists(&$page)
74 {
75 global $globals;
76
77 if (!$globals->asso('mail_domain')) {
78 return PL_NOT_FOUND;
79 }
80 $this->prepare_client($page);
81 $page->changeTpl('xnetlists/index.tpl');
82
83 if (Get::has('del')) {
84 S::assert_xsrf_token();
85 $this->client->unsubscribe(Get::v('del'));
86 pl_redirect('lists');
87 }
88 if (Get::has('add')) {
89 S::assert_xsrf_token();
90 $this->client->subscribe(Get::v('add'));
91 pl_redirect('lists');
92 }
93
94 if (Post::has('del_alias') && may_update()) {
95 S::assert_xsrf_token();
96
97 $alias = Post::v('del_alias');
98 // prevent group admin from erasing aliases from other groups
99 $alias = substr($alias, 0, strpos($alias, '@')).'@'.$globals->asso('mail_domain');
100 XDB::query(
101 'DELETE FROM r, v
102 USING x4dat.virtual AS v
103 LEFT JOIN x4dat.virtual_redirect AS r USING(vid)
104 WHERE v.alias={?}', $alias);
105 $page->trigSuccess(Post::v('del_alias')." supprimé !");
106 }
107
108 $listes = $this->client->get_lists();
109 $page->assign('listes', $listes);
110
111 $alias = XDB::iterator(
112 'SELECT alias,type
113 FROM x4dat.virtual
114 WHERE alias
115 LIKE {?} AND type="user"
116 ORDER BY alias', '%@'.$globals->asso('mail_domain'));
117 $page->assign('alias', $alias);
118
119 $page->assign('may_update', may_update());
120
121 if (count($listes) > 0 && !$globals->asso('has_ml')) {
122 XDB::execute("UPDATE groupex.asso
123 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
124 WHERE id = {?}",
125 $globals->asso('id'));
126 }
127 }
128
129 function handler_create(&$page)
130 {
131 global $globals;
132
133 if (!$globals->asso('mail_domain')) {
134 return PL_NOT_FOUND;
135 }
136 $this->prepare_client($page);
137 $page->changeTpl('xnetlists/create.tpl');
138
139 if (!Post::has('submit')) {
140 return;
141 } else {
142 S::assert_xsrf_token();
143 }
144
145 if (!Post::has('liste')) {
146 $page->trigError('champs «adresse souhaitée» vide');
147 return;
148 }
149
150 $liste = strtolower(Post::v('liste'));
151
152 if (!preg_match("/^[a-zA-Z0-9\-]*$/", $liste)) {
153 $page->trigError('le nom de la liste ne doit contenir que des lettres non accentuées, chiffres et tirets');
154 return;
155 }
156
157 $new = $liste.'@'.$globals->asso('mail_domain');
158 $res = XDB::query('SELECT alias FROM x4dat.virtual WHERE alias={?}', $new);
159
160 if ($res->numRows()) {
161 $page->trigError('cet alias est déjà pris');
162 return;
163 }
164 if (!Post::v('desc')) {
165 $page->trigError('le sujet est vide');
166 return;
167 }
168
169 $ret = $this->client->create_list(
170 $liste, utf8_decode(Post::v('desc')), Post::v('advertise'),
171 Post::v('modlevel'), Post::v('inslevel'),
172 array(S::user()->forlifeEmail()), array(S::user()->forlifeEmail()));
173
174 $dom = strtolower($globals->asso("mail_domain"));
175 $red = $dom.'_'.$liste;
176
177 if (!$ret) {
178 $page->kill("Un problème est survenu, contacter "
179 ."<a href='mailto:support@m4x.org'>support@m4x.org</a>");
180 return;
181 }
182 foreach (array('', 'owner', 'admin', 'bounces', 'unsubscribe') as $app) {
183 $mdir = $app == '' ? '+post' : '+' . $app;
184 if (!empty($app)) {
185 $app = '-' . $app;
186 }
187 XDB::execute('INSERT INTO x4dat.virtual (alias,type)
188 VALUES({?},{?})', $liste. $app . '@'.$dom, 'list');
189 XDB::execute('INSERT INTO x4dat.virtual_redirect (vid,redirect)
190 VALUES ({?}, {?})', XDB::insertId(),
191 $red . $mdir . '@listes.polytechnique.org');
192 }
193
194 XDB::execute("UPDATE groupex.asso
195 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
196 WHERE id = {?}",
197 $globals->asso('id'));
198
199 pl_redirect('lists/admin/'.$liste);
200 }
201
202 function handler_sync(&$page, $liste = null)
203 {
204 global $globals;
205
206 if (!$globals->asso('mail_domain')) {
207 return PL_NOT_FOUND;
208 }
209 $this->prepare_client($page);
210 $page->changeTpl('xnetlists/sync.tpl');
211
212 if (Env::has('add')) {
213 S::assert_xsrf_token();
214 $this->client->mass_subscribe($liste, array_keys(Env::v('add')));
215 }
216
217 list(,$members) = $this->client->get_members($liste);
218 $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
219 $subscribers = array_unique($mails);
220
221 $not_in_group_x = array();
222 $not_in_group_ext = array();
223
224 $ann = XDB::fetchColumn('SELECT uid
225 FROM groupex.membres
226 WHERE asso_id = {?}', $globals->asso('id'));
227 $users = User::getBuildUsersWithUIDs($ann, 'promo,full_name');
228 $not_in_list = array();
229 foreach ($users as $user) {
230 if (!in_array(strtolower($user->forlifeEmail()), $subscribers)) {
231 $not_in_list[] = $user;
232 }
233 }
234
235 $page->assign('not_in_list', $not_in_list);
236 }
237
238 function handler_aadmin(&$page, $lfull = null)
239 {
240 global $globals;
241
242 if (!$globals->asso('mail_domain') || is_null($lfull)) {
243 return PL_NOT_FOUND;
244 }
245 $page->changeTpl('xnetlists/alias-admin.tpl');
246
247 if (Env::has('add_member')) {
248 S::assert_xsrf_token();
249
250 $add = Env::t('add_member');
251 $user = User::getSilent($add);
252 if ($user) {
253 $add = $user->forlifeEmail();
254 } else if (!User::isForeignEmailAddress($add)) {
255 $add = null;
256 }
257 if (!empty($add)) {
258 XDB::execute('INSERT INTO x4dat.virtual_redirect (vid, redirect)
259 SELECT vid, {?},
260 FROM x4dat.virtual
261 WHERE alias = {?}', strtolower($add), $lfull);
262 $page->trigSuccess($add . ' ajouté.');
263 } else {
264 $page->trigError($add . ' n\'existe pas.');
265 }
266 }
267
268 if (Env::has('del_member')) {
269 S::assert_xsrf_token();
270 XDB::query(
271 "DELETE FROM x4dat.virtual_redirect
272 USING x4dat.virtual_redirect
273 INNER JOIN x4dat.virtual USING(vid)
274 WHERE redirect={?} AND alias={?}", Env::v('del_member'), $lfull);
275 pl_redirect('alias/admin/'.$lfull);
276 }
277
278 global $globals;
279 $emails = XDB::fetchColumn('SELECT redirect
280 FROM virtual_redirect AS vr
281 INNER JOIN virtual AS v USING(vid)
282 WHERE v.alias = {?}
283 ORDER BY redirect', $lfull);
284 $mem = array();
285 foreach ($emails as $email) {
286 $user = User::getSilent($email);
287 if ($user) {
288 $mem[] = array('user' => $user, 'email' => $email);
289 } else {
290 $mem[] = array('email' => $email);
291 }
292 }
293 $page->assign('mem', $mem);
294 }
295
296 function handler_acreate(&$page)
297 {
298 global $globals;
299
300 if (!$globals->asso('mail_domain')) {
301 return PL_NOT_FOUND;
302 }
303 $page->changeTpl('xnetlists/alias-create.tpl');
304
305 if (!Post::has('submit')) {
306 return;
307 } else {
308 S::assert_xsrf_token();
309 }
310
311 if (!Post::has('liste')) {
312 $page->trigError('champs «adresse souhaitée» vide');
313 return;
314 }
315 $liste = Post::v('liste');
316 if (!preg_match("/^[a-zA-Z0-9\-\.]*$/", $liste)) {
317 $page->trigError('le nom de l\'alias ne doit contenir que des lettres,'
318 .' chiffres, tirets et points');
319 return;
320 }
321
322 $new = $liste.'@'.$globals->asso('mail_domain');
323 $res = XDB::query('SELECT COUNT(*) FROM x4dat.virtual WHERE alias={?}', $new);
324 $n = $res->fetchOneCell();
325 if ($n) {
326 $page->trigError('cet alias est déjà pris');
327 return;
328 }
329
330 XDB::query('INSERT INTO x4dat.virtual (alias,type) VALUES({?}, "user")', $new);
331
332 pl_redirect("alias/admin/$new");
333 }
334
335 function handler_profile(&$page, $user = null)
336 {
337 http_redirect('https://www.polytechnique.org/profile/'.$user);
338 }
339 }
340
341 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
342 ?>