2 LibreSSL - CAcert web application
3 Copyright (C) 2004-2008 CAcert Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 include_once("/www/includes/general.php");
23 return(mysql_real_escape_string(strip_tags(trim($_REQUEST[$key]))));
26 function checkhostname($ref)
29 if($ref[count($ref)-1] == "." ||
$ref[count($ref)-1] == ":")
30 $ref = substr($ref, 0, -1);
33 $query = "select * from `stampcache` where `hostname`='$ref'";
34 $res = mysql_query($query);
35 if(mysql_num_rows($res) > 0)
37 $row = mysql_fetch_assoc($res);
38 if($row['cacheexpire'] >= date("U"))
39 return(array($row['valid'], $row));
41 if($row['certid'] > 0)
44 $query = "select * from `domaincerts` where `id`='$row[certid]' and `expire`>NOW() and `revoked`=0";
46 $query = "select * from `orgdomaincerts` where `id`='$row[certid]' and `expire`>NOW() and `revoked`=0";
47 if($_REQUEST['debug'] == 1)
49 $res = mysql_query($query);
50 if(mysql_num_rows($res) > 0)
52 $query = "update `stampcache` set `cacheexpire`='".(date("U")+
600)."' where `id`='$row[id]'";
53 if($_REQUEST['debug'] == 1)
56 return(array($row['valid'], $row));
59 $stampid = $row['id'];
63 $query = "select *,`domaincerts`.`id` as `certid`,`domaincerts`.`created` as `issued` from `domlink`,`domains`,`domaincerts`
64 where `domlink`.`domid`=`domains`.`id` and `domlink`.`certid`=`domaincerts`.`id` and
65 `domaincerts`.`revoked`=0 and `domaincerts`.`expire` > NOW() and
66 (`domaincerts`.`subject` like '%=DNS:$ref/%' OR `domaincerts`.`subject` like '%=$ref/%' OR
67 `domaincerts`.`subject` like '%=DNS:$ref' OR `domaincerts`.`subject` like '%=$ref')
68 group by `domaincerts`.`id` order by `domaincerts`.`id`";
69 if($_REQUEST['debug'] == 1)
71 $res = mysql_query($query);
72 if(mysql_num_rows($res) <= 0)
74 $bits = explode(".", $ref);
75 for($i = 1; $i < count($bits); $i++
)
81 $query = "select *,`domaincerts`.`id` as `certid`,`domaincerts`.`created` as `issued` from `domlink`,`domains`,`domaincerts`
82 where `domlink`.`domid`=`domains`.`id` and `domlink`.`certid`=`domaincerts`.`id` and
83 `domaincerts`.`revoked`=0 and `domaincerts`.`expire` > NOW() and
84 (`domaincerts`.`subject` like '%=DNS:$ref/%' or `domaincerts`.`subject` like '%=DNS:*.$ref2/%' OR
85 `domaincerts`.`subject` like '%=DNS:$ref' or `domaincerts`.`subject` like '%=DNS:*.$ref2' OR
86 `domaincerts`.`subject` like '%=$ref/%' or `domaincerts`.`subject` like '%=*.$ref2/%' OR
87 `domaincerts`.`subject` like '%=$ref' or `domaincerts`.`subject` like '%=*.$ref2')
88 group by `domaincerts`.`id` order by `domaincerts`.`id`";
89 if($_REQUEST['debug'] == 1)
91 $res = mysql_query($query);
92 if(mysql_num_rows($res) <= 0)
94 $query = "select *,`orgdomaincerts`.`id` as `certid`,`orgdomaincerts`.`created` as `issued` from `orgdomaincerts`,`orgdomlink`,`orgdomains` where
95 (`orgdomaincerts`.`subject` like '%=DNS:$ref/%' or `orgdomaincerts`.`subject` like '%=DNS:*.$ref2/%' OR
96 `orgdomaincerts`.`subject` like '%=DNS:$ref' or `orgdomaincerts`.`subject` like '%=DNS:*.$ref2' OR
97 `orgdomaincerts`.`subject` like '%=$ref/%' or `orgdomaincerts`.`subject` like '%=*.$ref2/%' OR
98 `orgdomaincerts`.`subject` like '%=$ref' or `orgdomaincerts`.`subject` like '%=*.$ref2') AND
99 `orgdomaincerts`.`id`=`orgdomlink`.`orgcertid` and `orgdomlink`.`orgdomid`=`orgdomains`.`id` and
100 `orgdomaincerts`.`revoked`=0 and `orgdomaincerts`.`expire` > NOW()
101 group by `orgdomaincerts`.`id` order by `orgdomaincerts`.`id`";
102 if($_REQUEST['debug'] == 1)
103 echo $query."<br>\n";
104 $res = mysql_query($query);
105 if(mysql_num_rows($res) <= 0)
116 $cert = mysql_fetch_assoc($res);
119 $query = "SELECT *, sum(`points`) AS `total` FROM `users`, `notary` WHERE `users`.`id` = '$cert[memid]' AND
120 `notary`.`to` = `users`.`id` and `notary`.`when` <= '$cert[issued]' GROUP BY `notary`.`to`";
121 $user = mysql_fetch_assoc(mysql_query($query));
123 $query = "select * from `orginfo` where `id`='$cert[orgid]'";
124 $orgi = mysql_fetch_assoc(mysql_query($query));
129 $query = "insert into `stampcache` set `certid`='$cert[certid]',`cacheexpire`='".(date("U")+
600)."',`issued`='$cert[issued]',
130 `expire`='$cert[expire]',`subject`='$cert[subject]',`hostname`='$ref',`org`='$org',`points`='$user[total]',
131 `O`='$orgi[O]',`L`='$orgi[L]',`ST`='$orgi[ST]',`C`='$orgi[C]',`valid`='$invalid'";
133 $query = "update `stampcache` set `certid`='$cert[certid]',`cacheexpire`='".(date("U")+
600)."',`issued`='$cert[issued]',
134 `expire`='$cert[expire]',`subject`='$cert[subject]',`hostname`='$ref',`org`='$org',`points`='$user[total]',
135 `O`='$orgi[O]',`L`='$orgi[L]',`ST`='$orgi[ST]',`C`='$orgi[C]',`valid`='$invalid' where `id`='$stampid'";
138 } else if($stampid > 0) {
139 mysql_query("update `stampcache` set `cacheexpire`='".(date("U")+
600)."' where `id`='$stampid'");
141 $query = "insert into `stampcache` set `cacheexpire`='".(date("U")+
600)."',`hostname`='$ref',`valid`='$invalid'";
145 $arr = array("issued" => $cert['issued'], "expire" => $cert['expire'], "subject" => $cert['subject'], "hostname" => $ref,
146 "org" => $org, "points" => $user['total'], "O" => $orgi['O'], "L" => $orgi['L'], "ST" => $orgi['ST'],
149 return(array($invalid, $arr));