correction d'une faute d'orthographe dans la confirmation de l'ajout à un alias
[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(
bfe9f4c7
SJ
31 '%grp/lists' => $this->make_hook('lists', AUTH_PASSWD, 'groupmember'),
32 '%grp/lists/create' => $this->make_hook('create', AUTH_PASSWD, 'groupmember'),
7b9d64a8 33
c92e5c6c
RB
34 '%grp/lists/members' => $this->make_hook('members', AUTH_COOKIE, 'groups'),
35 '%grp/lists/csv' => $this->make_hook('csv', AUTH_COOKIE, 'groups'),
36 '%grp/lists/annu' => $this->make_hook('annu', AUTH_COOKIE, 'groups'),
37 '%grp/lists/archives' => $this->make_hook('archives', AUTH_COOKIE, 'groups'),
020c8ed0 38 '%grp/lists/archives/rss' => $this->make_hook('rss', AUTH_PUBLIC),
7b9d64a8 39
bfe9f4c7
SJ
40 '%grp/lists/moderate' => $this->make_hook('moderate', AUTH_PASSWD, 'groups'),
41 '%grp/lists/admin' => $this->make_hook('admin', AUTH_PASSWD, 'groups'),
42 '%grp/lists/options' => $this->make_hook('options', AUTH_PASSWD, 'groups'),
43 '%grp/lists/delete' => $this->make_hook('delete', AUTH_PASSWD, 'groups'),
7b9d64a8 44
bfe9f4c7
SJ
45 '%grp/lists/soptions' => $this->make_hook('soptions', AUTH_PASSWD, 'groups'),
46 '%grp/lists/check' => $this->make_hook('check', AUTH_PASSWD, 'groups'),
47 '%grp/lists/sync' => $this->make_hook('sync', AUTH_PASSWD, 'groups'),
7b9d64a8 48
bfe9f4c7
SJ
49 '%grp/alias/admin' => $this->make_hook('aadmin', AUTH_PASSWD, 'groupadmin'),
50 '%grp/alias/create' => $this->make_hook('acreate', AUTH_PASSWD, '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
0ab26293 73 function handler_lists($page, $order_by = null, $order = null)
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();
0ab26293
SJ
105 // Default ordering is by ascending names.
106 if (is_null($order_by) || is_null($order)
107 || !in_array($order_by, array('list', 'desc', 'nbsub'))
108 || !in_array($order, array('asc', 'desc'))) {
109 $order_by = 'list';
110 $order = 'asc';
111 }
112
113 $compare = function ($a, $b) use ($order_by, $order)
114 {
115 switch ($order_by) {
116 case 'desc':
117 $a[$order_by] = replace_accent($a[$order_by]);
118 $b[$order_by] = replace_accent($b[$order_by]);
119 case 'list':
120 $res = strcasecmp($a[$order_by], $b[$order_by]);
121 break;
122 case 'nbsub':
123 $res = $a[$order_by] - $b[$order_by];
124 break;
125 default:
126 $res = 0;
127 }
128
129 if ($order == 'asc') {
130 return $res;
131 }
132 return $res * -1;
133 };
134 usort($listes, $compare);
41c0150b 135 $page->assign('listes', $listes);
0ab26293
SJ
136 $page->assign('order_by', $order_by);
137 $page->assign('order', $order);
85ddf64f 138 $page->assign('aliases', iterate_list_alias($globals->asso('mail_domain')));
9fdacf8d 139 $page->assign('may_update', may_update());
cff1f1ad
SJ
140 if (S::suid()) {
141 $page->trigWarning("Attention&nbsp;: l'affichage des listes de diffusion ne tient pas compte de l'option « Voir le site comme&hellip; ».");
142 }
41c0150b
SJ
143
144 if (count($listes) > 0 && !$globals->asso('has_ml')) {
eb41eda9 145 XDB::execute("UPDATE groups
41c0150b
SJ
146 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
147 WHERE id = {?}",
148 $globals->asso('id'));
149 }
9fdacf8d 150 }
151
26ba053e 152 function handler_create($page)
80f44cfe 153 {
154 global $globals;
155
c77e45f1 156 if (!$globals->asso('mail_domain')) {
157 return PL_NOT_FOUND;
158 }
80f44cfe 159 $this->prepare_client($page);
3fe218e4 160 $page->changeTpl('xnetlists/create.tpl');
80f44cfe 161
162 if (!Post::has('submit')) {
163 return;
8d73663d
VZ
164 } else {
165 S::assert_xsrf_token();
80f44cfe 166 }
167
85ddf64f 168 if (!Post::has('liste') || !Post::t('liste')) {
6bb2f79a 169 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
c9110c6c 170 return;
80f44cfe 171 }
172
85ddf64f
SJ
173 $list = strtolower(Post::t('liste'));
174 if (!preg_match("/^[a-zA-Z0-9\-]*$/", $list)) {
a7d35093 175 $page->trigError('le nom de la liste ne doit contenir que des lettres non accentuées, chiffres et tirets');
c9110c6c 176 return;
80f44cfe 177 }
178
85ddf64f
SJ
179 require_once 'emails.inc.php';
180 if (list_exist($list, $globals->asso('mail_domain'))) {
181 $page->trigError('Cet alias est déjà pris.');
c9110c6c 182 return;
80f44cfe 183 }
85ddf64f
SJ
184 if (!Post::t('desc')) {
185 $page->trigError('Le sujet est vide.');
c9110c6c 186 return;
80f44cfe 187 }
188
85ddf64f
SJ
189 $success = $this->client->create_list($list, utf8_decode(Post::t('desc')), Post::t('advertise'),
190 Post::t('modlevel'), Post::t('inslevel'),
191 array(S::user()->forlifeEmail()), array(S::user()->forlifeEmail()));
80f44cfe 192
85ddf64f 193 if (!$success) {
a7de4ef7 194 $page->kill("Un problème est survenu, contacter "
80f44cfe 195 ."<a href='mailto:support@m4x.org'>support@m4x.org</a>");
196 return;
197 }
85ddf64f 198 create_list($list, $globals->asso('mail_domain'));
41c0150b 199
eb41eda9 200 XDB::execute("UPDATE groups
41c0150b
SJ
201 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
202 WHERE id = {?}",
203 $globals->asso('id'));
204
85ddf64f 205 pl_redirect('lists/admin/' . $list);
80f44cfe 206 }
207
26ba053e 208 function handler_sync($page, $liste = null)
9fdacf8d 209 {
210 global $globals;
211
c77e45f1 212 if (!$globals->asso('mail_domain')) {
213 return PL_NOT_FOUND;
214 }
9fdacf8d 215 $this->prepare_client($page);
3fe218e4 216 $page->changeTpl('xnetlists/sync.tpl');
9fdacf8d 217
218 if (Env::has('add')) {
8d73663d 219 S::assert_xsrf_token();
5e2307dc 220 $this->client->mass_subscribe($liste, array_keys(Env::v('add')));
9fdacf8d 221 }
222
223 list(,$members) = $this->client->get_members($liste);
224 $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
cf5e8ef1 225 $subscribers = array_unique($mails);
9fdacf8d 226
832e6fcb 227 $ann = XDB::fetchColumn('SELECT uid
eb41eda9 228 FROM group_members
832e6fcb 229 WHERE asso_id = {?}', $globals->asso('id'));
ff82994d 230 $users = User::getBulkUsersWithUIDs($ann);
9fdacf8d 231
232 $not_in_list = array();
832e6fcb 233 foreach ($users as $user) {
cb8a8977
FB
234 if (!in_array(strtolower($user->forlifeEmail()), $subscribers)) {
235 $not_in_list[] = $user;
9fdacf8d 236 }
237 }
238
239 $page->assign('not_in_list', $not_in_list);
240 }
241
26ba053e 242 function handler_aadmin($page, $lfull = null)
d1fcf09c 243 {
c77e45f1 244 global $globals;
245
246 if (!$globals->asso('mail_domain') || is_null($lfull)) {
d1fcf09c 247 return PL_NOT_FOUND;
248 }
1490093c 249 $page->changeTpl('xnetlists/alias-admin.tpl');
d1fcf09c 250
85ddf64f
SJ
251 require_once 'emails.inc.php';
252 list($local_part, $domain) = explode('@', $lfull);
a9cdd5fc
SJ
253 if ($globals->asso('mail_domain') != $domain || !preg_match("/^[a-zA-Z0-9\-\.]*$/", $local_part)) {
254 $page->trigErrorRedirect('Le nom de l\'alias est erroné.', $globals->asso('diminutif') . '/lists');
255 }
256
257
d1fcf09c 258 if (Env::has('add_member')) {
8d73663d
VZ
259 S::assert_xsrf_token();
260
7e82b545 261 if (add_to_list_alias(Env::t('add_member'), $local_part, $domain)) {
41413228 262 $page->trigSuccess('Ajout réussi.');
d1fcf09c 263 } else {
7e82b545 264 $page->trigError('Ajout infructueux.');
d1fcf09c 265 }
266 }
267
268 if (Env::has('del_member')) {
8d73663d 269 S::assert_xsrf_token();
d1fcf09c 270
7e82b545
SJ
271 if (delete_from_list_alias(Env::t('del_member'), $local_part, $domain)) {
272 $page->trigSuccess('Suppression réussie.');
273 } else {
274 $page->trigError('Suppression infructueuse.');
275 }
cb8a8977 276 }
85ddf64f
SJ
277
278 $page->assign('members', list_alias_members($local_part, $domain));
d1fcf09c 279 }
280
26ba053e 281 function handler_acreate($page)
d1fcf09c 282 {
283 global $globals;
284
c77e45f1 285 if (!$globals->asso('mail_domain')) {
286 return PL_NOT_FOUND;
287 }
1490093c 288 $page->changeTpl('xnetlists/alias-create.tpl');
d1fcf09c 289
290 if (!Post::has('submit')) {
291 return;
8d73663d
VZ
292 } else {
293 S::assert_xsrf_token();
d1fcf09c 294 }
295
296 if (!Post::has('liste')) {
6bb2f79a 297 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
d1fcf09c 298 return;
299 }
85ddf64f
SJ
300 $list = Post::v('liste');
301 if (!preg_match("/^[a-zA-Z0-9\-\.]*$/", $list)) {
302 $page->trigError('Le nom de l\'alias ne doit contenir que des lettres,'
303 .' chiffres, tirets et points.');
d1fcf09c 304 return;
305 }
306
85ddf64f
SJ
307 require_once 'emails.inc.php';
308 if (list_exist($list, $globals->asso('mail_domain'))) {
309 $page->trigError('Cet alias est déjà pris.');
d1fcf09c 310 return;
311 }
312
05978b09 313 add_to_list_alias(S::i('uid'), $list, $globals->asso('mail_domain'));
85ddf64f 314 pl_redirect('alias/admin/' . $list . '@' . $globals->asso('mail_domain'));
d1fcf09c 315 }
316
26ba053e 317 function handler_profile($page, $user = null)
7b9d64a8 318 {
8b00e0e0 319 http_redirect('https://www.polytechnique.org/profile/'.$user);
7b9d64a8 320 }
321}
322
a7de4ef7 323// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
7b9d64a8 324?>