Adapts Xorg MLs and aliases handling to new mail chain.
[platal.git] / modules / xnetlists.php
CommitLineData
7b9d64a8 1<?php
2/***************************************************************************
12262f13 3 * Copyright (C) 2003-2011 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
26ba053e 57 function prepare_client($page, $user = null)
7b9d64a8 58 {
59 global $globals;
adb07f6f 60 Platal::load('lists', 'lists.inc.php');
7b9d64a8 61
7f1ff426
FB
62 if (is_null($user)) {
63 $user =& S::user();
64 }
65 $this->client = new MMList($user, $globals->asso('mail_domain'));
7b9d64a8 66
7b9d64a8 67 $page->assign('asso', $globals->asso());
68 $page->setType($globals->asso('cat'));
092945b4 69
70 return $globals->asso('mail_domain');
7b9d64a8 71 }
72
26ba053e 73 function handler_lists($page)
9fdacf8d 74 {
75 global $globals;
85ddf64f 76 require_once 'emails.inc.php';
9fdacf8d 77
c77e45f1 78 if (!$globals->asso('mail_domain')) {
79 return PL_NOT_FOUND;
80 }
9fdacf8d 81 $this->prepare_client($page);
1490093c 82 $page->changeTpl('xnetlists/index.tpl');
9fdacf8d 83
84 if (Get::has('del')) {
8d73663d 85 S::assert_xsrf_token();
5e2307dc 86 $this->client->unsubscribe(Get::v('del'));
8b00e0e0 87 pl_redirect('lists');
9fdacf8d 88 }
89 if (Get::has('add')) {
8d73663d 90 S::assert_xsrf_token();
5e2307dc 91 $this->client->subscribe(Get::v('add'));
8b00e0e0 92 pl_redirect('lists');
9fdacf8d 93 }
94
95 if (Post::has('del_alias') && may_update()) {
8d73663d
VZ
96 S::assert_xsrf_token();
97
85ddf64f
SJ
98 $alias = Post::t('del_alias');
99 list($local_part, ) = explode('@', $alias);
100 delete_list_alias($local_part, $globals->asso('mail_domain'));
101 $page->trigSuccess($alias . ' supprimé&nbsp;!');
9fdacf8d 102 }
103
104 $listes = $this->client->get_lists();
41c0150b 105 $page->assign('listes', $listes);
85ddf64f 106 $page->assign('aliases', iterate_list_alias($globals->asso('mail_domain')));
9fdacf8d 107 $page->assign('may_update', may_update());
41c0150b
SJ
108
109 if (count($listes) > 0 && !$globals->asso('has_ml')) {
eb41eda9 110 XDB::execute("UPDATE groups
41c0150b
SJ
111 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
112 WHERE id = {?}",
113 $globals->asso('id'));
114 }
9fdacf8d 115 }
116
26ba053e 117 function handler_create($page)
80f44cfe 118 {
119 global $globals;
120
c77e45f1 121 if (!$globals->asso('mail_domain')) {
122 return PL_NOT_FOUND;
123 }
80f44cfe 124 $this->prepare_client($page);
3fe218e4 125 $page->changeTpl('xnetlists/create.tpl');
80f44cfe 126
127 if (!Post::has('submit')) {
128 return;
8d73663d
VZ
129 } else {
130 S::assert_xsrf_token();
80f44cfe 131 }
132
85ddf64f 133 if (!Post::has('liste') || !Post::t('liste')) {
6bb2f79a 134 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
c9110c6c 135 return;
80f44cfe 136 }
137
85ddf64f
SJ
138 $list = strtolower(Post::t('liste'));
139 if (!preg_match("/^[a-zA-Z0-9\-]*$/", $list)) {
a7d35093 140 $page->trigError('le nom de la liste ne doit contenir que des lettres non accentuées, chiffres et tirets');
c9110c6c 141 return;
80f44cfe 142 }
143
85ddf64f
SJ
144 require_once 'emails.inc.php';
145 if (list_exist($list, $globals->asso('mail_domain'))) {
146 $page->trigError('Cet alias est déjà pris.');
c9110c6c 147 return;
80f44cfe 148 }
85ddf64f
SJ
149 if (!Post::t('desc')) {
150 $page->trigError('Le sujet est vide.');
c9110c6c 151 return;
80f44cfe 152 }
153
85ddf64f
SJ
154 $success = $this->client->create_list($list, utf8_decode(Post::t('desc')), Post::t('advertise'),
155 Post::t('modlevel'), Post::t('inslevel'),
156 array(S::user()->forlifeEmail()), array(S::user()->forlifeEmail()));
80f44cfe 157
85ddf64f 158 if (!$success) {
a7de4ef7 159 $page->kill("Un problème est survenu, contacter "
80f44cfe 160 ."<a href='mailto:support@m4x.org'>support@m4x.org</a>");
161 return;
162 }
85ddf64f 163 create_list($list, $globals->asso('mail_domain'));
41c0150b 164
eb41eda9 165 XDB::execute("UPDATE groups
41c0150b
SJ
166 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
167 WHERE id = {?}",
168 $globals->asso('id'));
169
85ddf64f 170 pl_redirect('lists/admin/' . $list);
80f44cfe 171 }
172
26ba053e 173 function handler_sync($page, $liste = null)
9fdacf8d 174 {
175 global $globals;
176
c77e45f1 177 if (!$globals->asso('mail_domain')) {
178 return PL_NOT_FOUND;
179 }
9fdacf8d 180 $this->prepare_client($page);
3fe218e4 181 $page->changeTpl('xnetlists/sync.tpl');
9fdacf8d 182
183 if (Env::has('add')) {
8d73663d 184 S::assert_xsrf_token();
5e2307dc 185 $this->client->mass_subscribe($liste, array_keys(Env::v('add')));
9fdacf8d 186 }
187
188 list(,$members) = $this->client->get_members($liste);
189 $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
cf5e8ef1 190 $subscribers = array_unique($mails);
9fdacf8d 191
832e6fcb 192 $ann = XDB::fetchColumn('SELECT uid
eb41eda9 193 FROM group_members
832e6fcb 194 WHERE asso_id = {?}', $globals->asso('id'));
ff82994d 195 $users = User::getBulkUsersWithUIDs($ann);
9fdacf8d 196
197 $not_in_list = array();
832e6fcb 198 foreach ($users as $user) {
cb8a8977
FB
199 if (!in_array(strtolower($user->forlifeEmail()), $subscribers)) {
200 $not_in_list[] = $user;
9fdacf8d 201 }
202 }
203
204 $page->assign('not_in_list', $not_in_list);
205 }
206
26ba053e 207 function handler_aadmin($page, $lfull = null)
d1fcf09c 208 {
c77e45f1 209 global $globals;
210
211 if (!$globals->asso('mail_domain') || is_null($lfull)) {
d1fcf09c 212 return PL_NOT_FOUND;
213 }
1490093c 214 $page->changeTpl('xnetlists/alias-admin.tpl');
d1fcf09c 215
85ddf64f
SJ
216 require_once 'emails.inc.php';
217 list($local_part, $domain) = explode('@', $lfull);
d1fcf09c 218 if (Env::has('add_member')) {
8d73663d
VZ
219 S::assert_xsrf_token();
220
85ddf64f
SJ
221 $email = Env::t('add_member');
222 $user = User::getSilent($email);
fd4ab3c7 223 if ($user) {
85ddf64f
SJ
224 add_to_list_alias($user, $local_part, $domain);
225 $page->trigSuccess($email . ' ajouté.');
d1fcf09c 226 } else {
85ddf64f 227 $page->trigError($email . " n'existe pas.");
d1fcf09c 228 }
229 }
230
231 if (Env::has('del_member')) {
8d73663d 232 S::assert_xsrf_token();
d1fcf09c 233
85ddf64f
SJ
234 $user = User::getSilent(Env::t('del_member'));
235 delete_from_list_alias($user, $local_part, $domain);
236 $page->trigSuccess($user->fullName() . ' supprimé.');
cb8a8977 237 }
85ddf64f
SJ
238
239 $page->assign('members', list_alias_members($local_part, $domain));
d1fcf09c 240 }
241
26ba053e 242 function handler_acreate($page)
d1fcf09c 243 {
244 global $globals;
245
c77e45f1 246 if (!$globals->asso('mail_domain')) {
247 return PL_NOT_FOUND;
248 }
1490093c 249 $page->changeTpl('xnetlists/alias-create.tpl');
d1fcf09c 250
251 if (!Post::has('submit')) {
252 return;
8d73663d
VZ
253 } else {
254 S::assert_xsrf_token();
d1fcf09c 255 }
256
257 if (!Post::has('liste')) {
6bb2f79a 258 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
d1fcf09c 259 return;
260 }
85ddf64f
SJ
261 $list = Post::v('liste');
262 if (!preg_match("/^[a-zA-Z0-9\-\.]*$/", $list)) {
263 $page->trigError('Le nom de l\'alias ne doit contenir que des lettres,'
264 .' chiffres, tirets et points.');
d1fcf09c 265 return;
266 }
267
85ddf64f
SJ
268 require_once 'emails.inc.php';
269 if (list_exist($list, $globals->asso('mail_domain'))) {
270 $page->trigError('Cet alias est déjà pris.');
d1fcf09c 271 return;
272 }
273
85ddf64f
SJ
274 add_to_list_alias(S::user(), $list, $globals->asso('mail_domain'));
275 pl_redirect('alias/admin/' . $list . '@' . $globals->asso('mail_domain'));
d1fcf09c 276 }
277
26ba053e 278 function handler_profile($page, $user = null)
7b9d64a8 279 {
8b00e0e0 280 http_redirect('https://www.polytechnique.org/profile/'.$user);
7b9d64a8 281 }
282}
283
a7de4ef7 284// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
7b9d64a8 285?>