<?php

$db_success = false;
$config_success = false;
$error = "";

$acp_host = "";
$acp_user = "";
$acp_pass = "";
$acp_db = "";
$l2j_ls_host = "";
$l2j_ls_user = "";
$l2j_ls_pass = "";
$l2j_ls_db = "";
$l2j_gs_host = "";
$l2j_gs_user = "";
$l2j_gs_pass = "";
$l2j_gs_db = "";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
	$acp_host = isset($_POST['acp_mysql_host']) ? $_POST['acp_mysql_host'] : '';
	$acp_user = isset($_POST['acp_mysql_user']) ? $_POST['acp_mysql_user'] : '';
	$acp_pass = isset($_POST['acp_mysql_pass']) ? $_POST['acp_mysql_pass'] : '';
	$acp_db = isset($_POST['acp_mysql_db']) ? $_POST['acp_mysql_db'] : '';
	
	if ($acp_host == '' || $acp_user == '' || $acp_db == '') {
		$error .= "Invalid ACP MySQL Information.<br>";
	}
	
	$l2j_ls_host = isset($_POST['l2j_ls_mysql_host']) ? $_POST['l2j_ls_mysql_host'] : '';
	$l2j_ls_user = isset($_POST['l2j_ls_mysql_user']) ? $_POST['l2j_ls_mysql_user'] : '';
	$l2j_ls_pass = isset($_POST['l2j_ls_mysql_pass']) ? $_POST['l2j_ls_mysql_pass'] : '';
	$l2j_ls_db = isset($_POST['l2j_ls_mysql_db']) ? $_POST['l2j_ls_mysql_db'] : '';
	
	if ($l2j_ls_host == '' || $l2j_ls_user == '' || $l2j_ls_db == '') {
		$error .= "Invalid L2J LoginServer MySQL Information.<br>";
	}
	
	
	$l2j_gs_host = isset($_POST['l2j_gs_mysql_host']) ? $_POST['l2j_gs_mysql_host'] : '';
	$l2j_gs_user = isset($_POST['l2j_gs_mysql_user']) ? $_POST['l2j_gs_mysql_user'] : '';
	$l2j_gs_pass = isset($_POST['l2j_gs_mysql_pass']) ? $_POST['l2j_gs_mysql_pass'] : '';
	$l2j_gs_db = isset($_POST['l2j_gs_mysql_db']) ? $_POST['l2j_gs_mysql_db'] : '';
	
	if ($l2j_gs_host == '' || $l2j_gs_user == '' || $l2j_gs_db == '') {
		$error .= "Invalid L2J GameServer MySQL Information.<br>";
	}
	
	if ($error == "") {
		try {
			$acp_sql = new PDO("mysql:host={$acp_host};dbname={$acp_db}", $acp_user, $acp_pass);
			$acp_sql->query(file_get_contents("install.sql"));
		} catch (PDOException $ex) {
			$error .= "Error installing to ACP MySQL: " . $ex->getMessage();
		}
	}
	
	if ($error == "") {
		try {
			$l2j_ls_sql = new PDO("mysql:host={$l2j_ls_host};dbname={$l2j_ls_db}", $l2j_ls_user, $l2j_ls_pass);
			$l2j_ls_sql->query(file_get_contents("l2jdb_install.sql"));
		} catch (PDOException $ex) {
			$error .= "Error installing to L2J LoginServer MySQL: " . $ex->getMessage();
		}
	}
	
	if ($error == "") {
		try {
			$l2j_gs_sql = new PDO("mysql:host={$l2j_gs_host};dbname={$l2j_gs_db}", $l2j_gs_user, $l2j_gs_pass);
		} catch (PDOException $ex) {
			$error .= "Error connecting to L2J GameServer MySQL: " . $ex->getMessage();
		}
	}
	
	if ($error == "") {
		$db_success = true;
		$config = file_get_contents("config.php");
		$server_config = file_get_contents("serverconfig.php");
		
		if (stristr($config, "%%acp_host%%") === FALSE || stristr($server_config, "%%l2j_ls_mysql_host%%") === FALSE) {
			$error = "Database settings in Config.php or ServerConfig.php could not be configured, please configure manually.";
		} else {
			$config_needle = array(
				'%%acp_host%%',
				'%%acp_user%%',
				'%%acp_pass%%',
				'%%acp_db%%',
			);
			$config_replace = array(
				$acp_host,
				$acp_user,
				$acp_pass,
				$acp_db,
			);
			$config = str_replace($config_needle, $config_replace, $config);
			
			$server_config_needle = array(
				'%%l2j_ls_mysql_host%%',
				'%%l2j_ls_mysql_user%%',
				'%%l2j_ls_mysql_pass%%',
				'%%l2j_ls_mysql_db%%',
				'%%l2j_gs_mysql_host%%',
				'%%l2j_gs_mysql_user%%',
				'%%l2j_gs_mysql_pass%%',
				'%%l2j_gs_mysql_db%%',
			);
			$server_config_replace = array(
				$l2j_ls_host,
				$l2j_ls_user,
				$l2j_ls_pass,
				$l2j_ls_db,
				$l2j_gs_host,
				$l2j_gs_user,
				$l2j_gs_pass,
				$l2j_gs_db,
			);
			$server_config = str_replace($server_config_needle, $server_config_replace, $server_config);
			
			if (file_put_contents("config.php", $config) === FALSE) {
				$error = "Could not write to config.php, please configure manually.";
			} else if (file_put_contents("serverconfig.php", $server_config) === FALSE) {
				$error = "Could not write to serverconfig.php, please configure manually.";
			} else {
				$config_success = true;
			}
		}
	}
}

