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(); |
---|
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 | } |
---|
51 | } |
---|
52 | |
---|
53 | function &_getCache() |
---|
54 | { |
---|
55 | static $cache; |
---|
56 | |
---|
57 | if (!isset($cache)) |
---|
58 | { |
---|
59 | $cache = array(); |
---|
60 | } |
---|
61 | return $cache; |
---|
62 | } |
---|
63 | |
---|
64 | function put($key, $data) |
---|
65 | { |
---|
66 | $cache = &config::_getCache(); |
---|
67 | $cache[$key] = $data; |
---|
68 | } |
---|
69 | |
---|
70 | function del($key) |
---|
71 | { |
---|
72 | $cache = &config::_getCache(); |
---|
73 | if (isset($cache[$key])) |
---|
74 | { |
---|
75 | unset($cache[$key]); |
---|
76 | } |
---|
77 | |
---|
78 | $qry = new DBQuery(); |
---|
79 | $qry->execute("delete from kb3_config where cfg_key = '".$key."' |
---|
80 | and cfg_site = '".KB_SITE."'"); |
---|
81 | } |
---|
82 | |
---|
83 | function set($key, $value) |
---|
84 | { |
---|
85 | $cache = &config::_getCache(); |
---|
86 | |
---|
87 | // only update the database when the old value differs |
---|
88 | if (isset($cache[$key])) |
---|
89 | { |
---|
90 | if ($cache[$key] === $value) |
---|
91 | { |
---|
92 | return; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | if (is_array($value)) |
---|
97 | { |
---|
98 | $cache[$key] = $value; |
---|
99 | $value = serialize($value); |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | $cache[$key] = stripslashes($value); |
---|
104 | } |
---|
105 | $value = addslashes($value); |
---|
106 | |
---|
107 | $qry = new DBQuery(); |
---|
108 | $qry->execute("select cfg_value from kb3_config |
---|
109 | where cfg_key = '".$key."' and cfg_site = '".KB_SITE."'"); |
---|
110 | if ($qry->recordCount()) |
---|
111 | { |
---|
112 | $sql = "update kb3_config set cfg_value = '".$value."' |
---|
113 | where cfg_site = '".KB_SITE."' and cfg_key = '".$key."'"; |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | $sql = "insert into kb3_config values ('".KB_SITE."','".$key."','".$value."')"; |
---|
118 | } |
---|
119 | $qry->execute($sql); |
---|
120 | } |
---|
121 | |
---|
122 | function &get($key) |
---|
123 | { |
---|
124 | $cache = &config::_getCache(); |
---|
125 | |
---|
126 | if (!isset($cache[$key])) |
---|
127 | { |
---|
128 | return config::defaultval($key); |
---|
129 | } |
---|
130 | return stripslashes($cache[$key]); |
---|
131 | } |
---|
132 | |
---|
133 | function defaultval($key) |
---|
134 | { |
---|
135 | // add important upgrade configs here, they will return the default if not set |
---|
136 | $defaults = array('summarytable_rowcount' => 6); |
---|
137 | |
---|
138 | if (!isset($defaults[$key])) |
---|
139 | { |
---|
140 | return null; |
---|
141 | } |
---|
142 | return $defaults[$key]; |
---|
143 | } |
---|
144 | } |
---|
145 | ?> |
---|