<?php
	function go_url($msgurl,$msgdefault="_self")
	{
		$str_url=$msgurl;
		if(substr($str_url,0,1)=="?") $str_url=WEB_SCRIPT_URL.$str_url;
		if($msgdefault=="_self"){
			Header("Location:".$str_url); 
		}else{
			echo "<script>window.open('".$str_url."','".$msgdefault."');</script>";
		}
		exit;
	}
	function set_log($msg_type,$msg_cont,$arr_parms=array(),$is_get=false){
		$str_dir=WEB_ROOT."/data/log/".$msg_type;
		dir_create($str_dir);
		$str_path=$str_dir."/".date("Y_m_d").".txt";
		$str_cont="";
		if(is_file($str_path)) $str_cont=file_get_contents($str_path).chr(10).chr(10);
		$str_cont.="===============".date("Y-m-d H:i:s")."===================";
		$str_cont.=chr(10).$msg_cont.chr(10)."-------------------parms---------------------";
		$str_referer="";
		if(isset($_SERVER["HTTP_REFERER"])) $str_referer=$_SERVER["HTTP_REFERER"];
		$arr_fields=array(
			"HTTP_REFERER"=>$str_referer,
			"HTTP_USER_AGENT"=>$_SERVER["HTTP_USER_AGENT"],
			"REMOTE_ADDR"=>$_SERVER["REMOTE_ADDR"],
			"SCRIPT_FILENAME"=>$_SERVER["SCRIPT_FILENAME"],
			"REQUEST_METHOD"=>$_SERVER["REQUEST_METHOD"],
			"QUERY_STRING"=>$_SERVER["QUERY_STRING"],
		);
		foreach($arr_fields as $item=>$key){
			$str_cont.=chr(10).$item." => ".$key;
		}
		foreach($arr_parms as $item=>$key){
			$str_cont.=chr(10).$item." => ".$key;
		}
		if($is_get){
			$str_cont.=chr(10)."-------------------GET---------------------";
			foreach($_GET as $item=>$key){
				$str_cont.=chr(10).$item."=>".$key;
			}
			$str_cont.=chr(10)."-------------------POST---------------------";
			foreach($_POST as $item=>$key){
				$str_cont.=chr(10).$item."=>".$key;
			}
		}
		file_put_contents($str_path,$str_cont);
	}
//----------Is---函数
	function is_date($msgval)
	{
		list($year, $month, $day) = sscanf($msgval, '%d-%d-%d');
		$strx=checkdate($month, $day, $year);
		return $strx;
	}
	function is_pic($msgval,$msgext="")
	{
		if($msgval==""&&$msgext=="") return false;
		$str_ext=$msgext;
		if($str_ext=="") $str_ext=get_file_ext($msgval);
		if(in_array(strtolower($str_ext),array("jpg","jpeg","gif","png","bmp"))){
			return true;
		}else{
			return false;
		}
	}
	function is_domain($msg_url){
		if(preg_match('/^([a-z-0-9_-]*\.){0,1}[a-z0-9_-]+\.[a-z]{2,3}$/is',$msg_url)){
			return true;
		}else{
			return false;
		}
	}
	function is_flash($msgval,$msgext="")
	{
		if($msgval==""&&$msgext=="") return false;
		$str_ext=$msgext;
		if($str_ext=="") $str_ext=get_file_ext($msgval);
		if(in_array($str_ext,array("swf"))){
			return true;
		}else{
			return false;
		}
	}
	function is_medio($msgval,$msgext="")
	{
		if($msgval==""&&$msgext=="") return false;
		$str_ext=$msgext;
		if($str_ext=="") $str_ext=get_file_ext($msgval);
		if(in_array($str_ext,array("mp3","flv","rm","wmv"))){
			return true;
		}else{
			return false;
		}
	}
	function is_assoc($arr) 
	{ 
		if(!is_array($arr)) return false;
		return array_keys($arr) !== range(0, count($arr) - 1); 
	} 

//---------new---函数
function new_stripslashes($string)
{
	if(!is_array($string)) return stripslashes($string);
	foreach($string as $key => $val) $string[$key] = new_stripslashes($val);
	return $string;
}
function new_addslashes($string, $force = 0) {
	!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
	if(!MAGIC_QUOTES_GPC || $force) {
		if(is_array($string)) {
			foreach($string as $key => $val) {
				$string[$key] = new_addslashes($val, $force);
			}
		} else {
			$string = addslashes($string);
		}
	}
	return $string;
}

//----------Get---函数
	function get_query_key(){
		$arr_x=array_unique($_GET+$_POST);
		ksort($arr_x);
		$str_x=serialize($arr_x);
		$arr_key=md5($str_x);
		return $arr_key;
	}
	function get_memory_size(){
		$lng_val=format_size(memory_get_usage(true));
		return $lng_val;
	}
	function get_date($date,$t='d',$is_lng=0,$n=0)
	{
		if($t=='d'){
		   $firstday = date('Y-m-d 00:00:00',strtotime("$n day"));
		   $lastday = date("Y-m-d 23:59:59",strtotime("$n day"));

		}elseif($t=='w'){
		   if($n!=0){$date = date('Y-m-d',strtotime("$n week"));}
		   $lastday = date("Y-m-d 23:59:59",strtotime("$date Sunday"));
		   $firstday = date("Y-m-d 00:00:00",strtotime("$lastday -6 days"));

		}elseif($t=='m'){
		   if($n!=0){$date = date('Y-m-d',strtotime("$n months"));}
		   $firstday = date("Y-m-01 23:59:59",strtotime($date));
		   $lastday = date("Y-m-d 00:00:00",strtotime("$firstday +1 month -1 day"));
		}
		if($is_lng==1){
			$firstday=strtotime($firstday);
			$lastday=strtotime($lastday);
		}
		return array($firstday,$lastday);
	}
    //根据，根路径，与目录，取得当前路径
	function get_arr_id($arr_id)
	{
		$str_id="";
		if(is_array($arr_id)){
			$arr_x=array();
			foreach($arr_id as $item=>$key)
			{
				$arr_x[]=intval($key);
			}
			$str_id=implode(",",$arr_x);
		}else if($arr_id!=""){
			$arr_id=explode(",",$arr_id);
			$lng_count=count($arr_id);
			if($lng_count>0){
				for($i=0;$i<$lng_count;$i++)
				{
					$arr_id[$i]=intval($arr_id[$i]);
				}
				$str_id=implode(",",$arr_id);
			}
		}
		return $str_id;
	}
	function get_select_html($msg_list,$msg_val=""){
		$arr_list=$msg_list;
		if(!is_array($arr_list)) return "";
		$str_return="";
		if(is_assoc($arr_list)){
			foreach($arr_list as $item=>$key){
				$str_sel="";
				if($key==$msg_val) $str_sel=" selected";
				$str_return.="<option value='".$key."'".$str_sel.">".$item."</option>";
			}
		}else{
			foreach($arr_list as $item){
				$str_sel="";
				if($item==$msg_val) $str_sel=" selected";
				$str_return.="<option value='".$item."'".$str_sel.">".$item."</option>";
			}
		}
		return $str_return;
	}
	function get_radio_html($msg_list,$msg_name,$msg_val="",$msg_sp=" "){
		$arr_list=$msg_list;
		if(!is_array($arr_list)) return "";
		$str_return="";
		$i=0;
		if(is_assoc($arr_list)){
			foreach($arr_list as $item=>$key){
				$str_sel="";
				if($key==$msg_val||$i==0) $str_sel=" checked";
				$str_return.="<input type='radio' name='".$msg_name."' value='".$key."'".$str_sel.">".$item.$msg_sp;
				$i++;
			}
		}else{
			foreach($arr_list as $item){
				$str_sel="";
				if($item==$msg_val||$i==0) $str_sel=" checked";
				$str_return.="<input type='radio' name='".$msg_name."' value='".$item."'".$str_sel.">".$item.$msg_sp;
				$i++;
			}
		}
		return $str_return;
	}
	function get_checkbox_html($msg_list,$msg_name,$msg_val=""){
		$arr_list=$msg_list;
		if(!is_array($arr_list)) return "";
		$arr_val=explode(",",$msg_val);
		$str_return="";
		if(is_assoc($arr_list)){
			foreach($arr_list as $item=>$key){
				$str_sel="";
				if(in_array($key,$arr_val)) $str_sel=" checked";
				$str_return.="<input type='checkbox' name='".$msg_name."[]' value='".$key."'".$str_sel.">".$item." ";
			}
		}else{
			foreach($arr_list as $item){
				$str_sel="";
				if(in_array($item,$arr_val)) $str_sel=" checked";
				$str_return.="<input type='checkbox' name='".$msg_name."[]' value='".$item."'".$str_sel.">".$item." ";
			}
		}
		return $str_return;
	}
	function get_real_path($msgroot,$msgpath)
	{
		$str_path=format_path($msgpath);
		($msgroot=="")?$msgroot=format_path(WEB_ROOT):$msgroot=format_path($msgroot);
		if(strlen($str_path)>strlen($msgroot))
		{
			$str_x=substr($str_path,0,strlen($msgroot));
			if($str_x!=$msgroot)
			{
				if(substr($str_path,0,1)!="/") $str_path="/".$str_path;
				$str_path=$msgroot.$str_path;
			}
		}else{
			if(substr($str_path,0,1)!="/") $str_path="/".$str_path;
			$str_path=$msgroot.$str_path;
		}
		if(substr($str_path,-1,1)=="/") $str_path=substr($str_path,0,-1);
		return $str_path;

	}
	function get_html_url($msg_url){
		if(!isset($msg_url)||$msg_url=="") return "";
		$msg_url=strtolower($msg_url);
		if(strstr($msg_url,"http://")){
			return $msg_url;
		}else{
			$arr_x=explode("/",$msg_url);
			if(preg_match("/[a-z0-9_-]+[.]{1}[a-z]{2,3}/is",$arr_x[0])){
				return "http://".$msg_url;
			}else{
				if(substr($msg_url,0,1)!="/") $msg_url="/".$msg_url;
				return WEB_URL.$msg_url;
			}
		}
	}
	function get_pages($total, $page = 1, $int_pagesize = 20)
	{
		if($total < 1) return '';
		$pages = ceil($total/$int_pagesize);
		$page = min($pages, $page);
		$prepg = $page-1;
		$nextpg = $page == $pages ? 0 : ($page+1);
		$str_left="";
		$str_right="";
		$pagenav ="<span class='info'>总数：<font color='#ff6600'>".$total."</font> 条&nbsp;页 $page/$pages</span>";
		$str_x="";
		if($pages>10){
			if($page>5){
				$lng_pre=$page-5;
				$lng_next=$page+5;
				$str_left="<li><a href='javascript:frmPage(1);'>首页</a></li>";
				$str_right="<li><a href='javascript:frmPage(".$pages.");'>尾页</a></li>";
			}else{
				$lng_pre=1;
				$lng_next=10;
				$str_right="<li><a href='javascript:frmPage(".$pages.");'>尾页</a></li>";
			}
		}else{
			$lng_pre=1;
			$lng_next=$pages;
		}
		if($lng_next>=$pages){
			$lng_next=$pages;
			$str_right="";
		}
		for($i=$lng_pre;$i<=$lng_next;$i++){
			$str_sel="";
			if($i==$page) $str_sel=" class='x_sel'";
			$str_x.="<li".$str_sel."><a href='javascript:frmPage(".$i.");'>[".$i."]</a></li>";
		}
		$pagenav.=$str_left.$str_x.$str_right;
		return $pagenav;
	}
	function get_pages_html($total, $page = 1, $int_pagesize = 20,$strpage="")
	{
		if($total < 1) return '';
		$pages = ceil($total/$int_pagesize);
		$page = min($pages, $page);
		$prepg = $page-1;
		$nextpg = $page == $pages ? 0 : ($page+1);

		if(strstr($strpage,"?"))
		{
			if(substr($strpage,-1)=="?"){
				$strpage.="1=1";
			}
		}else{
			$strpage.="?1=1";
		}
		$toppage=$strpage."&page=1";
		$endpage=$strpage."&page=".$pages;
		$prepage=$strpage."&page=".$prepg;
		$nextpage=$strpage."&page=".$nextpg;
		$pagenav ="<span class='info'>总数：<font color='#ff6600'>".$total."</font> 条&nbsp;页 $page/$pages</span>";
		$str_x="";
		if($pages>10){
			if($page>5){
				$lng_pre=$page-5;
				$lng_next=$page+5;
			}else{
				$lng_pre=1;
				$lng_next=10;
			}
		}else{
			$lng_pre=1;
			$lng_next=$pages;
		}
		for($i=$lng_pre;$i<=$lng_next;$i++){
			$str_sel="";
			if($i==$page) $str_sel=" class='x_sel'";
			$str_x.="<li".$str_sel."><a href='".$strpage."&page=".$i."'>".$i."</a></li>";
		}
		$pagenav.=$str_x;
		return $pagenav;
	}
	function get_request($msg_var)
	{
		if(isset($_REQUEST[$msg_var])){
			return $_REQUEST[$msg_var];
		}else{
			return "";
		}
	}
	function get_editor($textareaid = 'content', $msgtype="",$msg_w=0,$msg_h=0)
	{
		if($msgtype=="admin"){
			$toolbar="full";
			$width=750;
			$height=400;
		}else if($msgtype=="user"){
			$toolbar="standard";
			$width=750;
			$height=400;
		}else{
			$toolbar="basic";
			$width=750;
			$height=400;
		}
		if($msg_w>0) $width=$msg_w;
		if($msg_h>0) $height=$msg_h;
		return "\n<script type=\"text/javascript\" src=\"".WEB_URL."/plus/fckeditor/fckeditor.js\"></script>\n<script language=\"JavaScript\" type=\"text/JavaScript\">var SiteUrl = \"".WEB_URL."\"; var Module = \"special\"; var sBasePath = \"".WEB_URL."\" + '/plus/fckeditor/'; var oFCKeditor = new FCKeditor( '".$textareaid."' ) ; oFCKeditor.BasePath = sBasePath ; oFCKeditor.Height = '".$height."'; oFCKeditor.Width	= '".$width."' ; oFCKeditor.ToolbarSet	= '".$toolbar."' ;oFCKeditor.ReplaceTextarea() ;</script>";
		/*return "<iframe id='frm".$textareaid."' src='".WEB_URL."/plus/webedit/?obj=".$textareaid."' frameborder=0 scrolling='no' style='width:".$width."px;height:".$height."px'></iframe>";*/
	}
	function get_remote_content($url)
	{
		$str_val="";
		if(exists_remote_file($url))
		{
			$str_val=file_get_contents($url);
		}
		return $str_val;
	}
	function get_pwd($msgpwd,$msgkey="")
	{
		if($msgkey!=""){
			$msgpwd=$msgpwd.$msgkey;
		}
		return md5($msgpwd);
	}
	//获取用户ip
	function get_user_agent_ip() {
		$ip = $_SERVER['REMOTE_ADDR'];   
        return $ip;   
    }   
	function get_user_ip() {   
        if (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP']!='unknown') {   
            $ip = $_SERVER['HTTP_CLIENT_IP'];   
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']!='unknown') {   
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];   
        } else {   
            $ip = $_SERVER['REMOTE_ADDR'];   
        }   
        return $ip;   
    }

	function get_cookie($key)
	{
		if(defined('WEB_COOKIE_PRE')) $key=WEB_COOKIE_PRE.$key;
		if(isset($_COOKIE[$key])) return $_COOKIE[$key];
		return "";
	}
	//获取网卡信息,功能还未完成
	function get_network(){
		@exec("   ipconfig/all   >   ip.dat");   
		$strx=file_get_contents("ip.dat");
	}
	function get_cache_pic($str_pic,$lng_w,$lng_h,$msg_dir)
	{
		if($str_pic=="")return "";
		$str_return="";
		$str_old_pic=$str_pic;
		//网络图片
		$str_picname=basename($str_pic);
		if(substr($msg_dir,1,1)!="/") $msg_dir="/".$msg_dir;
		$msg_dir="/cache".$msg_dir;
		$str_path=get_real_path(WEB_ROOT,$msg_dir);
		$lng_x=strlen($str_picname);
		$str_dir_path=substr($str_pic,0,-$lng_x);
		$str_pic=strtolower($str_pic);
		if(strstr($str_pic,"http://")){
			$str_dir_path=format_path(str_replace(".","",substr($str_dir_path,7,strlen($str_dir_path))));
			$str_dir=$str_path."/".$str_dir_path;
			$str_url=WEB_URL.$msg_dir."/".$str_dir_path.$str_picname;
			$str_path=$str_dir.$str_picname;
			if(!is_file($str_path)){
				if(!exists_remote_file($str_old_pic)) return "";
				echo $str_path;exit;
				dir_create($str_dir);
				$str_data=file_get_contents($str_pic);
				file_put_contents($str_path,$str_data);
				$str_pic=$str_path;
			}else{
				return $str_url;
			}
		}else{
			$str_pic=get_real_path(WEB_ROOT,$str_pic);
			if(!is_file($str_pic)) return "";
			$str_dir=$str_path."/".$str_dir_path;
			$str_path=$str_dir.$str_picname;
			$str_url=WEB_URL.format_path($msg_dir."/".$str_dir_path.$str_picname);
			$str_dir=format_path($str_dir);
			$str_path=format_path($str_path);
			if(!is_file($str_path)){
				dir_create($str_dir);
			}else{
				return $str_url;
			}
		}

		$obj_cls_pic=new cls_pic();
		$str_return=$obj_cls_pic->on_resize($str_pic,$lng_w,$lng_h,$str_path);
		return $str_url;
	}
	function get_sql_request($msg){
		if(isset($_GET["$msg"])){
			return mysql_real_escape_string($_GET["$msg"]);
		}else{
			return "";
		}
	}
	function get_dir_name($msg_file,$msg_x=0){
		$str_file=str_replace("\\","/",$msg_file);
		$arr_file=explode("/",$str_file);
		$int_x=count($arr_file)-1-$msg_x;
		return $arr_file[$int_x];
	}
//----------chk-------函数
	function chk_dir_name($msgname)
	{
		if($msgname==""){
			$arr_return["err"]="目录名不能为空！";
			return $arr_return;
		}
		if(strlen($msgname)>25){
			$arr_return["err"]="目录名长度不能超过25个字节";
			return $arr_return;
		}
		if (!preg_match('/^[\xa1-\xffA-Za-z0-9_.]+$/', $msgname)){
			$arr_return["err"]="目录名只能包函[数字、字母、中文、下划线]";
			return $arr_return;
		}
		return "";
	}
	function chk_file_name($msgname)
	{
		$arr_return=array("err"=>"");
		if($msgname==""){
			$arr_return["err"]="文件名不能为空！";
			return $arr_return;
		}
		if(strlen($msgname)>25){
			$arr_return["err"]="文件名长度不能超过25个字节";
			return $arr_return;
		}
		if (!preg_match('/^['.chr(0xa1).'-'.chr(0xff).'A-Za-z0-9_.-]+$/', $msgname)){
			$arr_return["err"]="文件名只能包函[数字、字母、中文、下划线]";
			return $arr_return;
		}
		return $arr_return;
	}
	function chk_uname($msguname)
	{
		$arr_return=array("err"=>"");
		if(!isset($msguname)){
			$arr_return["err"]="用户名不能为空！";
			return $arr_return;
		}
		if(strlen($msguname)<3){
			$arr_return["err"]="用户名长度不能小于3个字符！";
			return $arr_return;
		}
		if(strlen($msguname)>50){
			$arr_return["err"]="用户名长度不能大于25个字符！";
			return $arr_return;
		}
		return $arr_return;
	}
	function chk_pwd($msgpwd)
	{
		$arr_return=array("err"=>"");
		if(!isset($msgpwd)){
			$arr_return["err"]="密码不能为空！";
			return $arr_return;
		}
		if(strlen($msgpwd)<3){
			$arr_return["err"]="密码长度不能小于3个字符！";
			return $arr_return;
		}
		if(strlen($msgpwd)>50){
			$arr_return["err"]="密码长度不能大于25个字符！";
			return $arr_return;
		}
		return $arr_return;
	}
	function chk_tel($msgtel)
	{
		if(!preg_match("/^[0-9_-]{7,13}$/",$msgtel)) return "请输入正确号码！";
		return "";
	}

	function chk_email($msgemail)
	{
		if(!preg_match("/^[a-zA-Z0-9]+([.a-zA-Z0-9_-])*@([a-zA-A0-9_-])+(.[a-zA-Z0-9_-]+)+[a-zA-Z0-9_-]$/",$msgemail)){
			return "输入邮箱不正确！";
		}
	}
	function chk_referer($msg_domain="",$msg_act="")
	{
		$get_act=$_SERVER["REQUEST_METHOD"];
		$get_referer=$_SERVER["HTTP_REFERER"];
		if($get_referer=="") return false;
		$arr_domain=array();
		$arr_act=array();
		if(is_array($msg_domain)){
			$arr_domain=$msg_domain;
			if(count($arr_domain)<1) $arr_domain[]=WEB_DOMAIN;
		}else{
			$arr_domain[]=WEB_DOMAIN;
		}
		if(is_array($msg_act)){
			$arr_act=$msg_act;
			if(count($arr_act)<1) $arr_act=array("GET","POST");
		}else{
			$arr_act=array("GET","POST");
		}
		if(in_array($get_act,$arr_act)){
			$arr_url=parse_url($get_referer);
			$str_domain=$arr_url["host"];
			if(!in_array($str_domain,$arr_domain)){
				return false;
			}
		}
		return true;
	}
	function chk_escape($str)
	{
		switch (gettype($str))
		{
			case 'string'	:	$str = chk_escape_str($str);
			break;
			case 'boolean'	:	$str = ($str === FALSE) ? 0 : 1;
			break;
			default			:	$str = ($str === NULL) ? 'NULL' : $str;
			break;
		}

		return $str;
	}
	function chk_escape_str($str)	
	{	
		// chk_escape single quotes
		//$str=str_replace("'", "''", $str);
		$str=mysql_real_escape_string($str);
		return $str;
	}
	function chk_match($msg_val,$arr_filter){
		if(!is_array($arr_filter)){
			if($arr_filter=="") return false;
			$str_val=$arr_filter;
		}else{
			$str_val="(".implode("|",$arr_filter).")+";
		}
		if(preg_match("/".$str_val."/is",$msg_val)){
			return true;
		}else{
			return false;
		}
	}
