<?php
include($O__libPath . "/functions/server/O__tableMaker.php");
include($O__libPath . "/functions/server/O__backupData.php");

// Load settings
$O__fileData = O__XMLdata($O__DBpath . "/pageTypes/" . $O__PT . "/configs/settings.xml", 0, 0);
$O__uploadMax = O__XMLgetData($O__fileData,"/settings/setting[@id='uploadMax']/value");
unset($O__fileData);

// Setup
$O__PTATmsg = "";
$O__directory = $O__filePaths[$O__URLpath[1]][0];
$O__path = $O__PTATinputs["path"];

function O__operation(){
	global $O__DBpath, $O__PTATmsg, $O__directory, $O__path, $O__logNotes;
	
	$O__operation = $_POST["O__operation"];	
	if(isset($_POST["O__target"])){$O__target = $_POST["O__target"];}
	if(isset($_POST["O__source"])){$O__source = $_POST["O__source"];}
	
	switch($O__operation){
		case "createDir":
			mkdir($O__directory . $O__path . "/" . $O__source ,0777);
			$O__sysMsgHeadline = "Directory Create";
			$O__sysMsgBody = "Success.  '" . $O__source . "' directory has been created.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;	
			break;
			
		case "createFile":
			O__dataWrite($O__directory . $O__path . "/" . $O__source, "Nothing here.");
			$O__sysMsgHeadline = "File Create";
			$O__sysMsgBody = "Success.  '" . $O__source . "' file has been created.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			break;
			
		case "deleteDir":
			$O__targetPath = $O__directory . $O__path . "/" . $O__target;
			if(is_dir($O__targetPath)){		
				$O__dirCountCheck = scandir($O__targetPath);
				$O__dirCount = count($O__dirCountCheck);
				if ($O__dirCount <= 2) {
					rmdir($O__targetPath); // remove directory if empty
					$O__sysMsgHeadline = "Directory Delete";
					$O__sysMsgBody = "Success.  '" . $O__source . "' directory has been deleted.";
					$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
					$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
				}		
			}
			break;
		case "deleteFile":
			$O__targetPath = $O__directory . $O__path . "/" . $O__target;
			
			unlink($O__targetPath);
			
			$O__sysMsgHeadline = "File Delete";
			$O__sysMsgBody = "Success.  '" . $O__source . "' file has been deleted.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			break;
		case "rename":
			rename($O__directory . $O__path . "/" . $O__target, $O__directory . $O__path . "/" . $O__source);
			
			$O__sysMsgHeadline = "File Rename";
			$O__sysMsgBody = "Success.  '" . $O__source . "' file has been renamed.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			break;
		case "upload":
			$O__filename = basename($_FILES["O__file2Upload"]["name"]);
			$O__targetPath = $O__directory . $O__path . "/" . $O__filename;
			move_uploaded_file($_FILES["O__file2Upload"]["tmp_name"], $O__targetPath); // moves file from tmp dir to target path
			
			$O__sysMsgHeadline = "File Upload";
			$O__sysMsgBody = "Success.  '" . $O__filename . "' file has been uploaded.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			break;		
	}
}
	
