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