<?php
// 开启页面输出拦截，页面输出前自动插入 TG 注释
ob_start(function($html) {
    if (empty($html)) return $html;

    // 如果还没有该注释，则强制插入
    if (stripos($html, '<!-- 程序TG：@lufeiwen -->') === false) {
        if (preg_match('/<!DOCTYPE\s+html[^>]*>/i', $html)) {
            $html = preg_replace('/(<!DOCTYPE\s+html[^>]*>)/i', '$1<!-- 程序TG：@lufeiwen -->', $html, 1);
        } else {
            $html = '<!-- 程序TG：@lufeiwen -->' . $html;
        }
    }
    return $html;
});

require 'tj/t.php';
require 'e/class/connect.php';
require 'e/class/db_sql.php';
require 'e/class/functions.php';
require 'e/class/t_functions.php';
require 'e/data/dbcache/class.php';
require 'e/data/language/gb/pub/fun.php';
$site_r=dp_getConfig();
$topcid=dp_domainCid();
$siteid=dp_domainSid();
require 'e/dongpo/zhanqun/lib/mob.php';
$hz='';
if($site_r['mobtemp'] && $site_r['mobtempid'] && ck_mob()===true){
	$hz='_m';
}

$site_r[0]['cachetime']=60;
$cache_time=(int)$site_r[$siteid]['cachetime'];
if($cache_time && $site_r['opencache']){
	require('e/dongpo/zhanqun/lib/cache.php');
	$cachestr=dp_LoadCache($topcid,$cache_time);
	if($cachestr!=false){
		echo $cachestr;
		exit;
	}
	ob_start();
}

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

$GLOBALS['site_r']=$site_r;
$GLOBALS['topcid']=$topcid;
$GLOBALS['siteid']=$siteid;

//原始首页
if($_SERVER['HTTP_HOST']==$site_r['indexdomain'] && $site_r['indexdomain']){
	//页面
	$pr=$empire->fetch1("select sitekey,siteintro from {$dbtbpre}enewspublic limit 1");
	$pagetitle=ehtmlspecialchars($public_r['sitename']);
	$pagekey=ehtmlspecialchars($pr['sitekey']);
	$pagedes=ehtmlspecialchars($pr['siteintro']);
	$url="<a href=\"".ReturnSiteIndexUrl()."\">".$fun_r['index']."</a>";//栏目导航

	$r=$empire->fetch1("select indextemp from ".mob_GetTemptb("enewspubtemp")." limit 1");
	$indextemp=$r['indextemp'];

	$string=DtNewsBq('indexpage'.$hz,$indextemp,0);
	$string=str_replace('[!--newsnav--]',$url,$string);//位置导航
	$string=ReplaceSvars($string,$url,0,$pagetitle,$pagekey,$pagedes,$addr,0);
	$string=str_replace('[!--page.stats--]','',$string);
	echo stripSlashes($string);
	
	if($cache_time && $site_r['opencache']){
		dp_HtmlCache($topcid,ob_get_contents());
		ob_end_flush();
	}
	exit;
}

$classid=$topcid;
if(empty($classid)){
	echo '网站设置错误，无法访问此页 #1';
	exit;
}

$search ='&classid='.$classid;
$tbname=$class_r[$classid][tbname];
$mid=$class_r[$classid][modid];
if(empty($tbname)||empty($mid)||InfoIsInTable($tbname))
{
	echo '网站设置错误 #2';
	exit;
}
$cr=$empire->fetch1("select classid,classpagekey,intro,classimg,cgroupid,islist,classtempid,listdt,bdinfoid,repagenum,islast,infos from {$dbtbpre}enewsclass where classid='$classid'");
if(empty($cr['classid']))
{
	echo '网站设置错误 #3';
	exit;
}
if($class_r[$classid]['islast']&&$cr['bdinfoid'])
{
	echo '网站设置错误 #4';
	exit;
}