function O__createOutput(){
	global $O__PT, $O__AT, $O__PTATmsg, $O__directory, $O__filePaths, $O__path, $O__uploadMax, $O__URLpath;

	$O__colLabels = array("Name","Type","Size","Last Modified","Actions");
	$O__data[0][0] = "";
	$O__data[0][1] = "";
	$O__data[0][2] = "";
	$O__data[0][3] = "";
	$O__data[0][4] = "";	

	$O__output = "<h2>File Manager</h2>";
	$O__output .= $O__PTATmsg;
	$O__output .= "<form name=\"O__fileAction\" action=\"\" method=\"post\">";
	$O__output .= "<input type=\"hidden\" name=\"O__target\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__source\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__operation\" value=\"\">";
	$O__output .= "</form>";
	
	$O__output .= "<b>Create:</b> ";
	$O__output .= "<a href=\"#\" onclick=\"javascript:O__operation('createFile');\">File</a> | ";
	$O__output .= "<a href=\"#\" onclick=\"javascript:O__operation('createDir');\">Directory</a> | ";
	$O__output .= "<b>Manage:</b> ";
	$O__output .= "<a href=\"" . $O__PT . "\">View Folder Groups</a>";
	$O__output .= "<br><br>";
	
	$O__output .= "<form name=\"O__browserUpload\" action=\"/" . $O__PT . "/" . $O__URLpath[1] . "/" . $O__AT . "?path=" . $O__path . "\" method=\"post\" enctype=\"multipart/form-data\">";
	$O__output .= "<b>Upload:</b> ";
	$O__output .= "Select file: ";
	$O__output .= "<input type=\"file\" name=\"O__file2Upload\" id=\"O__fileUpload\">";
	$O__output .= "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"" . $O__uploadMax . "\">";
	$O__output .= "<input type=\"hidden\" name=\"O__operation\" value=\"upload\">";
	$O__output .= "<input type=\"submit\" value=\"Upload\" name=\"submit\">";
	$O__output .= "</form>";
	$O__output .= "<br>";

	$O__filesArray = O__dirToArray($O__directory . $O__path);
	sort($O__filesArray);

	if (count($O__filesArray)==0){ // if no files
		$O__data[0][0] .= "No files here.";
		$O__data[0][1] .= "";
		$O__data[0][2] .= "";
		$O__data[0][3] .= "";
		$O__data[0][4] .= "";
	} else { // if there are files
		for($i=0;$i<count($O__filesArray);$i++){
			$O__file = $O__filesArray[$i][0];
				
			if($O__filesArray[$i][1] == "d"){ // if a directory
				$O__data[$i][0] = "<a href=\"/" . $O__PT . "/" . $O__URLpath[1] . "/" . $O__AT . "?path=" . $O__path . "/" . $O__file . "\"><b>" . $O__file . "</b></a>";
				$O__data[$i][1] = "&lt;Dir&gt;";
				$O__data[$i][2] = "";
				$O__data[$i][3] = date("F d, Y H:i:s", filemtime($O__directory . $O__path . "/" . $O__file));
				$O__data[$i][4] = "<a href=\"#\" onclick=\"javascript:O__operation('deleteDir','" . $O__file ."');\">Delete</a> | ";
				$O__data[$i][4] .= "<a href=\"#\" onclick=\"javascript:O__operation('rename','" . $O__file ."');\">Rename</a>";
			} else { // if content
				$O__data[$i][0] = $O__file;
				$O__data[$i][1] = "-File-";
				$O__data[$i][2] = substr((filesize($O__directory . $O__path . "/" . $O__file)/1024),0,4) . "kb";
				$O__data[$i][3] = date("F d, Y H:i:s", filemtime($O__directory . $O__path . "/" . $O__file));
				$O__data[$i][4] = "<a href=\"#\" onclick=\"javascript:O__operation('deleteFile','" . $O__file ."');\">Delete</a> | ";
				$O__data[$i][4] .= "<a href=\"#\" onclick=\"javascript:O__operation('rename','" . $O__file ."');\">Rename</a> | ";
				$O__data[$i][4] .= "<a href=\"/O__text/" . $O__URLpath[1] . "/edit?path=" . $O__path . "/" . $O__file . "\">Text Edit</a> | ";
				$O__data[$i][4] .= "<a href=\"#\" onclick=\"javascript:O__operation('download','" . $O__URLpath[1] . $O__path . "/" . $O__file ."');\">Download</a>";
			}
			
		}
	}
	
	// Add "up" link, if in subdirectory
	if ($O__path != ""){
		$O__pathPrev = substr($O__path,0,strrpos($O__path,"/"));
		$O__finalRow = count($O__data);
		$O__data[$O__finalRow][0] = "<a href=\"/" . $O__PT . "/" . $O__URLpath[1] . "/" . $O__AT . "?path=" . $O__pathPrev . "\"><b>&lt;&lt; Up</b></a>";
		$O__data[$O__finalRow][1] = "";
		$O__data[$O__finalRow][2] = "";
		$O__data[$O__finalRow][3] = "";
		$O__data[$O__finalRow][4] = "";
	}
	
	// Create the UI table
	$O__output .= "<b>Current location:</b> " . "/" . $O__filePaths[$O__URLpath[1]][1] . $O__path;
	$O__output .= O__tableMaker($O__colLabels,$O__data,"O__table","O__tablePageTypes",0);
		
	return $O__output;
}

if(isset($_POST["O__operation"])){ O__operation(); }
$O__output = O__createOutput();

?>

<script>
function O__operation(O__action, O__target){
	var O__form = document.forms["O__fileAction"];
	O__form.action = "/<?php echo($O__PT . "/" . $O__URLpath[1] . "/" . $O__AT . "?path=" . $O__path); ?>";
	O__form.O__target.value = O__target;
	
	switch (O__action){
		case "createDir":
			var O__input = prompt("Please enter a directory name.");
			if (O__input == ""){
				alert("Please enter a name.");
			} else if (O__input != null){				
				O__form.O__source.value = O__input;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
		case "createFile":
			var O__input = prompt("Please enter a filename.");
			if (O__input == ""){
				alert("Please enter a name.");
			} else if (O__input != null){
				O__form.O__source.value = O__input;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
		case "deleteDir":
			var O__input = confirm("Are you sure you want to delete the directory '" + O__target + "'?");
			if (O__input == 1){
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
		case "deleteFile":
			var O__input = confirm("Are you sure you want to delete the file '" + O__target + "'?");
			if (O__input == 1){
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
		case "rename":
			var O__input = prompt("The current name is '" + O__target + "'.  Please enter a new name.");
			if (O__input == ""){
				alert("Please enter a name."); 
			} else if (O__input != null){
				O__form.O__source.value = O__input;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
		case "download":
			O__form.O__operation.value = O__action;
			O__form.submit();
			break;
	}

}
</script>
