<?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__pathIndexers = $O__DBpath . "/pageTypes/" . $O__PT . O__XMLgetData($O__fileData,"/settings/setting[@id='storagePathIndexers']/value");
$O__pathMenus = $O__DBpath . "/pageTypes/" . $O__PT . O__XMLgetData($O__fileData,"/settings/setting[@id='storagePathMenus']/value");
unset($O__fileData);

$O__systemMsg = "";
$O__PTATmsg = "";

function O__operation(){
	global $O__DBpath, $O__systemMsg, $O__logNotes, $O__pathMenus, $O__pathIndexers, $O__PTATmsg;
	
	if(isset($_POST["O__target"])){$O__target = $_POST["O__target"];}
	if(isset($_POST["O__source"])){$O__source = $_POST["O__source"];}
		$O__operation = $_POST["O__operation"];
	
	switch($O__operation){
		case "create":
			
			$O__sysMsgHeadline = "Menu Create";
			if(!file_exists($O__pathMenus . "/" . $O__source . ".xml")){
				
				// Create XML
				$O__fileData = new DomDocument("1.0", "UTF-8");
				$O__menuTree = $O__fileData->createElement("menu", "");
				
				// Set up default item
				$O__menuItem = $O__fileData->createElement("item", "");
					$O__menuItem->appendChild($O__fileData->createElement("active", "1")); // add attributes to item
					$O__menuItem->appendChild($O__fileData->createElement("name", "Home Link"));
					$O__menuItem->appendChild($O__fileData->createElement("info", "Default link to main page"));
					$O__menuItem->appendChild($O__fileData->createElement("href", "/"));
					$O__menuItem->appendChild($O__fileData->createElement("path", " "));
					$O__menuItem->appendChild($O__fileData->createElement("bin", " "));
					$O__menuItem->appendChild($O__fileData->createElement("args", " "));
					$O__menuItem->appendChild($O__fileData->createElement("wait", " "));
					$O__menuItem->appendChild($O__fileData->createElement("img", "/files/images/OutSCIRT_600x600.jpg"));
				$O__menuTree->appendChild($O__menuItem); // add menu item to tree
				$O__fileData->appendChild($O__menuTree); // add tree to root file
				
				// Save XML file
				O__XMLdata($O__pathMenus . "/" . $O__source . ".xml", $O__fileData, 0);
				
				$O__sysMsgBody = "Success.  '" . $O__source . "' has been created.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			} else {
				$O__sysMsgBody = "Failed.  One with the same name already exists. '" . $O__source . "' has not been created.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgAlert");
			}
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;		
				
			break;
			
		case "delete":
			$O__sysMsgHeadline = "Menu Delete";
			if(file_exists($O__pathMenus . "/" . $O__target. ".xml")){
				O__backupData($O__pathMenus . "/" . $O__target . ".xml",1);
					if(file_exists($O__pathIndexers . "/" . $O__target. ".xml")){ // If there's an indexer
						O__backupData($O__pathIndexers . "/" . $O__target . ".xml",1);
					}
				$O__sysMsgBody = "Success.  '" . $O__target . "' has been deleted.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			} else {
				$O__sysMsgBody = "Failed.  Cannot find menu.  '" . $O__target . "' has NOT been deleted.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgAlert");
			}
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;

			break;
			
		case "rename":
			$O__sysMsgHeadline = "Menu Rename";
			if(file_exists($O__pathMenus . "/" . $O__source. ".xml")){
				$O__sysMsgBody = "Failed.  One with the same name exists.  '" . $O__target . "' has NOT been renamed.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgAlert");
			} else {
				rename($O__pathMenus . "/" . $O__target. ".xml", $O__pathMenus . "/" . $O__source . ".xml");
				if(file_exists($O__pathIndexers . "/" . $O__target. ".xml")){ // If there's an indexer
					rename($O__pathIndexers . "/" . $O__target. ".xml", $O__pathIndexers . "/" . $O__source . ".xml");
				}
				$O__sysMsgBody = "Success.  '" . $O__target . "' renamed to '" . $O__source . "'.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			}
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;	

			break;
			
		case "indexerCreate":
			$O__sysMsgHeadline = "Menu Indexer Create";
			if(!file_exists($O__pathIndexers . "/" . $O__target . ".xml")){
				
				// Create new XML
				$O__dataXML = new DomDocument("1.0", "UTF-8");
				$O__XMLroot = $O__dataXML->createElement("settings", "");
					
				// Define each setting
				$O__indexerSettingsArray = array(
					array("auto","1","1 = autoindex. 0 = reindex triggered manually with URL parameter \"reindex\"."),
					array("backup","1","1 = Automatically backup menu item when it gets reindexed."),
					array("sort","1","1 = sort result alphabetically, 2 = reverse sort."),
					array("pathFiles","/path/to/files","The folder with the files to be indexed."),
					array("pathImgs","/path/to/images","The folder with the thumbnail images.  Must be web-accessible."),
					array("pathImgsHref","/urlPath/to/images","The web page to the image."),
					array("imgDefault","/files/images/OutSCIRT_600x600.jpg","Default image webpath to use if image not available."),
					array("pathLauncher","/path/to/launcher/directory","The directory to the launcher binary.  Also known as the working directory."),
					array("pathLauncherBin","/path/to/launcher/directory/binary","The binary executable of the program launching the file."),
					array("launcherBinArgs"," -full","Arguments to include with launcher. Likely needs a space at the beginning."),
					array("wait","10","The time to show the launcher page for, in seconds.")
				);
				
				// Create each setting
				for($i=0; $i < count($O__indexerSettingsArray); $i++){
					
					$O__indexerSetting = $O__dataXML->createElement("setting","");
						$O__indexerSetting->setAttribute("id", $O__indexerSettingsArray[$i][0]);
						$O__XMLroot->appendChild($O__indexerSetting);
						
					$O__indexerSettingValue = $O__dataXML->createElement("value",$O__indexerSettingsArray[$i][1]);
						$O__indexerSetting->appendChild($O__indexerSettingValue);
						
					$O__indexerSettingNotes = $O__dataXML->createElement("desc",$O__indexerSettingsArray[$i][2]);
						$O__indexerSetting->appendChild($O__indexerSettingNotes);
				}
				
				// Wrap up and save XML
				$O__dataXML->appendChild($O__XMLroot);
				O__XMLdata($O__pathIndexers . "/" . $O__target . ".xml", $O__dataXML, 0);
				$O__sysMsgBody = "Success.  '" . $O__target . "' indexer has been created.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			} else {
				$O__sysMsgBody = "Failed.  Indexer already exists.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgAlert");
			}
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
				
			break;
			
		case "indexerDelete":
			$O__sysMsgHeadline = "Menu Indexer Delete";
			if(file_exists($O__pathIndexers . "/" . $O__target. ".xml")){
				O__backupData($O__pathIndexers . "/" . $O__target . ".xml",1);
				$O__sysMsgBody = "Success.  '" . $O__target . "' indexer has been deleted.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			} else {
				$O__sysMsgBody = "Failed.  Indexer does not exist.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgAlert");
			}
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			
			break;
	}
	
}

