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