<?php
if($_POST['submit']){
$files = "../Conf/config.php";
$config_str = "<?php \n ret";
$config_str .= "urn array(\n";
$config_str .= "'DB_TYPE'               => 'mysql',"; 
$config_str .= "\n";
$config_str .= "'DB_NAME'               => '".$_POST['dbname']."',"; 
$config_str .= "\n"; 
$config_str .= "'DB_USER'               => '".$_POST['dbuser']."',"; 
$config_str .= "\n"; 
$config_str .= "'DB_PWD'                => '".$_POST['dbpass']."',"; 
$config_str .= "\n"; 
$config_str .= "'DB_PORT'               => ".$_POST['dbport'].","; 
$config_str .= "\n"; 
$config_str .= "'DB_PREFIX'             => '".$_POST['dbqz']."',"; 
$config_str .= "\n"; 
$config_str .= "'DEFAULT_MODULE'     	=> 'Index',";
$config_str .= "\n"; 
$config_str .= "'URL_MODEL'          	=> '2',"; 
$config_str .= "\n"; 
$config_str .= "'SESSION_AUTO_START' 	=> true,"; 
$config_str .= "\n"; 
$config_str .= "'URL_HTML_SUFFIX'		=> '.html',";
$config_str .= "\n";
$config_str .= "'VAR_PAGE'    			=> 'p',";
$config_str .= "\n";
$config_str .= "'WEB_TITLE'             => 'n_n_w',";
$config_str .= "\n";
$config_str .= "'WEB_KEYWORDS'			=> 'n_n_k',";
$config_str .= "\n";
$config_str .= "'WEB_DESCRIPTION'       => 'n_n_d',";
$config_str .= "\n";
$config_str .= "'WEB_TONGJI'			=> '684330',";
$config_str .= "\n";
$config_str .= "'NR_USER_ID'          	=> 'NR_USER_ID',";
$config_str .= "\n";
$config_str .= "'NR_USER_KEY'           => 'NR_USER_KEY',";
$config_str .= "\n";
$config_str .= "'NR_ADMIN_ID'           => 'NR_ADMIN_ID',";
$config_str .= "\n";
$config_str .= "'NR_ADMIN_KEY'          => 'NR_ADMIN_KEY',";
$config_str .= "\n";
$config_str .= ");"; 
$config_str .= "\n";
$config_str .= '?>'; 
$ff = fopen($files, "w+"); 
fwrite($ff, $config_str); 
setcookie("dbhost",$_POST['dbhost'], time()+3600*24);
setcookie("dbuser",$_POST['dbuser'], time()+3600*24);
setcookie("dbname",$_POST['dbname'], time()+3600*24);
setcookie("dbpass",$_POST['dbpass'], time()+3600*24);
setcookie("dbqz",$_POST['dbqz'], time()+3600*24);
//	$html->strconf("DB_HOST",C('DB_HOST'),$_POST['dbhosst']);
//	$html->strconf("DB_NAME",C('DB_NAME'),$_POST['dbname']);
//	$html->strconf("DB_PREFIX",C('DB_PREFIX'),$_POST['dbqz']);
//	$html->strconf("DB_USER",C('DB_USER'),$_POST['dbuser']);
//	$html->strconf("DB_PWD",C('DB_PWD'),$_POST['dbpass']);
//	$html->strconf("DB_PORT",C('DB_PORT'),$_POST['dbport']);
	echo "<meta http-equiv='refresh' content='0.1;url=/Public/install.php?p=3'>";
}
?>
<!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>
<style type="text/css">
*{
	margin:0 auto;
	padding:0px;
}
.d1{
	width:580px;
	height:350px;
	margin-top:100px;
	border: 1px solid #003366;
}
.title{
	vertical-align:top;
	font-size:14px;
	font-weight:bold;
	color:#666666;
	line-height:40px;
	padding-left:5px;
	background-color:#CCCCCC;
	height:40px;
}
.cen{
	text-align:center;
	line-height:30px;
}
.next{
	text-align:center;
}
.next input{
	width:100px;
	height:35px;
}
</style>
</head>

<body>
</body>
</html>

