2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class ListsModule
extends PLModule
29 'lists' => $this->make_hook('lists', AUTH_MDP
),
30 'lists/ajax' => $this->make_hook('ajax', AUTH_MDP
, '', NO_AUTH
),
31 'lists/create' => $this->make_hook('create', AUTH_MDP
),
33 'lists/members' => $this->make_hook('members', AUTH_COOKIE
),
34 'lists/trombi' => $this->make_hook('trombi', AUTH_COOKIE
),
35 'lists/archives' => $this->make_hook('archives', AUTH_COOKIE
),
37 'lists/moderate' => $this->make_hook('moderate', AUTH_MDP
),
38 'lists/admin' => $this->make_hook('admin', AUTH_MDP
),
39 'lists/options' => $this->make_hook('options', AUTH_MDP
),
40 'lists/delete' => $this->make_hook('delete', AUTH_MDP
),
42 'lists/soptions' => $this->make_hook('soptions', AUTH_MDP
),
43 'lists/check' => $this->make_hook('check', AUTH_MDP
),
44 'admin/lists' => $this->make_hook('admin_all', AUTH_MDP
, 'admin'),
48 function on_subscribe($forlife, $uid, $promo, $password)
50 $this->prepare_client(null
);
51 $this->client
->subscribe("promo$promo");
54 function prepare_client(&$page)
58 require_once dirname(__FILE__
).'/lists/lists.inc.php';
60 $this->client
= new MMList(S
::v('uid'), S
::v('password'));
61 return $globals->mail
->domain
;
64 function clean_html($res)
66 $res = html_entity_decode($res);
67 $res = preg_replace('@<a[^>]*href=["\']([^ >]+)["\'][^>]*>([^<]*)</a>@ie',
68 "'\\2 [' . htmlentities('\\1') . ']'", $res);
69 $res = preg_replace("@<(/br|p|/div)[^>]*>(\\s*\n)?@i", "\n", $res);
70 $res = trim(strip_tags($res));
71 $res = preg_replace("/\n(\\s*\n)+/", "\n\n", $res);
75 function handler_lists(&$page)
77 function filter_owner($list)
82 function filter_member($list)
87 $this->prepare_client($page);
89 $page->changeTpl('listes/index.tpl');
90 $page->addJsLink('ajax.js');
91 $page->assign('xorg_title','Polytechnique.org - Listes de diffusion');
94 if (Get
::has('del')) {
95 $this->client
->unsubscribe(Get
::v('del'));
98 if (Get
::has('add')) {
99 $this->client
->subscribe(Get
::v('add'));
100 pl_redirect('lists');
102 if (Post
::has('promo_add')) {
103 $promo = Post
::i('promo_add');
104 if ($promo >= 1900 and $promo < 2100) {
105 $this->client
->subscribe("promo$promo");
107 $page->trig("promo incorrecte, il faut une promo sur 4 chiffres.");
110 $listes = $this->client
->get_lists();
111 $owner = array_filter($listes, 'filter_owner');
112 $listes = array_diff_key($listes, $owner);
113 $member = array_filter($listes, 'filter_member');
114 $listes = array_diff_key($listes, $member);
115 foreach ($owner as $key=>$liste) {
116 list($subs,$mails) = $this->client
->get_pending_ops($liste['list']);
117 $owner[$key]['subscriptions'] = $subs;
118 $owner[$key]['mails'] = $mails;
120 $page->register_modifier('hdc', 'list_header_decode');
121 $page->assign_by_ref('owner', $owner);
122 $page->assign_by_ref('member', $member);
123 $page->assign_by_ref('public', $listes);
126 function handler_ajax(&$page, $list = null
)
128 header('Content-Type: text/html; charset="iso-8859-15"');
129 $domain = $this->prepare_client($page);
130 $page->changeTpl('listes/liste.inc.tpl', NO_SKIN
);
131 if (Get
::has('unsubscribe')) {
132 $this->client
->unsubscribe($list);
134 if (Get
::has('subscribe')) {
135 $this->client
->subscribe($list);
137 if (Get
::has('sadd')) { /* 4 = SUBSCRIBE */
138 $this->client
->handle_request($list, Get
::v('sadd'), 4, '');
140 if (Get
::has('mid')) {
141 $this->moderate_mail($domain, $list, Get
::i('mid'));
144 list($liste, $members, $owners) = $this->client
->get_members($list);
146 list($subs,$mails) = $this->client
->get_pending_ops($list);
147 $liste['subscriptions'] = $subs;
148 $liste['mails'] = $mails;
150 $page->register_modifier('hdc', 'list_header_decode');
151 $page->assign_by_ref('liste', $liste);
154 function handler_create(&$page)
156 $page->changeTpl('listes/create.tpl');
158 $owners = preg_split("/[\s]+/", Post
::v('owners'), -1, PREG_SPLIT_NO_EMPTY
);
159 $members = preg_split("/[\s]+/", Post
::v('members'), -1, PREG_SPLIT_NO_EMPTY
);
161 // click on validate button 'add_owner_sub' or type <enter>
162 if (Post
::has('add_owner_sub') && Post
::has('add_owner')) {
163 require_once('user.func.inc.php');
164 // if we want to add an owner and then type <enter>, then both
165 // add_owner_sub and add_owner are filled.
166 $oforlifes = get_users_forlife_list(Post
::v('add_owner'), true
);
167 $mforlifes = get_users_forlife_list(Post
::v('add_member'), true
);
168 if (!is_null($oforlifes)) {
169 $owners = array_merge($owners, $oforlifes);
171 // if we want to add a member and then type <enter>, then
172 // add_owner_sub is filled, whereas add_owner is empty.
173 if (!is_null($mforlifes)) {
174 $members = array_merge($members, $mforlifes);
178 // click on validate button 'add_member_sub'
179 if (Post
::has('add_member_sub') && Post
::has('add_member')) {
180 require_once('user.func.inc.php');
181 $forlifes = get_users_forlife_list(Post
::v('add_member'), true
);
182 if (!is_null($forlifes)) {
183 $members = array_merge($members, $forlifes);
188 $owners = array_unique($owners);
190 $members = array_unique($members);
192 $page->assign('owners', join(' ', $owners));
193 $page->assign('members', join(' ', $members));
195 if (!Post
::has('submit')) {
199 $liste = Post
::v('liste');
202 $page->trig('champs «addresse souhaitée» vide');
204 if (!preg_match("/^[a-zA-Z0-9\-]*$/", $liste)) {
205 $page->trig('le nom de la liste ne doit contenir que des lettres, chiffres et tirets');
208 $res = XDB
::query("SELECT COUNT(*) FROM aliases WHERE alias={?}", $liste);
209 $n = $res->fetchOneCell();
212 $page->trig('cet alias est déjà pris');
215 if (!Post
::v(desc
)) {
216 $page->trig('le sujet est vide');
219 if (!count($owners)) {
220 $page->trig('pas de gestionnaire');
223 if (count($members)<4) {
224 $page->trig('pas assez de membres');
227 if (!$page->nb_errs()) {
228 $page->assign('created', true
);
229 require_once 'validations.inc.php';
230 $req = new ListeReq(S
::v('uid'), $liste,
231 Post
::v('desc'), Post
::i('advertise'),
232 Post
::i('modlevel'), Post
::i('inslevel'),
238 function handler_members(&$page, $liste = null
)
240 if (is_null($liste)) {
244 $this->prepare_client($page);
246 $page->changeTpl('listes/members.tpl');
248 if (Get
::has('del')) {
249 $this->client
->unsubscribe($liste);
250 pl_redirect('lists/members/'.$liste);
253 if (Get
::has('add')) {
254 $this->client
->subscribe($liste);
255 pl_redirect('lists/members/'.$liste);
258 $members = $this->client
->get_members($liste);
260 $tri_promo = !Env
::b('alpha');
262 if (list($det,$mem,$own) = $members) {
263 $membres = list_sort_members($mem, $tri_promo);
264 $moderos = list_sort_owners($own, $tri_promo);
266 $page->assign_by_ref('details', $det);
267 $page->assign_by_ref('members', $membres);
268 $page->assign_by_ref('owners', $moderos);
269 $page->assign('nb_m', count($mem));
271 $page->kill("La liste n'existe pas ou tu n'as pas le droit d'en voir les détails");
275 function _get_list($offset, $limit)
278 list($total, $members) = $this->client
->get_members_limit($platal->argv
[1], $offset, $limit);
281 foreach ($members as $member) {
282 list($m) = explode('@',$member[1]);
283 $res = XDB
::query("SELECT prenom,if (nom_usage='', nom, nom_usage) AS nom,
284 promo, a.alias AS forlife
285 FROM auth_user_md5 AS u
286 INNER JOIN aliases AS a ON u.user_id = a.id
287 WHERE a.alias = {?}", $m);
288 if ($tmp = $res->fetchOneAssoc()) {
289 $membres[$tmp['nom']] = $tmp;
291 $membres[$member[0]] = array('addr' => $member[0]);
294 return array($total, $membres);
297 function handler_trombi(&$page, $liste = null
)
299 if (is_null($liste)) {
303 $this->prepare_client($page);
305 $page->changeTpl('listes/trombi.tpl');
307 if (Get
::has('del')) {
308 $this->client
->unsubscribe($liste);
309 pl_redirect('lists/tromi/'.$liste);
311 if (Get
::has('add')) {
312 $this->client
->subscribe($liste);
313 pl_redirect('lists/tromi/'.$liste);
316 $owners = $this->client
->get_owners($liste);
318 if (is_array($owners)) {
319 $moderos = list_sort_owners($owners[1]);
321 $page->assign_by_ref('details', $owners[0]);
322 $page->assign_by_ref('owners', $moderos);
324 $trombi = new Trombi(array(&$this, '_get_list'));
325 $page->assign('trombi', $trombi);
327 $page->kill("La liste n'existe pas ou tu n'as pas le droit d'en voir les détails");
331 function handler_archives(&$page, $liste = null
)
335 if (is_null($liste)) {
339 $domain = $this->prepare_client($page);
341 $page->changeTpl('listes/archives.tpl');
343 $page->addCssLink('lists.archives.css');
344 if (list($det) = $this->client
->get_members($liste)) {
345 if (substr($liste,0,5) != 'promo' && ($det['ins'] ||
$det['priv'])
346 && !$det['own'] && ($det['sub'] < 2))
348 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter");
349 } elseif (Get
::has('file')) {
350 $file = Get
::v('file');
351 $rep = Get
::v('rep');
352 if (strstr('/', $file)!==false ||
!preg_match(',^\d+/\d+$,', $rep)) {
353 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter");
355 $page->assign('archives', $globals->lists
->spool
356 ."/{$domain}{$globals->lists->vhost_sep}$liste/$rep/$file");
360 foreach (glob($globals->lists
->spool
361 ."/{$domain}{$globals->lists->vhost_sep}$liste/*/*") as $rep)
363 if (preg_match(",/(\d*)/(\d*)$,", $rep, $matches)) {
364 $archs[intval($matches[1])][intval($matches[2])] = true
;
367 $page->assign('archs', $archs);
368 $page->assign('range', range(1,12));
371 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter");
375 function moderate_mail($domain, $liste, $mid)
377 $mail = $this->client
->get_pending_mail($liste, $mid);
380 $prenom = S
::v('prenom');
383 if (Env
::has('mok')) {
384 $action = 1; /** 2 = ACCEPT **/
385 $subject = "Message accepté";
386 $append .= "a été accepté par $prenom $nom.\n";
387 } elseif (Env
::has('mno')) {
388 $action = 2; /** 2 = REJECT **/
389 $subject = "Message refusé";
390 $reason = Post
::v('reason');
391 $append = "a été refusé par $prenom $nom avec la raison :\n\n"
393 } elseif (Env
::has('mdel')) {
394 $action = 3; /** 3 = DISCARD **/
395 $subject = "Message supprimé";
396 $append = "a été supprimé par $prenom $nom.\n\n"
397 . "Rappel: il ne faut utiliser cette opération "
398 . "que dans le cas de spams ou de virus !\n";
401 if (isset($action) && $this->client
->handle_request($liste, $mid, $action, $reason)) {
402 $texte = "le message suivant :\n\n"
403 ." Auteur: {$mail['sender']}\n"
404 ." Sujet : « {$mail['subj']} »\n"
405 ." Date : ".strftime("le %d %b %Y à %H:%M:%S", (int)$mail['stamp'])."\n\n"
407 require_once 'diogenes/diogenes.hermes.inc.php';
408 $mailer = new HermesMailer();
409 $mailer->addTo("$liste-owner@{$domain}");
410 $mailer->setFrom("$liste-bounces@{$domain}");
411 $mailer->addHeader('Reply-To', "$liste-owner@{$domain}");
412 $mailer->setSubject($subject);
413 $mailer->setTxtBody(wordwrap($texte,72));
421 function handler_moderate(&$page, $liste = null
)
423 if (is_null($liste)) {
427 $domain = $this->prepare_client($page);
429 $page->changeTpl('listes/moderate.tpl');
431 $page->register_modifier('qpd', 'quoted_printable_decode');
432 $page->register_modifier('hdc', 'list_header_decode');
433 $page->register_modifier('clean_html', array($this, 'clean_html'));
435 if (Env
::has('sadd')) { /* 4 = SUBSCRIBE */
436 $this->client
->handle_request($liste,Env
::v('sadd'),4,'');
437 pl_redirect('lists/moderate/'.$liste);
439 if (Post
::has('sdel')) { /* 2 = REJECT */
440 $this->client
->handle_request($liste,Post
::v('sdel'),2,Post
::v('reason'));
443 if (Post
::has('moderate_mails') && Post
::has('select_mails')) {
444 $mails = array_keys(Post
::v('select_mails'));
445 foreach($mails as $mail) {
446 $this->moderate_mail($domain, $liste, $mail);
448 } elseif (Env
::has('mid')) {
449 $mail = $this->moderate_mail($domain, $liste, Env
::i('mid'));
451 if (Get
::has('mid') && is_array($mail)) {
452 $msg = file_get_contents('/etc/mailman/fr/refuse.txt');
453 $msg = str_replace("%(adminaddr)s", "$liste-owner@{$domain}", $msg);
454 $msg = str_replace("%(request)s", "<< SUJET DU MAIL >>", $msg);
455 $msg = str_replace("%(reason)s", "<< TON EXPLICATION >>", $msg);
456 $msg = str_replace("%(listname)s", $liste, $msg);
458 $page->assign('msg', $msg);
460 $page->changeTpl('listes/moderate_mail.tpl');
461 $page->assign_by_ref('mail', $mail);
464 } elseif (Env
::has('sid')) {
465 if (list($subs,$mails) = $this->client
->get_pending_ops($liste)) {
466 foreach($subs as $user) {
467 if ($user['id'] == Env
::v('sid')) {
468 $page->changeTpl('listes/moderate_sub.tpl');
469 $page->assign('del_user', $user);
477 if (list($subs,$mails) = $this->client
->get_pending_ops($liste)) {
478 $page->assign_by_ref('subs', $subs);
479 $page->assign_by_ref('mails', $mails);
481 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la modérer");
485 function handler_admin(&$page, $liste = null
)
489 if (is_null($liste)) {
493 $this->prepare_client($page);
495 $page->changeTpl('listes/admin.tpl');
497 if (Env
::has('add_member')) {
498 require_once('user.func.inc.php');
499 $members = get_users_forlife_list(Env
::v('add_member'));
500 $arr = $this->client
->mass_subscribe($liste, $members);
501 if (is_array($arr)) {
502 foreach($arr as $addr) {
503 $page->trig("{$addr[0]} inscrit.");
508 if (Env
::has('del_member')) {
509 if (strpos(Env
::v('del_member'), '@') === false
) {
510 $this->client
->mass_unsubscribe(
511 $liste, array(Env
::v('del_member').'@'.$globals->mail
->domain
));
513 $this->client
->mass_unsubscribe($liste, array(Env
::v('del_member')));
515 pl_redirect('lists/admin/'.$liste);
518 if (Env
::has('add_owner')) {
519 require_once('user.func.inc.php');
520 $owners = get_users_forlife_list(Env
::v('add_owner'));
522 foreach ($owners as $login) {
523 if ($this->client
->add_owner($liste, $login)) {
524 $page->trig($alias." ajouté aux modérateurs.");
530 if (Env
::has('del_owner')) {
531 if (strpos(Env
::v('del_owner'), '@') === false
) {
532 $this->client
->del_owner($liste, Env
::v('del_owner').'@'.$globals->mail
->domain
);
534 $this->client
->del_owner($liste, Env
::v('del_owner'));
536 pl_redirect('lists/admin/'.$liste);
539 if (list($det,$mem,$own) = $this->client
->get_members($liste)) {
540 $membres = list_sort_members($mem, @$tri_promo);
541 $moderos = list_sort_owners($own, @$tri_promo);
543 $page->assign_by_ref('details', $det);
544 $page->assign_by_ref('members', $membres);
545 $page->assign_by_ref('owners', $moderos);
546 $page->assign('np_m', count($mem));
549 $page->kill("La liste n'existe pas ou tu n'as pas le droit de l'administrer");
553 function handler_options(&$page, $liste = null
)
555 if (is_null($liste)) {
559 $this->prepare_client($page);
561 $page->changeTpl('listes/options.tpl');
563 if (Post
::has('submit')) {
565 $this->client
->set_bogo_level($liste, intval($values['bogo_level']));
566 switch($values['moderate']) {
568 $values['generic_nonmember_action'] = 0;
569 $values['default_member_moderation'] = 0;
572 $values['generic_nonmember_action'] = 1;
573 $values['default_member_moderation'] = 0;
576 $values['generic_nonmember_action'] = 1;
577 $values['default_member_moderation'] = 1;
580 unset($values['submit'], $values['bogo_level'], $values['moderate']);
581 $values['send_goodbye_msg'] = !empty($values['send_goodbye_msg']);
582 $values['admin_notify_mchanges'] = !empty($values['admin_notify_mchanges']);
583 $values['subscribe_policy'] = empty($values['subscribe_policy']) ?
0 : 2;
584 if (isset($values['subject_prefix'])) {
585 $values['subject_prefix'] = trim($values['subject_prefix']).' ';
587 $this->client
->set_owner_options($liste, $values);
588 } elseif (isvalid_email(Post
::v('atn_add'))) {
589 $this->client
->add_to_wl($liste, Post
::v('atn_add'));
590 } elseif (Get
::has('atn_del')) {
591 $this->client
->del_from_wl($liste, Get
::v('atn_del'));
592 pl_redirect('lists/options/'.$liste);
595 if (list($details,$options) = $this->client
->get_owner_options($liste)) {
596 $page->assign_by_ref('details', $details);
597 $page->assign_by_ref('options', $options);
598 $page->assign('bogo_level', $this->client
->get_bogo_level($liste));
600 $page->kill("La liste n'existe pas ou tu n'as pas le droit de l'administrer");
604 function handler_delete(&$page, $liste = null
)
607 if (is_null($liste)) {
611 $domain = $this->prepare_client($page);
612 if ($domain == $globals->mail
->domain ||
$domain == $globals->mail
->domain2
) {
617 $domain = '@' . $domain;
622 $page->changeTpl('listes/delete.tpl');
623 if (Post
::v('valid') == 'OUI') {
624 if ($this->client
->delete_list($liste, Post
::b('del_archive'))) {
625 foreach (array('', '-owner', '-admin', '-bounces') as $app) {
626 XDB
::execute("DELETE FROM $table
627 WHERE type={?} AND alias={?}",
628 $type, $liste.$app.$domain);
630 $page->assign('deleted', true
);
632 $page->kill('Une erreur est survenue lors de la suppression de la liste.<br />'
633 . 'Contact les administrateurs du site pour régler le problème : '
634 . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>');
636 } elseif (list($details,$options) = $this->client
->get_owner_options($liste)) {
637 $page->assign_by_ref('details', $details);
638 $page->assign_by_ref('options', $options);
639 $page->assign('bogo_level', $this->client
->get_bogo_level($liste));
641 $page->kill("La liste n'existe pas ou tu n'as pas le droit de l'administrer");
645 function handler_soptions(&$page, $liste = null
)
647 if (is_null($liste)) {
651 $this->prepare_client($page);
653 $page->changeTpl('listes/soptions.tpl');
655 if (Post
::has('submit')) {
657 unset($values['submit']);
658 $values['advertised'] = empty($values['advertised']) ? false
: true
;
659 $values['archive'] = empty($values['archive']) ? false
: true
;
660 $this->client
->set_admin_options($liste, $values);
663 if (list($details,$options) = $this->client
->get_admin_options($liste)) {
664 $page->assign_by_ref('details', $details);
665 $page->assign_by_ref('options', $options);
667 $page->kill("La liste n'existe pas");
671 function handler_check(&$page, $liste = null
)
673 if (is_null($liste)) {
677 $this->prepare_client($page);
679 $page->changeTpl('listes/check.tpl');
681 if (Post
::has('correct')) {
682 $this->client
->check_options($liste, true
);
685 if (list($details,$options) = $this->client
->check_options($liste)) {
686 $page->assign_by_ref('details', $details);
687 $page->assign_by_ref('options', $options);
689 $page->kill("La liste n'existe pas");
693 function handler_admin_all(&$page) {
694 $page->changeTpl('listes/admin_all.tpl');
695 $page->assign('xorg_title','Polytechnique.org - Administration - Mailing lists');
697 $client = new MMList(S
::v('uid'), S
::v('password'));
698 $listes = $client->get_all_lists();
699 $page->assign_by_ref('listes', $listes);