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