<?php
error_reporting(0);

define('InEmpireCMS',TRUE);

$addgethtmlpath="";
$editor=0;

function db_connect(){
	global $phome_db_server,$phome_db_username,$phome_db_password,$phome_db_dbname,$phome_db_port,$phome_db_char,$phome_use_dbver;
	$dblocalhost=$phome_db_server;
	//端口
	if($phome_db_port)
	{
		$dblocalhost.=":".$phome_db_port;
	}
	$link=@mysql_connect($dblocalhost,$phome_db_username,$phome_db_password);
	if(!$link)
	{
		echo"Cann't connect to DB!";
		exit();
	}
	//编码
	if($phome_use_dbver>='4.1')
	{
		if($phome_db_char)
		{
			@mysql_query("SET names '".$phome_db_char."';");
		}
		if($phome_use_dbver>='5.0')
		{
			@mysql_query("SET sql_mode=''");
		}
	}
	@mysql_select_db($phome_db_dbname);
	return $link;
}

//设置编码
function DoSetDbChar($dbchar){
	if($dbchar)
	{
		@mysql_query("set names '".$dbchar."';");
	}
}

function db_close(){
	global $link;
	@mysql_close($link);
}

//设置COOKIE
function esetcookie($var,$val,$life=0){
	global $phome_cookiedomain,$phome_cookiepath,$phome_cookievarpre;
	return setcookie($phome_cookievarpre.$var,$val,$life,$phome_cookiepath,$phome_cookiedomain);
}

//返回cookie
function getcvar($var){
	global $phome_cookievarpre;
	$tvar=$phome_cookievarpre.$var;
	return $_COOKIE[$tvar];
}

//错误提示
function printerror($error="",$gotourl="",$ecms=0,$noautourl=0,$novar=0){
	global $empire,$editor,$ecmslang,$public_r;
	if($editor==1){$a="../";}
	elseif($editor==2){$a="../../";}
	elseif($editor==3){$a="../../../";}
	else{$a="";}
	if(strstr($gotourl,"(")||empty($gotourl))
	{
		$gotourl_js="history.go(-1)";
		$gotourl="javascript:history.go(-1)";
	}
	else
	{$gotourl_js="self.location.href='$gotourl';";}
	if(empty($error))
	{$error="DbError";}
	if($ecms==9)//弹出对话框
	{
		@include $a.LoadLang("pub/q_message.php");
		$error=empty($novar)?$qmessage_r[$error]:$error;
		echo"<script>alert('".$error."');".$gotourl_js."</script>";
		@db_close();
		$empire=null;
		exit();
	}
	if($ecms==0)
	{
		@include $a.LoadLang("pub/message.php");
		$error=empty($novar)?$message_r[$error]:$error;
		@include($a."message.php");
	}
	else
	{
		@include $a.LoadLang("pub/q_message.php");
		$error=empty($novar)?$qmessage_r[$error]:$error;
		@include($a."../message/index.php");
	}
	@db_close();
	$empire=null;
	exit();
}

//ajax错误提示
function ajax_printerror($error='isfail'){
	global $empire;
	echo $error;
	@db_close();
	$empire=null;
	exit();
}

//编码转换
function DoIconvVal($code,$targetcode,$str,$inc=0){
	global $editor;
	if($editor==1){$a="../";}
	elseif($editor==2){$a="../../";}
	elseif($editor==3){$a="../../../";}
	else{$a="";}
	if($inc)
	{
		@include_once("doiconv.php");
	}
	$iconv=new Chinese($a);
	$str=$iconv->Convert($code,$targetcode,$str);
	return $str;
}

//模板表转换
function GetTemptb($temptb){
	global $public_r,$ecmsdeftempid,$dbtbpre;
	if(!empty($ecmsdeftempid))
	{
		$tempid=$ecmsdeftempid;
	}
	else
	{
		$tempid=$public_r['deftempid'];
	}
	if(!empty($tempid)&&$tempid!=1)
	{
		$en="_".$tempid;
	}
	return $dbtbpre.$temptb.$en;
}

//返回操作模板表
function GetDoTemptb($temptb,$gid){
	global $dbtbpre;
	if(!empty($gid)&&$gid!=1)
	{
		$en="_".$gid;
	}
	return $dbtbpre.$temptb.$en;
}

//返回当前使用模板组ID
function GetDoTempGid(){
	global $ecmsdeftempid,$public_r;
	if($ecmsdeftempid)
	{
		$gid=$ecmsdeftempid;
	}
	elseif($public_r['deftempid'])
	{
		$gid=$public_r['deftempid'];
	}
	else
	{
		$gid=1;
	}
	return $gid;
}

//导入语言包
function LoadLang($file){
	global $ecmslang;
	return "../data/language/".$ecmslang."/".$file;
}

