NIT
[platal.git] / modules / lists.php
CommitLineData
bc4ad6aa 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 Polytechnique.org *
bc4ad6aa 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
22class ListsModule extends PLModule
23{
0baf0741 24 protected $client;
bc4ad6aa 25
26 function handlers()
27 {
28 return array(
2ca5f031
FB
29 'lists' => $this->make_hook('lists', AUTH_MDP),
30 'lists/ajax' => $this->make_hook('ajax', AUTH_MDP, 'user', NO_AUTH),
2914271e 31 'lists/create' => $this->make_hook('create', AUTH_MDP, 'lists'),
bc4ad6aa 32
2ca5f031
FB
33 'lists/members' => $this->make_hook('members', AUTH_COOKIE),
34 'lists/csv' => $this->make_hook('csv', AUTH_COOKIE),
35 'lists/annu' => $this->make_hook('annu', AUTH_COOKIE),
36 'lists/archives' => $this->make_hook('archives', AUTH_COOKIE),
37 'lists/archives/rss' => $this->make_hook('rss', AUTH_PUBLIC, 'user', NO_HTTPS),
bc4ad6aa 38
2ca5f031
FB
39 'lists/moderate' => $this->make_hook('moderate', AUTH_MDP),
40 'lists/admin' => $this->make_hook('admin', AUTH_MDP),
41 'lists/options' => $this->make_hook('options', AUTH_MDP),
42 'lists/delete' => $this->make_hook('delete', AUTH_MDP),
bc4ad6aa 43
2ca5f031
FB
44 'lists/soptions' => $this->make_hook('soptions', AUTH_MDP),
45 'lists/check' => $this->make_hook('check', AUTH_MDP),
2914271e 46 'admin/lists' => $this->make_hook('admin_all', AUTH_MDP, 'admin'),
bc4ad6aa 47 );
48 }
49
8c12f931 50 function prepare_client(&$page, $user = null)
bc4ad6aa 51 {
0ec52d96 52 global $globals;
53
460d8f55 54 $this->load('lists.inc.php');
8c12f931
FB
55 if (is_null($user)) {
56 $user = S::user();
57 }
bc4ad6aa 58
7f1ff426 59 $this->client = new MMList($user);
092945b4 60 return $globals->mail->domain;
bc4ad6aa 61 }
62
834fd0f6 63 function get_pending_ops($domain, $list)
64 {
65 list($subs,$mails) = $this->client->get_pending_ops($list);
66 $res = XDB::query("SELECT mid
2235cd7f 67 FROM email_list_moderate
834fd0f6 68 WHERE ml = {?} AND domain = {?}",
69 $list, $domain);
70 $mids = $res->fetchColumn();
71 foreach ($mails as $key=>$mail) {
72 if (in_array($mail['id'], $mids)) {
73 unset($mails[$key]);
74 }
75 }
76 return array($subs, $mails);
77 }
78
bc4ad6aa 79 function handler_lists(&$page)
80 {
50c655ee 81 function filter_owner($list)
82 {
83 return $list['own'];
84 }
85
86 function filter_member($list)
87 {
da398501 88 return $list['sub'];
50c655ee 89 }
90
834fd0f6 91 $domain = $this->prepare_client($page);
bc4ad6aa 92
edc4367b 93 $page->changeTpl('lists/index.tpl');
46f272fe 94 $page->setTitle('Listes de diffusion');
bc4ad6aa 95
96
97 if (Get::has('del')) {
2669eb7d 98 S::assert_xsrf_token();
5e2307dc 99 $this->client->unsubscribe(Get::v('del'));
8b00e0e0 100 pl_redirect('lists');
bc4ad6aa 101 }
102 if (Get::has('add')) {
2669eb7d 103 S::assert_xsrf_token();
5e2307dc 104 $this->client->subscribe(Get::v('add'));
8b00e0e0 105 pl_redirect('lists');
bc4ad6aa 106 }
107 if (Post::has('promo_add')) {
2669eb7d
VZ
108 S::assert_xsrf_token();
109
5e2307dc 110 $promo = Post::i('promo_add');
bc4ad6aa 111 if ($promo >= 1900 and $promo < 2100) {
112 $this->client->subscribe("promo$promo");
113 } else {
7be6ecba 114 $page->trigError("promo incorrecte, il faut une promo sur 4 chiffres.");
bc4ad6aa 115 }
116 }
2669eb7d 117
81a9ae96
SJ
118 if (!is_null($listes = $this->client->get_lists())) {
119 $owner = array_filter($listes, 'filter_owner');
120 $listes = array_diff_key($listes, $owner);
121 $member = array_filter($listes, 'filter_member');
122 $listes = array_diff_key($listes, $member);
123 foreach ($owner as $key => $liste) {
124 list($subs, $mails) = $this->get_pending_ops($domain, $liste['list']);
125 $owner[$key]['subscriptions'] = $subs;
126 $owner[$key]['mails'] = $mails;
127 }
128 $page->register_modifier('hdc', 'list_header_decode');
129 $page->assign_by_ref('owner', $owner);
130 $page->assign_by_ref('member', $member);
131 $page->assign_by_ref('public', $listes);
50c655ee 132 }
50c655ee 133 }
134
135 function handler_ajax(&$page, $list = null)
136 {
3cb500d5 137 pl_content_headers("text/html");
5cbb1fad 138 $domain = $this->prepare_client($page);
edc4367b 139 $page->changeTpl('lists/liste.inc.tpl', NO_SKIN);
2669eb7d
VZ
140 S::assert_xsrf_token();
141
50c655ee 142 if (Get::has('unsubscribe')) {
143 $this->client->unsubscribe($list);
144 }
145 if (Get::has('subscribe')) {
146 $this->client->subscribe($list);
147 }
148 if (Get::has('sadd')) { /* 4 = SUBSCRIBE */
149 $this->client->handle_request($list, Get::v('sadd'), 4, '');
150 }
151 if (Get::has('mid')) {
5cbb1fad 152 $this->moderate_mail($domain, $list, Get::i('mid'));
50c655ee 153 }
154
155 list($liste, $members, $owners) = $this->client->get_members($list);
156 if ($liste['own']) {
834fd0f6 157 list($subs,$mails) = $this->get_pending_ops($domain, $list);
50c655ee 158 $liste['subscriptions'] = $subs;
159 $liste['mails'] = $mails;
160 }
161 $page->register_modifier('hdc', 'list_header_decode');
162 $page->assign_by_ref('liste', $liste);
bc4ad6aa 163 }
164
165 function handler_create(&$page)
166 {
032b244e
SJ
167 global $globals;
168
edc4367b 169 $page->changeTpl('lists/create.tpl');
bc4ad6aa 170
18fb3c63 171 $user_promo = S::user()->profile()->yearPromo();
8ac67bda
SJ
172 $year = date('Y');
173 $month = date('m');
18fb3c63
PC
174 // scolar year starts in september
175 $scolarmonth = ($year - $user_promo) * 12 + ($month - 8);
8ac67bda 176 $young_promo = $very_young_promo = 0;
18fb3c63
PC
177 // binet are accessible only in april in the first year and until
178 // march of the 5th year
179 if ($scolarmonth >= 8 && $scolarmonth < 56) {
8ac67bda
SJ
180 $young_promo = 1;
181 }
18fb3c63
PC
182 // PSC aliases are accesible only between september and june of the second
183 // year of scolarity
184 if ($scolarmonth >= 12 && $scolarmonth < 22) {
8ac67bda
SJ
185 $very_young_promo = 1;
186 }
187 $page->assign('young_promo', $young_promo);
188 $page->assign('very_young_promo', $very_young_promo);
189
5e2307dc 190 $owners = preg_split("/[\s]+/", Post::v('owners'), -1, PREG_SPLIT_NO_EMPTY);
191 $members = preg_split("/[\s]+/", Post::v('members'), -1, PREG_SPLIT_NO_EMPTY);
bc4ad6aa 192
193 // click on validate button 'add_owner_sub' or type <enter>
194 if (Post::has('add_owner_sub') && Post::has('add_owner')) {
bc4ad6aa 195 // if we want to add an owner and then type <enter>, then both
196 // add_owner_sub and add_owner are filled.
7586ae0b
VZ
197 $oforlifes = User::getBulkForlifeEmails(Post::v('add_owner'), true);
198 $mforlifes = User::getBulkForlifeEmails(Post::v('add_member'), true);
e7545178 199 if (!is_null($oforlifes)) {
200 $owners = array_merge($owners, $oforlifes);
201 }
202 // if we want to add a member and then type <enter>, then
203 // add_owner_sub is filled, whereas add_owner is empty.
204 if (!is_null($mforlifes)) {
205 $members = array_merge($members, $mforlifes);
bc4ad6aa 206 }
207 }
208
209 // click on validate button 'add_member_sub'
210 if (Post::has('add_member_sub') && Post::has('add_member')) {
7586ae0b 211 $forlifes = User::getBulkForlifeEmails(Post::v('add_member'), true);
e7545178 212 if (!is_null($forlifes)) {
213 $members = array_merge($members, $forlifes);
bc4ad6aa 214 }
215 }
45070158 216 if (Post::has('add_member_sub') && isset($_FILES['add_member_file']) && $_FILES['add_member_file']['tmp_name']) {
f3df6d38 217 $upload =& PlUpload::get($_FILES['add_member_file'], S::user()->login(), 'list.addmember', true);
45070158
FB
218 if (!$upload) {
219 $page->trigError('Une erreur s\'est produite lors du téléchargement du fichier');
220 } else {
7586ae0b 221 $forlifes = User::getBulkForlifeEmails($upload->getContents(), true);
45070158
FB
222 if (!is_null($forlifes)) {
223 $members = array_merge($members, $forlifes);
224 }
225 }
226 }
bc4ad6aa 227
e7545178 228 ksort($owners);
229 $owners = array_unique($owners);
230 ksort($members);
231 $members = array_unique($members);
bc4ad6aa 232
45070158
FB
233 $page->assign('owners', join("\n", $owners));
234 $page->assign('members', join("\n", $members));
bc4ad6aa 235
236 if (!Post::has('submit')) {
237 return;
2669eb7d
VZ
238 } else {
239 S::assert_xsrf_token();
bc4ad6aa 240 }
241
59887c4a 242 $asso = Post::v('asso');
5e2307dc 243 $liste = Post::v('liste');
bc4ad6aa 244
245 if (empty($liste)) {
6bb2f79a 246 $page->trigError('Le champ «&nbsp;adresse souhaitée&nbsp;» est vide.');
bc4ad6aa 247 }
248 if (!preg_match("/^[a-zA-Z0-9\-]*$/", $liste)) {
59887c4a 249 $page->trigError('Le nom de la liste ne doit contenir que des lettres non accentuées, chiffres et tirets.');
bc4ad6aa 250 }
251
59887c4a
SJ
252 if (($asso == "binet") || ($asso == "alias")) {
253 $promo = Post::i('promo');
032b244e 254 $domain = $promo . '.' . $globals->mail->domain;
59887c4a
SJ
255
256 if (($promo < 1921) || ($promo > date('Y'))) {
6bb2f79a 257 $page->trigError('La promotion est mal renseignée, elle doit être du type&nbsp;: 2004.');
59887c4a
SJ
258 }
259
260 $new = $liste . '@' . $domain;
00112b2e 261 $res = XDB::query('SELECT COUNT(*) FROM virtual WHERE alias={?}', $new);
59887c4a
SJ
262
263 } else {
264 if ($asso == "groupex") {
265 $groupex_name = Post::v('groupex_name');
266
eb41eda9 267 $res_groupe = XDB::query('SELECT mail_domain FROM groups WHERE nom={?}', $groupex_name);
59887c4a
SJ
268 $domain = $res_groupe->fetchOneCell();
269
270 if (!$domain) {
271 $page->trigError('Il n\'y a aucun groupe de ce nom sur Polytechnique.net.');
272 }
273
274 $new = $liste . '@' . $domain;
00112b2e 275 $res = XDB::query('SELECT COUNT(*) FROM virtual WHERE alias={?}', $new);
59887c4a
SJ
276 } else {
277 $res = XDB::query("SELECT COUNT(*) FROM aliases WHERE alias={?}", $liste);
032b244e 278 $domain = $globals->mail->domain;
59887c4a 279 }
bc4ad6aa 280 }
281
59887c4a 282 $n = $res->fetchOneCell();
bc4ad6aa 283
284 if ($n) {
6bb2f79a 285 $page->trigError("L'«&nbsp;adresse souhaitée&nbsp;» est déjà prise.");
bc4ad6aa 286 }
287
92144f3e 288 if (!Post::v('desc')) {
59887c4a 289 $page->trigError('Le sujet est vide.');
bc4ad6aa 290 }
291
292 if (!count($owners)) {
59887c4a 293 $page->trigError('Il n\'y a pas de gestionnaire.');
bc4ad6aa 294 }
295
296 if (count($members)<4) {
59887c4a 297 $page->trigError('Il n\'y a pas assez de membres.');
bc4ad6aa 298 }
299
300 if (!$page->nb_errs()) {
562064b5 301 $page->trigSuccess('Demande de création envoyée&nbsp;!');
bc4ad6aa 302 $page->assign('created', true);
5daf68f6 303 $req = new ListeReq(S::user(), $asso, $liste, $domain,
5e2307dc 304 Post::v('desc'), Post::i('advertise'),
305 Post::i('modlevel'), Post::i('inslevel'),
bc4ad6aa 306 $owners, $members);
307 $req->submit();
308 }
309 }
310
311 function handler_members(&$page, $liste = null)
312 {
bc4ad6aa 313 if (is_null($liste)) {
314 return PL_NOT_FOUND;
315 }
316
7b9d64a8 317 $this->prepare_client($page);
bc4ad6aa 318
1490093c 319 $page->changeTpl('lists/members.tpl');
bc4ad6aa 320
321 if (Get::has('del')) {
2669eb7d 322 S::assert_xsrf_token();
bc4ad6aa 323 $this->client->unsubscribe($liste);
8b00e0e0 324 pl_redirect('lists/members/'.$liste);
bc4ad6aa 325 }
326
327 if (Get::has('add')) {
2669eb7d 328 S::assert_xsrf_token();
bc4ad6aa 329 $this->client->subscribe($liste);
8b00e0e0 330 pl_redirect('lists/members/'.$liste);
bc4ad6aa 331 }
332
333 $members = $this->client->get_members($liste);
334
5e2307dc 335 $tri_promo = !Env::b('alpha');
bc4ad6aa 336
337 if (list($det,$mem,$own) = $members) {
338 $membres = list_sort_members($mem, $tri_promo);
339 $moderos = list_sort_owners($own, $tri_promo);
340
341 $page->assign_by_ref('details', $det);
342 $page->assign_by_ref('members', $membres);
343 $page->assign_by_ref('owners', $moderos);
344 $page->assign('nb_m', count($mem));
345 } else {
6e828e47 346 $page->kill("La liste n'existe pas ou tu n'as pas le droit d'en voir les détails.");
bc4ad6aa 347 }
348 }
349
b73bc04b
FB
350 function handler_csv(PlPage &$page, $liste = null)
351 {
352 if (is_null($liste)) {
353 return PL_NOT_FOUND;
354 }
355 $this->prepare_client($page);
356 $members = $this->client->get_members($liste);
b97ecb9d 357 $list = list_fetch_basic_info(list_extract_members($members[1]));
3cb500d5 358 pl_content_headers("text/x-csv");
b73bc04b 359
b97ecb9d
SJ
360 echo "email,nom,promo\n";
361 echo implode("\n", $list);
b73bc04b
FB
362 exit;
363 }
364
1cc0afe7 365 function handler_annu(&$page, $liste = null, $action = null, $subaction = null)
bc4ad6aa 366 {
bc4ad6aa 367 if (is_null($liste)) {
368 return PL_NOT_FOUND;
369 }
370
7b9d64a8 371 $this->prepare_client($page);
bc4ad6aa 372
bc4ad6aa 373 if (Get::has('del')) {
2669eb7d 374 S::assert_xsrf_token();
bc4ad6aa 375 $this->client->unsubscribe($liste);
1cc0afe7 376 pl_redirect('lists/annu/'.$liste);
bc4ad6aa 377 }
378 if (Get::has('add')) {
2669eb7d 379 S::assert_xsrf_token();
bc4ad6aa 380 $this->client->subscribe($liste);
1cc0afe7 381 pl_redirect('lists/annu/'.$liste);
bc4ad6aa 382 }
383
384 $owners = $this->client->get_owners($liste);
1cc0afe7 385 if (!is_array($owners)) {
6e828e47 386 $page->kill("La liste n'existe pas ou tu n'as pas le droit d'en voir les détails.");
bc4ad6aa 387 }
1cc0afe7 388
389 global $platal;
390 list(,$members) = $this->client->get_members($liste);
391 $users = array();
392 foreach ($members as $m) {
393 $users[] = $m[1];
394 }
395 require_once 'userset.inc.php';
396 $view = new ArraySet($users);
397 $view->addMod('trombi', 'Trombinoscope', true, array('with_promo' => true));
398 if (empty($GLOBALS['IS_XNET_SITE'])) {
1fae7605 399 $view->addMod('minifiche', 'Mini-fiches', false);
1cc0afe7 400 }
1fe46b8f
SJ
401 // TODO: Reactivate when the new map is completed.
402 // $view->addMod('geoloc', 'Planisphère');
1cc0afe7 403 $view->apply("lists/annu/$liste", $page, $action, $subaction);
404 if ($action == 'geoloc' && $subaction) {
405 return;
406 }
407
408 $page->changeTpl('lists/annu.tpl');
409 $moderos = list_sort_owners($owners[1]);
410 $page->assign_by_ref('details', $owners[0]);
411 $page->assign_by_ref('owners', $moderos);
bc4ad6aa 412 }
413
fa7d6c7b 414 function handler_archives(&$page, $liste = null, $action = null, $artid = null)
bc4ad6aa 415 {
416 global $globals;
417
418 if (is_null($liste)) {
419 return PL_NOT_FOUND;
420 }
421
092945b4 422 $domain = $this->prepare_client($page);
bc4ad6aa 423
1490093c 424 $page->changeTpl('lists/archives.tpl');
bc4ad6aa 425
bc4ad6aa 426 if (list($det) = $this->client->get_members($liste)) {
427 if (substr($liste,0,5) != 'promo' && ($det['ins'] || $det['priv'])
fa7d6c7b 428 && !$det['own'] && ($det['sub'] < 2)) {
38421eaa 429 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter.");
fa7d6c7b 430 }
431 $get = Array('listname' => $liste, 'domain' => $domain);
432 if (Post::has('updateall')) {
433 $get['updateall'] = Post::v('updateall');
434 }
4f355064 435 require_once 'banana/ml.inc.php';
436 get_banana_params($get, null, $action, $artid);
437 run_banana($page, 'MLBanana', $get);
bc4ad6aa 438 } else {
38421eaa 439 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter.");
bc4ad6aa 440 }
441 }
442
6544d0e1 443 function handler_rss(&$page, $liste = null, $alias = null, $hash = null)
444 {
8c12f931
FB
445 if (!$liste) {
446 return PL_NOT_FOUND;
447 }
448 $user = Platal::session()->tokenAuth($alias, $hash);
449 if (is_null($user)) {
450 return PL_FORBIDDEN;
6544d0e1 451 }
452
8c12f931 453 $domain = $this->prepare_client($page, $user);
6544d0e1 454 if (list($det) = $this->client->get_members($liste)) {
455 if (substr($liste,0,5) != 'promo' && ($det['ins'] || $det['priv'])
456 && !$det['own'] && ($det['sub'] < 2)) {
eaf30d86 457 exit;
6544d0e1 458 }
459 require_once('banana/ml.inc.php');
8c12f931 460 $banana = new MLBanana($user, Array('listname' => $liste, 'domain' => $domain, 'action' => 'rss2'));
4f355064 461 $banana->run();
6544d0e1 462 }
463 exit;
464 }
465
5cbb1fad 466 function moderate_mail($domain, $liste, $mid)
50c655ee 467 {
50c655ee 468 if (Env::has('mok')) {
834fd0f6 469 $action = 'accept';
50c655ee 470 } elseif (Env::has('mno')) {
834fd0f6 471 $action = 'refuse';
50c655ee 472 } elseif (Env::has('mdel')) {
834fd0f6 473 $action = 'delete';
474 } else {
475 return false;
476 }
477 Get::kill('mid');
2235cd7f 478 return XDB::execute("INSERT IGNORE INTO email_list_moderate
834fd0f6 479 VALUES ({?}, {?}, {?}, {?}, {?}, NOW(), {?}, NULL)",
480 $liste, $domain, $mid, S::i('uid'), $action, Post::v('reason'));
50c655ee 481 }
482
bc4ad6aa 483 function handler_moderate(&$page, $liste = null)
484 {
bc4ad6aa 485 if (is_null($liste)) {
4b0d9ef3 486 return PL_NOT_FOUND;
bc4ad6aa 487 }
488
092945b4 489 $domain = $this->prepare_client($page);
bc4ad6aa 490
1490093c 491 $page->changeTpl('lists/moderate.tpl');
bc4ad6aa 492
c8529706 493 $page->register_modifier('hdc', 'list_header_decode');
bc4ad6aa 494
4b0d9ef3 495 if (Env::has('sadd') || Env::has('sdel')) {
2669eb7d
VZ
496 S::assert_xsrf_token();
497
4b0d9ef3 498 if (Env::has('sadd')) { /* 4 = SUBSCRIBE */
499 $sub = $this->client->get_pending_sub($liste, Env::v('sadd'));
500 $this->client->handle_request($liste,Env::v('sadd'),4,'');
501 $info = "validée";
502 }
503 if (Post::has('sdel')) { /* 2 = REJECT */
504 $sub = $this->client->get_pending_sub($liste, Env::v('sdel'));
a5878ac1 505 $this->client->handle_request($liste, Post::v('sdel'), 2, utf8_decode(Post::v('reason')));
4b0d9ef3 506 $info = "refusée";
507 }
508 if ($sub) {
509 $mailer = new PlMailer();
510 $mailer->setFrom("$liste-bounces@{$domain}");
511 $mailer->addTo("$liste-owner@{$domain}");
512 $mailer->addHeader('Reply-To', "$liste-owner@{$domain}");
513 $mailer->setSubject("L'inscription de {$sub['name']} a été $info");
4595d3e2 514 $text = "L'inscription de {$sub['name']} à la liste $liste@{$domain} a été $info par " . S::user()->fullName(true) . ".\n";
4b0d9ef3 515 if (trim(Post::v('reason'))) {
516 $text .= "\nLa raison invoquée est :\n" . Post::v('reason');
517 }
518 $mailer->setTxtBody(wordwrap($text, 72));
519 $mailer->send();
520 }
521 if (Env::has('sadd')) {
522 pl_redirect('lists/moderate/'.$liste);
eaf30d86 523 }
bc4ad6aa 524 }
525
e940f534 526 if (Post::has('moderate_mails') && Post::has('select_mails')) {
2669eb7d
VZ
527 S::assert_xsrf_token();
528
e940f534 529 $mails = array_keys(Post::v('select_mails'));
530 foreach($mails as $mail) {
531 $this->moderate_mail($domain, $liste, $mail);
532 }
533 } elseif (Env::has('mid')) {
d96379f6 534 if (Get::has('mid') && !Env::has('mok') && !Env::has('mdel')) {
ecc72a6d 535 require_once 'banana/moderate.inc.php';
bc4ad6aa 536
52d032a7 537 $page->changeTpl('lists/moderate_mail.tpl');
52d032a7
SJ
538 $params = array('listname' => $liste, 'domain' => $domain,
539 'artid' => Get::i('mid'), 'part' => Get::v('part'), 'action' => Get::v('action'));
540 $params['client'] = $this->client;
541 run_banana($page, 'ModerationBanana', $params);
542
ecc72a6d 543 $msg = file_get_contents('/etc/mailman/fr/refuse.txt');
52d032a7
SJ
544 $msg = str_replace("%(adminaddr)s", "$liste-owner@{$domain}", $msg);
545 $msg = str_replace("%(request)s", "<< SUJET DU MAIL >>", $msg);
546 $msg = str_replace("%(reason)s", "<< TON EXPLICATION >>", $msg);
547 $msg = str_replace("%(listname)s", $liste, $msg);
548 $page->assign('msg', $msg);
549 return;
ed03d07f 550 }
551
ecc72a6d 552 $this->moderate_mail($domain, $liste, Env::i('mid'));
bc4ad6aa 553 } elseif (Env::has('sid')) {
834fd0f6 554 if (list($subs,$mails) = $this->get_pending_ops($domain, $liste)) {
bc4ad6aa 555 foreach($subs as $user) {
5e2307dc 556 if ($user['id'] == Env::v('sid')) {
1490093c 557 $page->changeTpl('lists/moderate_sub.tpl');
6a20c6a3 558 $page->assign('del_user', $user);
559 return;
bc4ad6aa 560 }
561 }
562 }
563
564 }
565
834fd0f6 566 if (list($subs,$mails) = $this->get_pending_ops($domain, $liste)) {
46ab179a 567 foreach ($mails as $key=>$mail) {
568 $mails[$key]['stamp'] = strftime("%Y%m%d%H%M%S", $mail['stamp']);
717b9fa7
FB
569 if ($mail['fromx']) {
570 $page->assign('with_fromx', true);
571 } else {
572 $page->assign('with_nonfromx', true);
573 }
46ab179a 574 }
bc4ad6aa 575 $page->assign_by_ref('subs', $subs);
576 $page->assign_by_ref('mails', $mails);
577 } else {
6e828e47 578 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la modérer.");
bc4ad6aa 579 }
580 }
581
0baf0741 582 static public function no_login_callback($login)
583 {
be792642 584 global $list_unregistered, $globals;
0baf0741 585
61a7d279 586 $users = User::getPendingAccounts($login, true);
a58d8539 587 if ($users && $users->total()) {
0baf0741 588 if (!isset($list_unregistered)) {
589 $list_unregistered = array();
590 }
591 $list_unregistered[$login] = $users;
592 } else {
be792642
FB
593 list($name, $dom) = @explode('@', $login);
594 if ($dom == $globals->mail->domain || $dom == $globals->mail->domain2) {
750f63db 595 User::_default_user_callback($login);
be792642 596 }
0baf0741 597 }
598 }
599
bc4ad6aa 600 function handler_admin(&$page, $liste = null)
601 {
602 global $globals;
603
604 if (is_null($liste)) {
605 return PL_NOT_FOUND;
606 }
607
0baf0741 608 $domain = $this->prepare_client($page);
bc4ad6aa 609
1490093c 610 $page->changeTpl('lists/admin.tpl');
bc4ad6aa 611
0baf0741 612 if (Env::has('send_mark')) {
2669eb7d
VZ
613 S::assert_xsrf_token();
614
0baf0741 615 $actions = Env::v('mk_action');
616 $uids = Env::v('mk_uid');
617 $mails = Env::v('mk_email');
618 foreach ($actions as $key=>$action) {
619 switch ($action) {
620 case 'none':
621 break;
622
623 case 'marketu': case 'markets':
624 require_once 'emails.inc.php';
625 $mail = valide_email($mails[$key]);
626 if (isvalid_email_redirection($mail)) {
627 $from = ($action == 'marketu') ? 'user' : 'staff';
628 $market = Marketing::get($uids[$key], $mail);
629 if (!$market) {
630 $market = new Marketing($uids[$key], $mail, 'list', "$liste@$domain", $from, S::v('uid'));
631 $market->add();
632 break;
633 }
634 }
635
636 default:
637 XDB::execute('INSERT IGNORE INTO register_subs (uid, type, sub, domain)
638 VALUES ({?}, \'list\', {?}, {?})',
639 $uids[$key], $liste, $domain);
640 }
641 }
642 }
643
bc4ad6aa 644 if (Env::has('add_member')) {
2669eb7d
VZ
645 S::assert_xsrf_token();
646
7586ae0b 647 $members = User::getBulkForlifeEmails(Env::v('add_member'),
7129ea95 648 true,
7586ae0b 649 array('ListsModule', 'no_login_callback'));
bc4ad6aa 650 $arr = $this->client->mass_subscribe($liste, $members);
651 if (is_array($arr)) {
652 foreach($arr as $addr) {
a7d35093 653 $page->trigSuccess("{$addr[0]} inscrit.");
bc4ad6aa 654 }
655 }
656 }
657
108d1d90 658 if (isset($_FILES['add_member_file']) && $_FILES['add_member_file']['tmp_name']) {
2669eb7d
VZ
659 S::assert_xsrf_token();
660
f3df6d38 661 $upload =& PlUpload::get($_FILES['add_member_file'], S::user()->login(), 'list.addmember', true);
108d1d90
FB
662 if (!$upload) {
663 $page->trigError('Une erreur s\'est produite lors du téléchargement du fichier');
664 } else {
7586ae0b 665 $members = User::getBulkForlifeEmails($upload->getContents(),
7129ea95 666 true,
7586ae0b 667 array('ListsModule', 'no_login_callback'));
108d1d90
FB
668 $arr = $this->client->mass_subscribe($liste, $members);
669 if (is_array($arr)) {
670 foreach($arr as $addr) {
671 $page->trigSuccess("{$addr[0]} inscrit.");
672 }
673 }
674 }
675 }
676
bc4ad6aa 677 if (Env::has('del_member')) {
2669eb7d
VZ
678 S::assert_xsrf_token();
679
5e2307dc 680 if (strpos(Env::v('del_member'), '@') === false) {
bc4ad6aa 681 $this->client->mass_unsubscribe(
5e2307dc 682 $liste, array(Env::v('del_member').'@'.$globals->mail->domain));
bc4ad6aa 683 } else {
5e2307dc 684 $this->client->mass_unsubscribe($liste, array(Env::v('del_member')));
bc4ad6aa 685 }
8b00e0e0 686 pl_redirect('lists/admin/'.$liste);
bc4ad6aa 687 }
688
689 if (Env::has('add_owner')) {
2669eb7d
VZ
690 S::assert_xsrf_token();
691
7586ae0b 692 $owners = User::getBulkForlifeEmails(Env::v('add_owner'), false, array('ListsModule', 'no_login_callback'));
e7545178 693 if ($owners) {
694 foreach ($owners as $login) {
695 if ($this->client->add_owner($liste, $login)) {
7586ae0b 696 $page->trigSuccess($login ." ajouté aux modérateurs.");
e7545178 697 }
bc4ad6aa 698 }
699 }
700 }
701
702 if (Env::has('del_owner')) {
2669eb7d
VZ
703 S::assert_xsrf_token();
704
5e2307dc 705 if (strpos(Env::v('del_owner'), '@') === false) {
706 $this->client->del_owner($liste, Env::v('del_owner').'@'.$globals->mail->domain);
bc4ad6aa 707 } else {
5e2307dc 708 $this->client->del_owner($liste, Env::v('del_owner'));
bc4ad6aa 709 }
8b00e0e0 710 pl_redirect('lists/admin/'.$liste);
bc4ad6aa 711 }
712
713 if (list($det,$mem,$own) = $this->client->get_members($liste)) {
0baf0741 714 global $list_unregistered;
715 if ($list_unregistered) {
716 $page->assign_by_ref('unregistered', $list_unregistered);
717 }
30fc8ee7 718 $membres = list_sort_members($mem, @$tri_promo);
719 $moderos = list_sort_owners($own, @$tri_promo);
bc4ad6aa 720
721 $page->assign_by_ref('details', $det);
722 $page->assign_by_ref('members', $membres);
723 $page->assign_by_ref('owners', $moderos);
724 $page->assign('np_m', count($mem));
725
726 } else {
383eaddd 727 $page->kill("La liste n'existe pas ou tu n'as pas le droit de l'administrer.<br />"
6e828e47
SJ
728 . " Si tu penses qu'il s'agit d'une erreur, "
729 . "<a href='mailto:support@polytechnique.org'>contact le support</a>.");
bc4ad6aa 730 }
731 }
732
733 function handler_options(&$page, $liste = null)
734 {
bc4ad6aa 735 if (is_null($liste)) {
736 return PL_NOT_FOUND;
737 }
738
7b9d64a8 739 $this->prepare_client($page);
bc4ad6aa 740
1490093c 741 $page->changeTpl('lists/options.tpl');
bc4ad6aa 742
743 if (Post::has('submit')) {
2669eb7d
VZ
744 S::assert_xsrf_token();
745
bc4ad6aa 746 $values = $_POST;
5fb22b39 747 $values = array_map('utf8_decode', $values);
c638d8c8
FB
748 $spamlevel = intval($values['bogo_level']);
749 $unsurelevel = intval($values['unsure_level']);
750 if ($spamlevel == 0) {
751 $unsurelevel = 0;
752 }
753 if ($spamlevel > 3 || $spamlevel < 0 || $unsurelevel < 0 || $unsurelevel > 1) {
754 $page->trigError("Réglage de l'antispam non valide");
755 } else {
756 $this->client->set_bogo_level($liste, ($spamlevel << 1) + $unsurelevel);
757 }
bc4ad6aa 758 switch($values['moderate']) {
759 case '0':
760 $values['generic_nonmember_action'] = 0;
761 $values['default_member_moderation'] = 0;
762 break;
763 case '1':
764 $values['generic_nonmember_action'] = 1;
765 $values['default_member_moderation'] = 0;
766 break;
767 case '2':
768 $values['generic_nonmember_action'] = 1;
769 $values['default_member_moderation'] = 1;
770 break;
771 }
772 unset($values['submit'], $values['bogo_level'], $values['moderate']);
773 $values['send_goodbye_msg'] = !empty($values['send_goodbye_msg']);
774 $values['admin_notify_mchanges'] = !empty($values['admin_notify_mchanges']);
775 $values['subscribe_policy'] = empty($values['subscribe_policy']) ? 0 : 2;
776 if (isset($values['subject_prefix'])) {
777 $values['subject_prefix'] = trim($values['subject_prefix']).' ';
778 }
779 $this->client->set_owner_options($liste, $values);
5e2307dc 780 } elseif (isvalid_email(Post::v('atn_add'))) {
2669eb7d 781 S::assert_xsrf_token();
5e2307dc 782 $this->client->add_to_wl($liste, Post::v('atn_add'));
bc4ad6aa 783 } elseif (Get::has('atn_del')) {
2669eb7d 784 S::assert_xsrf_token();
5e2307dc 785 $this->client->del_from_wl($liste, Get::v('atn_del'));
8b00e0e0 786 pl_redirect('lists/options/'.$liste);
bc4ad6aa 787 }
788
789 if (list($details,$options) = $this->client->get_owner_options($liste)) {
790 $page->assign_by_ref('details', $details);
791 $page->assign_by_ref('options', $options);
c638d8c8
FB
792 $bogo_level = intval($this->client->get_bogo_level($liste));
793 $page->assign('unsure_level', $bogo_level & 1);
794 $page->assign('bogo_level', $bogo_level >> 1);
bc4ad6aa 795 } else {
796 $page->kill("La liste n'existe pas ou tu n'as pas le droit de l'administrer");
797 }
798 }
799
800 function handler_delete(&$page, $liste = null)
801 {
7c5842f3 802 global $globals;
bc4ad6aa 803 if (is_null($liste)) {
804 return PL_NOT_FOUND;
805 }
806
7c5842f3 807 $domain = $this->prepare_client($page);
808 if ($domain == $globals->mail->domain || $domain == $globals->mail->domain2) {
809 $domain = '';
810 $table = 'aliases';
811 $type = 'liste';
812 } else {
813 $domain = '@' . $domain;
814 $table = 'virtual';
815 $type = 'list';
816 }
bc4ad6aa 817
1490093c 818 $page->changeTpl('lists/delete.tpl');
7c5842f3 819 if (Post::v('valid') == 'OUI') {
2669eb7d
VZ
820 S::assert_xsrf_token();
821
7c5842f3 822 if ($this->client->delete_list($liste, Post::b('del_archive'))) {
7d427b10 823 foreach (array('', '-owner', '-admin', '-bounces', '-unsubscribe') as $app) {
7c5842f3 824 XDB::execute("DELETE FROM $table
825 WHERE type={?} AND alias={?}",
826 $type, $liste.$app.$domain);
827 }
828 $page->assign('deleted', true);
6bb2f79a 829 $page->trigSuccess('La liste a été détruite&nbsp;!');
7c5842f3 830 } else {
831 $page->kill('Une erreur est survenue lors de la suppression de la liste.<br />'
a7de4ef7 832 . 'Contact les administrateurs du site pour régler le problème : '
6e828e47 833 . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>.');
bc4ad6aa 834 }
bc4ad6aa 835 } elseif (list($details,$options) = $this->client->get_owner_options($liste)) {
44b071c2
SJ
836 if (!$details['own']) {
837 $page->trigWarning('Tu n\'es pas administrateur de la liste, mais du site.');
838 }
bc4ad6aa 839 $page->assign_by_ref('details', $details);
840 $page->assign_by_ref('options', $options);
841 $page->assign('bogo_level', $this->client->get_bogo_level($liste));
842 } else {
6e828e47 843 $page->kill("La liste n'existe pas ou tu n'as pas le droit de l'administrer.");
bc4ad6aa 844 }
845 }
846
847 function handler_soptions(&$page, $liste = null)
848 {
bc4ad6aa 849 if (is_null($liste)) {
850 return PL_NOT_FOUND;
851 }
852
7b9d64a8 853 $this->prepare_client($page);
bc4ad6aa 854
1490093c 855 $page->changeTpl('lists/soptions.tpl');
bc4ad6aa 856
857 if (Post::has('submit')) {
2669eb7d
VZ
858 S::assert_xsrf_token();
859
bc4ad6aa 860 $values = $_POST;
5fb22b39 861 $values = array_map('utf8_decode', $values);
bc4ad6aa 862 unset($values['submit']);
863 $values['advertised'] = empty($values['advertised']) ? false : true;
864 $values['archive'] = empty($values['archive']) ? false : true;
865 $this->client->set_admin_options($liste, $values);
866 }
867
868 if (list($details,$options) = $this->client->get_admin_options($liste)) {
869 $page->assign_by_ref('details', $details);
870 $page->assign_by_ref('options', $options);
871 } else {
6e828e47 872 $page->kill("La liste n'existe pas.");
bc4ad6aa 873 }
874 }
875
876 function handler_check(&$page, $liste = null)
877 {
bc4ad6aa 878 if (is_null($liste)) {
879 return PL_NOT_FOUND;
880 }
881
7b9d64a8 882 $this->prepare_client($page);
bc4ad6aa 883
1490093c 884 $page->changeTpl('lists/check.tpl');
bc4ad6aa 885
886 if (Post::has('correct')) {
2669eb7d 887 S::assert_xsrf_token();
bc4ad6aa 888 $this->client->check_options($liste, true);
889 }
890
891 if (list($details,$options) = $this->client->check_options($liste)) {
892 $page->assign_by_ref('details', $details);
893 $page->assign_by_ref('options', $options);
894 } else {
6e828e47 895 $page->kill("La liste n'existe pas.");
bc4ad6aa 896 }
897 }
92423144 898
7f1ff426
FB
899 function handler_admin_all(&$page)
900 {
1490093c 901 $page->changeTpl('lists/admin_all.tpl');
46f272fe 902 $page->setTitle('Administration - Mailing lists');
9bb8bf21 903
7f1ff426
FB
904 $this->prepare_client($page);
905 $listes = $this->client->get_all_lists();
9bb8bf21 906 $page->assign_by_ref('listes', $listes);
92423144 907 }
bc4ad6aa 908}
909
a7de4ef7 910// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
bc4ad6aa 911?>