Moving to GitHub.
[platal.git] / modules / xnetlists.php
CommitLineData
7b9d64a8 1<?php
2/***************************************************************************
c441aabe 3 * Copyright (C) 2003-2014 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{
7b9d64a8 26 function handlers()
27 {
28 return array(
bfe9f4c7
SJ
29 '%grp/lists' => $this->make_hook('lists', AUTH_PASSWD, 'groupmember'),
30 '%grp/lists/create' => $this->make_hook('create', AUTH_PASSWD, 'groupmember'),
7b9d64a8 31
c92e5c6c
RB
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'),
020c8ed0 36 '%grp/lists/archives/rss' => $this->make_hook('rss', AUTH_PUBLIC),
7b9d64a8 37
bfe9f4c7
SJ
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'),
7b9d64a8 42
bfe9f4c7
SJ
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'),
7b9d64a8 46
bfe9f4c7
SJ
47 '%grp/alias/admin' => $this->make_hook('aadmin', AUTH_PASSWD, 'groupadmin'),
48 '%grp/alias/create' => $this->make_hook('acreate', AUTH_PASSWD, 'groupadmin'),
d1fcf09c 49
7b9d64a8 50 /* hack: lists uses that */
020c8ed0 51 'profile' => $this->make_hook('profile', AUTH_PUBLIC),
7b9d64a8 52 );
53 }
54
e0c9fb4d 55 protected function get_lists_domain()
7b9d64a8 56 {
57 global $globals;
092945b4 58 return $globals->asso('mail_domain');
7b9d64a8 59 }
60
0ab26293 61 function handler_lists($page, $order_by = null, $order = null)
9fdacf8d 62 {
85ddf64f 63 require_once 'emails.inc.php';
9fdacf8d 64
e0c9fb4d 65 if (!$this->get_lists_domain()) {
c77e45f1 66 return PL_NOT_FOUND;
67 }
1490093c 68 $page->changeTpl('xnetlists/index.tpl');
9fdacf8d 69
70 if (Get::has('del')) {
8d73663d 71 S::assert_xsrf_token();
e0c9fb4d
RB
72 $mlist = $this->prepare_list(Get::v('del'));
73 $mlist->unsubscribe();
8b00e0e0 74 pl_redirect('lists');
9fdacf8d 75 }
76 if (Get::has('add')) {
8d73663d 77 S::assert_xsrf_token();
e0c9fb4d
RB
78 $mlist = $this->prepare_list(Get::v('add'));
79 $mlist->subscribe();
8b00e0e0 80 pl_redirect('lists');
9fdacf8d 81 }
82
83 if (Post::has('del_alias') && may_update()) {
8d73663d
VZ
84 S::assert_xsrf_token();
85
85ddf64f
SJ
86 $alias = Post::t('del_alias');
87 list($local_part, ) = explode('@', $alias);
e0c9fb4d 88 delete_list_alias($local_part, $this->get_lists_domain());
85ddf64f 89 $page->trigSuccess($alias . ' supprimé&nbsp;!');
9fdacf8d 90 }
91
e0c9fb4d
RB
92 $client = $this->prepare_client();
93 $listes = $client->get_lists();
0ab26293
SJ
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);
41c0150b 124 $page->assign('listes', $listes);
0ab26293
SJ
125 $page->assign('order_by', $order_by);
126 $page->assign('order', $order);
e0c9fb4d 127 $page->assign('aliases', iterate_list_alias($this->get_lists_domain()));
9fdacf8d 128 $page->assign('may_update', may_update());
cff1f1ad
SJ
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 }
41c0150b 132
e0c9fb4d 133 global $globals;
41c0150b 134 if (count($listes) > 0 && !$globals->asso('has_ml')) {
eb41eda9 135 XDB::execute("UPDATE groups
41c0150b
SJ
136 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
137 WHERE id = {?}",
138 $globals->asso('id'));
139 }
9fdacf8d 140 }
141
26ba053e 142 function handler_create($page)
80f44cfe 143 {
e0c9fb4d 144 if (!$this->get_lists_domain()) {
c77e45f1 145 return PL_NOT_FOUND;
146 }
3fe218e4 147 $page->changeTpl('xnetlists/create.tpl');
80f44cfe 148
149 if (!Post::has('submit')) {
150 return;
8d73663d
VZ
151 } else {
152 S::assert_xsrf_token();
80f44cfe 153 }
154
85ddf64f 155 if (!Post::has('liste') || !Post::t('liste')) {
6bb2f79a 156 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
c9110c6c 157 return;
80f44cfe 158 }
159
85ddf64f
SJ
160 $list = strtolower(Post::t('liste'));
161 if (!preg_match("/^[a-zA-Z0-9\-]*$/", $list)) {
a7d35093 162 $page->trigError('le nom de la liste ne doit contenir que des lettres non accentuées, chiffres et tirets');
c9110c6c 163 return;
80f44cfe 164 }
165
85ddf64f 166 require_once 'emails.inc.php';
e0c9fb4d 167 if (list_exist($list, $this->get_lists_domain())) {
85ddf64f 168 $page->trigError('Cet alias est déjà pris.');
c9110c6c 169 return;
80f44cfe 170 }
85ddf64f
SJ
171 if (!Post::t('desc')) {
172 $page->trigError('Le sujet est vide.');
c9110c6c 173 return;
80f44cfe 174 }
175
e0c9fb4d 176 $mlist = $this->prepare_list($list);
8476b70a
RB
177 $success = MailingList::create($mlist->mbox, $mlist->domain, S::user(),
178 Post::t('desc'),
e0c9fb4d
RB
179 Post::t('advertise'), Post::t('modlevel'), Post::t('inslevel'),
180 array(S::user()->forlifeEmail()), array(S::user()->forlifeEmail()));
80f44cfe 181
85ddf64f 182 if (!$success) {
a7de4ef7 183 $page->kill("Un problème est survenu, contacter "
80f44cfe 184 ."<a href='mailto:support@m4x.org'>support@m4x.org</a>");
185 return;
186 }
e0c9fb4d 187 create_list($mlist->mbox, $mlist->domain);
41c0150b 188
e0c9fb4d 189 global $globals;
eb41eda9 190 XDB::execute("UPDATE groups
41c0150b
SJ
191 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
192 WHERE id = {?}",
193 $globals->asso('id'));
194
85ddf64f 195 pl_redirect('lists/admin/' . $list);
80f44cfe 196 }
197
26ba053e 198 function handler_sync($page, $liste = null)
9fdacf8d 199 {
e0c9fb4d
RB
200 if (!$this->get_lists_domain()) {
201 return PL_NOT_FOUND;
202 }
203 if (!$liste) {
c77e45f1 204 return PL_NOT_FOUND;
205 }
e0c9fb4d 206
3fe218e4 207 $page->changeTpl('xnetlists/sync.tpl');
9fdacf8d 208
e0c9fb4d
RB
209 $mlist = $this->prepare_list($liste);
210
9fdacf8d 211 if (Env::has('add')) {
8d73663d 212 S::assert_xsrf_token();
e0c9fb4d 213 $mlist->subscribeBulk(array_keys(Env::v('add')));
9fdacf8d 214 }
215
e0c9fb4d 216 list(,$members) = $mlist->getMembers();
9fdacf8d 217 $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
cf5e8ef1 218 $subscribers = array_unique($mails);
9fdacf8d 219
e0c9fb4d 220 global $globals;
832e6fcb 221 $ann = XDB::fetchColumn('SELECT uid
eb41eda9 222 FROM group_members
832e6fcb 223 WHERE asso_id = {?}', $globals->asso('id'));
ff82994d 224 $users = User::getBulkUsersWithUIDs($ann);
9fdacf8d 225
226 $not_in_list = array();
832e6fcb 227 foreach ($users as $user) {
cb8a8977
FB
228 if (!in_array(strtolower($user->forlifeEmail()), $subscribers)) {
229 $not_in_list[] = $user;
9fdacf8d 230 }
231 }
232
233 $page->assign('not_in_list', $not_in_list);
234 }
235
26ba053e 236 function handler_aadmin($page, $lfull = null)
d1fcf09c 237 {
e0c9fb4d 238 if (!$this->get_lists_domain() || is_null($lfull)) {
d1fcf09c 239 return PL_NOT_FOUND;
240 }
1490093c 241 $page->changeTpl('xnetlists/alias-admin.tpl');
d1fcf09c 242
85ddf64f
SJ
243 require_once 'emails.inc.php';
244 list($local_part, $domain) = explode('@', $lfull);
e0c9fb4d
RB
245 if ($this->get_lists_domain() != $domain || !preg_match("/^[a-zA-Z0-9\-\.]*$/", $local_part)) {
246 global $globals;
a9cdd5fc
SJ
247 $page->trigErrorRedirect('Le nom de l\'alias est erroné.', $globals->asso('diminutif') . '/lists');
248 }
249
250
d1fcf09c 251 if (Env::has('add_member')) {
8d73663d
VZ
252 S::assert_xsrf_token();
253
7e82b545 254 if (add_to_list_alias(Env::t('add_member'), $local_part, $domain)) {
41413228 255 $page->trigSuccess('Ajout réussi.');
d1fcf09c 256 } else {
7e82b545 257 $page->trigError('Ajout infructueux.');
d1fcf09c 258 }
259 }
260
261 if (Env::has('del_member')) {
8d73663d 262 S::assert_xsrf_token();
d1fcf09c 263
7e82b545
SJ
264 if (delete_from_list_alias(Env::t('del_member'), $local_part, $domain)) {
265 $page->trigSuccess('Suppression réussie.');
266 } else {
267 $page->trigError('Suppression infructueuse.');
268 }
cb8a8977 269 }
85ddf64f
SJ
270
271 $page->assign('members', list_alias_members($local_part, $domain));
d1fcf09c 272 }
273
26ba053e 274 function handler_acreate($page)
d1fcf09c 275 {
e0c9fb4d 276 if (!$this->get_lists_domain()) {
c77e45f1 277 return PL_NOT_FOUND;
278 }
1490093c 279 $page->changeTpl('xnetlists/alias-create.tpl');
d1fcf09c 280
281 if (!Post::has('submit')) {
282 return;
8d73663d
VZ
283 } else {
284 S::assert_xsrf_token();
d1fcf09c 285 }
286
287 if (!Post::has('liste')) {
6bb2f79a 288 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
d1fcf09c 289 return;
290 }
85ddf64f
SJ
291 $list = Post::v('liste');
292 if (!preg_match("/^[a-zA-Z0-9\-\.]*$/", $list)) {
293 $page->trigError('Le nom de l\'alias ne doit contenir que des lettres,'
294 .' chiffres, tirets et points.');
d1fcf09c 295 return;
296 }
297
85ddf64f 298 require_once 'emails.inc.php';
e0c9fb4d
RB
299 $lists_domain = $this->get_lists_domain();
300 if (list_exist($list, $lists_domain)) {
85ddf64f 301 $page->trigError('Cet alias est déjà pris.');
d1fcf09c 302 return;
303 }
304
e0c9fb4d
RB
305 add_to_list_alias(S::i('uid'), $list, $lists_domain);
306 pl_redirect('alias/admin/' . $list . '@' . $lists_domain);
d1fcf09c 307 }
308
26ba053e 309 function handler_profile($page, $user = null)
7b9d64a8 310 {
8b00e0e0 311 http_redirect('https://www.polytechnique.org/profile/'.$user);
7b9d64a8 312 }
313}
314
448c8cdc 315// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
7b9d64a8 316?>