<?php
session_start(); 
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');
	require_once('inc/check.php');
	//$restoredb=$restoreDBName;
	echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
	
	$host=$_POST['host'];
	$restoredb =$_POST['dbname'];
	$username=$_POST['dbusername'];
	$password=$_POST['dbpassword'];
	
if(!isset($_POST['restore']))
{

	
	$filename="inc/conn_var.php";
	$handle = fopen ($filename,"r");
	while (!feof ($handle)) 
	{
		$partstr0 =  fgets ($handle, 3000);
		echo $partstr0;
		if(strpos($partstr0,"host=")>0){$initHost=trim(str_replace("';","",str_replace("\$host='","",$partstr0)));}											
		if(strpos($partstr0,"dbname=")>0){$initDBName=trim(str_replace("';","",str_replace("\$dbname='","",$partstr0)));}										
		if(strpos($partstr0,"username=")>0){$initDBUserName=trim(str_replace("';","",str_replace("\$username='","",$partstr0)));}											
		if(strpos($partstr0,"password=")>0){$initDBPassword=trim(str_replace("';","",str_replace("\$password='","",$partstr0)));}	

	}
	//echo strlen($partstr);
	fclose ($handle);
	
}
else
{
	$conn=mysql_connect($host,$username,$password) or die ("can't connect DBS");
	$dropdb_sql="DROP DATABASE `".$restoredb."` ";
	$dropdb_query=mysql_query($dropdb_sql,$conn);
	if($dropdb_query)
	{
		echo "<br/>已经删除数据库：".$restoredb;
	}
	else
	{echo "<br/>删除原有数据库：".$restoredb."失败！<br/>";}
	$creatdb_sql="CREATE DATABASE `".$restoredb."` ";
	$creatdb_query=mysql_query($creatdb_sql,$conn);

	if($creatdb_query)
	{echo "<br/>重新创建数据库：".$restoredb."成功！<br/>";}
	else
	{die("<br/>重新创建数据库：".$restoredb."失败，数据库恢复终止！<br/>");}

	$restoredblink=mysql_select_db($restoredb,$conn);
	
	$filename=$backupSQLFile;
	
	if (!file_exists ($filename))
	{ 
	   die( "数据库文件 ".$filename." 不存在!"); 	   
	}

	$strlength=$maxLenSQLStr;
	$allnum =0;
	$num =0;
	$handle = fopen ($filename,"r");
	$partstr="";
	$partstr0="";
	while ($partstr0 =  fgets ($handle, 6000)) 
	{
		//echo $partstr0. "ss";
		//echo substr(substr ($partstr0,-3),0,1);
		if(substr(substr ($partstr0,-3),0,1)==";")// get the end of line ;
		{
			$partstr.=$partstr0;
			//echo "11";
		}
		else
		{
			$partstr.=$partstr0;
			//echo "22";
			continue;
		 }
		//echo $partstr. "ss";
		$num=strlen($partstr);
   	 	$allnum += $num;
		$sqlstr=$partstr;
		$gettablename=$partstr;
		$tablename="";
		$iftablename=strpos($gettablename,"create table `");
		$tablenamestart=strlen("create table `");
		$tablenameend=strpos($gettablename,"` (`");
		$tablenamelen=$tablenameend-$tablenamestart;

		if($iftablename===0)
		{
			$tablename=substr($gettablename,$tablenamestart,$tablenamelen);
		}
		
		$query=mysql_query($sqlstr);
		if($query)
		{
			if($tablename=="")
			{echo " 已经写入数据 ".$num." 字节!<br/>";}
			else
			{echo "创建表 ".$tablename." OK! <br/>";}
			
		}
		$partstr="";
		
	}
	echo "恢复数据库 ".$restoredb." 完成!<br/>";
	fclose ($handle);
	die("");

}
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=$engineName?>数据库恢复</title>
<link type="text/css" rel="stylesheet" href="css/admin.css" />
</head>

<body>
<table width="96%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#6396d6">
  <tr>
	<td height="18" background="images/admin_bg.gif"><div align="center"><font color="#ffffff"><strong>
	  恢复数据库</strong></font></div></td>
  </tr>
  <tr>
	<td height="213" valign="top" bgcolor="#dee7ff"><br>
		<table border="0" width="90%" align="center">
		  <tr>
			<td><div align="center">恢复数据库将覆盖现有数据库的的所有内容</div></td>
		  </tr>
		</table>
	  <form action="restore.php" method="post" enctype="multipart/form-data" name="install" id="form1">
		<table width="500" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF">
		  <tr bgcolor="#B9DAFF">
			<td height="28" colspan="2" align="center">请填写数据库参数</td>
		  </tr>
		  <tr bgcolor="#E8F2FF">
			<td width="30%" height="28" align="center" bgcolor="#E1EEFF"  style='padding-left:50px;'><div align="left">安装主机</div></td>
			<td width="30%" align="center" bgcolor="#E1EEFF"><label>
			  <input name="host" type="text" id="textfield" value="<?=$initHost?>" />
			</label></td>
		  </tr>
		  <tr bgcolor="#E8F2FF">
			<td height="28" align="center" bgcolor="#E1EEFF"  style='padding-left:50px;'><div align="left">数据库</div></td>
			<td align="center" bgcolor="#E1EEFF"><input name="dbname" type="text" id="textfield2" value="<?=$initDBName?>" /></td>
		  </tr>
		  <tr bgcolor="#E8F2FF">
			<td height="28" align="center" bgcolor="#E1EEFF"  style='padding-left:50px;'><div align="left">数据库用户名</div></td>
			<td align="center" bgcolor="#E1EEFF"><input name="dbusername" type="text" id="textfield3" value="<?=$initDBUserName?>" /></td>
		  </tr>
		  <tr bgcolor="#E8F2FF">
			<td height="28" align="center" bgcolor="#E1EEFF" style='padding-left:50px;'><div align="left">数据库密码</div></td>
			<td align="center" bgcolor="#E1EEFF"><input name="dbpassword" type="text" id="textfield4" value="<?=$initDBPassword?>" /></td>
		  </tr>
		  <tr bgcolor="#E8F2FF">
			<td height="28" align="center" bgcolor="#E1EEFF"><div align="left"></div></td>
			<td align="center" bgcolor="#E1EEFF">&nbsp;</td>
		  </tr>
		  <tr bgcolor="#E8F2FF">
			<td height="28" colspan="2" align="center" bgcolor="#E1EEFF"><label>
			  <input type="submit" name="restore" id="button" value="开始恢复数据库" />
			</label></td>
		  </tr>
		</table>
		<p>&nbsp;</p>
      </form>
	</td>
  </tr>
</table>
</body>
</html>
