1 | <?php |
---|
2 | class ApiCache |
---|
3 | { |
---|
4 | function ApiCache($site) |
---|
5 | { |
---|
6 | ApiCache::init(); |
---|
7 | } |
---|
8 | |
---|
9 | function checkCheckbox($name) |
---|
10 | { |
---|
11 | if ($_POST[$name] == 'on') |
---|
12 | { |
---|
13 | config::set($name, '1'); |
---|
14 | return true; |
---|
15 | } |
---|
16 | config::set($name, '0'); |
---|
17 | return false; |
---|
18 | } |
---|
19 | |
---|
20 | function init() |
---|
21 | { |
---|
22 | global $ApiCache_init; |
---|
23 | |
---|
24 | if ($ApiCache_init) |
---|
25 | { |
---|
26 | return; |
---|
27 | } |
---|
28 | |
---|
29 | $db = new DBQuery(true); |
---|
30 | $db->execute("CREATE TABLE IF NOT EXISTS `kb3_apicache` ( |
---|
31 | `cfg_site` varchar(16) NOT NULL default '', |
---|
32 | `cfg_key` varchar(32) NOT NULL default '', |
---|
33 | `cfg_value` text NOT NULL, |
---|
34 | PRIMARY KEY (`cfg_site`,`cfg_key`) |
---|
35 | )"); |
---|
36 | $db->execute('select * from kb3_apicache where cfg_site=\''.KB_SITE."'"); |
---|
37 | $ApiCache = &ApiCache::_getCache(); |
---|
38 | while ($row = $db->getRow()) |
---|
39 | { |
---|
40 | if (substr($row['cfg_value'], 0, 2) == 'a:') |
---|
41 | { |
---|
42 | $row['cfg_value'] = unserialize($row['cfg_value']); |
---|
43 | } |
---|
44 | $ApiCache[$row['cfg_key']] = $row['cfg_value']; |
---|
45 | } |
---|
46 | $ApiCache_init = true; |
---|
47 | } |
---|
48 | |
---|
49 | function &_getCache() |
---|
50 | { |
---|
51 | static $cache; |
---|
52 | |
---|
53 | if (!isset($cache)) |
---|
54 | { |
---|
55 | $cache = array(); |
---|
56 | } |
---|
57 | return $cache; |
---|
58 | } |
---|
59 | |
---|
60 | function put($key, $data) |
---|
61 | { |
---|
62 | $cache = &ApiCache::_getCache(); |
---|
63 | $cache[$key] = $data; |
---|
64 | } |
---|
65 | |
---|
66 | function del($key) |
---|
67 | { |
---|
68 | $cache = &ApiCache::_getCache(); |
---|
69 | if (isset($cache[$key])) |
---|
70 | { |
---|
71 | unset($cache[$key]); |
---|
72 | } |
---|
73 | |
---|
74 | $qry = new DBQuery(); |
---|
75 | $qry->execute("delete from kb3_apicache where cfg_key = '".$key."' |
---|
76 | and cfg_site = '".KB_SITE."'"); |
---|
77 | } |
---|
78 | |
---|
79 | function set($key, $value) |
---|
80 | { |
---|
81 | //$cache = &ApiCache::_getCache(); |
---|
82 | |
---|
83 | // only update the database when the old value differs |
---|
84 | if (isset($cache[$key])) |
---|
85 | { |
---|
86 | if ($cache[$key] === $value) |
---|
87 | { |
---|
88 | return; |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | if (is_array($value)) |
---|
93 | { |
---|
94 | $cache[$key] = $value; |
---|
95 | $value = serialize($value); |
---|
96 | } |
---|
97 | else |
---|
98 | { |
---|
99 | $cache[$key] = stripslashes($value); |
---|
100 | } |
---|
101 | $value = addslashes($value); |
---|
102 | |
---|
103 | $qry = new DBQuery(); |
---|
104 | $qry->execute("select cfg_value from kb3_apicache |
---|
105 | where cfg_key = '".$key."' and cfg_site = '".KB_SITE."'"); |
---|
106 | if ($qry->recordCount()) |
---|
107 | { |
---|
108 | $sql = "update kb3_apicache set cfg_value = '".$value."' |
---|
109 | where cfg_site = '".KB_SITE."' and cfg_key = '".$key."'"; |
---|
110 | } |
---|
111 | else |
---|
112 | { |
---|
113 | $sql = "insert into kb3_apicache values ('".KB_SITE."','".$key."','".$value."')"; |
---|
114 | } |
---|
115 | $qry->execute($sql); |
---|
116 | } |
---|
117 | |
---|
118 | function &get($key) |
---|
119 | { |
---|
120 | $cache = &ApiCache::_getCache(); |
---|
121 | |
---|
122 | if (!isset($cache[$key])) |
---|
123 | { |
---|
124 | return ApiCache::defaultval($key); |
---|
125 | } |
---|
126 | return stripslashes($cache[$key]); |
---|
127 | } |
---|
128 | |
---|
129 | function &getnumerical($key) |
---|
130 | { |
---|
131 | $cache = &ApiCache::_getCache(); |
---|
132 | |
---|
133 | if (!isset($cache[$key])) |
---|
134 | { |
---|
135 | return ApiCache::defaultval($key); |
---|
136 | } |
---|
137 | return $cache[$key]; |
---|
138 | } |
---|
139 | function defaultval($key) |
---|
140 | { |
---|
141 | // add important upgrade configs here, they will return the default if not set |
---|
142 | // they will be shown as set but take no space in the database |
---|
143 | //$defaults = array('summarytable_rowcount' => 8); |
---|
144 | //$defaults = array('killcount' => 50); |
---|
145 | |
---|
146 | if (!isset($defaults[$key])) |
---|
147 | { |
---|
148 | return null; |
---|
149 | } |
---|
150 | return $defaults[$key]; |
---|
151 | } |
---|
152 | } |
---|
153 | ?> |
---|