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





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

include("global.php");


if( $job == "makeHtml" ){

	@$fid = $_GET['fid'];//当前栏目fid
	$updatapage = $_GET['updatapage'];//总和生成文章数量
	
	if( $updatapage == 'all' ){/******    第一次循环要计算出  文章总数  和  最大aid    ******/
		
		//取得  mid
		$query="SELECT mid FROM `w6_sort` WHERE `id`='".$fid."'";
		$result=mysql_query($query);
		$mid     =  mysql_result($result,"0","mid");
		//取得 fid 栏目文章总数
		$query   =  "SELECT fid FROM `w6_module_content_$mid` WHERE `shows` = '1' AND `fid`='".$fid."'";
		$result  =  mysql_query($query);
		$totalNum     =  mysql_num_rows($result);
		//取最大 aid
		$query   =  "SELECT max( aid ) FROM w6_module_content_$mid WHERE `shows` = '1' AND `fid`='".$fid."'";
		$result  =  mysql_query($query);
		$max_aid =  mysql_result($result,"0","max( aid )");
		
		$aid = $max_aid;//第一次循环时 aid = max_aid
		$updatapage = $totalNum;////第一次循环时 updatapage = totalNum 栏目文章总数
		
	}


	$aid || $aid = $_GET['aid'];//得到当前要更新的文章aid

	//导航菜单
	$menu = get_menudb();
	//取得 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"));
	$spe_htmlname_set   =  stripslashes(mysql_result($result,"0","spe_htmlname_set"));
	$spe_htmlname   =  stripslashes(mysql_result($result,"0","spe_htmlname"));

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

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

//推荐
$tj_list_db = get_listdb( $fid ,' ORDER BY `lastview` 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 , 20 );

	
/**
*上一篇与下一篇,比较影响速度
**/
$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'] .','.$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> >> 正文';




//列表页默认模板设置	
$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`,`tpl_adv_set`,`tpl_adv_swap`,`tpl_adv_swap2`,`tpl_adv_css`,`tpl_adv_css2` 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_adv_set     =  mysql_result($result,"0","tpl_adv_set");
$tpl_adv_swap     =  mysql_result($result,"0","tpl_adv_swap");
$tpl_adv_swap2     =  mysql_result($result,"0","tpl_adv_swap2");
$tpl_adv_css     =  mysql_result($result,"0","tpl_adv_css");
$tpl_adv_css2     =  mysql_result($result,"0","tpl_adv_css2");

$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>";
	
	//静态链接处理
	$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($Pid){ $html=preg_replace($Pid_rules_str,$Pid,$html); }//淘宝客pid替换
	
	if( $passwd ){//有栏目密码
	$html="<META HTTP-EQUIV=REFRESH CONTENT='0;URL=$webdb[www_url]/show.php?aid=$aid'>";
	}
	
	
	$htmlname = $show_db['htmlname'];

	//取得静态文件名
	$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 );
	}
	if ( !file_exists( $dir ) ) {
		notfate_any_mkdir("$dir",0777);
	}


	//由于css名称中不能用 . 所以替换成 _
	$htmlname=str_replace( "." , "_" , $htmlname );
	//模板高级设置
	if( $tpl_adv_set == 1 ){
	// 1 替换内容
	eval("\$tpl_adv_swap2 = \"$tpl_adv_swap2\";");
	//exit ( "\$tpl_adv_swap2 = \"$tpl_adv_swap2\";" );
	$html=str_replace( $tpl_adv_swap , $tpl_adv_swap2 , $html );
	// 2 复制css模板 并替换内容
	$filetype = GetFileType($tpl_adv_css);
	$newcssfiletname = $htmlname.'.'.$filetype;
	//exit ( $newcssfiletname );
	eval("\$tpl_adv_css2 = \"$tpl_adv_css2\";");
	//exit ( "\$tpl_adv_css2 = \"$tpl_adv_css2\";" );
	$html=str_replace( $tpl_adv_css , $tpl_adv_css2 , $html );
	$css = read_file("./tpl/$style/$tpl_adv_css");
	$css=str_replace( $tpl_adv_swap , $tpl_adv_swap2 , $css );
	updatetofile( $dir."/".$newcssfiletname , $css );
	//copy ( "./tpl/$style/$tpl_adv_css" , $dir."/".$newcssfiletname );
	}
	
	
	updatetofile( $dir.'/'.$filename , $html );
	
	$updatapage--;
	
		if( $updatapage > 0 ){
			jsGo2Url("$webdb[www_url]/MakeHtmlShow.php?job=makeHtml&action=$action&aid=".$backdb['aid']."&updatapage=$updatapage&MHSsp=$MHSsp&cache_id=$cache_id");//updatapage要生成的静态页数
			echo "正在生成文章: $dir/$filename ";
			exit();
		}else{
			echo "更新完成！";
			if($action == "MakeHtmlShow"){
			$MHSsp++;
			jsGo2Url("$webdb[www_url]/$admin_dir/sys/MakeHtml2.php?job=makeHtml&action=$action&MHSsp=$MHSsp&cache_id=$cache_id");
			}
		}

}

?>