home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Config_File.class.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  10.2 KB  |  366 lines

  1. <?php
  2.  
  3. /**
  4.  * Config_File class.
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  * You may contact the author of Config_File by e-mail at:
  21.  * {@link andrei@php.net}
  22.  *
  23.  * Or, write to:
  24.  * Andrei Zmievski
  25.  * Software Engineer, ispi
  26.  * 237 S. 70th suite 220
  27.  * Lincoln, NE 68510
  28.  *
  29.  * The latest version of Config_File can be obtained from:
  30.  * http://smarty.php.net/
  31.  *
  32.  * @link http://smarty.php.net/
  33.  * @version 2.5.0
  34.  * @copyright Copyright: 2001,2002 ispi of Lincoln, Inc.
  35.  * @author Andrei Zmievski <andrei@php.net>
  36.  * @access public
  37.  * @package Smarty
  38.  */
  39.  
  40. /* $Id: Config_File.class.php,v 1.2 2003/04/20 21:30:18 CelloG Exp $ */
  41. /**
  42.  * Config file reading class
  43.  * @package Smarty
  44.  */
  45. class Config_File {
  46.     /**#@+
  47.      * Options
  48.      * @var boolean
  49.      */
  50.     /**
  51.      * Controls whether variables with the same name overwrite each other.
  52.      */
  53.     var $overwrite        =    true;
  54.  
  55.     /**
  56.      * Controls whether config values of on/true/yes and off/false/no get
  57.      * converted to boolean values automatically.
  58.      */
  59.     var $booleanize        =    true;
  60.  
  61.     /**
  62.      * Controls whether hidden config sections/vars are read from the file.
  63.      */
  64.     var $read_hidden     =    true;
  65.  
  66.     /**
  67.      * Controls whether or not to fix mac or dos formatted newlines.
  68.      * If set to true, \r or \r\n will be changed to \n.
  69.      */
  70.     var $fix_newlines =    true;
  71.     /**#@-*/
  72.     
  73.     /** @access private */
  74.     var $_config_path    = "";
  75.     var $_config_data    = array();
  76.     var $_separator        = "";
  77.     /**#@-*/
  78.  
  79.     /**
  80.      * Constructs a new config file class.
  81.      *
  82.      * @param string $config_path (optional) path to the config files
  83.      */
  84.     function Config_File($config_path = NULL)
  85.     {
  86.         if (substr(PHP_OS, 0, 3) == "WIN" || substr(PHP_OS, 0, 4) == "OS/2")
  87.             $this->_separator = "\\";
  88.         else
  89.             $this->_separator = "/";
  90.  
  91.         if (isset($config_path))
  92.             $this->set_path($config_path);
  93.     }
  94.  
  95.  
  96.     /**
  97.      * Set the path where configuration files can be found.
  98.      *
  99.      * @param string $config_path path to the config files
  100.      */
  101.     function set_path($config_path)
  102.     {
  103.         if (!empty($config_path)) {
  104.             if (!is_string($config_path) || !file_exists($config_path) || !is_dir($config_path)) {
  105.                 $this->_trigger_error_msg("Bad config file path '$config_path'");
  106.                 return;
  107.             }
  108.  
  109.             $this->_config_path = $config_path . $this->_separator;
  110.         }
  111.     }
  112.  
  113.     
  114.     /**
  115.      * Retrieves config info based on the file, section, and variable name.
  116.      *
  117.      * @param string $file_name config file to get info for
  118.      * @param string $section_name (optional) section to get info for
  119.      * @param string $var_name (optional) variable to get info for
  120.      * @return string|array a value or array of values
  121.      */
  122.     function &get($file_name, $section_name = NULL, $var_name = NULL)
  123.     {
  124.         if (empty($file_name)) {
  125.             $this->_trigger_error_msg('Empty config file name');
  126.             return;
  127.         } else {
  128.             $file_name = $this->_config_path . $file_name;
  129.             if (!isset($this->_config_data[$file_name]))
  130.                 $this->load_file($file_name, false);
  131.         }
  132.         
  133.         if (!empty($var_name)) {
  134.             if (empty($section_name)) {
  135.                 return $this->_config_data[$file_name]["vars"][$var_name];
  136.             } else {
  137.                 if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name]))
  138.                     return $this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name];
  139.                 else
  140.                     return array();
  141.             }
  142.         } else {
  143.             if (empty($section_name)) {
  144.                 return (array)$this->_config_data[$file_name]["vars"];
  145.             } else {
  146.                 if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"]))
  147.                     return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"];
  148.                 else
  149.                     return array();
  150.             }
  151.         }
  152.     }
  153.     
  154.  
  155.     /**
  156.      * Retrieves config info based on the key.
  157.      *
  158.      * @param $file_name string config key (filename/section/var)
  159.      * @return string|array same as get()
  160.      * @uses get() retrieves information from config file and returns it
  161.      */
  162.     function &get_key($config_key)
  163.     {
  164.         list($file_name, $section_name, $var_name) = explode('/', $config_key, 3);
  165.         $result = &$this->get($file_name, $section_name, $var_name);
  166.         return $result;
  167.     }
  168.  
  169.     /**
  170.      * Get all loaded config file names.
  171.      *
  172.      * @return array an array of loaded config file names
  173.      */
  174.     function get_file_names()
  175.     {
  176.         return array_keys($this->_config_data);
  177.     }
  178.     
  179.  
  180.     /**
  181.      * Get all section names from a loaded file.
  182.      *
  183.      * @param string $file_name config file to get section names from
  184.      * @return array an array of section names from the specified file
  185.      */
  186.     function get_section_names($file_name)
  187.     {
  188.         $file_name = $this->_config_path . $file_name;
  189.         if (!isset($this->_config_data[$file_name])) {
  190.             $this->_trigger_error_msg("Unknown config file '$file_name'");
  191.             return;
  192.         }
  193.         
  194.         return array_keys($this->_config_data[$file_name]["sections"]);
  195.     }
  196.     
  197.  
  198.     /**
  199.      * Get all global or section variable names.
  200.      *
  201.      * @param string $file_name config file to get info for
  202.      * @param string $section_name (optional) section to get info for
  203.      * @return array an array of variables names from the specified file/section
  204.      */
  205.     function get_var_names($file_name, $section = NULL)
  206.     {
  207.         if (empty($file_name)) {
  208.             $this->_trigger_error_msg('Empty config file name');
  209.             return;
  210.         } else if (!isset($this->_config_data[$file_name])) {
  211.             $this->_trigger_error_msg("Unknown config file '$file_name'");
  212.             return;
  213.         }
  214.         
  215.         if (empty($section))
  216.             return array_keys($this->_config_data[$file_name]["vars"]);
  217.         else
  218.             return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]);
  219.     }
  220.     
  221.  
  222.     /**
  223.      * Clear loaded config data for a certain file or all files.
  224.      *
  225.      * @param string $file_name file to clear config data for
  226.      */
  227.     function clear($file_name = NULL)
  228.     {
  229.         if ($file_name === NULL)
  230.             $this->_config_data = array();
  231.         else if (isset($this->_config_data[$file_name]))
  232.             $this->_config_data[$file_name] = array();
  233.     }
  234.  
  235.  
  236.     /**
  237.      * Load a configuration file manually.
  238.      *
  239.      * @param string $file_name file name to load
  240.      * @param boolean $prepend_path whether current config path should be
  241.      *                              prepended to the filename
  242.      */
  243.     function load_file($file_name, $prepend_path = true)
  244.     {
  245.         if ($prepend_path && $this->_config_path != "")
  246.             $config_file = $this->_config_path . $file_name;
  247.         else
  248.             $config_file = $file_name;
  249.  
  250.         ini_set('track_errors', true);
  251.         $fp = @fopen($config_file, "r");
  252.         if (!is_resource($fp)) {
  253.             $this->_trigger_error_msg("Could not open config file '$config_file'");
  254.             return false;
  255.         }
  256.  
  257.         $contents = fread($fp, filesize($config_file));
  258.         fclose($fp);
  259.         
  260.         if($this->fix_newlines) {
  261.             // fix mac/dos formatted newlines
  262.             $contents = preg_replace('!\r\n?!',"\n",$contents);
  263.         }
  264.  
  265.         $config_data = array();
  266.  
  267.         /* Get global variables first. */
  268.         if (preg_match("/^(.*?)(\n\[|\Z)/s", $contents, $match))
  269.             $config_data["vars"] = $this->_parse_config_block($match[1]);
  270.         
  271.         /* Get section variables. */
  272.         $config_data["sections"] = array();
  273.         preg_match_all("/^\[(.*?)\]/m", $contents, $match);
  274.         foreach ($match[1] as $section) {
  275.             if ($section{0} == '.' && !$this->read_hidden)
  276.                 continue;
  277.             if (preg_match("/\[".preg_quote($section)."\](.*?)(\n\[|\Z)/s", $contents, $match))
  278.                 if ($section{0} == '.')
  279.                     $section = substr($section, 1);
  280.                 $config_data["sections"][$section]["vars"] = $this->_parse_config_block($match[1]);
  281.         }
  282.  
  283.         $this->_config_data[$config_file] = $config_data;
  284.         
  285.         return true;
  286.     }
  287.  
  288.     /**#@+ @access private */
  289.     /**
  290.      * @var string $config_block
  291.      */
  292.     function _parse_config_block($config_block)
  293.     {
  294.         $vars = array();
  295.  
  296.         /* First we grab the multi-line values. */
  297.         if (preg_match_all("/^([^=\n]+)=\s*\"{3}(.*?)\"{3}\s*$/ms", $config_block, $match, PREG_SET_ORDER)) {
  298.             for ($i = 0; $i < count($match); $i++) {
  299.                 $this->_set_config_var($vars, trim($match[$i][1]), $match[$i][2], false);
  300.             }
  301.             $config_block = preg_replace("/^[^=\n]+=\s*\"{3}.*?\"{3}\s*$/ms", "", $config_block);
  302.         }
  303.         
  304.         
  305.         $config_lines = preg_split("/\n+/", $config_block);
  306.  
  307.         foreach ($config_lines as $line) {
  308.             if (preg_match("/^\s*(\.?\w+)\s*=(.*)/", $line, $match)) {
  309.                 $var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', trim($match[2]));
  310.                 $this->_set_config_var($vars, trim($match[1]), $var_value, $this->booleanize);
  311.             }
  312.         }
  313.  
  314.         return $vars;
  315.     }
  316.     
  317.     /**
  318.      * @param array &$container
  319.      * @param string $var_name
  320.      * @param mixed $var_value
  321.      * @param boolean $booleanize determines whether $var_value is converted to
  322.      *                            to true/false
  323.      */
  324.     function _set_config_var(&$container, $var_name, $var_value, $booleanize)
  325.     {
  326.         if ($var_name{0} == '.') {
  327.             if (!$this->read_hidden)
  328.                 return;
  329.             else
  330.                 $var_name = substr($var_name, 1);
  331.         }
  332.  
  333.         if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {
  334.             $this->_trigger_error_msg("Bad variable name '$var_name'");
  335.             return;
  336.         }
  337.  
  338.         if ($booleanize) {
  339.             if (preg_match("/^(on|true|yes)$/i", $var_value))
  340.                 $var_value = true;
  341.             else if (preg_match("/^(off|false|no)$/i", $var_value))
  342.                 $var_value = false;
  343.         }
  344.                 
  345.         if (!isset($container[$var_name]) || $this->overwrite)
  346.             $container[$var_name] = $var_value;
  347.         else {
  348.             settype($container[$var_name], 'array');
  349.             $container[$var_name][] = $var_value;
  350.         }
  351.     }
  352.  
  353.     /**
  354.      * @uses trigger_error() creates a PHP warning/error
  355.      * @param string $error_msg
  356.      * @param integer $error_type one of 
  357.      */
  358.     function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
  359.     {
  360.         trigger_error("Config_File error: $error_msg", $error_type);
  361.     }
  362.     /**#@-*/
  363. }
  364.  
  365. ?>
  366.