<?php
require('../class/connect.php');
require('../class/functions.php');
require('../class/t_functions.php');
require LoadLang('pub/fun.php');
require('../data/dbcache/class.php');
require('../data/dbcache/MemberLevel.php');
require('../class/eafun.php');

//是否開啟
eapage_IsOpenEapage();

//參數
$egn=(int)$_GET['egn'];
$ecmsck=RepPostVar($_GET['ecmsck']);
$dornd=RepPostVar($_GET['dornd']);
$dotime=(int)$_GET['dotime'];
$dopass=RepPostVar($_GET['dopass']);
//參數

$classid=(int)$_GET['classid'];
$id=(int)$_GET['id'];
$page=(int)$_GET['page'];
$page=RepPIntvar($page);
$mid=(int)$class_r[$classid]['modid'];
$tbname=$class_r[$classid]['tbname'];

$urlcs='';
if(!$classid||!$dornd||!$dotime||!$dopass||!$ecmsck)
{
	printerror('EapageErrorPass',$public_r['newsurl'],1);
}
if($ecmsck<>'eaShowInfo')
{
	printerror('EapageErrorPass',$public_r['newsurl'],1);
}
$urlcs=eapage_CkPassShowInfo($classid,$id,$dornd,$dotime,$dopass,$ecmsck);

//db
$link=db_connect();
$empire=new mysqlquery();

//驗證信息
if(!$classid||!$id||!$mid||!$tbname||InfoIsInTable($tbname))
{
	printerror('此信息不存在','',1,0,1);
}
//索引表
$index_r=$empire->fetch1("select id,classid,checked from {$dbtbpre}ecms_".$tbname."_index where id='$id'".do_dblimit_one());
if(!$index_r['id']||$index_r['classid']!=$classid)
{
	printerror("ErrorUrl","history.go(-1)",1);
}
//返回表
$infotb=ReturnInfoMainTbname($tbname,$index_r['checked']);
$r=$empire->fetch1("select * from ".$infotb." where id='$id'".do_dblimit_one());
if(!$r['id']||$classid!=$r['classid'])
{
	printerror('此信息不存在','',1,0,1);
}
//外部鏈接
if($r['isurl'])
{
	$titleurl=$r['titleurl'];
	Header("Location:$titleurl");
	exit();
}
//返回表信息
$infodatatb=ReturnInfoDataTbname($tbname,$index_r['checked'],$r['stb']);
//副表
$finfor=$empire->fetch1("select ".ReturnSqlFtextF($mid)." from ".$infodatatb." where id='".$r['id']."'".do_dblimit_one());
$r=array_merge($r,$finfor);
//存文本
if($emod_r[$mid]['savetxtf'])
{
	$stf=$emod_r[$mid]['savetxtf'];
	if($r[$stf])
	{
		eCheckStrPathGet($r[$stf]);
		$r[$stf]=GetTxtFieldText($r[$stf]);
	}
}
//初始值
$search="&classid=$classid&id=$id";
$line=1;
$start=0;
$page_line=6;//每頁顯示鏈接數
$offset=$page*$line;//總偏移量
$GLOBALS['navclassid']=$r['classid'];
$GLOBALS['navinfor']=$r;
//取得內容模板
$r['newstempid']=$r['newstempid']?$r['newstempid']:$class_r[$r['classid']]['newstempid'];
$r['newstempid']=(int)$r['newstempid'];
$newstemp_r=$empire->fetch1("select tempid,temptext,showdate from ".GetTemptb("enewsnewstemp")." where tempid='".$r['newstempid']."'");