//取得IP
function egetip(){
	if(getenv('HTTP_CLIENT_IP')&&strcasecmp(getenv('HTTP_CLIENT_IP'),'unknown')) 
	{
		$ip=getenv('HTTP_CLIENT_IP');
	} 
	elseif(getenv('HTTP_X_FORWARDED_FOR')&&strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),'unknown'))
	{
		$ip=getenv('HTTP_X_FORWARDED_FOR');
	}
	elseif(getenv('REMOTE_ADDR')&&strcasecmp(getenv('REMOTE_ADDR'),'unknown'))
	{
		$ip=getenv('REMOTE_ADDR');
	}
	elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],'unknown'))
	{
		$ip=$_SERVER['REMOTE_ADDR'];
	}
	return preg_replace("/^([\d\.]+).*/", "\\1",$ip);
}

//返回地址
function DoingReturnUrl($url,$from=''){
	if(empty($from))
	{
		return $url;
	}
	elseif($from==9)
	{
		$from=$_SERVER['HTTP_REFERER']?$_SERVER['HTTP_REFERER']:$url;
	}
	return $from;
}

//参数处理函数
function RepPostVar($val){
	if($val!=addslashes($val))
	{
		exit();
	}
	$val=str_replace(" ","",$val);
	$val=str_replace("%20","",$val);
	$val=str_replace("*","",$val);
	$val=str_replace("'","",$val);
	$val=str_replace("\"","",$val);
	$val=str_replace("/","",$val);
	$val=str_replace(";","",$val);
	$val=addslashes(stripSlashes($val));
	return $val;
}

//处理提交字符
function RepPostStr($val){
	$val=htmlspecialchars($val);
	return $val;
}

//-------------------取得文件扩展名
function GetFiletype($filename){
	$filer=explode(".",$filename);
	$count=count($filer)-1;
	return strtolower(".".$filer[$count]);
}

//取得文件名
function GetFilename($filename){
	if(strstr($filename,"\\"))
	{
		$exp="\\";
	}
	else
	{
		$exp='/';
	}
	$filer=explode($exp,$filename);
	$count=count($filer)-1;
	return $filer[$count];
}

//---------------------------字符截取函数2
function sub($Modi_Str,$start,$length,$mode = false){ 
	$n = 0;
	for($i=0;$i<$start;$i++){ 
		if(ord(substr($Modi_Str,$i,1))>0xa0){
			if($mode){
				$start++;
				$i++;
			}
			$n++;
		}
	}
	if(!$mode)$start = $start + $n%2;
	$The_length = $start+$length;
	for($i=$start;$i<$The_length;$i++){ 
		if(ord(substr($Modi_Str,$i,1))>0xa0){ 
			$The_Str.=substr($Modi_Str,$i,2); 
			$i++;
			if($mode) $The_length++;
		}else{ 
			$The_Str.=substr($Modi_Str,$i,1); 
		}
	}
	return $The_Str;
}

//取得随机数
function make_password($pw_length){
	$low_ascii_bound=50;
	$upper_ascii_bound=122;
	$notuse=array(58,59,60,61,62,63,64,73,79,91,92,93,94,95,96,108,111);
	while($i<$pw_length)
	{
		mt_srand((double)microtime()*1000000);
		$randnum=mt_rand($low_ascii_bound,$upper_ascii_bound);
		if(!in_array($randnum,$notuse))
		{
			$password1=$password1.chr($randnum);
			$i++;
		}
	}
	return $password1;
}

//取得随机数(数字)
function no_make_password($pw_length){
	$low_ascii_bound=48;
	$upper_ascii_bound=57;
	$notuse=array(58,59,60,61,62,63,64,73,79,91,92,93,94,95,96,108,111);
	while($i<$pw_length)
	{
		mt_srand((double)microtime()*1000000);
		$randnum=mt_rand($low_ascii_bound,$upper_ascii_bound);
		if(!in_array($randnum,$notuse))
		{
			$password1=$password1.chr($randnum);
			$i++;
		}
	}
	return $password1;
}

