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