function O__createOutput(){
	global $O__DBpath, $O__PT, $O__pathMenus, $O__pathIndexers, $O__systemMsg, $O__PTATmsg;

	$O__output = "<h2>Kiosk Manager</h2>";
	$O__output .= $O__PTATmsg;
	$O__output .= "<form name=\"O__menusManage\" action=\"/" . $O__PT . "/default/manage\" 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 .= "<p></p><b>Create:</b> ";
	$O__output .= "<a href=\"#\" onclick=\"javascript:O__operation('create');\">New</a></p>";

	$O__colLabels = array("Menu","Last Updated","Indexer Actions","Menu Actions");
	$O__filesArray = O__dirToArray($O__pathMenus);
	sort($O__filesArray);
		
	for($i=0;$i<count($O__filesArray);$i++){
		$O__menuName = substr($O__filesArray[$i][0],0,-4);
		$O__data[$i][0] = "<a href=\"/" . $O__PT . "/" . $O__menuName . "/display\">" . $O__menuName . "</a>";
		$O__data[$i][1] = date("F d, Y H:i:s", filemtime($O__pathMenus . "/" . $O__filesArray[$i][0]));
		if(file_exists($O__pathIndexers . "/" . $O__menuName . ".xml")){
				$O__data[$i][2] = "<a href=\"/O__settings/" . $O__menuName . "/edit?type=i&PT=O__menu\">Edit</a> | ";
				$O__data[$i][2] .= "<a href=\"#\" onclick=\"javascript:O__operation('indexerDelete','" .  $O__menuName ."');\">Delete</a>";
			} else {
				$O__data[$i][2] = "<a href=\"#\" onclick=\"javascript:O__operation('indexerCreate','" .  $O__menuName ."');\">Create</a>";
			}
		$O__data[$i][3] = "<a href=\"/" .  $O__PT . "/" . $O__menuName . "/edit\">Edit</a> | ";
		$O__data[$i][3] .= "<a href=\"#\" onclick=\"javascript:O__operation('rename','" .  $O__menuName ."');\">Rename</a> | ";
		$O__data[$i][3] .= "<a href=\"#\" onclick=\"javascript:O__operation('delete','" .  $O__menuName ."');\">Delete</a>";
	}
	
	$O__output .= O__tableMaker($O__colLabels,$O__data,"O__table","O__tableMenus",0);
	$O__output .= "<br>";
	unset($O__data);

	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__menusManage"];
	O__form.O__target.value = O__target;
	
	switch (O__action){
		case "create":
			var O__input = prompt("Please enter a menu 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 "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__target.value = O__target;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
			
		case "delete":
			var O__input = confirm("Are you sure you want to delete the menu '" + O__target + "'?");
			if (O__input == 1){
				O__form.O__target.value = O__target;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
			
		case "indexerCreate":
			var O__input = confirm("Are you sure you want to create an indexer for '" + O__target + "'?");
			if (O__input == 1){
				O__form.O__target.value = O__target;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
			
		case "indexerDelete":
			var O__input = confirm("Are you sure you want to delete the indexer for '" + O__target + "'?");
			if (O__input == 1){
				O__form.O__target.value = O__target;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
	}

}
</script>
