<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
@set_time_limit(1000);
set_magic_quotes_runtime(0);
if(PHP_VERSION < '4.1.0') {
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
}
define('MONKEYMAO', true);
define('MODEPATH', '');
$time=time();
$installfile = basename(__FILE__);
$sqlfile = './install/mysql.sql';
$sqlpicdb = './install/mysql.sql';
$lockfile = './cache/install.lock';
$attachdir = './uploads';
$attachurl = 'uploads';
@include './install/language.php';
@include './install/global.func.php';
@include './config.inc.php';
@include './includes/db_mysql.class.php';
if(!defined('INSTALL_LANG') || !function_exists('mexit') || !is_readable($sqlfile) || !is_readable($sqlpicdb)) {
	exit('Please upload all files to install!<br>&#x5B89;&#x88C5;&#x672C;&#x7A0B;&#x5E8F;&#x60A8;&#x5FC5;&#x987B;&#x4E0A;&#x4F20;&#x6240;&#x6709;&#x6587;&#x4EF6;&#xFF0C;&#x5426;&#x5219;&#x65E0;&#x6CD5;&#x7EE7;&#x7EED;<br>&#x5B89;&#x88DD;&#x672C;&#x7A0B;&#x5F0F;&#x60A8;&#x5FC5;&#x9808;&#x4E0A;&#x50B3;&#x6240;&#x6709;&#x6587;&#x6A94;&#xFF0C;&#x5426;&#x5247;&#x7121;&#x6CD5;&#x7E7C;&#x7E8C;');
}elseif(!isset($dbhost)){
	msg('config_notfined');
} elseif(!ini_get('short_open_tag')) {
	msg('short_open_tag_invalid');
} elseif(file_exists($lockfile)) {
	msg('install_lock');
} elseif(!class_exists('db_class')) {
	msg('database_nonexistence');
}
$dbcharset = str_replace('-', '', $charset);
function_exists('head') && head();

	$mustset = false;
	$os = PHP_OS;	
	$php_version = PHP_VERSION;
	if($php_version < '4.0.6') {
		$php_version= "<font color=\"#FF0000\">$lang[php_version]</font>";
		$mustset = true;
	}	
	if(@ini_get(file_uploads)) {		
		$file_uploads = $lang['upload_enabled'].@ini_get(upload_max_filesize);
	} else {
		$file_uploads = "<font color=\"#FF0000\">$lang[upload_disabled]</font>";
		$mustset = true;
	}
	if(@extension_loaded('gd')) {		
		$gd = @gd_info();		
		$gdinfo= $lang['gd_enabled'].$gd['GD Version'];		
	} else {
		$gdinfo= "<font color=\"#FF0000\">$lang[gd_disabled]</font>";
		$mustset = true;
	}	
	if(@ini_get('allow_url_fopen') || (!@ini_get('allow_url_fopen') && @function_exists('fsockopen'))) {			
		$url_fopen = $lang['url_fopen_enabled'];		
	} else {
		$url_fopen = "<font color=\"#FF0000\">$lang[url_fopen_disabled]</font>";
		$mustset = true;
	}
	if(@function_exists('mysql_connect')) {			
		$mysql = $lang['mysql_enabled'];		
	} else {
		$mysql = "<font color=\"#FF0000\">$lang[mysql_disabled]</font>";
		$mustset = true;
	}

	if(@mkdir('mkdirtext',0777)) {		
		rmdir('mkdirtext');
		$mkdir = $lang['writeable'];		
	} else {
		$mkdir = "<font color=\"#FF0000\">$lang[mkdir_disabled]</font>";
		$mustset = true;
	}
	$dirs = array(
		'cache' => './cache',
		'themes' => './cache/themes',
		'flashshow' => './flashshow',
		'import' => './import',
		'uploads' => $attachdir,		
	);

	$checkwrite = array();
	
	if(is_writeable('./config.inc.php')) {
		$checkwrite['config'] = $lang['writeable'];		
	} else {
		$checkwrite['config'] = "<font color=\"#FF0000\">$lang[unwriteable]</font>";
		$mustset = true;
	}
	
	foreach($dirs AS $key => $dir) {
		if(checkwrite($dir)) {
			$checkwrite[$key] = $lang['writeable'];
		}else{
			$checkwrite[$key] = "<font color=\"#FF0000\">".$lang['unwriteable']."</font>";
			$mustset = true;
		}
	}
	
	$step = $_POST['step'] ? $_POST['step'] : $_GET['step'];
	$step = !$mustset && in_array($step, array('dbset','importdb','setadmin')) ? $step : '';