//--------------------------------------分页
function page1($num,$line,$page_line,$start,$page,$search){
	global $fun_r;
	$pagetotal=$line*$page_line;//所要显示的总条数
	$total=ceil(($num-$start)/$line);//取得总页数
	$total_start=ceil($num/$pagetotal);//取得总偏移数
	$returnstr=$fun_r[totalrecord]."&nbsp;".$num."&nbsp;".$fun_r[totalrecorde]."&nbsp;&nbsp;";
	$PHP_SELF=$_SERVER['PHP_SELF'];
	if($start!=0)
	{
		$old_start=$start-$pagetotal;
		$returnstr.="&nbsp;&nbsp;<a href=".$PHP_SELF."?page=0&start=".$old_start.$search." title='UP".$page_line."Pages'><font face=webdings>7</font></a>";
	}
	$pagestart=$start/$pagetotal*$page_line;//取得当前页数
	for($i=0;$i<$total&&$i<$page_line;$i++)
	{
		if($page==$i)
		{$is_1="<b>[";$is_2="]</b>";}
		else
		{$is_1="<a href=".$PHP_SELF."?page=".$i."&start=".$start.$search.">";$is_2="</a>";}
		$pagenum=$pagestart+$i+1;
		$returnstr.="&nbsp;&nbsp;".$is_1.$pagenum.$is_2;
	}
	if($total_start!=($start/$pagetotal+1)&&$num!=0)
	{
		$new_start=$start+$pagetotal;
		$returnstr.="&nbsp;&nbsp;<a href=".$PHP_SELF."?page=0&start=".$new_start.$search." title='Next".$page_line."Pages'><font face=webdings>8</font></a>";
	}
	return $returnstr;
}

//时间转换函数
function to_time($datetime){
	if(strlen($datetime)==10)
	{
		$datetime.=" 00:00:00";
	}
	$r=explode(" ",$datetime);
	$t=explode("-",$r[0]);
	$k=explode(":",$r[1]);
	$dbtime=mktime($k[0],$k[1],$k[2],$t[1],$t[2],$t[0]);
	return $dbtime;
}

//时期转日期
function date_time($time,$format="Y-m-d H:i:s"){
	$threadtime=date($format,$time);
	return $threadtime;
}

//格式化日期
function format_datetime($newstime,$format){
	if($newstime=="0000-00-00 00:00:00")
	{return $newstime;}
	$time=to_time($newstime);
	$newdate=date_time($time,$format);
	return $newdate;
}

//时间转换函数
function to_date($date){
	$date.=" 00:00:00";
	$r=explode(" ",$date);
	$t=explode("-",$r[0]);
	$k=explode(":",$r[1]);
	$dbtime=@mktime($k[0],$k[1],$k[2],$t[1],$t[2],$t[0]);
	return $dbtime;
}

//选择时间
function ToChangeTime($time,$day){
	$truetime=$time-$day*24*3600;
	$date=date_time($truetime,"Y-m-d");
	return $date;
}

//删除文件
function DelFiletext($filename){
	@unlink($filename);
}

//取得文件内容
function ReadFiletext($filepath){
	$filepath=trim($filepath);
	$htmlfp=@fopen($filepath,"r");
	//远程
	if(strstr($filepath,"://"))
	{
		while($data=@fread($htmlfp,500000))
	    {
			$string.=$data;
		}
	}
	//本地
	else
	{
		$string=@fread($htmlfp,@filesize($filepath));
	}
	@fclose($htmlfp);
	return $string;
}

//写文件
function WriteFiletext($filepath,$string){
	global $public_r;
	$string=stripSlashes($string);
	$fp=@fopen($filepath,"w");
	@fputs($fp,$string);
	@fclose($fp);
	if($public_r[filechmod]==1)
	{}
	else
	{
		@chmod($filepath,0777);
	}
}

//写文件
function WriteFiletext_n($filepath,$string){
	global $public_r;
	$fp=@fopen($filepath,"w");
	@fputs($fp,$string);
	@fclose($fp);
	if($public_r[filechmod]==1)
	{}
	else
	{
		@chmod($filepath,0777);
	}
}

//标题属性后
function DoTitleFont($titlefont,$title){
	if(empty($titlefont))
	{
		return $title;
	}
	$r=explode(',',$titlefont);
	if(!empty($r[0]))
	{
		$title="<font color='".$r[0]."'>".$title."</font>";
	}
	if(empty($r[1]))
	{return $title;}
	//粗体
	if(strstr($r[1],"b"))
	{$title="<strong>".$title."</strong>";}
	//斜体
	if(strstr($r[1],"i"))
	{$title="<i>".$title."</i>";}
	//删除线
	if(strstr($r[1],"s"))
	{$title="<s>".$title."</s>";}
	return $title;
}

//建立目录函数
function DoMkdir($path){
	global $public_r;
	//不存在则建立
	if(!file_exists($path))
	{
		//安全模式
		if($public_r[phpmode])
		{
			$pr[0]=$path;
			FtpMkdir($ftpid,$pr,0777);
			$mk=1;
		}
		else
		{
			$mk=@mkdir($path,0777);
			@chmod($path,0777);
		}
		if(empty($mk))
		{
			echo $path;
			printerror("CreatePathFail","history.go(-1)");
		}
	}
	return true;
}

//设置上传文件权限
function DoChmodFile($file){
	global $public_r;
	if($public_r['filechmod']!=1)
	{
		@chmod($file,0777);
	}
}