//----------format----函数
   function format_url_query($arr_x){
	   $str_url=$_SERVER["SCRIPT_NAME"];
	   $str_param=$_SERVER["QUERY_STRING"];
	   $arr_param=explode("&",$str_param);
	   $arr_keys=array_keys($arr_x);
	   $lng_count=count($arr_param);
	   for($i=0;$i<$lng_count;$i++){
		    if(empty($arr_param[$i])){
				unset($arr_param[$i]);
				continue;
			}
			$arr_y=explode("=",$arr_param[$i]);
			if(in_array($arr_y[0],$arr_keys)){
				if($arr_x[$arr_y[0]]!=""){
					$arr_param[$i]=$arr_y[0]."=".urlencode($arr_x[$arr_y[0]]);
				}else{
					unset($arr_param[$i]);
				}
				unset($arr_x[$arr_y[0]]);
			}
	   }
	   foreach($arr_x as $item=>$key){
		   if($key!="") $arr_param[]=$item."=".urlencode($key);
	   }
	   $str_url.="?".implode("&",$arr_param);
	   return $str_url;
   }
   function format_filter($msg_val,$arr_filter){
		if($msg_val=="") return $msg_val;
		if(is_assoc($arr_filter)){
			$arr_keys=array_keys($arr_filter);
			$arr_values=array_values($arr_filter);
		}else{
			if(!is_array($arr_filter)&&$arr_filter=="") return $msg_val;
			$arr_keys=$arr_filter;
			$arr_values="";
		}
		$str_val=str_replace($arr_keys,$arr_values,$msg_val);
		return $str_val;
   }
   function format_ip_int($ip) {   
      $ips = explode('.',$ip);
	  $int=0;
       if (count($ips)>=4) {   
            $int = $ips[0]*256*256*256+$ips[1]*256*256+$ips[2]*256+$ips[3];   
        }
       return $int;   
    }   
   function format_ip($ip) {   
      $ips = explode('.',$ip);
       if (count($ips)>=4) {   
            $ip = $ips[0].".".$ips[1].".*.*";   
        }
       return $ip;   
    }   
	function format_path($msg_path)
	{
		if($msg_path=="") return "";
		$str_path=str_replace("\\","/",$msg_path);
		while(strstr($str_path,"//"))
		{
			$str_path=str_replace("//","/",$str_path);
		}
		if(substr($str_path, -1,1) == '/') $str_path = substr($str_path,0,-1);
		return $str_path;
	}
	function format_len($string,$length,$msg_add="") { 
		$wordscut="";
		if(strlen($string) > $length) { 
			for($i = 0; $i < $length; $i++) { 
				if(ord($string[$i]) > 127) { 
					$wordscut .= $string[$i].$string[$i + 1]; 
					$i++; 
				} else { 
					$wordscut .= $string[$i]; 
				} 
			} 
			return $wordscut.$msg_add; 
		} 
		return $string; 
	}
	function format_date($msg_date,$msg_style='Y-m-d')
	{
		$date_val=date($msg_style,$msg_date);
		return $date_val;
	}
	function format_date_int($msg_date)
	{
		$date_val=strtotime($msg_date);
		return $date_val;
	}

	function format_number($msgval)
	{
		return floatval($msgval);
	}
	function format_html($str){
		if($str!="")
		{
			$str=str_replace(" ","&nbsp;",$str);
			//$str=str_replace(chr(10),"<br>",$str);
			$str=str_replace(chr(13),"<br>",$str);
		}
		return $str; 
	} 

	function format_filesize($msgsize,$msg_sign="M",$msg_sign2="B")
	{
		$msgsize=format_number($msgsize);
		$msg_sign=strtoupper($msg_sign);
		$msg_sign2=strtoupper($msg_sign2);
		if($msg_sign==$msg_sign2) return $msgsize;
		$int_bytes=$msgsize;
		$int_size=0;
		if(!$msg_sign2!="B")
		{
			switch($msg_sign2)
			{
				case "K":
					$int_bytes=$msgsize*1024;
				case "M":
					$int_bytes=$msgsize*1024*1024;
				case "G":
					$int_bytes=$msgsize*1024*1024*1024;
			}
		}
		switch($msg_sign)
		{
			case 'K':
				$int_size=round($int_bytes/1024,2);
				break;
			case "M":
				$int_size=round($int_bytes/(1024*1024),2);
				break;
			case "G":
				$int_size=round($int_bytes/(1024*1024*1024),2);
				break;
		}
		return $int_size;
	}
	function format_size($size){ 
		$unit=array('B','K','M','G','T','P'); 
		return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; 
	} 
	function format_tohtml($msg_html)
	{
		$str_return=$msg_html;
		$str_return=str_replace(" ","&nbsp",$str_return);
		$str_return=nl2br($str_return);
		return $str_return;

	}
	function format_sql_safe($msg_x){
		$str_content=mysql_real_escape_string($msg_x);
		return $str_content;
	}
	function format_utf8_gbk($value) { 
		if(is_null($value)||empty($value)) return "";
		return mb_convert_encoding($value,"gbk","UTF-8"); 
	} 
	function format_gbk_utf8($value) { 
		if(is_null($value)||empty($value)) return "";
		return iconv("gbk", "UTF-8", $value); 
	} 
	function format_filename($msg_name){
		$str_name=$msg_name;
		$str_name=str_replace("\\","",$str_name);
		$str_name=preg_replace("/[\/:\"*><|?]*/is", "", $str_name);
		return $str_name;
	}
	function format_urlencode($msg_word){
		$msg_word=urlencode($msg_word);
		return $msg_word;
	}
	function format_request($str_x,$is_reback=false){
		if(is_array($str_x)){
			for($i=0;$i<count($str_x);$i++){
			$str_x[$i]=format_request_base($str_x[$i],$is_reback);
			}
		}else{
			$str_x=format_request_base($str_x,$is_reback);
		}
		return $str_x;
	}
	function format_request_base($str_x,$is_reback=false){
        if (function_exists('htmlspecialchars')) {
 			if($is_reback){
			  $str_x=htmlspecialchars_decode($str_x);
			}else{
			  $str_x=htmlspecialchars($str_x);
			}
			return $str_x;
        }
        $search = array(
            "&", 
            '"', 
            "'", 
            "<", 
            ">"
        );
        $replace = array(
            "&amp;", 
            "&quot;", 
            "&#039;", 
            "&lt;", 
            "&gt;"
        );
		if($is_reback){
			$str_x=str_replace($replace, $search, $str_x);
		}else{
			$str_x=str_replace($search, $replace, $str_x);
		}
		return $str_x;
	}
	//生成加密码字符串
	function get_safecode($msg_code){
		$str_code=$msg_code;
		if(defined("WEB_SAFECODE")){
			$str_code.=WEB_SAFECODE;
		}
		$str_code=md5($str_code);
		return $str_code;
	}
	//取本周第一天时间
	function get_toweek_time(){
		$toweek=date('w');
		$tomonth=date('n');
		$todate=date('j');
		$toyear=date('Y');
		$time=mktime(0,0,0,$tomonth,$todate,$toyear);
		$toweek=$toweek-1;
		if($toweek<0) $toweek=6;
		$toweek=60*60*24*$toweek;
		$time=$time-$toweek;
		return $time;
	}
	function get_content_pic($msg_content){
		//preg_match("/^[\x{4e00}-\x{9fa5}]*$/");  utf-8中文
		$arr_return=array();
		$str_content=$msg_content;
		if(stripos($str_content,"<img")){
			$str_content=preg_replace("/src=[\"']{0,1}([".chr(0xa1)."-".chr(0xff)."A-Za-z0-9\/:._-\s]*)[\"']{0,1}/is", "[img]\\1[/img]", $str_content);
			$arr_x=explode("[img]",$str_content);
			foreach($arr_x as $item){
				$arr_y=explode("[/img]",$item);
				if(count($arr_y)>1){
					$arr_return[]=$arr_y[0];
				}
			}
		}
		return $arr_return;
	}
