<?php
	error_reporting("E_ALL & ~E_NOTICE");
	if(file_exists("lock"))
		die("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />要重新安装网站，请手动删除install目录下的lock文件");
	$action = trim($_GET["action"]);
	if($action == "install"){
		array_pop($_POST);//表单提交上来的数据都存入$_POST数组中，现在弹出最后一个数组值，因为最后一个是按纽
		extract($_POST);//把数组名作为变量名，数组值作为变量值
		$mysql_user = trim($mysql_user);
		$mysql_pwd = trim($mysql_pwd);
		$mysql_name = trim($mysql_name);
	}
?>
<!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>HMCSS安装</title>
<style type="text/css">
ul,li{list-style:none; margin:0px auto; padding:0px;}
li{float:left; display:inline-block; height:30px; line-height:30px; color:#666;}
</style>
<script language = "javascript" src="../admin/include/jquery.min.js" type="text/javascript"></script>
<script language="javascript">
function do_install(i_numb)
{
	var install_arr = new Array("开始连接数据库",
							"正在安装数据库文件",
							"正在配置网站数据库连接文件",
							"网站已成功安装，初始管理员和密码都是admin，正在转向首页……"
						);
	$("#msg").html(install_arr[i_numb]);
}
</script>
</head>

<body>
<div style="width:803px; margin:10px auto; height:50px; line-height:50px; border:#CCC 1px solid; font-size:18px; font-weight:bold; color:#06F; text-align:center;">HMCSS安装</div>
<div style="width:783px; margin:10px auto; height:auto; line-height:20px; border:#CCC 1px solid; font-size:14px; color:#333; padding:10px;">1、请把如下目录设为可写权限"best"，"flash_pic"，"honner"，"install"，"product"，"upload_file"<br>
2、请把以下文件设为可写权限"bcastr.xml","admin/include/conn.php"<br>
<span style="color:#F00">3、检查你的MYSQL权限，是否有创建、删除表等权限</span></div>
<div style="width:803px; margin:0px auto; height:150px;">
 <form id="form1" name="form1" method="post" action="index.php?action=install">
	<ul>
    	<li style="width:150px;">MYSQL用户名：</li>
        <li style="width:600px;"><input name="mysql_user" type="text" id="mysql_user" size="30" maxlength="50" value="<?php echo $mysql_user; ?>" /> </li>
        <li style="width:150px;">MYSQL密码：</li>
        <li style="width:600px;"><input name="mysql_pwd" type="text" id="mysql_pwd" size="30" maxlength="50" value="<?php echo $mysql_pwd; ?>" /> </li>
        <li style="width:150px;">MYSQL数据库名：</li>
        <li style="width:600px;"><input name="mysql_name" type="text" id="mysql_name" size="30" maxlength="50" value="<?php echo $mysql_name; ?>" /> </li>
        <li style="width:150px;">操作：</li>
        <li style="width:600px;">
          <input type="submit" name="button" id="button" value="  提 交  " />
        </li>
    </ul>
 </form>
</div>
<?php
	if($action == "install"){
		echo "<div id=\"msg\" style=\"width:803px; margin:20px auto; height:50px; line-height:50px; border:#CCC 1px solid; font-size:16px; color:#666; text-align:center;\"></div>";
		if(strlen($mysql_user) <2 || strlen($mysql_pwd) <2 || strlen($mysql_name) <2){
			die("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />请填入HMCSS安装信息");
		}
		$file_dir = dirname(__FILE__);
		$file_name = "hmcss.sql";
		ob_start();
		print str_repeat("", 4096);
		echo "<script>do_install(0);</script>";
		echo str_pad("", 256);
		ob_flush();
		flush();
		sleep(2);
		$conn = mysql_connect("localhost",$mysql_user,$mysql_pwd)
			or die("数据库连接出错，请检查数据库配置");
		mysql_select_db($mysql_name,$conn);
		mysql_query("set names utf8");
		$get_sql_data = file_get_contents($file_name,$file_dir);
		$explode = explode(";\r\n",$get_sql_data);
		$cnt = count($explode);
		print str_repeat("", 4096);
		echo "<script>do_install(1);</script>";
		ob_flush();
		flush();
		sleep(5);
		for($i=0;$i < $cnt-1;$i++){
    		$sql = $explode[$i];
    		$result = mysql_query($sql);
		}
		print str_repeat("", 4096);
		echo "<script>do_install(2);</script>";
		ob_flush();
		flush();
		//配置conn.php文件
		if(file_exists("../admin/include/conn.php"))
			unlink("../admin/include/conn.php");
		$file = fopen("../admin/include/conn.php","x");
		fwrite($file,"<?php
			ini_set(\"display_errors\",\"off\");
			ini_set(\"magic_quotes_gpc\",\"off\");
			mysql_connect(\"localhost\",\"$mysql_user\",\"$mysql_pwd\")
				or die(\"<meta http-equiv=\\\"Content-Type\\\" content=\\\"text/html; charset=utf-8\\\" />数据库连接出错，请检查数据库配置\");
			mysql_select_db(\"$mysql_name\");
			mysql_query(\"set names utf8\");
?>
			");
		fclose($file);
		sleep(3);
		print str_repeat("", 4096);
		echo "<script>do_install(3);</script>";
		ob_end_flush();
		flush();
		sleep(3);
		if(!file_exists("lock"))
			$file = fopen("lock","x");
		fclose($file);
		die("<script>location.href='../index.php';</script>");
	}
?>
</body>
</html>