//返回栏目链接字符串
function ReturnClassLink($classid){
	global $class_r,$public_r,$fun_r;
	if(empty($class_r[$classid][featherclass]))
	{$class_r[$classid][featherclass]="|";}
	$r=explode("|",$class_r[$classid][featherclass].$classid."|");
	$string="<a href=\"".$public_r[newsurl]."\" class=\"classlinkclass\">".$fun_r['index']."</a>";
	for($i=1;$i<count($r)-1;$i++)
	{
		//静态列表
		if(empty($class_r[$r[$i]][listdt]))
		{
			//无绑定域名
			if(empty($class_r[$r[$i]][classurl]))
			{$url=$public_r[newsurl].$class_r[$r[$i]][classpath]."/";}
			else
			{$url=$class_r[$r[$i]][classurl];}
		}
		else
		{
			$url=$public_r[newsurl]."e/action/ListInfo?classid=$r[$i]";
		}
		$string.="&nbsp;".$public_r[navfh]."&nbsp;<a href=\"".$url."\" class=\"classlinkclass\">".$class_r[$r[$i]][classname]."</a>";
	}
	$string.="&nbsp;".$public_r[navfh];
	return $string;
}

//返回专题链接字符串
function ReturnZtLink($ztid){
	global $class_zr,$public_r,$fun_r;
	$string="<a href=\"".$public_r[newsurl]."\" class=\"classlinkclass\">".$fun_r['index']."</a>";
	//无绑定域名
	if(empty($class_zr[$ztid][zturl]))
	{$url=$public_r[newsurl].$class_zr[$ztid][ztpath]."/";}
	else
	{$url=$class_zr[$ztid][zturl];}
    $string.="&nbsp;".$public_r[navfh]."&nbsp;<a href=\"".$url."\" class=\"classlinkclass\">".$class_zr[$ztid][ztname]."</a>";
	$string.="&nbsp;".$public_r[navfh];
	return $string;
}

//返回标题链接
function sys_ReturnBqTitleLink($r){
	global $public_r,$class_r;
	if(empty($r[titleurl]))
	{
		if($class_r[$r[classid]][showdt])//动态刷新
		{
			$titleurl=$public_r[newsurl]."e/action/ShowInfo?classid=$r[classid]&id=$r[id]";
			return $titleurl;
		}
		if($class_r[$r[classid]][filename]==3)
		{
			$filename=ReturnInfoSPath($r[filename]);
		}
		else
		{
			$filetype=$r[groupid]?'.php':$class_r[$r[classid]][filetype];
			$filename=$r[filename].$filetype;
		}
		$iclasspath=ReturnSaveInfoPath($r[classid],$r[id]);
		$newspath=empty($r[newspath])?'':$r[newspath]."/";
		if($class_r[$r[classid]][classurl]&&$class_r[$r[classid]][ipath]=='')//域名
		{
			$titleurl=$class_r[$r[classid]][classurl]."/".$newspath.$filename;
		}
		else
		{
			$titleurl=$public_r[newsurl].$iclasspath.$newspath.$filename;
		}
	}
	else
	{
		if($public_r['opentitleurl'])
		{
			$titleurl=$r[titleurl];
		}
		else
		{
			$titleurl=$public_r[newsurl]."e/public/jump/?classid=".$r[classid]."&id=".$r[id]."&url=".urlencode($r[titleurl]);
		}
	}
	return $titleurl;
}

//返回标题链接
function sys_ReturnBqAutoTitleLink($r){
	global $public_r,$class_r;
	if(empty($r[titleurl]))
	{
		if($class_r[$r[classid]][filename]==3)
		{
			$filename=ReturnInfoSPath($r[filename]);
		}
		else
		{
			$filetype=$r[groupid]?'.php':$class_r[$r[classid]][filetype];
			$filename=$r[filename].$filetype;
		}
		$iclasspath=ReturnSaveInfoPath($r[classid],$r[id]);
		$newspath=empty($r[newspath])?'':$r[newspath]."/";
		if($class_r[$r[classid]][classurl]&&$class_r[$r[classid]][ipath]=='')//域名
		{
			$titleurl=$class_r[$r[classid]][classurl]."/".$newspath.$filename;
		}
		else
		{
			$titleurl=$public_r[newsurl].$iclasspath.$newspath.$filename;
		}
	}
	else
	{
		if($public_r['opentitleurl'])
		{
			$titleurl=$r[titleurl];
		}
		else
		{
			$titleurl=$public_r[newsurl]."e/public/jump/?classid=".$r[classid]."&id=".$r[id]."&url=".urlencode($r[titleurl]);
		}
	}
	return $titleurl;
}

