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

$O__output = "";

function O__operation(){
	global $O__DBpath, $O__PT, $O__PTATmsg, $O__logNotes;
	if (isset($_POST["O__target"])){ $O__target = (int)$_POST["O__target"]; }
	if (isset($_POST["O__source"])){ $O__source = $_POST["O__source"]; }
	if (isset($_POST["O__state"])){ $O__state = $_POST["O__state"]; }
	if (isset($_POST["O__from"])){ $O__from = $_POST["O__from"]; }
	if (isset($_POST["O__to"])){ $O__to = $_POST["O__to"]; }
	if (isset($_POST["O__code"])){ $O__code = $_POST["O__code"]; }
	$O__operation = $_POST["O__operation"];
	
	switch($O__operation){			
		case "redirectAdd":
			$O__fileData = O__XMLdata($O__DBpath . "/pageTypes/" . $O__PT . "/data/redirects.xml", 0, 0);
			
			// Add new redirect
			$O__permissionAdd = $O__fileData->addChild("redirect");
				$O__permissionAdd->addChild("state","0");
				$O__permissionAdd->addChild("from","FROM");
				$O__permissionAdd->addChild("to","TO");
				$O__permissionAdd->addChild("code","301");
			$O__fileData = $O__fileData->asXML();
			O__dataWrite($O__DBpath . "/pageTypes/" . $O__PT . "/data/redirects.xml",$O__fileData);
			clearstatcache(); // clear cache so newest version can be loaded
					
			$O__sysMsgHeadline = "Redirect Add";
			$O__sysMsgBody = "Success. New empty redirect entry has been added.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			break;
			
		case "redirectRemove":
			$O__fileData = O__XMLdata($O__DBpath . "/pageTypes/" . $O__PT . "/data/redirects.xml", 0, 0);
			unset($O__fileData->redirect[$O__target]);
			$O__fileData = $O__fileData->asXML();
			O__dataWrite($O__DBpath . "/pageTypes/" . $O__PT . "/data/redirects.xml",$O__fileData);
			clearstatcache(); // clear cache so newest version can be loaded
					
			$O__sysMsgHeadline = "Redirect Remove";
			$O__sysMsgBody = "Success. Redirect #" . $O__target . " '" . $O__from . "'(from) -> '" . $O__to . "'(to) has been removed.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			break;
			
		case "redirectUpdate":
			$O__fileData = O__XMLdata($O__DBpath . "/pageTypes/" . $O__PT . "/data/redirects.xml", 0, 0);
			$O__fileData->redirect[$O__target]->state = $O__state;
			$O__fileData->redirect[$O__target]->from = $O__from;
			$O__fileData->redirect[$O__target]->to = $O__to;
			$O__fileData->redirect[$O__target]->code = $O__code;
			$O__fileData = $O__fileData->asXML();
			O__dataWrite($O__DBpath . "/pageTypes/" . $O__PT . "/data/redirects.xml",$O__fileData);
			clearstatcache(); // clear cache so newest version can be loaded
			
			$O__sysMsgHeadline = "Redirect Update";
			$O__sysMsgBody = "Success. Redirect '" . $O__target . "' has been updated.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			break;
	}
}

function O__createOutput(){
	global $O__DBpath, $O__PT, $O__AT, $O__PTATmsg;

	$O__fileData = O__XMLdata($O__DBpath . "/pageTypes/" . $O__PT . "/data/redirects.xml", 0, 0);
		
	$O__output = "<h2>Redirects Manager</h2>";
	$O__output .= $O__PTATmsg;
	$O__output .= "<form name=\"O__redirectsEdit\" action=\"/" . $O__PT . "/default/" . $O__AT . "\" 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__state\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__from\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__to\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__code\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__operation\" value=\"\">";
	
	// Redirects
	$O__output .= "<b>States:</b>";
	$O__output .= "<i><br>0 = Redirect is off";
	$O__output .= "<br>1 = Partial match on FROM URL path";
	$O__output .= "<br>2 = Partial match on FROM full URL";
	$O__output .= "<br>3 = Exact match on FROM URL path";
	$O__output .= "<br>4 = Exact match on FROM full URL</i>";
	$O__output .= "<br>For a list of redirection codes, please see <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections\" target=\"_new\">Mozilla's page</a>.";
	
	$O__output .= "<br><br><b>Add:</b> ";
	$O__output .= "<a href=\"#\" onclick=\"javascript:O__operation('redirectAdd');\">Redirect</a>";
	for ($i=0; $i<count($O__fileData->redirect); $i++){
		$O__data[$i][0] = "<input type=\"text\" name=\"" . $i . "-state\" id=\"" . $i . "-state\" value=\"" . $O__fileData->redirect[$i]->state . "\">";
		$O__data[$i][1] = "<input type=\"text\" name=\"" . $i . "-from\" id=\"" . $i . "-from\" value=\"" . $O__fileData->redirect[$i]->from . "\">";
		$O__data[$i][2] = "<input type=\"text\" name=\"" . $i . "-to\" id=\"" . $i . "-to\" value=\"" . $O__fileData->redirect[$i]->to . "\">";
		$O__data[$i][3] = "<input type=\"text\" name=\"" . $i . "-code\" id=\"" . $i . "-code\" value=\"" . $O__fileData->redirect[$i]->code . "\">";
		$O__data[$i][4] = "<a href=\"#\" onclick=\"javascript:O__operation('redirectUpdate','" . $i . "','" . $O__fileData->redirect[$i]->from . "','" . $O__fileData->redirect[$i]->to . "');\">Update</a> | ";
		$O__data[$i][4] .= "<a href=\"#\" onclick=\"javascript:O__operation('redirectRemove','" . $i . "','" . $O__fileData->redirect[$i]->from . "','" . $O__fileData->redirect[$i]->to . "');\">Delete</a>";
	
	}
	$O__colLabels = array("State","From","To","Code","Action");
	$O__output .= O__tableMaker($O__colLabels,$O__data,"O__table","O__tablePageTypes",0);
	$O__output .= "</form>";
	
	return $O__output;
}

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

<script>
function O__operation(O__action, O__target, O__from, O__to){
	var O__form = document.forms["O__redirectsEdit"];
	O__form.O__target.value = O__target;
	
	switch (O__action){
		case "redirectAdd":
			var O__input = confirm("Are you sure you want to create a new redirect entry?");
			if (O__input == 1){
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
			
		case "redirectUpdate":
			var O__input = confirm("Are you sure you want to update the redirect '" + O__from + "' pointing to '" + O__to + "'?");
			if (O__input == 1){
				O__form.O__target.value = O__target;
				O__stateNew = O__target + "-state";
					O__form.O__state.value = document.getElementById(O__stateNew).value;
				O__fromNew = O__target + "-from";
					O__form.O__from.value = document.getElementById(O__fromNew).value;
				O__toNew = O__target + "-to";
					O__form.O__to.value = document.getElementById(O__toNew).value;
				O__codeNew = O__target + "-code";
					O__form.O__code.value = document.getElementById(O__codeNew).value;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
			
		case "redirectRemove":
			var O__input = confirm("Are you sure you want to remove the redirect '" + O__from + "' pointing to '" + O__to + "'?");
			if (O__input == 1){
				O__form.O__target.value = O__target;
				O__form.O__from.value = O__from;
				O__form.O__to.value = O__to;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
	}

}
</script>
