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