<?PHP
/*
* All copyright reserved (c) 2004-2010 by Gohappy
*/

//???????
ini_set('date.timezone','Asia/Shanghai');
//???????
if($sys_error_reporting=='0'){error_reporting(0);	ini_set('html_errors',false);ini_set('display_errors',false);}else{error_reporting(E_ERROR | E_WARNING | E_PARSE);}

//???????????????
if (!defined('PMA_GRAB_GLOBALS_include_onceD')) { 
    define('PMA_GRAB_GLOBALS_include_onceD', 1); 
    if (!empty($_GET)) { 
        extract($_GET, EXTR_OVERWRITE); 
    } else if (!empty($HTTP_GET_VARS)) { 
        extract($HTTP_GET_VARS, EXTR_OVERWRITE); 
    } // end if 

    if (!empty($_POST)) { 
        extract($_POST, EXTR_OVERWRITE); 
    } else if (!empty($HTTP_POST_VARS)) { 
        extract($HTTP_POST_VARS, EXTR_OVERWRITE); 
    } // end if 

    if (!empty($_FILES)) { 
        while (list($name, $value) = each($_FILES)) { 
            $$name = $value['tmp_name']; 
        } 
    } else if (!empty($HTTP_POST_FILES)) { 
        while (list($name, $value) = each($HTTP_POST_FILES)) { 
            $$name = $value['tmp_name']; 
        } 
    } // end if 

    if (!empty($_SERVER) && isset($_SERVER['PHP_SELF'])) { 
        $PHP_SELF = $_SERVER['PHP_SELF']; 
    } else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['PHP_SELF'])) { 
        $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF']; 
    } // end if 

    // Securety fix: disallow accessing serious server files via "?goto=" 
    if (isset($goto) && strpos(' ' . $goto, '/') > 0 && substr($goto, 0, 2) != './') { 
        unset($goto); 
    } // end if 
} 

//?ip??????
unset($onlineip);
if($_SERVER['HTTP_CLIENT_IP']){
     $onlineip=$_SERVER['HTTP_CLIENT_IP'];
}elseif($_SERVER['HTTP_X_FORWARDED_FOR']){
     $onlineip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
     $onlineip=$_SERVER['REMOTE_ADDR'];
}

$ip		=	$onlineip;
$jtime		=	date("Y-m-d H:i:s");
$now_time		=	date("Y-m-d H:i:s");
$now_time_end		=	date("Y-m-d H:i:s",mktime(date("23"),date("59"),date("59"),date("m"),date("d"),date("y")));

//??????
function upload_file()
{
	$sfile	=	Func_get_arg(0);
	$msfile	=	Func_get_arg(1);
	if(@copy($sfile, $msfile) || (function_exists('move_uploaded_file') && @move_uploaded_file($sfile, $msfile)))
	{
			@unlink($sfile);			
	}	
}

//# ????get_templet()
function get_templet()
{
 $sfile=Func_get_arg(0);
 $fd=fopen($sfile,'r');
 $fgoutputstr='';
 $buffer='';
 while($buffer = fgets($fd,1024))
 {
  $fgoutputstr.=$buffer;
 }
 fclose($fd);
 return $fgoutputstr;
}


//???????????????
function rights_check()
{
$re='';
$string=Func_get_arg(0);
$need=Func_get_arg(1);
$i=strlen($string);
for ($j=0;$j<$i;$j++)
{	
	if ($need==substr($string,$j,1)){$re="true";}
}
if ($re==""){$re="false";}
return $re;
}

/*                                                            
 ????????:	????????
 ????????:	str ???????start???λ len???????λ
 ?????????	Gohappy 
*/ 
function substrgb_old($str, $start, $len) {
    $tmpstr = "";
    $strlen = $start + $len;
    for($i = 0; $i < $strlen; $i++) {
        if(ord(substr($str, $i, 1)) > 0xa0) {
            $tmpstr .= substr($str, $i, 2);
            $i++;
        } else
            $tmpstr .= substr($str, $i, 1);
    }
    return $tmpstr;
}

function substrgb_en($str,$start,$len){
for($i=0;$i<$start+$len;$i++){
$tmpstr=(ord($str[$i])>=161 && ord($str[$i])<=247&& 
ord($str[$i+1])>=161 && 
ord($str[$i+1])<=254)?$str[$i].$str[++$i]:$tmpstr=$str[$i];
if ($i>=$start&&$i<($start+$len))$tmp .=$tmpstr;
}
return $tmp;
}

