⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smarty_config.class.php

📁 极限网络智能办公系统—MYOA26—100%—源程序。
💻 PHP
字号:
<?php

require_once( "./smarty/PEAR.php" );
class config_file extends pear
{

	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" || 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 ) || !file_exists( $config_path ) || !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 && $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( "/^(.*?)(\n\\[|\\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] == "." && !$this->read_hidden )
			{
			}
			else
			{
				if ( preg_match( "/\\[".preg_quote( $section )."\\](.*?)(\n\\[|\\Z)/s", $contents, $match ) && $section[0] == "." )
				{
					$section = substr( $section, 1 );
				}
				$config_data['sections'][$section]['vars'] = $this->_parse_config_block( $match[1] );
			}
		}
		$this->_config_data[$config_file] = $config_data;
	}

	function _parse_config_block( $config_block )
	{
		$vars = array( );
		if ( preg_match_all( "/^([^=\n]+)=\\s*\"{3}(.*?)\"{3}\\s*\$/ms", $config_block, $match, PREG_SET_ORDER ) )
		{
			$i = 0;
			for ( ;	$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 )
		{
			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 );
			}
		}
		return $vars;
	}

	function _set_config_var( &$container, $var_name, $var_value, $booleanize )
	{
		if ( $var_name[0] == "." )
		{
			if ( !$this->read_hidden )
			{
				return;
			}
			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] ) || $this->overwrite )
		{
			$container[$var_name] = $var_value;
		}
		else
		{
			settype( $container[$var_name], "array" );
			$container[$var_name][] = $var_value;
		}
	}

}

class config_file_error extends pear_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."\n", $code, $mode, $level );
	}

}

?>

⌨️ 快捷键说明

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