/[cvs]/nfo/php/libs/net.php.smarty/Config_File.class.php
ViewVC logotype

Annotation of /nfo/php/libs/net.php.smarty/Config_File.class.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Thu Dec 19 16:40:20 2002 UTC (21 years, 6 months ago) by joko
Branch: MAIN
Changes since 1.1: +14 -1 lines
+ updated to version 2.3.1

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed