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