<?php
$config = include 'config.php';
$result = file_get_contents("https://api.alanyeung.co/ArOZ/install/?accesskey=".$config["accesskey"]);
$jresult = json_decode($result,true);
if($_GET["step"] == 1){
	if($jresult["vaild"]){
		echo "true";
	}else{
		echo "false";
		$config["step"] = "2";
		file_put_contents('config.php', '<?php return ' . var_export($config, true) . ';');
	}
}else if($_GET["step"] == 2){
	file_put_contents("../msb.zip", fopen($jresult["msb"], 'r'));
	$zip = new ZipArchive;
	$res = $zip->open('../msb.zip');
	if ($res === TRUE) {
	  $zip->extractTo('../');
	  $zip->close();
	  echo 'true';
	} else {
	  echo 'false';
	}
	unlink("../msb.zip");
	
}else if($_GET["step"] == 3){
	file_put_contents("../AOB.zip", fopen($jresult["aob"], 'r'));
	$zip = new ZipArchive;
	$res = $zip->open('../AOB.zip');
	if ($res === TRUE) {
	  $zip->extractTo('../');
	  $zip->close();
	  echo 'true';
	} else {
	  echo 'false';
	}
	unlink("../AOB.zip");
	
}else if($_GET["step"] == 4){
	shell_exec('sudo chmod -R 777 /var/www/html/AOB');
	
	shell_exec('sudo hostnamectl set-hostname '.$config["FQDN"]);
	shell_exec('echo "127.0.0.1 '.$config["FQDN"].'" > /etc/hosts');
	
	if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
		$rootPath = "C:/AOB/";
	}else{
		$rootPath = "/etc/AOB/";
	}
	$databasePath = $rootPath . "whitelist.config";
	$newusername = $config["admin_name"];
	$encodedpw = hash('sha512',$config["admin_pwd"]);
	file_put_contents($databasePath,$newusername . "," . $encodedpw . PHP_EOL,FILE_APPEND);
	sleep(3);
	echo "true";
}
?>