1 | <?php |
---|
2 | class Config |
---|
3 | { |
---|
4 | function Config($site) |
---|
5 | { |
---|
6 | config::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 $config_init; |
---|
23 | |
---|
24 | if ($config_init) |
---|
25 | { |
---|
26 | return; |
---|
27 | } |
---|
28 | |
---|
29 | $db = new DBQuery(true); |
---|
30 | $db->execute('select * from kb3_config where cfg_site=\''.KB_SITE."'"); |
---|
31 | $config = &config::_getCache(); |
---|
32 | while ($row = $db->getRow()) |
---|
33 | { |
---|
34 | if (substr($row['cfg_value'], 0, 2) == 'a:') |
---|
35 | { |
---|
36 | $row['cfg_value'] = unserialize($row['cfg_value']); |
---|
37 | } |
---|
38 | $config[$row['cfg_key']] = $row['cfg_value']; |
---|
39 | } |
---|
40 | $config_init = true; |
---|
41 | |
---|
42 | if (config::get('post_password') === null) |
---|
43 | { |
---|
44 | // no config supplied, generate standard one |
---|
45 | config::set('style_name', 'default'); |
---|
46 | config::set('style_banner', 'default'); |
---|
47 | config::set('kill_points', 1); |
---|
48 | config::set('least_active', 0); |
---|
49 | config::set('post_password', 'CHANGEME'); |
---|
50 | config::set('comment_password', 'CHANGEME'); |
---|
51 | config::set('cfg_memcache', 0); |
---|
52 | config::set('cfg_memcache_server', 'memcached server address'); |
---|
53 | config::set('cfg_memcache_port', 'memcached server port'); |
---|
54 | config::set('cache_dir', 'cache/cache'); |
---|
55 | config::set('km_cache_dir', 'cache/mails'); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | function &_getCache() |
---|
60 | { |
---|
61 | static $cache; |
---|
62 | |
---|
63 | if (!isset($cache)) |
---|
64 | { |
---|
65 | $cache = array(); |
---|
66 | } |
---|
67 | return $cache; |
---|
68 | } |
---|
69 | |
---|
70 | function put($key, $data) |
---|
71 | { |
---|
72 | $cache = &config::_getCache(); |
---|
73 | $cache[$key] = $data; |
---|
74 | } |
---|
75 | |
---|
76 | function del($key) |
---|
77 | { |
---|
78 | $cache = &config::_getCache(); |
---|
79 | if (isset($cache[$key])) |
---|
80 | { |
---|
81 | unset($cache[$key]); |
---|
82 | } |
---|
83 | |
---|
84 | $qry = new DBQuery(); |
---|
85 | $qry->execute("delete from kb3_config where cfg_key = '".$key."' |
---|
86 | and cfg_site = '".KB_SITE."'"); |
---|
87 | } |
---|
88 | |
---|
89 | function set($key, $value) |
---|
90 | { |
---|
91 | $cache = &config::_getCache(); |
---|
92 | |
---|
93 | // only update the database when the old value differs |
---|
94 | if (isset($cache[$key])) |
---|
95 | { |
---|
96 | if ($cache[$key] === $value) |
---|
97 | { |
---|
98 | return; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | if (is_array($value)) |
---|
103 | { |
---|
104 | $cache[$key] = $value; |
---|
105 | $value = serialize($value); |
---|
106 | } |
---|
107 | else |
---|
108 | { |
---|
109 | $cache[$key] = stripslashes($value); |
---|
110 | } |
---|
111 | $value = addslashes($value); |
---|
112 | |
---|
113 | $qry = new DBQuery(); |
---|
114 | $qry->execute("select cfg_value from kb3_config |
---|
115 | where cfg_key = '".$key."' and cfg_site = '".KB_SITE."'"); |
---|
116 | if ($qry->recordCount()) |
---|
117 | { |
---|
118 | $sql = "update kb3_config set cfg_value = '".$value."' |
---|
119 | where cfg_site = '".KB_SITE."' and cfg_key = '".$key."'"; |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | $sql = "insert into kb3_config values ('".KB_SITE."','".$key."','".$value."')"; |
---|
124 | } |
---|
125 | $qry->execute($sql); |
---|
126 | } |
---|
127 | |
---|
128 | function &get($key) |
---|
129 | { |
---|
130 | $cache = &config::_getCache(); |
---|
131 | |
---|
132 | if (!isset($cache[$key])) |
---|
133 | { |
---|
134 | return config::defaultval($key); |
---|
135 | } |
---|
136 | return stripslashes($cache[$key]); |
---|
137 | } |
---|
138 | |
---|
139 | function &getnumerical($key) |
---|
140 | { |
---|
141 | $cache = &config::_getCache(); |
---|
142 | |
---|
143 | if (!isset($cache[$key])) |
---|
144 | { |
---|
145 | return config::defaultval($key); |
---|
146 | } |
---|
147 | return $cache[$key]; |
---|
148 | } |
---|
149 | function defaultval($key) |
---|
150 | { |
---|
151 | // add important upgrade configs here, they will return the default if not set |
---|
152 | // they will be shown as set but take no space in the database |
---|
153 | $defaults = array('summarytable_rowcount' => 8); |
---|
154 | $defaults = array('killcount' => 50); |
---|
155 | |
---|
156 | if (!isset($defaults[$key])) |
---|
157 | { |
---|
158 | return null; |
---|
159 | } |
---|
160 | return $defaults[$key]; |
---|
161 | } |
---|
162 | } |
---|
163 | ?> |
---|