]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/Config_File.class.php
Release 6.5.0
[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         $contents = @sugar_file_get_contents($config_file);
265         if ($contents === false) {
266             $this->_trigger_error_msg("Could not open config file '$config_file'");
267             return false;
268         }
269
270         $this->_config_data[$config_file] = $this->parse_contents($contents);
271         return true;
272     }
273
274     /**
275      * Store the contents of a file manually.
276      *
277      * @param string $config_file file name of the related contents
278      * @param string $contents the file-contents to parse
279      */
280     function set_file_contents($config_file, $contents)
281     {
282         $this->_config_data[$config_file] = $this->parse_contents($contents);
283         return true;
284     }
285
286     /**
287      * parse the source of a configuration file manually.
288      *
289      * @param string $contents the file-contents to parse
290      */
291     function parse_contents($contents)
292     {
293         if($this->fix_newlines) {
294             // fix mac/dos formatted newlines
295             $contents = preg_replace('!\r\n?!', "\n", $contents);
296         }
297
298         $config_data = array();
299         $config_data['sections'] = array();
300         $config_data['vars'] = array();
301
302         /* reference to fill with data */
303         $vars =& $config_data['vars'];
304
305         /* parse file line by line */
306         preg_match_all('!^.*\r?\n?!m', $contents, $match);
307         $lines = $match[0];
308         for ($i=0, $count=count($lines); $i<$count; $i++) {
309             $line = $lines[$i];
310             if (empty($line)) continue;
311
312             if ( substr($line, 0, 1) == '[' && preg_match('!^\[(.*?)\]!', $line, $match) ) {
313                 /* section found */
314                 if (substr($match[1], 0, 1) == '.') {
315                     /* hidden section */
316                     if ($this->read_hidden) {
317                         $section_name = substr($match[1], 1);
318                     } else {
319                         /* break reference to $vars to ignore hidden section */
320                         unset($vars);
321                         $vars = array();
322                         continue;
323                     }
324                 } else {
325                     $section_name = $match[1];
326                 }
327                 if (!isset($config_data['sections'][$section_name]))
328                     $config_data['sections'][$section_name] = array('vars' => array());
329                 $vars =& $config_data['sections'][$section_name]['vars'];
330                 continue;
331             }
332
333             if (preg_match('/^\s*(\.?\w+)\s*=\s*(.*)/s', $line, $match)) {
334                 /* variable found */
335                 $var_name = rtrim($match[1]);
336                 if (strpos($match[2], '"""') === 0) {
337                     /* handle multiline-value */
338                     $lines[$i] = substr($match[2], 3);
339                     $var_value = '';
340                     while ($i<$count) {
341                         if (($pos = strpos($lines[$i], '"""')) === false) {
342                             $var_value .= $lines[$i++];
343                         } else {
344                             /* end of multiline-value */
345                             $var_value .= substr($lines[$i], 0, $pos);
346                             break;
347                         }
348                     }
349                     $booleanize = false;
350
351                 } else {
352                     /* handle simple value */
353                     $var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', rtrim($match[2]));
354                     $booleanize = $this->booleanize;
355
356                 }
357                 $this->_set_config_var($vars, $var_name, $var_value, $booleanize);
358             }
359             /* else unparsable line / means it is a comment / means ignore it */
360         }
361         return $config_data;
362     }
363
364     /**#@+ @access private */
365     /**
366      * @param array &$container
367      * @param string $var_name
368      * @param mixed $var_value
369      * @param boolean $booleanize determines whether $var_value is converted to
370      *                            to true/false
371      */
372     function _set_config_var(&$container, $var_name, $var_value, $booleanize)
373     {
374         if (substr($var_name, 0, 1) == '.') {
375             if (!$this->read_hidden)
376                 return;
377             else
378                 $var_name = substr($var_name, 1);
379         }
380
381         if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {
382             $this->_trigger_error_msg("Bad variable name '$var_name'");
383             return;
384         }
385
386         if ($booleanize) {
387             if (preg_match("/^(on|true|yes)$/i", $var_value))
388                 $var_value = true;
389             else if (preg_match("/^(off|false|no)$/i", $var_value))
390                 $var_value = false;
391         }
392
393         if (!isset($container[$var_name]) || $this->overwrite)
394             $container[$var_name] = $var_value;
395         else {
396             settype($container[$var_name], 'array');
397             $container[$var_name][] = $var_value;
398         }
399     }
400
401     /**
402      * @uses trigger_error() creates a PHP warning/error
403      * @param string $error_msg
404      * @param integer $error_type one of
405      */
406     function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
407     {
408         trigger_error("Config_File error: $error_msg", $error_type);
409     }
410     /**#@-*/
411 }
412
413 ?>