<?

include("mydb.php");
include("../include/FileDir.php");
$mysqlhost=$_POST['mysqlhost'];
$mysqluser=$_POST['mysqluser'];
$mysqlpwd=$_POST['mysqlpwd'];
$mysqldb=$_POST['mysqldb'];
$d=new db($mysqlhost,$mysqluser,$mysqlpwd,$mysqldb);
$d->query("set names utf8");
?>
<?
$flag=true;
if(!$_POST['serverfile'])
{
	$msgs="No buckup file";
	$flag=false;	
}
if(!eregi("_v[0-9]+",$_POST['serverfile'])&&$flag)
{
	$filename="./dbsql/".$_POST['serverfile'];
	if(import($filename)) 
	{
		$msgs="<div style='text-align:center;'>";
		$msgs.="安装成功<br><br>";
		$msgs.="<a href='../'>首页</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='../admin'>后台</a>";
		$msgs.="</div>";
	}
	else 
	{
		$msgs="import failing";	
		$flag=false;
	}
}


function import($fname)
{
	global $d;
	$sqls=file($fname);
	foreach($sqls as $sql)
	{
		str_replace("\r","",$sql);
		str_replace("\n","",$sql);
		if(!$d->query(trim($sql))) return false;
	}
	return true;
}
//replace configuration file
if($flag)
{
	$current_dir=str_replace('\\','/',dirname(__FILE__));
	$configuration_dir=substr($current_dir,0,strrpos($current_dir,'/'));
	//read
	$filepath=$configuration_dir."/Configuration.php";
	$fp= fopen ($filepath,"r");
	$content = fread ($fp,filesize($filepath));
	$content=str_replace("{mysqlhost}",$mysqlhost,$content);
	$content=str_replace("{mysqluser}",$mysqluser,$content);
	$content=str_replace("{mysqlpwd}",$mysqlpwd,$content);
	$content=str_replace("{mysqldb}",$mysqldb,$content);
	
	//write
	$fp= fopen ($filepath,"w");
	fwrite($fp,$content,strlen($content));
	fclose ($fp);
	
	$dir=new FileDir();
	$dir->deleteAllDir($configuration_dir."/install");
	
	//updata admin password
	$admin_psw=$_POST['admin_psw'];
	$sql="update hono_bbs_users set psw='".$admin_psw."' where username='admin'";
	$d->query($sql);
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<link href="../css/style.css" rel="stylesheet" type="text/css">
<META content="MSHTML 6.00.2900.3395" name=GENERATOR>
<title></title>
</HEAD>
<BODY>
<div style="padding:100px;">
<table class='login_container' width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="5" height="6" class="box_top_left"><img src="../images/box_top_left.gif" /></td>
    <td    class="box_top_bg">&nbsp;</td>
    <td width="5"  class="box_top_right"></td>
  </tr>
  <tr>
    <td  class="box_left">&nbsp;</td>
    <td align="center" class="login_td">
		<?=$msgs?>
   </td>
    <td  class="box_right">&nbsp;</td>
  </tr>
  <tr>
    <td height="6" class="box_bottom_left"></td>
    <td  class="box_bottom_bg">&nbsp;</td>
    <td class="box_bottom_right"></td>
  </tr>
</table>
</div>
 </BODY></HTML>