<?
$C_126cmsadmin_tname = 'MakeHtml_config';

include(dirname(__FILE__)."/global.php");



$cache_id = $_GET['cache_id'] ? $_GET['cache_id'] : time();
$cache_makelist = '../../cache/' . $cache_id . '_makeList.php';
$cache_makeshow = '../../cache/' . $cache_id . '_makeShow.php';

@$job = $submitted_vars["job"];
@$action = $submitted_vars["action"];

$sp = $_GET['sp'] ? $_GET['sp'] : 0;

$MHLsp = $_GET['MHLsp'] ? $_GET['MHLsp'] : 0;

$MHSsp = $_GET['MHSsp'] ? $_GET['MHSsp'] : 0;

if($job == "makeHtml"){


	
	if($action == ""){
		$fiddb = $_POST['fiddb'];
		$allfid= implode( ',' , $fiddb );
	
		$str="<?php\n\$allfid='$allfid';";
		writetofile_w( $cache_makelist , $str );
	
		//取每个栏目下要生成的文章aid
		echo '写入列表 fid 缓存成功 '.$allfid.' <meta http-equiv="Refresh" content="0;URL=MakeHtml2.php?job=makeHtml&action=cache_makeshow&sp=0&cache_id='. $cache_id .'" />';
	}
	
	
	
	if($action == "cache_makeshow"){
		
		if($sp==0){
		$str="<?php\n";
		writetofile_w( $cache_makeshow , $str );
		}
		
			include($cache_makelist);
			$fiddb = explode(',', $allfid);
			$total = count($fiddb);
		
			$fid = $fiddb[$sp];
		
			$sql = "SELECT * FROM `w6_sort` WHERE `id`='".$fid."'";
			$result=mysql_query($sql);
			$mid =  mysql_result($result,"0","mid");
		
			$sql = "SELECT `aid`,`fid`,`shows` FROM `w6_module_content_$mid` WHERE `fid` = '$fid' AND `shows` = '1'";
			$result=mysql_query($sql);
			$num = mysql_num_rows($result);
			if( $num == 0 ){
				$allaid = '-1';
				$str = "\$fiddb['$sp'] = $fid;\$aiddb['$sp'] = '$allaid';\n";
				writetofile( $cache_makeshow , $str );
				$sp++;
			}else{
		
				$s = 1;
				while( $rs  = mysql_fetch_array($result) ){
					if($s == 1){
						$allaid = $rs['aid'];
					}else{
						$allaid .= ',' . $rs['aid'];
					}
					$s++;
				}
				$str = "\$fiddb['$sp'] = $fid;\$aiddb['$sp'] = '$allaid';\n";
				writetofile( $cache_makeshow , $str );
				$sp++;

			}
			
		if($sp < $total){
			echo '写入栏目 '.$fid.' 内容成功 '.$allaid.'  <meta http-equiv="Refresh" content="0;URL=MakeHtml2.php?job=makeHtml&action=cache_makeshow&sp='. $sp .'&cache_id='. $cache_id .'" />';
		}else{
			echo '开始更新列表内容 <meta http-equiv="Refresh" content="0;URL=MakeHtml2.php?job=makeHtml&action=MakeHtmlList&cache_id='. $cache_id .'" />';
		}

	}
	
	
	
	if($action == "MakeHtmlList"){
	
		include($cache_makelist);
		$fiddb = explode(',', $allfid);
		$total = count($fiddb);
		$fid = $fiddb[$MHLsp];
	
		if($MHLsp<$total){
			echo '更新列表 '.$fid.' <meta http-equiv="Refresh" content="0;URL=../../list.php?job=makeHtml&action=MakeHtmlList&fid='. $fid .'&updatapage=all&MHLsp='. $MHLsp .'&cache_id='. $cache_id .'" />';
		}else{
			echo '开始更新文章内容页 <meta http-equiv="Refresh" content="0;URL=MakeHtml2.php?job=makeHtml&action=MakeHtmlShow&cache_id='. $cache_id .'" />';
			unlink ($cache_makelist);
		}
	}
	
  
  
	if($action == "MakeHtmlShow"){
		include($cache_makeshow);
		$total = count($fiddb);
		if($MHSsp<$total){
			if ( $aiddb[$MHSsp] == '-1' ){
				$MHSsp++;
				echo '<meta http-equiv="Refresh" content="0;URL=MakeHtml2.php?job=makeHtml&action=MakeHtmlShow&MHSsp='. $MHSsp .'&cache_id='. $cache_id .'" />';
			}else{
				$fid = $fiddb[$MHSsp];
				echo '开始更新栏目 '.$fid.' 文章内容页<meta http-equiv="Refresh" content="0;URL=../../MakeHtmlShow.php?job=makeHtml&action=MakeHtmlShow&fid='.$fid.'&updatapage=all&MHSsp='. $MHSsp .'&cache_id='. $cache_id .'" />';
			}
		}else{
			echo '静态页更新完成 ';
			unlink ($cache_makeshow);
		}

	}
	
  
  
}


//写入文件,当文件存在时替换
function writetofile_w($filename,$book)
{
$fd=fopen("$filename","w");
fwrite($fd,$book);
fclose($fd);
return true;
}