//返回栏目链接
function sys_ReturnBqClassname($r,$have_class=0){
	global $public_r,$class_r;
	if($have_class)
	{
		if($class_r[$r[classid]][classurl])
		{
			$classurl=$class_r[$r[classid]][classurl];
		}
		else
		{
			$classurl=$public_r[newsurl].$class_r[$r[classid]][classpath]."/";
		}
		//动态列表
		if($class_r[$r[classid]][listdt])
		{
			$classurl=$public_r[newsurl]."e/action/ListInfo?classid=$r[classid]";
		}
		if(empty($class_r[$r[classid]][bname]))
		{$classname=$class_r[$r[classid]][classname];}
		else
		{$classname=$class_r[$r[classid]][bname];}
		$myadd="[<a href=".$classurl.">".$classname."</a>]";
		//只返回链接
		if($have_class==9)
		{$myadd=$classurl;}
	}
	else
	{$myadd="";}
	return $myadd;
}

//返回专题链接
function sys_ReturnBqZtname($r){
	global $public_r,$class_zr;
	if($class_zr[$r[ztid]][zturl])
	{
		$zturl=$class_zr[$r[ztid]][zturl];
    }
	else
	{
		$zturl=$public_r[newsurl].$class_zr[$r[ztid]][ztpath]."/";
    }
	return $zturl;
}

//文件大小格式转换
function ChTheFilesize($size){
	if($size>=1024*1024)//MB
	{
		$filesize=number_format($size/(1024*1024),2,'.','')." MB";
	}
	elseif($size>=1024)//KB
	{
		$filesize=number_format($size/1024,2,'.','')." KB";
	}
	else
	{
		$filesize=$size." Bytes";
	}
	return $filesize;
}

//替换列表模板
function ReplaceListVars($no,$listtemp,$subnews,$subtitle,$formatdate,$url,$haveclass=0,$r,$field){
	global $empire,$public_r,$class_r,$fun_r;
	if($haveclass)
	{
		$add=sys_ReturnBqClassname($r,$haveclass);
	}
	if(empty($r[oldtitle]))
	{$r[oldtitle]=$r[title];}
	//变量
	$nottobrf=',newstext,title,titlepic,newstime,befrom,writer,downpath,onlinepath,musicpath,booktexturl,';
	$fr=explode(",",$field['listtempvar']);
	for($i=1;$i<count($fr);$i++)
	{
		$f=$fr[$i];
		$value=$r[$f];
		if($f=="downpath")//下载地址
		{
			//$value=ReturnDownSoftHtml($r);
		}
		elseif($f=="onlinepath")//观看地址
		{
			//$value=ReturnOnlinepathHtml($r);
		}
		elseif($f=="morepic")//图片集
		{
			//$value=ReturnMorepicpathHtml($r);
		}
		elseif($f=="title")//标题
		{
			//截取字符
	        if(!empty($subtitle))
	        {
				$value=sub($value,0,$subtitle,false);
	        }
			$value=DoTitleFont($r[titlefont],$value);
		}
		elseif($f=="newstime")//时间
		{
			if(strstr($listtemp,"[!--newstime--]"))
			{
				$value=format_datetime($value,$formatdate);
			}
		}
		elseif($f=="smalltext")//简介
		{
			if(strstr($listtemp,"[!--smalltext--]"))
			{
				//截取字符
				if(!empty($subnews))
				{
					$value=sub($value,0,$subnews,false);
				}
			}
		}
		elseif($f=="flashsay")//flash简介
		{
			if(strstr($listtemp,"[!--flashsay--]"))
			{
				//截取字符
				if(!empty($subnews))
				{
					$value=sub($value,0,$subnews,false);
				}
			}
		}
		elseif($f=="softsay")//soft简介
		{
			if(strstr($listtemp,"[!--softsay--]"))
			{
				//截取字符
				if(!empty($subnews))
				{
					$value=sub($value,0,$subnews,false);
				}
			}
		}
		elseif($f=="moviesay")//movie简介
		{
			if(strstr($listtemp,"[!--moviesay--]"))
			{
				//截取字符
				if(!empty($subnews))
				{
					$value=sub($value,0,$subnews,false);
				}
			}
		}
		elseif($f=="titlepic")//标题图片
		{
			if(empty($value))
		    {$value=$public_r[newsurl]."e/data/images/notimg.gif";}
		}
		elseif($f=="befrom")//信息来源
		{
			if(strstr($listtemp,"[!--befrom--]"))
			{
				$value=ReplaceBefrom($value);
			}
		}
		elseif($f=="writer")//作者
		{
			if(strstr($listtemp,"[!--writer--]"))
			{
				$value=ReplaceWriter($value);
			}
		}
		else
		{}
		//正常字段
		if(!strstr($nottobrf,','.$f.','))
		{
			if(!strstr($field['tobrf'],','.$f.','))//加br
			{
				$value=nl2br($value);
			}
			if(strstr($field['dohtmlf'],','.$f.','))//去除html
			{
				$value=htmlspecialchars($value);
			}
		}
		$listtemp=str_replace("[!--".$f."--]",$value,$listtemp);
	}
	//链接
	$titleurl=sys_ReturnBqTitleLink($r);
	//加入收藏
	$listtemp=str_replace("[!--fava.url--]",$public_r[newsurl]."e/member/fava/add?classid=$r[classid]&id=".$r[id],$listtemp);
	$listtemp=str_replace("[!--id--]",$r[id],$listtemp);
	$listtemp=str_replace("[!--classid--]",$r[classid],$listtemp);
	$listtemp=str_replace("[!--class.name--]",$add,$listtemp);
	$listtemp=str_replace("[!--userfen--]",$r[userfen],$listtemp);
	$listtemp=str_replace("[!--titleurl--]",$titleurl,$listtemp);
	$listtemp=str_replace("[!--no.num--]",$no,$listtemp);
	$listtemp=str_replace("[!--plnum--]",$r[plnum],$listtemp);
	$listtemp=str_replace("[!--news.url--]",$public_r[newsurl],$listtemp);
	$listtemp=str_replace("[!--userid--]",$r[userid],$listtemp);
	$listtemp=str_replace("[!--username--]",$r[username],$listtemp);
	$listtemp=str_replace("[!--ismember--]",$r[ismember],$listtemp);
	$listtemp=str_replace("[!--onclick--]",$r[onclick],$listtemp);
	$listtemp=str_replace("[!--oldtitle--]",$r[oldtitle],$listtemp);
	$listtemp=str_replace("[!--totaldown--]",$r[totaldown],$listtemp);
	//类别链接
	if(strstr($listtemp,"[!--this.classlink--]"))
	{
		$thisclasslink=sys_ReturnBqClassname($r,9);
		$listtemp=str_replace("[!--this.classlink--]",$thisclasslink,$listtemp);
	}
	$thisclassname=$class_r[$r[classid]][bname]?$class_r[$r[classid]][bname]:$class_r[$r[classid]][classname];
	$listtemp=str_replace("[!--this.classname--]",$thisclassname,$listtemp);
	return $listtemp;
}