<?php
if(!$_GET['p']){
?>
<table class="d1" cellspacing="1" cellpadding="1">
  <tr>
    <td class="title">第一步：查看文件权限是否可读可写</td>
  </tr>
  <tr>
    <td class="cen">
<?php
$files = "../Conf/config.php"; 
if(!is_writable($files)){ 
echo "<font color=red>Conf/config.php不可写！！！</font><br />"; 
}else{ 
echo "<font color=green>Conf/config.php 正常</font><br />"; 
} 
$file = "../Public/install.sql"; 
if(!is_readable($file)){ 
echo "<font color=red>Public/install.sql 不可写！！！</font><br />"; 
}else{ 
echo "<font color=green>Public/install.sql 正常</font><br />"; 
} 
?></td>
  </tr>
  <tr>
    <td class="next">
<?php
if(!is_writable($files) || !is_readable($file)){
?>
	<input type="button" value="下一步" disabled="disabled" />
<?php }else{?>
	<input type="button" value="下一步" onClick="onclick=window.location='/Public/install.php?p=2'" />
<?php }?>
	</td>
  </tr>
</table>
<?php
}elseif($_GET['p'] == 2){
?>
<table class="d1" cellspacing="1" cellpadding="1">
  <tr>
    <td class="title">第二步：填写数据库信息</td>
  </tr>
  <tr>
    <td class="cen">
<form action="" method="post">
<table cellspacing="1" cellpadding="1">
  <tr>
    <td>主机地址：</td>
    <td><input name="dbhost" type="text" value="localhost" /></td>
  </tr>
  <tr>
    <td>数据库名：</td>
    <td><input name="dbname" type="text" /></td>
  </tr>
  <tr>
    <td>表前缀：</td>
    <td><input name="dbqz" type="text" value="NR_" /></td>
  </tr>
  <tr>
    <td>数据库用户名：</td>
    <td><input name="dbuser" type="text" /></td>
  </tr>
  <tr>
    <td>数据库密码：</td>
    <td><input name="dbpass" type="text" /></td>
  </tr>
  <tr>
    <td>连接端口：</td>
    <td><input name="dbport" type="text" value="3306" /></td>
  </tr>
</table>
	</td>
  </tr>
  <tr>
    <td class="next">
	<input type="button" value="上一步" onClick="onclick=window.location='/Public/install.php'" />
	<input name="submit" type="submit" value="下一步" />
	</td>
  </tr>
</table>
</form>
<?php
}elseif($_GET['p'] == 3){
?>
<table class="d1" cellspacing="1" cellpadding="1">
  <tr>
    <td class="title">第三步：导入数据库</td>
  </tr>
  <tr>
    <td class="cen">
<iframe src="/Public/install.php?p=sql"></iframe>
	</td>
  </tr>
  <tr>
    <td class="next">
	<input type="button" value="上一步" onClick="onclick=window.location='/Public/install.php?p=2'" />
	<input type="button" value="下一步" onClick="onclick=window.location='/Public/install.php?p=4'" />
	</td>
  </tr>
</table>
<?php
}elseif($_GET['p'] == 4){
if($_POST['subadmin']){
		$dbhost = $_COOKIE['dbhost'];
		$dbname = $_COOKIE['dbname'];
		$dbuser = $_COOKIE['dbuser'];
		$dbpass = $_COOKIE['dbpass'];
		$dbqz	= $_COOKIE['dbqz'];
		mysql_connect($dbhost,$dbuser,$dbpass) or die("数据库连接失败，请返回第二步检查您填写是否正确！");
		mysql_select_db($dbname) or die("数据库名称填写错误，请返回第二步检查您填写是否正确！");
	$qz = $_COOKIE['dbqz'];
	$password = md5($_POST['password']);
	$sql = "
	INSERT INTO `".$qz."user`(`id`,`username`,`password`,`uname`) VALUES(1,'$_POST[username]','$password','admin'
	)";
	$d = time();
	$sql2 = "
	INSERT INTO `".$qz."user_date` (`uid`, `rank`, `money`, `from`, `sex`, `birth`, `date`) VALUES ('1', '0', '0', '-', '1', '1', $d)
	";
	$sql3 = "
	INSERT INTO `".$qz."admin`(`uid`,`password`) VALUES(1,'$password')
	";
	mysql_query($sql);
	mysql_query($sql2);
	if(mysql_query($sql3)){
		echo "<script>alert('安装成功！');</script>";
		rename("../Public/install.php","../Public/install.php.bak");
		echo "<meta http-equiv='refresh' content='0.1;url=/'>";
	}
}
?>
<table class="d1" cellspacing="1" cellpadding="1">
  <tr>
    <td class="title">第四步：填管理员账号</td>
  </tr>
  <tr>
    <td class="cen">
<form action="" method="post">
<table cellspacing="1" cellpadding="1">
  <tr>
    <td>管理员账号：</td>
    <td><input name="username" type="text" value="22222@qq.com" /></td>
  </tr>
  <tr>
    <td>管理员密码：</td>
    <td><input name="password" type="text" /></td>
  </tr>
  <tr>
    <td>再输一次：</td>
    <td><input name="repassword" type="text" /></td>
  </tr>
</table>
	</td>
  </tr>
  <tr>
    <td class="next">
	<input type="button" value="上一步" onClick="onclick=window.location='/Public/install.php?p=3'" />
	<input name="subadmin" type="submit" value="下一步" />
	</td>
  </tr>
</table>
</form>
<?php
}elseif($_GET['p'] == 'sql'){
		$dbhost = $_COOKIE['dbhost'];
		$dbname = $_COOKIE['dbname'];
		$dbuser = $_COOKIE['dbuser'];
		$dbpass = $_COOKIE['dbpass'];
		$dbqz	= $_COOKIE['dbqz'];
		mysql_connect($dbhost,$dbuser,$dbpass) or die("数据库连接失败，请返回第二步检查您填写是否正确！");
		mysql_select_db($dbname) or die("数据库名称填写错误，请返回第二步检查您填写是否正确！");
		
	if($_GET['n'] == 2){
		$sql = "
CREATE TABLE `".$dbqz."admin` (
  `uid` int(5) NOT NULL,
  `password` varchar(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
";
		if(mysql_query($sql)){
			echo "admin表导入成功，请稍等...";
			echo "<meta http-equiv='refresh' content='1;url=/Public/install.php?p=sql&&n=3'>";
		}
	}elseif($_GET['n'] == 3){
		$sql = "
CREATE TABLE `".$dbqz."follower` (
  `id` int(5) NOT NULL auto_increment,
  `uid` int(5) NOT NULL,
  `fid` int(5) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
		";
		mysql_query($sql);
		echo "follower表导入成功，请稍等...";
		echo "<meta http-equiv='refresh' content='1;url=/Public/install.php?p=sql&&n=4'>";
	}elseif($_GET['n'] == 4){
		$sql = "
CREATE TABLE `".$dbqz."forum` (
  `id` int(5) NOT NULL auto_increment,
  `title` varchar(255) character set gbk NOT NULL,
  `memu` tinyint(2) NOT NULL,
  `hot` int(5) NOT NULL,
  `reply` int(5) NOT NULL,
  `pid` int(5) NOT NULL,
  `content` text character set gbk NOT NULL,
  `ishot` tinyint(2) NOT NULL,
  `date` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
		";
		mysql_query($sql);
		echo "forum表导入成功，请稍等...";
		echo "<meta http-equiv='refresh' content='1;url=/Public/install.php?p=sql&&n=5'>";
	}elseif($_GET['n'] == 5){
		$sql = "
CREATE TABLE `".$dbqz."memu` (
  `id` int(5) NOT NULL auto_increment,
  `title` varchar(55) character set gbk NOT NULL,
  `type` tinyint(2) NOT NULL,
  `fid` tinyint(2) NOT NULL,
  `path` varchar(55) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=14
		";
		$sql2 = "
INSERT INTO `".$dbqz."memu` (`id`, `title`, `type`, `fid`, `path`) VALUES
(1, 'test', 0, 0, '0'),
(2, 'test', 0, 1, '0-1'),
(3, 'test', 0, 1, '0-1'),
(4, 'test', 0, 1, '0-1'),
(5, 'test', 0, 0, '0'),
(6, 'test', 0, 5, '0-5'),
(7, 'test', 0, 5, '0-5'),
(8, 'test', 0, 5, '0-5'),
(9, 'test', 0, 5, '0-5'),
(10, 'test', 0, 0, '0'),
(11, 'test', 0, 10, '0-10'),
(12, 'test', 0, 10, '0-10'),
(13, 'test', 0, 10, '0-10')
		";
		mysql_query($sql);
		mysql_query($sql2);
		echo "memu表导入成功，请稍等...";
		echo "<meta http-equiv='refresh' content='1;url=/Public/install.php?p=sql&&n=6'>";
	}elseif($_GET['n'] == 6){
		$sql = "
CREATE TABLE `".$dbqz."message` (
  `id` int(5) NOT NULL auto_increment,
  `pid` int(5) NOT NULL,
  `uid` int(5) NOT NULL,
  `content` varchar(255) character set gbk NOT NULL,
  `type` tinyint(2) NOT NULL,
  `date` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
		";
		mysql_query($sql);
		echo "message表导入成功，请稍等...";
		echo "<meta http-equiv='refresh' content='1;url=/Public/install.php?p=sql&&n=7'>";
	}elseif($_GET['n'] == 7){
		$sql = "
CREATE TABLE `".$dbqz."reply` (
  `id` int(5) NOT NULL auto_increment,
  `fid` int(5) NOT NULL,
  `pid` int(5) NOT NULL,
  `content` text character set gbk NOT NULL,
  `type` tinyint(2) NOT NULL,
  `date` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
		";
		mysql_query($sql);
		echo "reply表导入成功，请稍等...";
		echo "<meta http-equiv='refresh' content='1;url=/Public/install.php?p=sql&&n=8'>";
	}elseif($_GET['n'] == 8){
		$sql = "
CREATE TABLE `".$dbqz."user` (
  `id` int(5) NOT NULL auto_increment,
  `username` varchar(255) NOT NULL,
  `password` varchar(32) NOT NULL,
  `uname` varchar(12) character set gbk NOT NULL,
  `isok` tinyint(2) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
		";
		mysql_query($sql);
		echo "user表导入成功，请稍等...";
		echo "<meta http-equiv='refresh' content='1;url=/Public/install.php?p=sql&&n=9'>";
	}elseif($_GET['n'] == 9){
		$sql = "
CREATE TABLE `".$dbqz."user_date` (
  `uid` int(5) NOT NULL,
  `rank` tinyint(2) NOT NULL,
  `money` int(5) NOT NULL,
  `from` varchar(255) character set gbk NOT NULL,
  `sex` tinyint(2) NOT NULL,
  `birth` int(11) NOT NULL,
  `date` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
		";
		mysql_query($sql);
		echo "user_date表导入成功，请稍等...";
		echo "<meta http-equiv='refresh' content='1;url=/Public/install.php?p=sql&&n=10'>";
	}elseif($_GET['n'] == 10){
		$sql = "
CREATE TABLE `".$dbqz."xunzhang` (
  `id` int(5) NOT NULL auto_increment,
  `uid` int(5) NOT NULL,
  `imgid` int(5) NOT NULL,
  `date` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
		";
		mysql_query($sql);
		echo "xunzhang表导入成功，请稍等...";
		echo "<meta http-equiv='refresh' content='1;url=/Public/install.php?p=sql&&n=11'>";
	}elseif($_GET['n'] == 11){
		$sql = "
CREATE TABLE `".$dbqz."xunzhang_list` (
  `id` int(5) NOT NULL auto_increment,
  `url` varchar(55) NOT NULL,
  `about` varchar(255) character set gbk NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
		";
		mysql_query($sql);
		echo "xunzhang_list表导入成功，请稍等...";
		echo "<meta http-equiv='refresh' content='1;url=/Public/install.php?p=sql&&n=12'>";
	}elseif($_GET['n'] == 12){
		echo "<font color=red>所有数据库导入成功，可以进入下一步！</font>";
	}else{
		echo "正在导入数据...";
		echo "<meta http-equiv='refresh' content='1;url=/Public/install.php?p=sql&&n=2'>";
	}
}
?>