<?php
ob_start();
error_reporting(0);
?>
<!DOCTYPE html>
<html>
<head>
<title>Install</title>
	<style>
		@import url('css/reset.css');
		
		@import url('css/main.css');
	</style>
</head>
<br />

<div style="width: 70%;background: white;margin-left:auto;margin-right:auto;padding: 25px;border-radius:5px;">
<?php
$message = '';
if(isset($_POST['install'])){
	if(empty($_POST['db_host']) || empty($_POST['db_name']) || empty($_POST['db_user'])){
		$message = '<center><font color="red"><b>You did not fill the necesary spaces</b></font></center>';
	} else {
		$db_host = htmlentities($_POST['db_host']);
		$db_name = htmlentities($_POST['db_name']);
		$db_user = htmlentities($_POST['db_user']);
		$db_pass = htmlentities($_POST['db_pass']);
		$status_connection = 1;
		$connection = mysql_connect("$db_host", "$db_user", "$db_pass") or $status_connection = 2;
		mysql_select_db(''.$db_name.'', $connection) or $status_connection = 2;
		if($status_connection == 2){
			echo  '<center><font color="red"><b>Connection Failed, please retry</b></font></center>';
		} else {
$text = <<<PHP
<?php
\$database = array();
\$database['host']     = '$db_host';
\$database['user']     = '$db_user';
\$database['password'] = '$db_pass';
\$database['table']    = '$db_name';

\$connect_error = 'Sorry, there are some connection problems.';
mysql_connect(\$database['host'], \$database['user'], \$database['password']) or die(\$connect_error);
mysql_select_db(\$database['table']) or die(\$connect_error);

?>
PHP;
		$file = "core/database/connect.php";
		$command = fopen($file, w);
		fwrite($command, $text);
		fclose($command);
		
		mysql_query("
		CREATE TABLE IF NOT EXISTS `guests` (
		  `ip` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
		  `last_activity` varchar(65) COLLATE utf8_unicode_ci NOT NULL
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;", $connection);
		
		mysql_query("
		CREATE TABLE IF NOT EXISTS `comments` (
		  `id` int(11) NOT NULL AUTO_INCREMENT,
		  `server_id` int(11) NOT NULL,
		  `user_id` int(11) NOT NULL,
		  `comment` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
		  PRIMARY KEY (`id`)
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
		", $connection);
		
		mysql_query("
		CREATE TABLE IF NOT EXISTS `servers` (
		  `id` int(11) NOT NULL AUTO_INCREMENT,
		  `user_id` int(11) NOT NULL,
		  `category_id` VARCHAR(255) NOT NULL DEFAULT '[1]',
		  `ip` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
		  `vip` tinyint(1) NOT NULL DEFAULT '0',
		  `port` varchar(5) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
		  `banner` varchar(555) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
		  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
		  `country` varchar(3) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'XX',
		  `description` varchar(2500) COLLATE utf8_unicode_ci NOT NULL,
		  `website` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
		  `youtube_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
		  `votes` int(11) NOT NULL DEFAULT '0',
		  `disabled` tinyint(1) NOT NULL DEFAULT '0',
		  `status` tinyint(1) NOT NULL DEFAULT '0',
		  `votifier_key` varchar(2500) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'false',
		  `votifier_ip` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
		  `votifier_port` varchar(10) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'false',
		  `Players` int(11) NOT NULL,
		  `MaxPlayers` int(11) NOT NULL,
		  `serverVersion` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
		  `cache_time` text COLLATE utf8_unicode_ci NOT NULL,
		  `protocol` int(11) NOT NULL DEFAULT '1',
		  PRIMARY KEY (`id`)
		) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
		", $connection);
		
		mysql_query("
		CREATE TABLE IF NOT EXISTS `settings` (
		  `id` int(11) NOT NULL DEFAULT '1',
		  `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Comples Servers List',
		  `description` varchar(512) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'false',
		  `url` varchar(255) NOT NULL,
		  `news_message` varchar(555) NOT NULL,
		  `facebook` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'false',
		  `twitter` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'false',
		  `contact_email` varchar(1024) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
		  `pagination` int(11) NOT NULL DEFAULT '15',
		  `register` int(11) NOT NULL DEFAULT '1',
		  `server_cache` varchar(5) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '600',
		  `show_offline_servers` int(11) NOT NULL DEFAULT '0',
		  `disable_query` int(11) NOT NULL DEFAULT '0',
		  `max_categories` int(11) NOT NULL DEFAULT '4',
		  `email_confirmation` int(11) NOT NULL DEFAULT '1',
		  `server_confirmation` int(11) NOT NULL DEFAULT '1',
		  `recaptcha_public` varchar(128) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '6Le43tISAAAAADni-XsMzvEaStTluh6vSFmbhpfC',
		  `recaptcha_private` varchar(128) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '6Le43tISAAAAANP9dDZb-ConEQRFxdyTpNFo09Q3',
		  `advertise_top` varchar(2555) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'false',
		  `advertise_bottom` varchar(2555) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'false',
		  PRIMARY KEY (`id`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8;", $connection);
		
		mysql_query("
		CREATE TABLE IF NOT EXISTS `users` (
		  `user_id` int(11) NOT NULL AUTO_INCREMENT,
		  `username` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
		  `password` varchar(128) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
		  `email` varchar(1024) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
		  `email_code` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
		  `name` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
		  `avatar` varchar(65) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
		  `active` int(11) NOT NULL DEFAULT '0',
		  `type` int(1) NOT NULL DEFAULT '0',
		  `ip` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
		  `date` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
		  `last_activity` varchar(65) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
		  PRIMARY KEY (`user_id`)
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;", $connection);
		
		mysql_query("
		CREATE TABLE IF NOT EXISTS `votes` (
		  `id` int(11) NOT NULL AUTO_INCREMENT,
		  `ip` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
		  `server_id` int(11) NOT NULL,
		  `timestamp` int(11) NOT NULL,
		  PRIMARY KEY (`id`),
		  KEY `server_id` (`server_id`)
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;", $connection);
		
		mysql_query("CREATE TABLE IF NOT EXISTS `categories` (
		  `category_id` int(11) NOT NULL AUTO_INCREMENT,
		  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
		  PRIMARY KEY (`category_id`)
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ;", $connection);
		
		mysql_query("
		INSERT INTO `categories` (`category_id`, `name`) VALUES
		(1, 'Survival')", $connection);
		
		mysql_query("
		INSERT INTO `users` (`user_id`, `username`, `password`, `email`, `email_code`, `name`, `avatar`, `active`, `type`, `ip`, `date`, `last_activity`) VALUES
		(1, 'admin', 'f45cee75a2964847b436e004a6917fe8a2745b3c9968cbd57d71ba49df9b3de87b8409d05c15fb5a6e1329b0e8f138a15a9b88dc3941b63e4948c7c560863139', 'email', '', 'Admin', 'avatars/7963843a08.png', 1, 1, '0.0.0.0', 'beginning of time', '')", $connection);
		
		mysql_query("
		INSERT INTO `settings` (`id`, `description`, `title`, `url`, `news_message`, `twitter`, `facebook`, `contact_email`, `pagination`, `register`, `server_cache`, `show_offline_servers`, `disable_query`, `email_confirmation`, `server_confirmation`, `advertise_top`, `advertise_bottom`) VALUES
		(1, 'Complex Minecraft Servers List', '', 'http://mywebsite.com', '', 'false', 'false', 'ContactEmail', 15, 1, '600', 0, '1', 0, 0, 'false', 'false');

		", $connection);
		
		header('Location: install.php?success');
		}
	}		
}
if(isset($_GET['success']) && empty($_GET['success'])) {
	echo '<center>
		<font color=\'green\'>The mysql query and the database configuration is successfully updated!</font>
		<br />Please delete this file now(install.php), for security reasons!<br /><br />
		<br />Login with admin right with the next account<br />
		<b>Username:</b> admin<br />
		<b>Password:</b> admin
		</center>
	';
}

?>
			
	<center>
	<?php echo $message; ?>
		<form name="install" method="post" action="">
		<h3>Database Connection</h3>
			<p>
				<table>
					<tr>
						<td><label>Database Host</label></td>
						<td><input  name="db_host" type="text" size="30" value="localhost" /></td>
					</tr>
					<tr>
						<td><label>Database Name</label></td>
						<td><input  name="db_name" type="text" size="30" value="<?php echo $_POST['db_name']; ?>" /></td>
					</tr>
					<tr>
						<td><label>Database User</label></td>
						<td><input  name="db_user" type="text" size="30" value="<?php echo $_POST['db_user']; ?>" /></td>
					</tr>
					<tr>
						<td><label>Database Password</label></td>
						<td><input  name="db_pass" type="text" size="30" value="<?php echo $_POST['db_pass']; ?>" /></td>
					</tr>
					<tr>
						<td><br />
							<input  type="submit" name="install" value="Install" />		
						</td>
					</tr>
				</table>
			</p>
		</form>
	</center>
</div>
<?php
ob_flush();
?>