//加上防复制字符
function AddNotCopyRndStr($text){
	global $public_r;
	if($public_r['opencopytext']){
		$rnd=make_password(3).$public_r['sitename'];
		$text=str_replace("<br />","<span style=\"display:none\">".$rnd."</span><br />",$text);
		$text=str_replace("</p>","<span style=\"display:none\">".$rnd."</span></p>",$text);
	}
	return $text;
}

//替换信息来源
function ReplaceBefrom($befrom){
	global $empire,$dbtbpre;
	if(empty($befrom))
	{return $befrom;}
	$befrom=addslashes($befrom);
	$r=$empire->fetch1("select befromid,sitename,siteurl from {$dbtbpre}enewsbefrom where sitename='$befrom' limit 1");
	if(empty($r[befromid]))
	{return $befrom;}
	$return_befrom="<a href='".$r[siteurl]."' target=_blank>".$r[sitename]."</a>";
	return $return_befrom;
}

//替换作者
function ReplaceWriter($writer){
	global $empire,$dbtbpre;
	if(empty($writer))
	{return $writer;}
	$writer=addslashes($writer);
	$r=$empire->fetch1("select wid,writer,email from {$dbtbpre}enewswriter where writer='$writer' limit 1");
	if(empty($r[wid])||empty($r[email]))
	{
		return $writer;
	}
	$return_writer="<a href='".$r[email]."'>".$r[writer]."</a>";
	return $return_writer;
}

//备份下载记录
function BakDown($classid,$id,$pathid,$userid,$username,$title,$cardfen,$online){
	global $empire,$dbtbpre;
	$truetime=time();
	$id=(int)$id;
	$pathid=(int)$pathid;
	$userid=(int)$userid;
	$cardfen=(int)$cardfen;
	$classid=(int)$classid;
	$sql=$empire->query("insert into {$dbtbpre}enewsdownrecord(id,pathid,userid,username,title,cardfen,truetime,classid,online) values($id,$pathid,$userid,'$username','".addslashes($title)."',$cardfen,$truetime,$classid,$online);");
}

//截取简介
function SubSmalltextVal($value,$len){
	$value=str_replace(array("\r\n","<br />","<br>","&nbsp;","[!--empirenews.page--]","[/!--empirenews.page--]"),array("","\r\n","\r\n"," ","",""),$value);
	$value=strip_tags($value);
	if($len)
	{
		$value=sub($value,0,$len,false);
	}
	$value=trim($value,"\r\n");
	return $value;
}

