<?php
@set_time_limit(1000);
set_magic_quotes_runtime(0);
if(PHP_VERSION < '4.1.0') {
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
}
$installfile = basename(__FILE__);
$sqlfile = './install/ftbbs.sql';
$lockfile = './cache/install.lock';
$quit = FALSE;

@include './install/install.lang.php';
@include './install/global.func.php';
@include './inc/config.php';
@include './inc/ftbbsdbclass.php';

$inslang = defined('INSTALL_LANG') ? INSTALL_LANG : '';
$version = '5.6'.$lang[$inslang];

if(!defined('INSTALL_LANG') || !function_exists('instmsg') || !is_readable($sqlfile)) {
	exit("Please upload all files to install FTBBS Board<br>安装 FTBBS 论坛您必须上传所有文件，否则无法继续");
}elseif(!ini_get('short_open_tag')) {
	instmsg('short_open_tag_invalid');
} elseif(!isset($dbhost) || !isset($cookieprename)) {
	instmsg('config_nonexistence');
} elseif(file_exists($lockfile)) {
	instmsg('lock_exists');
} elseif(!class_exists('MysqlConnector')) {
	instmsg('database_nonexistence');
}

if(function_exists('instheader')) {
	instheader();
}

if(empty($dbcharset) && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) {
	$dbcharset = str_replace('-', '', $charset);
}

$action = $_POST['action'] ? $_POST['action'] : $_GET['action'];
if(in_array($action, array('check', 'config'))) {
	if(is_writeable('./inc/config.php')) {
		$writeable['config'] = result(1, 0);
		$write_error = 0;
	} else {
		$writeable['config'] = result(1, 0);
		$write_error = 1;
	}
}

