时间:2021-07-01 10:21:17 帮助过:26人阅读
<?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' );
}
}
代码如下:
// 发送SQL语句函数
public function send_query( $sql )
{
switch ( strtoupper( $this->config->database ) )
{
case 'MYSQL' :
return $this->send_mysql_query( $sql );
break;
case 'ACCESS' :
return $this->send_odbc_query( $sql );
break;
default :
$this->sys_err( '数据库类型错误,该类目前只支持MYSQL与ACCESS两种数据库', 'die' );
break;
}
}
// 发送SQL语句到MYSQL函数
private function send_mysql_query( $sql )
{
@$rs = mysql_query( $sql, $this->conn );
if ( $rs == false )
{
$mysql_err = mysql_error();
$this->sys_err( "SQL语句:{{$sql}}执行失败,原因是:{{$mysql_err}}", 'die' );
}
return $rs;
}
// 发送SQL语句到ACCESS函数
private function send_odbc_query( $sql )
{
@$rs = odbc_exec( $this->conn, $sql );
if ( $rs == false )
{
$odbc_err = odbc_errormsg( $this->conn );
$this->sys_err( "SQL语句:{{$sql}}执行失败,原因是:{{$odbc_err}}", 'die' );
}
return $rs;
}
// 获取查询返回函数
public function select_query( $sql, $retuen_res = false )
{
$res = $this->send_query( $sql );
if ( $retuen_res == true )
{
return $res;
}
switch ( strtoupper( $this->config->database ) )
{
case 'MYSQL' :
return $this->select_mysql_query( $res );
break;
case 'ACCESS' :
return $this->select_access_query( $res );
break;
default :
$this->sys_err( '数据库类型错误,该类目前只支持MYSQL与ACCESS两种数据库', 'die');
break;
}
}
// 获取MYSQL查询返回函数
private function select_mysql_query( $res )
{
$arr = array();
while ( false != ( $rs = mysql_fetch_assoc( $res ) ) )
{
$arr[] = $rs;
}
mysql_free_result( $res );
return ( count( $arr ) > 0 ? $arr : false );
}
// 获取ACCCESS查询返回函数
private function select_access_query( $res )
{
$arr = array();
while ( false != ( $rs = odbc_fetch_array( $res ) ) )
{
$arr[] = $rs;
}
odbc_free_result( $res );
return ( count( $arr ) > 0 ? $arr : false );
}
// 获取系统错误函数
public function sys_err( $err_msg, $method, $err_notice = '很抱歉,本站发生系统错误,请稍候再试。' )
{
$this->err_record( 'sys', $err_msg );
switch ( $method )
{
case 'keep':
return;
break;
default:
$this->err_notice( $err_notice );
break;
}
}
// 获取用户错误函数
public function user_err( $err_notice, $method, $re_href = '', $err_msg = '' )
{
if ( !empty( $err_msg ) )
{
$this->err_record( 'user', $err_msg );
}
switch ( $method )
{
case 'keep':
return;
break;
default:
$this->err_notice( $err_notice, $re_href );
break;
}
}
// 记录错误函数
private function err_record( $type, $err_msg )
{
$err_url = $this->the_dir . 'lib/error/';
$err_url .= ( $type == 'sys' ? 'system.err' : 'user.err' );
$record_msg = date( 'Y-m-d H:i:s' ) . '|' . $err_msg . "\n";
$this->file_put( $err_url, $record_msg, 'ab' );
}
// 文件写入函数
public function file_put( $url, $content, $method )
{
$dir = str_replace( basename( $url ), '', $url );
if ( !file_exists( $dir ) )
{
$this->sys_err( "{{$dir}}文件夹不存在", 'die' );
}
@$f = fopen( $url, $method );
@flock( $f, LOCK_NM );
@fwrite( $f, $content, strlen( $content ) );
@flock( $f, LOCK_UN );
@fclose( $f );
}
// 提示错误函数
protected function err_notice( $err_notice, $re_href = '' )
{
$err_page = $this->the_dir . $this->err_page;
if ( !file_exists( $err_page ) )
{
$this->sys_err( '错误提示页面丢失', 'keep' );
die( '很抱歉,本站发生系统错误,请稍候再试。' );
}
$err_html = file_get_contents( $err_page );
$err_html = str_replace( '<%$err_notice%>', $err_notice, $err_html);
$err_html = str_replace( '<%$this_url%>', $this->the_dir, $err_html);
$err_html = str_replace( '<%$re_href%>', $re_href, $err_html);
echo $err_html;
exit;
}
// 用于设定模版文件路径的函数
function set_dir( $file_dir = 'smarty_file' )
{
if ( !preg_match( '{/$}', $file_dir ) )
{
$file_dir .= '/';
}
if ( !file_exists( $this->the_dir . $file_dir . $this->template_dir ) )
{
$this->sys_err( 'smarty模版路径丢失', 'die' );
}
if ( !file_exists( $this->the_dir . $file_dir . $this->compile_dir ) )
{
$this->sys_err( 'smarty编译路径丢失', 'die' );
}
if ( !file_exists( $this->the_dir . $file_dir . $this->cache_dir ) )
{
$this->sys_err( 'smarty缓存路径丢失', 'die' );
}
$this->template_dir = $this->the_dir . $file_dir . $this->template_dir;
$this->compile_dir = $this->the_dir . $file_dir . $this->compile_dir;
$this->cache_dir = $this->the_dir . $file_dir . $this->cache_dir;
}
// 生成静态页面函数
public function create_html( $tpl, $file_url = '', $html_name = '' )
{
$html_tpl = $this->fetch( $tpl );
//生成静态文件的文件名
if ( empty( $html_name ) )
{
$file_name = strtolower( basename( $_SERVER['PHP_SELF'] ) );
$file_name = str_replace( '.php', ".{$this->html_cache}", $file_name );
}
else
{
$file_name = $html_name;
}
if ( !empty( $file_url ) && !preg_match( '!\/$!', $file_url ) )
{
$file_url .= '/';
}
$file_url = !empty( $file_url ) ? $this->the_dir . $file_url : "./{$file_url}";
$file_url .= $file_name;
$this->file_put( $file_url, $html_tpl, 'wb');
header("location:{$file_url}");
exit();
}
// 转到静态页面
public function goto_html( $left_time = null, $file_url = '', $html_name = '' )
{
$left_time = ( $left_time == null ? $this->html_cache_lifetime : intval( $left_time ) );
//获取静态文件的文件名
if ( empty( $html_name ) )
{
$file_name = strtolower( basename( $_SERVER['PHP_SELF'] ) );
$file_name = str_replace( '.php', ".{$this->html_cache}", $file_name );
}
else
{
$file_name = $html_name;
}
if ( !empty( $file_url ) && !preg_match( '!\/$!', $file_url ) )
{
$file_url .= '/';
}
$file_url = !empty( $file_url ) ? $this->the_dir . $file_url : "./{$file_url}";
$file_url .= $file_name;
if ( !file_exists( $file_url) )
{
return;
}
if ( $left_time == -1 )
{
header("location:{$file_url}");
exit;
}
else
{
@$fmtime = filemtime( $file_url );
$fmtime = intval( $fmtime );
if ( time() - $fmtime <= $left_time )
{
header("location:{$file_url}");
exit;
}
}
}
}
?>
<?php
// 添斜线
function m_addslashes( $gpc )
{
if ( !get_magic_quotes_gpc() )
{
if( is_array( $gpc ) )
{
while( list( $k, $v ) = each( $gpc ) )
{
if( is_array( $gpc[$k] ) )
{
while( list( $k2, $v2 ) = each( $gpc[$k] ) )
{
$gpc[$k][$k2] = addslashes( $v2 );
}
@reset( $gpc[$k] );
}
else
{
$gpc[$k] = addslashes( $v );
}
}
@reset( $gpc );
}
else
{
$gpc = addslashes( $gpc );
}
}
return $gpc;
}
?>
<?php
function m_check_fill( $check_arr = array() )
{
$pattern['idname'] = array('!^[a-z0-9]{3,8}$!i' , '您输入的用户名格式不正确');
$pattern['username'] = array('!^.{4,12}$!' , '您输入的用户昵称格式不正确');
$pattern['email'] = array('!^([a-z0-9]+(\.[a-z0-9]+)?@[a-z0-9]+\.[a-z0-9]+(\.[a-z0-9]+)?)?$!i' , '您输入的电子邮箱格式不正确');
$pattern['oicq'] = array('!^([0-9]{4,12})?$!' , '您输入的OICQ格式不正确');
$pattern['password'] = array('!^[a-z0-9]{6,14}$!i' , '您输入的密码格式不正确');
$pattern['real_name'] = array('!^.{4,20}$!' , '您输入的真实姓名格式不正确');
$pattern['id_card'] = array('!^[0-9]{15}([0-9]{2}[a-z0-9])?$!i' , '您输入的身份证号码格式不正确');
$pattern['title'] = array('!^.{1,255}$!' , '您输入的帖子标题格式不正确');
$pattern['block_name'] = array('!^.{3,12}$!' , '您输入的板块名称格式不正确');
$err_msg = '';
if ( !is_array( $check_arr ) )
{
return '很抱歉,系统出现参数传递错误,请通知管理员,谢谢合作';
}
foreach ( $check_arr as $key => $value )
{
if ( !empty( $pattern[$key] ) )
{
if( !preg_match( $pattern[$key][0], $value ) )
{
$err_msg .= $pattern[$key][1] . '<br>';
}
}
}
return $err_msg;
}
?>
<?php
//防止跨站攻击函数
function m_check_key( $no_reload = true )
{
if ( empty( $_COOKIE['check'] ) || $_COOKIE['check'] != $_POST['check'] )
{
setcookie( 'check', '', time()-86400 );
return '发生错误,这也许是由于您重复提交数据造成的,请重新提交。<br>';
}
if ( $no_reload == true )
{
setcookie( 'check', '', time()-86400 );
}
}
?>
<?php
//防止跨站攻击函数
function m_md5( $password = '' )
{
if ( empty( $password ) )
{
$password = md5( uniqid ( rand(), true ) );
setcookie( 'check', $password );
}
else
{
$key = 'machine';
$password = md5( $password . $key );
}
return $password;
}
?>
<?php
//中文截取函数
function m_cnsubstr( $string, $sublen )
{
if( $sublen >= strlen( $string ) )
{
return $string;
}
$s = "";
for ( $i = 0; $i < $sublen; $i++ )
{
if( ord( $string{$i} ) > 127 )
{
$s .= $string{$i} . $string{++$i};
continue;
}
else
{
$s .= $string{$i};
continue;
}
}
return $s . '…';
}
?>
<?php
// 分页类,使用时调用类下面的函数即可
class Page_class
{
//帖子总数
private $count = 0;
//每页显示多少行
private $rows;
//每页显示多少个跳转页号
private $link_num;
//共有多少页
private $pages = 0;
//当前页
private $current_page;
//开始记录
private $start = 0;
//上一大页
private $prve = 0;
//下一大页
private $next = 0;
//数字链接
private $links = array();
//返回值
public $return_rs = array();
public function __construct( $count, $current_page = 1, $rows = 10, $link_num = 7 )
{
//获取传入的帖子总数
$this->count = intval( $count );
//获取当前页
$this->current_page = intval( $current_page );
//显示多少行
$this->rows = intval( $rows );
//每页显示多少个跳转页号
$this->link_num = $link_num;
//调用计算页数的方法
$this->count_page();
//调用返回跳转页号的方法
$this->return_links();
//返回值
$this->return_rs = array(
'rows' => $this->rows,
'prve' => $this->prve,
'next' => $this->next,
'pages' => $this->pages,
'start' => $this->start,
'count' => $this->count,
'links' => $this->links,
'current_page' => $this->current_page
);
}
public function __destruct()
{
}
//计算页数
private function count_page()
{
//计算共有多少页
@$this->pages = ceil( $this->count / $this->rows );
//如果当前页大于最大页数,则使其等于最大页数;如果当前页小于1,则使其等于1
$this->current_page > $this->pages ? $this->current_page = $this->pages : true ;
$this->current_page < 1 ? $this->current_page = 1 : true;
//计算查询的开始记录数
$this->start = ( $this->current_page - 1 ) * $this->rows;
}
//返回页面跳转页号的函数
private function return_links()
{
//用当前页除以显示页数得到当前是第几“大页”
$start_s = floor( $this->current_page / $this->link_num );
//如果当前页号正好整除显示页数,则应该对$start_s减一,因为设想一下,如果当前是第7页
//显示页数也是7,则$start_s=1,也就是说已经到了第二“大页”了,而实际上它应该还是在
//第一“大页”
( $this->current_page % $this->link_num ) == 0 ? $start_s-- : true;
//计算当前“大页”开始页号,算法是(当前“大页”*显示页数)+1;例如0*7+1=1,1*7+1=8,2*7+1=15
$start_page = ( $start_s * $this->link_num ) + 1;
//上一大页
$this->prve = $start_page - 1;
//下一大页
$this->next = $start_page + $this->link_num;
//开始循环计算当前大页中的小页号
for ( $i=0; $i < $this->link_num; $i++ )
{
//如果下一个页号已经超出了总页数,则说明应该停止了
if ( $start_page + $i > $this->pages )
{
break;
}
//将页号记录在$this->links_arr数组中
$this->links[] = $start_page + $i;
}
}
}
function m_page( $count, $current_page = 1, $rows = 10, $link_num = 7 )
{
$page = new Page_class( $count, $current_page, $rows, $link_num );
return $page->return_rs;
}
?>
<?php
// 文本操作函数
// 修改
function m_txt_replace( $pattern, $text, $content )
{
$pattern_start = "<!--$pattern-->";
$pattern_end = "<!--/$pattern-->";
@$ok = preg_match( "{{$pattern_start}.*{$pattern_end}}Ssi", $content, $match );
if ( $ok != true )
{
return false;
}
$replace = "{$pattern_start}{$text}{$pattern_end}";
$new_content = str_replace( $match[0], $replace, $content );
return $new_content;
}
// 追加
function m_txt_add( $pattern, $text, $content )
{
$pattern = "<!--{$pattern}-->";
@$ok = preg_match( "{{$pattern}}Ssi", $content );
if ( $ok != true )
{
return false;
}
$add = "{$pattern}\n{$text}";
$new_content = str_replace( $pattern, $add, $content );
return $new_content;
}
// 删除
function m_txt_delete( $pattern, $content )
{
$pattern_start = "<!--$pattern-->";
$pattern_end = "<!--/$pattern-->";
@$ok = preg_match( "{{$pattern_start}.*{$pattern_end}}Ssi", $content, $match );
if ( $ok != true )
{
return false;
}
$new_content = str_replace( $match[0], '', $content );
return $new_content;
}
//获取
function m_txt_get( $pattern, $content )
{
$pattern_start = "<!--$pattern-->";
$pattern_end = "<!--/$pattern-->";
@$ok = preg_match( "{{$pattern_start}.*{$pattern_end}}Ssi", $content, $match );
if ( $ok != true )
{
return false;
}
return $match[0];
}
?>
<?php
// 上传函数
function m_up_file( $files, $up_url, $type, $max_size = 2097152 )
{
$i = 0;
if ( !is_array( $files ) )
{
die( '参数传递错误' );
}
$type_pattern = is_array( $type ) ? '\.(' . implode( ')|(', $type ) . ')' : "\.({$type})";
foreach ( $files as $key => $arr )
{
$ok = false;
if( $arr['error'] == 0 )
{
if ( !is_uploaded_file( $arr['tmp_name'] ) )
{
$err_msg .= "文件:<b>{$arr['name']}</b>不可上传<br>";
continue;
}
elseif ( $_FILES['up_file']['size'] > $max_size )
{
$err_msg .= "文件:<b>{$arr['name']}</b>上传失败,原因是:文件超过限定大小<br>";
continue;
}
elseif ( !preg_match( "!{$type_pattern}!Si", $arr['name'] ) )
{
$err_msg .= "文件<b>{$arr['name']}</b>上传失败,原因是:格式不正确<br>";
continue;
}
else
{
$txt = substr( str_shuffle( 'abcdefghijklmnopqrstuvwxyz' ), -4 );
$hz = strtolower( strstr( $arr['name'], '.' ) );
$new_name = date( 'YmdHis' ) . $txt . $hz;
if ( !is_array( $up_url ) )
{
!preg_match( '!\/$!', $up_url ) ? $up_url .= '/' : true;
$new_url = $up_url . $new_name;
}
else
{
$key = str_replace( '.', '', $hz );
$up_url = array_change_key_case( $up_url, CASE_LOWER );
!preg_match( '!\/$!', $up_url[$key] ) ? $up_url[$key] .= '/' : true;
$new_url = $up_url[$key] . $new_name;
}
@$ok = move_uploaded_file( $arr['tmp_name'], THIS_DIR . $new_url );
}
}
if ( $ok == true )
{
$rs[$i]['url'] = $new_url;
$rs[$i]['name'] = $arr['name'];
$rs[$i]['type'] = strtoupper( str_replace( '.', '', $hz ) );
$i++;
}
elseif( !empty($arr['name']) )
{
$err_msg .= "文件<b>{$arr['name']}</b>上传出错<br>";
continue;
}
}
return array( 'arr' => $rs, 'err_msg' => $err_msg, 'num' => $i );
}
?>