<?php
defined('PHP168_PATH') or die();

/**
* 首页生成静态
**/

//$this_controller->check_admin_action($ACTION) or message('no_privilege');
if($_POST['id']){
	$form = '';
	foreach($_POST['id'] as $site){
		$form .= '<form action="'.$this_url.'" method="post" id="'. $site .'" target="'. $site .'">'.
			'<input type="hidden" name="'.$site.'">'.
			'<input type="hidden" name="site" value="'.$site.'">'.
			'</form>'.
			'<iframe style="display: none;" name="'. $site .'"></iframe>'.
			'<script type="text/javascript">document.getElementById("'. $site .'").submit();</script>';
	}
	message($P8LANG['html_success'].$form,$this_router.'/farm-list',2);
	exit;
}else{	
	$site = $_POST['site'];
	$site or message('html_site_must_be_selected');
	$sdata = $this_system->get_site($site);
	if(empty($sdata['domain'])){
		if(REQUEST_METHOD == 'GET'){
			message('html_fail');
		}else{
			exit(p8_json(array('-1')));
		}
	}
	//定义生成静态的常量
	define('P8_GENERATE_HTML', true);
	$uri = '/s.php/'. $site;

	$_SERVER['_REQUEST_URI'] = $uri;
	
	//强制更新缓存
	/*
	$LABEL = &$core->load_module('label');
	$query = $core->DB_master->query("SELECT id FROM $LABEL->table WHERE site = '$site'");
	while($v = $core->DB_master->fetch_array($query)){
		$LABEL->cache($v['id']);
	}
	*/
	$index_file = $this_system->path.'/html/'.$site.'/index.html';
	$index_file_bak = $this_system->path.'/html/'.$site.'/index_bak.html';

	ob_start();
	require PHP168_PATH .'s.php';
	$content = ob_get_clean();
	
	
	//文件大于300字节生成
	if(strlen($content)>300 || !file_exists($index_file_bak) || filesize($index_file_bak)<300){
		/*生成新文件替换前换先复制备份*/
		if(file_exists($index_file) && filesize($index_file)>300) cp($index_file, $this_system->path .'/html/'.$site.'/index_bak.html');				
		write_file($index_file, $content);
		/*判断文件是否生成，如生成应大于300字节*/
		if(filesize($index_file) < 300 && filesize($index_file_bak) > 300)
			cp($this_system->path .'/html/'.$site.'/index_bak.html',$index_file);			
	}
	@chmod($index_file, 0644);

	if(REQUEST_METHOD == 'GET'){
		message('html_success',$sdata['domain'],2);
	}else{
		exit(p8_json(array($sdata['domain']?$sdata['domain']:$sdata['htmlurl'])));
	}
}