Source for file Config_File.class.php
Documentation is available at Config_File.class.php
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* @link http://smarty.php.net/
* @copyright Copyright: 2001-2005 New Digital Group, Inc.
* @author Andrei Zmievski <andrei@php.net>
/* $Id: Config_File.class.php,v 1.2 2007/03/13 17:53:15 mmerlone Exp $ */
* Config file reading class
* Controls whether variables with the same name overwrite each other.
* Controls whether config values of on/true/yes and off/false/no get
* converted to boolean values automatically.
* Controls whether hidden config sections/vars are read from the file.
* Controls whether or not to fix mac or dos formatted newlines.
* If set to true, \r or \r\n will be changed to \n.
* Constructs a new config file class.
* @param string $config_path (optional) path to the config files
* Set the path where configuration files can be found.
* @param string $config_path path to the config files
if (!empty($config_path)) {
$this->_trigger_error_msg("Bad config file path '$config_path'");
if(substr($config_path, -
1) !=
DIRECTORY_SEPARATOR) {
$config_path .=
DIRECTORY_SEPARATOR;
$this->_config_path =
$config_path;
* Retrieves config info based on the file, section, and variable name.
* @param string $file_name config file to get info for
* @param string $section_name (optional) section to get info for
* @param string $var_name (optional) variable to get info for
* @return string|arraya value or array of values
function get($file_name, $section_name =
NULL, $var_name =
NULL)
$this->_trigger_error_msg('Empty config file name');
$file_name =
$this->_config_path .
$file_name;
if (empty($section_name)) {
if(isset
($this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name]))
return $this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name];
if (empty($section_name)) {
if(isset
($this->_config_data[$file_name]["sections"][$section_name]["vars"]))
return (array)
$this->_config_data[$file_name]["sections"][$section_name]["vars"];
* Retrieves config info based on the key.
* @param $file_name string config key (filename/section/var)
* @return string|arraysame as get()
* @uses get() retrieves information from config file and returns it
list
($file_name, $section_name, $var_name) =
explode('/', $config_key, 3);
$result =
&$this->get($file_name, $section_name, $var_name);
* Get all loaded config file names.
* @return array an array of loaded config file names
* Get all section names from a loaded file.
* @param string $file_name config file to get section names from
* @return array an array of section names from the specified file
$file_name =
$this->_config_path .
$file_name;
$this->_trigger_error_msg("Unknown config file '$file_name'");
* Get all global or section variable names.
* @param string $file_name config file to get info for
* @param string $section_name (optional) section to get info for
* @return array an array of variables names from the specified file/section
$this->_trigger_error_msg('Empty config file name');
$this->_trigger_error_msg("Unknown config file '$file_name'");
* Clear loaded config data for a certain file or all files.
* @param string $file_name file to clear config data for
function clear($file_name =
NULL)
* Load a configuration file manually.
* @param string $file_name file name to load
* @param boolean $prepend_path whether current config path should be
* prepended to the filename
function load_file($file_name, $prepend_path =
true)
if ($prepend_path &&
$this->_config_path !=
"")
$config_file =
$this->_config_path .
$file_name;
$config_file =
$file_name;
$fp =
@fopen($config_file, "r");
$this->_trigger_error_msg("Could not open config file '$config_file'");
$contents =
($size =
filesize($config_file)) ?
fread($fp, $size) :
'';
* Store the contents of a file manually.
* @param string $config_file file name of the related contents
* @param string $contents the file-contents to parse
* parse the source of a configuration file manually.
* @param string $contents the file-contents to parse
// fix mac/dos formatted newlines
$config_data['sections'] =
array();
$config_data['vars'] =
array();
/* reference to fill with data */
$vars =
& $config_data['vars'];
/* parse file line by line */
for ($i=
0, $count=
count($lines); $i<
$count; $i++
) {
if (empty($line)) continue;
if ( substr($line, 0, 1) ==
'[' &&
preg_match('!^\[(.*?)\]!', $line, $match) ) {
if (substr($match[1], 0, 1) ==
'.') {
$section_name =
substr($match[1], 1);
/* break reference to $vars to ignore hidden section */
$section_name =
$match[1];
if (!isset
($config_data['sections'][$section_name]))
$config_data['sections'][$section_name] =
array('vars' =>
array());
$vars =
& $config_data['sections'][$section_name]['vars'];
if (preg_match('/^\s*(\.?\w+)\s*=\s*(.*)/s', $line, $match)) {
$var_name =
rtrim($match[1]);
if (strpos($match[2], '"""') ===
0) {
/* handle multiline-value */
$lines[$i] =
substr($match[2], 3);
if (($pos =
strpos($lines[$i], '"""')) ===
false) {
$var_value .=
$lines[$i++
];
/* end of multiline-value */
$var_value .=
substr($lines[$i], 0, $pos);
/* handle simple value */
$this->_set_config_var($vars, $var_name, $var_value, $booleanize);
/* else unparsable line / means it is a comment / means ignore it */
/**#@+ @access private */
* @param array &$container
* @param string $var_name
* @param mixed $var_value
* @param boolean $booleanize determines whether $var_value is converted to
function _set_config_var(&$container, $var_name, $var_value, $booleanize)
if (substr($var_name, 0, 1) ==
'.') {
$var_name =
substr($var_name, 1);
$this->_trigger_error_msg("Bad variable name '$var_name'");
else if (preg_match("/^(off|false|no)$/i", $var_value))
if (!isset
($container[$var_name]) ||
$this->overwrite)
$container[$var_name] =
$var_value;
settype($container[$var_name], 'array');
$container[$var_name][] =
$var_value;
* @uses trigger_error() creates a PHP warning/error
* @param string $error_msg
* @param integer $error_type one of
function _trigger_error_msg($error_msg, $error_type =
E_USER_WARNING)
Documentation generated on Sun, 09 Mar 2008 23:51:02 -0300 by phpDocumentor 1.4.0