99958cca074f108cd2246598030cbd241c9062d2
[platal.git] / bin / cron / emails.check.php
1 #!/usr/bin/php5 -q
2 <?php
3 /***************************************************************************
4 * Copyright (C) 2003-2007 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 /* Number of consecutive month of bounce before deactivating a redirection
24 */
25 $panne_level = 3;
26
27 require('./connect.db.inc.php');
28
29 /*
30 * Check duplicated addresses
31 */
32 $sql = "SELECT a1.alias, a2.alias, e1.email
33 FROM emails AS e1
34 INNER JOIN emails AS e2 ON (e1.email = e2.email AND e1.uid != e2.uid
35 AND (e1.uid < e2.uid OR NOT FIND_IN_SET(e2.flags, 'active')))
36 LEFT JOIN emails_watch AS w ON (e1.email = w.email)
37 INNER JOIN aliases AS a1 ON (a1.id = e1.uid AND a1.type = 'a_vie')
38 INNER JOIN aliases AS a2 ON (a2.id = e2.uid AND a2.type = 'a_vie')
39 INNER JOIN auth_user_md5 AS u1 ON (a1.id = u1.user_id)
40 INNER JOIN auth_user_md5 AS u2 ON (a2.id = u2.user_id)
41 WHERE FIND_IN_SET(e1.flags, 'active') AND u1.nom != u2.nom_usage AND u2.nom != u1.nom_usage AND w.email IS NULL
42 ORDER BY a1.alias";
43
44 $it = Xdb::iterRow($sql);
45
46 $insert = array();
47 $conflits = array();
48 while (list($alias1, $alias2, $mail) = $it->next()) {
49 $insert[] = "('$mail', 'pending', CURDATE(), NOW())";
50 $conflits[] = "* $mail sur $alias1 et $alias2";
51 }
52
53 if (count($conflits) > 0) {
54 echo "Nouvelles adresses en doublon detectees :\n" . join("\n", $conflits)
55 . "\n\nVous pouvez entrer les informations collectees a ce sujet sur la page :\n"
56 . "http://www.polytechnique.org/admin/emails/duplicated";
57
58 echo "\n\n";
59 $sql = "INSERT IGNORE INTO emails_watch (email, state, detection, last)
60 VALUES " . join(", ", $insert);
61 XDB::execute($sql);
62 if (XDB::errno() != 0) {
63 echo 'Error : ' . XDB::error() . "\n$sql";
64 }
65 }
66
67 /*
68 * Check dead emails
69 */
70 if ($panne_level > 0) {
71 $sql = "SELECT e.email, a.alias AS forlife
72 FROM emails AS e
73 INNER JOIN aliases AS a ON a.id = e.uid AND a.type = 'a_vie'
74 WHERE e.panne_level = $panne_level AND e.flags = 'active'
75 ORDER BY a.alias";
76 $res = Xdb::query($sql);
77
78 if ($res->numRows()) {
79 $result = $res->fetchAllAssoc();
80 echo "Nouvelles adresses en panne detectees :\n";
81 foreach ($result as $assoc) {
82 echo '* ' . $assoc['email'] . ' pour ' . $assoc['forlife'] . "\n";
83 }
84 echo "\n\n";
85
86 Xdb::execute("UPDATE emails
87 SET flags = 'panne'
88 WHERE panne_level = 3 AND flags = 'active'");
89 }
90
91 Xdb::execute("UPDATE emails
92 SET panne_level = $panne_level
93 WHERE panne_level > $panne_level");
94 }
95
96 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
97 ?>