5 Modification information for LGPL compliance
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
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
17 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
19 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
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
23 r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
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.
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.
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
47 * @link http://smarty.php.net/
49 * @copyright Copyright: 2001-2005 New Digital Group, Inc.
50 * @author Andrei Zmievski <andrei@php.net>
58 * Config file reading class
67 * Controls whether variables with the same name overwrite each other.
69 var $overwrite = true;
72 * Controls whether config values of on/true/yes and off/false/no get
73 * converted to boolean values automatically.
75 var $booleanize = true;
78 * Controls whether hidden config sections/vars are read from the file.
80 var $read_hidden = true;
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.
86 var $fix_newlines = true;
89 /** @access private */
90 var $_config_path = "";
91 var $_config_data = array();
95 * Constructs a new config file class.
97 * @param string $config_path (optional) path to the config files
99 function Config_File($config_path = NULL)
101 if (isset($config_path))
102 $this->set_path($config_path);
107 * Set the path where configuration files can be found.
109 * @param string $config_path path to the config files
111 function set_path($config_path)
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'");
118 if(substr($config_path, -1) != DIRECTORY_SEPARATOR) {
119 $config_path .= DIRECTORY_SEPARATOR;
122 $this->_config_path = $config_path;
128 * Retrieves config info based on the file, section, and variable name.
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
135 function get($file_name, $section_name = NULL, $var_name = NULL)
137 if (empty($file_name)) {
138 $this->_trigger_error_msg('Empty config file name');
141 $file_name = $this->_config_path . $file_name;
142 if (!isset($this->_config_data[$file_name]))
143 $this->load_file($file_name, false);
146 if (!empty($var_name)) {
147 if (empty($section_name)) {
148 return $this->_config_data[$file_name]["vars"][$var_name];
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];
156 if (empty($section_name)) {
157 return (array)$this->_config_data[$file_name]["vars"];
159 if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"]))
160 return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"];
169 * Retrieves config info based on the key.
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
175 function &get_key($config_key)
177 list($file_name, $section_name, $var_name) = explode('/', $config_key, 3);
178 $result = &$this->get($file_name, $section_name, $var_name);
183 * Get all loaded config file names.
185 * @return array an array of loaded config file names
187 function get_file_names()
189 return array_keys($this->_config_data);
194 * Get all section names from a loaded file.
196 * @param string $file_name config file to get section names from
197 * @return array an array of section names from the specified file
199 function get_section_names($file_name)
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'");
207 return array_keys($this->_config_data[$file_name]["sections"]);
212 * Get all global or section variable names.
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
218 function get_var_names($file_name, $section = NULL)
220 if (empty($file_name)) {
221 $this->_trigger_error_msg('Empty config file name');
223 } else if (!isset($this->_config_data[$file_name])) {
224 $this->_trigger_error_msg("Unknown config file '$file_name'");
229 return array_keys($this->_config_data[$file_name]["vars"]);
231 return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]);
236 * Clear loaded config data for a certain file or all files.
238 * @param string $file_name file to clear config data for
240 function clear($file_name = NULL)
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();
250 * Load a configuration file manually.
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
256 function load_file($file_name, $prepend_path = true)
258 if ($prepend_path && $this->_config_path != "")
259 $config_file = $this->_config_path . $file_name;
261 $config_file = $file_name;
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'");
270 $this->_config_data[$config_file] = $this->parse_contents($contents);
275 * Store the contents of a file manually.
277 * @param string $config_file file name of the related contents
278 * @param string $contents the file-contents to parse
280 function set_file_contents($config_file, $contents)
282 $this->_config_data[$config_file] = $this->parse_contents($contents);
287 * parse the source of a configuration file manually.
289 * @param string $contents the file-contents to parse
291 function parse_contents($contents)
293 if($this->fix_newlines) {
294 // fix mac/dos formatted newlines
295 $contents = preg_replace('!\r\n?!', "\n", $contents);
298 $config_data = array();
299 $config_data['sections'] = array();
300 $config_data['vars'] = array();
302 /* reference to fill with data */
303 $vars =& $config_data['vars'];
305 /* parse file line by line */
306 preg_match_all('!^.*\r?\n?!m', $contents, $match);
308 for ($i=0, $count=count($lines); $i<$count; $i++) {
310 if (empty($line)) continue;
312 if ( substr($line, 0, 1) == '[' && preg_match('!^\[(.*?)\]!', $line, $match) ) {
314 if (substr($match[1], 0, 1) == '.') {
316 if ($this->read_hidden) {
317 $section_name = substr($match[1], 1);
319 /* break reference to $vars to ignore hidden section */
325 $section_name = $match[1];
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'];
333 if (preg_match('/^\s*(\.?\w+)\s*=\s*(.*)/s', $line, $match)) {
335 $var_name = rtrim($match[1]);
336 if (strpos($match[2], '"""') === 0) {
337 /* handle multiline-value */
338 $lines[$i] = substr($match[2], 3);
341 if (($pos = strpos($lines[$i], '"""')) === false) {
342 $var_value .= $lines[$i++];
344 /* end of multiline-value */
345 $var_value .= substr($lines[$i], 0, $pos);
352 /* handle simple value */
353 $var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', rtrim($match[2]));
354 $booleanize = $this->booleanize;
357 $this->_set_config_var($vars, $var_name, $var_value, $booleanize);
359 /* else unparsable line / means it is a comment / means ignore it */
364 /**#@+ @access private */
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
372 function _set_config_var(&$container, $var_name, $var_value, $booleanize)
374 if (substr($var_name, 0, 1) == '.') {
375 if (!$this->read_hidden)
378 $var_name = substr($var_name, 1);
381 if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {
382 $this->_trigger_error_msg("Bad variable name '$var_name'");
387 if (preg_match("/^(on|true|yes)$/i", $var_value))
389 else if (preg_match("/^(off|false|no)$/i", $var_value))
393 if (!isset($container[$var_name]) || $this->overwrite)
394 $container[$var_name] = $var_value;
396 settype($container[$var_name], 'array');
397 $container[$var_name][] = $var_value;
402 * @uses trigger_error() creates a PHP warning/error
403 * @param string $error_msg
404 * @param integer $error_type one of
406 function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
408 trigger_error("Config_File error: $error_msg", $error_type);