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