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