Refactor lists.php/xnetlists.php.
[platal.git] / modules / xnetlists.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 function handlers()
27 {
28 return array(
29 '%grp/lists' => $this->make_hook('lists', AUTH_PASSWD, 'groupmember'),
30 '%grp/lists/create' => $this->make_hook('create', AUTH_PASSWD, 'groupmember'),
31
32 '%grp/lists/members' => $this->make_hook('members', AUTH_COOKIE, 'groups'),
33 '%grp/lists/csv' => $this->make_hook('csv', AUTH_COOKIE, 'groups'),
34 '%grp/lists/annu' => $this->make_hook('annu', AUTH_COOKIE, 'groups'),
35 '%grp/lists/archives' => $this->make_hook('archives', AUTH_COOKIE, 'groups'),
36 '%grp/lists/archives/rss' => $this->make_hook('rss', AUTH_PUBLIC),
37
38 '%grp/lists/moderate' => $this->make_hook('moderate', AUTH_PASSWD, 'groups'),
39 '%grp/lists/admin' => $this->make_hook('admin', AUTH_PASSWD, 'groups'),
40 '%grp/lists/options' => $this->make_hook('options', AUTH_PASSWD, 'groups'),
41 '%grp/lists/delete' => $this->make_hook('delete', AUTH_PASSWD, 'groups'),
42
43 '%grp/lists/soptions' => $this->make_hook('soptions', AUTH_PASSWD, 'groups'),
44 '%grp/lists/check' => $this->make_hook('check', AUTH_PASSWD, 'groups'),
45 '%grp/lists/sync' => $this->make_hook('sync', AUTH_PASSWD, 'groups'),
46
47 '%grp/alias/admin' => $this->make_hook('aadmin', AUTH_PASSWD, 'groupadmin'),
48 '%grp/alias/create' => $this->make_hook('acreate', AUTH_PASSWD, 'groupadmin'),
49
50 /* hack: lists uses that */
51 'profile' => $this->make_hook('profile', AUTH_PUBLIC),
52 );
53 }
54
55 protected function get_lists_domain()
56 {
57 global $globals;
58 return $globals->asso('mail_domain');
59 }
60
61 function handler_lists($page, $order_by = null, $order = null)
62 {
63 require_once 'emails.inc.php';
64
65 if (!$this->get_lists_domain()) {
66 return PL_NOT_FOUND;
67 }
68 $page->changeTpl('xnetlists/index.tpl');
69
70 if (Get::has('del')) {
71 S::assert_xsrf_token();
72 $mlist = $this->prepare_list(Get::v('del'));
73 $mlist->unsubscribe();
74 pl_redirect('lists');
75 }
76 if (Get::has('add')) {
77 S::assert_xsrf_token();
78 $mlist = $this->prepare_list(Get::v('add'));
79 $mlist->subscribe();
80 pl_redirect('lists');
81 }
82
83 if (Post::has('del_alias') && may_update()) {
84 S::assert_xsrf_token();
85
86 $alias = Post::t('del_alias');
87 list($local_part, ) = explode('@', $alias);
88 delete_list_alias($local_part, $this->get_lists_domain());
89 $page->trigSuccess($alias . ' supprimé&nbsp;!');
90 }
91
92 $client = $this->prepare_client();
93 $listes = $client->get_lists();
94 // Default ordering is by ascending names.
95 if (is_null($order_by) || is_null($order)
96 || !in_array($order_by, array('list', 'desc', 'nbsub'))
97 || !in_array($order, array('asc', 'desc'))) {
98 $order_by = 'list';
99 $order = 'asc';
100 }
101
102 $compare = function ($a, $b) use ($order_by, $order)
103 {
104 switch ($order_by) {
105 case 'desc':
106 $a[$order_by] = replace_accent($a[$order_by]);
107 $b[$order_by] = replace_accent($b[$order_by]);
108 case 'list':
109 $res = strcasecmp($a[$order_by], $b[$order_by]);
110 break;
111 case 'nbsub':
112 $res = $a[$order_by] - $b[$order_by];
113 break;
114 default:
115 $res = 0;
116 }
117
118 if ($order == 'asc') {
119 return $res;
120 }
121 return $res * -1;
122 };
123 usort($listes, $compare);
124 $page->assign('listes', $listes);
125 $page->assign('order_by', $order_by);
126 $page->assign('order', $order);
127 $page->assign('aliases', iterate_list_alias($this->get_lists_domain()));
128 $page->assign('may_update', may_update());
129 if (S::suid()) {
130 $page->trigWarning("Attention&nbsp;: l'affichage des listes de diffusion ne tient pas compte de l'option « Voir le site comme&hellip; ».");
131 }
132
133 global $globals;
134 if (count($listes) > 0 && !$globals->asso('has_ml')) {
135 XDB::execute("UPDATE groups
136 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
137 WHERE id = {?}",
138 $globals->asso('id'));
139 }
140 }
141
142 function handler_create($page)
143 {
144 if (!$this->get_lists_domain()) {
145 return PL_NOT_FOUND;
146 }
147 $page->changeTpl('xnetlists/create.tpl');
148
149 if (!Post::has('submit')) {
150 return;
151 } else {
152 S::assert_xsrf_token();
153 }
154
155 if (!Post::has('liste') || !Post::t('liste')) {
156 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
157 return;
158 }
159
160 $list = strtolower(Post::t('liste'));
161 if (!preg_match("/^[a-zA-Z0-9\-]*$/", $list)) {
162 $page->trigError('le nom de la liste ne doit contenir que des lettres non accentuées, chiffres et tirets');
163 return;
164 }
165
166 require_once 'emails.inc.php';
167 if (list_exist($list, $this->get_lists_domain())) {
168 $page->trigError('Cet alias est déjà pris.');
169 return;
170 }
171 if (!Post::t('desc')) {
172 $page->trigError('Le sujet est vide.');
173 return;
174 }
175
176 $mlist = $this->prepare_list($list);
177 $success = $mlist->create(Post::t('desc'),
178 Post::t('advertise'), Post::t('modlevel'), Post::t('inslevel'),
179 array(S::user()->forlifeEmail()), array(S::user()->forlifeEmail()));
180
181 if (!$success) {
182 $page->kill("Un problème est survenu, contacter "
183 ."<a href='mailto:support@m4x.org'>support@m4x.org</a>");
184 return;
185 }
186 create_list($mlist->mbox, $mlist->domain);
187
188 global $globals;
189 XDB::execute("UPDATE groups
190 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
191 WHERE id = {?}",
192 $globals->asso('id'));
193
194 pl_redirect('lists/admin/' . $list);
195 }
196
197 function handler_sync($page, $liste = null)
198 {
199 if (!$this->get_lists_domain()) {
200 return PL_NOT_FOUND;
201 }
202 if (!$liste) {
203 return PL_NOT_FOUND;
204 }
205
206 $page->changeTpl('xnetlists/sync.tpl');
207
208 $mlist = $this->prepare_list($liste);
209
210 if (Env::has('add')) {
211 S::assert_xsrf_token();
212 $mlist->subscribeBulk(array_keys(Env::v('add')));
213 }
214
215 list(,$members) = $mlist->getMembers();
216 $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
217 $subscribers = array_unique($mails);
218
219 global $globals;
220 $ann = XDB::fetchColumn('SELECT uid
221 FROM group_members
222 WHERE asso_id = {?}', $globals->asso('id'));
223 $users = User::getBulkUsersWithUIDs($ann);
224
225 $not_in_list = array();
226 foreach ($users as $user) {
227 if (!in_array(strtolower($user->forlifeEmail()), $subscribers)) {
228 $not_in_list[] = $user;
229 }
230 }
231
232 $page->assign('not_in_list', $not_in_list);
233 }
234
235 function handler_aadmin($page, $lfull = null)
236 {
237 if (!$this->get_lists_domain() || is_null($lfull)) {
238 return PL_NOT_FOUND;
239 }
240 $page->changeTpl('xnetlists/alias-admin.tpl');
241
242 require_once 'emails.inc.php';
243 list($local_part, $domain) = explode('@', $lfull);
244 if ($this->get_lists_domain() != $domain || !preg_match("/^[a-zA-Z0-9\-\.]*$/", $local_part)) {
245 global $globals;
246 $page->trigErrorRedirect('Le nom de l\'alias est erroné.', $globals->asso('diminutif') . '/lists');
247 }
248
249
250 if (Env::has('add_member')) {
251 S::assert_xsrf_token();
252
253 if (add_to_list_alias(Env::t('add_member'), $local_part, $domain)) {
254 $page->trigSuccess('Ajout réussi.');
255 } else {
256 $page->trigError('Ajout infructueux.');
257 }
258 }
259
260 if (Env::has('del_member')) {
261 S::assert_xsrf_token();
262
263 if (delete_from_list_alias(Env::t('del_member'), $local_part, $domain)) {
264 $page->trigSuccess('Suppression réussie.');
265 } else {
266 $page->trigError('Suppression infructueuse.');
267 }
268 }
269
270 $page->assign('members', list_alias_members($local_part, $domain));
271 }
272
273 function handler_acreate($page)
274 {
275 if (!$this->get_lists_domain()) {
276 return PL_NOT_FOUND;
277 }
278 $page->changeTpl('xnetlists/alias-create.tpl');
279
280 if (!Post::has('submit')) {
281 return;
282 } else {
283 S::assert_xsrf_token();
284 }
285
286 if (!Post::has('liste')) {
287 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
288 return;
289 }
290 $list = Post::v('liste');
291 if (!preg_match("/^[a-zA-Z0-9\-\.]*$/", $list)) {
292 $page->trigError('Le nom de l\'alias ne doit contenir que des lettres,'
293 .' chiffres, tirets et points.');
294 return;
295 }
296
297 require_once 'emails.inc.php';
298 $lists_domain = $this->get_lists_domain();
299 if (list_exist($list, $lists_domain)) {
300 $page->trigError('Cet alias est déjà pris.');
301 return;
302 }
303
304 add_to_list_alias(S::i('uid'), $list, $lists_domain);
305 pl_redirect('alias/admin/' . $list . '@' . $lists_domain);
306 }
307
308 function handler_profile($page, $user = null)
309 {
310 http_redirect('https://www.polytechnique.org/profile/'.$user);
311 }
312 }
313
314 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
315 ?>