function substrgb($string, $start = 0, $sublen, $code = 'UTF-8') 
{ 
    if($code == 'UTF-8') 
    { 
        $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; 
        preg_match_all($pa, $string, $t_string); 
 
        if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen)).".."; 
        return join('', array_slice($t_string[0], $start, $sublen)); 
    } 
    else 
    { 
        $start = $start*2; 
        $sublen = $sublen*2; 
        $strlen = strlen($string); 
        $tmpstr = ''; 
 
        for($i=0; $i< $strlen; $i++) 
        { 
            if($i>=$start && $i< ($start+$sublen)) 
            { 
                if(ord(substr($string, $i, 1))>129) 
                { 
                    $tmpstr.= substr($string, $i, 2); 
                } 
                else 
                { 
                    $tmpstr.= substr($string, $i, 1); 
                } 
            } 
            if(ord(substr($string, $i, 1))>129) $i++; 
        } 
        if(strlen($tmpstr)< $strlen ) $tmpstr.= ".."; 
        return $tmpstr; 
    } 
} 
function substrgb_no($string, $start = 0, $sublen, $code = 'UTF-8') 
{ 
    if($code == 'UTF-8') 
    { 
        $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; 
        preg_match_all($pa, $string, $t_string); 
 
        if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen)).""; 
        return join('', array_slice($t_string[0], $start, $sublen)); 
    } 
    else 
    { 
        $start = $start*2; 
        $sublen = $sublen*2; 
        $strlen = strlen($string); 
        $tmpstr = ''; 
 
        for($i=0; $i< $strlen; $i++) 
        { 
            if($i>=$start && $i< ($start+$sublen)) 
            { 
                if(ord(substr($string, $i, 1))>129) 
                { 
                    $tmpstr.= substr($string, $i, 2); 
                } 
                else 
                { 
                    $tmpstr.= substr($string, $i, 1); 
                } 
            } 
            if(ord(substr($string, $i, 1))>129) $i++; 
        } 
        if(strlen($tmpstr)< $strlen ); 
        return $tmpstr; 
    } 
}

function clear_br($string)
{
		$pregfind = array(
		"/<br.*>/siU"
		);
	$pregreplace = array(
		''
	);
	$string = preg_replace($pregfind, $pregreplace, $string);
  return $string; 
}

//??????id
function getinid()
{
		$T_Time		=	gmdate(date(U));	//???????
 		$I_Num		=	rand(0,99999);		//?????
 		$getinid	=	"$T_Time"."$I_Num";
		return $getinid;
}



//????get_sux()??????
function get_sux($file_name)
{
 	$file_name		=	explode(".",$file_name);
	$file_len=count($file_name)-1;     
	$sux	=strtolower($file_name[$file_len]);
	return $sux;
}

//????get_sux()??????
function get_filename($file_name,$sux)
{
 	$file_name		=	explode(".$sux",$file_name);
	return $file_name[0];
}

//?????
function build_path($file_path)
{
 	$intime_mulu	=	Date("Y-m-d H:i:s");
 	$intime_mulu	=	substr($intime_mulu,0,4);
 	$file_path=$file_path.$intime_mulu;
 	if(!file_exists($file_path))
	      {
	            mkdir($file_path,0777);
	      }
 return $intime_mulu;
 
}
//????????????????
function check_date($date) { 
    $dateArr = explode("-", $date);
    if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2])) {
        return checkdate($dateArr[1],$dateArr[2],$dateArr[0]);
    }
    return false;
}

//???????
//---------------------------------------------------------------------------------------------------------
function parse_template($tplfile,$tlp_panth,$in_panth='')
{
	
	$objfile = $tlp_panth.$tplfile.'.tpl.php';
	$tplfile = $in_panth.$tplfile.'.html';
	$key_panth	=	explode("/tpl",$tlp_panth);	
	$cache_key = $key_panth[0]."/upload/cache_key.txt"; //??????????
  if(!file_exists($cache_key) or !file_exists($objfile) )//????建????? ?? ????????? ??????????????
  {
		$template = sreadfile($tplfile);	
		if(empty($template)){
			exit("Template file : $tplfile Not found or have no access!");
		} 
		//PHP????
		$template = preg_replace("/\<\!\-\-\{eval\s+(.+?)\s*\}\-\-\>/ies", "evaltags('\\1')", $template);
		//??????????
		$var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)";
		$template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
		$template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);
		$template = preg_replace("/(\\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\.([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/s", "\\1['\\2']", $template);
		$template = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s", "<?=\\1?>", $template);
		$template = preg_replace("/$var_regexp/es", "addquote('<?=\\1?>')", $template);
		$template = preg_replace("/\<\?\=\<\?\=$var_regexp\?\>\?\>/es", "addquote('<?=\\1?>')", $template);
		//????
		$template = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/s", "<?=\\1?>", $template);
		//????
		$template = preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template);
		swritefile($objfile, $template);
	}	
	return $objfile;
}

