<?php
session_cache_limiter( 'private,must-revalidate');
session_start();
if ( !defined( 'THIS_DIR' ) )
{
die( '系统错误:路径常量丢失' );
}
if ( @!include_once( THIS_DIR . 'lib/smarty/Smarty.class.php' ) )
{
die( '系统错误:smarty文件丢失' );
}
unset( $GLOBALS );
unset( $HTTP_COOKIE_VARS );
unset( $HTTP_ENV_VARS );
unset( $HTTP_GET_VARS );
unset( $HTTP_POST_FILES );
unset( $HTTP_POST_VARS );
unset( $HTTP_SERVER_VARS );
unset( $HTTP_SESSION_VARS );
class Machine_m extends Smarty
{
// 数据库资源
private $conn;
// 路径
private $the_dir;
// 配置文件
private $config = array();
private $config_url;
// 外部函数列表
private $func_list = array();
private $func_list_url;
// 错误提示页面
public $err_page = 'lib/error/err_page.htm';
// 静态生成
public $html_cache = 'html';
public $html_cache_lifetime = 86400;
// 构造函数
public function __construct( $test = false )
{
// 保留smarty类的构造部分
$this->assign('SCRIPT_NAME', isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME']
: @$GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
// 现在是machine_m的构造部分
$this->left_delimiter = '<%';
$this->right_delimiter = '%>';
$this->the_dir = THIS_DIR;
$this->config_url = "{$this->the_dir}lib/config/config.php";
$this->config = $this->parse_ini();
$this->func_list_url = "{$this->the_dir}lib/config/func_list.php";
$this->func_list = $this->parse_func();
$this->state( $test );
$this->load_func( array( 'm_addslashes', 'm_stripslashes' ) );
$this->connect();
}
// 析构函数
public function __destruct()
{
}
// 设置网站状态函数
private function state( $test )
{
if ( $test == true)
{
$this->load_func( array( 'm_print_r', 'm_var_dump' ) );
$this->compile_check = true;
error_reporting(E_ALL);
}
else
{
$this->compile_check = false;
error_reporting(0);
}
}
// 解析配置文件函数
private function parse_ini()
{
if ( !file_exists( $this->config_url ) )
{
$this->sys_err( "config配置文件丢失", 'die' );
}
$config = parse_ini_file( $this->config_url );
if ( !array_key_exists( 'host_name', $config )
|| !array_key_exists( 'user_name', $config )
|| !array_key_exists( 'db_name', $config )
|| !array_key_exists( 'password', $config ) )
{
$this->sys_err( '配置错误,请检查config配置文件', 'die');
}
$config = $this->decode_config( $config );
settype( $config, 'object');
return $config;
}
// 解析函数列表函数
private function parse_func()
{
if ( !file_exists( $this->func_list_url ) )
{
$this->sys_err( "func_list配置文件丢失", 'die' );
}
$func_list = parse_ini_file( $this->func_list_url );
return $func_list;
}
// 外部函数加载函数
public function load_func( $func )
{
if ( is_array( $func ) )
{
foreach ( $func as $func_name )
{
$this->include_file( $this->func_list[$func_name] );
}
}
else
{
$this->include_file( $this->func_list[$func] );
}
}
// 外部函数包含函数
public function include_file( $file_url )
{
$file_url = $this->the_dir .$file_url;
@$ok = include_once( $file_url );
if ( $ok != true )
{
$this->sys_err( "文件{{$file_url}}加载失败", 'die' );
}
}
// 对config文件解码函数(将数据库用户名和密码明文纪录是不安全的,最好先加密,再在此解密,本函数可以重载)
protected function decode_config( $config )
{
return $config;
}
// 连接数据库函数
private function connect()
{
switch ( strtoupper( $this->config->database ) )
{
case 'MYSQL' :
$this->connect_mysql();
break;
case 'ACCESS' :
$this->connect_access();
break;
default :
$this->sys_err( '数据库类型错误,该类目前只支持MYSQL与ACCESS两种数据库', 'die');
break;
}
}
// 连接MYSQL数据库函数
private function connect_mysql()
{
if ( $this->conn != null )
{
@mysql_close( $this->conn );
$this->conn = null;
}
@$this->conn = mysql_connect( $this->config->host_name, $this->config->user_name, $this->config->password );
if ( $this->conn == false )
{
$mysql_err = mysql_error();
$this->sys_err( "MYSQL数据库连接失败,原因是:{{$mysql_err}}", 'die' );
}
@$db = mysql_select_db( $this->config->db_name, $this->conn ) ;
if ( $db == false )
{
$mysql_err = mysql_error();
$this->sys_err( "数据表连接失败,原因是:{{$mysql_err}}", 'die' );
}
}
// 连接ACCESS数据库函数
private function connect_access()
{
if ( $this->conn != null )
{
@odbc_close( $this->conn );
$this->conn = null;
}
$dsn = 'Driver=Microsoft Access Driver (*.mdb);dbq=' . realpath( $this->the_dir . $this->config->db_name );
@$this->conn = odbc_connect( $dsn, $this->config->user_name, $this->config->password );
if ( $this->conn == false )
{
@$odbc_err = odbc_errormsg( $this->conn );
$this->sys_err( "ACCESS数据库连接失败,原因是:{{$odbc_err}}", 'die' );
}
}
1
您可能感兴趣的文章: