<?php
header("Cache-control: no-cache");
header('Content-type:text/html;charset=utf-8');
include ("include/config.php");
$id = htmlspecialchars(trim($_GET['id']), ENT_QUOTES);
$deid = num_decode($id);
if(!($id)){
	header('Content-Type: text/html');
	header("HTTP/1.1 404 Not Found");
	echo '<html>';
	echo '<head><title>404 Not Found</title></head>';
	echo '<body>';
	echo '<center><h1>404 Not Found</h1></center>';
	echo '<hr><center>nginx</center>';
	echo '</body>';
	echo '</html>';
	exit();
}
//当前域名
require_once('include/safe_8.php');
$crhe = $type . $deid;
$filename  = md5($crhe);    //缓存文件名
$filepath = $cacheurl .  'view/'.substr($filename,0,2).'/'.substr($filename,2,2).'/';//缓存保存的路径目录
$fdir = $filepath.$filename.'.inc';
!is_dir($filepath) ? mkdir($filepath, 0777, true) : null; //创建目录
if (!file_exists($fdir)) {
if($_GET['type'] != 'pic'){
	include ("include/conn.php");
    include ("include/view.php");
}else{
    include ("include/conn.php");
    include ("include/pic.php");
}
if($info!=null){//标题内容同时不空则写入
//写入缓存
$fp = @fopen($fdir,'w');
@fwrite($fp,$info);
@fclose($fp);
} else {
	header('Content-Type: text/html');
	header("HTTP/1.1 404 Not Found");
	echo '<html>';
	echo '<head><title>404 Not Found</title></head>';
	echo '<body>';
	echo '<center><h1>404 Not Found</h1></center>';
	echo '<hr><center>nginx</center>';
	echo '</body>';
	echo '</html>';
	exit();
}
} else {
//读取本地缓存
    $info = file_get_contents($fdir);
}
	$info = explode("%%", $info);
	
	//print_r($info);

	$title = $info[0];
	$sub = $info[1];
	$thumb = $info[2];
	$time = $info[3];
	$body = $info[4];
	$ftitle = $info[5];
	$fname = $info[6];
	$up = $info[7];
	$down = $info[8];
	$rrelist = $info[9];
	
//屏蔽非法词
$keypb = $title . $sub;
$words = file_get_contents('/data1/ffc/ffc.txt', true);  //读取词库
$words = str_replace("\r\n", "|",$words); //去掉换行符改为|
if(preg_match('/'.$words.'/', $keypb)){
	header('Content-Type: text/html');
	header("HTTP/1.1 404 Not Found");
	echo '<html>';
	echo '<head><title>404 Not Found</title></head>';
	echo '<body>';
	echo '<center><h1>404 Not Found</h1></center>';
	echo '<hr><center>nginx</center>';
	echo '</body>';
	echo '</html>';
	exit();
} 

if($_GET['type'] != $fname){
	header('Content-Type: text/html');
	header("HTTP/1.1 404 Not Found");
	echo '<html>';
	echo '<head><title>404 Not Found</title></head>';
	echo '<body>';
	echo '<center><h1>404 Not Found</h1></center>';
	echo '<hr><center>nginx</center>';
	echo '</body>';
	echo '</html>';
	exit();
}

$body = preg_replace('#src="#i', 'src="//img.yd166.com/', $body);
$body = preg_replace('#</div>#i', '', $body);
$body = preg_replace("/<(div.*?)>/si","",$body);
//$body = substr($body, 0, -1);
$des = mb_substr(strip_tags($body),0,200);
$like= $up . $down;
$like1 = explode("##",$like);
$like1 = array_unique($like1);
$g = 1;
foreach($like1 as $value){
$linkarr = explode("@@",$value);
if($linkarr[2] !=NULL){$sub1 = '（' .$linkarr[2] . '）';}else{$sub1 = '';}
if ($g <= 10 && $linkarr[0] !=NULL) {
		$top4 .= '<li><div class="plist"><dl><dt><a href="/' . $linkarr[5] . '/' . num_encode($linkarr[0]) . '.html" title="' . $linkarr[1] . $sub1 . '" target="_blank"><img alt="' . $linkarr[1] . $sub1 . '" src="' . $picurl . 'thumb/' . $linkarr[3] . '.jpg" /></a></dt><dd><h3><a href="/' . $linkarr[5] . '/' . num_encode($linkarr[0]) . '.html" title="' . $linkarr[1] . $sub1 . '" target="_blank">' . $linkarr[1] . $sub1 . '</a></h3><div class="infot"><a href="/' . $linkarr[5] . '/' . num_encode($linkarr[0]) . '.html" title="' . $linkarr[1] . $sub1 . '" target="_blank">阅读全文>></a><span>' . date('Y-m-d H:i:s',$linkarr[4]) . '</span></div></div></li>';
	}
$g++;
}

