<?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__webUpload = O__XMLgetData($O__fileData,"/settings/setting[@id='webUpload']/value");
unset($O__fileData);

$O__systemMsg = "";

function O__operations(){
	global $O__DBpath, $O__libPath, $O__systemMsg, $O__logNotes;

	$O__operation = $_POST["O__operation"];
	$O__target = $_POST["O__target"];
	$O__source = $_POST["O__source"];
	$O__continue = 1;

	switch($O__operation){
		case "pageTypeAdd":
			/* NOTE: Expects a .tar file with the following structure:
				file.tar
					[folder that is name of PageType]
					[ActionType file]
					[ActionType file]
					[ActionType file. etc.]
					header.php (optional)
					[PageType].xml
					/libraries (optional)
						[library file]
						[library file]
						[library file. etc.]
			*/
		
			/* Upload file */
			$O__pathTemp = $O__DBpath . "/temp/PTinstall/";
			if (!file_exists($O__pathTemp)){ mkdir($O__pathTemp,0777); }
			$O__filename = basename($_FILES["O__file2Upload"]["name"]);
			move_uploaded_file($_FILES["O__file2Upload"]["tmp_name"], $O__pathTemp . $O__filename);

			/* Unzip file */
			try {
				$O__PTarchive = new PharData($O__pathTemp . $O__filename);
				$O__PTarchive->extractTo($O__pathTemp);
				unlink($O__pathTemp . $O__filename);
			
				// Get PageType name
				$O__filesArray = O__dirToArray($O__pathTemp);
				for($i=0;$i<count($O__filesArray);$i++){
					if ($O__filesArray[$i][1] == "d"){
						$O__PTname = $O__filesArray[$i][0];
						break;
					}
				}
			} catch(Exception $O__errorExcep) {
				unlink($O__pathTemp . $O__filename);
				rmdir($O__pathTemp); // cleanup
				$O__sysMsgLabel = "Install PageType: ";
				$O__sysMsgValue = "Failed to extract " . $O__filename . ". File removed.";
				$O__systemMsg = O__sysMsg("<b>" . $O__sysMsgLabel . "</b>" . $O__sysMsgValue, "O__sysMsg");
				$O__logNotes .= $O__sysMsgLabel . $O__sysMsgValue;			
				$O__continue = 0;
			}
					
			/* Copy config file */
			if ($O__continue == 1) {
				$O__pathPTtempConfig = $O__DBpath . "/temp/PTinstall/". $O__PTname . "/" . $O__PTname . ".xml";
				if (file_exists($O__pathPTtempConfig)){
					rename($O__pathPTtempConfig, $O__DBpath . "/configs/pageTypes/" . $O__PTname . ".xml");
				} else {
					$O__sysMsgLabel = "Install PageType: ";
					$O__sysMsgValue = "Failed for '". $O__PTname . "'. Configuration file not found.";
					$O__systemMsg = O__sysMsg("<b>" . $O__sysMsgLabel . "</b>" . $O__sysMsgValue, "O__sysMsg");
					$O__logNotes .= $O__sysMsgLabel . $O__sysMsgValue;			
					$O__continue = 0;
				}
			}
			
			/* Copy ActionType files */
			if ($O__continue == 1) {
				
				// If PageType folder doesn't already exist, then create it
				$O__pathPT = $O__libPath . "/pageTypes/" . $O__PTname;				
				if (!file_exists($O__pathPT)){ mkdir($O__pathPT,0777); }
				
				// Move new files to PageType path
				$O__filesArray = O__dirToArray($O__pathTemp . $O__PTname);
				for($i=0;$i<count($O__filesArray);$i++){
					if ($O__filesArray[$i][1] == "f"){ // move item if it is a file
						rename($O__pathTemp . $O__PTname . "/" . $O__filesArray[$i][0], $O__pathPT . "/" . $O__filesArray[$i][0]);
					}
				}
			}
			
			/* Copy library files */
			if ($O__continue == 1) {
				
				$O__pathPTtempLib = $O__pathTemp . $O__PTname . "/libraries/";
				if (file_exists($O__pathPTtempLib)){ // If there is a libraries directory in package
					
					// If PageType libraries folder doesn't already exist, then create it
					$O__pathPTlib = $O__libPath . "/pageTypes/" . $O__PTname . "/libraries";				
					if (!file_exists($O__pathPTlib)){ mkdir($O__pathPTlib,0777); }
					
					// Move new files to PageType library path
					$O__filesArray = O__dirToArray($O__pathPTtempLib);
					for($i=0;$i<count($O__filesArray);$i++){
						rename($O__pathPTtempLib . $O__filesArray[$i][0], $O__pathPTlib . "/" . $O__filesArray[$i][0]);
					}
					rmdir($O__pathPTtempLib); // cleanup
				}
				rmdir($O__pathTemp . $O__PTname); // cleanup
				rmdir($O__pathTemp); // cleanup
			
				/* Success message */
				$O__sysMsgLabel = "Add PageType: ";
				$O__sysMsgValue = "Success for '" . $O__PTname . "'. Please check installation files to confirm.  This PageType will need to be whitelisted in your INI file.";
				$O__systemMsg = O__sysMsg("<b>" . $O__sysMsgLabel . "</b>" . $O__sysMsgValue, "O__sysMsg");
				$O__logNotes .= $O__sysMsgLabel . $O__sysMsgValue;
				$O__continue = 0;
			}
		
			break;
		case "pageTypeRemove":
			$O__PTname = $O__target;
		
			/* Backup and remove config file */
			O__backupData($O__DBpath . "/configs/pageTypes/" . $O__target . ".xml",1);
			
			/* Backup and remove PageType library files */
			$O__pathPTlib = $O__libPath . "/pageTypes/" . $O__PTname . "/libraries";
			if(file_exists($O__pathPTlib)){
				$O__pathPTlibArray = O__dirToArray($O__pathPTlib);
				for($i=0; $i<count($O__pathPTlibArray); $i++){
					O__backupData($O__pathPTlib . "/" . $O__pathPTlibArray[$i][0],1);
				}
				rmdir($O__pathPTlib);
			}
			
			/* Backup and remove ActionType files */
			$O__pathPT = $O__libPath . "/pageTypes/" . $O__PTname;
			$O__pathPTarray = O__dirToArray($O__pathPT);
			for($i=0; $i<count($O__pathPTarray); $i++){
				O__backupData($O__pathPTfile = $O__pathPT . "/" . $O__pathPTarray[$i][0],1);
			}
			
			try {
				rmdir($O__pathPT);
				$O__sysMsgLabel = "Remove PageType: ";
				$O__sysMsgValue = "Success for '"  . $O__target . "'. Data files not removed. Please ensure that this PageType is no longer whitelisted in your INI file.";
				$O__systemMsg = O__sysMsg("<b>" . $O__sysMsgLabel . "</b>" . $O__sysMsgValue, "O__sysMsg");
				$O__logNotes .= $O__sysMsgLabel . $O__sysMsgValue;
			} catch(Exception $O__errorExcep) {
				rmdir($O__pathPT);
				$O__sysMsgLabel = "Remove PageType: ";
				$O__sysMsgValue = "Failed for '"  . $O__target . "'. Removal may only have been partially successful.  Please check the PageTypes directory for any remaining files.";
				$O__systemMsg = O__sysMsg("<b>" . $O__sysMsgLabel . "</b>" . $O__sysMsgValue, "O__sysMsg");
				$O__logNotes .= $O__sysMsgLabel . $O__sysMsgValue;
			}	
			
			break;
		}
	
}

