Merge branch 'bug-978' into release
[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 = "SELECT `domaincerts`.`id`, `users`.`fname`, `users`.`lname`, `users`.`email`,
72 `domains`.`memid`, `domaincerts`.`subject`, `domaincerts`.`crt_name`,
73 `domaincerts`.`CN`,
74 (UNIX_TIMESTAMP(`domaincerts`.`expire`) - UNIX_TIMESTAMP(NOW())) / 86400 AS `daysleft`
75 FROM `users`, `domaincerts`, `domlink`, `domains`
76 WHERE UNIX_TIMESTAMP(`domaincerts`.`expire`) - UNIX_TIMESTAMP(NOW()) > -7 * 86400 AND
77 UNIX_TIMESTAMP(`domaincerts`.`expire`) - UNIX_TIMESTAMP(NOW()) < $day * 86400 AND
78 `domaincerts`.`renewed`=0 AND `domaincerts`.`warning` <= '$warning' AND
79 `domaincerts`.`revoked`=0 AND `users`.`id` = `domains`.`memid` AND
80 `domlink`.`certid` = `domaincerts`.`id` AND `domains`.`id` = `domlink`.`domid`";
81 $res = mysql_query($query);
82 while($row = mysql_fetch_assoc($res))
83 {
84 if($row['subject'] == "")
85 $row['subject'] = $row['CN'];
86
87 $row['daysleft'] = ceil($row['daysleft']);
88 $body = sprintf(_("Hi %s"), $row['fname']).",\n\n";
89 $body .= _("You are receiving this email as you are the listed contact for:")."\n\n";
90 $body .= $row['subject']."\n\n";
91 $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";
92 $body .= "https://www.cacert.org/account.php?id=12\n\n";
93 $body .= _("Best Regards")."\n"._("CAcert Support");
94 sendmail($row['email'], "[CAcert.org] "._("Your Certificate is about to expire"), $body, "support@cacert.org", "", "", "CAcert Support");
95 echo $row['fname']." ".$row['lname']." <".$row['email']."> (memid: ".$row['memid']." Subj: ".$row['CN']." timeleft: ".$row['daysleft'].")\n";
96 $query = "update `domaincerts` set `warning`='".($warning+1)."' where `id`='".$row['id']."'";
97 mysql_query($query);
98 }
99 }
100 ?>