162b07eb |
1 | #!/usr/bin/php5 -q |
0337d704 |
2 | <?php |
3 | /*************************************************************************** |
5ddeb07c |
4 | * Copyright (C) 2003-2007 Polytechnique.org * |
0337d704 |
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 | |
dc557110 |
23 | ini_set('include_path', '.:../include:/usr/share/php'); |
24 | |
25 | require_once('connect.db.inc.php'); |
26 | require_once('xorg.inc.php'); |
27 | require_once('emails.inc.php'); |
162b07eb |
28 | require_once('../classes/plmailer.php'); |
0337d704 |
29 | |
a3eb78bc |
30 | $opts = getopt('i:o:'); |
0337d704 |
31 | if (($opts['i'] && $opts['i'] == '-') || empty($opts['i'])) { |
32 | $file = 'php://stdin'; |
33 | } else { |
34 | $file = $opts['i']; |
35 | } |
a3eb78bc |
36 | if (($opts['o'] && $opts['o'] == '-') || empty($opts['o'])) { |
37 | $output = 'php://stdout'; |
38 | } else { |
39 | $output = $opts['o']; |
40 | } |
0337d704 |
41 | |
575dd9be |
42 | $emails = explode("\n", file_get_contents($file)); |
a3eb78bc |
43 | $list = array(); |
dc557110 |
44 | |
0337d704 |
45 | foreach ($emails as $_email) { |
46 | $email = valide_email($_email); |
47 | if (empty($email) || $email=='@') { |
48 | continue; |
49 | } |
50 | |
08cce2ff |
51 | $sel = XDB::query( |
0337d704 |
52 | "SELECT e1.uid, e1.panne != 0 AS panne, count(e2.uid) AS nb_mails, u.nom, u.prenom, u.promo, a.alias |
53 | FROM emails AS e1 |
54 | LEFT JOIN emails AS e2 ON (e1.uid = e2.uid AND FIND_IN_SET('active', e2.flags) AND e1.email != e2.email) |
55 | INNER JOIN auth_user_md5 AS u ON (e1.uid = u.user_id) |
56 | INNER JOIN aliases AS a ON (u.user_id = a.id AND FIND_IN_SET('bestalias',a.flags)) |
57 | WHERE e1.email = {?} |
58 | GROUP BY e1.uid", $email); |
59 | if ($x = $sel->fetchOneAssoc()) { |
60 | if (!$x['panne']) { |
dc557110 |
61 | XDB::execute("UPDATE emails |
162b07eb |
62 | SET panne=NOW(), last=NOW(), |
dc557110 |
63 | panne_level = 1 |
64 | WHERE email = {?}", |
65 | $email); |
66 | } else { |
67 | XDB::execute("UPDATE emails |
68 | SET last = CURDATE(), |
69 | panne_level = panne_level + 1 |
70 | WHERE email = {?} |
d846e628 |
71 | AND DATE_ADD(last, INTERVAL 7 DAY) < CURDATE()", $email); |
0337d704 |
72 | } |
73 | |
74 | if (empty($x['nb_mails'])) { |
75 | echo "$email : seule adresse active de {$x['prenom']} {$x['nom']}\n"; |
76 | } else { |
162b07eb |
77 | $mail = new PlMailer('emails/broken.mail.tpl'); |
1d55fe45 |
78 | $mail->addTo("\"{$x['prenom']} {$x['nom']}\" <{$x['alias']}@" . $globals->mail->domain . '>'); |
162b07eb |
79 | $mail->assign('x', $x); |
80 | $mail->assign('email', $email); |
0337d704 |
81 | $mail->send(); |
a7de4ef7 |
82 | echo "$email : mail envoyé\n"; |
0337d704 |
83 | } |
a3eb78bc |
84 | |
85 | if (!isset($list[$x['alias']])) { |
86 | $list[$x['alias']] = array($email); |
87 | } else { |
88 | $list[$x['alias']][] = $email; |
89 | } |
0337d704 |
90 | } else { |
91 | echo "$email : cette addresse n'est pas dans la base\n"; |
a3eb78bc |
92 | } |
0337d704 |
93 | } |
94 | |
dc557110 |
95 | XDB::execute("UPDATE emails |
96 | SET panne_level = panne_level - 1 |
97 | WHERE flags = 'active' AND panne_level > 1 |
98 | AND last != CURDATE()"); |
45585d5c |
99 | XDB::execute("UPDATE emails |
100 | SET panne_level = 0 |
101 | WHERE flags = 'active' AND panne_level = 1 |
102 | AND DATE_ADD(last, INTERVAL 1 YEAR) < CURDATE()"); |
dc557110 |
103 | |
a3eb78bc |
104 | $csv = "nom;prenom;promo;alias;bounce;nbmails\n"; |
105 | foreach ($list as $alias=>$mails) { |
106 | $sel = Xdb::query( |
107 | "SELECT u.user_id, count(e.email) AS nb_mails, u.nom, u.prenom, u.promo |
108 | FROM aliases AS a |
109 | INNER JOIN auth_user_md5 AS u ON a.id = u.user_id |
110 | LEFT JOIN emails AS e ON (e.uid = u.user_id AND FIND_IN_SET('active', e.flags) AND e.panne = 0) |
111 | WHERE a.alias = {?} |
112 | GROUP BY u.user_id", $alias); |
113 | if ($x = $sel->fetchOneAssoc()) { |
114 | $csv .= $x['nom'].';'.$x['prenom'].';' .$x['promo'].';'.$alias.';' . join(',', $mails) . ';'.$x['nb_mails']."\n"; |
115 | } |
116 | } |
117 | |
118 | $fo = fopen($output, 'w+'); |
119 | fwrite($fo, $csv); |
120 | fclose($fo); |
121 | |
a7de4ef7 |
122 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 |
123 | ?> |