-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_functions.php
116 lines (89 loc) · 3.44 KB
/
db_functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/*
nvdCache
Copyright (c) 2007 The Hursk Group, LLC. All rights reserved.
www.hursk.com
hurskgroup@hursk.com
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
*/
function dbf_connectDB($config_database) {
$db_link = mysqli_connect($config_database[host], $config_database[user], $config_database[password], $config_database[db_name], $config_database[port], $config_database[socket]);
if(!$db_link) {
$xml = c_initiate_xml($config_nvdcache);
$xml_error = $xml->addchild('error');
$xml_error->addchild('code', '500');
$xml_error->addchild('description', 'DB Error trying to connect.');
c_announce($xml);
}
//mysqli_autocommit($db_link, FALSE); // turning off auto commit to be safer.
return $db_link;
}
function dbf_cache_stats($db_link) {
$query = "SELECT * FROM statistics";
$result = mysqli_query($db_link, $query);
if(!$result) { // no results
return null;
}
$cache_stats = mysqli_fetch_assoc($result);
$cache_stats[seconds_since_last_update] = time() - $cache_stats[last_db_update_epoch];
$cache_stats[minutes_since_last_update] = $cache_stats[seconds_since_last_update]/60;
$cache_stats[hours_since_last_update] = $cache_stats[minutes_since_last_update]/60;
return $cache_stats;
}
function dbf_put_entry_in_db($entry, $db_link) {
// make sql safe the text
$entry_mysql_safe = mysqli_real_escape_string($db_link, $entry);
//echo $entry."\n";
//echo $entry_mysql_safe."\n";
$xml_entry = new SimpleXMLElement($entry);
$query = "REPLACE INTO nvdData SET name = '$xml_entry[name]', type = '$xml_entry[type]', entry = '$entry_mysql_safe'";
if(!$result = mysqli_query($db_link, $query)) {
$xml = c_initiate_xml($config_nvdcache);
$xml_error = $xml->addchild('error');
$xml_error->addchild('code', '500');
$xml_error->addchild('description', 'DB Error: '.mysqli_error($db_link));
c_announce($xml);
}
}
function dbf_update_stats($db_link) {
$query = "UPDATE statistics SET last_db_update_epoch = '".time()."' WHERE stat_id = '1'";
if(!$result = mysqli_query($db_link, $query)) {
$xml = c_initiate_xml($config_nvdcache);
$xml_error = $xml->addchild('error');
$xml_error->addchild('code', '500');
$xml_error->addchild('description', 'DB Error: '.mysqli_error($db_link));
c_announce($xml);
}
}
//
function dbf_getEntryData($db_link, $entryName, $entryType, $config_nvdcache) {
$query = "SELECT * FROM nvdData WHERE type = '$entryType' AND name = '$entryName'";
if(!$result = mysqli_query($db_link, $query)) {
$xml = c_initiate_xml($config_nvdcache);
$xml_error = $xml->addchild('error');
$xml_error->addchild('code', '500');
$xml_error->addchild('description', 'DB Error: '.mysqli_error($db_link));
c_announce($xml);
}
if(mysqli_num_rows($result) == 0) {
$xml = c_initiate_xml($config_nvdcache);
$xml_error = $xml->addchild('error');
$xml_error->addchild('code', '400');
$xml_error->addchild('description', $entryType.' entry '.$entryName.' was not found.');
c_announce($xml);
}
// start building the xml data string
//$xml_parent_start = '';
$xml_parent_start = '<nvdCache version="'.$config_nvdcache[version].'" cacheHost="'.$config_nvdcache[cacheHost].'">';
$xml_parent_end = '</nvdCache>';
while ($row = mysqli_fetch_assoc($result)) {
$xml_result = $xml_parent_start;
$xml_result .= $row[entry];
$xml_result .= $xml_parent_end;
}
$xml = new SimpleXMLElement($xml_result);
return $xml;
}
?>