<?php

require_once 'common.php';

//引入数据库类
require_once '../class/MysqlDb.Class.php';
//引入数据库备份恢复类
require_once '../class/DbManage.Class.php';

//判断是否提交
if(isset($_POST['NextButton'])){
	
	$DbHost = $_POST['dbhost'];		//数据库地址
	$DbName = $_POST['dbname'];		//数据库名
	$DbUser = $_POST['dbuser'];		//数据库用户名
	$DbPw = $_POST['dbpw'];		//数据库密码
	$TabPre = $_POST['tabpre'];		//数据库表名前缀
	$WebUrl = $_POST['weburl'];		//系统安装路径
	
	//判断提交的信息是否为空
	if($DbHost == '' || $DbName == '' || $TabPre == '' || $WebUrl == ''){
		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
		echo "<script>alert('请将数据库信息填写完整！');</script>";
		echo "<script>window.location = 'systemset.php'</script>";
		exit();
	}
	
	//测试数据库是否可以连接
	$DataBase = new MysqlDb($DbHost, $DbUser, $DbPw, $DbName, $TabPre);
	if($DataBase->open_db() == false){
		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
		echo "<script>alert('打开数据库失败！请确认数据库是否存在或用户名和密码是否正确！');</script>";
		echo "<script>window.location = 'systemset.php'</script>";
		exit();		
	}
	else{
		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
		$DataBase->close_dblink();
		//修改表名前缀
		$SqlFile = fopen('../data/backup/20190405204126_2YkmGlt0uB/20190405204126_all_v1.sql', 'r+');
		$SqlFileContent = fread($SqlFile, filesize('../data/backup/20190405204126_2YkmGlt0uB/20190405204126_all_v1.sql'));
		$SqlFileContent = str_replace('gbook_', $TabPre, $SqlFileContent);
		$SqlFileContent = str_replace('ZTJMESSAGE_WEB_URL', $WebUrl, $SqlFileContent);
		date_default_timezone_set('PRC');
		$SqlFileContent = str_replace('ZTJMESSAGE_INSTALL_TIME', date('Y-m-d H:i:s'), $SqlFileContent);
		fclose($SqlFile);
		$NewSqlFile = fopen('../data/backup/20190405204126_2YkmGlt0uB/20190405204126_all_v1.sql', 'w+');
		fwrite($NewSqlFile, $SqlFileContent);
		fclose($NewSqlFile);
		//导入数据库文件
		$db = new DBManage($DbHost, $DbUser, $DbPw, $DbName, 'utf8');
		if($db->_import('../data/backup/20190405204126_2YkmGlt0uB/20190405204126_all_v1.sql') == false){
			echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
			echo "<script>alert('数据导入失败！请检查原因！');</script>";
			echo "<script>window.location = 'systemset.php'</script>";
			exit();			
		}
		else{
			echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
			echo "<script>alert('数据已导入！');</script>";		
		}
		$SqlImport = true;
		//检测数据库是否成功安装
		$TableArr = array(
				$TabPre.'adminuser',
				$TabPre.'message',
				$TabPre.'nav',
				$TabPre.'setting'
		);
		$DataBase = new MysqlDb($DbHost, $DbUser, $DbPw, $DbName, $TabPre);
		$DataBase->open_db();
		$TableCheck = array();
		$i = 0;
		foreach($TableArr as $TableName){
			$k = $i++;
			if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$TableName."'", $DataBase->conn)) == 1) {
				$TableCheck[$k] = '数据表 '.$TableName.' 安装成功.';
			}
			else{
				$TableCheck[$k] = '<b style="color:#F00;">数据表 '.$TableName.' 安装失败.</b>';
				$SqlImport = false;
			}
		}
		$DataBase->sel("setting");
		if(count($DataBase->Result) == 37){
			$TableCheck[4] = '系统设置信息写入成功.';
		}
		else{
			$TableCheck[4] = '<b style="color:#F00;">系统设置信息写入失败.</b>';
			$SqlImport = false;
		}
		$DataBase->sel("nav");
		if(count($DataBase->Result) == 3){
			$TableCheck[5] = '导航栏目初始化成功.';
		}
		else{
			$TableCheck[5] = '<b style="color:#F00;">导航栏目初始化失败.</b>';
			$SqlImport = false;
		}
		$DataBase->sel("message");
		if(count($DataBase->Result) == 1){
			$TableCheck[6] = '默认留言写入成功.';
		}
		else{
			$TableCheck[6] = '<b style="color:#F00;">默认留言写入失败.</b>';
			$SqlImport = false;
		}
		$DataBase->sel("adminuser");
		if(count($DataBase->Result) == 1){
			$TableCheck[7] = '默认用户添加成功.';
		}
		else{
			$TableCheck[7] = '<b style="color:#F00;">默认用户添加失败.</b>';
			$SqlImport = false;
		}					
		//将数据库配置信息写入config文件
		$ConFile = fopen('../config.php', 'r+');
		$ConFileContent = fread($ConFile, filesize('../config.php'));
		$ConFileContent = str_replace('localhost', $DbHost, $ConFileContent);
		$ConFileContent = str_replace('root', $DbUser, $ConFileContent);
		$ConFileContent = str_replace('password', $DbPw, $ConFileContent);
		$ConFileContent = str_replace('ztjmessage', $DbName, $ConFileContent);
		$ConFileContent = str_replace('gbook_', $TabPre, $ConFileContent);
		fclose($ConFile);
		$NewConFile = fopen('../config.php', 'w+');
		if(fwrite($NewConFile, $ConFileContent) == false){
			$TableCheck[8] = '<b style="color:#F00;">系统配置文件写入失败.</b>';
			$SqlImport = false;			
		}
		else{
			$TableCheck[8] = '系统配置文件写入成功.';
		}
		fclose($NewConFile);
		//修改百度编辑器保存路径
		$BaiduConFile = fopen('../include/editor/baidu/php/config.json', 'r+');
		$BaiduConFileC = fread($BaiduConFile, filesize('../include/editor/baidu/php/config.json'));
		$thisurl = $_SERVER['PHP_SELF'];
		$urlarray = explode('/', $thisurl);
		$urlarraysize = count($urlarray);
		unset($urlarray[$urlarraysize-1]);
		unset($urlarray[$urlarraysize-2]);
		$ZTJMessage35Dir = '';
		foreach($urlarray as $v){
			$ZTJMessage35Dir .= $v.'/';
		}
		$ZTJMessage35Dir = substr($ZTJMessage35Dir, 0, -1);
		$BaiduConFileC = str_replace('/ZTJMessage35UploadDir', $ZTJMessage35Dir, $BaiduConFileC);
		fclose($BaiduConFile);
		$NewBaiduConFile = fopen('../include/editor/baidu/php/config.json', 'w+');
		if(fwrite($NewBaiduConFile, $BaiduConFileC) == false){
			$TableCheck[9] = '<b style="color:#F00;">编辑器配置文件写入失败.</b>';
			$SqlImport = false;
		}
		else{
			$TableCheck[9] = '编辑器配置文件写入成功.';
		}
		fclose($NewBaiduConFile);
		if(unlink('../data/backup/20190405204126_2YkmGlt0uB/20190405204126_all_v1.sql') == true){
			$TableCheck[10] = '删除数据文件成功.';
			rmdir('../data/backup/20190405204126_2YkmGlt0uB');
		}
		else{
			$TableCheck[10] = '<b style="color:#F00;">删除数据文件失败.</b>';
		}
		
		$DataBase->close_dblink();

	}
	
}
else{
	exit();
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ZTJMessage 安装程序</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/script.js"></script>
</head>

<body>

<div class="box">

<div id="header">
<img src="images/logo.png" /><span>版本：3.5 Beta</span>
</div>

<div id="loac">
1.运行环境检测
&raquo;
2.系统设置
&raquo;
<b style="color:#F00; font-size:24px;">3.安装数据库</b>
&raquo;
4.设置管理员账号
&raquo;
5.完成安装
</div>

<div class="importsql">
<table style="font-size:12px; color:#999;">
	<tr>
    	<td>
		<?php 
		foreach ($TableCheck as $r){
			echo $r.'<br/>';
		}
		?>
        </td>
    </tr>
    <tr>
    	<td></td>
    </tr>
    <tr>
    	<td>
    	<?php if($SqlImport == true){ ?>
    	<div style="color:#00A600; margin-bottom:5px;">数据库安装成功！</div>
    	<input type="button" name="next" value="下一步" onclick="ToAdminuser();" />
    	<?php }else{ ?>
    	<div style="color:#F00;">数据库安装失败，请检查原因</div>
    	<?php } ?>
    	</td>
    </tr>    
</table>
</div>

</div>

</body>

</html>