?>
<html>

<head>
    <title>ACP Installer</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
</head>

<body class="bg-light">
    <div class="container">
        <br><br>
        <div class="row">
            <h3 class="mx-auto">ACP Installer</h3>
        </div>
        <br>
	<?php if ($db_success): ?>
        <div class="alert alert-success">
	  <strong>Success!</strong><br> ACP Database installation is complete.
	</div>
	<?php endif; ?>
	<?php if ($config_success): ?>
        <div class="alert alert-success">
	  <strong>Success!</strong><br> ACP Database configuration settings have been saved.
	</div>
	<?php endif; ?>
	<?php if ($error != ""): ?>
        <div class="alert alert-danger">
	  <strong>Error!</strong><br> <?php echo $error; ?>
	</div>
	<?php endif; ?>
        <form method="POST">
            <div class="row">
                <div class="card mx-auto" style="width: 550px">
                    <div class="card-body">
                        <h4 class="card-title text-center">ACP MySQL Information</h4>
                        <br>
                        <div class="form-group row">
                            <label for="acp_mysql_host" class="col-sm-3 col-form-label">Host</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="acp_mysql_host" name="acp_mysql_host" value="<?php echo $acp_host; ?>" placeholder="MySQL Host Address">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="acp_mysql_user" class="col-sm-3 col-form-label">User</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="acp_mysql_user" name="acp_mysql_user" value="<?php echo $acp_user; ?>" placeholder="MySQL User">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="acp_mysql_pass" class="col-sm-3 col-form-label">Password</label>
                            <div class="col-sm-9">
                                <input type="password" class="form-control" id="acp_mysql_pass" name="acp_mysql_pass" value="<?php echo $acp_pass; ?>" placeholder="MySQL Password">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="acp_mysql_db" class="col-sm-3 col-form-label">Database</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="acp_mysql_db" name="acp_mysql_db" value="<?php echo $acp_db; ?>" placeholder="MySQL Database Name">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="card mx-auto" style="width: 550px">
                    <div class="card-body">
                        <h4 class="card-title text-center">L2J LoginServer MySQL Information</h4>
                        <br>
                        <div class="form-group row">
                            <label for="l2j_ls_mysql_host" class="col-sm-3 col-form-label">Host</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="l2j_ls_mysql_host" name="l2j_ls_mysql_host" value="<?php echo $l2j_ls_host; ?>" placeholder="MySQL Host Address">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="l2j_ls_mysql_user" class="col-sm-3 col-form-label">User</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="l2j_ls_mysql_user" name="l2j_ls_mysql_user" value="<?php echo $l2j_ls_user; ?>" placeholder="MySQL User">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="l2j_ls_mysql_pass" class="col-sm-3 col-form-label">Password</label>
                            <div class="col-sm-9">
                                <input type="password" class="form-control" id="l2j_ls_mysql_pass" name="l2j_ls_mysql_pass" value="<?php echo $l2j_ls_pass; ?>" placeholder="MySQL Password">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="l2j_ls_mysql_db" class="col-sm-3 col-form-label">Database</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="l2j_ls_mysql_db" name="l2j_ls_mysql_db" value="<?php echo $l2j_ls_db; ?>" placeholder="MySQL Database Name">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="card mx-auto" style="width: 550px">
                    <div class="card-body">
                        <h4 class="card-title text-center">L2J GameServer MySQL Information</h4>
                        <br>
                        <div class="form-group row">
                            <label for="l2j_gs_mysql_host" class="col-sm-3 col-form-label">Host</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="l2j_gs_mysql_host" name="l2j_gs_mysql_host" value="<?php echo $l2j_gs_host; ?>" placeholder="MySQL Host Address">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="l2j_gs_mysql_user" class="col-sm-3 col-form-label">User</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="l2j_gs_mysql_user" name="l2j_gs_mysql_user" value="<?php echo $l2j_gs_user; ?>" placeholder="MySQL User">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="l2j_gs_mysql_pass" class="col-sm-3 col-form-label">Password</label>
                            <div class="col-sm-9">
                                <input type="password" class="form-control" id="l2j_gs_mysql_pass" name="l2j_gs_mysql_pass" value="<?php echo $l2j_gs_pass; ?>" placeholder="MySQL Password">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="l2j_gs_mysql_db" class="col-sm-3 col-form-label">Database</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="l2j_gs_mysql_db" name="l2j_gs_mysql_db" value="<?php echo $l2j_gs_db; ?>" placeholder="MySQL Database Name">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="card mx-auto" style="width: 550px">
                    <div class="card-body text-center">
                        <button type="submit" class="btn btn-primary mb-2">Submit</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</body>

</html>