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