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