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