Do not spamm support on xnet account creation.
[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());
cff1f1ad
SJ
108 if (S::suid()) {
109 $page->trigWarning("Attention&nbsp;: l'affichage des listes de diffusion ne tient pas compte de l'option « Voir le site comme&hellip; ».");
110 }
41c0150b
SJ
111
112 if (count($listes) > 0 && !$globals->asso('has_ml')) {
eb41eda9 113 XDB::execute("UPDATE groups
41c0150b
SJ
114 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
115 WHERE id = {?}",
116 $globals->asso('id'));
117 }
9fdacf8d 118 }
119
26ba053e 120 function handler_create($page)
80f44cfe 121 {
122 global $globals;
123
c77e45f1 124 if (!$globals->asso('mail_domain')) {
125 return PL_NOT_FOUND;
126 }
80f44cfe 127 $this->prepare_client($page);
3fe218e4 128 $page->changeTpl('xnetlists/create.tpl');
80f44cfe 129
130 if (!Post::has('submit')) {
131 return;
8d73663d
VZ
132 } else {
133 S::assert_xsrf_token();
80f44cfe 134 }
135
85ddf64f 136 if (!Post::has('liste') || !Post::t('liste')) {
6bb2f79a 137 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
c9110c6c 138 return;
80f44cfe 139 }
140
85ddf64f
SJ
141 $list = strtolower(Post::t('liste'));
142 if (!preg_match("/^[a-zA-Z0-9\-]*$/", $list)) {
a7d35093 143 $page->trigError('le nom de la liste ne doit contenir que des lettres non accentuées, chiffres et tirets');
c9110c6c 144 return;
80f44cfe 145 }
146
85ddf64f
SJ
147 require_once 'emails.inc.php';
148 if (list_exist($list, $globals->asso('mail_domain'))) {
149 $page->trigError('Cet alias est déjà pris.');
c9110c6c 150 return;
80f44cfe 151 }
85ddf64f
SJ
152 if (!Post::t('desc')) {
153 $page->trigError('Le sujet est vide.');
c9110c6c 154 return;
80f44cfe 155 }
156
85ddf64f
SJ
157 $success = $this->client->create_list($list, utf8_decode(Post::t('desc')), Post::t('advertise'),
158 Post::t('modlevel'), Post::t('inslevel'),
159 array(S::user()->forlifeEmail()), array(S::user()->forlifeEmail()));
80f44cfe 160
85ddf64f 161 if (!$success) {
a7de4ef7 162 $page->kill("Un problème est survenu, contacter "
80f44cfe 163 ."<a href='mailto:support@m4x.org'>support@m4x.org</a>");
164 return;
165 }
85ddf64f 166 create_list($list, $globals->asso('mail_domain'));
41c0150b 167
eb41eda9 168 XDB::execute("UPDATE groups
41c0150b
SJ
169 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
170 WHERE id = {?}",
171 $globals->asso('id'));
172
85ddf64f 173 pl_redirect('lists/admin/' . $list);
80f44cfe 174 }
175
26ba053e 176 function handler_sync($page, $liste = null)
9fdacf8d 177 {
178 global $globals;
179
c77e45f1 180 if (!$globals->asso('mail_domain')) {
181 return PL_NOT_FOUND;
182 }
9fdacf8d 183 $this->prepare_client($page);
3fe218e4 184 $page->changeTpl('xnetlists/sync.tpl');
9fdacf8d 185
186 if (Env::has('add')) {
8d73663d 187 S::assert_xsrf_token();
5e2307dc 188 $this->client->mass_subscribe($liste, array_keys(Env::v('add')));
9fdacf8d 189 }
190
191 list(,$members) = $this->client->get_members($liste);
192 $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
cf5e8ef1 193 $subscribers = array_unique($mails);
9fdacf8d 194
832e6fcb 195 $ann = XDB::fetchColumn('SELECT uid
eb41eda9 196 FROM group_members
832e6fcb 197 WHERE asso_id = {?}', $globals->asso('id'));
ff82994d 198 $users = User::getBulkUsersWithUIDs($ann);
9fdacf8d 199
200 $not_in_list = array();
832e6fcb 201 foreach ($users as $user) {
cb8a8977
FB
202 if (!in_array(strtolower($user->forlifeEmail()), $subscribers)) {
203 $not_in_list[] = $user;
9fdacf8d 204 }
205 }
206
207 $page->assign('not_in_list', $not_in_list);
208 }
209
26ba053e 210 function handler_aadmin($page, $lfull = null)
d1fcf09c 211 {
c77e45f1 212 global $globals;
213
214 if (!$globals->asso('mail_domain') || is_null($lfull)) {
d1fcf09c 215 return PL_NOT_FOUND;
216 }
1490093c 217 $page->changeTpl('xnetlists/alias-admin.tpl');
d1fcf09c 218
85ddf64f
SJ
219 require_once 'emails.inc.php';
220 list($local_part, $domain) = explode('@', $lfull);
a9cdd5fc
SJ
221 if ($globals->asso('mail_domain') != $domain || !preg_match("/^[a-zA-Z0-9\-\.]*$/", $local_part)) {
222 $page->trigErrorRedirect('Le nom de l\'alias est erroné.', $globals->asso('diminutif') . '/lists');
223 }
224
225
d1fcf09c 226 if (Env::has('add_member')) {
8d73663d
VZ
227 S::assert_xsrf_token();
228
7e82b545
SJ
229 if (add_to_list_alias(Env::t('add_member'), $local_part, $domain)) {
230 $page->trigSuccess('Ajout réussit.');
d1fcf09c 231 } else {
7e82b545 232 $page->trigError('Ajout infructueux.');
d1fcf09c 233 }
234 }
235
236 if (Env::has('del_member')) {
8d73663d 237 S::assert_xsrf_token();
d1fcf09c 238
7e82b545
SJ
239 if (delete_from_list_alias(Env::t('del_member'), $local_part, $domain)) {
240 $page->trigSuccess('Suppression réussie.');
241 } else {
242 $page->trigError('Suppression infructueuse.');
243 }
cb8a8977 244 }
85ddf64f
SJ
245
246 $page->assign('members', list_alias_members($local_part, $domain));
d1fcf09c 247 }
248
26ba053e 249 function handler_acreate($page)
d1fcf09c 250 {
251 global $globals;
252
c77e45f1 253 if (!$globals->asso('mail_domain')) {
254 return PL_NOT_FOUND;
255 }
1490093c 256 $page->changeTpl('xnetlists/alias-create.tpl');
d1fcf09c 257
258 if (!Post::has('submit')) {
259 return;
8d73663d
VZ
260 } else {
261 S::assert_xsrf_token();
d1fcf09c 262 }
263
264 if (!Post::has('liste')) {
6bb2f79a 265 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
d1fcf09c 266 return;
267 }
85ddf64f
SJ
268 $list = Post::v('liste');
269 if (!preg_match("/^[a-zA-Z0-9\-\.]*$/", $list)) {
270 $page->trigError('Le nom de l\'alias ne doit contenir que des lettres,'
271 .' chiffres, tirets et points.');
d1fcf09c 272 return;
273 }
274
85ddf64f
SJ
275 require_once 'emails.inc.php';
276 if (list_exist($list, $globals->asso('mail_domain'))) {
277 $page->trigError('Cet alias est déjà pris.');
d1fcf09c 278 return;
279 }
280
05978b09 281 add_to_list_alias(S::i('uid'), $list, $globals->asso('mail_domain'));
85ddf64f 282 pl_redirect('alias/admin/' . $list . '@' . $globals->asso('mail_domain'));
d1fcf09c 283 }
284
26ba053e 285 function handler_profile($page, $user = null)
7b9d64a8 286 {
8b00e0e0 287 http_redirect('https://www.polytechnique.org/profile/'.$user);
7b9d64a8 288 }
289}
290
a7de4ef7 291// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
7b9d64a8 292?>