//解析本栏目文档
$related = explode("##",$rrelist);
$e = 1;
foreach($related as $value){
	$linkarr = explode("@@",$value);
	if($linkarr[2] !=NULL){$sub1 = '（' .$linkarr[2] . '）';}else{$sub1 = '';}
	if ($e <= 10) {
			$rd1.= '<li>• <a href="/' . $linkarr[5] . '/' . num_encode($linkarr[0]) . '.html">' . $linkarr[1] . $sub1 . '</a></li>';
		}else if($e <= 16) {
			$rd2 .= '<li><div class="plist"><dl><dt><a href="/' . $linkarr[5] . '/' . num_encode($linkarr[0]) . '.html" title="' . $linkarr[1] . $sub1 . '" target="_blank"><img alt="' . $linkarr[1] . $sub1 . '" src="' . $picurl . 'thumb/' . $linkarr[3] . '.jpg" /></a></dt><dd><h3><a href="/' . $linkarr[5] . '/' . num_encode($linkarr[0]) . '.html" title="' . $linkarr[1] . $sub1 . '" target="_blank">' . $linkarr[1] . $sub1 . '</a></h3><div class="infot"><a href="/' . $linkarr[5] . '/' . num_encode($linkarr[0]) . '.html" title="' . $linkarr[1] . $sub1 . '" target="_blank">阅读全文>></a><span>' . date('Y-m-d H:i:s',$linkarr[4]) . '</span></div></div></li>';
		}
	$e++;
}

