Merge remote branch 'origin/platal-1.0.0'
[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_basic_info(list_extract_members($members[1]));
360 pl_content_headers("text/x-csv");
361
362 echo "email,nom,promo\n";
363 echo implode("\n", $list);
364 exit;
365 }
366
367 function handler_annu(&$page, $liste = null, $action = null, $subaction = null)
368 {
369 if (is_null($liste)) {
370 return PL_NOT_FOUND;
371 }
372
373 $this->prepare_client($page);
374
375 if (Get::has('del')) {
376 S::assert_xsrf_token();
377 $this->client->unsubscribe($liste);
378 pl_redirect('lists/annu/'.$liste);
379 }
380 if (Get::has('add')) {
381 S::assert_xsrf_token();
382 $this->client->subscribe($liste);
383 pl_redirect('lists/annu/'.$liste);
384 }
385
386 $owners = $this->client->get_owners($liste);
387 if (!is_array($owners)) {
388 $page->kill("La liste n'existe pas ou tu n'as pas le droit d'en voir les détails.");
389 }
390
391 global $platal;
392 list(,$members) = $this->client->get_members($liste);
393 $users = array();
394 foreach ($members as $m) {
395 $users[] = $m[1];
396 }
397 require_once 'userset.inc.php';
398 $view = new ArraySet($users);
399 $view->addMod('trombi', 'Trombinoscope', true, array('with_promo' => true));
400 if (empty($GLOBALS['IS_XNET_SITE'])) {
401 $view->addMod('minifiche', 'Mini-fiches', false);
402 }
403 // TODO: Reactivate when the new map is completed.
404 // $view->addMod('geoloc', 'Planisphère');
405 $view->apply("lists/annu/$liste", $page, $action, $subaction);
406 if ($action == 'geoloc' && $subaction) {
407 return;
408 }
409
410 $page->changeTpl('lists/annu.tpl');
411 $moderos = list_sort_owners($owners[1]);
412 $page->assign_by_ref('details', $owners[0]);
413 $page->assign_by_ref('owners', $moderos);
414 }
415
416 function handler_archives(&$page, $liste = null, $action = null, $artid = null)
417 {
418 global $globals;
419
420 if (is_null($liste)) {
421 return PL_NOT_FOUND;
422 }
423
424 $domain = $this->prepare_client($page);
425
426 $page->changeTpl('lists/archives.tpl');
427
428 if (list($det) = $this->client->get_members($liste)) {
429 if (substr($liste,0,5) != 'promo' && ($det['ins'] || $det['priv'])
430 && !$det['own'] && ($det['sub'] < 2)) {
431 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter.");
432 }
433 $get = Array('listname' => $liste, 'domain' => $domain);
434 if (Post::has('updateall')) {
435 $get['updateall'] = Post::v('updateall');
436 }
437 require_once 'banana/ml.inc.php';
438 get_banana_params($get, null, $action, $artid);
439 run_banana($page, 'MLBanana', $get);
440 } else {
441 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter.");
442 }
443 }
444
445 function handler_rss(&$page, $liste = null, $alias = null, $hash = null)
446 {
447 if (!$liste) {
448 return PL_NOT_FOUND;
449 }
450 $user = Platal::session()->tokenAuth($alias, $hash);
451 if (is_null($user)) {
452 return PL_FORBIDDEN;
453 }
454
455 $domain = $this->prepare_client($page, $user);
456 if (list($det) = $this->client->get_members($liste)) {
457 if (substr($liste,0,5) != 'promo' && ($det['ins'] || $det['priv'])
458 && !$det['own'] && ($det['sub'] < 2)) {
459 exit;
460 }
461 require_once('banana/ml.inc.php');
462 $banana = new MLBanana($user, Array('listname' => $liste, 'domain' => $domain, 'action' => 'rss2'));
463 $banana->run();
464 }
465 exit;
466 }
467
468 function moderate_mail($domain, $liste, $mid)
469 {
470 if (Env::has('mok')) {
471 $action = 'accept';
472 } elseif (Env::has('mno')) {
473 $action = 'refuse';
474 } elseif (Env::has('mdel')) {
475 $action = 'delete';
476 } else {
477 return false;
478 }
479 Get::kill('mid');
480 return XDB::execute("INSERT IGNORE INTO email_list_moderate
481 VALUES ({?}, {?}, {?}, {?}, {?}, NOW(), {?}, NULL)",
482 $liste, $domain, $mid, S::i('uid'), $action, Post::v('reason'));
483 }
484
485 function handler_moderate(&$page, $liste = null)
486 {
487 if (is_null($liste)) {
488 return PL_NOT_FOUND;
489 }
490
491 $domain = $this->prepare_client($page);
492
493 $page->changeTpl('lists/moderate.tpl');
494
495 $page->register_modifier('hdc', 'list_header_decode');
496
497 if (Env::has('sadd') || Env::has('sdel')) {
498 S::assert_xsrf_token();
499
500 if (Env::has('sadd')) { /* 4 = SUBSCRIBE */
501 $sub = $this->client->get_pending_sub($liste, Env::v('sadd'));
502 $this->client->handle_request($liste,Env::v('sadd'),4,'');
503 $info = "validée";
504 }
505 if (Post::has('sdel')) { /* 2 = REJECT */
506 $sub = $this->client->get_pending_sub($liste, Env::v('sdel'));
507 $this->client->handle_request($liste, Post::v('sdel'), 2, utf8_decode(Post::v('reason')));
508 $info = "refusée";
509 }
510 if ($sub) {
511 $mailer = new PlMailer();
512 $mailer->setFrom("$liste-bounces@{$domain}");
513 $mailer->addTo("$liste-owner@{$domain}");
514 $mailer->addHeader('Reply-To', "$liste-owner@{$domain}");
515 $mailer->setSubject("L'inscription de {$sub['name']} a été $info");
516 $text = "L'inscription de {$sub['name']} à la liste $liste@{$domain} a été $info par " . S::v('prenom') . ' '
517 . S::v('nom') . '(' . S::v('promo') . ")\n";
518 if (trim(Post::v('reason'))) {
519 $text .= "\nLa raison invoquée est :\n" . Post::v('reason');
520 }
521 $mailer->setTxtBody(wordwrap($text, 72));
522 $mailer->send();
523 }
524 if (Env::has('sadd')) {
525 pl_redirect('lists/moderate/'.$liste);
526 }
527 }
528
529 if (Post::has('moderate_mails') && Post::has('select_mails')) {
530 S::assert_xsrf_token();
531
532 $mails = array_keys(Post::v('select_mails'));
533 foreach($mails as $mail) {
534 $this->moderate_mail($domain, $liste, $mail);
535 }
536 } elseif (Env::has('mid')) {
537 if (Get::has('mid') && !Env::has('mok') && !Env::has('mdel')) {
538 require_once 'banana/moderate.inc.php';
539
540 $page->changeTpl('lists/moderate_mail.tpl');
541 $params = array('listname' => $liste, 'domain' => $domain,
542 'artid' => Get::i('mid'), 'part' => Get::v('part'), 'action' => Get::v('action'));
543 $params['client'] = $this->client;
544 run_banana($page, 'ModerationBanana', $params);
545
546 $msg = file_get_contents('/etc/mailman/fr/refuse.txt');
547 $msg = str_replace("%(adminaddr)s", "$liste-owner@{$domain}", $msg);
548 $msg = str_replace("%(request)s", "<< SUJET DU MAIL >>", $msg);
549 $msg = str_replace("%(reason)s", "<< TON EXPLICATION >>", $msg);
550 $msg = str_replace("%(listname)s", $liste, $msg);
551 $page->assign('msg', $msg);
552 return;
553 }
554
555 $this->moderate_mail($domain, $liste, Env::i('mid'));
556 } elseif (Env::has('sid')) {
557 if (list($subs,$mails) = $this->get_pending_ops($domain, $liste)) {
558 foreach($subs as $user) {
559 if ($user['id'] == Env::v('sid')) {
560 $page->changeTpl('lists/moderate_sub.tpl');
561 $page->assign('del_user', $user);
562 return;
563 }
564 }
565 }
566
567 }
568
569 if (list($subs,$mails) = $this->get_pending_ops($domain, $liste)) {
570 foreach ($mails as $key=>$mail) {
571 $mails[$key]['stamp'] = strftime("%Y%m%d%H%M%S", $mail['stamp']);
572 if ($mail['fromx']) {
573 $page->assign('with_fromx', true);
574 } else {
575 $page->assign('with_nonfromx', true);
576 }
577 }
578 $page->assign_by_ref('subs', $subs);
579 $page->assign_by_ref('mails', $mails);
580 } else {
581 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la modérer.");
582 }
583 }
584
585 static public function no_login_callback($login)
586 {
587 global $list_unregistered, $globals;
588
589 $users = User::getPendingAccounts($login, true);
590 if ($users && $users->total()) {
591 if (!isset($list_unregistered)) {
592 $list_unregistered = array();
593 }
594 $list_unregistered[$login] = $users;
595 } else {
596 list($name, $dom) = @explode('@', $login);
597 if ($dom == $globals->mail->domain || $dom == $globals->mail->domain2) {
598 User::_default_user_callback($login);
599 }
600 }
601 }
602
603 function handler_admin(&$page, $liste = null)
604 {
605 global $globals;
606
607 if (is_null($liste)) {
608 return PL_NOT_FOUND;
609 }
610
611 $domain = $this->prepare_client($page);
612
613 $page->changeTpl('lists/admin.tpl');
614
615 if (Env::has('send_mark')) {
616 S::assert_xsrf_token();
617
618 $actions = Env::v('mk_action');
619 $uids = Env::v('mk_uid');
620 $mails = Env::v('mk_email');
621 foreach ($actions as $key=>$action) {
622 switch ($action) {
623 case 'none':
624 break;
625
626 case 'marketu': case 'markets':
627 require_once 'emails.inc.php';
628 $mail = valide_email($mails[$key]);
629 if (isvalid_email_redirection($mail)) {
630 $from = ($action == 'marketu') ? 'user' : 'staff';
631 $market = Marketing::get($uids[$key], $mail);
632 if (!$market) {
633 $market = new Marketing($uids[$key], $mail, 'list', "$liste@$domain", $from, S::v('uid'));
634 $market->add();
635 break;
636 }
637 }
638
639 default:
640 XDB::execute('INSERT IGNORE INTO register_subs (uid, type, sub, domain)
641 VALUES ({?}, \'list\', {?}, {?})',
642 $uids[$key], $liste, $domain);
643 }
644 }
645 }
646
647 if (Env::has('add_member')) {
648 S::assert_xsrf_token();
649
650 $members = User::getBulkForlifeEmails(Env::v('add_member'),
651 true,
652 array('ListsModule', 'no_login_callback'));
653 $arr = $this->client->mass_subscribe($liste, $members);
654 if (is_array($arr)) {
655 foreach($arr as $addr) {
656 $page->trigSuccess("{$addr[0]} inscrit.");
657 }
658 }
659 }
660
661 if (isset($_FILES['add_member_file']) && $_FILES['add_member_file']['tmp_name']) {
662 S::assert_xsrf_token();
663
664 $upload =& PlUpload::get($_FILES['add_member_file'], S::user()->login(), 'list.addmember', true);
665 if (!$upload) {
666 $page->trigError('Une erreur s\'est produite lors du téléchargement du fichier');
667 } else {
668 $members = User::getBulkForlifeEmails($upload->getContents(),
669 true,
670 array('ListsModule', 'no_login_callback'));
671 $arr = $this->client->mass_subscribe($liste, $members);
672 if (is_array($arr)) {
673 foreach($arr as $addr) {
674 $page->trigSuccess("{$addr[0]} inscrit.");
675 }
676 }
677 }
678 }
679
680 if (Env::has('del_member')) {
681 S::assert_xsrf_token();
682
683 if (strpos(Env::v('del_member'), '@') === false) {
684 $this->client->mass_unsubscribe(
685 $liste, array(Env::v('del_member').'@'.$globals->mail->domain));
686 } else {
687 $this->client->mass_unsubscribe($liste, array(Env::v('del_member')));
688 }
689 pl_redirect('lists/admin/'.$liste);
690 }
691
692 if (Env::has('add_owner')) {
693 S::assert_xsrf_token();
694
695 $owners = User::getBulkForlifeEmails(Env::v('add_owner'), false, array('ListsModule', 'no_login_callback'));
696 if ($owners) {
697 foreach ($owners as $login) {
698 if ($this->client->add_owner($liste, $login)) {
699 $page->trigSuccess($login ." ajouté aux modérateurs.");
700 }
701 }
702 }
703 }
704
705 if (Env::has('del_owner')) {
706 S::assert_xsrf_token();
707
708 if (strpos(Env::v('del_owner'), '@') === false) {
709 $this->client->del_owner($liste, Env::v('del_owner').'@'.$globals->mail->domain);
710 } else {
711 $this->client->del_owner($liste, Env::v('del_owner'));
712 }
713 pl_redirect('lists/admin/'.$liste);
714 }
715
716 if (list($det,$mem,$own) = $this->client->get_members($liste)) {
717 global $list_unregistered;
718 if ($list_unregistered) {
719 $page->assign_by_ref('unregistered', $list_unregistered);
720 }
721 $membres = list_sort_members($mem, @$tri_promo);
722 $moderos = list_sort_owners($own, @$tri_promo);
723
724 $page->assign_by_ref('details', $det);
725 $page->assign_by_ref('members', $membres);
726 $page->assign_by_ref('owners', $moderos);
727 $page->assign('np_m', count($mem));
728
729 } else {
730 $page->kill("La liste n'existe pas ou tu n'as pas le droit de l'administrer.<br />"
731 . " Si tu penses qu'il s'agit d'une erreur, "
732 . "<a href='mailto:support@polytechnique.org'>contact le support</a>.");
733 }
734 }
735
736 function handler_options(&$page, $liste = null)
737 {
738 if (is_null($liste)) {
739 return PL_NOT_FOUND;
740 }
741
742 $this->prepare_client($page);
743
744 $page->changeTpl('lists/options.tpl');
745
746 if (Post::has('submit')) {
747 S::assert_xsrf_token();
748
749 $values = $_POST;
750 $values = array_map('utf8_decode', $values);
751 $spamlevel = intval($values['bogo_level']);
752 $unsurelevel = intval($values['unsure_level']);
753 if ($spamlevel == 0) {
754 $unsurelevel = 0;
755 }
756 if ($spamlevel > 3 || $spamlevel < 0 || $unsurelevel < 0 || $unsurelevel > 1) {
757 $page->trigError("Réglage de l'antispam non valide");
758 } else {
759 $this->client->set_bogo_level($liste, ($spamlevel << 1) + $unsurelevel);
760 }
761 switch($values['moderate']) {
762 case '0':
763 $values['generic_nonmember_action'] = 0;
764 $values['default_member_moderation'] = 0;
765 break;
766 case '1':
767 $values['generic_nonmember_action'] = 1;
768 $values['default_member_moderation'] = 0;
769 break;
770 case '2':
771 $values['generic_nonmember_action'] = 1;
772 $values['default_member_moderation'] = 1;
773 break;
774 }
775 unset($values['submit'], $values['bogo_level'], $values['moderate']);
776 $values['send_goodbye_msg'] = !empty($values['send_goodbye_msg']);
777 $values['admin_notify_mchanges'] = !empty($values['admin_notify_mchanges']);
778 $values['subscribe_policy'] = empty($values['subscribe_policy']) ? 0 : 2;
779 if (isset($values['subject_prefix'])) {
780 $values['subject_prefix'] = trim($values['subject_prefix']).' ';
781 }
782 $this->client->set_owner_options($liste, $values);
783 } elseif (isvalid_email(Post::v('atn_add'))) {
784 S::assert_xsrf_token();
785 $this->client->add_to_wl($liste, Post::v('atn_add'));
786 } elseif (Get::has('atn_del')) {
787 S::assert_xsrf_token();
788 $this->client->del_from_wl($liste, Get::v('atn_del'));
789 pl_redirect('lists/options/'.$liste);
790 }
791
792 if (list($details,$options) = $this->client->get_owner_options($liste)) {
793 $page->assign_by_ref('details', $details);
794 $page->assign_by_ref('options', $options);
795 $bogo_level = intval($this->client->get_bogo_level($liste));
796 $page->assign('unsure_level', $bogo_level & 1);
797 $page->assign('bogo_level', $bogo_level >> 1);
798 } else {
799 $page->kill("La liste n'existe pas ou tu n'as pas le droit de l'administrer");
800 }
801 }
802
803 function handler_delete(&$page, $liste = null)
804 {
805 global $globals;
806 if (is_null($liste)) {
807 return PL_NOT_FOUND;
808 }
809
810 $domain = $this->prepare_client($page);
811 if ($domain == $globals->mail->domain || $domain == $globals->mail->domain2) {
812 $domain = '';
813 $table = 'aliases';
814 $type = 'liste';
815 } else {
816 $domain = '@' . $domain;
817 $table = 'virtual';
818 $type = 'list';
819 }
820
821 $page->changeTpl('lists/delete.tpl');
822 if (Post::v('valid') == 'OUI') {
823 S::assert_xsrf_token();
824
825 if ($this->client->delete_list($liste, Post::b('del_archive'))) {
826 foreach (array('', '-owner', '-admin', '-bounces', '-unsubscribe') as $app) {
827 XDB::execute("DELETE FROM $table
828 WHERE type={?} AND alias={?}",
829 $type, $liste.$app.$domain);
830 }
831 $page->assign('deleted', true);
832 $page->trigSuccess('La liste a été détruite&nbsp;!');
833 } else {
834 $page->kill('Une erreur est survenue lors de la suppression de la liste.<br />'
835 . 'Contact les administrateurs du site pour régler le problème : '
836 . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>.');
837 }
838 } elseif (list($details,$options) = $this->client->get_owner_options($liste)) {
839 if (!$details['own']) {
840 $page->trigWarning('Tu n\'es pas administrateur de la liste, mais du site.');
841 }
842 $page->assign_by_ref('details', $details);
843 $page->assign_by_ref('options', $options);
844 $page->assign('bogo_level', $this->client->get_bogo_level($liste));
845 } else {
846 $page->kill("La liste n'existe pas ou tu n'as pas le droit de l'administrer.");
847 }
848 }
849
850 function handler_soptions(&$page, $liste = null)
851 {
852 if (is_null($liste)) {
853 return PL_NOT_FOUND;
854 }
855
856 $this->prepare_client($page);
857
858 $page->changeTpl('lists/soptions.tpl');
859
860 if (Post::has('submit')) {
861 S::assert_xsrf_token();
862
863 $values = $_POST;
864 $values = array_map('utf8_decode', $values);
865 unset($values['submit']);
866 $values['advertised'] = empty($values['advertised']) ? false : true;
867 $values['archive'] = empty($values['archive']) ? false : true;
868 $this->client->set_admin_options($liste, $values);
869 }
870
871 if (list($details,$options) = $this->client->get_admin_options($liste)) {
872 $page->assign_by_ref('details', $details);
873 $page->assign_by_ref('options', $options);
874 } else {
875 $page->kill("La liste n'existe pas.");
876 }
877 }
878
879 function handler_check(&$page, $liste = null)
880 {
881 if (is_null($liste)) {
882 return PL_NOT_FOUND;
883 }
884
885 $this->prepare_client($page);
886
887 $page->changeTpl('lists/check.tpl');
888
889 if (Post::has('correct')) {
890 S::assert_xsrf_token();
891 $this->client->check_options($liste, true);
892 }
893
894 if (list($details,$options) = $this->client->check_options($liste)) {
895 $page->assign_by_ref('details', $details);
896 $page->assign_by_ref('options', $options);
897 } else {
898 $page->kill("La liste n'existe pas.");
899 }
900 }
901
902 function handler_admin_all(&$page)
903 {
904 $page->changeTpl('lists/admin_all.tpl');
905 $page->setTitle('Administration - Mailing lists');
906
907 $this->prepare_client($page);
908 $listes = $this->client->get_all_lists();
909 $page->assign_by_ref('listes', $listes);
910 }
911 }
912
913 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
914 ?>