bbf79ace8a33db5e1e8d1f896bee692bd3032e3a
[cacert-devel.git] / scripts / cron / warning.php
1 #!/usr/bin/php -q
2 <? /*
3 LibreSSL - CAcert web application
4 Copyright (C) 2004-2008 CAcert Inc.
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; version 2 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 require_once(dirname(__FILE__).'/../../includes/mysql.php');
21 require_once(dirname(__FILE__).'/../../includes/lib/l10n.php');
22
23 $days = array("1" => "3", "15" => "2", "30" => "1", "45" => "0");
24
25 foreach($days as $day => $warning)
26 {
27 $query = "SELECT `emailcerts`.`id`,`users`.`fname`,`users`.`lname`,`users`.`email`,`emailcerts`.`memid`,
28 `emailcerts`.`subject`, `emailcerts`.`crt_name`,`emailcerts`.`CN`, `emailcerts`.`serial`,
29 (UNIX_TIMESTAMP(`emailcerts`.`expire`) - UNIX_TIMESTAMP(NOW())) / 86400 as `daysleft`
30 FROM `users`,`emailcerts`
31 WHERE UNIX_TIMESTAMP(`emailcerts`.`expire`) - UNIX_TIMESTAMP(NOW()) > -7 * 86400 and
32 UNIX_TIMESTAMP(`emailcerts`.`expire`) - UNIX_TIMESTAMP(NOW()) < $day * 86400 and
33 `emailcerts`.`renewed`=0 and `emailcerts`.`warning` <= '$warning' and
34 `emailcerts`.`revoked`=0 and `users`.`id`=`emailcerts`.`memid`";
35 $res = mysql_query($query);
36 while($row = mysql_fetch_assoc($res))
37 {
38 L10n::set__recipient_language(intval($row['id']));
39 if($row['subject'] == "")
40 {
41 $row['crt_name'] = str_replace("../", "www/", $row['crt_name']);
42 $row['crt_name'] = "/home/cacert/".$row['crt_name'];
43 $subject = `openssl x509 -in '$row[crt_name]' -text -noout|grep Subject:`;
44 $bits = explode("/", $subject);
45 foreach($bits as $val)
46 {
47 $sub = explode("=", trim($val));
48 if($sub['0'] == "emailAddress")
49 {
50 $row['subject'] = "/CN=".$row['CN']."/emailAddress=".$sub['1'];
51 break;
52 }
53 }
54 }
55 if($row['subject'] == "")
56 $row['subject'] = "/CN=".$row['CN'];
57 $row['daysleft'] = ceil($row['daysleft']);
58 $body = sprintf(_("Hi %s"), $row['fname']).",\n\n";
59 $body .= _("You are receiving this email as you are the listed contact for:")."\n\n";
60 $body .= $row['subject']."\n\n";
61 $body .= sprintf(_("Your certificate with the serial number %s is ".
62 "set to expire in approximately %s days time. You can ".
63 "renew it by going to the following URL:"),
64 $row['serial'],
65 $row['daysleft'])."\n\n";
66 $body .= "https://www.cacert.org/account.php?id=5\n\n";
67 $body .= _("Best Regards")."\n"._("CAcert Support");
68 sendmail($row['email'], "[CAcert.org] "._("Your Certificate is about to expire"), $body, "support@cacert.org", "", "", "CAcert Support");
69 echo $row['fname']." ".$row['lname']." <".$row['email']."> (memid: ".$row['memid']." Subj: ".$row['subject']." timeleft: ".$row['daysleft'].")\n";
70 $query = "update `emailcerts` set `warning`='".($warning+1)."' where `id`='".$row['id']."'";
71 mysql_query($query);
72 }
73 }
74
75 foreach($days as $day => $warning)
76 {
77 $query =
78 "SELECT DISTINCT `domaincerts`.`id`,
79 `users`.`fname`, `users`.`lname`, `users`.`email`,
80 `domains`.`memid`,
81 `domaincerts`.`subject`, `domaincerts`.`crt_name`,
82 `domaincerts`.`CN`,
83 `domaincerts`.`serial`,
84 (UNIX_TIMESTAMP(`domaincerts`.`expire`) -
85 UNIX_TIMESTAMP(NOW())) / 86400 AS `daysleft`
86
87 FROM `users`, `domaincerts`, `domlink`, `domains`
88 WHERE UNIX_TIMESTAMP(`domaincerts`.`expire`) -
89 UNIX_TIMESTAMP(NOW()) > -7 * 86400
90 AND UNIX_TIMESTAMP(`domaincerts`.`expire`) -
91 UNIX_TIMESTAMP(NOW()) < $day * 86400
92 AND `domaincerts`.`renewed` = 0
93 AND `domaincerts`.`warning` <= '$warning'
94 AND `domaincerts`.`revoked` = 0
95 AND (
96 `domaincerts`.`domid` = `domains`.`id`
97 OR (
98 `domaincerts`.`id` = `domlink`.`certid`
99 AND `domlink`.`domid` = `domains`.`id`
100 )
101 )
102 AND `domains`.`memid` = `users`.`id`";
103 $res = mysql_query($query);
104 while($row = mysql_fetch_assoc($res))
105 {
106 L10n::set__recipient_language(intval($row['memid']));
107 if($row['subject'] == "")
108 $row['subject'] = $row['CN'];
109
110 $row['daysleft'] = ceil($row['daysleft']);
111 $body = sprintf(_("Hi %s"), $row['fname']).",\n\n";
112 $body .= _("You are receiving this email as you are the listed contact for:")."\n\n";
113 $body .= $row['subject']."\n\n";
114 $body .= sprintf(_("Your certificate with the serial number %s is ".
115 "set to expire in approximately %s days time. You can ".
116 "renew it by going to the following URL:"),
117 $row['serial'],
118 $row['daysleft'])."\n\n";
119 $body .= "https://www.cacert.org/account.php?id=12\n\n";
120 $body .= _("Best Regards")."\n"._("CAcert Support");
121 sendmail($row['email'], "[CAcert.org] "._("Your Certificate is about to expire"), $body, "support@cacert.org", "", "", "CAcert Support");
122 echo $row['fname']." ".$row['lname']." <".$row['email']."> (memid: ".$row['memid']." Subj: ".$row['CN']." timeleft: ".$row['daysleft'].")\n";
123 $query = "update `domaincerts` set `warning`='".($warning+1)."' where `id`='".$row['id']."'";
124 mysql_query($query);
125 }
126 }
127 ?>