<?php
!defined('M_COM') && exit('No Permission');
function index_static($cnstr = '',$needwri = 1){
	global $db,$tblprefix,$cms_abs,$sid,$homedefault,$enablestatic,$cnindexcircle,$cms_abs,$hometpl,$timestamp,$btags;
	$item = array();
	if(!$cnstr){
		$tplname = !$sid ? $hometpl : $btags['hometpl'];
		if(!$enablestatic || !$tplname || !($template = load_tpl($tplname))){//子站的各种配置切换
			index_unstatic($cnstr,$needwri);
			return false;
		}
		$htmlfile = M_ROOT.($sid ? cn_htmldir($cnstr,$sid,1) : '').$homedefault;
		$item['rss'] = $cms_abs.'rss.php'.($sid ? "?sid=$sid" : '');
	}else{
		if(!$cnode = cnodearr($cnstr,$sid)) return false;
		if(!$enablestatic || !cn_allowstatic($cnstr,$sid) || !($tplname = $cnode['indextpl']) || !($template = load_tpl($tplname))){
			index_unstatic($cnstr,$needwri);
			return false;
		}
		$htmlfile = M_ROOT.cn_htmldir($cnstr,$sid,1).$homedefault;
		$item = cn_parsearr($cnstr,$sid,-1,'main');
		re_cnode($item,$cnstr,$cnode);
	}
	parse_str($cnstr,$temparr);
	$temparr['nsid'] = $sid;

	$item['m_thumbid'] = 'main';
	nreplace($template,'up',$item);
	nreplace($template,'c',$temparr);
	nreplace($template,'u',$item);
	nreplace($template,'b',$item);
	code_parse($template);
	uparam_reset();
	$template .= "<script language=\"javascript\" src=\"".$cms_abs."static.php?mode=cnindex".($sid ? "&sid=$sid" : '').($cnstr ? "&$cnstr" : '')."\"></script>";
	@str2file($template,$htmlfile);
	if($needwri){
		if($cnstr){
			$db->query("UPDATE {$tblprefix}cnodes SET ineedstatic='".($timestamp + (!$cnindexcircle ? 86400*365 : $cnindexcircle * 60))."' WHERE ename='$cnstr'");
		}elseif($sid){
			$db->query("UPDATE {$tblprefix}subsites SET ineedstatic='".($timestamp + (!$cnindexcircle ? 86400*365 : $cnindexcircle * 60))."' WHERE sid='$sid'");
		}else $db->query("UPDATE {$tblprefix}mconfigs SET value='".($timestamp + (!$cnindexcircle ? 86400*365 : $cnindexcircle * 60))."' WHERE varname='ineedstatic'");
	}
	unset($template,$item,$temparr,$cnode);
	return true;
}
function list_static($cnstr = '',$isbk = 0,$needwri = 1){
	global $db,$tblprefix,$cms_abs,$liststaticnum,$virtualurl,$sid,$enablestatic,$cnlistcircle,$cms_abs,$timestamp;
	if(!$cnode = cnodearr($cnstr,$sid)) return false;
	if(!$enablestatic || !cn_allowstatic($cnstr,$sid) || !($tplname = $cnode[$isbk ? 'bktpl' : 'listtpl']) || !($template = load_tpl($tplname))){
		list_unstatic($cnstr,$isbk,$needwri);
		return false;
	}
	view_cnurl($cnstr,$cnode);
	$item = cn_parsearr($cnstr,$sid,-1,'main');
	re_cnode($item,$cnstr,$cnode);
	$filepre = cn_htmldir($cnstr,$sid,1).($isbk ? 'bk_' : '');

	parse_str($cnstr,$temparr);
	$temparr['nsid'] = $sid;
	$pagenum = 1;//初始化页数
	$item['m_thumbid'] = 'main';
	if($mptag = fetch_mptag($template)){//首先分析分页情况
		$mpinfo = mpinfo($mptag,$temparr);
		$mpinfo['durlpre'] = $cms_abs.en_virtual('list.php?'.($isbk ? 'bk=1&' : '').($sid ? "sid=$sid&" : '').$cnstr.'&page={$page}',1);
		$mpinfo['surlpre'] = substr($cnode[$isbk ? 'bkurl' : 'listurl'],0,-6).'{$page}.html';
		$mpinfo['static'] = 1;
		$pagenum = empty($liststaticnum) ? $mpinfo['pcount'] : min($mpinfo['pcount'],$liststaticnum);
	}

	for($i = 1;$i <= $pagenum;$i++){
		$temparr['nowpage'] = $i;
		$pagebody = $template;
		nreplace($pagebody,'up',$item);
		if(!empty($mpinfo)){//存在分页标识
			$mpinfo['nowpage'] = $i;
			$item['mpnav'] = mpnav($mpinfo,$liststaticnum);
			nreplace($pagebody,'p',$temparr);
		}
		nreplace($pagebody,'c',$temparr);
		nreplace($pagebody,'u',$item);
		nreplace($pagebody,'b',$item);
		code_parse($pagebody);
		uparam_reset();
		$pagebody .= "<script language=\"javascript\" src=\"".$cms_abs."static.php?mode=".($isbk ? 'cnbk' : 'cnlist').($sid ? "&sid=$sid" : '').($cnstr ? "&$cnstr" : '')."\"></script>";
		@str2file($pagebody,M_ROOT.$filepre.$i.'.html');
	}
	if($needwri){
		$db->query("UPDATE {$tblprefix}cnodes SET ".($isbk ? 'bk' : 'l')."needstatic='".($timestamp + (!$cnlistcircle ? 86400*365 : $cnlistcircle * 60))."' WHERE ename='$cnstr'");
	}
	unset($pagebody,$template,$item,$temparr,$mpinfo,$cnode);
	return;
}
function index_unstatic($cnstr='',$needwri=1){
	global $db,$tblprefix,$sid,$cnindexcircle,$homedefault,$timestamp;
	if($cnstr){
		if(!($cnode = cnodearr($cnstr,$sid))) return;
		cn_blank($cnstr,'i',$sid,1);
		$needwri && $db->query("UPDATE {$tblprefix}cnodes SET ineedstatic='".($timestamp + (!$cnindexcircle ? 86400*365 : $cnindexcircle * 60))."' WHERE ename='$cnstr'");
	}elseif($sid){
		cn_blank($cnstr,'i',$sid,1);
		$needwri && $db->query("UPDATE {$tblprefix}subsites SET ineedstatic='".($timestamp + (!$cnindexcircle ? 86400*365 : $cnindexcircle * 60))."' WHERE sid='$sid'");
	}else{
		@unlink(M_ROOT.$homedefault);
		$needwri && $db->query("UPDATE {$tblprefix}mconfigs SET value='".($timestamp + (!$cnindexcircle ? 86400*365 : $cnindexcircle * 60))."' WHERE varname='ineedstatic'");
	}
	return true;
}
function list_unstatic($cnstr,$isbk = 0,$needwri=1){
	global $db,$tblprefix,$sid,$cnlistcircle,$timestamp;
	if(!($cnode = cnodearr($cnstr,$sid))) return;
	cn_blank($cnstr,$isbk ? 'bk' : 'l',$sid,1);
	$needwri && $db->query("UPDATE {$tblprefix}cnodes SET ".($isbk ? 'bk' : 'l')."needstatic='".($timestamp + (!$cnlistcircle ? 86400*365 : $cnlistcircle * 60))."' WHERE ename='$cnstr'");
	return;
}

?>