//图片自动alt及分页代码开始
$windex = 0;
while(preg_match('/<\s*?img.*?src=\"([^"]+)\".*?>/ms',$body))
{
	$windex++;
	$wimgtitle = $title.','.$sub.'('.$windex.')';
	$body = preg_replace('/<\s*?img.*?src=\"([^"]+)\".*?><\/p>/ms','<[wimg] src="$1" alt="'.$wimgtitle.'" /></p>%%%%',$body,1);
}
$cxcishu = mb_substr_count($body,"[wimg]");
$body = preg_replace('/\[wimg\]/','img',$body); 
if($cxcishu >='7'){
	$list2 = array_map(function($item) {
	   return explode('%%%%', $item);
	}, explode('%%%%', $body));
//整理缓存数据分页
	if(!isset($_GET['page']))
	{
	  $page = 1;
	}
	else
	{
	 $page=$_GET['page'];
	}
	$size=4;//每页显示的记录数
	$pnum = ceil(count($list2) / $size); //总页数，ceil()函数用于求大于数字的最小整数
	//用array_slice(array,offset,length) 函数在数组中根据条件取出一段值;array(数组),offset(元素的开始位置),length(组的长度)
	$newarr = array_slice($list2, ($page-1)*$size, $size);
	for($i=0;$i<count($newarr);$i++)
	{
		$list8 .= $newarr[$i][0];
	}


//超出页输出404

	if ($page > $pnum) {
		header('Content-Type: text/html');
		header("HTTP/1.1 404 Not Found");
		echo '<html>';
		echo '<head><title>404 Not Found</title></head>';
		echo '<body>';
		echo '<center><h1>404 Not Found</h1></center>';
		echo '<hr><center>nginx</center>';
		echo '</body>';
		echo '</html>';
		exit();
    }

//栏目上下页链接
	if(!isset($page) || $page<=1){
		$pageup = '<span>首页</span> ';
	}else if($page<=2){
		$pageup = '<a href="/' . $_GET['type'] . '/' . $id . '.html">上一页</a>';
		}else{
		$pageup = '<a href="/' . $_GET['type']  . '/' . $id . '_' . ($page-1) . '.html">上一页</a>';
		}
	if($page>=$pnum) {
		$pagedown = '<span>末页</span>';
	}else{
		$pagedown = '<a href="/' . $_GET['type'] . '/' . $id . '_' . ($page+1) . '.html">下一页</a>';
		}

//栏目全部分页链接
	if($_GET['page'] =='6'){
		$pagenum =range(1,6,1);
	}else if($_GET['page'] =='7'){
		$pagenum =range(2,7,1);
	}else if($_GET['page'] =='8'){
		$pagenum =range(3,8,1);
	}else if($_GET['page'] =='9'){
		$pagenum =range(4,9,1);
	}else if($_GET['page'] =='10'){
		$pagenum =range(5,10,1);
	}else{
		$pagenum =range(1,5,1);
	}

	$p = 0;
	//exit;
	foreach($pagenum as $v)
    {
	if ($p <= 0) {
		if($_GET['page'] <='5'){
			if($_GET['page'] =='' || $_GET['page'] =='1'){
				$pagema .= '<span>1</span>';
			}else{
				$pagema .= '<a href="/' . $_GET['type'] . '/' . $id . '.html">1</a>';
			}
		}
            }else if ($p <= ($pnum-1)) {
				if($_GET['page'] == $pagenum[$p]){
					$pagema1 .= '<span>' . $pagenum[$p] . '</span>';
				}else{
					$pagema1 .= '<a href="/' . $_GET['type'] . '/' . $id . '_' . $pagenum[$p] . '.html">' . $pagenum[$p] . '</a>';
				}
            }
			
		$p++;
	}
}else{
	$list8 .= $body;
}
$list8 = preg_replace('/%%%%/','',$list8); 
$list8 = keywords($list8);
$head = file_get_contents($weburl . 'tpls/headbk-m.html', true);//获取头部
$foot = file_get_contents($weburl . 'tpls/footbk-m.html', true);//获取网站底部
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<title><?=$title;?><?php if($sub!=null){echo '（' . $sub . '）';}?> - <?=$webname;?> - <?=$pcurl;?></title>
<meta name="Keywords" content="<?=$title;?><?php if($sub!=null){echo ',' . $sub;}?>">
<meta name="Description" content="<?=$des;?>">
<meta name="applicable-device" content="mobile">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,minimal-ui">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
<script src="//www.yd166.com/uploads/images/wap/view_m.js" type="text/javascript"></script><script>uaredirect("http://<?=$pcurl;?>/<?=$type;?>/<?=$id;?>.html");</script>
<link href="//www.yd166.com/uploads/images/wap/mcss.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?=$head;?>
<div class="c_1"><script>view_1();</script></div>
<div class="title">
	<h1><?=$title;?><?php if($sub!=null){echo ',' . $sub;}?></h1>
	<p class="viewinfo clearfix"><span><a href="/">首页</a> &gt; <a href='/<?=$fname;?>/'><?=$ftitle;?></a> &gt; </span><span class="zuozhe">作者：YD166</span><span class="fr"><?php echo date('Y-m-d H:i:s',$time);?></span></p>
</div>
<div class="content" id="soft-intro-wrap" style="height:auto; overflow: hidden;">
	<div id="content"><?=$list8;?></div>
</div>
<div class="c_2"><script>view_2();</script></div>
<?php if($cxcishu >='7'){echo '<div class="listpage">' .$pageup . $pagema . $pagema1 . $pagedown .'</div><div class="c_5"><script>view_6();</script></div>';}?>
<div class="tittb"><h2>栏目热文</h2></div>
<div class="txt"><ul><?=$top4;?></ul></div>
<div class="c_3"><script>view_3();</script></div>
<div class="tittb"><h2>文档排行</h2></div>
<div class="r_wz"><ul><?=$rd1;?></ul></div>
<div class="c_4"><script>view_4();</script></div>
<div class="tittb"><h2>本站推荐</h2></div>
<div class="txt"><ul><?=$rd2;?></ul></div>
<div class="c_5"><script>view_5();</script></div>
<?=$foot;?>
<script type="application/ld+json">
{
	"@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld",
	"@id": "/<?=$fname;?>/<?=$id;?>.html",
	"title": "<?=$title;?><?php if($sub!=null){echo '（' . $sub . '）';}?>",
	"images": ["<?=$picurl;?>thumb/<?=$thumb;?>.jpg"],
	"description": "<?=$des;?>",
	"pubDate":"<?php echo date('Y-m-d H:i:s',$time);?>",
	"upDate":"<?php echo date('Y-m-d H:i:s',$time);?>",
    "data":{
	"WebPage":{
		 "pcUrl":"//www.yd166.com/<?=$fname;?>/<?=$id;?>.html",
		 "wapUrl":"//m.yd166.com/<?=$fname;?>/<?=$id;?>.html",
		 "fromSrc":"原点资讯",
	  }
   }
}
</script>
</body>
</html>