ef748a8bfa5fb100a645ce64a08c9763cfd0028c
[platal.git] / modules / lists.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 var $client;
25
26 function handlers()
27 {
28 return array(
29 'lists' => $this->make_hook('lists', AUTH_MDP),
30 'lists/create' => $this->make_hook('create', AUTH_MDP),
31
32 'lists/members' => $this->make_hook('members', AUTH_COOKIE),
33 'lists/trombi' => $this->make_hook('trombi', AUTH_COOKIE),
34 'lists/archives' => $this->make_hook('archives', AUTH_COOKIE),
35
36 'lists/moderate' => $this->make_hook('moderate', AUTH_MDP),
37 'lists/admin' => $this->make_hook('admin', AUTH_MDP),
38 'lists/options' => $this->make_hook('options', AUTH_MDP),
39 'lists/delete' => $this->make_hook('delete', AUTH_MDP),
40
41 'lists/soptions' => $this->make_hook('soptions', AUTH_MDP),
42 'lists/check' => $this->make_hook('check', AUTH_MDP),
43 'admin/lists' => $this->make_hook('admin_all', AUTH_MDP, 'admin'),
44 );
45 }
46
47 function on_subscribe($forlife, $uid, $promo, $password)
48 {
49 $this->prepare_client(null);
50 $this->client->subscribe("promo$promo");
51 }
52
53 function prepare_client(&$page)
54 {
55 global $globals;
56
57 require_once dirname(__FILE__).'/lists/lists.inc.php';
58
59 $this->client = new MMList(S::v('uid'), S::v('password'));
60 return $globals->mail->domain;
61 }
62
63 function clean_html($res)
64 {
65 $res = html_entity_decode($res);
66 $res = preg_replace('@<a[^>]*href=["\']([^ >]+)["\'][^>]*>([^<]*)</a>@i', "\\2 [\\1]", $res);
67 $res = preg_replace("@<(/br|p|/div)[^>]*>(\\s*\n)?@i", "\n", $res);
68 $res = trim(strip_tags($res));
69 $res = preg_replace("/\n(\\s*\n)+/", "\n\n", $res);
70 return htmlentities($res);
71 }
72
73 function handler_lists(&$page)
74 {
75 $this->prepare_client($page);
76
77 $page->changeTpl('listes/index.tpl');
78 $page->assign('xorg_title','Polytechnique.org - Listes de diffusion');
79
80
81 if (Get::has('del')) {
82 $this->client->unsubscribe(Get::v('del'));
83 pl_redirect('lists');
84 }
85 if (Get::has('add')) {
86 $this->client->subscribe(Get::v('add'));
87 pl_redirect('lists');
88 }
89 if (Post::has('promo_add')) {
90 $promo = Post::i('promo_add');
91 if ($promo >= 1900 and $promo < 2100) {
92 $this->client->subscribe("promo$promo");
93 } else {
94 $page->trig("promo incorrecte, il faut une promo sur 4 chiffres.");
95 }
96 }
97 $listes = $this->client->get_lists();
98 $page->assign_by_ref('listes', $listes);
99 }
100
101 function handler_create(&$page)
102 {
103 $page->changeTpl('listes/create.tpl');
104
105 $owners = preg_split("/[\s]+/", Post::v('owners'), -1, PREG_SPLIT_NO_EMPTY);
106 $members = preg_split("/[\s]+/", Post::v('members'), -1, PREG_SPLIT_NO_EMPTY);
107
108 // click on validate button 'add_owner_sub' or type <enter>
109 if (Post::has('add_owner_sub') && Post::has('add_owner')) {
110 require_once('user.func.inc.php');
111 // if we want to add an owner and then type <enter>, then both
112 // add_owner_sub and add_owner are filled.
113 $oforlifes = get_users_forlife_list(Post::v('add_owner'), true);
114 $mforlifes = get_users_forlife_list(Post::v('add_member'), true);
115 if (!is_null($oforlifes)) {
116 $owners = array_merge($owners, $oforlifes);
117 }
118 // if we want to add a member and then type <enter>, then
119 // add_owner_sub is filled, whereas add_owner is empty.
120 if (!is_null($mforlifes)) {
121 $members = array_merge($members, $mforlifes);
122 }
123 }
124
125 // click on validate button 'add_member_sub'
126 if (Post::has('add_member_sub') && Post::has('add_member')) {
127 require_once('user.func.inc.php');
128 $forlifes = get_users_forlife_list(Post::v('add_member'), true);
129 if (!is_null($forlifes)) {
130 $members = array_merge($members, $forlifes);
131 }
132 }
133
134 ksort($owners);
135 $owners = array_unique($owners);
136 ksort($members);
137 $members = array_unique($members);
138
139 $page->assign('owners', join(' ', $owners));
140 $page->assign('members', join(' ', $members));
141
142 if (!Post::has('submit')) {
143 return;
144 }
145
146 $liste = Post::v('liste');
147
148 if (empty($liste)) {
149 $page->trig('champs «addresse souhaitée» vide');
150 }
151 if (!preg_match("/^[a-zA-Z0-9\-]*$/", $liste)) {
152 $page->trig('le nom de la liste ne doit contenir que des lettres, chiffres et tirets');
153 }
154
155 $res = XDB::query("SELECT COUNT(*) FROM aliases WHERE alias={?}", $liste);
156 $n = $res->fetchOneCell();
157
158 if ($n) {
159 $page->trig('cet alias est déjà pris');
160 }
161
162 if (!Post::v(desc)) {
163 $page->trig('le sujet est vide');
164 }
165
166 if (!count($owners)) {
167 $page->trig('pas de gestionnaire');
168 }
169
170 if (count($members)<4) {
171 $page->trig('pas assez de membres');
172 }
173
174 if (!$page->nb_errs()) {
175 $page->assign('created', true);
176 require_once 'validations.inc.php';
177 $req = new ListeReq(S::v('uid'), $liste,
178 Post::v('desc'), Post::i('advertise'),
179 Post::i('modlevel'), Post::i('inslevel'),
180 $owners, $members);
181 $req->submit();
182 }
183 }
184
185 function handler_members(&$page, $liste = null)
186 {
187 if (is_null($liste)) {
188 return PL_NOT_FOUND;
189 }
190
191 $this->prepare_client($page);
192
193 $page->changeTpl('listes/members.tpl');
194
195 if (Get::has('del')) {
196 $this->client->unsubscribe($liste);
197 pl_redirect('lists/members/'.$liste);
198 }
199
200 if (Get::has('add')) {
201 $this->client->subscribe($liste);
202 pl_redirect('lists/members/'.$liste);
203 }
204
205 $members = $this->client->get_members($liste);
206
207 $tri_promo = !Env::b('alpha');
208
209 if (list($det,$mem,$own) = $members) {
210 $membres = list_sort_members($mem, $tri_promo);
211 $moderos = list_sort_owners($own, $tri_promo);
212
213 $page->assign_by_ref('details', $det);
214 $page->assign_by_ref('members', $membres);
215 $page->assign_by_ref('owners', $moderos);
216 $page->assign('nb_m', count($mem));
217 } else {
218 $page->kill("La liste n'existe pas ou tu n'as pas le droit d'en voir les détails");
219 }
220 }
221
222 function _get_list($offset, $limit)
223 {
224 global $platal;
225 list($total, $members) = $this->client->get_members_limit($platal->argv[1], $offset, $limit);
226
227 $membres = Array();
228 foreach ($members as $member) {
229 list($m) = explode('@',$member[1]);
230 $res = XDB::query("SELECT prenom,if (nom_usage='', nom, nom_usage) AS nom,
231 promo, a.alias AS forlife
232 FROM auth_user_md5 AS u
233 INNER JOIN aliases AS a ON u.user_id = a.id
234 WHERE a.alias = {?}", $m);
235 if ($tmp = $res->fetchOneAssoc()) {
236 $membres[$tmp['nom']] = $tmp;
237 } else {
238 $membres[$member[0]] = array('addr' => $member[0]);
239 }
240 }
241 return array($total, $membres);
242 }
243
244 function handler_trombi(&$page, $liste = null)
245 {
246 if (is_null($liste)) {
247 return PL_NOT_FOUND;
248 }
249
250 $this->prepare_client($page);
251
252 $page->changeTpl('listes/trombi.tpl');
253
254 if (Get::has('del')) {
255 $this->client->unsubscribe($liste);
256 pl_redirect('lists/tromi/'.$liste);
257 }
258 if (Get::has('add')) {
259 $this->client->subscribe($liste);
260 pl_redirect('lists/tromi/'.$liste);
261 }
262
263 $owners = $this->client->get_owners($liste);
264
265 if (is_array($owners)) {
266 $moderos = list_sort_owners($owners[1]);
267
268 $page->assign_by_ref('details', $owners[0]);
269 $page->assign_by_ref('owners', $moderos);
270
271 $trombi = new Trombi(array(&$this, '_get_list'));
272 $page->assign('trombi', $trombi);
273 } else {
274 $page->kill("La liste n'existe pas ou tu n'as pas le droit d'en voir les détails");
275 }
276 }
277
278 function handler_archives(&$page, $liste = null)
279 {
280 global $globals;
281
282 if (is_null($liste)) {
283 return PL_NOT_FOUND;
284 }
285
286 $domain = $this->prepare_client($page);
287
288 $page->changeTpl('listes/archives.tpl');
289
290 $page->addCssLink('lists.archives.css');
291 if (list($det) = $this->client->get_members($liste)) {
292 if (substr($liste,0,5) != 'promo' && ($det['ins'] || $det['priv'])
293 && !$det['own'] && ($det['sub'] < 2))
294 {
295 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter");
296 } elseif (Get::has('file')) {
297 $file = Get::v('file');
298 $rep = Get::v('rep');
299 if (strstr('/', $file)!==false || !preg_match(',^\d+/\d+$,', $rep)) {
300 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter");
301 } else {
302 $page->assign('archives', $globals->lists->spool
303 ."/{$domain}{$globals->lists->vhost_sep}$liste/$rep/$file");
304 }
305 } else {
306 $archs = Array();
307 foreach (glob($globals->lists->spool
308 ."/{$domain}{$globals->lists->vhost_sep}$liste/*/*") as $rep)
309 {
310 if (preg_match(",/(\d*)/(\d*)$,", $rep, $matches)) {
311 $archs[intval($matches[1])][intval($matches[2])] = true;
312 }
313 }
314 $page->assign('archs', $archs);
315 $page->assign('range', range(1,12));
316 }
317 } else {
318 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter");
319 }
320 }
321
322 function handler_moderate(&$page, $liste = null)
323 {
324 if (is_null($liste)) {
325 return PL_NOT_FOUND;
326 }
327
328 $domain = $this->prepare_client($page);
329
330 $page->changeTpl('listes/moderate.tpl');
331
332 $page->register_modifier('qpd', 'quoted_printable_decode');
333 $page->register_modifier('hdc', 'list_header_decode');
334 $page->register_modifier('clean_html', array($this, 'clean_html'));
335
336 if (Env::has('sadd')) { /* 4 = SUBSCRIBE */
337 $this->client->handle_request($liste,Env::v('sadd'),4,'');
338 pl_redirect('lists/moderate/'.$liste);
339 }
340
341 if (Post::has('sdel')) { /* 2 = REJECT */
342 $this->client->handle_request($liste,Post::v('sdel'),2,Post::v('reason'));
343 }
344
345 if (Env::has('mid')) {
346 $mid = Env::v('mid');
347 $mail = $this->client->get_pending_mail($liste, $mid);
348 $reason = '';
349
350 $prenom = S::v('prenom');
351 $nom = S::v('nom');
352
353 if (Env::has('mok')) {
354 $action = 1; /** 2 = ACCEPT **/
355 $subject = "Message accepté";
356 $append .= "a été accepté par $prenom $nom.\n";
357 } elseif (Env::has('mno')) {
358 $action = 2; /** 2 = REJECT **/
359 $subject = "Message refusé";
360 $reason = Post::v('reason');
361 $append = "a été refusé par $prenom $nom avec la raison :\n\n"
362 . $reason;
363 } elseif (Env::has('mdel')) {
364 $action = 3; /** 3 = DISCARD **/
365 $subject = "Message supprimé";
366 $append = "a été supprimé par $prenom $nom.\n\n"
367 . "Rappel: il ne faut utiliser cette opération "
368 . "que dans le cas de spams ou de virus !\n";
369 }
370
371 if (isset($action) && $this->client->handle_request($liste, $mid, $action, $reason)) {
372 $texte = "le message suivant :\n\n"
373 ." Auteur: {$mail['sender']}\n"
374 ." Sujet : « {$mail['subj']} »\n"
375 ." Date : ".strftime("le %d %b %Y à %H:%M:%S", (int)$mail['stamp'])."\n\n"
376 .$append;
377 require_once 'diogenes/diogenes.hermes.inc.php';
378 $mailer = new HermesMailer();
379 $mailer->addTo("$liste-owner@{$domain}");
380 $mailer->setFrom("$liste-bounces@{$domain}");
381 $mailer->addHeader('Reply-To', "$liste-owner@{$domain}");
382 $mailer->setSubject($subject);
383 $mailer->setTxtBody(wordwrap($texte,72));
384 $mailer->send();
385 Get::kill('mid');
386 }
387
388 if (Get::has('mid') && is_array($mail)) {
389 $msg = file_get_contents('/etc/mailman/fr/refuse.txt');
390 $msg = str_replace("%(adminaddr)s", "$liste-owner@{$domain}", $msg);
391 $msg = str_replace("%(request)s", "<< SUJET DU MAIL >>", $msg);
392 $msg = str_replace("%(reason)s", "<< TON EXPLICATION >>", $msg);
393 $msg = str_replace("%(listname)s", $liste, $msg);
394 $page->assign('msg', $msg);
395
396 $page->changeTpl('listes/moderate_mail.tpl');
397 $page->assign_by_ref('mail', $mail);
398 return;
399 }
400
401 } elseif (Env::has('sid')) {
402
403 if (list($subs,$mails) = $this->client->get_pending_ops($liste)) {
404 foreach($subs as $user) {
405 if ($user['id'] == Env::v('sid')) {
406 $page->changeTpl('listes/moderate_sub.tpl');
407 $page->assign('del_user', $user);
408 return;
409 }
410 }
411 }
412
413 }
414
415 if (list($subs,$mails) = $this->client->get_pending_ops($liste)) {
416 $page->assign_by_ref('subs', $subs);
417 $page->assign_by_ref('mails', $mails);
418 } else {
419 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la modérer");
420 }
421 }
422
423 function handler_admin(&$page, $liste = null)
424 {
425 global $globals;
426
427 if (is_null($liste)) {
428 return PL_NOT_FOUND;
429 }
430
431 $this->prepare_client($page);
432
433 $page->changeTpl('listes/admin.tpl');
434
435 if (Env::has('add_member')) {
436 require_once('user.func.inc.php');
437 $members = get_users_forlife_list(Env::v('add_member'));
438 $arr = $this->client->mass_subscribe($liste, $members);
439 if (is_array($arr)) {
440 foreach($arr as $addr) {
441 $page->trig("{$addr[0]} inscrit.");
442 }
443 }
444 }
445
446 if (Env::has('del_member')) {
447 if (strpos(Env::v('del_member'), '@') === false) {
448 $this->client->mass_unsubscribe(
449 $liste, array(Env::v('del_member').'@'.$globals->mail->domain));
450 } else {
451 $this->client->mass_unsubscribe($liste, array(Env::v('del_member')));
452 }
453 pl_redirect('lists/admin/'.$liste);
454 }
455
456 if (Env::has('add_owner')) {
457 require_once('user.func.inc.php');
458 $owners = get_users_forlife_list(Env::v('add_owner'));
459 if ($owners) {
460 foreach ($owners as $login) {
461 if ($this->client->add_owner($liste, $login)) {
462 $page->trig($alias." ajouté aux modérateurs.");
463 }
464 }
465 }
466 }
467
468 if (Env::has('del_owner')) {
469 if (strpos(Env::v('del_owner'), '@') === false) {
470 $this->client->del_owner($liste, Env::v('del_owner').'@'.$globals->mail->domain);
471 } else {
472 $this->client->del_owner($liste, Env::v('del_owner'));
473 }
474 pl_redirect('lists/admin/'.$liste);
475 }
476
477 if (list($det,$mem,$own) = $this->client->get_members($liste)) {
478 $membres = list_sort_members($mem, $tri_promo);
479 $moderos = list_sort_owners($own, $tri_promo);
480
481 $page->assign_by_ref('details', $det);
482 $page->assign_by_ref('members', $membres);
483 $page->assign_by_ref('owners', $moderos);
484 $page->assign('np_m', count($mem));
485
486 } else {
487 $page->kill("La liste n'existe pas ou tu n'as pas le droit de l'administrer");
488 }
489 }
490
491 function handler_options(&$page, $liste = null)
492 {
493 if (is_null($liste)) {
494 return PL_NOT_FOUND;
495 }
496
497 $this->prepare_client($page);
498
499 $page->changeTpl('listes/options.tpl');
500
501 if (Post::has('submit')) {
502 $values = $_POST;
503 $this->client->set_bogo_level($liste, intval($values['bogo_level']));
504 switch($values['moderate']) {
505 case '0':
506 $values['generic_nonmember_action'] = 0;
507 $values['default_member_moderation'] = 0;
508 break;
509 case '1':
510 $values['generic_nonmember_action'] = 1;
511 $values['default_member_moderation'] = 0;
512 break;
513 case '2':
514 $values['generic_nonmember_action'] = 1;
515 $values['default_member_moderation'] = 1;
516 break;
517 }
518 unset($values['submit'], $values['bogo_level'], $values['moderate']);
519 $values['send_goodbye_msg'] = !empty($values['send_goodbye_msg']);
520 $values['admin_notify_mchanges'] = !empty($values['admin_notify_mchanges']);
521 $values['subscribe_policy'] = empty($values['subscribe_policy']) ? 0 : 2;
522 if (isset($values['subject_prefix'])) {
523 $values['subject_prefix'] = trim($values['subject_prefix']).' ';
524 }
525 $this->client->set_owner_options($liste, $values);
526 } elseif (isvalid_email(Post::v('atn_add'))) {
527 $this->client->add_to_wl($liste, Post::v('atn_add'));
528 } elseif (Get::has('atn_del')) {
529 $this->client->del_from_wl($liste, Get::v('atn_del'));
530 pl_redirect('lists/options/'.$liste);
531 }
532
533 if (list($details,$options) = $this->client->get_owner_options($liste)) {
534 $page->assign_by_ref('details', $details);
535 $page->assign_by_ref('options', $options);
536 $page->assign('bogo_level', $this->client->get_bogo_level($liste));
537 } else {
538 $page->kill("La liste n'existe pas ou tu n'as pas le droit de l'administrer");
539 }
540 }
541
542 function handler_delete(&$page, $liste = null)
543 {
544 if (is_null($liste)) {
545 return PL_NOT_FOUND;
546 }
547
548 $this->prepare_client($page);
549
550 $page->changeTpl('listes/delete.tpl');
551
552 if (Post::v('valid') == 'OUI'
553 && $this->client->delete_list($liste, Post::b('del_archive')))
554 {
555 foreach (array('', '-owner', '-admin', '-bounces') as $app) {
556 XDB::execute("DELETE FROM aliases
557 WHERE type='liste' AND alias='{?}'",
558 $liste.$app);
559 }
560 $page->assign('deleted', true);
561 } elseif (list($details,$options) = $this->client->get_owner_options($liste)) {
562 $page->assign_by_ref('details', $details);
563 $page->assign_by_ref('options', $options);
564 $page->assign('bogo_level', $this->client->get_bogo_level($liste));
565 } else {
566 $page->kill("La liste n'existe pas ou tu n'as pas le droit de l'administrer");
567 }
568 }
569
570 function handler_soptions(&$page, $liste = null)
571 {
572 if (is_null($liste)) {
573 return PL_NOT_FOUND;
574 }
575
576 $this->prepare_client($page);
577
578 $page->changeTpl('listes/soptions.tpl');
579
580 if (Post::has('submit')) {
581 $values = $_POST;
582 unset($values['submit']);
583 $values['advertised'] = empty($values['advertised']) ? false : true;
584 $values['archive'] = empty($values['archive']) ? false : true;
585 $this->client->set_admin_options($liste, $values);
586 }
587
588 if (list($details,$options) = $this->client->get_admin_options($liste)) {
589 $page->assign_by_ref('details', $details);
590 $page->assign_by_ref('options', $options);
591 } else {
592 $page->kill("La liste n'existe pas");
593 }
594 }
595
596 function handler_check(&$page, $liste = null)
597 {
598 if (is_null($liste)) {
599 return PL_NOT_FOUND;
600 }
601
602 $this->prepare_client($page);
603
604 $page->changeTpl('listes/check.tpl');
605
606 if (Post::has('correct')) {
607 $this->client->check_options($liste, true);
608 }
609
610 if (list($details,$options) = $this->client->check_options($liste)) {
611 $page->assign_by_ref('details', $details);
612 $page->assign_by_ref('options', $options);
613 } else {
614 $page->kill("La liste n'existe pas");
615 }
616 }
617
618 function handler_admin_all(&$page) {
619 $page->changeTpl('listes/admin_all.tpl');
620 $page->assign('xorg_title','Polytechnique.org - Administration - Mailing lists');
621
622 $client = new MMList(S::v('uid'), S::v('password'));
623 $listes = $client->get_all_lists();
624 $page->assign_by_ref('listes', $listes);
625 }
626 }
627
628 ?>