<html>
<head>
<title>拓网长微博系统 安装向导</title>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<style type="text/css">
A { TEXT-DECORATION: none;}
a:hover{ text-decoration: underline;}
.t {font-family: Verdana, Arial, Sans-serif;font-size  : 12px;padding-left: 10px;font-weight: normal;color : #333366;}
.r {font-family: Arial, Sans-serif;font-size  : 12px;font-weight: normal;line-height: 200%;color : #0000EE;}
.c {font-family: Arial, Sans-serif;font-size  : 12px;font-weight: normal;line-height: 200%;color : #EE0000;}
.h {font-family: Arial, Sans-serif;padding-top: 5px;padding-left: 10px;font-size  : 20px;font-weight: bold;color : #000000;}
.i {font-family: Arial, Sans-serif;padding-top: 5px;padding-left: 10px;font-size  : 14px;font-weight: bold;color : #000000;}
table {vertical-align: top;background-color: #f0f0f0;}
</style>
<body vlink='#000000' link='#000000' bgcolor='#6A71A3' leftmargin=0 topmargin=5 marginwidth="0" marginheight="0">
<table width="80%" cellspacing=0 cellpadding=0 align=center valign='center' border=0>
<tr><td class=h valign=top align=left>
<span style="COLOR: #cc0000">&gt;&gt;</span> TopWang Long Weibo 1.0 Installation 
<hr align="center" width="99%" size="1" color=#9999cc></td></tr>
<tr><td class='t' valign='top'>欢迎来到 拓网长微博系统 安装向导，安装前请仔细阅读 安装说明里的每处细节后才能开始安装。安装文件夹里同样提供了有关软件安装的说明，请您同样仔细阅读，以保证安装进程的顺利进行。 
<hr align="center" width="99%" size="1" color=#9999cc></td></tr>
<tr><td class='t' valign='top'>
<b>注意</b><br>
<span class='r'>此种状况表示状态正确.</span><br>
<span class='c'>此种状况表示状态发生错误.</span><hr align="center" width="99%" size="1" color=#9999cc>
<p>
</td>
</tr>
</table>

<?php
error_reporting(E_ERROR | E_PARSE);
@set_time_limit(0);
set_magic_quotes_runtime(0);
if(!@ini_get('register_globals') || !get_magic_quotes_gpc()){
	@extract($_POST,EXTR_SKIP);
	@extract($_GET,EXTR_SKIP);
}
!$_POST && $_POST=array();
!$_GET && $_GET=array();
foreach($_POST as $_key=>$_value){
	!ereg("^\_",$_key) && $$_key=$_POST[$_key];
}
foreach($_GET as $_key=>$_value){
	!ereg("^\_",$_key) && $$_key=$_GET[$_key];
}
define('SYSTEM_ROOT',dirname(__FILE__)."/");
define('IN_SYSTEM', TRUE);
$charset="utf8";
$step=$_REQUEST['step'];
switch($step){
	case "step1":
		act("检查必要目录和文件是否可写，如果发生错误，请更改文件/目录属性 777 ");
		step1();
		break;
	case "step2":
		act("设置数据库资料与管理员账号");
		step2();
		break;
	case "step3":
		act("检查数据库配置文件");
		step3();
		break;
	case "step4":
		act("系统安装进程");
		step4();
		break;
	default:
		act("拓网长微博系统 用户许可协议");
		step0();
}
function act($action_title){
?>
<table width="80%" cellspacing=0 cellpadding=0 align=center valign='center' border=0>
<tr>
<td valign=top align=center>
<b><font><?php echo $action_title;?></font></b><br>
　</td>
</tr>
</table>
<?php
}
function step0(){
	$licence_content=read_file(SYSTEM_ROOT."licence.txt");
?>
<table width="80%" cellspacing=0 cellpadding=0 align=center valign='center' border=0>
<tr>
<td class='t' valign=top align=left>
	<?php
		if($licence_content){
			echo nl2br($licence_content);
		}
	?>
　<p></td>
</tr>
<tr>
<td valign=top align='center'>
<form method="post" action='?'>
<input type="hidden" name="step" value="step1">
<input type="submit" name="submit" value="同意">&nbsp;
<input type="button" name="exit" value="不同意" onclick="javascript: window.close();">
</form>
　</td>
</tr>
</table>
<?php
}
function step1(){
	
?>
<table width="80%" cellspacing=0 cellpadding=0 align=center valign='center' border=0>
<tr>
<td class='t' valign=top align=left>
<p>
	<?php
		echo check_wable('files')."<br>";
		echo check_wable('templates')."<br>";
		echo check_wable('config.php');
	?>
<p><hr align="center" width="99%" size="1" color=#9999cc></td>
<tr>
<td valign=top align='center'>
<form method="post" action='?'>
<input type="hidden" name="step" value="step2">
<input type="submit" name="submit" value="下一步">&nbsp;
</p>
</td>
</table>

<?php
}
function step2(){
?>
<table width="80%" cellspacing=0 cellpadding=0 align=center valign='center' border=0>
<form method="post" action='?'>
<tr>
<td class='t' valign=top align=left>
	<table width="70%" cellspacing=0 cellpadding=1 align=center>
<tr><td bgcolor='#6A71A3'>
	<table width="100%" cellspacing=1 cellpadding=3 align=center>
	<tr><td class='t' colspan=2 bgcolor='#6A71A3'><font color="#FFFFFF">服务器配置</font></td></tr>
	<tr><td class='t' width='40%'>&nbsp;&nbsp;数据库服务器</td>
	<td class='t'><input type='text' name='db_host' value='localhost'></td></tr>
	<tr><td class='t'>&nbsp;&nbsp;数据库用户名</td>
	<td class='t'><input type='text' name='db_user' value=''></td></tr>
	<tr><td class='t'>&nbsp;&nbsp;数据库密码</td>
	<td class='t'><input type='text' name='db_pwd' value=''></td></tr>
	<tr><td class='t'>&nbsp;&nbsp;数据库名</td>
	<td class='t'><input type='text' name='db_name' value=''></td></tr>
	</table>
</td></tr></table>
</td>
</tr>
<tr><td align="center" height="50">
<input type="hidden" name="step" value="step3">
<input type="submit" name="submit" value="开始安装">
</td></tr>
</form>
</table>
<?php
}
function step3(){
	global $charset;
	$dbhost=$_POST['db_host'];
	$dbuser=$_POST['db_user'];
	$dbpwd=$_POST['db_pwd'];
	$dbname=$_POST['db_name'];
	$conn=@mysql_connect($dbhost,$dbuser,$dbpwd);
	if(!$conn) alert("抱歉，数据库连接出错，无法进行安装!","back",0);
	
	if(!@mysql_select_db($dbname)) {
		if(mysql_get_server_info() > '4.1' && $charset){
			mysql_query("CREATE DATABASE $dbname DEFAULT CHARACTER SET $charset");
		}else{
			mysql_query("CREATE DATABASE $dbname");
		}
	}
	$sdb=@mysql_select_db($dbname);
	if(!$sdb) alert("抱歉，数据库名错误，请正确填写！","back",0);
	$sysconfig=
"<?php
\$SYS_CONFIG['dbhost']='$dbhost';      // 数据库服务器
\$SYS_CONFIG['dbuser']='$dbuser';      // 数据库用户名
\$SYS_CONFIG['dbpwd']='$dbpwd';        // 数据库密码
\$SYS_CONFIG['dbname']='$dbname';      // 数据库名
\$SYS_CONFIG['pconnect']=0;            // 数据库持久连接 0=关闭, 1=打开
".'?>';
	$wstate=write_file("config.php",$sysconfig);
?>
<table width="80%" cellspacing=0 cellpadding=0 align=center valign='center' border=0>
<tr>
<td class='t' valign=top align=left>
<font class='r'>数据库配置资料写入完成</font>
</td>
</tr>
<tr>
<td class='t' valign=top align=center>
<form method="post" action='?'>
<input type="hidden" name="step" value="step4">
<input type="submit" name="submit" value="继续安装">&nbsp;
<input onclick='history.go(-1)' type='button' value='返回上一页'>
</form>
</td>
</tr>
</table>
<?php
}
function step4(){
?>
<table width="80%" cellspacing=0 cellpadding=0 align=center valign='center' border=0>
<tr>
<td class='t' valign=top align=left>
<?php
	$sqlcontent=read_file(SYSTEM_ROOT.'install.sql');
	if(!$sqlcontent) alert('数据库安装文件install.sql不存在无法进行安装，请修复该文件后再安装本系统!',"back",0);
	runquery($sqlcontent);
	@unlink(SYSTEM_ROOT."install.php")
?>
</td>
</tr>
<tr>
<td class='t' valign=top align=center>
<input type="button" value="进入系统" onclick="location.href='index.php'">&nbsp;
</td>
</tr>
</table>
<?php
}
?>
</body>
</html>
<?php
function read_file($filename,$method="rb"){
	if($handle=@fopen($filename,$method)){
		flock($handle,LOCK_SH);
		$filedata=@fread($handle,filesize($filename));
		fclose($handle);
	}
	return $filedata;
}
function write_file($filename,$data,$method="rb+"){
	@touch($filename);
	if($handle=@fopen($filename,$method)){
		flock($handle,LOCK_EX);
		fputs($handle,$data);
		if($method=="rb+") ftruncate($handle,strlen($data));
		fclose($handle);
	}
}
function check_wable($path){
	if(is_writeable(SYSTEM_ROOT.$path)){
		return $path."......"."<font class='r'>OK</font>";
	}else{
		return $path."......"."<font class='c'>NO</font>";
	}
}

function alert($sysmsg,$backurl,$topwindow){
	if ($sysmsg!=""){
		echo "<script>alert('".$sysmsg."');";
	}
	if($topwindow==1){
		$winname="top";
	}else{
		$winname="self";
	}
	switch($backurl){
		case "back":
			echo $winname.".history.back();";
			break;
		case "close":
			echo "windows.close();";
			break;
		default:
			if (!empty($backurl)){
				echo $winname . ".location.href = '".$backurl."';";
			}
	}
	echo "</script>";
	die();
 }

	global $charset;
	require SYSTEM_ROOT.'./include/cls_database.php';
	$clsdb = new cls_database;

function runquery($sql) {
	require SYSTEM_ROOT.'./config.php';
	require SYSTEM_ROOT.'./include/cls_database.php';
	$clsdb = new cls_database;
	$clsdb->connect($SYS_CONFIG['dbhost'],$SYS_CONFIG['dbuser'],$SYS_CONFIG['dbpwd'], $SYS_CONFIG['dbname']);
	global $charset;
	$sql = str_replace("\r", "\n", $sql);
	$ret = array();
	$num = 0;
	foreach(explode(";\n", trim($sql)) as $query) {
		$queries = explode("\n", trim($query));
		foreach($queries as $query) {
			$ret[$num] .= $query[0] == '#' || $query[0].$query[1] == '--' ? '' : $query;
		}
		$num++;
	}
	unset($sql);
	foreach($ret as $query) {
		$query = trim($query);
		if($query) {
			if(substr($query, 0, 12) == 'CREATE TABLE') {
				$name=substr($query,13,strpos($query,'(')-13);
				echo '创建 '.$name.' ... <font color="#0000EE">成功</font><br>';
				$clsdb->query(createtable($query,$charset));
			} else {
				$clsdb->query($query);
			}
		}
	}
}
function createtable($sql, $charset) {
	$type = strtoupper(preg_replace("/^\s*CREATE TABLE\s+.+\s+\(.+?\).*(ENGINE|TYPE)\s*=\s*([a-z]+?).*$/isU", "\\2", $sql2));
	$type = in_array($type, array('MYISAM', 'HEAP')) ? $type : 'MYISAM';
	return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).
		(mysql_get_server_info() > '4.1' ? " ENGINE=$type DEFAULT CHARSET=$charset" : " TYPE=$type");
}
?>