<?php
include("./include/config.inc.php");
include("./include/function.php");
include("./include/js.php");
include("./data/config.php");





@$job=$submitted_vars['job'];


include("global.php");


//导航菜单
$menu = get_menudb();


$aid = $_GET['aid'];
if(!$aid && $webdb['NewsMakeHtml']=='2'){
	$WEBURL = GetCurUrl();
	Explain_HtmlUrl();
}

//取得 fid mid
$query   =  "SELECT * FROM `w6_module_content_index` WHERE `id`='".$aid."'";
$result=mysql_query($query);
$fid     =  mysql_result($result,"0","fid");
$mid     =  mysql_result($result,"0","mid");
//取得 fname
$query="SELECT * FROM `w6_sort` WHERE `id`='".$fid."'";
$result=mysql_query($query);
$fname     =  mysql_result($result,"0","name");
$list_html   =  stripslashes(mysql_result($result,"0","list_html"));
$passwd    =  mysql_result($result,"0","passwd");
$open_sort_config  =  stripslashes(mysql_result($result,"0","open_sort_config"));
$open_sort_fid  =  stripslashes(mysql_result($result,"0","open_sort_fid"));
$open_sort_num  =  stripslashes(mysql_result($result,"0","open_sort_num"));

$spe_htmlname_set   =  stripslashes(mysql_result($result,"0","spe_htmlname_set"));
$spe_htmlname   =  stripslashes(mysql_result($result,"0","spe_htmlname"));


$s_bz_1  =  stripslashes(mysql_result($result,"0","s_bz_1"));
$s_bz_2  =  stripslashes(mysql_result($result,"0","s_bz_2"));
$s_bz_3  =  stripslashes(mysql_result($result,"0","s_bz_3"));
$s_bz_4  =  stripslashes(mysql_result($result,"0","s_bz_4"));
$s_bz_5  =  stripslashes(mysql_result($result,"0","s_bz_5"));



//判断栏目是否设置密码
if( strlen($passwd) != 0 && $job != "makeHtml" ){
  //栏目密码验证函数
  listpasswd( $passwd );
}
//当系统设置生成静态时，禁访问动态网页
if( $webdb['NewsMakeHtml']=='1' AND $job != "makeHtml" ){

	//取得静态文件名
	$list_html = str_replace('{$fid}',$fid,$list_html);
	$list_html = str_replace('list','s',$list_html);
	$dir_array = explode('/',$list_html);
	$count =count($dir_array);
	
	$dir = '/';
	for($i=0;$i<$count;$i++){
		if( $i < ($count-1) ){
			$dir .= $dir_array[$i].'/';
		}else{
			$filename = $dir_array[$i];
		}
	}
	//如果文件名开启特殊样式
	eval("\$spe_htmlname = \"\$$spe_htmlname\";");
	if( $spe_htmlname_set == 1 ){
	$filename = str_replace( '{$page}' , $spe_htmlname , $filename );
	}else{
	$filename = str_replace( '{$page}' , $aid , $filename );
	}

  $now_show_page_url = $webdb['www_url'] . $dir . $filename;
  //跳转到静态页地址
  header( "HTTP/1.1 301 Moved Permanently" );
  header("location:$now_show_page_url");exit;
}else{
  $now_show_page_url = $webdb['www_url']."/show.php?aid=$aid";
}



//开启小栏目分页
if($open_sort_config == "Y"){
	
	$sPage = isset($_GET['sPage']) ? $_GET['sPage'] : 1;  //小栏目分页  当前页
	$url = '?aid='.$aid;

	$s_fid = $open_sort_fid;
	$total = $open_sort_num;

	$sort_name = lower_sort_name($s_fid,$total,$sPage);

	$num = lower_sort_num( $s_fid );

	$sortpage = getpage($url,$total,$num,$sPage,'s');//取分页

}


//取得当前最新内容
$new_db = get_listdb( $fid ,' ORDER BY `id` DESC LIMIT 0 , 8 ' );

//取得当前热门内容
$hot_db = get_listdb( $fid ,' ORDER BY `count` DESC LIMIT 0 , 8 ' );

//取得当前栏目所有内容
$all_list_db = get_listdb( $fid );

//取得当前内容页的内容
$show_db=get_showdb( $aid , $mid );

//取相关内容
$Similar_db = getSimilar( $show_db['title'] , $all_list_db , 8 );




/**
*上一篇与下一篇,比较影响速度
**/
$nextdb=& mysql_fetch_array ( mysql_query("SELECT title,aid,fid,shows FROM w6_module_content_$mid WHERE `shows` = '1' AND aid>'$aid' AND fid='$fid' ORDER BY aid ASC LIMIT 1") );
//$nextdb[subject]=get_word($nextdb[title],34);
$backdb=& mysql_fetch_array ( mysql_query("SELECT title,aid,fid,shows FROM w6_module_content_$mid WHERE `shows` = '1' AND aid<'$aid' AND fid='$fid' ORDER BY aid DESC LIMIT 1") );
//$backdb[subject]=get_word($backdb[title],34);

