1 | <?php |
---|
2 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
3 | |
---|
4 | /** |
---|
5 | * CF_Config class |
---|
6 | */ |
---|
7 | class CF_Config { |
---|
8 | static $default_config = array(); |
---|
9 | |
---|
10 | protected $config_values; |
---|
11 | protected $db_key = null; |
---|
12 | protected $db_comment = null; |
---|
13 | protected $config_lang; |
---|
14 | |
---|
15 | /* ************************ */ |
---|
16 | /* ** Constructor ** */ |
---|
17 | /* ************************ */ |
---|
18 | |
---|
19 | function CF_Config() { |
---|
20 | } |
---|
21 | |
---|
22 | /* ************************ */ |
---|
23 | /* ** Accessors ** */ |
---|
24 | /* ************************ */ |
---|
25 | |
---|
26 | function get_value($key) { |
---|
27 | if (isset($this->config_values[$key])) { |
---|
28 | return $this->config_values[$key]; |
---|
29 | } |
---|
30 | return null; |
---|
31 | } |
---|
32 | |
---|
33 | function set_value($key, $value) { |
---|
34 | $this->config_values[$key] = $value; |
---|
35 | } |
---|
36 | |
---|
37 | function set_db_key($key) { |
---|
38 | $this->db_key = $key; |
---|
39 | } |
---|
40 | |
---|
41 | function get_config_lang() { |
---|
42 | return $this->config_lang; |
---|
43 | } |
---|
44 | |
---|
45 | function get_lang_value($item, $language=null) { |
---|
46 | if (null == $language) { |
---|
47 | global $user; |
---|
48 | $language = $user['language']; |
---|
49 | } |
---|
50 | $value = $this->config_lang->get_value($language, $item); |
---|
51 | if (empty($value)) { |
---|
52 | cf_switch_to_default_lang(); |
---|
53 | $value = l10n($item); |
---|
54 | cf_switch_back_to_user_lang(); |
---|
55 | } |
---|
56 | return $value; |
---|
57 | } |
---|
58 | |
---|
59 | /* ************************ */ |
---|
60 | /* ** Loading methods ** */ |
---|
61 | /* ************************ */ |
---|
62 | |
---|
63 | function load_config() { |
---|
64 | $this->config_lang = null; |
---|
65 | if (null != $this->db_key) { |
---|
66 | $query = ' |
---|
67 | SELECT value |
---|
68 | FROM '.CONFIG_TABLE.' |
---|
69 | WHERE param = \''. $this->db_key .'\' |
---|
70 | ;'; |
---|
71 | $result = pwg_query($query); |
---|
72 | if($result) { |
---|
73 | $row = mysql_fetch_row($result); |
---|
74 | if(is_string($row[0])) { |
---|
75 | $this->config_values = unserialize($row[0]); |
---|
76 | if (isset($this->config_values['config_lang'])) { |
---|
77 | $this->config_lang = $this->config_values['config_lang']; |
---|
78 | $this->config_values['config_lang'] = null; |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | // CF_Log::add_debug($this->config_lang, 'CF_Config::load_config'); |
---|
84 | $this->load_default_config(); |
---|
85 | } |
---|
86 | |
---|
87 | protected function load_default_config() { |
---|
88 | if (null == $this->config_lang) { |
---|
89 | $this->config_lang = new CF_Config_Lang(); |
---|
90 | $this->config_values['config_lang'] = null; |
---|
91 | CF_Log::add_debug($this->config_lang,'CF_Config::load_default_config'); |
---|
92 | } |
---|
93 | $this->config_lang->set_default_values(); |
---|
94 | $this->config_lang->update_keys(); |
---|
95 | foreach (CF_Config::$default_config as $key => $value) { |
---|
96 | if (!isset($this->config_values[$key])) { |
---|
97 | $this->config_values[$key] = $value; |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | /* ************************ */ |
---|
103 | /* ** Saving methods ** */ |
---|
104 | /* ************************ */ |
---|
105 | |
---|
106 | function save_config() { |
---|
107 | if (null == $this->db_key) { |
---|
108 | return false; |
---|
109 | } |
---|
110 | $this->config_values['config_lang'] = $this->config_lang; |
---|
111 | if (!isset($this->config_values[CF_CFG_COMMENT])) { |
---|
112 | $this->set_value(CF_CFG_COMMENT, CF_CFG_DB_COMMENT); |
---|
113 | } |
---|
114 | $db_comment = sprintf($this->config_values[CF_CFG_COMMENT], |
---|
115 | $this->db_key); |
---|
116 | $query = ' |
---|
117 | REPLACE INTO '.CONFIG_TABLE.' |
---|
118 | VALUES( |
---|
119 | \''. $this->db_key .'\', |
---|
120 | \''.serialize($this->config_values).'\', |
---|
121 | \''. $db_comment . '\') |
---|
122 | ;'; |
---|
123 | $result = pwg_query($query); |
---|
124 | if($result) { |
---|
125 | return true; |
---|
126 | } else { |
---|
127 | return false; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | /* ************************ */ |
---|
132 | /* ** Maintain methods ** */ |
---|
133 | /* ************************ */ |
---|
134 | |
---|
135 | static function install($plugin_id) { |
---|
136 | $config = new CF_Config(); |
---|
137 | $config->set_db_key($plugin_id); |
---|
138 | $config->load_config(); |
---|
139 | $default_config = CF_Config::$default_config; |
---|
140 | if (isset($default_config[CF_CFG_COMMENT])) { |
---|
141 | // Override comment |
---|
142 | $config->set_value(CF_CFG_COMMENT, $default_config[CF_CFG_COMMENT]); |
---|
143 | } |
---|
144 | $result = $config->save_config(); |
---|
145 | return $result; |
---|
146 | } |
---|
147 | |
---|
148 | static function uninstall($plugin_id) { |
---|
149 | $query = ' |
---|
150 | DELETE FROM '.CONFIG_TABLE.' |
---|
151 | WHERE param = \'' . $plugin_id . '\' |
---|
152 | ;'; |
---|
153 | $result = pwg_query($query); |
---|
154 | if($result) { |
---|
155 | return true; |
---|
156 | } else { |
---|
157 | return false; |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | ?> |
---|