<?php
	$step = $_GET['step'];
	if (empty($step)){
		Header("Location:index.php?step=1");
		exit();
	}
?>
<!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=gb2312">
<title>PhpColor 安装向导</title>
<link rel="stylesheet" type="text/css" href="images/style.css" />
</head>
<body>
<div class="container">
	<div class="header">
		<h1>PhpColor 安装向导</h1>
		<span>PhpColor 简体中文GBK V4.0 Beta</span>
	</div>
<?php
	if (file_exists('install.lock')){
?>
	<div class="main" style="margin-top:-123px;">
		<div class="error">
			<span>您已经成功安装过程序,如需重新安装,请先删除./install/install.lock文件</span>
		</div>
	</div>
<?php
	} else {
		if ($step == '1'){
?>
	<div class="main" style="margin-top:-123px;">
		<div class="setup step1">
			<h2>开始安装</h2>
			<p>环境以及文件目录权限检查</p>
		</div>
		<div class="stepstat">
			<ul>
				<li class="current">1</li>
				<li class="unactivated">2</li>
				<li class="unactivated">3</li>
				<li class="unactivated last">4</li>
			</ul>
			<div class="stepstatbg stepstat1"></div>
		</div>
	</div>
	<div class="main"><h2 class="title">环境检查</h2>
		<table class="tb" style="margin:20px 0 20px 55px;">
			<tr>
				<th>项目</th>
				<th class="padleft">PhpColor 所需配置</th>
				<th class="padleft">PhpColor 最佳</th>
				<th class="padleft">当前服务器</th>
			</tr>
			<tr>
				<td>操作系统</td>
				<td class="padleft">不限制</td>
				<td class="padleft">Windows</td>
				<td class="w pdleft1"><?=PHP_OS?></td>
			</tr>
			<tr>
				<td>PHP 版本</td>
				<td class="padleft">5.0以上</td>
				<td class="padleft">5.2.6</td>
				<td class="w pdleft1"><?=phpversion()?></td>
			</tr>
			<tr>
				<td>附件上传</td>
				<td class="padleft">不限制</td>
				<td class="padleft">2M</td>
				<td class="w pdleft1"><?=ini_get('upload_max_filesize')?></td>
			</tr>
		</table>
		<h2 class="title">目录、文件权限检查</h2>
		<table class="tb" style="margin:20px 0 20px 55px;width:90%;">
			<tr>
				<th>目录文件</th>
				<th class="padleft">所需状态</th>
				<th class="padleft">当前状态</th>
			</tr>
<?php
			$filestatus = array('configs/db_config.php','configs/config.php','configs/email.php','cache','install','install/index.php','template_c','upload','admin/backup','admin/cache','admin/template_c');
			foreach($filestatus as $val){
				echo "<tr>\n<td>./$val</td><td class=\"w pdleft1\">可写</td>\n";
				if (is_writable('../'.$val)){
					echo "<td class=\"w pdleft1\">可写</td>\n";
				} else {
					echo "<td class=\"nw pdleft1\">不可写</td>\n";
				}
				echo "</tr>\n";
			}
?>
		</table>
	</div>
	<div class="btnbox marginbot">
		<form action="index.php?step=2" method="post">
			<input type="submit" value="下一步" />
		</form>
	</div>
<?php
		} elseif ($step == '2'){
?>
	<div class="main" style="margin-top:-123px;">
		<div class="setup step2">
			<h2>安装数据库</h2>
			<p>连接数据库并进行安装</p>
		</div>
		<div class="stepstat">
			<ul>
				<li class="">1</li>
				<li class="current">2</li>
				<li class="unactivated">3</li>
				<li class="unactivated last">4</li>
			</ul>
			<div class="stepstatbg stepstat1"></div>
		</div>
	</div>
	<div class="main">
		<form action="index.php?step=3" method="post">
			<br />
			<div class="desc"><b>填写数据库信息</b></div>
			<table class="tb2">
				<tr>
					<th class="tbopt">&nbsp;数据库服务器:</th>
					<td><input type="text" name="dbinfo[dbhost]" value="localhost" size="35" class="txt"></td>
					<td>&nbsp;数据库服务器地址, 一般为 localhost</td>
				</tr>
				<tr>
					<th class="tbopt">&nbsp;数据库名:</th>
					<td><input type="text" name="dbinfo[dbname]" size="35" class="txt"></td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<th class="tbopt">&nbsp;数据库用户名:</th>
					<td><input type="text" name="dbinfo[dbuser]" value="root" size="35" class="txt"></td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<th class="tbopt">&nbsp;数据库密码:</th>
					<td><input type="text" name="dbinfo[dbpw]" value="" size="35" class="txt"></td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<th class="tbopt">&nbsp;数据表前缀:</th>
					<td><input type="text" name="dbinfo[tablepre]" value="phpcolor_" size="35" class="txt"></td>
					<td>&nbsp;同一数据库运行多个系统时，请修改前缀</td>
				</tr>
			</table>
			<div class="desc"><b>填写管理员信息</b></div>
			<table class="tb2">
				<tr>
					<th class="tbopt">&nbsp;管理员账号:</th>
					<td><input type="text" name="admininfo[name]" size="35" class="txt"></td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<th class="tbopt">&nbsp;管理员密码:</th>
					<td><input type="password" name="admininfo[password]" size="35" class="txt"></td>
					<td>&nbsp;管理员密码不能为空</td>
				</tr>
				<tr>
					<th class="tbopt">&nbsp;重复密码:</th>
					<td><input type="password" name="admininfo[confirmpassword]" size="35" class="txt"></td>
					<td>&nbsp;</td>
				</tr>
			</table>
			<div class="btnbox marginbot">
					<input type="submit" value="下一步" />
			</div>
		</form>
	</div>
<?php
		} elseif ($step == '3'){
			$dbinfo = $_POST['dbinfo'];
			$admininfo = $_POST['admininfo'];
?>
	<div class="main" style="margin-top:-123px;">
		<div class="setup step3">
			<h2>正在安装</h2>
			<p>正在执行数据库安装</p>
		</div>
		<div class="stepstat">
			<ul>
				<li class="">1</li>
				<li class="">2</li>
				<li class="current">3</li>
				<li class="unactivated last">4</li>
			</ul>
			<div class="stepstatbg stepstat1"></div>
		</div>
	</div>
	<div class="main">
		<textarea>
<?php
			$config = "<?php\n\t
             define(\"c_sqlhost\",\"$dbinfo[dbhost]\");\n\t
             define(\"c_sqluser\",\"$dbinfo[dbuser]\");\n\t
             define(\"c_sqlpw\",\"$dbinfo[dbpw]\");\n\t
             define(\"c_sqldb\",\"$dbinfo[dbname]\");\n\t
             define(\"c_sqlprefix\",\"$dbinfo[tablepre]\");\n\t
             define(\"version\",\"4.0 Beta\");\n?>";
			$fp = fopen('../configs/db_config.php','w');
			fwrite($fp,$config);
			fclose($fp);
			$data = file_get_contents('install.sql');
			$data = str_replace('phpcolor_',$dbinfo['tablepre'],$data);
			$sql = explode(';',$data);
			array_pop($sql);
			$sql[] = "INSERT INTO ".$dbinfo['tablepre']."user(username, passwd, registered, loginip, rank) VALUES ('".$admininfo['name']."', '".md5($admininfo['password'])."','".date('Y-m-d H:i:s')."' , '".$_SERVER['REMOTE_ADDR']."', '2')";
			$sql[] = "INSERT INTO ".$dbinfo['tablepre']."admin(uid, purview) VALUES ('1','')";
			$sql[] = "INSERT INTO ".$dbinfo['tablepre']."style(name, link, type) VALUES ('蓝色经典', 'phpcolor_blue', '1')";
			$sql[] = "INSERT INTO ".$dbinfo['tablepre']."shop_card (`cardname`, `introduction`, `stock`, `price`, `dprice`, `vprice`, `queue`, `type`) VALUES('自沉卡', '可以对自己发布的贴子执行沉贴操作。', 0, 0, 0, 0, 1, 1),('自锁卡', '可以对自己发布的贴子执行锁定操作。', 0, 0, 0, 0, 2, 1),('自删主题卡', '可以对自己发布的贴子执行删除操作。', 0, 0, 0, 0, 3, 1),('自删回贴卡', '可以对自己发布的贴子执行删除操作。', 0, 0, 0, 0, 4, 1),('自管卡', '可以对自己发布主题中，他人的回贴执行删除操作。', 0, 0, 0, 0, 5, 1),('悔过卡', '会员专属道具！可以解除非全局封锁。', 0, 0, 0, 0, 6, 2),('编辑主题卡', '会员专属道具！可以编辑自己发布的主题。', 0, 0, 0, 0, 7, 2),('编辑回贴卡', '会员专属道具！可以编辑自己发布的回贴。', 0, 0, 0, 0, 8, 2),('加精卡', '会员专属道具！可以对任意贴子执行加精操作。', 0, 0, 0, 0, 9, 2),('置顶卡', '会员专属道具！可以对任意贴子执行置顶操作。', 0, 0, 0, 0, 10, 2),('锁定卡', '会员专属道具！可以对任意贴子执行置顶操作。', 0, 0, 0, 0, 11, 2),('沉贴卡', '会员专属道具！可以对任意贴子执行沉贴操作。', 0, 0, 0, 0, 12, 2)";
			@mysql_connect($dbinfo['dbhost'],$dbinfo['dbuser'],$dbinfo['dbpw']) or die('连接数据库失败!');
			@mysql_select_db($dbinfo['dbname']) or die('无法连接到数据库 '.$dbinfo['dbname']);
			@mysql_query("SET NAMES gbk;");
			$i=0;
			foreach($sql as $val){
				if (@mysql_query($val)){
					$i++;
					echo "第 $i 条SQL语句执行成功...\n";
				}
			}
?>
			</textarea>
			<form action="index.php?step=4" method="post">
				<div class="btnbox marginbot">
						<input type="submit" value="下一步" />
				</div>
			</form>
		</div>
<?php
		} elseif ($step == '4'){
?>
	<div class="main" style="margin-top:-123px;">
		<div class="setup step4">
			<h2>安装成功</h2>
			<p>系统安装成功</p>
		</div>
		<div class="stepstat">
			<ul>
				<li class="">1</li>
				<li class="">2</li>
				<li class="">3</li>
				<li class="current last">4</li>
			</ul>
			<div class="stepstatbg stepstat1"></div>
		</div>
	</div>
	<div class="main">
		<div class="error">
			安装成功! <a href="../index.php">点击进入</a>
		</div>
	</div>
<?php
			$fp = fopen('install.php','w');
			fwrite($fp,'');
			fclose($fp);
			rename('install.php','install.lock');
			@unlink('index.php');
		}
	}
?>
	<div class="footer">&copy; 2010-2014 PhpColor</div>
</div>
</body>
</html>