0c97ba2f87f22471e2e6f15e0406453b4900694b
[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
22 $days = array("1" => "3", "15" => "2", "30" => "1", "45" => "0");
23
24 foreach($days as $day => $warning)
25 {
26 $query = "SELECT `emailcerts`.`id`,`users`.`fname`,`users`.`lname`,`users`.`email`,`emailcerts`.`memid`,
27 `emailcerts`.`subject`, `emailcerts`.`crt_name`,`emailcerts`.`CN`, `emailcerts`.`serial`,
28 (UNIX_TIMESTAMP(`emailcerts`.`expire`) - UNIX_TIMESTAMP(NOW())) / 86400 as `daysleft`
29 FROM `users`,`emailcerts`
30 WHERE UNIX_TIMESTAMP(`emailcerts`.`expire`) - UNIX_TIMESTAMP(NOW()) > -7 * 86400 and
31 UNIX_TIMESTAMP(`emailcerts`.`expire`) - UNIX_TIMESTAMP(NOW()) < $day * 86400 and
32 `emailcerts`.`renewed`=0 and `emailcerts`.`warning` <= '$warning' and
33 `emailcerts`.`revoked`=0 and `users`.`id`=`emailcerts`.`memid`";
34 $res = mysql_query($query);
35 while($row = mysql_fetch_assoc($res))
36 {
37 if($row['subject'] == "")
38 {
39 $row['crt_name'] = str_replace("../", "www/", $row['crt_name']);
40 $row['crt_name'] = "/home/cacert/".$row['crt_name'];
41 $subject = `openssl x509 -in '$row[crt_name]' -text -noout|grep Subject:`;
42 $bits = explode("/", $subject);
43 foreach($bits as $val)
44 {
45 $sub = explode("=", trim($val));
46 if($sub['0'] == "emailAddress")
47 {
48 $row['subject'] = "/CN=".$row['CN']."/emailAddress=".$sub['1'];
49 break;
50 }
51 }
52 }
53 if($row['subject'] == "")
54 $row['subject'] = "/CN=".$row['CN'];
55 $row['daysleft'] = ceil($row['daysleft']);
56 $body = sprintf(_("Hi %s"), $row['fname']).",\n\n";
57 $body .= _("You are receiving this email as you are the listed contact for:")."\n\n";
58 $body .= $row['subject']."\n\n";
59 $body .= sprintf(_("Your certificate with the serial number %s is ".
60 "set to expire in approximately %s days time. You can ".
61 "renew it by going to the following URL:"),
62 $row['serial'],
63 $row['daysleft'])."\n\n";
64 $body .= "https://www.cacert.org/account.php?id=5\n\n";
65 $body .= _("Best Regards")."\n"._("CAcert Support");
66 sendmail($row['email'], "[CAcert.org] "._("Your Certificate is about to expire"), $body, "support@cacert.org", "", "", "CAcert Support");
67 echo $row['fname']." ".$row['lname']." <".$row['email']."> (memid: ".$row['memid']." Subj: ".$row['subject']." timeleft: ".$row['daysleft'].")\n";
68 $query = "update `emailcerts` set `warning`='".($warning+1)."' where `id`='".$row['id']."'";
69 mysql_query($query);
70 }
71 }
72
73 foreach($days as $day => $warning)
74 {
75 $select_clause =
76 "`domaincerts`.`id`,
77 `users`.`fname`, `users`.`lname`, `users`.`email`,
78 `domains`.`memid`,
79 `domaincerts`.`subject`, `domaincerts`.`crt_name`,
80 `domaincerts`.`CN`,
81 `domaincerts`.`serial`,
82 (UNIX_TIMESTAMP(`domaincerts`.`expire`) -
83 UNIX_TIMESTAMP(NOW())) / 86400 AS `daysleft`";
84 $where_clause =
85 "UNIX_TIMESTAMP(`domaincerts`.`expire`) -
86 UNIX_TIMESTAMP(NOW()) > -7 * 86400
87 AND UNIX_TIMESTAMP(`domaincerts`.`expire`) -
88 UNIX_TIMESTAMP(NOW()) < $day * 86400
89 AND `domaincerts`.`renewed` = 0
90 AND `domaincerts`.`warning` <= '$warning'
91 AND `domaincerts`.`revoked` = 0
92 AND `domains`.`memid` = `users`.`id`";
93 $query =
94 "SELECT $select_clause
95 FROM `users`, `domaincerts`, `domains`
96 WHERE $where_clause
97 AND `domaincerts`.`domid` = `domains`.`id`
98 UNION DISTINCT
99 SELECT $select_clause
100 FROM `users`,
101 `domaincerts` LEFT JOIN `domlink` ON
102 (`domaincerts`.`id` = `domlink`.`certid`),
103 `domains`
104 WHERE $where_clause
105 AND `domlink`.`domid` = `domains`.`id`";
106 $res = mysql_query($query);
107 while($row = mysql_fetch_assoc($res))
108 {
109 if($row['subject'] == "")
110 $row['subject'] = $row['CN'];
111
112 $row['daysleft'] = ceil($row['daysleft']);
113 $body = sprintf(_("Hi %s"), $row['fname']).",\n\n";
114 $body .= _("You are receiving this email as you are the listed contact for:")."\n\n";
115 $body .= $row['subject']."\n\n";
116 $body .= sprintf(_("Your certificate with the serial number %s is ".
117 "set to expire in approximately %s days time. You can ".
118 "renew it by going to the following URL:"),
119 $row['serial'],
120 $row['daysleft'])."\n\n";
121 $body .= "https://www.cacert.org/account.php?id=12\n\n";
122 $body .= _("Best Regards")."\n"._("CAcert Support");
123 sendmail($row['email'], "[CAcert.org] "._("Your Certificate is about to expire"), $body, "support@cacert.org", "", "", "CAcert Support");
124 echo $row['fname']." ".$row['lname']." <".$row['email']."> (memid: ".$row['memid']." Subj: ".$row['CN']." timeleft: ".$row['daysleft'].")\n";
125 $query = "update `domaincerts` set `warning`='".($warning+1)."' where `id`='".$row['id']."'";
126 mysql_query($query);
127 }
128 }
129 ?>