/[hr-web]/inc/Config_File.class.php
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /inc/Config_File.class.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Tue May 1 16:43:23 2001 UTC (22 years, 11 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
zadnje promjene

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

  ViewVC Help
Powered by ViewVC 1.1.26