<?php
header('Access-Control-Allow-Origin: *');
include_once("checkToken.php");
//Create the storage directory if not exists
$storageDir = trim(file_get_contents("storageDirectory.config"));
if (!file_exists($storageDir)){
	mkdir($storageDir,0777,true);
}

//Check if the GET variable for username and password is valid
/*
if (isset($_GET['username']) && $_GET['username'] != "" && isset($_GET['usrpw']) && $_GET['usrpw'] != ""){
	$username = $_GET['username'];
	$pw = hash('sha512', $_GET['usrpw']);
	$pw = trim(strtoupper($pw));
}else{
	die("ERROR. Unable to perform auth process.");
}
*/
//Check where is the config file located without auth.php
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
	$sysConfigDir = "C:/AOB/";
}else{
	$sysConfigDir = "/etc/AOB/";
}
if (file_exists("../../../root.inf") && filesize("../../../root.inf") > 0){
	//This user uses a specially defined path for directory
	$sysConfigDir = file_get_contents("../../../root.inf");
}

//Handle authentication
/*
$authFile = $sysConfigDir . "whitelist.config";
$db = file_get_contents($authFile);
$db = trim($db);
$db = explode("\n",$db);
$valudUser = false;
foreach ($db as $item){
	$item = trim($item);
	$data = explode(",",$item);
	if ($data[0] == $username && $data[1] == $pw){
		$valudUser = true;
	}
}

if (!$valudUser){
	die("ERROR. Incorrect username or password." . $pw);
}
*/
//Upload handling code
$target_dir = $storageDir;
$target_file = $target_dir . basename($_FILES["file"]["name"]);
if (strpos($target_file,".") !== false){
	die("ERROR. You cannot upload non-file chunks here");
}
// Check if image file is a actual image or fake image
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
	echo "DONE";
} else {
	echo "ERROR. Unable to move uploaded files. Is your hard drive fulled?";
}
?>
