1 |
<?php |
2 |
|
3 |
/** |
4 |
* Config_File class. |
5 |
* |
6 |
* @version 2.3.0 |
7 |
* @author Andrei Zmievski <andrei@php.net> |
8 |
* @access public |
9 |
* |
10 |
* Copyright: 2001,2002 ispi of Lincoln, Inc. |
11 |
* |
12 |
* This library is free software; you can redistribute it and/or |
13 |
* modify it under the terms of the GNU Lesser General Public |
14 |
* License as published by the Free Software Foundation; either |
15 |
* version 2.1 of the License, or (at your option) any later version. |
16 |
* |
17 |
* This library is distributed in the hope that it will be useful, |
18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 |
* Lesser General Public License for more details. |
21 |
* |
22 |
* You should have received a copy of the GNU Lesser General Public |
23 |
* License along with this library; if not, write to the Free Software |
24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
25 |
* |
26 |
* You may contact the author of Config_File by e-mail at: |
27 |
* andrei@php.net |
28 |
* |
29 |
* Or, write to: |
30 |
* Andrei Zmievski |
31 |
* Software Engineer, ispi |
32 |
* 237 S. 70th suite 220 |
33 |
* Lincoln, NE 68510 |
34 |
* |
35 |
* The latest version of Config_File can be obtained from: |
36 |
* http://www.phpinsider.com |
37 |
*/ |
38 |
|
39 |
class Config_File { |
40 |
/* Options */ |
41 |
/** |
42 |
* Controls whether variables with the same name overwrite each other. |
43 |
* |
44 |
* @access public |
45 |
*/ |
46 |
var $overwrite = true; |
47 |
|
48 |
/** |
49 |
* Controls whether config values of on/true/yes and off/false/no get |
50 |
* converted to boolean values automatically. |
51 |
* |
52 |
* @access public |
53 |
*/ |
54 |
var $booleanize = true; |
55 |
|
56 |
/** |
57 |
* Controls whether hidden config sections/vars are read from the file. |
58 |
* |
59 |
* @access public |
60 |
*/ |
61 |
var $read_hidden = true; |
62 |
|
63 |
/* Private variables */ |
64 |
var $_config_path = ""; |
65 |
var $_config_data = array(); |
66 |
var $_separator = ""; |
67 |
|
68 |
|
69 |
/** |
70 |
* Constructs a new config file class. |
71 |
* |
72 |
* @param $config_path string (optional) path to the config files |
73 |
* @access public |
74 |
*/ |
75 |
function Config_File($config_path = NULL) |
76 |
{ |
77 |
if (substr(PHP_OS, 0, 3) == "WIN" || substr(PHP_OS, 0, 4) == "OS/2") |
78 |
$this->_separator = "\\"; |
79 |
else |
80 |
$this->_separator = "/"; |
81 |
|
82 |
if (isset($config_path)) |
83 |
$this->set_path($config_path); |
84 |
} |
85 |
|
86 |
|
87 |
/** |
88 |
* Set the path where configuration files can be found. |
89 |
* |
90 |
* @param $config_path string path to the config files |
91 |
* @access public |
92 |
*/ |
93 |
function set_path($config_path) |
94 |
{ |
95 |
if (!empty($config_path)) { |
96 |
if (!is_string($config_path) || !file_exists($config_path) || !is_dir($config_path)) { |
97 |
$this->_trigger_error_msg("Bad config file path '$config_path'"); |
98 |
return; |
99 |
} |
100 |
|
101 |
$this->_config_path = $config_path . $this->_separator; |
102 |
} |
103 |
} |
104 |
|
105 |
|
106 |
/** |
107 |
* Retrieves config info based on the file, section, and variable name. |
108 |
* |
109 |
* @access public |
110 |
* @param $file_name string config file to get info for |
111 |
* @param $section_name string (optional) section to get info for |
112 |
* @param $var_name string (optional) variable to get info for |
113 |
* @return mixed a value or array of values |
114 |
*/ |
115 |
function &get($file_name, $section_name = NULL, $var_name = NULL) |
116 |
{ |
117 |
if (empty($file_name)) { |
118 |
$this->_trigger_error_msg('Empty config file name'); |
119 |
return; |
120 |
} else { |
121 |
$file_name = $this->_config_path . $file_name; |
122 |
if (!isset($this->_config_data[$file_name])) |
123 |
$this->load_file($file_name, false); |
124 |
} |
125 |
|
126 |
if (!empty($var_name)) { |
127 |
if (empty($section_name)) { |
128 |
return $this->_config_data[$file_name]["vars"][$var_name]; |
129 |
} else { |
130 |
if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name])) |
131 |
return $this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name]; |
132 |
else |
133 |
return array(); |
134 |
} |
135 |
} else { |
136 |
if (empty($section_name)) { |
137 |
return (array)$this->_config_data[$file_name]["vars"]; |
138 |
} else { |
139 |
if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"])) |
140 |
return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"]; |
141 |
else |
142 |
return array(); |
143 |
} |
144 |
} |
145 |
} |
146 |
|
147 |
|
148 |
/** |
149 |
* Retrieves config info based on the key. |
150 |
* |
151 |
* @access public |
152 |
* @param $file_name string config key (filename/section/var) |
153 |
* @return mixed a value or array of values |
154 |
*/ |
155 |
function &get_key($config_key) |
156 |
{ |
157 |
list($file_name, $section_name, $var_name) = explode('/', $config_key, 3); |
158 |
$result = &$this->get($file_name, $section_name, $var_name); |
159 |
return $result; |
160 |
} |
161 |
|
162 |
/** |
163 |
* Get all loaded config file names. |
164 |
* |
165 |
* @access public |
166 |
* @return array an array of loaded config file names |
167 |
*/ |
168 |
function get_file_names() |
169 |
{ |
170 |
return array_keys($this->_config_data); |
171 |
} |
172 |
|
173 |
|
174 |
/** |
175 |
* Get all section names from a loaded file. |
176 |
* |
177 |
* @access public |
178 |
* @param $file_name string config file to get section names from |
179 |
* @return array an array of section names from the specified file |
180 |
*/ |
181 |
function get_section_names($file_name) |
182 |
{ |
183 |
$file_name = $this->_config_path . $file_name; |
184 |
if (!isset($this->_config_data[$file_name])) { |
185 |
$this->_trigger_error_msg("Unknown config file '$file_name'"); |
186 |
return; |
187 |
} |
188 |
|
189 |
return array_keys($this->_config_data[$file_name]["sections"]); |
190 |
} |
191 |
|
192 |
|
193 |
/** |
194 |
* Get all global or section variable names. |
195 |
* |
196 |
* @access public |
197 |
* @param $file_name string config file to get info for |
198 |
* @param $section_name string (optional) section to get info for |
199 |
* @return array an array of variables names from the specified file/section |
200 |
*/ |
201 |
function get_var_names($file_name, $section = NULL) |
202 |
{ |
203 |
if (empty($file_name)) { |
204 |
$this->_trigger_error_msg('Empty config file name'); |
205 |
return; |
206 |
} else if (!isset($this->_config_data[$file_name])) { |
207 |
$this->_trigger_error_msg("Unknown config file '$file_name'"); |
208 |
return; |
209 |
} |
210 |
|
211 |
if (empty($section)) |
212 |
return array_keys($this->_config_data[$file_name]["vars"]); |
213 |
else |
214 |
return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]); |
215 |
} |
216 |
|
217 |
|
218 |
/** |
219 |
* Clear loaded config data for a certain file or all files. |
220 |
* |
221 |
* @access public |
222 |
* @param $file_name string file to clear config data for |
223 |
*/ |
224 |
function clear($file_name = NULL) |
225 |
{ |
226 |
if ($file_name === NULL) |
227 |
$this->_config_data = array(); |
228 |
else if (isset($this->_config_data[$file_name])) |
229 |
$this->_config_data[$file_name] = array(); |
230 |
} |
231 |
|
232 |
|
233 |
/** |
234 |
* Load a configuration file manually. |
235 |
* |
236 |
* @access public |
237 |
* @param $file_name string file name to load |
238 |
* @param $prepend_path boolean whether current config path should be prepended to the filename |
239 |
*/ |
240 |
function load_file($file_name, $prepend_path = true) |
241 |
{ |
242 |
if ($prepend_path && $this->_config_path != "") |
243 |
$config_file = $this->_config_path . $file_name; |
244 |
else |
245 |
$config_file = $file_name; |
246 |
|
247 |
ini_set('track_errors', true); |
248 |
$fp = @fopen($config_file, "r"); |
249 |
if (!is_resource($fp)) { |
250 |
$this->_trigger_error_msg("Could not open config file '$config_file'"); |
251 |
return; |
252 |
} |
253 |
|
254 |
$contents = fread($fp, filesize($config_file)); |
255 |
fclose($fp); |
256 |
|
257 |
$config_data = array(); |
258 |
|
259 |
/* Get global variables first. */ |
260 |
if (preg_match("/^(.*?)(\n\[|\Z)/s", $contents, $match)) |
261 |
$config_data["vars"] = $this->_parse_config_block($match[1]); |
262 |
|
263 |
/* Get section variables. */ |
264 |
$config_data["sections"] = array(); |
265 |
preg_match_all("/^\[(.*?)\]/m", $contents, $match); |
266 |
foreach ($match[1] as $section) { |
267 |
if ($section{0} == '.' && !$this->read_hidden) |
268 |
continue; |
269 |
if (preg_match("/\[".preg_quote($section)."\](.*?)(\n\[|\Z)/s", $contents, $match)) |
270 |
if ($section{0} == '.') |
271 |
$section = substr($section, 1); |
272 |
$config_data["sections"][$section]["vars"] = $this->_parse_config_block($match[1]); |
273 |
} |
274 |
|
275 |
$this->_config_data[$config_file] = $config_data; |
276 |
} |
277 |
|
278 |
|
279 |
function _parse_config_block($config_block) |
280 |
{ |
281 |
$vars = array(); |
282 |
|
283 |
/* First we grab the multi-line values. */ |
284 |
if (preg_match_all("/^([^=\n]+)=\s*\"{3}(.*?)\"{3}\s*$/ms", $config_block, $match, PREG_SET_ORDER)) { |
285 |
for ($i = 0; $i < count($match); $i++) { |
286 |
$this->_set_config_var($vars, trim($match[$i][1]), $match[$i][2], false); |
287 |
} |
288 |
$config_block = preg_replace("/^[^=\n]+=\s*\"{3}.*?\"{3}\s*$/ms", "", $config_block); |
289 |
} |
290 |
|
291 |
|
292 |
$config_lines = preg_split("/\n+/", $config_block); |
293 |
|
294 |
foreach ($config_lines as $line) { |
295 |
if (preg_match("/^\s*(\.?\w+)\s*=(.*)/", $line, $match)) { |
296 |
$var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', trim($match[2])); |
297 |
$this->_set_config_var($vars, trim($match[1]), $var_value, $this->booleanize); |
298 |
} |
299 |
} |
300 |
|
301 |
return $vars; |
302 |
} |
303 |
|
304 |
function _set_config_var(&$container, $var_name, $var_value, $booleanize) |
305 |
{ |
306 |
if ($var_name{0} == '.') { |
307 |
if (!$this->read_hidden) |
308 |
return; |
309 |
else |
310 |
$var_name = substr($var_name, 1); |
311 |
} |
312 |
|
313 |
if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) { |
314 |
$this->_trigger_error_msg("Bad variable name '$var_name'"); |
315 |
return; |
316 |
} |
317 |
|
318 |
if ($booleanize) { |
319 |
if (preg_match("/^(on|true|yes)$/i", $var_value)) |
320 |
$var_value = true; |
321 |
else if (preg_match("/^(off|false|no)$/i", $var_value)) |
322 |
$var_value = false; |
323 |
} |
324 |
|
325 |
if (!isset($container[$var_name]) || $this->overwrite) |
326 |
$container[$var_name] = $var_value; |
327 |
else { |
328 |
settype($container[$var_name], 'array'); |
329 |
$container[$var_name][] = $var_value; |
330 |
} |
331 |
} |
332 |
|
333 |
function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING) |
334 |
{ |
335 |
trigger_error("Config_File error: $error_msg", $error_type); |
336 |
} |
337 |
} |
338 |
|
339 |
?> |