6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * 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
20 * You may contact the author of Config_File by e-mail at:
21 * {@link andrei@php.net}
23 * The latest version of Config_File can be obtained from:
24 * http://smarty.php.net/
26 * @link http://smarty.php.net/
28 * @copyright Copyright: 2001-2003 ispi of Lincoln, Inc.
29 * @author Andrei Zmievski <andrei@php.net>
34 /* $Id: Config_File.class.php,v 1.53 2003/10/08 23:43:34 mohrt Exp $ */
36 * Config file reading class
45 * Controls whether variables with the same name overwrite each other.
47 var $overwrite = true;
50 * Controls whether config values of on/true/yes and off/false/no get
51 * converted to boolean values automatically.
53 var $booleanize = true;
56 * Controls whether hidden config sections/vars are read from the file.
58 var $read_hidden = true;
61 * Controls whether or not to fix mac or dos formatted newlines.
62 * If set to true, \r or \r\n will be changed to \n.
64 var $fix_newlines = true;
67 /** @access private */
68 var $_config_path = "";
69 var $_config_data = array();
73 * Constructs a new config file class.
75 * @param string $config_path (optional) path to the config files
77 function Config_File($config_path = NULL)
79 if (isset($config_path))
80 $this->set_path($config_path);
85 * Set the path where configuration files can be found.
87 * @param string $config_path path to the config files
89 function set_path($config_path)
91 if (!empty($config_path)) {
92 if (!is_string($config_path) ||
!file_exists($config_path) ||
!is_dir($config_path)) {
93 $this->_trigger_error_msg("Bad config file path '$config_path'");
96 if(substr($config_path, -1) != DIRECTORY_SEPARATOR
) {
97 $config_path .= DIRECTORY_SEPARATOR
;
100 $this->_config_path
= $config_path;
106 * Retrieves config info based on the file, section, and variable name.
108 * @param string $file_name config file to get info for
109 * @param string $section_name (optional) section to get info for
110 * @param string $var_name (optional) variable to get info for
111 * @return string|array a value or array of values
113 function &get($file_name, $section_name = NULL, $var_name = NULL)
115 if (empty($file_name)) {
116 $this->_trigger_error_msg('Empty config file name');
119 $file_name = $this->_config_path
. $file_name;
120 if (!isset($this->_config_data
[$file_name]))
121 $this->load_file($file_name, false);
124 if (!empty($var_name)) {
125 if (empty($section_name)) {
126 return $this->_config_data
[$file_name]["vars"][$var_name];
128 if(isset($this->_config_data
[$file_name]["sections"][$section_name]["vars"][$var_name]))
129 return $this->_config_data
[$file_name]["sections"][$section_name]["vars"][$var_name];
134 if (empty($section_name)) {
135 return (array)$this->_config_data
[$file_name]["vars"];
137 if(isset($this->_config_data
[$file_name]["sections"][$section_name]["vars"]))
138 return (array)$this->_config_data
[$file_name]["sections"][$section_name]["vars"];
147 * Retrieves config info based on the key.
149 * @param $file_name string config key (filename/section/var)
150 * @return string|array same as get()
151 * @uses get() retrieves information from config file and returns it
153 function &get_key($config_key)
155 list($file_name, $section_name, $var_name) = explode('/', $config_key, 3);
156 $result = &$this->get($file_name, $section_name, $var_name);
161 * Get all loaded config file names.
163 * @return array an array of loaded config file names
165 function get_file_names()
167 return array_keys($this->_config_data
);
172 * Get all section names from a loaded file.
174 * @param string $file_name config file to get section names from
175 * @return array an array of section names from the specified file
177 function get_section_names($file_name)
179 $file_name = $this->_config_path
. $file_name;
180 if (!isset($this->_config_data
[$file_name])) {
181 $this->_trigger_error_msg("Unknown config file '$file_name'");
185 return array_keys($this->_config_data
[$file_name]["sections"]);
190 * Get all global or section variable names.
192 * @param string $file_name config file to get info for
193 * @param string $section_name (optional) section to get info for
194 * @return array an array of variables names from the specified file/section
196 function get_var_names($file_name, $section = NULL)
198 if (empty($file_name)) {
199 $this->_trigger_error_msg('Empty config file name');
201 } else if (!isset($this->_config_data
[$file_name])) {
202 $this->_trigger_error_msg("Unknown config file '$file_name'");
207 return array_keys($this->_config_data
[$file_name]["vars"]);
209 return array_keys($this->_config_data
[$file_name]["sections"][$section]["vars"]);
214 * Clear loaded config data for a certain file or all files.
216 * @param string $file_name file to clear config data for
218 function clear($file_name = NULL)
220 if ($file_name === NULL)
221 $this->_config_data
= array();
222 else if (isset($this->_config_data
[$file_name]))
223 $this->_config_data
[$file_name] = array();
228 * Load a configuration file manually.
230 * @param string $file_name file name to load
231 * @param boolean $prepend_path whether current config path should be
232 * prepended to the filename
234 function load_file($file_name, $prepend_path = true)
236 if ($prepend_path && $this->_config_path
!= "")
237 $config_file = $this->_config_path
. $file_name;
239 $config_file = $file_name;
241 ini_set('track_errors', true);
242 $fp = @fopen
($config_file, "r");
243 if (!is_resource($fp)) {
244 $this->_trigger_error_msg("Could not open config file '$config_file'");
248 $contents = fread($fp, filesize($config_file));
251 if($this->fix_newlines
) {
252 // fix mac/dos formatted newlines
253 $contents = preg_replace('!\r\n?!',"\n",$contents);
256 $config_data = array();
258 /* Get global variables first. */
259 if ($contents{0} != '[' && preg_match("/^(.*?)(\n\[|\Z)/s", $contents, $match))
260 $config_data["vars"] = $this->_parse_config_block($match[1]);
262 /* Get section variables. */
263 $config_data["sections"] = array();
264 preg_match_all("/^\[(.*?)\]/m", $contents, $match);
265 foreach ($match[1] as $section) {
266 if ($section{0} == '.' && !$this->read_hidden
)
268 if (preg_match("/\[".preg_quote($section, '/')."\](.*?)(\n\[|\Z)/s", $contents, $match))
269 if ($section{0} == '.')
270 $section = substr($section, 1);
271 $config_data["sections"][$section]["vars"] = $this->_parse_config_block($match[1]);
274 $this->_config_data
[$config_file] = $config_data;
279 /**#@+ @access private */
281 * @var string $config_block
283 function _parse_config_block($config_block)
287 /* First we grab the multi-line values. */
288 if (preg_match_all("/^([^=\n]+)=\s*\"{3}(.*?)\"{3}\s*$/ms", $config_block, $match, PREG_SET_ORDER
)) {
289 for ($i = 0; $i < count($match); $i++
) {
290 $this->_set_config_var($vars, trim($match[$i][1]), $match[$i][2], false);
292 $config_block = preg_replace("/^[^=\n]+=\s*\"{3}.*?\"{3}\s*$/ms", "", $config_block);
296 $config_lines = preg_split("/\n+/", $config_block);
298 foreach ($config_lines as $line) {
299 if (preg_match("/^\s*(\.?\w+)\s*=(.*)/", $line, $match)) {
300 $var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', trim($match[2]));
301 $this->_set_config_var($vars, trim($match[1]), $var_value, $this->booleanize
);
309 * @param array &$container
310 * @param string $var_name
311 * @param mixed $var_value
312 * @param boolean $booleanize determines whether $var_value is converted to
315 function _set_config_var(&$container, $var_name, $var_value, $booleanize)
317 if ($var_name{0} == '.') {
318 if (!$this->read_hidden
)
321 $var_name = substr($var_name, 1);
324 if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {
325 $this->_trigger_error_msg("Bad variable name '$var_name'");
330 if (preg_match("/^(on|true|yes)$/i", $var_value))
332 else if (preg_match("/^(off|false|no)$/i", $var_value))
336 if (!isset($container[$var_name]) ||
$this->overwrite
)
337 $container[$var_name] = $var_value;
339 settype($container[$var_name], 'array');
340 $container[$var_name][] = $var_value;
345 * @uses trigger_error() creates a PHP warning/error
346 * @param string $error_msg
347 * @param integer $error_type one of
349 function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING
)
351 trigger_error("Config_File error: $error_msg", $error_type);
This page took 0.435631 seconds and 4 git commands to generate.