function evaltags($php) {
	global $_SGLOBAL;

	$_SGLOBAL['i']++;
	$search = "<!--EVAL_TAG_{$_SGLOBAL['i']}-->";
	$_SGLOBAL['block_search'][$_SGLOBAL['i']] = $search;
	$_SGLOBAL['block_replace'][$_SGLOBAL['i']] = "<?php ".stripvtags($php)." ?>";
	
	return $search;
}

function addquote($var){

	return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));
}

//??????????
function sreadfile($filename) {
	$content = '';
	if(@$fp = fopen($filename, 'r'))
	{
			@$content = fread($fp, filesize($filename));
			@fclose($fp);
	}	
	return $content;
}

//д?????
function swritefile($filename, $writetext, $openmod='w') {
	if(@$fp = fopen($filename, $openmod)) {
		flock($fp, 2);
		fwrite($fp, $writetext);
		fclose($fp);
		return true;
	} else {
		echo  "File: $filename write error.";
		return false;
	}
}
//-----------------------------------------------------------------------------------------------------------------


//????top_bottom
function  include_top_bottom($in_path)
{
	global $db,$matthew_top,$matthew_bottom;	
	$matthew_top=get_templet($in_path.'index_top_g.html');
	$matthew_bottom=get_templet($in_path.'index_bottom_g.html');
	//include 'check_sub.php';	
	include 'top_bottom.php';	
}
//提示内容/ 跳转链接 空值返回历史/ 跳转等待时间/ 模板路径/ 图标类型 ok error tishi info1 info2 info3 / 
function web_echo($ttishi,$tlink,$ttime,$ttemplete,$ico)
{
	$temp_root=$ttemplete;
	$fgdialogpage	=	$ttemplete."/php/updata_g.html";
	$ttemplete	=	get_templet($fgdialogpage);	
	if($tlink!=''){	
		$tjsecho="wopen(\'$tlink\');";
		$tmod="<meta HTTP-EQUIV=\"refresh\" CONTENT=\"$ttime;URL=$tlink \">";
		}else{
		$tjsecho='window.history.back();';
		$tmod='';
		}	
	$showdialogs=$tmod."	
	<SCRIPT src=\"$temp_root/js/ShowMags.js\" type=\"text/javascript\"></SCRIPT>	
	<script type=\"text/JavaScript\">
	function ShowBox(){
	ShowMags('$ico','$ttishi','$tjsecho','$temp_root');
	}
	window.onload=function(){
	ShowBox();
	}
	</script>"; 	
	$bodyTXT="请稍等！数据更新中。。。。。。";	
	$ttemplete= ereg_replace("{matthew_meta}",$showdialogs,$ttemplete);
	$ttemplete= ereg_replace("{matthew_body}",$bodyTXT,$ttemplete);
 	//return $ttemplete; 
 	echo  $ttemplete;
}	

function A_bao_han_B($param,$param_sub)   
{ 
	if(strpos($param,$param_sub)   !==   false) 
	{ 
		return   "yes"; 
	} 
	return  'no'; 
} 

//js??????
function js_go_url($url)
{
 	echo '<script language="javascript">';
	echo ' location="'.$url.'";';
	echo '</script>';
}


 //计算出给出的日期是星期几
function GetWeekDay($date) 
{ 
    $dateArr = explode("-", $date);
    return date("w", mktime(0,0,0,$dateArr[1],$dateArr[2],$dateArr[0]));
}
//转换星期为大写
function daxie_weekday($num)
{
    $week = array(1  => "一",
											2  => "二",
											3  => "三",
											4  => "四",
											5  => "五",
											6  => "六",
											7  => "日");
   return $week[$num];    
}

?>