<?php include "header.php";?>
<?php
$cover_data = isset($_POST['cover_old_data']) ? $_POST['cover_old_data'] : '' ;

//选择了覆盖原数据库
if($cover_data == 'cover')
{
	if(is_file($src_db_file))
	{
		if(!empty($db_conf) && connectDb($db_conf))
		{
			if(filesize($src_db_file) > 0 )
			{
				$fp = @fopen($src_db_file, 'r');
				while(!feof($fp))
				{
					$row = trim(fgets($fp, 1024*1024));
					if($row != '')
					{
						$row = str_replace('`mo_','`'.$db_conf['prefix'],$row);
						@mysql_query($row);
					}
				}
				fclose($fp);
			}
			else
			{
				echo '数据库文件损坏，安装数据失败';
				exit();
			}
		}
		else
		{
			echo '数据库连接失败，安装数据失败';
			exit();
		}
	}
	else
	{
		echo '数据库文件不存在！安装数据失败';
		exit();
	}
}

//寻找安装的模板文件夹
$path_tpl = INSTALL_DATA.'template'.DIRECTORY_SEPARATOR;
$folder = MoFolder::read($path_tpl,2);
$folder = current($folder);
$source_file = $path_tpl.$folder.DIRECTORY_SEPARATOR;

//创建一个唯一的 真实的安装目录
$template = WEB_DIR.'template'.DIRECTORY_SEPARATOR;
$folder_name = createFolderName($folder,$template);
$target_file = $template.$folder_name.DIRECTORY_SEPARATOR;

//拷贝模板文件到模板目录
MoFolder::copyFolder($source_file,$target_file);

//寻找模板封面图片并拷贝到相关目录
$tpl_cover = MoFolder::readFileByPrefix($target_file,'','jpg');
if(!empty($tpl_cover))
{
	$cover_image = current($tpl_cover);
	$tpl_image  = $target_file.$cover_image;
	$target_cover_image = time().'_'.mt_rand(10,99).'.jpg';
	if(is_file($tpl_image))
	{
		$taget_tpl_image = WEB_DIR.'static'.DIRECTORY_SEPARATOR.'uploadfile'.DIRECTORY_SEPARATOR.'template'.DIRECTORY_SEPARATOR.$target_cover_image;
		rename($tpl_image,$taget_tpl_image);
	}
}
else
{
	$tpl_image  = INSTALL_DIR.'res'.DIRECTORY_SEPARATOR.'tpl_cover.png';
	$target_cover_image = time().'_'.mt_rand(10,99).'.jpg';
	if(is_file($tpl_image))
	{
		$taget_tpl_image = WEB_DIR.'static'.DIRECTORY_SEPARATOR.'uploadfile'.DIRECTORY_SEPARATOR.'template'.DIRECTORY_SEPARATOR.$target_cover_image;
		rename($tpl_image,$taget_tpl_image);
	}
}

//改写模板配置文件
$tpl_config_file = $target_file.'config.php';
$tpl_config = include($tpl_config_file);
$tpl_config['identify'] = $folder_name;
$tpl_config['disable'] = 0;
$tpl_config['image'] = $target_cover_image;
setCacheData($tpl_config_file,$tpl_config);

//拷贝资源文件
$resource_file = INSTALL_DATA.'resource'.DIRECTORY_SEPARATOR;
$target_file   = WEB_DIR.'static'.DIRECTORY_SEPARATOR.'uploadfile'.DIRECTORY_SEPARATOR;
MoFolder::copyFolder($resource_file,$target_file);

//清理模板缓存
$tpl_cache_folder = WEB_DIR.'data'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'views';
MoFolder::clearRecur($tpl_cache_folder);

//清理文件
@unlink(WEB_DIR.'index.html');
?>

<div class="head">
	<h2>铭万--专业易用的网站内容管理系统</h2>
</div>


<div class="content">
	<div class="ctxt bg2">
		<img src="<?php echo URL_RES;?>images/congrate.jpg" alt="安装成功" class="warnimg"/>
		<a href="<?php echo HOST_NAME;?>admin" class="goin">&nbsp;</a>
		<div class="warntxt">
			<p>安装完成，请务必删除此安装文件夹，请到后台切换模板、更新静态文件即可。</p>
		</div>
	</div>
</div>
</body>
</html>