function O__createOutput(){
	global $O__DBpath, $O__libPath, $O__PT, $O__systemMsg, $O__webUpload;

	$O__output = "<h2>PageType Manager</h2>";
	$O__output .= $O__systemMsg;
	
	if ($O__webUpload == 1){
		$O__output .= "<form name=\"O__pageTypeEdit\" action=\"./" . $O__PT . "\" 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 .= "<form name=\"O__browserUpload\" action=\"" . $O__PT . "\" method=\"post\" enctype=\"multipart/form-data\">";
		$O__output .= "<b>Add/ Update Pagetype:</b> Select file: ";
		$O__output .= "<input type=\"file\" name=\"O__file2Upload\" id=\"O__fileUpload\">";
		$O__output .= "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"512000\">";
		$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=\"pageTypeAdd\">";
		$O__output .= "<input type=\"submit\" value=\"Upload\" name=\"submit\">";
		$O__output .= "</form>";
		$O__output .= "<br>";
	}
	
	$O__colLabels = array("Name","Description","Author","PageType","Action Types","Libraries","Version","Actions");
	$O__filesArray = O__dirToArray($O__libPath . "/pageTypes");
	sort($O__filesArray);
		
	for($i=0;$i<count($O__filesArray);$i++){
		$O__fileData = O__XMLdata($O__libPath . "/pageTypes/" . $O__filesArray[$i][0] . "/info.xml", 0, 0);
		
		// Get basic info
		//$O__data[$i][0] = "<b>". $O__fileData->name . "</b>";
		$O__data[$i][0] = "<b><a href=\"/" . $O__fileData->pageType . "\">". $O__fileData->name . "</a></b>";
		$O__data[$i][1] = $O__fileData->desc;
		$O__data[$i][2] = $O__fileData->author;
		$O__data[$i][3] = $O__fileData->pageType;
		
		// Get PageType actions
		$O__data[$i][4] = "";
		$O__temp = $O__fileData->actions->all;
		$O__temp = explode("," , $O__temp);
		for ($j=0; $j<count($O__temp); $j++){
			if (trim($O__temp[$j]) == trim($O__fileData->actions->default)){
				$O__data[$i][4] .= trim($O__temp[$j]) . "<i>(default)</i>, ";
			} else {
				$O__data[$i][4] .= trim($O__temp[$j]) . ", ";
			}
		}
		$O__data[$i][4] = rtrim($O__data[$i][4], ", ");
		
		// Get libraries
		$O__data[$i][5] = "";
		$O__temp = $O__fileData->library;
		$O__temp = explode("," , $O__temp);
		for ($j=0; $j<count($O__temp); $j++){
			$O__data[$i][5] .= trim($O__temp[$j]) . ", ";
		}
		$O__data[$i][5] = rtrim($O__data[$i][5], ", ");
		
		$O__data[$i][6] = $O__fileData->version;
		
		// Actions column
		$O__data[$i][7] = "";
		if (file_exists($O__DBpath . "/pageTypes/" . $O__filesArray[$i][0] . "/configs/settings.xml")){
			$O__data[$i][7] .= "<a href=\"/O__settings/" . $O__filesArray[$i][0] . "/edit/PT\">Settings</a>";
		}
		
		if ($O__webUpload == 1){ $O__data[$i][7] .= "| <a href=\"#\" onclick=\"javascript:O__operation('pageTypeRemove','" . $O__filesArray[$i][0] . "');\">Remove</a>"; }
	}
	
	$O__output .= O__tableMaker($O__colLabels,$O__data,"O__table","O__tablePageTypes",0);
	
	return $O__output;
}

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

<script>
function O__operation(O__action, O__target){
	var O__form = document.forms["O__pageTypeEdit"];
	O__form.O__target.value = O__target;
	
	switch (O__action){
		case "pageTypeRemove":
			var O__input = confirm("Are you sure you want to remove the PageType '" + 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>