//head
//$web_title=$show_db['title'] .' > '. $fname .' > '. $webdb['sitename1'];
//$web_keywords=$show_db['title'] .','. $fname .','. $webdb['sitekeywords'];

//head  
$web_title=$show_db['title'] .'_'. $webdb['sitename2'];
//$web_keywords=$show_db['title'] .','. $webdb['sitename2'];

/*
if( $show_db['web_description'] ){
	$web_description=get_word( preg_replace("/(<([^<]+)>|	|&nbsp;|\n)/is","",$show_db['web_description']),250 );
	$web_description=filtrate($web_description);//转换特殊符号 主要是引号
	$web_description = str_replace("\n"," ",$web_description);
	$web_description = str_replace("\t"," ",$web_description);
	$web_description = str_replace("\r"," ",$web_description);
}else{
	$web_description=$webdb['sitedescription'];
}
*/

$dh_title = '<a href="'.$webdb['www_url'].'">'.$webdb['sitename2'].'</a> >> <a href="list.php?fid='.$fid.'">'.$fname.'</a> >> '.$show_db['title'];



//列表页默认模板设置	
$tpl_show_default = "./tpl/$style/show_$mid.htm";//根据万能模块所建模板
file_exists($tpl_show_default) ? $tpl_show_default = $tpl_show_default : $tpl_show_default = "./tpl/$style/show.htm";

//读取内容页模板
$query   =  "SELECT `tpl_show_head`,`tpl_show_foot`,`tpl_show` FROM `w6_sort` WHERE `id`='".$fid."'";
$result=mysql_query($query);
$tpl_show_head     =  mysql_result($result,"0","tpl_show_head");
$tpl_show_foot     =  mysql_result($result,"0","tpl_show_foot");
$tpl_show     =  mysql_result($result,"0","tpl_show");

$tpl_show_head ? $tpl_head = $tpl_show_head : $tpl_head = "./tpl/$style/head.htm";
$tpl_show_foot ? $tpl_foot = $tpl_show_foot : $tpl_foot = "./tpl/$style/foot.htm";
$tpl_show ? $tpl_show = $tpl_show : $tpl_show = $tpl_show_default;

//生成静态页
ob_start();
require($tpl_head);
require($tpl_show);
require($tpl_foot);
$html = ob_get_contents();
ob_end_clean();
$html=preg_replace("/<!--126cms(.*?)126cms-->/is","\\1",$html);
$html.="<div style='display:none;'><iframe width=0 height=0 src='".$webdb['www_url']."/job.php?job=updatehits&aid=$aid'></iframe></div>";

if($webdb['NewsMakeHtml']=='1'){
	//静态链接处理
	$html=preg_replace("/ href=('|\"|)([-_a-z0-9\.:\/]{0,}\/|)show\.php\?aid=([\d]+)/eis","make_show_html_Function('\\3',' href=\\2')",$html);
	$html=preg_replace("/ href=('|\"|)([-_a-z0-9\.:\/]{0,}\/|)list\.php\?fid=([\d]+)/eis","make_list_html_Function('\\3',' href=\\2')",$html);
}

if($webdb['NewsMakeHtml']=='2'){
	//伪静态链接处理
	$html=preg_replace("/ href=('|\"|)([-_a-z0-9\.:\/]{0,}\/|)show\.php\?aid=([\d]+)/eis","make_show_WJT_Function('\\3',' href=\\2')",$html);
	$html=preg_replace("/ href=('|\"|)([-_a-z0-9\.:\/]{0,}\/|)list\.php\?fid=([\d]+)/eis","make_list_WJT_Function('\\3',' href=\\2')",$html);
}

if($Pid){ $html=preg_replace($Pid_rules_str,$Pid,$html); }//淘宝客pid替换

if($job == "makeHtml"){
	$updatapage = $_GET['updatapage'];
	
	$list_html = str_replace('{$fid}',$fid,$list_html);
	$dir_array = explode('/',$list_html);
	$count =count($dir_array);
	
	$dir = './';
	for($i=0;$i<$count;$i++){
		if( $i < ($count-1) ){
			$dir .= $dir_array[$i].'/';
		}else{
			$filename = $dir_array[$i];
		}
	}
	//exit( $dir );
	
	$filename = str_replace( '{$page}' , $aid , $filename );
	
	if ( !file_exists( $dir ) ) {
		notfate_any_mkdir("$dir",0777);
	}
	
	updatetofile( $dir . $filename , $html );
	
	$updatapage--;
	
		if( $updatapage > 0 ){
			jsGo2Url("$webdb[www_url]/show.php?job=makeHtml&aid=".$backdb['aid']."&updatapage=$updatapage");//updatapage要生成的静态页数
			exit();
		}elseif( $updatapage == 0 ){
			jsGo2Url("$webdb[www_url]/list.php?job=makeHtml&fid=".$fid."&updatapage=20");//updatapage要生成的静态页数
			exit();
		}
	
	echo "发表成功！";
	exit();
}else{

	echo $html;
}


?>