<?php
	if(isset($_POST["action"]) && !empty($_POST["action"])) {
		$action = htmlentities($_POST["action"]);
		switch($action) {
			case 'Software' :
				uploadSoftware();
				break;
			case 'Configuration' :
				echo uploadConfiguration();
				break;
			case 'Simulation' :
				echo uploadSimulation();
				break;
			case 'Splash' :
				echo uploadSplash();
				break;
			case 'GetFiles':
				echo getFiles();
				break;
		}
	}

	function uploadSoftware() {
		
		$target_dir = "uploads/";
		$file_name = urlencode(basename($_FILES["fileToUpload"]["name"]));
		$target_file = $target_dir . $file_name;
		
		$bfd_regex = '/BFD_(.+)_(.+)\.zip$/';
		$qbd_regex = '/QBD_(.+)_(.+)\.zip$/';
		$board_regex = '/^BD_(.+)_(.+)\.zip$/';
		$display_regex = '/DS_(.+)_(.+)\.zip$/';

		if (preg_match($board_regex, $file_name)) {
			delete_files($target_dir, 'BD_*.zip');
		} else if (preg_match($display_regex, $file_name)) {
			delete_files($target_dir, 'DS_*.zip');
		} else if (preg_match($bfd_regex, $file_name)) {
			delete_files($target_dir, 'BFD_*.zip');
		} else if (preg_match($qbd_regex, $file_name)) {
			delete_files($target_dir, 'QBD_*.zip');
		}
		
		move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
		
		die();
	}
	
	function uploadConfiguration() {

		if (isset($_FILES["fileToUpload"])) {
			if ($_FILES["fileToUpload"]["error"] == 0) {

				$target_dir = "logs/database/";
				$target_file = $target_dir . urlencode(strtolower(basename($_FILES["fileToUpload"]["name"])));
				
				if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
					echo "File uploaded successfully.";
				} else {
					echo "File upload failed\n";
					echo "</p>";
					echo '<pre>';
					echo 'Here is some more debugging info:';
					print_r($_FILES);
					print "</pre>";
				}
			} else {
				echo "Error between client and server. Error " . $_FILES["fileToUpload"]["error"];
			}
		} else {
			echo "Upload file doesn't exist.";
		}
		die();
	}
	
	function uploadSimulation() {
		$target_dir = "logs/DataLogs/";
		$target_file = $target_dir . urlencode(basename($_FILES["fileToUpload"]["name"]));
		
		if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
			echo "File uploaded successfully.";
		} else {
			echo "File upload failed\n";
			echo "</p>";
			echo '<pre>';
			echo 'Here is some more debugging info:';
			print_r($_FILES);
			print "</pre>";
		}
	
		die();
	}

	function uploadSplash() {
		$target_dir = "uploads/";	
		$ext = strtolower(pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION));	
		$target_file = $target_dir . "splash_" . $_FILES["fileToUpload"]["name"];

		if ($ext == "png" || $ext == "jpg" || $ext == "jpeg" || $ext == "xbm") {
			delete_files($target_dir, 'splash_*');
			move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
		}
		die();
	}
	
	function getFiles() {
		$target_dir = "uploads/";
		
		return json_encode(scandir($target_dir));
	}
	
	function delete_files($target, $extension) {
		if ( is_dir($target) ) {
			$files = glob( $target . $extension, GLOB_MARK );
			
			foreach ( $files as $file )
			{
				delete_files( $file , '');
			}
		} elseif ( is_file($target) ) {
			unlink( $target );
		}
	}
?>