//返回不生成html的栏目
function ReturnNreInfoWhere(){
	global $public_r;
	if(empty($public_r['nreinfo'])||$public_r['nreinfo']==',')
	{
		return '';
	}
	$cids='';
	$r=explode(',',$public_r['nreinfo']);
	$count=count($r);
	for($i=1;$i<$count-1;$i++)
	{
		$dh=',';
		if($i==1)
		{
			$dh='';
		}
		$cids.=$dh.$r[$i];
	}
	$where=' and classid not in ('.$cids.')';
	return $where;
}

//返回标签不调用栏目
function ReturnNottoBqWhere(){
	global $public_r;
	if(empty($public_r['nottobq'])||$public_r['nottobq']==',')
	{
		return '';
	}
	$cids='';
	$r=explode(',',$public_r['nottobq']);
	$count=count($r);
	for($i=1;$i<$count-1;$i++)
	{
		$dh=',';
		if($i==1)
		{
			$dh='';
		}
		$cids.=$dh.$r[$i];
	}
	$where=' and classid not in ('.$cids.')';
	return $where;
}

//返回文件名及扩展名
function ReturnCFiletype($file){
	$r=explode('.',$file);
	$count=count($r)-1;
	$re['filetype']=$r[$count];
	$re['filename']=substr($file,0,strlen($file)-strlen($re['filetype'])-1);
	return $re;
}

//返回栏目目录
function ReturnSaveClassPath($classid,$f=0){
	global $class_r;
	$classpath=$class_r[$classid][classpath];
	if($f==1){
		$classpath.="/index".$class_r[$classid][classtype];
	}
	return $classpath;
}

//返回专题目录
function ReturnSaveZtPath($classid,$f=0){
	global $class_zr;
	$classpath=$class_zr[$classid][ztpath];
	if($f==1){
		$classpath.="/index".$class_zr[$classid][zttype];
	}
	return $classpath;
}

//返回首页文件
function ReturnSaveIndexFile(){
	global $public_r;
	$file="index".$public_r[indextype];
	return $file;
}

//返回内容页存放目录
function ReturnSaveInfoPath($classid,$id){
	global $class_r;
	if($class_r[$classid][ipath]==''){
		$path=$class_r[$classid][classpath].'/';
	}
	else{
		$path=$class_r[$classid][ipath]=='/'?'':$class_r[$classid][ipath].'/';
	}
	return $path;
}

//格式化信息目录
function FormatPath($classid,$mynewspath,$enews=0){
	global $class_r,$addgethtmlpath;
	if($enews){
		$newspath=$mynewspath;
	}
	else{
		$newspath=date($class_r[$classid][newspath]);
	}
	if(empty($newspath)){
		return "";
	}
	$path=$addgethtmlpath."../../".ReturnSaveInfoPath($classid,$id);
	$returnpath="";
	$r=explode("/",$newspath);
	$count=count($r);
	for($i=0;$i<$count;$i++){
		if($i>0){
			$returnpath.="/".$r[$i];
		}
		else{
			$returnpath.=$r[$i];
		}
		$createpath=$path.$returnpath;
		$mk=DoMkdir($createpath);
		if(empty($mk)){
			printerror("CreatePathFail","");
		}
	}
	return $returnpath;
}

//返回内容页目录
function ReturnInfoSPath($filename){
	return str_replace('/index','',$filename);
}

//返回附件目录
function ReturnFileSavePath($classid,$fpath=''){
	global $public_r,$class_r;
	$fpath=$fpath||strstr(','.$fpath.',',',0,')?$fpath:$public_r['fpath'];
	if($fpath==1)//p目录
	{
		$r['filepath']='d/file/p/';
		$r['fileurl']=$public_r['fileurl'].'p/';
	}
	elseif($fpath==2)//file目录
	{
		$r['filepath']='d/file/';
		$r['fileurl']=$public_r['fileurl'];
	}
	else
	{
		if(empty($classid))
		{
			$r['filepath']='d/file/p/';
			$r['fileurl']=$public_r['fileurl'].'p/';
		}
		else
		{
			$r['filepath']='d/file/'.$class_r[$classid][classpath].'/';
			$r['fileurl']=$public_r['fileurl'].$class_r[$classid][classpath].'/';
		}
	}
	return $r;
}

//格式化附件目录
function FormatFilePath($classid,$mynewspath,$enews=0){
	global $public_r,$addgethtmlpath;
	if($enews)
	{
		$newspath=$mynewspath;
	}
	else
	{
		$newspath=date($public_r['filepath']);
	}
	if(empty($newspath))
	{
		return "";
	}
	$fspath=ReturnFileSavePath($classid);
	$path=$addgethtmlpath."../../".$fspath['filepath'];
	$returnpath="";
	$r=explode("/",$newspath);
	$count=count($r);
	for($i=0;$i<$count;$i++){
		if($i>0){
			$returnpath.="/".$r[$i];
		}
		else{
			$returnpath.=$r[$i];
		}
		$createpath=$path.$returnpath;
		$mk=DoMkdir($createpath);
		if(empty($mk)){
			printerror("CreatePathFail","");
		}
	}
	return $returnpath;
}

