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