if(!$action) {

	$ftbbs_license = str_replace('  ', '&nbsp; ', $lang['license']);

?>
<tr><td><b><?=$lang['current_process']?> </b><font color="#0000EE"><?=$lang['show_license']?></font></td></tr>
<tr><td><hr noshade align="center" width="100%" size="1"></td></tr>
<tr><td><br>
<table width="90%" cellspacing="1" bgcolor="#000000" border="0" align="center">
<tr><td class="altbg1">
<table width="99%" cellspacing="1" border="0" align="center">
<tr><td><?=$ftbbs_license?></td></tr>
</table></td></tr></table>
</td></tr>
<tr><td align="center">
<br><form method="post" action="<?=$installfile?>">
<input type="hidden" name="action" value="config">
<input type="submit" name="submit" value="<?=$lang['agreement_yes']?>" style="height: 25">&nbsp;
<input type="button" name="exit" value="<?=$lang['agreement_no']?>" style="height: 25" onclick="javascript: window.close();">
</form></td></tr>
<?
} elseif($action == 'config') {
?>
<tr><td><b><?=$lang['current_process']?> </b><font color="#0000EE"> <?=$lang['edit_config']?></font></td></tr>
<tr><td><hr noshade align="center" width="100%" size="1"></td></tr>
<tr><td><br>
<?php
    if ($write_error==1){
	$inputreadonly = $write_error ? 'readonly' : '';
	$msg = '<li>'.$lang['config_unwriteable'].'</li>';
	}else{
	$msg = '<li>'.$lang['config_comment'].'</li>';
    }
	if($_POST['saveconfig']) {
		$msg = '';
		$dbhost = setconfig($_POST['dbhost']);
		$dbuser = setconfig($_POST['dbuser']);
		$dbpw = setconfig($_POST['dbpw']);
		$dbname = setconfig($_POST['dbname']);
		$domain = setconfig($_POST['domain']);
		$tablepre = setconfig($_POST['tablepre']);
		
	    $checkdirarray = array('inc' => './inc','cache' => './cache','flashad' => './flashad');
	    foreach($checkdirarray as $key => $dir) {
		       if(dir_writeable($dir)) {
			   
		       } else {
			     $langkey = $key.'_unwriteable';
			     $msg .= "<li>$lang[$langkey]</li>";
			     $quit = TRUE;
		      }
	    }
		if($rot = @fopen("./test.txt", 'w')) {
			@fclose($rot);
			@unlink("./test.txt");
		}else{
			$msg = '<li>'.$lang['siteroot'].'</li>';
			$quit = TRUE;
		}
		 if(empty($dbname)) {
			 $msg .= '<li>'.$lang['dbname_invalid'].'</li>';
			 $quit = TRUE;
		   } else {
			 if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {
				$errormsg = 'database_errno_2003';
				$msg .= '<li>'.$lang[$errormsg].'</li>';
				$quit = TRUE;
			 } else {
				if(mysql_get_server_info() > '4.1') {
					mysql_query("CREATE DATABASE IF NOT EXISTS `$dbname` DEFAULT CHARACTER SET $dbcharset");
				} else {
					mysql_query("CREATE DATABASE IF NOT EXISTS `$dbname`");
				}
				if(mysql_errno()) {
					$errormsg = 'database_errno_1044';
					$msg .= "'<li>$errormsg ".$lang[$errormsg].'</li>';
					$quit = TRUE;
				}
				mysql_close();
			}
		 }

		if(!$quit){
			if(!$write_error) {
				$fp = fopen('./inc/config.php', 'r');
				$configfile = fread($fp, filesize('./inc/config.php'));
				fclose($fp);

				$configfile = preg_replace("/[$]dbhost\s*\=\s*[\"'].*?[\"'];/is", "\$dbhost = '$dbhost';", $configfile);
				$configfile = preg_replace("/[$]dbuser\s*\=\s*[\"'].*?[\"'];/is", "\$dbuser = '$dbuser';", $configfile);
				$configfile = preg_replace("/[$]dbpw\s*\=\s*[\"'].*?[\"'];/is", "\$dbpw = '$dbpw';", $configfile);
				$configfile = preg_replace("/[$]dbname\s*\=\s*[\"'].*?[\"'];/is", "\$dbname = '$dbname';", $configfile);
				$configfile = preg_replace("/[$]cookiedomain\s*\=\s*[\"'].*?[\"'];/is", "\$cookiedomain = '$domain';", $configfile);
				$configfile = preg_replace("/[$]tablepre\s*\=\s*[\"'].*?[\"'];/is", "\$tablepre = '$tablepre';", $configfile);

				$fp = fopen('./inc/config.php', 'w');
				fwrite($fp, trim($configfile));
				fclose($fp);
			}
			redirect("$installfile?action=admin");
		}
	}

?>
<tr><td align="center">
<table width="80%" cellspacing="1" bgcolor="#000000" border="0" align="center">
<tr bgcolor="#085878"><td style="color: #FFFFFF; padding-left: 10px" width="32%"><?=$lang['tips_message']?></td>
</tr><tr>
<td class="message"><?=$msg?></td>
</tr></table><br>
<form method="post" action="<?=$installfile?>">
<table width="80%" cellspacing="1" bgcolor="#000000" border="0" align="center">
<tr class="header">
<td width="20%"><?=$lang['variable']?></td><td width="30%"><?=$lang['value']?></td><td width="50%"><?=$lang['comment']?></td>
</tr><tr>
<td class="altbg1">&nbsp;<span class="redfont"><?=$lang['dbhost']?></span></td>
<td class="altbg2"><input type="text" name="dbhost" value="<?=$dbhost?>" <?=$inputreadonly?> size="30"></td>
<td class="altbg1">&nbsp;<?=$lang['dbhost_comment']?></td>
</tr><tr>
<td class="altbg1">&nbsp;<?=$lang['dbuser']?></td>
<td class="altbg2"><input type="text" name="dbuser" value="<?=$dbuser?>" <?=$inputreadonly?> size="30"></td>
<td class="altbg1">&nbsp;<?=$lang['dbuser_comment']?></td>
</tr><tr>
<td class="altbg1">&nbsp;<?=$lang['dbpw']?></td>
<td class="altbg2"><input type="password" name="dbpw" value="<?=$dbpw?>" <?=$inputreadonly?> size="30"></td>
<td class="altbg1">&nbsp;<?=$lang['dbpw_comment']?></td>
</tr><tr>
<td class="altbg1">&nbsp;<?=$lang['dbname']?></td>
<td class="altbg2"><input type="test" name="dbname" value="<?=$dbname?>" <?=$inputreadonly?> size="30"></td>
<td class="altbg1">&nbsp;<?=$lang['dbname_comment']?></td>
</tr>
<tr>
<td class="altbg1">&nbsp;<?=$lang['tablepre']?></td>
<td class="altbg2"><input name="tablepre" type="text" id="tablepre" value="<?=$tablepre?>" size="30" <?=$inputreadonly?> ></td>
<td class="altbg1">&nbsp;<?=$lang['tablepre_comment']?></td>
</tr>
<tr>
<td class="altbg1">&nbsp;<?=$lang['domain']?></td>
<td class="altbg2"><input type="text" name="domain" value="<?=$domain?>" <?=$inputreadonly?> size="30"></td>
<td class="altbg1">&nbsp;<?=$lang['domain_comment']?></td>
</tr>
<!--<tr>
<td class="altbg1">&nbsp;<span class="redfont"><?=$lang['tablepre']?></span></td>
<td class="altbg2"><input type="text" name="tablepre" value="<?=$ft?>" <?=$inputreadonly?> size="30"></td>
<td class="altbg1">&nbsp;<?=$lang['tablepre_comment']?></td>
</tr>-->
</table><br>
<input type="hidden" name="action" value="config">
<input type="hidden" name="saveconfig" value="1">
<input type="button" name="submit" value=" <?=$lang['old_step']?> " style="height: 25" onclick="window.location='install.php'">&nbsp;
<input type="submit" name="submit" value=" <?=$lang['new_step']?> " style="height: 25">
</form></td></tr>
<?php
instfooter();
} elseif($action == 'admin') {

?>
<tr><td><b><?=$lang['current_process']?> </b><font color="#0000EE"> <?=$lang['add_admin']?></font></td></tr>
<tr><td><hr noshade align="center" width="100%" size="1"></td></tr>
<tr><td><br>
<?php

	$msg = '<li>'.$lang['add_admin'].'</li>';
	if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {
		$errormsg = 'database_errno_1045';
		$msg .= '<li>'.$lang[$errormsg].'</li>';
		$quit = TRUE;
	} else {
		$curr_mysql_version = mysql_get_server_info();
		if($curr_mysql_version < '3.23') {
			$msg .= '<li>'.$lang['mysql_version_323'].'</li>';
			$quit = TRUE;
		}

		$sqlarray = array(
				'createtable' => 'CREATE TABLE mydb_test (test TINYINT (3) UNSIGNED)',
				'insert' => 'INSERT INTO mydb_test (test) VALUES (1)',
				'select' => 'SELECT * FROM mydb_test',
				'update' => 'UPDATE mydb_test SET test=\'2\' WHERE test=\'1\'',
				'delete' => 'DELETE FROM mydb_test WHERE test=\'2\'',
				'droptable' => 'DROP TABLE mydb_test'
			);

		foreach($sqlarray as $key => $sql) {
			mysql_select_db($dbname);
			mysql_query($sql);
			if(mysql_errno()) {
				$errnolang = 'dbpriv_'.$key;
				$msg .= '<li>'.$lang[$errnolang].'</li>';
				$quit = TRUE;
			}
		}

	}

	if($_POST['submit']) {

		$username = $_POST['username'];
		$password1 = $_POST['password1'];
		$password2 = $_POST['password2'];

		if($username && $password1 && $password2) {
			if($password1 != $password2) {
				$msg .= '<li><font color="#FF0000">'.$lang['admin_password_invalid'].'</font></li>';
				$quit = TRUE;
			} elseif(strlen($username) > 15 || preg_match("/^$|^c:\\con\\con$|　|[,\"\s\t\<\>&]|^游客|^Guest/is", $username)) {
				$msg = $lang['admin_username_invalid'];
			}
		} else {
			$msg .= '<li><font color="#FF0000">'.$lang['admin_invalid'].'</font></li>';
			$quit = TRUE;
		}

		if(!$quit){
			redirect("$installfile?action=install&username=".rawurlencode($username)."&password=".md5($password1));
		}

	}

?>
<tr><td align="center">
<table width="80%" cellspacing="1" bgcolor="#000000" border="0" align="center">
<tr bgcolor="#085878"><td style="color: #FFFFFF; padding-left: 10px" width="32%"><?=$lang['tips_message']?></td></tr>
<tr><td class="message"><?=$msg?></td></tr></table><br>
</td></tr>
<tr><td align="center">
<form method="post" action="<?=$installfile?>" <?=$alert?>>
<table width="80%" cellspacing="1" bgcolor="#000000" border="0" align="center">
<tr bgcolor="#085878">
<td style="color: #FFFFFF; padding-left: 10px" colspan="2"><?=$lang['add_admin']?></td>
</tr><tr>
<td class="altbg1" width="20%">&nbsp;<?=$lang['username']?></td>
<td class="altbg2" width="80%">&nbsp;<input type="text" name="username" value="admin" size="30"></td>
</tr><tr>
<td class="altbg1">&nbsp;<?=$lang['password']?></td>
<td class="altbg2">&nbsp;<input type="password" name="password1" size="30"></td>
</tr><tr>
<td class="altbg1">&nbsp;<?=$lang['repeat_password']?></td>
<td class="altbg2">&nbsp;<input type="password" name="password2" size="30"></td>
</tr></table><br>
<input type="hidden" name="action" value="admin">
<input type="button" name="submit" value=" <?=$lang['old_step']?> " style="height: 25" onclick="window.location='?action=config'">&nbsp;
<input type="submit" name="submit" value=" <?=$lang['new_step']?> " style="height: 25">
</form></td></tr>
<?php
} elseif($action == 'install') {

	$username = htmlspecialchars($_GET['username']);
	$password = htmlspecialchars($_GET['password']);
	
    $db = new MysqlConnector();
    $db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
    $db->select_db($dbname);

?>
<tr><td><b><?=$lang['current_process']?> </b><font color="#0000EE"> <?=$lang['start_install']?></font></td></tr>
<tr><td><hr noshade align="center" width="100%" size="1"></td></tr>
<tr><td align="center"><br>
<script type="text/javascript">
	function showmessage(message) {
		document.getElementById('notice').value += message + "\r\n";
	}
</script>
<textarea name="notice" style="width: 80%; height: 330px" readonly id="notice"></textarea>

<br><br>
<input type="button" name="submit" value=" <?=$lang['install_in_processed']?> " disabled style="height: 25" onclick="window.location='default.php'" id="laststep"><br><br>
<br>
</td></tr>
<?php
	instfooter();
	$fp = fopen($sqlfile, 'rb');
	$sql = fread($fp, filesize($sqlfile));
	fclose($fp);
	runquery($sql);
	$db->query("UPDATE {$tablepre}ftbbs_admin SET admin_user='$username',admin_pwd='$password' WHERE id=1;");

	$db->query("insert into {$tablepre}ftbbs_touxian(xiao,da,nick,classx) values(0,50,'小野人','userheadimg/class8.gif');");
	$db->query("insert into {$tablepre}ftbbs_touxian(xiao,da,nick,classx) values(50,150,'野人','userheadimg/class9.gif');");
	$db->query("insert into {$tablepre}ftbbs_touxian(xiao,da,nick,classx) values(150,500,'土人','userheadimg/class10.gif');");
	$db->query("insert into {$tablepre}ftbbs_touxian(xiao,da,nick,classx) values(500,1000,'咕噜','userheadimg/class11.gif');");
	$db->query("insert into {$tablepre}ftbbs_touxian(xiao,da,nick,classx) values(1000,2000,'大咕噜','userheadimg/class12.gif');");
	$db->query("insert into {$tablepre}ftbbs_touxian(xiao,da,nick,classx) values(2000,4000,'咕噜队长','userheadimg/class13.gif');");
	$db->query("insert into {$tablepre}ftbbs_touxian(xiao,da,nick,classx) values(4000,8000,'初级守卫','userheadimg/class14.gif');");
	$db->query("insert into {$tablepre}ftbbs_touxian(xiao,da,nick,classx) values(8000,12000,'守卫队长','userheadimg/class15.gif');");
	$db->query("insert into {$tablepre}ftbbs_touxian(xiao,da,nick,classx) values(12000,16000,'骑士','userheadimg/class16.gif');");
	$db->query("insert into {$tablepre}ftbbs_touxian(xiao,da,nick,classx) values(16000,18000,'青铜骑士','userheadimg/class17.gif');");
	$db->query("insert into {$tablepre}ftbbs_touxian(xiao,da,nick,classx) values(18000,20000,'白银骑士','userheadimg/class18.gif');");
	$db->query("insert into {$tablepre}ftbbs_touxian(xiao,da,nick,classx) values(20000,22000,'黄金骑士','userheadimg/class19.gif');");
	$db->query("insert into {$tablepre}ftbbs_touxian(xiao,da,nick,classx) values(22000,800000,'神秘头衔','userheadimg/class20.gif');");

    $sitenav='<div id="TopNav"><div><a href="http://www.ftbbs.cn/default.asp?a=A_5&b=A_5_5">官方动态</a>';
    $sitenav=$sitenav.'·<a href="http://php.ftbbs.cn" target="_blank">PHP版讨论区</a>';
    $sitenav=$sitenav.'·<a href="http://www.ftbbs.cn/order.asp" target="_blank">购买流程</a>';
    $sitenav=$sitenav.'·<a href="http://www.ftbbs.cn/dingzhi.asp" target="_blank">定制服务</a>';
    $sitenav=$sitenav.'·<a href="http://www.ftbbs.cn/host.asp" target="_blank">空间租用</a>';
    $sitenav=$sitenav.'·<a href="http://www.ftbbs.cn/pay.asp" target="_blank">支付方式</a>';
    $sitenav=$sitenav.'·<a href="http://www.chinaymz.com" target="_blank">源码下载</a>';
    $sitenav=$sitenav.'·<a href="http://www.ftbbs.cn" target="_blank">官方首页</a></div>';
    $sitenav=$sitenav.'</div>';
	$db->query("UPDATE {$tablepre}ftbbsmb SET sitenav='$sitenav' WHERE id=1;");
	$db->query("insert into {$tablepre}xunzhang(xzms, xzimg, flag) VALUES ('灌水天才奖', 'xz1.gif', '0');");
	$db->query("insert into {$tablepre}xunzhang(xzms, xzimg, flag) VALUES ('特殊贡献奖', 'xz2.gif', '0');");
	$db->query("insert into {$tablepre}xunzhang(xzms, xzimg, flag) VALUES ('优秀版主奖', 'xz3.gif', '0');");
	$db->query("insert into {$tablepre}xunzhang(xzms, xzimg, flag) VALUES ('新人进步奖', 'xz4.gif', '0');");
	$db->query("insert into {$tablepre}xunzhang(xzms, xzimg, flag) VALUES ('终身成就奖', 'xz5.gif', '0');");
	$db->query("insert into {$tablepre}xunzhang(xzms, xzimg, flag) VALUES ('宣传大使奖', 'xz6.gif', '0');");
	@touch($lockfile);

	echo '<script type="text/javascript">document.getElementById("laststep").disabled = false; </script>'."\r\n";
	echo '<script type="text/javascript">document.getElementById("laststep").value = \''.$lang['install_succeed'].'\'; </script>'."\r\n";
	echo '<iframe width="0" height="0" src="http://www.ftbbs.cn"></iframe>';
}
?>