if($step == 'dbset') {
	$msg = '';

	if($_POST['saveconfig']) {
		$dbhost = setconfig($_POST['dbhost']);
		$dbuser = setconfig($_POST['dbuser']);
		$dbpassword = setconfig($_POST['dbpassword']);
		$dbname = setconfig($_POST['dbname']);
		$tablepre = setconfig($_POST['tablepre']);
		$picdb = $_POST['picdb'];
		if(empty($dbname)) {
			$msg .= '<li>'.$lang['dbname_invalid'].'</li>';
			$mustset = true;
		} else {
			$errormsg = '';
			if(!@mysql_connect($dbhost, $dbuser, $dbpassword)) {
				$errormsg = 'database_errno_'.mysql_errno();
				$msg .= '<li>'.$lang[$errormsg].'</li>';
				$mustset = 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_'.mysql_errno();
					$msg .= "'<li>$errormsg ".$lang[$errormsg].'</li>';
					$mustset = true;
				}

				mysql_close();
			}
		}

		if(strstr($tablepre, '.')) {
			$msg .= '<li>'.$lang['tablepre_invalid'].'</li>';
			$mustset = true;
		}

		if(!$mustset){
			if(!$write_error) {
				$configs = readfromfile('./config.inc.php');
				$configs = preg_replace("/[$]dbhost\s*\=\s*[\"'].*?[\"'];/is", "\$dbhost = '$dbhost';", $configs);
				$configs = preg_replace("/[$]dbuser\s*\=\s*[\"'].*?[\"'];/is", "\$dbuser = '$dbuser';", $configs);
				$configs = preg_replace("/[$]dbpassword\s*\=\s*[\"'].*?[\"'];/is", "\$dbpassword = '$dbpassword';", $configs);
				$configs = preg_replace("/[$]dbname\s*\=\s*[\"'].*?[\"'];/is", "\$dbname = '$dbname';", $configs);
				$configs = preg_replace("/[$]tablepre\s*\=\s*[\"'].*?[\"'];/is", "\$tablepre = '$tablepre';", $configs);
				$configs = preg_replace("/[$]cookiepre\s*\=\s*[\"'].*?[\"'];/is", "\$cookiepre = '".random(3)."_';", $configs);
				writetofile('./config.inc.php',$configs);
			}
			msg('gonext',"?step=setadmin&picdb=$picdb");
		}
	}

	include './install/dbset.inc.php';
}elseif($step == 'setadmin'){
$picdb = $_GET['picdb'];
		$msg = $alert='';
	if(!@mysql_connect($dbhost, $dbuser, $dbpassword)) {
		$errormsg = 'database_errno_'.mysql_errno();
		$msg .= '<li>'.($lang[$errormsg] ? $lang[$errormsg] : mysql_error()) .'</li>';
		$mustset = true;
	} else {
		$curr_mysql_version = mysql_get_server_info();
		if($curr_mysql_version < '3.23') {
			$msg .= '<li>'.$lang['mysql_version_323'].'</li>';
			$mustset = true;
		}

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

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

		$result = mysql_query("SELECT COUNT(*) FROM $tablepre"."settings");
		if($result) {
			$msg .= '<li>'.$lang['db_not_null'].'</li>';
			
		}
	}


	if($_POST['setadmin']) {

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

		if($username && $email && $password1 && $password2) {
			if($password1 != $password2) {
				$msg .= '<li>'.$lang['admin_password_invalid'].'</li>';
				$mustset = true;
			} elseif(strlen($username) > 15 || preg_match("/^$|^c:\\con\\con$|¡¡|[,\"\s\t\<\>&]|^ÓÎ¿Í|^Guest/is", $username)) {
				$msg .= $lang['admin_username_invalid'];
				$mustset = true;
			} elseif(!strstr($email, '@') || $email != stripslashes($email) || $email != htmlspecialchars($email)) {
				$msg .= $lang['admin_email_invalid'];
				$mustset = true;
			}
		} else {
			$msg .= '<li>'.$lang['admin_invalid'].'</li>';
			$mustset = true;
		}

		if(!$mustset){
			msg('gonext',"$installfile?step=importdb&picdb=$picdb&username=".rawurlencode($username)."&email=".rawurlencode($email)."&password=".md5($password1));
		}

	}

	include './install/setadmin.inc.php';

}elseif($step == 'importdb'){
	$username = htmlspecialchars($_GET['username']);
	$email = htmlspecialchars($_GET['email']);
	$password = htmlspecialchars($_GET['password']);
	$picdb = htmlspecialchars($_GET['picdb']);

	
	$db = new db_class;
	$db->mysql_con($dbhost, $dbuser, $dbpassword, $dbname, $connectmode);
	$db->select_db($dbname);
	include './install/importdb.inc.php';
	if($picdb){
	$sql = readfromfile($sqlpicdb);
	} else {
	$sql = readfromfile($sqlfile);
	}
	runquery($sql);
	$backupdir = substr(md5($_SERVER['SERVER_ADDR'].$_SERVER['HTTP_USER_AGENT'].substr($time, 0, 4)), 8, 6);
	@mkdir('cache/backup_'.$backupdir, 0777);

	$authkey = substr(md5($_SERVER['SERVER_ADDR'].$_SERVER['HTTP_USER_AGENT'].$dbhost.$dbuser.$dbpw.$dbname.$username.$password.$pconnect.substr($time, 0, 6)), 8, 6).random(10);

	$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
	$siteuniqueid = $chars[date('y')%60].$chars[date('n')].$chars[date('j')].$chars[date('G')].$chars[date('i')].$chars[date('s')].substr(md5($onlineip.$time), 0, 4).random(6);

	$db->query("REPLACE INTO {$tablepre}settings (variable, value) VALUES ('authkey', '$authkey')");
	$db->query("REPLACE INTO {$tablepre}settings (variable, value) VALUES ('siteuniqueid', '$siteuniqueid')");

	$db->query("REPLACE INTO {$tablepre}settings (variable, value) VALUES ('backupdir', '".$backupdir."')");
	$db->query("REPLACE INTO {$tablepre}settings (variable, value) VALUES ('attachdir', '$attachdir')");
	$db->query("REPLACE INTO {$tablepre}settings (variable, value) VALUES ('attachurl', '$attachurl')");
	$db->query("DELETE FROM {$tablepre}members");
	$db->query("INSERT INTO {$tablepre}members (mid, username, password, adminid, groupid, gender, regtime, email, pageviews, invisible, lastvisit, lastactivity) VALUES ('1', '$username', '$password', '1', '1', '0', '$time', '$email', '0', '0', '$time', '$time');");

	@touch(MODEPATH.$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="flash.php?aid="></iframe>';
}else{
	include './install/main.inc.php';
}
function_exists('foot') && foot();
?>