//----------exists----函数
	function exists_remote_file($url_file)
	{
		//检测输入
		$url_file = trim($url_file);
		if (empty($url_file)) { return false; }
		$url_arr = parse_url($url_file);
		if (!is_array($url_arr) || empty($url_arr)){ return false; }

		//获取请求数据
		if(!isset($url_arr['host'])) return false;
		$host = $url_arr['host'];
		$path = $url_arr['path'];
		if(isset($url_arr['query'])) $path.="?".$url_arr['query'];
		$port = isset($url_arr['port']) ? $url_arr['port'] : "80";
		//连接服务器
		$fp = fsockopen($host, $port, $err_no, $err_str, 30);
		if (!$fp){ return false; }
		//构造请求协议
		$request_str = "GET ".$path." HTTP/1.1\r\n";
		$request_str .= "Host: ".$host."\r\n";
		$request_str .= "Connection: Close\r\n\r\n";

		//发送请求
		fwrite($fp, $request_str);
		$first_header = fgets($fp, 102400);
		fclose($fp);

		//判断文件是否存在
		if (trim($first_header) == ""){ return false; }
		if (!preg_match("/200/", $first_header)){
				return false;
		}
		return true;
	}
//----------set----函数
	function set_cookie($msg_name,$msg_val="",$msg_expire=30,$msg_path="/",$msg_domain=WEB_DOMAIN,$msg_httponly=false)
	{
		if(stristr(strtolower($msg_domain),"http:")){
			$arr_x=parse_url($msg_domain);
			$arr_y=explode(".",$arr_x["host"]);
			$lng_x=count($arr_y)-1;
			if($arr_x["host"]=="localhost" || is_numeric($arr_y[$lng_x]) ){
				$msg_domain="";
			}else{
				if($lng_x>1){
					$msg_domain=$arr_y[$lng_x-1].".".$arr_y[$lng_x];
				}else{
					$msg_domain=$arr_x["host"];
				}
			}
		}
		if(isset($_COOKIE["agent_time"]) && is_date($_COOKIE["agent_time"])){
			$int_expire=strtotime($_COOKIE["agent_time"]);
		}else if(isset($_SERVER["REQUEST_TIME"])){
			$int_expire=$_SERVER["REQUEST_TIME"];
		}else{
			$int_expire=TIME;
		}
		if($msg_expire>=1){
			//$msg_expire=$msg_expire-1;
			$int_expire=$int_expire+$msg_expire*86400;
		}else if($msg_expire>0){
			$int_expire=$int_expire+$msg_expire*10000;
		}

		$str_source = $_SERVER['SERVER_PORT'] == 443 ? 1 : 0;
		if(defined('WEB_COOKIE_PRE')) $msg_name=WEB_COOKIE_PRE.$msg_name;
		if(PHP_VERSION < '5.2.0') {
			setcookie($msg_name,$msg_val,$int_expire,$msg_path,$msg_domain,$str_source);
		}else{
			setcookie($msg_name,$msg_val,$int_expire,$msg_path,$msg_domain,$str_source,$msg_httponly);
		}
	}
	function safe_visit(){
		global $obj_db,$obj_js_msg;
		$str_from_url="http://".$_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"];
		$str_my_url=WEB_URL."/".basename($_SERVER["SCRIPT_NAME"]);
		if($str_from_url!=$str_my_url) return;
		$str_page=str_replace("/","_",$_SERVER["SCRIPT_NAME"]);
		if(isset($_SESSION["visit_".$str_page])){
			if($_SESSION["visit_".$str_page]==1) {
				$lng_time1=intval(TIME);
				$lng_time2=intval($_SESSION["visit_".$str_page."_time"]);
				if($lng_time1-$lng_time2<5){
					$lng_num=intval($_SESSION["visit_".$str_page."_num"]);
					//echo $lng_time1."<br>".$lng_time2;
					if($lng_num>0){
						$str_ip=get_user_ip();
						$sql="insert into ".DB_PRE."visit(visit_ip,visit_page,visit_addtime) values('".$str_ip."','".$str_page."','".TIME."')";
						$obj_db->execute($sql);
						$obj_js_msg->on_refresh();
						$_SESSION["visit_".$str_page."_time"]=TIME;
						exit;
					}else{
						$_SESSION["visit_".$str_page."_num"]=$lng_num+1;
					}
				}else{
					$_SESSION["visit_".$str_page."_num"]=0;
				}
			}
		}else{
			$_SESSION["visit_".$str_page]=1;
			$_SESSION["visit_".$str_page."_num"]=0;
		}
		$_SESSION["visit_".$str_page."_time"]=TIME;
	}

?>