<?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=utf-8">
<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 简体中文UTF-8 V3.3</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$" .
				"core_mysql_host = '$dbinfo[dbhost]';\n\t$" .
				"core_mysql_user = '$dbinfo[dbuser]';\n\t$" .
				"core_mysql_pw	= '$dbinfo[dbpw]';\n\t$" .
				"core_mysql_dbname = '$dbinfo[dbname]';\n\t$" .
				"core_mysql_prefix = '$dbinfo[tablepre]';\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']."link(name, link, picture, type, queue) VALUES ('多彩贴吧', 'http://www.phpcolor.com/','images/logo.gif' , '1', '1')";
			@mysql_connect($dbinfo['dbhost'],$dbinfo['dbuser'],$dbinfo['dbpw']) or die('连接数据库失败!');
			@mysql_select_db($dbinfo['dbname']) or die('无法连接到数据库 '.$dbinfo['dbname']);
			@mysql_query("SET NAMES utf8;");
			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-2012 PhpColor</div>
</div>
</body>
</html>