Mailman don't understand UTF8 (Closes #761)
[platal.git] / bin / cron / cron_ml_moderate.php
1 #!/usr/bin/php5 -q
2 <?php
3 /***************************************************************************
4 * Copyright (C) 2003-2008 Polytechnique.org *
5 * http://opensource.polytechnique.org/ *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., *
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
21 ***************************************************************************/
22
23 require('./connect.db.inc.php');
24 ini_set('max_execution_time', '75');
25 ini_set('memory_limit', '128M');
26 $sent_mails = 0;
27 $handler = time();
28
29 while ($sent_mails < $globals->lists->max_mail_per_min
30 && time() - $handler < 60) {
31 // take a lock on a mail
32 XDB::execute("UPDATE ml_moderate
33 SET handler = {?}
34 WHERE handler IS NULL
35 ORDER BY ts
36 LIMIT 1", $handler);
37 if (XDB::affectedRows() == 0) {
38 break;
39 }
40 $query = XDB::query("SELECT nom, prenom, user_id, password,
41 ml, domain, mid, action, message
42 FROM auth_user_md5 AS u
43 INNER JOIN ml_moderate AS ml ON (u.user_id = ml.uid)
44 WHERE ml.handler = {?}", $handler);
45 list($nom, $prenom, $uid, $password, $list, $domain, $mid, $action, $reason) = $query->fetchOneRow();
46
47 // build the client
48 $client = new MMList($uid, $password, $domain);
49
50 // send the mail
51 $mail = $client->get_pending_mail($list, $mid);
52 list($det,$mem,$own) = $client->get_members($list);
53 $count = 0;
54 switch ($action) {
55 case 'accept':
56 $action = 1; /** 1 = ACCEPT **/
57 $subject = "Message accepté";
58 $append = "a été accepté par $prenom $nom.\n";
59 $type = 'nonspam';
60 $count += count($mem) + count($own);
61 break;
62 case 'refuse':
63 $action = 2; /** 2 = REJECT **/
64 $subject = "Message refusé";
65 $append = "a été refusé par $prenom $nom avec la raison :\n\n" . $reason;
66 $type = 'nonspam';
67 $count += count($own) + 1;
68 break;
69 case 'delete':
70 $action = 3; /** 3 = DISCARD **/
71 $subject = "Message supprimé";
72 $append = "a été supprimé par $prenom $nom.\n\n"
73 . "Rappel: il ne faut utiliser cette opération "
74 . "que dans le cas de spams ou de virus !\n";
75 $type = 'spam';
76 $count += count($own);
77 break;
78 }
79
80 if ($client->handle_request($list, $mid, $action, utf8_decode($reason))) {
81 $sent_mails += $count;
82 $texte = "le message suivant :\n\n"
83 . " Auteur: {$mail['sender']}\n"
84 . " Sujet : « {$mail['subj']} »\n"
85 . " Date : ".strftime("le %d %b %Y à %H:%M:%S", (int)$mail['stamp'])."\n\n"
86 . $append;
87 $mailer = new PlMailer();
88 $mailer->addTo("$list-owner@{$domain}");
89 $mailer->setFrom("$list-bounces@{$domain}");
90 $mailer->addHeader('Reply-To', "$list-owner@{$domain}");
91 $mailer->setSubject($subject);
92 $mailer->setTxtBody($texte);
93 $mailer->send();
94 }
95
96 // if the mail was classified as Unsure, feed bogo
97 $raw_mail = html_entity_decode($client->get_pending_mail($list, $mid, 1));
98 // search for the X-Spam-Flag header
99 $end_of_headers = strpos($raw_mail, "\r\n\r\n");
100 if ($end_of_headers === false) { // sometimes headers are separated by \n
101 $end_of_headers = strpos($raw_mail, "\n\n");
102 }
103 $x_spam_flag = '';
104 if (preg_match('/^X-Spam-Flag: ([a-zA-Z]+), tests=bogofilter/m', substr($raw_mail, 0, $end_of_headers + 1), $matches)) {
105 $x_spam_flag = $matches[1];
106 }
107 if ($x_spam_flag == 'Unsure') {
108 $mailer = new PlMailer();
109 $mailer->addTo($type . '@' . $globals->mail->domain);
110 $mailer->setFrom('"' . $prenom . ' ' . $nom . '" <web@' . $globals->mail->domain . '>');
111 $mailer->setTxtBody($type . ' soumis par ' . $prenom . ' ' . $nom . ' via la modération de la liste ' . $list . '@' . $domain);
112 $mailer->addAttachment($raw_mail, 'message/rfc822', $type . '.mail', false);
113 $mailer->send();
114 }
115
116 // release the lock
117 XDB::execute("DELETE FROM ml_moderate WHERE handler = {?}",
118 $handler);
119 sleep(60 * $count / $globals->lists->max_mail_per_min);
120 }
121
122 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
123 ?>