//替换表前缀
function RepSqlTbpre($sql){
	global $dbtbpre;
	$sql=str_replace("[!db.pre!]",$dbtbpre,$sql);
	return $sql;
}

//时间转换
function ToChangeUseTime($time){
	global $fun_r;
	$usetime=time()-$time;
	if($usetime<60)
	{
		$tstr=$usetime.$fun_r['TimeSecond'];
	}
	else
	{
		$usetime=round($usetime/60);
		$tstr=$usetime.$fun_r['TimeMinute'];
	}
	return $tstr;
}

//返回栏目集合
function ReturnClass($sonclass){
	if($sonclass==""||$sonclass=="|"){
		return "classid=0";
	}
	$where="classid in (".RepSonclassSql($sonclass).")";
	return $where;
}

//替换子栏目子
function RepSonclassSql($sonclass){
	if($sonclass==""||$sonclass=="|"){
		return 0;
	}
	$sonclass=substr($sonclass,1,strlen($sonclass)-2);
	$sonclass=str_replace("|",",",$sonclass);
	return $sonclass;
}

//返回多栏目
function sys_ReturnMoreClass($sonclass,$son=0){
	global $class_r;
	$r=explode(",",$sonclass);
	$count=count($r);
	$return_r[0]=$r[0];
	$where="";
	for($i=0;$i<$count;$i++)
	{
		$or=" or ";
		if($i==0)
		{
			$or="";
		}
		if($son==1)
		{
			if($class_r[$r[$i]]['tbname']&&!$class_r[$r[$i]]['islast'])
			{
				$where.=$or."classid in (".RepSonclassSql($class_r[$r[$i]]['sonclass']).")";
			}
			else
			{
				$where.=$or."classid='".$r[$i]."'";
			}
		}
		else
		{
			$where.=$or."classid='".$r[$i]."'";
		}
	}
	$return_r[1]=$where;
	return $return_r;
}

//返回多专题
function sys_ReturnMoreZt($zt){
	$r=explode(",",$zt);
	$count=count($r);
	$return_r[0]=$r[0];
	$where="";
	for($i=0;$i<$count;$i++)
	{
		$or=" or ";
		if($i==0)
		{
			$or="";
		}
		$where.=$or."ztid like '%|".$r[$i]."|%'";
	}
	$return_r[1]=$where;
	return $return_r;
}

//验证是否包含栏目
function CheckHaveInClassid($cr,$checkclass){
	global $class_r;
	if($cr['islast'])
	{
		$chclass='|'.$cr['classid'].'|';
	}
	else
	{
		$chclass=$cr['sonclass'];
	}
	$return=0;
	$r=explode('|',$chclass);
	$count=count($r);
	for($i=1;$i<$count-1;$i++)
	{
		if(strstr($checkclass,'|'.$r[$i].'|'))
		{
			$return=1;
			break;
		}
	}
	return $return;
}

//返回加前缀的下载地址
function ReturnDownQzPath($path,$urlid){
	global $empire,$dbtbpre;
	if(empty($urlid))
	{
		$re['repath']=$path;
		$re['downtype']=0;
    }
	else
	{
		$r=$empire->fetch1("select urlid,url,downtype from {$dbtbpre}enewsdownurlqz where urlid='$urlid'");
		if($r['urlid'])
		{
			$re['repath']=$r['url'].$path;
		}
		else
		{
			$re['repath']=$path;
		}
		$re['downtype']=$r['downtype'];
	}
	return $re;
}

//返回带防盗链的绝对地址
function ReturnDSofturl($downurl,$qz,$path='../../'){
	$urlr=ReturnDownQzPath(stripSlashes($downurl),$qz);
	$url=$urlr['repath'];
	@include($path."class/enpath.php");//防盗链
	$url=DoEnOnlinepath($url);
	return $url;
}

//验证提交来源
function CheckCanPostUrl(){
	global $public_r;
	if($public_r['canposturl'])
	{
		$r=explode("\r\n",$public_r['canposturl']);
		$count=count($r);
		$b=0;
		for($i=0;$i<$count;$i++)
		{
			if(strstr($_SERVER['HTTP_REFERER'],$r[$i]))
			{
				$b=1;
				break;
			}
		}
		if($b==0)
		{
			printerror('NotCanPostUrl','',1);
		}
	}
}
?>