Kyberia v2.0
[mirrors/Kyberia-bloodline.git] / inc / smarty / Config_File.class.php
1 <?php
2
3 /**
4 * Config_File class.
5 *
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.
10 *
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.
15 *
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
19 *
20 * You may contact the author of Config_File by e-mail at:
21 * {@link andrei@php.net}
22 *
23 * The latest version of Config_File can be obtained from:
24 * http://smarty.php.net/
25 *
26 * @link http://smarty.php.net/
27 * @version 2.6.0-RC2
28 * @copyright Copyright: 2001-2003 ispi of Lincoln, Inc.
29 * @author Andrei Zmievski <andrei@php.net>
30 * @access public
31 * @package Smarty
32 */
33
34 /* $Id: Config_File.class.php,v 1.53 2003/10/08 23:43:34 mohrt Exp $ */
35 /**
36 * Config file reading class
37 * @package Smarty
38 */
39 class Config_File {
40 /**#@+
41 * Options
42 * @var boolean
43 */
44 /**
45 * Controls whether variables with the same name overwrite each other.
46 */
47 var $overwrite = true;
48
49 /**
50 * Controls whether config values of on/true/yes and off/false/no get
51 * converted to boolean values automatically.
52 */
53 var $booleanize = true;
54
55 /**
56 * Controls whether hidden config sections/vars are read from the file.
57 */
58 var $read_hidden = true;
59
60 /**
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.
63 */
64 var $fix_newlines = true;
65 /**#@-*/
66
67 /** @access private */
68 var $_config_path = "";
69 var $_config_data = array();
70 /**#@-*/
71
72 /**
73 * Constructs a new config file class.
74 *
75 * @param string $config_path (optional) path to the config files
76 */
77 function Config_File($config_path = NULL)
78 {
79 if (isset($config_path))
80 $this->set_path($config_path);
81 }
82
83
84 /**
85 * Set the path where configuration files can be found.
86 *
87 * @param string $config_path path to the config files
88 */
89 function set_path($config_path)
90 {
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'");
94 return;
95 }
96 if(substr($config_path, -1) != DIRECTORY_SEPARATOR) {
97 $config_path .= DIRECTORY_SEPARATOR;
98 }
99
100 $this->_config_path = $config_path;
101 }
102 }
103
104
105 /**
106 * Retrieves config info based on the file, section, and variable name.
107 *
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
112 */
113 function &get($file_name, $section_name = NULL, $var_name = NULL)
114 {
115 if (empty($file_name)) {
116 $this->_trigger_error_msg('Empty config file name');
117 return;
118 } else {
119 $file_name = $this->_config_path . $file_name;
120 if (!isset($this->_config_data[$file_name]))
121 $this->load_file($file_name, false);
122 }
123
124 if (!empty($var_name)) {
125 if (empty($section_name)) {
126 return $this->_config_data[$file_name]["vars"][$var_name];
127 } else {
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];
130 else
131 return array();
132 }
133 } else {
134 if (empty($section_name)) {
135 return (array)$this->_config_data[$file_name]["vars"];
136 } else {
137 if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"]))
138 return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"];
139 else
140 return array();
141 }
142 }
143 }
144
145
146 /**
147 * Retrieves config info based on the key.
148 *
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
152 */
153 function &get_key($config_key)
154 {
155 list($file_name, $section_name, $var_name) = explode('/', $config_key, 3);
156 $result = &$this->get($file_name, $section_name, $var_name);
157 return $result;
158 }
159
160 /**
161 * Get all loaded config file names.
162 *
163 * @return array an array of loaded config file names
164 */
165 function get_file_names()
166 {
167 return array_keys($this->_config_data);
168 }
169
170
171 /**
172 * Get all section names from a loaded file.
173 *
174 * @param string $file_name config file to get section names from
175 * @return array an array of section names from the specified file
176 */
177 function get_section_names($file_name)
178 {
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'");
182 return;
183 }
184
185 return array_keys($this->_config_data[$file_name]["sections"]);
186 }
187
188
189 /**
190 * Get all global or section variable names.
191 *
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
195 */
196 function get_var_names($file_name, $section = NULL)
197 {
198 if (empty($file_name)) {
199 $this->_trigger_error_msg('Empty config file name');
200 return;
201 } else if (!isset($this->_config_data[$file_name])) {
202 $this->_trigger_error_msg("Unknown config file '$file_name'");
203 return;
204 }
205
206 if (empty($section))
207 return array_keys($this->_config_data[$file_name]["vars"]);
208 else
209 return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]);
210 }
211
212
213 /**
214 * Clear loaded config data for a certain file or all files.
215 *
216 * @param string $file_name file to clear config data for
217 */
218 function clear($file_name = NULL)
219 {
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();
224 }
225
226
227 /**
228 * Load a configuration file manually.
229 *
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
233 */
234 function load_file($file_name, $prepend_path = true)
235 {
236 if ($prepend_path && $this->_config_path != "")
237 $config_file = $this->_config_path . $file_name;
238 else
239 $config_file = $file_name;
240
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'");
245 return false;
246 }
247
248 $contents = fread($fp, filesize($config_file));
249 fclose($fp);
250
251 if($this->fix_newlines) {
252 // fix mac/dos formatted newlines
253 $contents = preg_replace('!\r\n?!',"\n",$contents);
254 }
255
256 $config_data = array();
257
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]);
261
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)
267 continue;
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]);
272 }
273
274 $this->_config_data[$config_file] = $config_data;
275
276 return true;
277 }
278
279 /**#@+ @access private */
280 /**
281 * @var string $config_block
282 */
283 function _parse_config_block($config_block)
284 {
285 $vars = array();
286
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);
291 }
292 $config_block = preg_replace("/^[^=\n]+=\s*\"{3}.*?\"{3}\s*$/ms", "", $config_block);
293 }
294
295
296 $config_lines = preg_split("/\n+/", $config_block);
297
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);
302 }
303 }
304
305 return $vars;
306 }
307
308 /**
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
313 * to true/false
314 */
315 function _set_config_var(&$container, $var_name, $var_value, $booleanize)
316 {
317 if ($var_name{0} == '.') {
318 if (!$this->read_hidden)
319 return;
320 else
321 $var_name = substr($var_name, 1);
322 }
323
324 if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {
325 $this->_trigger_error_msg("Bad variable name '$var_name'");
326 return;
327 }
328
329 if ($booleanize) {
330 if (preg_match("/^(on|true|yes)$/i", $var_value))
331 $var_value = true;
332 else if (preg_match("/^(off|false|no)$/i", $var_value))
333 $var_value = false;
334 }
335
336 if (!isset($container[$var_name]) || $this->overwrite)
337 $container[$var_name] = $var_value;
338 else {
339 settype($container[$var_name], 'array');
340 $container[$var_name][] = $var_value;
341 }
342 }
343
344 /**
345 * @uses trigger_error() creates a PHP warning/error
346 * @param string $error_msg
347 * @param integer $error_type one of
348 */
349 function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
350 {
351 trigger_error("Config_File error: $error_msg", $error_type);
352 }
353 /**#@-*/
354 }
355
356 ?>
This page took 0.496093 seconds and 4 git commands to generate.