Should fix NL look in some webmails
[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 'lists.inc.php';
58
59 $this->client =& lists_xmlrpc(S::v('uid'), S::v('password'));
60 return $globals->mail->domain;
61 }
62
63 function handler_lists(&$page)
64 {
65 $this->prepare_client($page);
66
67 $page->changeTpl('listes/index.tpl');
68 $page->assign('xorg_title','Polytechnique.org - Listes de diffusion');
69
70
71 if (Get::has('del')) {
72 $this->client->unsubscribe(Get::v('del'));
73 pl_redirect('lists');
74 }
75 if (Get::has('add')) {
76 $this->client->subscribe(Get::v('add'));
77 pl_redirect('lists');
78 }
79 if (Post::has('promo_add')) {
80 $promo = Post::i('promo_add');
81 if ($promo >= 1900 and $promo < 2100) {
82 $this->client->subscribe("promo$promo");
83 } else {
84 $page->trig("promo incorrecte, il faut une promo sur 4 chiffres.");
85 }
86 }
87 $listes = $this->client->get_lists();
88 $page->assign_by_ref('listes', $listes);
89 }
90
91 function handler_create(&$page)
92 {
93 $page->changeTpl('listes/create.tpl');
94
95 $owners = preg_split("/[\s]+/", Post::v('owners'), -1, PREG_SPLIT_NO_EMPTY);
96 $members = preg_split("/[\s]+/", Post::v('members'), -1, PREG_SPLIT_NO_EMPTY);
97
98 // click on validate button 'add_owner_sub' or type <enter>
99 if (Post::has('add_owner_sub') && Post::has('add_owner')) {
100 require_once('user.func.inc.php');
101 // if we want to add an owner and then type <enter>, then both
102 // add_owner_sub and add_owner are filled.
103 if (Post::v('add_owner') != "") {
104 if (($forlife = get_user_forlife(Post::v('add_owner'))) !== false) {
105 $owners [] = $forlife;
106 }
107 // if we want to add a member and then type <enter>, then
108 // add_owner_sub is filled, whereas add_owner is empty.
109 } else if (Post::has('add_member')) {
110 if (($forlife = get_user_forlife(Post::v('add_member'))) !== false) {
111 $members[] = $forlife;
112 }
113 }
114 }
115
116 // click on validate button 'add_member_sub'
117 if (Post::has('add_member_sub') && Post::has('add_member')) {
118 require_once('user.func.inc.php');
119 if (($forlife = get_user_forlife(Post::v('add_member'))) !== false) {
120 $members[] = $forlife;
121 }
122 }
123
124 ksort($owners); array_unique($owners);
125 ksort($members); array_unique($members);
126
127 $page->assign('owners', join(' ', $owners));
128 $page->assign('members', join(' ', $members));
129
130 if (!Post::has('submit')) {
131 return;
132 }
133
134 $liste = Post::v('liste');
135
136 if (empty($liste)) {
137 $page->trig('champs «addresse souhaitée» vide');
138 }
139 if (!preg_match("/^[a-zA-Z0-9\-]*$/", $liste)) {
140 $page->trig('le nom de la liste ne doit contenir que des lettres, chiffres et tirets');
141 }
142
143 $res = XDB::query("SELECT COUNT(*) FROM aliases WHERE alias={?}", $liste);
144 $n = $res->fetchOneCell();
145
146 if ($n) {
147 $page->trig('cet alias est déjà pris');
148 }
149
150 if (!Post::v(desc)) {
151 $page->trig('le sujet est vide');
152 }
153
154 if (!count($owners)) {
155 $page->trig('pas de gestionnaire');
156 }
157
158 if (count($members)<4) {
159 $page->trig('pas assez de membres');
160 }
161
162 if (!$page->nb_errs()) {
163 $page->assign('created', true);
164 require_once 'validations.inc.php';
165 $req = new ListeReq(S::v('uid'), $liste,
166 Post::v('desc'), Post::i('advertise'),
167 Post::i('modlevel'), Post::i('inslevel'),
168 $owners, $members);
169 $req->submit();
170 }
171 }
172
173 function handler_members(&$page, $liste = null)
174 {
175 if (is_null($liste)) {
176 return PL_NOT_FOUND;
177 }
178
179 $this->prepare_client($page);
180
181 $page->changeTpl('listes/members.tpl');
182
183 if (Get::has('del')) {
184 $this->client->unsubscribe($liste);
185 pl_redirect('lists/members/'.$liste);
186 }
187
188 if (Get::has('add')) {
189 $this->client->subscribe($liste);
190 pl_redirect('lists/members/'.$liste);
191 }
192
193 $members = $this->client->get_members($liste);
194
195 $tri_promo = !Env::b('alpha');
196
197 if (list($det,$mem,$own) = $members) {
198 $membres = list_sort_members($mem, $tri_promo);
199 $moderos = list_sort_owners($own, $tri_promo);
200
201 $page->assign_by_ref('details', $det);
202 $page->assign_by_ref('members', $membres);
203 $page->assign_by_ref('owners', $moderos);
204 $page->assign('nb_m', count($mem));
205 } else {
206 $page->kill("La liste n'existe pas ou tu n'as pas le droit d'en voir les détails");
207 }
208 }
209
210 function _get_list($offset, $limit)
211 {
212 global $platal;
213 list($total, $members) = $this->client->get_members_limit($platal->argv[1], $offset, $limit);
214
215 $membres = Array();
216 foreach ($members as $member) {
217 list($m) = explode('@',$member[1]);
218 $res = XDB::query("SELECT prenom,if (nom_usage='', nom, nom_usage) AS nom,
219 promo, a.alias AS forlife
220 FROM auth_user_md5 AS u
221 INNER JOIN aliases AS a ON u.user_id = a.id
222 WHERE a.alias = {?}", $m);
223 if ($tmp = $res->fetchOneAssoc()) {
224 $membres[$tmp['nom']] = $tmp;
225 } else {
226 $membres[$member[0]] = array('addr' => $member[0]);
227 }
228 }
229 return array($total, $membres);
230 }
231
232 function handler_trombi(&$page, $liste = null)
233 {
234 if (is_null($liste)) {
235 return PL_NOT_FOUND;
236 }
237
238 $this->prepare_client($page);
239
240 $page->changeTpl('listes/trombi.tpl');
241
242 if (Get::has('del')) {
243 $this->client->unsubscribe($liste);
244 pl_redirect('lists/tromi/'.$liste);
245 }
246 if (Get::has('add')) {
247 $this->client->subscribe($liste);
248 pl_redirect('lists/tromi/'.$liste);
249 }
250
251 $owners = $this->client->get_owners($liste);
252
253 if (is_array($owners)) {
254 require_once 'trombi.inc.php';
255 $moderos = list_sort_owners($owners[1]);
256
257 $page->assign_by_ref('details', $owners[0]);
258 $page->assign_by_ref('owners', $moderos);
259
260 $trombi = new Trombi(array(&$this, '_get_list'));
261 $page->assign('trombi', $trombi);
262 } else {
263 $page->kill("La liste n'existe pas ou tu n'as pas le droit d'en voir les détails");
264 }
265 }
266
267 function handler_archives(&$page, $liste = null)
268 {
269 global $globals;
270
271 if (is_null($liste)) {
272 return PL_NOT_FOUND;
273 }
274
275 $domain = $this->prepare_client($page);
276
277 $page->changeTpl('listes/archives.tpl');
278
279 $page->addCssLink('lists.archives.css');
280 if (list($det) = $this->client->get_members($liste)) {
281 if (substr($liste,0,5) != 'promo' && ($det['ins'] || $det['priv'])
282 && !$det['own'] && ($det['sub'] < 2))
283 {
284 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter");
285 } elseif (Get::has('file')) {
286 $file = Get::v('file');
287 $rep = Get::v('rep');
288 if (strstr('/', $file)!==false || !preg_match(',^\d+/\d+$,', $rep)) {
289 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter");
290 } else {
291 $page->assign('archives', $globals->lists->spool
292 ."/{$domain}{$globals->lists->vhost_sep}$liste/$rep/$file");
293 }
294 } else {
295 $archs = Array();
296 foreach (glob($globals->lists->spool
297 ."/{$domain}{$globals->lists->vhost_sep}$liste/*/*") as $rep)
298 {
299 if (preg_match(",/(\d*)/(\d*)$,", $rep, $matches)) {
300 $archs[intval($matches[1])][intval($matches[2])] = true;
301 }
302 }
303 $page->assign('archs', $archs);
304 $page->assign('range', range(1,12));
305 }
306 } else {
307 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter");
308 }
309 }
310
311 function handler_moderate(&$page, $liste = null)
312 {
313 if (is_null($liste)) {
314 return PL_NOT_FOUND;
315 }
316
317 $domain = $this->prepare_client($page);
318
319 $page->changeTpl('listes/moderate.tpl');
320
321 $page->register_modifier('qpd', 'quoted_printable_decode');
322 $page->register_modifier('hdc', 'list_header_decode');
323
324 if (Env::has('sadd')) { /* 4 = SUBSCRIBE */
325 $this->client->handle_request($liste,Env::v('sadd'),4,'');
326 pl_redirect('lists/moderate/'.$liste);
327 }
328
329 if (Post::has('sdel')) { /* 2 = REJECT */
330 $this->client->handle_request($liste,Post::v('sdel'),2,Post::v('reason'));
331 }
332
333 if (Env::has('mid')) {
334 $mid = Env::v('mid');
335 $mail = $this->client->get_pending_mail($liste, $mid);
336 $reason = '';
337
338 $prenom = S::v('prenom');
339 $nom = S::v('nom');
340
341 if (Env::has('mok')) {
342 $action = 1; /** 2 = ACCEPT **/
343 $subject = "Message accepté";
344 $append .= "a été accepté par $prenom $nom.\n";
345 } elseif (Env::has('mno')) {
346 $action = 2; /** 2 = REJECT **/
347 $subject = "Message refusé";
348 $reason = Post::v('reason');
349 $append = "a été refusé par $prenom $nom avec la raison :\n\n"
350 . $reason;
351 } elseif (Env::has('mdel')) {
352 $action = 3; /** 3 = DISCARD **/
353 $subject = "Message supprimé";
354 $append = "a été supprimé par $prenom $nom.\n\n"
355 . "Rappel: il ne faut utiliser cette opération "
356 . "que dans le cas de spams ou de virus !\n";
357 }
358
359 if (isset($action) && $this->client->handle_request($liste, $mid, $action, $reason)) {
360 $texte = "le message suivant :\n\n"
361 ." Auteur: {$mail['sender']}\n"
362 ." Sujet : « {$mail['subj']} »\n"
363 ." Date : ".strftime("le %d %b %Y à %H:%M:%S", (int)$mail['stamp'])."\n\n"
364 .$append;
365 require_once 'diogenes/diogenes.hermes.inc.php';
366 $mailer = new HermesMailer();
367 $mailer->addTo("$liste-owner@{$domain}");
368 $mailer->setFrom("$liste-bounces@{$domain}");
369 $mailer->addHeader('Reply-To', "$liste-owner@{$domain}");
370 $mailer->setSubject($subject);
371 $mailer->setTxtBody(wordwrap($texte,72));
372 $mailer->send();
373 Get::kill('mid');
374 }
375
376 if (Get::has('mid') && is_array($mail)) {
377 $msg = file_get_contents('/etc/mailman/fr/refuse.txt');
378 $msg = str_replace("%(adminaddr)s", "$liste-owner@{$domain}", $msg);
379 $msg = str_replace("%(request)s", "<< SUJET DU MAIL >>", $msg);
380 $msg = str_replace("%(reason)s", "<< TON EXPLICATION >>", $msg);
381 $msg = str_replace("%(listname)s", $liste, $msg);
382 $page->assign('msg', $msg);
383
384 $page->changeTpl('listes/moderate_mail.tpl');
385 $page->assign_by_ref('mail', $mail);
386 return;
387 }
388
389 } elseif (Env::has('sid')) {
390
391 if (list($subs,$mails) = $this->client->get_pending_ops($liste)) {
392 foreach($subs as $user) {
393 if ($user['id'] == Env::v('sid')) {
394 $page->changeTpl('listes/moderate_sub.tpl');
395 $page->assign('del_user', $user);
396 return;
397 }
398 }
399 }
400
401 }
402
403 if (list($subs,$mails) = $this->client->get_pending_ops($liste)) {
404 $page->assign_by_ref('subs', $subs);
405 $page->assign_by_ref('mails', $mails);
406 } else {
407 $page->kill("La liste n'existe pas ou tu n'as pas le droit de la modérer");
408 }
409 }
410
411 function handler_admin(&$page, $liste = null)
412 {
413 global $globals;
414
415 if (is_null($liste)) {
416 return PL_NOT_FOUND;
417 }
418
419 $this->prepare_client($page);
420
421 $page->changeTpl('listes/admin.tpl');
422
423 if (Env::has('add_member')) {
424
425 require_once('user.func.inc.php');
426 $members = explode(' ', Env::v('add_member'));
427 if ($members) foreach ($members as $i => $alias) {
428 if (($login = get_user_forlife($alias)) !== false) {;
429 $members[$i] = $login;
430 }
431 }
432
433 $arr = $this->client->mass_subscribe($liste, $members);
434 if (is_array($arr)) {
435 foreach($arr as $addr) {
436 $page->trig("{$addr[0]} inscrit.");
437 }
438 }
439 }
440
441 if (Env::has('del_member')) {
442 if (strpos(Env::v('del_member'), '@') === false) {
443 $this->client->mass_unsubscribe(
444 $liste, array(Env::v('del_member').'@'.$globals->mail->domain));
445 } else {
446 $this->client->mass_unsubscribe($liste, array(Env::v('del_member')));
447 }
448 pl_redirect('lists/admin/'.$liste);
449 }
450
451 if (Env::has('add_owner')) {
452 require_once('user.func.inc.php');
453
454 $owners = explode(' ', Env::v('add_owner'));
455
456 if ($owners) foreach ($owners as $alias) {
457 if (($login = get_user_forlife($alias)) === false) {;
458 $login = $alias;
459 }
460
461 if ($this->client->add_owner($liste, $login)) {
462 $page->trig($alias." ajouté aux modérateurs.");
463 }
464 }
465 }
466
467 if (Env::has('del_owner')) {
468 if (strpos(Env::v('del_owner'), '@') === false) {
469 $this->client->del_owner($liste, Env::v('del_owner').'@'.$globals->mail->domain);
470 } else {
471 $this->client->del_owner($liste, Env::v('del_owner'));
472 }
473 pl_redirect('lists/admin/'.$liste);
474 }
475
476 if (list($det,$mem,$own) = $this->client->get_members($liste)) {
477
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 require_once 'lists.inc.php';
622
623 $client =& lists_xmlrpc(S::v('uid'), S::v('password'));
624 $listes = $client->get_all_lists();
625 $page->assign_by_ref('listes',$listes);
626 }
627
628 }
629
630 ?>