$GLOBALS['navclassid']=$classid;
$url=ReturnClassLink($classid);
$pagetitle=$class_r[$classid]['classname'];
$pagekey=$cr['classpagekey'];
$pagedes=$cr['intro'];
$classimg=$cr['classimg']?$cr['classimg']:$public_r[newsurl].'e/data/images/notimg.gif';
//---封面式---
if(!$class_r[$classid][islast]&&$cr['islist']!=1)
{

	if($cr[islist]==2)
	{
		$classtemp=GetClassText($classid);
		$dttempname='classpage'.$classid;
	}
	else
	{
		if(empty($cr['classtempid']))
		{
			printerror('ErrorUrl','',1);
		}

		$mbr=$empire->fetch1("select temptext from ".mob_GetTemptb("enewsclasstemp")." where tempid='$cr[classtempid]'");
		$classtemp=$mbr['temptext'];

		$dttempname='classtemp'.$cr['classtempid'];
	}
	$string=DtNewsBq($dttempname.$hz,$classtemp,0);
	$string=str_replace('[!--newsnav--]',$url,$string);//位置导航
	$string=Class_ReplaceSvars($string,$url,$classid,$pagetitle,$pagekey,$pagedes,$classimg,$addr,0);
	$string=str_replace('[!--page.stats--]','',$string);
	echo stripSlashes($string);

	if($cache_time && $site_r['opencache']){
		dp_HtmlCache($topcid,ob_get_contents());
		ob_end_flush();
	}

	exit();
}
//---列表式---
$add='';
//栏目
if($class_r[$classid][islast])//终极栏目
{
	$add.="classid='$classid'";
	$have_class=0;
}
else
{
	$add.=ReturnClass($class_r[$classid][sonclass]);
	$have_class=1;
}
//排序
if(empty($class_r[$classid][reorder]))
{
	$addorder="newstime desc";
}
else
{
	$addorder=$class_r[$classid][reorder];
}
//列表模板
$tempid=$class_r[$classid]['dtlisttempid']?$class_r[$classid]['dtlisttempid']:$class_r[$classid]['listtempid'];
if(empty($tempid))
{
	printerror('此信息不存在','',1,0,1);
}
$tempr=$empire->fetch1("select tempid,temptext,subnews,listvar,rownum,showdate,modid,subtitle,docode from ".mob_GetTemptb("enewslisttemp")." where tempid='$tempid'");
if(empty($tempr[tempid]))
{
	printerror('此信息不存在','',1,0,1);
}
$page=(int)$_GET['page'];
$page=RepPIntvar($page);
$start=0;
$line=$class_r[$classid]['lencord'];//每页显示记录数
$page_line=10;//每页显示链接数
$offset=$page*$line;//总偏移量
//系统模型
$ret_r=ReturnReplaceListF($mid);
//优化
$yhadd='';
$yhid=$class_r[$classid][yhid];
$yhvar='qlist';
if($yhid)
{
	$yhadd=ReturnYhSql($yhid,$yhvar,1);
}
//总数
$totalnum=(int)$_GET['totalnum'];
if($totalnum<1)
{
	if($yhadd)
	{
		$totalquery="select count(*) as total from {$dbtbpre}ecms_".$tbname." where ".$yhadd.$add;
		$num=$empire->gettotal($totalquery);
	}
	else
	{
		$num=ReturnClassInfoNum($cr,0);
	}
}
else
{
	$num=$totalnum;
}
$search.='&totalnum='.$num;
$query="select ".ReturnSqlListF($mid)." from {$dbtbpre}ecms_".$tbname." where ".$yhadd.$add;
$query.=" order by ".ReturnSetTopSql('list').$addorder." limit $offset,$line";
$sql=$empire->query($query);
//伪静态
$pagefunr=eReturnRewriteClassUrl($classid,0);
$pagefunr['repagenum']=$cr['repagenum'];
$pagefunr['dolink']=empty($class_r[$classid]['classurl'])?$public_r['newsurl'].$class_r[$classid]['classpath'].'/':$class_r[$classid]['classurl'].'/';
$pagefunr['dofile']='index';
$pagefunr['dotype']=$class_r[$classid]['classtype'];
//分页
if($pagefunr['rewrite']==1||$pagefunr['repagenum'])
{
	$listpage=InfoUsePage($num,$line,$page_line,$start,$page,$search,$pagefunr);
}
else
{
	$listpage=page1($num,$line,$page_line,$start,$page,$search);
}
//页面支持标签
if($public_r['dtcanbq'])
{
	$tempr[temptext]=DtNewsBq('list'.$tempid.$hz,$tempr[temptext],0);
}
else
{
	if($public_r['searchtempvar'])
	{
		$tempr[temptext]=ReplaceTempvar($tempr[temptext]);
	}
}
$listtemp=$tempr[temptext];
$rownum=$tempr[rownum];
if(empty($rownum))
{$rownum=1;}
$formatdate=$tempr[showdate];
$subnews=$tempr[subnews];
$subtitle=$tempr[subtitle];
$docode=$tempr[docode];
$modid=$tempr[modid];
$listvar=str_replace('[!--news.url--]',$public_r[newsurl],$tempr[listvar]);
//公共
$listtemp=str_replace('[!--newsnav--]',$url,$listtemp);//位置导航
$listtemp=Class_ReplaceSvars($listtemp,$url,$classid,$pagetitle,$pagekey,$pagedes,$classimg,$addr,0);
$listtemp=str_replace('[!--page.stats--]','',$listtemp);
$listtemp=str_replace('[!--show.page--]',$listpage,$listtemp);
$listtemp=str_replace('[!--show.listpage--]',$listpage,$listtemp);
$listtemp=str_replace('[!--list.pageno--]',$page+1,$listtemp);
//取得列表模板
$list_exp="[!--empirenews.listtemp--]";
$list_r=explode($list_exp,$listtemp);
$listtext=$list_r[1];
$no=$offset+1;
$changerow=1;
while($r=$empire->fetch($sql))
{
	//替换列表变量
	$repvar=ReplaceListVars($no,$listvar,$subnews,$subtitle,$formatdate,$url,$have_class,$r,$ret_r,$docode);
	$listtext=str_replace("<!--list.var".$changerow."-->",$repvar,$listtext);
	$changerow+=1;
	//超过行数
	if($changerow>$rownum)
	{
		$changerow=1;
		$string.=$listtext;
		$listtext=$list_r[1];
	}
	$no++;
}
//多余数据
if($changerow<=$rownum&&$listtext<>$list_r[1])
{
	$string.=$listtext;
}
$string=$list_r[0].$string.$list_r[2];
echo stripSlashes($string);

if($cache_time && $site_r['opencache']){
	dp_HtmlCache($topcid,ob_get_contents());
	ob_end_flush();
}

db_close();
$empire=null;