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