<!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>配置数据库</title>
</head>

<body>
<?php 
require "fun.php";
if(file_exists("install.off"))
{
	echo"《CuuMall商城系统》安装程序已锁定。如果要重新安装，请删除<b>/install/install.off</b>文件！";
	exit();
}
$_GET=daddslashes($_GET,1,TRUE);
$_POST=daddslashes($_POST,1,TRUE);
$_COOKIE=daddslashes($_COOKIE,1,TRUE);
$_SERVER=daddslashes($_SERVER);
$_FILES=daddslashes($_FILES);
$_REQUEST=daddslashes($_REQUEST,1,TRUE);
inject_checksql();
  $host=$_POST["host"];
  $port=$_POST["port"];
  $username=$_POST["username"];
  $password=$_POST["password"];
  $datadb=$_POST["datadb"];
  $thre=$_POST["thre"];
  $guestcook=$_POST["guestcook"];
  $admincook=$_POST["admincook"];
  if(!$port)
  {
   $port1=":".$port;
  }
  $link=@mysql_connect($host.$port1,$username,$password);
  if(!$link)
  {
    echo "<script>alert('无法链接数据库，请检查用户名或密码');location.href='step4.php?f=4';</script>";
	exit;
  }
  //创建数据库
  $createdb=@mysql_query("CREATE DATABASE IF NOT EXISTS ".$datadb." DEFAULT CHARACTER SET UTF8");
  if(!$createdb)
		{
			echo "<script>alert('数据库创建失败,请检查是否有同名数据库');location.href='step4.php?f=4';</script>";
			exit;
		}
		@mysql_select_db($datadb);
		
   $sqlfile="data/cuumall.com.sql";
   $fp=fopen($sqlfile,'r');
	$sql=fread($fp,filesize($sqlfile));
	fclose($fp);
	if(empty($sql))
	{
		echo "<script>alert('文件丢失,安装不成功');location.href='step4.php?f=4';</script>";
		exit;
	}
	//创建表
	$aa=explode(";",$sql);
	foreach($aa as $key=>$value)
	{
	 $ab=str_replace("mall_",$thre,$value);
	 if(strpos($ab,'CREATE TABLE'))
	 		{
			preg_match("/--(\s)*Table([\s]*[\w])*/",$ab,$temp);
			$ab=str_replace($temp[0],"",$ab);
            $ab=str_replace("--","",$ab);
			if(@mysql_query($ab))
			{
			preg_match("/CREATE TABLE \`[\w]*\`/",$ab,$name);
			$name1=str_replace("CREATE TABLE","",$name[0]);
			echo"建立数据表: <b>".$name1."</b> 完毕......<br>";
			}
			else
			{
			 echo "<script>alert('数据表建立失败');location.href='step4.php?f=4';</script>";
			 exit;
			}
			}
	}
	//写入配置文件
	$con=require("../Conf/config.php");
	$con["DB_HOST"]=$host;
	if(!$port)
	{
	$con["DB_PORT"]=$port;
	}
	$con["DB_USER"]=$username;
	$con["DB_PWD"]=$password;
	$con["DB_NAME"]=$datadb;
	$con["DB_PREFIX"]=$thre;
	$con["GUESTCOOK"]=$guestcook;
	$con["ADMINCOOK"]=$admincook;
	$con["RBAC_USER_TABLE"]=$thre."zthink_role_user";
	$con["RBAC_ROLE_TABLE"]=$thre."zthink_role";
	$con["RBAC_ACCESS_TABLE"]=$thre."zthink_access";
	$con["RBAC_NODE_TABLE"]=$thre."zthink_node";
	$aa=var_export($con,true);
   		$str="<?php return ".$aa.";?>";
	$cf=fopen("../Conf/config.php","w+");
	fwrite($cf,$str);
	fclose($cf);
    //执行成功跳转
	echo "<script>location.href='step5.php?f=5';</script>";
	
?>
</body>
</html>
