<?php
/**
 * XNova Legacies
 *
 * @license http://www.xnova-ng.org/license-legacies
 * @see http://www.xnova-ng.org/
 *
 * Copyright (c) 2009-Present, XNova Support Team
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *  - Neither the name of the team or any contributor may be used to endorse or
 * promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 *
 *                                --> NOTICE <--
 *  This file is part of the core development branch, changing its contents will
 * make you unable to use the automatic updates manager. Please refer to the
 * documentation for further information about customizing XNova.
 *
 */

define('INSIDE' , true);
define('INSTALL' , false);
require_once dirname(__FILE__) .'/common.php';
include(ROOT_PATH . 'includes/databaseinfos.'.$phpEx);
include(ROOT_PATH . 'includes/migrateinfo.'.$phpEx);


$Mode     = $_GET['mode'];
$Page     = $_GET['page'];
$phpself  = $_SERVER['PHP_SELF'];
$nextpage = $Page + 1;

	if (empty($Mode)) { $Mode = 'intro'; }
	if (empty($Page)) { $Page = 1;       }

	$MainTPL = gettemplate('install/ins_body');
	includeLang('install/install');
	switch ($Mode) {
		case 'intro':
				$SubTPL = gettemplate ('install/ins_intro');
				$bloc   = $lang;
				$frame  = parsetemplate ( $SubTPL, $bloc );
		 	break;
		case 'ins':
			if ($Page == 1) {
				if ($_GET['error'] == 1) {
				adminMessage ($lang['ins_error1'], $lang['ins_error']);
				}
				elseif ($_GET['error'] == 2) {
				adminMessage ($lang['ins_error2'], $lang['ins_error']);
				}

				$SubTPL = gettemplate ('install/ins_form');
				$bloc   = $lang;
				$frame  = parsetemplate ( $SubTPL, $bloc );
			}
			elseif ($Page == 2) {
				$host   = $_POST['host'];
				$user   = $_POST['user'];
				$pass   = $_POST['passwort'];
				$prefix = $_POST['prefix'];
				$db     = $_POST['db'];

				$connection = @mysql_connect($host, $user, $pass);
					if (!$connection) {
					header("Location: ?mode=ins&page=1&error=1");
					exit();
					}

				$dbselect = @mysql_select_db($db);
					if (!$dbselect) {
					header("Location: ?mode=ins&page=1&error=1");
					exit();
					}

				$numcookie = mt_rand(1000, 1234567890);
				$dz = fopen("../config.php", "w");
					if (!$dz) {
					header("Location: ?mode=ins&page=1&error=2");
					exit();
					}

				fwrite($dz, "<?php\n");
				fwrite($dz, "if(!defined(\"INSIDE\")){ die(\"attemp hacking\"); }\n");
				fwrite($dz, "\$dbsettings = Array(\n");
				fwrite($dz, "\"server\"     => \"".$host."\", // MySQL server name.\n");
				fwrite($dz, "\"user\"       => \"".$user."\", // MySQL username.\n");
				fwrite($dz, "\"pass\"       => \"".$pass."\", // MySQL password.\n");
				fwrite($dz, "\"name\"       => \"".$db."\", // MySQL database name.\n");
				fwrite($dz, "\"prefix\"     => \"".$prefix."\", // Tables prefix.\n");
				fwrite($dz, "\"secretword\" => \"XNova".$numcookie."\"); // Cookies.\n");
				fwrite($dz, "?>");
				fclose($dz);

				function doquery ($InQry, $TblName) {
					global $prefix;
					$Table  = $prefix.$TblName;
					$DoQry  = str_replace("{{table}}", $Table, $InQry);
					$return = mysql_query($DoQry) or die("MySQL Error: <b>".mysql_error()."</b>");
				return $return;
				}

				doquery ( $QryTableAks        , 'aks'        );
				doquery ( $QryTableAnnonce    , 'annonce'    );
				doquery ( $QryTableAlliance   , 'alliance'   );
				doquery ( $QryTableBanned     , 'banned'     );
				doquery ( $QryTableBuddy      , 'buddy'      );
				doquery ( $QryTableChat       , 'chat'       );
				doquery ( $QryTableConfig     , 'config'     );
				doquery ( $QryInsertConfig    , 'config'     );
				doquery ( $QryTabledeclared        , 'declared'        );
				doquery ( $QryTableErrors     , 'errors'     );
				doquery ( $QryTableFleets     , 'fleets'     );
				doquery ( $QryTableGalaxy     , 'galaxy'     );
				doquery ( $QryTableIraks      , 'iraks'      );
				doquery ( $QryTableLunas      , 'lunas'      );
				doquery ( $QryTableMessages   , 'messages'   );
				doquery ( $QryTableNotes      , 'notes'      );
				doquery ( $QryTablePlanets    , 'planets'    );
				doquery ( $QryTableRw         , 'rw'         );
				doquery ( $QryTableStatPoints , 'statpoints' );
				doquery ( $QryTableUsers      , 'users'      );
				doquery ( $QryTableMulti      , 'multi'      );

				$SubTPL = gettemplate ('install/ins_form_done');
				$bloc   = $lang;
				$frame  = parsetemplate ( $SubTPL, $bloc );
			}
			elseif ($Page == 3) {
				if ($_GET['error'] == 3) {
				adminMessage ($lang['ins_error3'], $lang['ins_error']);
				}

				$SubTPL = gettemplate ('install/ins_acc');
				$bloc   = $lang;
				$frame  = parsetemplate ( $SubTPL, $bloc );
			}
			elseif ($Page == 4) {
				$adm_user   = $_POST['adm_user'];
				$adm_pass   = $_POST['adm_pass'];
				$adm_email  = $_POST['adm_email'];
				$adm_planet = $_POST['adm_planet'];
				$adm_sex    = $_POST['adm_sex'];
				$md5pass    = md5($adm_pass);

				if (!$_POST['adm_user']) {
					header("Location: ?mode=ins&page=3&error=3");
					exit();
				}
				if (!$_POST['adm_pass']) {
					header("Location: ?mode=ins&page=3&error=3");
					exit();
				}
				if (!$_POST['adm_email']) {
					header("Location: ?mode=ins&page=3&error=3");
					exit();
				}
				if (!$_POST['adm_planet']) {
					header("Location: ?mode=ins&page=3&error=3");
					exit();
				}

				include($xnova_root_path.'config.php');
				$db_host   = $dbsettings['server'];
				$db_user   = $dbsettings['user'];
				$db_pass   = $dbsettings['pass'];
				$db_prefix = $dbsettings['prefix'];
				$db_db     = $dbsettings['name'];

				$connection = @mysql_connect($db_host, $db_user, $db_pass);
					if (!$connection) {
					header("Location: ?mode=ins&page=1&error=1");
					exit();
					}

				$dbselect = @mysql_select_db($db_db);
					if (!$dbselect) {
					header("Location: ?mode=ins&page=1&error=1");
					exit();
					}

				function doquery ($InQry, $TblName) {
					global $db_prefix;
					$Table  = $db_prefix.$TblName;
					$DoQry  = str_replace("{{table}}", $Table, $InQry);
					$return = mysql_query($DoQry) or die("MySQL Error: <b>".mysql_error()."</b>");
				return $return;
				}

				$QryInsertAdm  = "INSERT INTO {{table}} SET ";
				$QryInsertAdm .= "`id`                = '1', ";
				$QryInsertAdm .= "`username`          = '". $adm_user ."', ";
				$QryInsertAdm .= "`email`             = '". $adm_email ."', ";
				$QryInsertAdm .= "`email_2`           = '". $adm_email ."', ";
				$QryInsertAdm .= "`authlevel`         = '3', ";
				$QryInsertAdm .= "`sex`               = '". $adm_sex ."', ";
				$QryInsertAdm .= "`id_planet`         = '1', ";
				$QryInsertAdm .= "`galaxy`            = '1', ";
				$QryInsertAdm .= "`system`            = '1', ";
				$QryInsertAdm .= "`planet`            = '1', ";
				$QryInsertAdm .= "`current_planet`    = '1', ";
				$QryInsertAdm .= "`register_time`     = '". time() ."', ";
				$QryInsertAdm .= "`password`          = '". $md5pass ."';";
				doquery($QryInsertAdm, 'users');

				$QryAddAdmPlt  = "INSERT INTO {{table}} SET ";
				$QryAddAdmPlt .= "`name`              = '". $adm_planet ."', ";
				$QryAddAdmPlt .= "`id_owner`          = '1', ";
				$QryAddAdmPlt .= "`galaxy`            = '1', ";
				$QryAddAdmPlt .= "`system`            = '1', ";
				$QryAddAdmPlt .= "`planet`            = '1', ";
				$QryAddAdmPlt .= "`last_update`       = '". time() ."', ";
				$QryAddAdmPlt .= "`planet_type`       = '1', ";
				$QryAddAdmPlt .= "`image`             = 'normaltempplanet02', ";
				$QryAddAdmPlt .= "`diameter`          = '12750', ";
				$QryAddAdmPlt .= "`field_max`         = '163', ";
				$QryAddAdmPlt .= "`temp_min`          = '47', ";
				$QryAddAdmPlt .= "`temp_max`          = '87', ";
				$QryAddAdmPlt .= "`metal`             = '500', ";
				$QryAddAdmPlt .= "`metal_perhour`     = '0', ";
				$QryAddAdmPlt .= "`metal_max`         = '1000000', ";
				$QryAddAdmPlt .= "`crystal`           = '500', ";
				$QryAddAdmPlt .= "`crystal_perhour`   = '0', ";
				$QryAddAdmPlt .= "`crystal_max`       = '1000000', ";
				$QryAddAdmPlt .= "`deuterium`         = '500', ";
				$QryAddAdmPlt .= "`deuterium_perhour` = '0', ";
				$QryAddAdmPlt .= "`deuterium_max`     = '1000000';";
				doquery($QryAddAdmPlt, 'planets');

				$QryAddAdmGlx  = "INSERT INTO {{table}} SET ";
				$QryAddAdmGlx .= "`galaxy`            = '1', ";
				$QryAddAdmGlx .= "`system`            = '1', ";
				$QryAddAdmGlx .= "`planet`            = '1', ";
				$QryAddAdmGlx .= "`id_planet`         = '1'; ";
				doquery($QryAddAdmGlx, 'galaxy');

				doquery("UPDATE {{table}} SET `config_value` = '1' WHERE `config_name` = 'LastSettedGalaxyPos';", 'config');
				doquery("UPDATE {{table}} SET `config_value` = '1' WHERE `config_name` = 'LastSettedSystemPos';", 'config');
				doquery("UPDATE {{table}} SET `config_value` = '1' WHERE `config_name` = 'LastSettedPlanetPos';", 'config');
				doquery("UPDATE {{table}} SET `config_value` = `config_value` + '1' WHERE `config_name` = 'users_amount' LIMIT 1;", 'config');

				$SubTPL = gettemplate ('install/ins_acc_done');
				$bloc   = $lang;
				$frame  = parsetemplate ( $SubTPL, $bloc );
			}
			break;
		case 'goto':
			if ($Page == 1) {
				$SubTPL = gettemplate ('install/ins_goto_intro');
				$bloc   = $lang;
				$frame  = parsetemplate ( $SubTPL, $bloc );
			}
			elseif ($Page == 2) {
				if ($_GET['error'] == 1) {
				adminMessage ($lang['ins_error1'], $lang['ins_error']);
				}
				elseif ($_GET['error'] == 2) {
				adminMessage ($lang['ins_error2'], $lang['ins_error']);
				}

				$SubTPL = gettemplate ('install/ins_goto_form');
				$bloc   = $lang;
				$frame  = parsetemplate ( $SubTPL, $bloc );
			}
			elseif ($Page == 3) {
				$host   = $_POST['host'];
				$user   = $_POST['user'];
				$pass   = $_POST['passwort'];
				$prefix = $_POST['prefix'];
				$db     = $_POST['db'];

				$connection = @mysql_connect($host, $user, $pass);
					if (!$connection) {
					header("Location: ?mode=goto&page=2&error=1");
					exit();
					}

				$dbselect = @mysql_select_db($db);
					if (!$dbselect) {
					header("Location: ?mode=goto&page=2&error=1");
					exit();
					}

				$numcookie = mt_rand(1000, 1234567890);
				$dz = fopen("../config.php", "w");
					if (!$dz) {
					header("Location: ?mode=ins&page=1&error=2");
					exit();
					}

				fwrite($dz, "<?php\n");
				fwrite($dz, "if(!defined(\"INSIDE\")){ die(\"attemp hacking\"); }\n");
				fwrite($dz, "\$dbsettings = Array(\n");
				fwrite($dz, "\"server\"     => \"".$host."\", // MySQL server name.\n");
				fwrite($dz, "\"user\"       => \"".$user."\", // MySQL username.\n");
				fwrite($dz, "\"pass\"       => \"".$pass."\", // MySQL password.\n");
				fwrite($dz, "\"name\"       => \"".$db."\", // MySQL database name.\n");
				fwrite($dz, "\"prefix\"     => \"".$prefix."\", // Tables prefix.\n");
				fwrite($dz, "\"secretword\" => \"XNova".$numcookie."\"); // Cookies.\n");
				fwrite($dz, "?>");
				fclose($dz);

				function doquery($query, $p) {
					$query = str_replace("{{prefix}}", $p, $query);
					$return = mysql_query($query) or die("MySQL Error: <b>".mysql_error()."</b>");
				return $return;
				}
				foreach ($QryMigrate as $query) {
					doquery($query, $prefix);
				}

				$SubTPL = gettemplate ('install/ins_goto_done');
				$bloc   = $lang;
				$frame  = parsetemplate ( $SubTPL, $bloc );
			}
		 	break;
		case 'upg':
		 	break;
		case 'bye':
				header("Location: ../");
		 	break;
		default:
	}

	$parse                 = $lang;
	$parse['ins_state']    = $Page;
	$parse['ins_page']     = $frame;
	$parse['dis_ins_btn']  = "?mode=$Mode&page=$nextpage";
	$Displ                 = parsetemplate ($MainTPL, $parse);

	display ($Displ, "Installeur", false, '', true);

?>