smarty_config.class.php

来自「通达OA2007SE源代码 非常好的」· PHP 代码 · 共 257 行

PHP
257
字号
<?
  class config_file
  {
    var $overwrite = true;
    var $booleanize = true;
    var $read_hidden = true;
    var $_config_path = '';
    var $_config_data = array ();
    var $_separator = '';
    function config_file ($config_path = NULL)
    {
      $this->PEAR ();
      if (((substr (PHP_OS, 1, 3) == 'WIN') OR (substr (PHP_OS, 1, 4) == 'OS/2')))
      {
        $this->_separator = '\\';
      }
      else
      {
        $this->_separator = '/';
      }
      if (isset ($config_path))
      {
        $this->set_path ($config_path);
      }
    }
    function set_path ($config_path)
    {
      if (!empty ($config_path))
      {
        if (((!is_string ($config_path) OR !file_exists ($config_path)) OR !is_dir ($config_path)))
        {
          return new Config_File_Error ((((''.'Bad config file path \'').$config_path).'\''));
        }
        $this->_config_path = ($config_path.$this->_separator);
      }
    }
    function get ($file_name, $section_name = NULL, $var_name = NULL)
    {
      if (empty ($file_name))
      {
        return new Config_File_Error ('Empty config file name');
      }
      else
      {
        $file_name = ($this->_config_path.$file_name);
        if (!isset ($this->_config_data[$file_name]))
        {
          $this->load_file ($file_name, false);
        }
      }
      if (!empty ($var_name))
      {
        if (empty ($section_name))
        {
          return $this->_config_data[$file_name]['vars'][$var_name];
        }
        else
        {
          return $this->_config_data[$file_name]['sections'][$section_name]['vars'][$var_name];
        }
      }
      else
      {
        if (empty ($section_name))
        {
          return (array)$this->_config_data[$file_name]['vars'];
        }
        else
        {
          return (array)$this->_config_data[$file_name]['sections'][$section_name]['vars'];
        }
      }
    }
    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;
    }
    function get_file_names ()
    {
      return array_keys ($this->_config_data);
    }
    function get_section_names ($file_name)
    {
      $file_name = ($this->_config_path.$file_name);
      if (!isset ($this->_config_data[$file_name]))
      {
        return new Config_File_Error ((((''.'Unknown config file \'').$file_name).'\''));
      }
      return array_keys ($this->_config_data[$file_name]['sections']);
    }
    function get_var_names ($file_name, $section = NULL)
    {
      if (empty ($file_name))
      {
        return new Config_File_Error ('Empty config file name');
      }
      else
      {
        if (!isset ($this->_config_data[$file_name]))
        {
          return new Config_File_Error ((((''.'Unknown config file \'').$file_name).'\''));
        }
      }
      if (empty ($section))
      {
        return array_keys ($this->_config_data[$file_name]['vars']);
      }
      else
      {
        return array_keys ($this->_config_data[$file_name]['sections'][$section]['vars']);
      }
    }
    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 ();
        }
      }
    }
    function load_file ($file_name, $prepend_path = true)
    {
      global $php_errormsg;
      if (($prepend_path AND ($this->_config_path != '')))
      {
        $config_file = ($this->_config_path.$file_name);
      }
      else
      {
        $config_file = $file_name;
      }
      ini_set ('track_errors', true);
      $fp = fopen ($config_file, 'r');
      if (!is_resource ($fp))
      {
        return new Config_File_Error ($php_errormsg);
      }
      $contents = fread ($fp, filesize ($config_file));
      fclose ($fp);
      $config_data = array ();
      if (preg_match ('/^(.*?)(
\\[|\\Z)/s', $contents, $match))
      {
        $config_data['vars'] = $this->_parse_config_block ($match[1]);
      }
      $config_data['sections'] = array ();
      preg_match_all ('/^\\[(.*?)\\]/m', $contents, $match);
      foreach ($match[1] as $section)
      {
        if ((($section[0] == '.') AND !$this->read_hidden))
        {
          continue;
        }
        else
        {
          if (preg_match ((('/\\['.preg_quote ($section)).'\\](.*?)(
\\[|\\Z)/s'), $contents, $match))
          {
            if (($section[0] == '.'))
            {
              $section = substr ($section, 1);
            }
          }
          $config_data['sections'][$section]['vars'] = $this->_parse_config_block ($match[1]);
          continue;
        }
      }
      $this->_config_data[$config_file] = $config_data;
    }
    function _parse_config_block ($config_block)
    {
      $vars = array ();
      if (preg_match_all ((''.'/^([^=
]+)=\\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 ((''.'/^[^=
]+=\\s*"{3}.*?"{3}\\s*$/ms'), '', $config_block);
      }
      $config_lines = preg_split ('/
+/', $config_block);
      foreach ($config_lines as $line)
      {
        if (preg_match ('/^\\s*(\\.?\\w+)\\s*=(.*)/', $line, $match))
        {
          $var_value = preg_replace ('/^([\'"])(.*)\\1$/', '\\2', trim ($match[2]));
          $this->_set_config_var ($vars, trim ($match[1]), $var_value, $this->booleanize);
          continue;
        }
      }
      return $vars;
    }
    function _set_config_var (&$container, $var_name, $var_value, $booleanize)
    {
      if (($var_name[0] == '.'))
      {
        if (!$this->read_hidden)
        {
          return null;
        }
        else
        {
          $var_name = substr ($var_name, 1);
        }
      }
      if (!preg_match ((''.'/^[a-zA-Z_]\\w*$/'), $var_name))
      {
        return new Config_File_Error ((((''.'Bad variable name \'').$var_name).'\''));
      }
      if ($booleanize)
      {
        if (preg_match ((''.'/^(on|true|yes)$/i'), $var_value))
        {
          $var_value = true;
        }
        else
        {
          if (preg_match ((''.'/^(off|false|no)$/i'), $var_value))
          {
            $var_value = false;
          }
        }
      }
      if ((!isset ($container[$var_name]) OR $this->overwrite))
      {
        $container[$var_name] = $var_value;
      }
      else
      {
        settype ($container[$var_name], 'array');
        $container[$var_name][] = $var_value;
      }
    }
  }
  class config_file_error
  {
    var $error_message_prefix = 'Config_File: ';
    function config_file_error ($message, $code = 0, $mode = PEAR_ERROR_PRINT, $level = E_USER_NOTICE)
    {
      $this->PEAR_Error (($message.'
'), $code, $mode, $level);
    }
  }
  require_once './smarty/PEAR.php';
?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?