<?php 
header("Content-type: text/html; charset=utf-8");
if(file_exists("install.lock")){echo "您已安装过,请删除 install.lock";die;}
$status = isset($_POST['status'])	? $_POST['status'] : false;
if($status){
	
	//检测数据库账号密码
	$mysql = mysql_connect($_POST['db_host'],$_POST['db_user'],$_POST['db_pwd']);
	$link =  $mysql ? 1 : 2;
	if($link == 1){

		//创建数据库
		$db_name = $_POST['db_name'];
		//查询数据库是否存在 ，存在则删除，再创建
		$sel_db = mysql_select_db($_POST['db_name'],$mysql);
		if(!$sel_db){
			$dbname02 = mysql_query("CREATE DATABASE $db_name");
		}
			mysql_query("set names utf8");
			mysql_select_db($_POST['db_name'],$mysql);
			//创建 默认数据表
			$sql_arr = array(
					//清除原始数据
					"DROP TABLE IF EXISTS ".$_POST['db_prefix']."setting;",
					//创建setting表
					"CREATE TABLE ".$_POST['db_prefix']."setting (
								  `id` int(11) NOT NULL AUTO_INCREMENT,
								  `sitename` varchar(251) DEFAULT NULL,
								  `keyword` varchar(251) DEFAULT NULL,
								  `description` varchar(500) DEFAULT NULL,
								  `content` mediumtext,
								  `custom` mediumtext COMMENT '自定义变量',
								  `mail` varchar(500) DEFAULT NULL COMMENT '邮件配置',
								  PRIMARY KEY (`id`)
								) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='设置表' AUTO_INCREMENT=2",
					//插入初始数据
					'INSERT INTO `'.$_POST['db_prefix'].'setting` (`id`, `sitename`, `keyword`, `description`, `content`, `custom`, `mail`) VALUES (1, \'SmSoft单网页系统\', \'SmSoft单网页系统\', \'SmSoft单网页系统\', NULL, NULL, \'{"stype":"email","state":"false","server":"smtp.163.com","account":"18630123528@163.com","password":"123a123.","recaccount":""}\');',
					"DROP TABLE IF EXISTS ".$_POST['db_prefix']."order;",
					"CREATE TABLE ".$_POST['db_prefix']."order (
								  `id` smallint(8) NOT NULL AUTO_INCREMENT,
								  `content` mediumtext NOT NULL,
								  `from` varchar(500) DEFAULT NULL COMMENT '来源',
								  `ip` varchar(16) DEFAULT NULL,
								  `time` int(10) unsigned NOT NULL,
								  PRIMARY KEY (`id`)
								) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;",
								
								
					"DROP TABLE IF EXISTS ".$_POST['db_prefix']."admin;",
					"CREATE TABLE ".$_POST['db_prefix']."admin (
							  `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
							  `username` varchar(16) CHARACTER SET utf8 NOT NULL,
							  `password` varchar(32) CHARACTER SET utf8 NOT NULL,
							  PRIMARY KEY (`id`)
					) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;",
			);
			//print_r(count($sql_arr));
			$i = 0;
			foreach ($sql_arr as $k=>$v){
				$result = mysql_query($v,$mysql);
				if($result){
					$i += 1;
				}
			}

			if($i == 7){
				$result = mysql_query("INSERT INTO `".$_POST['db_prefix']."admin` (`id`, `username`, `password`) VALUES (null, '".$_POST['userid']."', '".md5($_POST['pwd'])."');",$mysql);
				if($result){
					//创建项目的配置文件
					$config ='<?php
								$dbhost = "'.$_POST['db_host'].'";	// 服务器地址
								$dbuser = "'.$_POST['db_user'].'";	// 数据库账号
								$dbpass = "'.$_POST['db_pwd'].'";	// 数据库密码
								$dbname = "'.$_POST['db_name'].'";	// 数据库名
								$qz = "'.$_POST['db_prefix'].'";	//表前缀
								?>';
					$fp = fopen('../config.php',"w");
					$result = fwrite($fp, $config);
					
					if($result){
						file_put_contents('install.lock','');
						$url = explode('/', $_SERVER['PHP_SELF']);
						echo "<script>alert('安装成功，现在跳转到后台登陆界面'); location.href='../';</script>";
					}
				}
			}

	}else if($link == 2){

		echo "<script>alert('数据库服务器或登录密码不正确'); history.go(-1);</script>";
		exit;

	}
}
$step = isset($_GET['step']) ? $_GET['step'] : 1;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.css" type="text/css"/>
<link rel="stylesheet" href="css/install.css" type="text/css"/>

<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js" language="javascript" type="text/javascript"></script>
<script src="js/install.js" language="javascript" type="text/javascript"></script>


</head>
<body class="body">
	<div class="con_bg">
		<div class="con">
			<?php 
				if($step == 1){
			?>
				<div class="con-config">
					<form action="" id='fomr01' class="editprofileform" method="post">
						<input type="hidden" name="status" value="1">
						<div class="con_title"><h3><b>SmSoft单网页管理系统安装</b></h3></div>
						<div class="con-agreement01">
							<div class="con-email">
								<br/>
								<div style="color : #337ab7;"><label>数据库配置</label></div>
								<p>
									<label style="width:150px;">数据库主机：</label>
									<input type="text" class="input-xlarge" name="db_host" id="db_host" value="127.0.0.1" placeholder="例如:localhost" style="width:350px;"/>
								</p>
								<p>
									<label style="width:150px;">数据库账号：</label>
									<input type="text" class="input-xlarge" name="db_user" id="db_user" value="root" placeholder="" style="width:350px;"/>
								</p>
								<p>
									<label style="width:150px;">数据库密码：</label>
									<input type="text" class="input-xlarge" name="db_pwd" id="db_pwd" value="" placeholder="" style="width:350px;"/>
								</p>
								<p>
									<label style="width:150px;">数据表前缀：</label>
									<input type="text" class="input-xlarge" name="db_prefix" id="db_prefix" value="smf_" placeholder="如：smf_" style="width:350px;"/>
								</p>
								<p>
									<label style="width:150px;">数据库名称：</label>
									<input type="text" class="input-xlarge" name="db_name" id="db_name" value="" placeholder="如：SmSoft" style="width:350px;"/>
									
								</p>
								<p>
									<label style="width:150px;"></label>
									<font>如数据库已存在，系统将会覆盖已有的数据库</font>
								</p>
							</div>
							
							<div class="con-admin">
								<br/>
								<div style="color : #337ab7;"><label>管理员初始密码</label></div>
								<p>
									<label style="width:150px;">管理员账号：</label>
									<input type="text" class="input-xlarge" name="userid" id="userid" value="admin" placeholder="" style="width:350px;"/>
								</p>
								<p>
									<label style="width:150px;">管理员密码：</label>
									<input type="text" class="input-xlarge" name="pwd" id="pwd" value="admin" placeholder="" style="width:350px;"/>
								</p>
							</div>
							<br/>
						</div>
						<div class="con-base">
	   						 <input type="button" class="btn btn-primary" value="&nbsp;&nbsp;&nbsp;后退&nbsp;&nbsp;&nbsp;" onclick="window.location.href='index.php?step=1';" />&nbsp;&nbsp;&nbsp;
	            			<input type="button" class="btn btn-primary" value="开始安装" onclick="DoInstall();" />
						</div>
					</form>
				</div>
			<?php }?>
		</div>
	</div>
</body>
</html>
