1 | <?php |
---|
2 | |
---|
3 | class Config |
---|
4 | { |
---|
5 | function Config($site) |
---|
6 | { |
---|
7 | config::init(); |
---|
8 | } |
---|
9 | |
---|
10 | function getPostPassword() |
---|
11 | { |
---|
12 | return config::get('post_password'); |
---|
13 | } |
---|
14 | |
---|
15 | function getPostMailto() |
---|
16 | { |
---|
17 | return config::get('post_mailto'); |
---|
18 | } |
---|
19 | |
---|
20 | function getKillPoints() |
---|
21 | { |
---|
22 | return config::get('kill_points'); |
---|
23 | } |
---|
24 | |
---|
25 | function getLeastActive() |
---|
26 | { |
---|
27 | return config::get('least_active'); |
---|
28 | } |
---|
29 | |
---|
30 | function getConfig($key) |
---|
31 | { |
---|
32 | return config::get($key); |
---|
33 | } |
---|
34 | |
---|
35 | function setConfig($key, $value) |
---|
36 | { |
---|
37 | config::set($key, $value); |
---|
38 | } |
---|
39 | |
---|
40 | function delConfig($key) |
---|
41 | { |
---|
42 | config::del($key, $value); |
---|
43 | } |
---|
44 | |
---|
45 | function checkCheckbox($name) |
---|
46 | { |
---|
47 | if ($_POST[$name] == 'on') |
---|
48 | { |
---|
49 | $this->setConfig($name, '1'); |
---|
50 | return true; |
---|
51 | } |
---|
52 | $this->setConfig($name, '0'); |
---|
53 | return false; |
---|
54 | } |
---|
55 | |
---|
56 | function setStyleName($name) |
---|
57 | { |
---|
58 | $this->setConfig("style_name", $name); |
---|
59 | } |
---|
60 | |
---|
61 | function setStyleBanner($banner) |
---|
62 | { |
---|
63 | $this->setConfig("style_banner", $banner); |
---|
64 | } |
---|
65 | |
---|
66 | function setPostPassword($password) |
---|
67 | { |
---|
68 | $this->setConfig("post_password", $password); |
---|
69 | } |
---|
70 | |
---|
71 | function setPostMailto($mailto) |
---|
72 | { |
---|
73 | $this->setConfig("post_mailto", $mailto); |
---|
74 | } |
---|
75 | |
---|
76 | function setKillPoints($flag) |
---|
77 | { |
---|
78 | $this->setConfig("kill_points", $flag); |
---|
79 | } |
---|
80 | |
---|
81 | function setLeastActive($flag) |
---|
82 | { |
---|
83 | $this->setConfig("least_active", $flag); |
---|
84 | } |
---|
85 | |
---|
86 | function init() |
---|
87 | { |
---|
88 | static $init = false; |
---|
89 | |
---|
90 | if ($init) |
---|
91 | { |
---|
92 | return; |
---|
93 | } |
---|
94 | |
---|
95 | $db = new DBQuery(); |
---|
96 | $db->execute('select * from kb3_config where cfg_site=\''.KB_SITE."'"); |
---|
97 | while ($row = $db->getRow()) |
---|
98 | { |
---|
99 | config::put($row['cfg_key'], $row['cfg_value']); |
---|
100 | } |
---|
101 | $init = true; |
---|
102 | |
---|
103 | if (config::get('post_password') === null) |
---|
104 | { |
---|
105 | // no config supplied, generate standard one |
---|
106 | config::set('style_name', 'default'); |
---|
107 | config::set('style_banner', 'default'); |
---|
108 | config::set('kill_points', 1); |
---|
109 | config::set('least_active', 0); |
---|
110 | config::set('post_password', 'CHANGEME'); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | function &_getCache() |
---|
115 | { |
---|
116 | static $cache; |
---|
117 | |
---|
118 | if (!isset($cache)) |
---|
119 | { |
---|
120 | $cache = array(); |
---|
121 | } |
---|
122 | return $cache; |
---|
123 | } |
---|
124 | |
---|
125 | function put($key, $data) |
---|
126 | { |
---|
127 | $cache = &config::_getCache(); |
---|
128 | $cache[$key] = $data; |
---|
129 | } |
---|
130 | |
---|
131 | function del($key) |
---|
132 | { |
---|
133 | $cache = &config::_getCache(); |
---|
134 | if (isset($cache[$key])) |
---|
135 | { |
---|
136 | unset($cache[$key]); |
---|
137 | } |
---|
138 | |
---|
139 | $qry = new DBQuery(); |
---|
140 | $qry->execute("delete from kb3_config where cfg_key = '".$key."' |
---|
141 | and cfg_site = '".KB_SITE."'"); |
---|
142 | } |
---|
143 | |
---|
144 | function set($key, $value) |
---|
145 | { |
---|
146 | $cache = &config::_getCache(); |
---|
147 | |
---|
148 | // only update the database when the old value differs |
---|
149 | if (isset($cache[$key])) |
---|
150 | { |
---|
151 | if ($cache[$key] == $value) |
---|
152 | { |
---|
153 | return; |
---|
154 | } |
---|
155 | } |
---|
156 | $cache[$key] = $value; |
---|
157 | |
---|
158 | $qry = new DBQuery(); |
---|
159 | $qry->execute("select cfg_value from kb3_config |
---|
160 | where cfg_key = '".$key."' and cfg_site = '".KB_SITE."'"); |
---|
161 | if ($qry->recordCount()) |
---|
162 | { |
---|
163 | $sql = "update kb3_config set cfg_value = '".$value."' |
---|
164 | where cfg_site = '".KB_SITE."' and cfg_key = '".$key."'"; |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | $sql = "insert into kb3_config values ('".KB_SITE."','".$key."','".$value."')"; |
---|
169 | } |
---|
170 | $qry->execute($sql); |
---|
171 | } |
---|
172 | |
---|
173 | function get($key) |
---|
174 | { |
---|
175 | $cache = &config::_getCache(); |
---|
176 | |
---|
177 | if (!isset($cache[$key])) |
---|
178 | { |
---|
179 | return null; |
---|
180 | } |
---|
181 | return $cache[$key]; |
---|
182 | } |
---|
183 | } |
---|
184 | ?> |
---|