//替換模板變量
function DtGetHtml($add,$newstemp_r,$mid,$tbname,$line,$page_line,$start,$page,$search){
	global $public_r,$class_r,$class_zr,$class_tr,$fun_r,$empire,$dbtbpre,$emod_r,$level_r,$urlcs;
	//更新點擊
	//$empire->query("update {$dbtbpre}ecms_".$tbname." set onclick=onclick+1 where id='".$add['id']."'".do_dblimit_upone());
	//$add['onclick']=$add['onclick']+1;
	//模板參數
	$newstemptext=$newstemp_r['temptext'];
	$formatdate=$newstemp_r['showdate'];
	//頁面
	$pagetitle=ehtmlspecialchars($add['title']);
	$url=ReturnClassLink($add['classid']);//導航
	$newstemptext=DtInfo_ReplaceSvars($newstemptext,$url,$add['classid'],$pagetitle,$add['keyboard'],$pagetitle);
	//相關信息
	if(strstr($newstemptext,'[!--other.link--]'))
	{
    	$keyboardtext=GetKeyboard($add['keyboard'],$add['keyid'],$add['classid'],$add['id'],$class_r[$add['classid']]['link_num']);
	}
	//分頁字段
	$ptitle=$add['title'];
	$truepage='';
	$titleselect='';
	$expage='[!--empirenews.page--]';//分頁符
	$pf=$emod_r[$mid]['pagef'];
	if($pf&&strstr($add[$pf],$expage))//有分頁
	{
		$n_r=explode($expage,$add[$pf]);
		$thispagenum=count($n_r);
		//checkpageno
		eCheckListPageNo($page,$line,$thispagenum);
		if($page<0||$page>$thispagenum-1)
		{
			$page=0;
		}
		$add[$pf]=$n_r[$page];
		if($page)
		{
			$ti_r=explode('[/!--empirenews.page--]',$n_r[$page]);
			if(count($ti_r)>=2)
			{
				$ptitle=$ti_r[0];
				$add[$pf]=$ti_r[1];
			}
			else
			{
				$ptitle=$add['title'].'('.($page+1).')';
			}
		}
		//偽靜態
		$pagefunr=eReturnRewriteInfoUrl($add['classid'],$add['id'],0);
		$pagefunr['pageurl']=$public_r['newsurl']."e/eapage/eaShowInfo.php?".$urlcs;
		$pagefunr['rewrite']=0;
		$pagefunr['repagenum']=0;
		//取得分頁
		$truepage=InfoUsePage($thispagenum,$line,$page_line,$start,$page,$search,$pagefunr);
		//下拉式分頁
		if(strstr($newstemptext,'[!--title.select--]'))
		{
			for($j=0;$j<$thispagenum;$j++)
			{
				$spurl=eReturnRewritePageLink($pagefunr,$j);
				if($j==0)
				{
					$sptitle=$add['title'];
				}
				else
				{
					$ti_r=explode('[/!--empirenews.page--]',$n_r[$j]);
					$sptitle=count($ti_r)>=2?$ti_r[0]:$add['title'].'('.($j+1).')';
				}
				$select='';
				if($page==$j)
				{
					$ptitle=$sptitle;
					$select=' selected';
				}
				$titleselect.='<option value="'.$spurl.'"'.$select.'>'.strip_tags($sptitle).'</option>';
			}
			$titleselect='<select name="titleselect" onchange="self.location.href=this.options[this.selectedIndex].value">'.$titleselect.'</select>';
		}
		//下一頁鏈接
		if($page==$thispagenum-1)
		{
			$thisnextlink=eReturnRewritePageLink($pagefunr,0);
		}
		else
		{
			$thisnextlink=eReturnRewritePageLink($pagefunr,$page+1);
		}
	}
	else
	{
		//checkpageno
		if($page!=0)
		{
			printerror('ErrorUrl','history.go(-1)',1);
		}
	}
	//返回替換驗證字符
	$docheckrep=ReturnCheckDoRepStr();
	if($add['newstext'])
	{
		if(empty($public_r['dorepword'])&&$docheckrep[3])
		{
			$add['newstext']=ReplaceWord($add['newstext']);//過濾字符
		}
		if(empty($public_r['dorepkey'])&&$docheckrep[4]&&!empty($add['dokey']))//替換關鍵字
		{
			$add['newstext']=ReplaceKey($add['newstext'],$add['classid']);
		}
		if($public_r['opencopytext'])
		{
			$add['newstext']=AddNotCopyRndStr($add['newstext']);//隨機複製字符
		}
	}
	//變量
	$tempf=$emod_r[$mid]['tempf'];
	$fr=explode(',',$tempf);
	$fcount=count($fr)-1;
	//變量替換
	$newstempstr=$newstemptext;//模板
	for($i=1;$i<$fcount;$i++)
	{
		$f=$fr[$i];
		$value=$add[$f];
		if($f=='downpath')//下載地址
		{
			if(strstr($newstemptext,'[!--downpath--]'))
			{
				$value=ReturnDownSoftHtml($add);
			}
		}
		elseif($f=='onlinepath')//觀看地址
		{
			if(strstr($newstemptext,'[!--onlinepath--]'))
			{
				$value=ReturnOnlinepathHtml($add);
			}
		}
		elseif($f=='morepic')//圖片集
		{
			if(strstr($newstemptext,'[!--morepic--]'))
			{
				$value=ReturnMorepicpathHtml($add);
			}
		}
		elseif($f=='newstime')//時間
		{
			if(strstr($newstemptext,'[!--newstime--]'))
			{
				$value=date($formatdate,$value);
			}
		}
		elseif($f=='befrom')//信息來源
		{
			if($docheckrep[1]&&strstr($newstemptext,'[!--befrom--]'))
			{
				$value=ReplaceBefrom($value);
			}
		}
		elseif($f=='writer')//作者
		{
			if($docheckrep[2]&&strstr($newstemptext,'[!--writer--]'))
			{
				$value=ReplaceWriter($value);
			}
		}
		elseif($f=='titlepic')//標題圖片
		{
			if(empty($value))
			{$value=$public_r['newsurl'].'e/data/images/notimg.gif';}
		}
		elseif($f=='title')//標題
		{
		}
		else//正常字段
		{
			if(!strstr($emod_r[$mid]['editorf'],','.$f.','))
			{
				if(strstr($emod_r[$mid]['tobrf'],','.$f.','))//加br
				{
					$value=nl2br($value);
				}
				if(!strstr($emod_r[$mid]['dohtmlf'],','.$f.','))//去除html
				{
					$value=RepFieldtextNbsp(ehtmlspecialchars($value));
				}
			}
		}
		$newstempstr=str_replace('[!--'.$f.'--]',$value,$newstempstr);
	}
	//固定變量
	$newstempstr=str_replace('[!--id--]',$add['id'],$newstempstr);
	$newstempstr=str_replace('[!--classid--]',$add['classid'],$newstempstr);
	$newstempstr=str_replace('[!--class.name--]',$class_r[$add['classid']]['classname'],$newstempstr);
	$newstempstr=str_replace('[!--ttid--]',$add['ttid'],$newstempstr);
	$newstempstr=str_replace('[!--tt.name--]',$class_tr[$add['ttid']]['tname'],$newstempstr);
	$newstempstr=str_replace('[!--tt.url--]',sys_ReturnBqInfoTypeUrl($add['ttid']),$newstempstr);
	$newstempstr=str_replace('[!--onclick--]',$add['onclick'],$newstempstr);
	$newstempstr=str_replace('[!--userfen--]',$add['userfen'],$newstempstr);
	$newstempstr=str_replace('[!--username--]',$add['username'],$newstempstr);
	//帶鏈接的用戶名
	if($add['ismember']==1&&$add['userid'])
	{
		$newstempstr=str_replace('[!--linkusername--]',"<a href='".$public_r['newsurl']."e/space/?userid=".$add['userid']."' target=_blank>".$add['username']."</a>",$newstempstr);
	}
	else
	{
		$newstempstr=str_replace('[!--linkusername--]',$add['username'],$newstempstr);
	}
	$newstempstr=str_replace('[!--userid--]',$add['userid'],$newstempstr);
	$newstempstr=str_replace('[!--other.link--]',$keyboardtext,$newstempstr);
	$newstempstr=str_replace('[!--news.url--]',$public_r['newsurl'],$newstempstr);
	$newstempstr=str_replace('[!--plnum--]',$add['plnum'],$newstempstr);
	$newstempstr=str_replace('[!--totaldown--]',$add['totaldown'],$newstempstr);
	$newstempstr=str_replace('[!--keyboard--]',$add['keyboard'],$newstempstr);
	//鏈接
	$titleurl=sys_ReturnBqTitleLink($add);
	$newstempstr=str_replace('[!--titleurl--]',$titleurl,$newstempstr);
	$newstempstr=str_replace('[!--page.stats--]','',$newstempstr);
	$classurl=sys_ReturnBqClassname($add,9);
	$newstempstr=str_replace('[!--class.url--]',$classurl,$newstempstr);
	//下一篇
	if(strstr($newstemptext,'[!--info.next--]'))
	{
		$next_r=$empire->fetch1("select isurl,titleurl,classid,id,title from {$dbtbpre}ecms_".$class_r[$add['classid']]['tbname']." where id>".$add['id']." and classid='".$add['classid']."' order by id".do_dblimit_one());
		if(empty($next_r['id']))
		{
			$infonext="<a href='".$classurl."'>".$fun_r['HaveNoNextLink']."</a>";
		}
		else
		{
			//鏈接
			$nexttitleurl=sys_ReturnBqTitleLink($next_r);
			$infonext="<a href='".$nexttitleurl."'>".$next_r['title']."</a>";
		}
		$newstempstr=str_replace('[!--info.next--]',$infonext,$newstempstr);
	}
	//上一篇
	if(strstr($newstemptext,'[!--info.pre--]'))
	{
		$next_r=$empire->fetch1("select isurl,titleurl,classid,id,title from {$dbtbpre}ecms_".$class_r[$add['classid']]['tbname']." where id<".$add['id']." and classid='".$add['classid']."' order by id desc".do_dblimit_one());
		if(empty($next_r['id']))
		{
			$infonext="<a href='".$classurl."'>".$fun_r['HaveNoNextLink']."</a>";
		}
		else
		{
			//鏈接
			$nexttitleurl=sys_ReturnBqTitleLink($next_r);
			$infonext="<a href='".$nexttitleurl."'>".$next_r['title']."</a>";
		}
		$newstempstr=str_replace('[!--info.pre--]',$infonext,$newstempstr);
	}
	//投票
	if(strstr($newstemptext,'[!--info.vote--]'))
	{
		$myvotetext=sys_GetInfoVote($add['classid'],$add['id']);
		$newstempstr=str_replace('[!--info.vote--]',$myvotetext,$newstempstr);
	}
	//評分
	if(strstr($newstemptext,'[!--pinfopfen--]'))
	{
		$pinfopfen=$add['infopfennum']?round($add['infopfen']/$add['infopfennum']):0;
		$newstempstr=str_replace('[!--pinfopfen--]',$pinfopfen,$newstempstr);
		$newstempstr=str_replace('[!--infopfennum--]',$add['infopfennum'],$newstempstr);
	}
	$string=$newstempstr;
	//替換變量
	$string=str_replace('[!--p.title--]',strip_tags($ptitle),$string);
	$string=str_replace('[!--next.page--]',$thisnextlink,$string);
	$string=str_replace('[!--page.url--]',$truepage,$string);
	$string=str_replace('[!--title.select--]',$titleselect,$string);
	return $string;
}

if(empty($newstemp_r['tempid']))
{
	printerror('ErrorUrl','',1);
}
//頁面支持標籤
if($public_r['dtncanbq'])
{
	$newstemp_r['temptext']=DtNewsBq('text'.$newstemp_r['tempid'],$newstemp_r['temptext'],1);
}
else
{
	if($public_r['searchtempvar'])
	{
		$newstemp_r['temptext']=ReplaceTempvar($newstemp_r['temptext']);
	}
}
$string=DtGetHtml($r,$newstemp_r,$mid,$tbname,$line,$page_line,$start,$page,$search);
echo stripSlashes($string);
db_close();
$empire=null;
?>