]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/Config_File.class.php
Release 6.1.4
[Github/sugarcrm.git] / include / Smarty / Config_File.class.php
1 <?php
2
3 /*
4
5 Modification information for LGPL compliance
6
7 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
8
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
10
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
12
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
14
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
16
17 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
18
19 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
20
21 r10971 - 2006-01-12 14:58:30 -0800 (Thu, 12 Jan 2006) - chris - Bug 4128: updating Smarty templates to 2.6.11, a version supposedly that plays better with PHP 5.1
22
23 r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
24
25
26 */
27
28
29
30 /**
31  * Config_File class.
32  *
33  * This library is free software; you can redistribute it and/or
34  * modify it under the terms of the GNU Lesser General Public
35  * License as published by the Free Software Foundation; either
36  * version 2.1 of the License, or (at your option) any later version.
37  *
38  * This library is distributed in the hope that it will be useful,
39  * but WITHOUT ANY WARRANTY; without even the implied warranty of
40  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
41  * Lesser General Public License for more details.
42  *
43  * You should have received a copy of the GNU Lesser General Public
44  * License along with this library; if not, write to the Free Software
45  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
46  *
47  * @link http://smarty.php.net/
48  * @version 2.6.11
49  * @copyright Copyright: 2001-2005 New Digital Group, Inc.
50  * @author Andrei Zmievski <andrei@php.net>
51  * @access public
52  * @package Smarty
53  */
54
55
56
57 /**
58  * Config file reading class
59  * @package Smarty
60  */
61 class Config_File {
62     /**#@+
63      * Options
64      * @var boolean
65      */
66     /**
67      * Controls whether variables with the same name overwrite each other.
68      */
69     var $overwrite        =    true;
70
71     /**
72      * Controls whether config values of on/true/yes and off/false/no get
73      * converted to boolean values automatically.
74      */
75     var $booleanize        =    true;
76
77     /**
78      * Controls whether hidden config sections/vars are read from the file.
79      */
80     var $read_hidden     =    true;
81
82     /**
83      * Controls whether or not to fix mac or dos formatted newlines.
84      * If set to true, \r or \r\n will be changed to \n.
85      */
86     var $fix_newlines =    true;
87     /**#@-*/
88
89     /** @access private */
90     var $_config_path    = "";
91     var $_config_data    = array();
92     /**#@-*/
93
94     /**
95      * Constructs a new config file class.
96      *
97      * @param string $config_path (optional) path to the config files
98      */
99     function Config_File($config_path = NULL)
100     {
101         if (isset($config_path))
102             $this->set_path($config_path);
103     }
104
105
106     /**
107      * Set the path where configuration files can be found.
108      *
109      * @param string $config_path path to the config files
110      */
111     function set_path($config_path)
112     {
113         if (!empty($config_path)) {
114             if (!is_string($config_path) || !file_exists($config_path) || !is_dir($config_path)) {
115                 $this->_trigger_error_msg("Bad config file path '$config_path'");
116                 return;
117             }
118             if(substr($config_path, -1) != DIRECTORY_SEPARATOR) {
119                 $config_path .= DIRECTORY_SEPARATOR;
120             }
121
122             $this->_config_path = $config_path;
123         }
124     }
125
126
127     /**
128      * Retrieves config info based on the file, section, and variable name.
129      *
130      * @param string $file_name config file to get info for
131      * @param string $section_name (optional) section to get info for
132      * @param string $var_name (optional) variable to get info for
133      * @return string|array a value or array of values
134      */
135     function get($file_name, $section_name = NULL, $var_name = NULL)
136     {
137         if (empty($file_name)) {
138             $this->_trigger_error_msg('Empty config file name');
139             return;
140         } else {
141             $file_name = $this->_config_path . $file_name;
142             if (!isset($this->_config_data[$file_name]))
143                 $this->load_file($file_name, false);
144         }
145
146         if (!empty($var_name)) {
147             if (empty($section_name)) {
148                 return $this->_config_data[$file_name]["vars"][$var_name];
149             } else {
150                 if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name]))
151                     return $this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name];
152                 else
153                     return array();
154             }
155         } else {
156             if (empty($section_name)) {
157                 return (array)$this->_config_data[$file_name]["vars"];
158             } else {
159                 if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"]))
160                     return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"];
161                 else
162                     return array();
163             }
164         }
165     }
166
167
168     /**
169      * Retrieves config info based on the key.
170      *
171      * @param $file_name string config key (filename/section/var)
172      * @return string|array same as get()
173      * @uses get() retrieves information from config file and returns it
174      */
175     function &get_key($config_key)
176     {
177         list($file_name, $section_name, $var_name) = explode('/', $config_key, 3);
178         $result = &$this->get($file_name, $section_name, $var_name);
179         return $result;
180     }
181
182     /**
183      * Get all loaded config file names.
184      *
185      * @return array an array of loaded config file names
186      */
187     function get_file_names()
188     {
189         return array_keys($this->_config_data);
190     }
191
192
193     /**
194      * Get all section names from a loaded file.
195      *
196      * @param string $file_name config file to get section names from
197      * @return array an array of section names from the specified file
198      */
199     function get_section_names($file_name)
200     {
201         $file_name = $this->_config_path . $file_name;
202         if (!isset($this->_config_data[$file_name])) {
203             $this->_trigger_error_msg("Unknown config file '$file_name'");
204             return;
205         }
206
207         return array_keys($this->_config_data[$file_name]["sections"]);
208     }
209
210
211     /**
212      * Get all global or section variable names.
213      *
214      * @param string $file_name config file to get info for
215      * @param string $section_name (optional) section to get info for
216      * @return array an array of variables names from the specified file/section
217      */
218     function get_var_names($file_name, $section = NULL)
219     {
220         if (empty($file_name)) {
221             $this->_trigger_error_msg('Empty config file name');
222             return;
223         } else if (!isset($this->_config_data[$file_name])) {
224             $this->_trigger_error_msg("Unknown config file '$file_name'");
225             return;
226         }
227
228         if (empty($section))
229             return array_keys($this->_config_data[$file_name]["vars"]);
230         else
231             return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]);
232     }
233
234
235     /**
236      * Clear loaded config data for a certain file or all files.
237      *
238      * @param string $file_name file to clear config data for
239      */
240     function clear($file_name = NULL)
241     {
242         if ($file_name === NULL)
243             $this->_config_data = array();
244         else if (isset($this->_config_data[$file_name]))
245             $this->_config_data[$file_name] = array();
246     }
247
248
249     /**
250      * Load a configuration file manually.
251      *
252      * @param string $file_name file name to load
253      * @param boolean $prepend_path whether current config path should be
254      *                              prepended to the filename
255      */
256     function load_file($file_name, $prepend_path = true)
257     {
258         if ($prepend_path && $this->_config_path != "")
259             $config_file = $this->_config_path . $file_name;
260         else
261             $config_file = $file_name;
262
263         ini_set('track_errors', true);
264         $fp = @fopen($config_file, "r");
265         if (!is_resource($fp)) {
266             $this->_trigger_error_msg("Could not open config file '$config_file'");
267             return false;
268         }
269
270         $contents = ($size = filesize($config_file)) ? fread($fp, $size) : '';
271         fclose($fp);
272
273         $this->_config_data[$config_file] = $this->parse_contents($contents);
274         return true;
275     }
276
277     /**
278      * Store the contents of a file manually.
279      *
280      * @param string $config_file file name of the related contents
281      * @param string $contents the file-contents to parse
282      */
283     function set_file_contents($config_file, $contents)
284     {
285         $this->_config_data[$config_file] = $this->parse_contents($contents);
286         return true;
287     }
288
289     /**
290      * parse the source of a configuration file manually.
291      *
292      * @param string $contents the file-contents to parse
293      */
294     function parse_contents($contents)
295     {
296         if($this->fix_newlines) {
297             // fix mac/dos formatted newlines
298             $contents = preg_replace('!\r\n?!', "\n", $contents);
299         }
300
301         $config_data = array();
302         $config_data['sections'] = array();
303         $config_data['vars'] = array();
304
305         /* reference to fill with data */
306         $vars =& $config_data['vars'];
307
308         /* parse file line by line */
309         preg_match_all('!^.*\r?\n?!m', $contents, $match);
310         $lines = $match[0];
311         for ($i=0, $count=count($lines); $i<$count; $i++) {
312             $line = $lines[$i];
313             if (empty($line)) continue;
314
315             if ( substr($line, 0, 1) == '[' && preg_match('!^\[(.*?)\]!', $line, $match) ) {
316                 /* section found */
317                 if (substr($match[1], 0, 1) == '.') {
318                     /* hidden section */
319                     if ($this->read_hidden) {
320                         $section_name = substr($match[1], 1);
321                     } else {
322                         /* break reference to $vars to ignore hidden section */
323                         unset($vars);
324                         $vars = array();
325                         continue;
326                     }
327                 } else {                    
328                     $section_name = $match[1];
329                 }
330                 if (!isset($config_data['sections'][$section_name]))
331                     $config_data['sections'][$section_name] = array('vars' => array());
332                 $vars =& $config_data['sections'][$section_name]['vars'];
333                 continue;
334             }
335
336             if (preg_match('/^\s*(\.?\w+)\s*=\s*(.*)/s', $line, $match)) {
337                 /* variable found */
338                 $var_name = rtrim($match[1]);
339                 if (strpos($match[2], '"""') === 0) {
340                     /* handle multiline-value */
341                     $lines[$i] = substr($match[2], 3);
342                     $var_value = '';
343                     while ($i<$count) {
344                         if (($pos = strpos($lines[$i], '"""')) === false) {
345                             $var_value .= $lines[$i++];
346                         } else {
347                             /* end of multiline-value */
348                             $var_value .= substr($lines[$i], 0, $pos);
349                             break;
350                         }
351                     }
352                     $booleanize = false;
353
354                 } else {
355                     /* handle simple value */
356                     $var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', rtrim($match[2]));
357                     $booleanize = $this->booleanize;
358
359                 }
360                 $this->_set_config_var($vars, $var_name, $var_value, $booleanize);
361             }
362             /* else unparsable line / means it is a comment / means ignore it */
363         }
364         return $config_data;
365     }
366
367     /**#@+ @access private */
368     /**
369      * @param array &$container
370      * @param string $var_name
371      * @param mixed $var_value
372      * @param boolean $booleanize determines whether $var_value is converted to
373      *                            to true/false
374      */
375     function _set_config_var(&$container, $var_name, $var_value, $booleanize)
376     {
377         if (substr($var_name, 0, 1) == '.') {
378             if (!$this->read_hidden)
379                 return;
380             else
381                 $var_name = substr($var_name, 1);
382         }
383
384         if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {
385             $this->_trigger_error_msg("Bad variable name '$var_name'");
386             return;
387         }
388
389         if ($booleanize) {
390             if (preg_match("/^(on|true|yes)$/i", $var_value))
391                 $var_value = true;
392             else if (preg_match("/^(off|false|no)$/i", $var_value))
393                 $var_value = false;
394         }
395
396         if (!isset($container[$var_name]) || $this->overwrite)
397             $container[$var_name] = $var_value;
398         else {
399             settype($container[$var_name], 'array');
400             $container[$var_name][] = $var_value;
401         }
402     }
403
404     /**
405      * @uses trigger_error() creates a PHP warning/error
406      * @param string $error_msg
407      * @param integer $error_type one of
408      */
409     function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
410     {
411         trigger_error("Config_File error: $error_msg", $error_type);
412     }
413     /**#@-*/
414 }
415
416 ?>