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