ef570fd500d41a425ae0dce542fcd55fb560d625
[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`,
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 is set to expire in approximately %s days time, you can renew this by going to the following URL:"), $row['daysleft'])."\n\n";
60 $body .= "https://www.cacert.org/account.php?id=5\n\n";
61 $body .= _("Best Regards")."\n"._("CAcert Support");
62 sendmail($row['email'], "[CAcert.org] "._("Your Certificate is about to expire"), $body, "support@cacert.org", "", "", "CAcert Support");
63 echo $row['fname']." ".$row['lname']." <".$row['email']."> (memid: ".$row['memid']." Subj: ".$row['subject']." timeleft: ".$row['daysleft'].")\n";
64 $query = "update `emailcerts` set `warning`='".($warning+1)."' where `id`='".$row['id']."'";
65 mysql_query($query);
66 }
67 }
68
69 foreach($days as $day => $warning)
70 {
71 $query =
72 "SELECT DISTINCT `domaincerts`.`id`,
73 `users`.`fname`, `users`.`lname`, `users`.`email`,
74 `domains`.`memid`,
75 `domaincerts`.`subject`, `domaincerts`.`crt_name`,
76 `domaincerts`.`CN`,
77 (UNIX_TIMESTAMP(`domaincerts`.`expire`) -
78 UNIX_TIMESTAMP(NOW())) / 86400 AS `daysleft`
79
80 FROM `users`, `domaincerts`, `domlink`, `domains`
81 WHERE UNIX_TIMESTAMP(`domaincerts`.`expire`) -
82 UNIX_TIMESTAMP(NOW()) > -7 * 86400
83 AND UNIX_TIMESTAMP(`domaincerts`.`expire`) -
84 UNIX_TIMESTAMP(NOW()) < $day * 86400
85 AND `domaincerts`.`renewed` = 0
86 AND `domaincerts`.`warning` <= '$warning'
87 AND `domaincerts`.`revoked` = 0
88 AND (
89 `domaincerts`.`domid` = `domains`.`id`
90 OR (
91 `domaincerts`.`id` = `domlink`.`certid`
92 AND `domlink`.`domid` = `domains`.`id`
93 )
94 )
95 AND `domains`.`memid` = `users`.`id`";
96 $res = mysql_query($query);
97 while($row = mysql_fetch_assoc($res))
98 {
99 if($row['subject'] == "")
100 $row['subject'] = $row['CN'];
101
102 $row['daysleft'] = ceil($row['daysleft']);
103 $body = sprintf(_("Hi %s"), $row['fname']).",\n\n";
104 $body .= _("You are receiving this email as you are the listed contact for:")."\n\n";
105 $body .= $row['subject']."\n\n";
106 $body .= sprintf(_("Your certificate is set to expire in approximately %s days time, you can renew this by going to the following URL:"), $row['daysleft'])."\n\n";
107 $body .= "https://www.cacert.org/account.php?id=12\n\n";
108 $body .= _("Best Regards")."\n"._("CAcert Support");
109 sendmail($row['email'], "[CAcert.org] "._("Your Certificate is about to expire"), $body, "support@cacert.org", "", "", "CAcert Support");
110 echo $row['fname']." ".$row['lname']." <".$row['email']."> (memid: ".$row['memid']." Subj: ".$row['CN']." timeleft: ".$row['daysleft'].")\n";
111 $query = "update `domaincerts` set `warning`='".($warning+1)."' where `id`='".$row['id']."'";
112 mysql_query($query);
113 }
114 }
115 ?>