2001-02-19 19:10:58 +00:00
|
|
|
<?php
|
2001-01-24 17:16:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Config_File class.
|
|
|
|
*
|
2001-02-08 14:48:51 +00:00
|
|
|
* 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.
|
2001-01-24 17:16:32 +00:00
|
|
|
*
|
2001-02-08 14:48:51 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2001-01-24 17:16:32 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2001-02-08 14:48:51 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-01-24 17:16:32 +00:00
|
|
|
*
|
2001-02-08 14:48:51 +00:00
|
|
|
* 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
|
2001-01-24 17:16:32 +00:00
|
|
|
*
|
|
|
|
* You may contact the author of Config_File by e-mail at:
|
2003-02-03 16:32:45 +00:00
|
|
|
* {@link andrei@php.net}
|
2001-01-24 17:16:32 +00:00
|
|
|
*
|
|
|
|
* Or, write to:
|
|
|
|
* Andrei Zmievski
|
|
|
|
* Software Engineer, ispi
|
|
|
|
* 237 S. 70th suite 220
|
|
|
|
* Lincoln, NE 68510
|
|
|
|
*
|
|
|
|
* The latest version of Config_File can be obtained from:
|
2003-04-14 08:51:25 +00:00
|
|
|
* http://smarty.php.net/
|
2003-02-03 16:32:45 +00:00
|
|
|
*
|
2003-04-14 08:51:25 +00:00
|
|
|
* @link http://smarty.php.net/
|
2003-04-11 13:40:40 +00:00
|
|
|
* @version 2.5.0
|
2003-02-03 16:32:45 +00:00
|
|
|
* @copyright Copyright: 2001,2002 ispi of Lincoln, Inc.
|
|
|
|
* @author Andrei Zmievski <andrei@php.net>
|
|
|
|
* @access public
|
2003-04-20 21:12:13 +00:00
|
|
|
* @package Smarty
|
2001-01-24 17:16:32 +00:00
|
|
|
*/
|
|
|
|
|
2003-04-14 08:51:25 +00:00
|
|
|
/* $Id$ */
|
2003-04-20 21:12:13 +00:00
|
|
|
/**
|
|
|
|
* Config file reading class
|
|
|
|
* @package Smarty
|
|
|
|
*/
|
2002-02-18 17:42:11 +00:00
|
|
|
class Config_File {
|
2003-02-03 16:32:45 +00:00
|
|
|
/**#@+
|
|
|
|
* Options
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2001-01-24 17:16:32 +00:00
|
|
|
/**
|
|
|
|
* Controls whether variables with the same name overwrite each other.
|
|
|
|
*/
|
|
|
|
var $overwrite = true;
|
2001-10-09 16:29:10 +00:00
|
|
|
|
2001-01-24 17:16:32 +00:00
|
|
|
/**
|
|
|
|
* Controls whether config values of on/true/yes and off/false/no get
|
|
|
|
* converted to boolean values automatically.
|
|
|
|
*/
|
|
|
|
var $booleanize = true;
|
|
|
|
|
2001-10-09 16:29:10 +00:00
|
|
|
/**
|
|
|
|
* Controls whether hidden config sections/vars are read from the file.
|
|
|
|
*/
|
|
|
|
var $read_hidden = true;
|
|
|
|
|
2002-09-23 20:14:46 +00:00
|
|
|
/**
|
|
|
|
* Controls whether or not to fix mac or dos formatted newlines.
|
|
|
|
* If set to true, \r or \r\n will be changed to \n.
|
|
|
|
*/
|
2003-02-03 16:32:45 +00:00
|
|
|
var $fix_newlines = true;
|
|
|
|
/**#@-*/
|
2002-09-23 20:14:46 +00:00
|
|
|
|
2003-02-03 16:32:45 +00:00
|
|
|
/** @access private */
|
2001-01-24 17:16:32 +00:00
|
|
|
var $_config_path = "";
|
|
|
|
var $_config_data = array();
|
|
|
|
var $_separator = "";
|
2003-02-03 16:32:45 +00:00
|
|
|
/**#@-*/
|
2001-01-24 17:16:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new config file class.
|
|
|
|
*
|
2003-02-03 16:32:45 +00:00
|
|
|
* @param string $config_path (optional) path to the config files
|
2001-01-24 17:16:32 +00:00
|
|
|
*/
|
|
|
|
function Config_File($config_path = NULL)
|
|
|
|
{
|
2002-04-03 14:45:03 +00:00
|
|
|
if (substr(PHP_OS, 0, 3) == "WIN" || substr(PHP_OS, 0, 4) == "OS/2")
|
2001-01-24 17:16:32 +00:00
|
|
|
$this->_separator = "\\";
|
|
|
|
else
|
|
|
|
$this->_separator = "/";
|
|
|
|
|
|
|
|
if (isset($config_path))
|
|
|
|
$this->set_path($config_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the path where configuration files can be found.
|
|
|
|
*
|
2003-02-03 16:32:45 +00:00
|
|
|
* @param string $config_path path to the config files
|
2001-01-24 17:16:32 +00:00
|
|
|
*/
|
|
|
|
function set_path($config_path)
|
|
|
|
{
|
2001-08-01 16:28:28 +00:00
|
|
|
if (!empty($config_path)) {
|
|
|
|
if (!is_string($config_path) || !file_exists($config_path) || !is_dir($config_path)) {
|
2002-02-18 17:42:11 +00:00
|
|
|
$this->_trigger_error_msg("Bad config file path '$config_path'");
|
|
|
|
return;
|
2001-08-01 16:28:28 +00:00
|
|
|
}
|
2001-01-24 17:16:32 +00:00
|
|
|
|
|
|
|
$this->_config_path = $config_path . $this->_separator;
|
2001-08-01 16:28:28 +00:00
|
|
|
}
|
2001-01-24 17:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves config info based on the file, section, and variable name.
|
|
|
|
*
|
2003-02-03 16:32:45 +00:00
|
|
|
* @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|array a value or array of values
|
2001-01-24 17:16:32 +00:00
|
|
|
*/
|
|
|
|
function &get($file_name, $section_name = NULL, $var_name = NULL)
|
|
|
|
{
|
2002-02-18 17:42:11 +00:00
|
|
|
if (empty($file_name)) {
|
|
|
|
$this->_trigger_error_msg('Empty config file name');
|
|
|
|
return;
|
|
|
|
} else {
|
2001-05-31 14:29:49 +00:00
|
|
|
$file_name = $this->_config_path . $file_name;
|
2001-01-24 17:16:32 +00:00
|
|
|
if (!isset($this->_config_data[$file_name]))
|
|
|
|
$this->load_file($file_name, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($var_name)) {
|
2002-07-23 13:39:59 +00:00
|
|
|
if (empty($section_name)) {
|
2001-01-24 17:16:32 +00:00
|
|
|
return $this->_config_data[$file_name]["vars"][$var_name];
|
2002-07-23 13:39:59 +00:00
|
|
|
} else {
|
|
|
|
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];
|
|
|
|
else
|
|
|
|
return array();
|
2001-01-24 17:16:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2002-07-23 13:39:59 +00:00
|
|
|
if (empty($section_name)) {
|
2001-04-26 19:52:27 +00:00
|
|
|
return (array)$this->_config_data[$file_name]["vars"];
|
2002-07-23 13:39:59 +00:00
|
|
|
} else {
|
|
|
|
if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"]))
|
|
|
|
return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"];
|
|
|
|
else
|
|
|
|
return array();
|
|
|
|
}
|
2001-01-24 17:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves config info based on the key.
|
|
|
|
*
|
|
|
|
* @param $file_name string config key (filename/section/var)
|
2003-02-03 16:32:45 +00:00
|
|
|
* @return string|array same as get()
|
|
|
|
* @uses get() retrieves information from config file and returns it
|
2001-01-24 17:16:32 +00:00
|
|
|
*/
|
|
|
|
function &get_key($config_key)
|
|
|
|
{
|
|
|
|
list($file_name, $section_name, $var_name) = explode('/', $config_key, 3);
|
|
|
|
$result = &$this->get($file_name, $section_name, $var_name);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all loaded config file names.
|
|
|
|
*
|
|
|
|
* @return array an array of loaded config file names
|
|
|
|
*/
|
|
|
|
function get_file_names()
|
|
|
|
{
|
|
|
|
return array_keys($this->_config_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all section names from a loaded file.
|
|
|
|
*
|
2003-02-03 16:32:45 +00:00
|
|
|
* @param string $file_name config file to get section names from
|
2001-01-24 17:16:32 +00:00
|
|
|
* @return array an array of section names from the specified file
|
|
|
|
*/
|
|
|
|
function get_section_names($file_name)
|
|
|
|
{
|
2001-05-31 14:29:49 +00:00
|
|
|
$file_name = $this->_config_path . $file_name;
|
2002-02-18 17:42:11 +00:00
|
|
|
if (!isset($this->_config_data[$file_name])) {
|
|
|
|
$this->_trigger_error_msg("Unknown config file '$file_name'");
|
|
|
|
return;
|
|
|
|
}
|
2001-01-24 17:16:32 +00:00
|
|
|
|
|
|
|
return array_keys($this->_config_data[$file_name]["sections"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all global or section variable names.
|
|
|
|
*
|
2003-02-03 16:32:45 +00:00
|
|
|
* @param string $file_name config file to get info for
|
|
|
|
* @param string $section_name (optional) section to get info for
|
2001-01-24 17:16:32 +00:00
|
|
|
* @return array an array of variables names from the specified file/section
|
|
|
|
*/
|
|
|
|
function get_var_names($file_name, $section = NULL)
|
|
|
|
{
|
2002-02-18 17:42:11 +00:00
|
|
|
if (empty($file_name)) {
|
|
|
|
$this->_trigger_error_msg('Empty config file name');
|
|
|
|
return;
|
|
|
|
} else if (!isset($this->_config_data[$file_name])) {
|
|
|
|
$this->_trigger_error_msg("Unknown config file '$file_name'");
|
|
|
|
return;
|
|
|
|
}
|
2001-01-24 17:16:32 +00:00
|
|
|
|
|
|
|
if (empty($section))
|
|
|
|
return array_keys($this->_config_data[$file_name]["vars"]);
|
|
|
|
else
|
|
|
|
return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear loaded config data for a certain file or all files.
|
|
|
|
*
|
2003-02-03 16:32:45 +00:00
|
|
|
* @param string $file_name file to clear config data for
|
2001-01-24 17:16:32 +00:00
|
|
|
*/
|
|
|
|
function clear($file_name = NULL)
|
|
|
|
{
|
|
|
|
if ($file_name === NULL)
|
|
|
|
$this->_config_data = array();
|
|
|
|
else if (isset($this->_config_data[$file_name]))
|
|
|
|
$this->_config_data[$file_name] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a configuration file manually.
|
|
|
|
*
|
2003-02-03 16:32:45 +00:00
|
|
|
* @param string $file_name file name to load
|
|
|
|
* @param boolean $prepend_path whether current config path should be
|
|
|
|
* prepended to the filename
|
2001-01-24 17:16:32 +00:00
|
|
|
*/
|
|
|
|
function load_file($file_name, $prepend_path = true)
|
|
|
|
{
|
|
|
|
if ($prepend_path && $this->_config_path != "")
|
2001-05-31 14:29:49 +00:00
|
|
|
$config_file = $this->_config_path . $file_name;
|
2001-01-24 17:16:32 +00:00
|
|
|
else
|
|
|
|
$config_file = $file_name;
|
|
|
|
|
|
|
|
ini_set('track_errors', true);
|
|
|
|
$fp = @fopen($config_file, "r");
|
2002-02-18 17:42:11 +00:00
|
|
|
if (!is_resource($fp)) {
|
2002-03-26 17:51:29 +00:00
|
|
|
$this->_trigger_error_msg("Could not open config file '$config_file'");
|
2002-12-09 23:37:35 +00:00
|
|
|
return false;
|
2002-02-18 17:42:11 +00:00
|
|
|
}
|
2001-01-24 17:16:32 +00:00
|
|
|
|
|
|
|
$contents = fread($fp, filesize($config_file));
|
|
|
|
fclose($fp);
|
2002-09-23 20:14:46 +00:00
|
|
|
|
|
|
|
if($this->fix_newlines) {
|
|
|
|
// fix mac/dos formatted newlines
|
|
|
|
$contents = preg_replace('!\r\n?!',"\n",$contents);
|
|
|
|
}
|
2001-01-24 17:16:32 +00:00
|
|
|
|
|
|
|
$config_data = array();
|
|
|
|
|
|
|
|
/* Get global variables first. */
|
|
|
|
if (preg_match("/^(.*?)(\n\[|\Z)/s", $contents, $match))
|
|
|
|
$config_data["vars"] = $this->_parse_config_block($match[1]);
|
|
|
|
|
|
|
|
/* Get section variables. */
|
|
|
|
$config_data["sections"] = array();
|
|
|
|
preg_match_all("/^\[(.*?)\]/m", $contents, $match);
|
|
|
|
foreach ($match[1] as $section) {
|
2001-10-09 16:29:10 +00:00
|
|
|
if ($section{0} == '.' && !$this->read_hidden)
|
|
|
|
continue;
|
2001-01-24 17:16:32 +00:00
|
|
|
if (preg_match("/\[".preg_quote($section)."\](.*?)(\n\[|\Z)/s", $contents, $match))
|
2001-10-09 16:29:10 +00:00
|
|
|
if ($section{0} == '.')
|
|
|
|
$section = substr($section, 1);
|
2001-01-24 17:16:32 +00:00
|
|
|
$config_data["sections"][$section]["vars"] = $this->_parse_config_block($match[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_config_data[$config_file] = $config_data;
|
2002-12-09 23:37:35 +00:00
|
|
|
|
|
|
|
return true;
|
2001-01-24 17:16:32 +00:00
|
|
|
}
|
|
|
|
|
2003-02-03 16:32:45 +00:00
|
|
|
/**#@+ @access private */
|
|
|
|
/**
|
|
|
|
* @var string $config_block
|
|
|
|
*/
|
2001-01-24 17:16:32 +00:00
|
|
|
function _parse_config_block($config_block)
|
|
|
|
{
|
|
|
|
$vars = array();
|
|
|
|
|
|
|
|
/* First we grab the multi-line values. */
|
|
|
|
if (preg_match_all("/^([^=\n]+)=\s*\"{3}(.*?)\"{3}\s*$/ms", $config_block, $match, PREG_SET_ORDER)) {
|
|
|
|
for ($i = 0; $i < count($match); $i++) {
|
|
|
|
$this->_set_config_var($vars, trim($match[$i][1]), $match[$i][2], false);
|
|
|
|
}
|
|
|
|
$config_block = preg_replace("/^[^=\n]+=\s*\"{3}.*?\"{3}\s*$/ms", "", $config_block);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$config_lines = preg_split("/\n+/", $config_block);
|
|
|
|
|
|
|
|
foreach ($config_lines as $line) {
|
2001-10-09 16:29:10 +00:00
|
|
|
if (preg_match("/^\s*(\.?\w+)\s*=(.*)/", $line, $match)) {
|
2001-01-24 17:16:32 +00:00
|
|
|
$var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', trim($match[2]));
|
|
|
|
$this->_set_config_var($vars, trim($match[1]), $var_value, $this->booleanize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2003-02-03 16:32:45 +00:00
|
|
|
/**
|
|
|
|
* @param array &$container
|
|
|
|
* @param string $var_name
|
|
|
|
* @param mixed $var_value
|
|
|
|
* @param boolean $booleanize determines whether $var_value is converted to
|
|
|
|
* to true/false
|
|
|
|
*/
|
2001-01-24 17:16:32 +00:00
|
|
|
function _set_config_var(&$container, $var_name, $var_value, $booleanize)
|
|
|
|
{
|
2001-10-09 16:29:10 +00:00
|
|
|
if ($var_name{0} == '.') {
|
|
|
|
if (!$this->read_hidden)
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
$var_name = substr($var_name, 1);
|
|
|
|
}
|
|
|
|
|
2002-02-18 17:42:11 +00:00
|
|
|
if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {
|
|
|
|
$this->_trigger_error_msg("Bad variable name '$var_name'");
|
|
|
|
return;
|
|
|
|
}
|
2001-01-24 17:16:32 +00:00
|
|
|
|
|
|
|
if ($booleanize) {
|
2001-07-20 16:48:36 +00:00
|
|
|
if (preg_match("/^(on|true|yes)$/i", $var_value))
|
2001-01-24 17:16:32 +00:00
|
|
|
$var_value = true;
|
2001-07-20 16:48:36 +00:00
|
|
|
else if (preg_match("/^(off|false|no)$/i", $var_value))
|
2001-01-24 17:16:32 +00:00
|
|
|
$var_value = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($container[$var_name]) || $this->overwrite)
|
|
|
|
$container[$var_name] = $var_value;
|
|
|
|
else {
|
|
|
|
settype($container[$var_name], 'array');
|
|
|
|
$container[$var_name][] = $var_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-03 16:32:45 +00:00
|
|
|
/**
|
|
|
|
* @uses trigger_error() creates a PHP warning/error
|
|
|
|
* @param string $error_msg
|
|
|
|
* @param integer $error_type one of
|
|
|
|
*/
|
2002-02-18 17:42:11 +00:00
|
|
|
function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
|
2001-01-24 17:16:32 +00:00
|
|
|
{
|
2002-02-18 17:42:11 +00:00
|
|
|
trigger_error("Config_File error: $error_msg", $error_type);
|
2001-01-24 17:16:32 +00:00
|
|
|
}
|
2003-02-03 16:32:45 +00:00
|
|
|
/**#@-*/
|
2001-01-24 17:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|