<?php
	//THIS DOCUMENT CHECKS IF A PERSON IS LOGGED IN AND HAS EDITING CAPABILITIES. IF TRUE, THEY ARE ALLOWED TO VIEW THE PAGE, OTHERWISE IF THEY AREN'T LOGGED IN THEY ARE REDIRECTED TO THE LOGIN PAGE. IF THEY DON'T HAVE EDITING CAPABILITIES, THEY ARE SENT TO HOME PAGE WITH AN ERROR MESSAGE.
	
	
	if(!isset($_SESSION)){
		session_start();
	}
	
	if(isset($_POST['action']) && !empty($_POST['action'])){
		$theAction = " - " . $_POST['action'];
	}else{
		$theAction = "";
	}
	
	if(isset($_SESSION['lastRegeneration'])){
		if(++$_SESSION['lastRegeneration'] > 10) {
			$_SESSION['lastRegeneration'] = 0;
			session_regenerate_id();
		}
	}else{
		$_SESSION['lastRegeneration'] = 1;
	}
	
	//echo "<h3>Last Regeneration: " . $_SESSION['lastRegeneration'];
	//echo "<br>" . session_id() . "</h3>";
	
	if($_SERVER['REQUEST_URI'] !== "/admin/interactions.php"){
		$_SESSION['returnURL'] = $_SERVER['REQUEST_URI'];
	}
	
	if(isset($_SESSION['LoggedIn']) && isset($_SESSION['Username']) && $_SESSION['LoggedIn'] === 1){
		$loggedIn = true;
		$canEdit = $_SESSION['Edit'] + 0;
		
		if($canEdit !== 1){
			header("Location: //" . $_SERVER['SERVER_NAME'] . "?error=noAccess");
			die;
		}
	}else{
		header("Location: //" . $_SERVER['SERVER_NAME'] . "/admin/login.php");
		die;
	}
	
	$log = $_SERVER['DOCUMENT_ROOT'] . "/admin/inc/loggedUsers.txt";
	
	$theEntry = date("Y-m-d H:i:s") . " - " . htmlentities($_SESSION['Username'], ENT_QUOTES) . " (" . $_SERVER['REQUEST_URI'] . $theAction . ")\n";
	
	$handle = fopen($log, "a+") or die("can't open file");
	fwrite($handle, $theEntry);
	fclose($handle);
?>
<?php require_once($_SERVER['DOCUMENT_ROOT'] . '/common/constants.php'); ?>
