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

$elOutputHeader = "";
$elOutputBody = "";
$O__logNotes .= "";

function O__loginVerify(){
	global $elOutputHeader,
			$elOutputBody,
			$elOutputBodyBottom,
			$O__logNotes,
			$O__DBpath,
			$O__storagePath;

	$O__userID = O__xCrypt(O__sanitize($_POST["O__userID"]),"e");
	$O__password = O__xCrypt(O__sanitize($_POST["O__password"]),"e");
	$O__userAcct = $O__DBpath . $O__storagePath . "users/" . $O__userID . ".xml";
	
	if (file_exists($O__userAcct)){ // if user ID exists
		$O__fileData = O__XMLdata($O__userAcct, 0, 0);
		$O__userAcctID = $O__fileData->name;
		$O__userAcctPW = $O__fileData->pw;
		
		if (($O__userID == O__xCrypt($O__userAcctID,"e"))&&($O__password == $O__userAcctPW)) {
			$_SESSION["O__loggedIn"] = 1;
			$_SESSION["O__userID"] = $_POST["O__userID"];
			$O__logNotes .= "User '" . $O__userAcctID . "' login succeeded.";
			$elOutputHeader = "<meta http-equiv='refresh' content='1; url=/O__pageTypes'>\n";				

			$elOutputBody = "Login validated.";
			$elOutputBodyBottom = "Signing in...";				
		} else {
			$elOutputBody = "Login unsuccessful.";
			$O__logNotes .= "User '" . $O__userAcctID . "' login failed.";
			$elOutputBodyBottom = "<input type='button' value='Go Back' onclick='javascript:history.go(-1);' class='O__input'>";
		}				
		
	} else {
		$elOutputBody = "Login unsuccessful.";
		$O__logNotes .= "User '" . $_POST["O__userID"] . "' login failed with '" . $_POST["O__password"] ."'";
		$elOutputBodyBottom = "<input type='button' value='Go Back' onclick='javascript:history.go(-1);' class='O__input'>";
	}	
}

function O__createOutput(){
	global $elOutputBody, $elOutputBodyBottom;
	
	$O__output = "<table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">";
	$O__output .= "<tr><td class=\"O__text\">Verification:</td></tr>";
	$O__output .= "<tr><td class=\"O__text\">" . $elOutputBody . "</td></tr>";
	$O__output .= "<tr><td class=\"O__text\">" . $elOutputBodyBottom . "</td></tr>";
	$O__output .= "</table>";
	
	return $O__output;
}

O__loginVerify();
$O__output = O__createOutput();
?>

<script>	
	// Swap visuals on load
	var	O__menuInfoArea = "O__menuItemDesc";
	
	window.addEventListener("load", function(){
		document.getElementById(O__menuInfoArea).innerHTML = "Checking credentials...";
	});
</script>

<?php echo($elOutputHeader); ?>
