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

include("global.php");

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


@$job=$submitted_vars['job'];
@$action=$submitted_vars['action'];
@$MHLsp=$submitted_vars['MHLsp'];
@$cache_id=$submitted_vars['cache_id'];

$fid = $_GET['fid'];
$page = isset($_GET['page']) ? $_GET['page'] : 1;  //内容分页  当前页

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

//取得 mid
$query="SELECT * FROM `w6_sort` WHERE `id`='".$fid."'";
$result=mysql_query($query);
$mid       =  mysql_result($result,"0","mid");
$fname     =  mysql_result($result,"0","name");
$maxperpage     =  mysql_result($result,"0","maxperpage");
$list_html   =  stripslashes(mysql_result($result,"0","list_html"));
$titlekeywords    =  stripslashes(mysql_result($result,"0","titlekeywords"));
$metakeywords     =  stripslashes(mysql_result($result,"0","metakeywords"));
$metadescription  =  stripslashes(mysql_result($result,"0","metadescription"));
$htmldescription  =  stripslashes(mysql_result($result,"0","htmldescription"));
$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"));

$type  =  stripslashes(mysql_result($result,"0","type"));

$SpecialSql  =  stripslashes(mysql_result($result,"0","SpecialSql"));//如果是专题页，要读取专题sql

$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"));

$passwd    =  mysql_result($result,"0","passwd");

//判断栏目是否设置密码
if( strlen($passwd) != 0 && $job != "makeHtml" ){

  //栏目密码验证函数
  listpasswd( $passwd );
  
}

//取得列表页显示的内容数量
if( $maxperpage != 0 ){
	$article_page_size = $maxperpage;
}

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

	$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');//取分页
	$sortpage = str_replace("&sPage=$sPage","",$sortpage);
	

}else{

	$url = '?fid='.$fid;

}





if($type == "3"){//专题页处理

	$SpecialSql_a = explode('[||]',$SpecialSql);
	$SpecialSql_1 = $SpecialSql_a['0'];
	$SpecialSql_2 = $SpecialSql_a['1'];
	//取得栏目分页
	eval("\$SpecialSql_1 = \"$SpecialSql_1\";");//exit($SpecialSql_1);
	$result=mysql_query($SpecialSql_1);
	$num=mysql_num_rows($result);
	$showpage = getpage($url,$article_page_size,$num,$page);//exit($showpage);
	//取得栏目数据
	$star_page = ( $page-1 ) * $article_page_size;
	eval("\$SpecialSql_2 = \"$SpecialSql_2\";");
	$SpecialSql_2 = str_replace("{=star_page=}","\$star_page",$SpecialSql_2);//exit($SpecialSql_2);
	$list_db = get_Special_listdb( $fid , $SpecialSql_2 , $star_page );
	
}else{


//取分页及当前栏目内容
$query = "SELECT * FROM `w6_module_content_".$mid."` WHERE `shows` = '1' AND `fid` = '".$fid."'";
$result=mysql_query($query) or die("Could not connect: " . mysql_error());
$num=mysql_num_rows($result);//取得当前栏目内容总数
$showpage = getpage($url,$article_page_size,$num,$page);//取分页

$sql = ' ORDER BY `content_list` DESC LIMIT '.(($page-1)*$article_page_size).' , '.$article_page_size;
$list_db = get_listdb( $fid , $sql);

if($open_sort_config == "Y") $showpage = str_replace("&page=$page","",$showpage);//开启小栏目分页



}







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

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

//推荐
//$tj_list_db = get_listdb( $fid ,' ORDER BY `lastview` DESC LIMIT 0 , 8 ' );

//取得当前栏目所有内容
//$all_list_db = get_listdb( $fid ,' ORDER BY `count` DESC LIMIT 48 , 100 '  );



//head
if( $titlekeywords ){	$web_title=$titlekeywords;	}else{	$web_title=$fname .' > '. $webdb['sitename1'];	}
//在栏目分页加页次
if( $page != 1 ){	if( $titlekeywords ){	$web_title=$titlekeywords.' - ( '.$page.' )';	}else{	$web_title=$fname .'( '.$page.' ) > '. $webdb['sitename1'];	}  }
//if( $metakeywords ){	$web_keywords=$metakeywords;	}else{	$web_keywords=$fname .','. $webdb['sitekeywords'];	}
//if( $metadescription ){	$web_description=$metadescription;	}else{	$web_description=$fname .' > '. $webdb['sitedescription'];;	}

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



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

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

$tpl_head ? $tpl_head = $tpl_head : $tpl_head = "./tpl/$style/head.htm";
$tpl_foot ? $tpl_foot = $tpl_foot : $tpl_foot = "./tpl/$style/foot.htm";
$tpl_list ? $tpl_list = $tpl_list : $tpl_list = $tpl_list_default;

//生成静态页
ob_start();
require($tpl_head);
require($tpl_list);
require($tpl_foot);
$html = ob_get_contents();
ob_end_clean();
$html=preg_replace("/<!--126cms(.*?)126cms-->/is","\\1",$html);

if($webdb['NewsMakeHtml']=='1'){
	//处理列表页分页
	$html=preg_replace("/ href=('|\"|)([-_a-z0-9\.:\/]{0,}\/|)\?fid=([\d]+)&page=([\d]+)/eis","make_list_page_html_Function('\\3','\\4',' href=\\2')",$html);
	//静态链接处理
	$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,}\/|)\?fid=([\d]+)&page=([\d]+)/eis","make_list_page_WJT_Function('\\3','\\4',' href=\\2')",$html);
	//伪静态链接处理
	$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"){
	
	if( $passwd ){//有栏目密码
	$html="<META HTTP-EQUIV=REFRESH CONTENT='0;URL=$webdb[www_url]/list.php?fid=$fid'>";
	}
	
	$totalpae = ceil( $num/$article_page_size );//取得文章列表页总页数
	
	$updatapage = $_GET['updatapage'];
	
		if( $updatapage > $totalpae || $updatapage == 'all' ) $updatapage = $totalpae;

	$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 );
	
		if($page == 1){
			$filename = 'index.html';
		}else{
			$filename = str_replace( '{$page}' , 'index-'.$page , $filename );
		}
	
	//$filename = str_replace( '{$page}' , $aid , $filename );
	if ( !file_exists( $dir ) ) {
		notfate_any_mkdir("$dir",0777);
	}
	
	updatetofile( $dir . $filename , $html );
	
	$updatapage--;
	$page++;
	
		if( $updatapage > 0 ){//只有一页时是 = 0 , 0 篇文章时是 < 0
			jsGo2Url("$webdb[www_url]/list.php?job=makeHtml&action=$action&fid=".$fid."&updatapage=$updatapage&page=$page&MHLsp=$MHLsp&cache_id=$cache_id");//updatapage要生成的静态页数
			exit();
		}
		/*elseif( $updatapage == 0 ){
			jsGo2Url("$webdb[www_url]/list.php?job=makeHtml&fid=".$fid."&updatapage=$updatapage&page=$page");//updatapage要生成的静态页数
			exit();
		}
	*/
	echo "更新完成！";
	if($action == "MakeHtmlList"){
	$MHLsp++;
	jsGo2Url("$webdb[www_url]/$admin_dir/sys/MakeHtml2.php?job=makeHtml&action=$action&MHLsp=$MHLsp&cache_id=$cache_id");
	}
	exit();
}else{

	echo $html;
}

?>