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