<?php
//ini_set('session.gc_maxlifetime', 7*24*60*60);
$isAdmin = stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true;
//if($isAdmin){
//	session_name("elite_admin");
//}else{
//	session_name("elite_sales");
//}

session_set_cookie_params(7*24*60*60);


if (!session_start()) {
	header("Location: login.php");
}
include_once("functions.php");

function checkToken(){
	global $con;
	$token = $_REQUEST['token'];
	$query = "SELECT ID FROM SALESMAN WHERE SUBSTRING(MD5(CONCAT(Username,Password)),5,8) LIKE '{$token}'";
	$result = mysqli_query($con, $query);
	$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
}

function checkForCookies() {
	global $con;
	if(isset($_COOKIE['hash']) && isset($_COOKIE['type'])){
		$hash = $_COOKIE['hash'];
		if ($_COOKIE['type'] == "customer") {
			$query = "SELECT * FROM CUSTOMER WHERE CookieHash = '$hash' AND CookieExpire > NOW()";

			$result = mysqli_query($con, $query);
			$rows = mysqli_num_rows($result);
			if ($rows == 1) {
				$row = mysqli_fetch_array($result);
				if ($row['Active']) {
					$_SESSION['LoggedIn'] = 1;
					$_SESSION['isCustomer'] = true;
					$_SESSION['AccountNumber'] = $row['AccountNumber'];
					$_SESSION['LastLogin'] = $row['LastLogin'];
					$_SESSION['Name'] = $row['Name'];

				//header("Location: index.php");
				return true;
				} else {
					header("Location: login.php");
					return false;
				}
			}else {
				header("Location: login.php");
				return false;
			}
		}else {
			$query = "SELECT * FROM SALESMAN WHERE CookieHash = '$hash' AND CookieExpire > NOW()";
			$result = mysqli_query($con, $query);
			$rows = mysqli_num_rows($result);
			if ($rows == 1) {
				$row = mysqli_fetch_array($result);
				$_SESSION['LoggedIn'] = 1;
				$_SESSION['isCustomer'] = false;
				$_SESSION['SalesmanID'] = $row['ID'];
				$_SESSION['Username'] = $row['Username'];
				$_SESSION['setCustomer'] = "";
				$_SESSION['setCustomerMode'] = false;
				$_SESSION['Name'] = $row['FullName'];
				$_SESSION['LastLogin'] = $row['LastLogin'];
				$_SESSION['pricePlanMode'] = false;
				$_SESSION['priceListMode'] = false;
				$_SESSION['AccountNumber'] = '';
				$_SESSION['Language'] = $row['Language'];


				//header("Location: index.php");
				return true;
			}else {
				header("Location: login.php");
				return false;
			}
		}

	}else {
		header("Location: login.php");
		return false;
	}
}
if (!isset($_SESSION['isCustomer']) || !isset($_SESSION['isAdmin'])) {
	//checkForCookies();
	//header("Location: login.php");
}

if (isset($_SESSION['LoggedIn'])) {
	if($_SESSION['LoggedIn'] != 1){
		if (!checkForCookies()) {
			header("Location: login.php");
		}
	}
} else {
	if (!checkForCookies()) {
		header("Location: login.php");
		exit();
	}
}
$self = $_SERVER['PHP_SELF'];
$adminSection = (stripos($self, "admin"))?true:false;
$isAdmin = isset($_SESSION['isAdmin'])?$_SESSION['isAdmin']:false;
if($adminSection && !$isAdmin){
	header("Location: logout.php");
	exit;
}elseif (!$adminSection && $isAdmin) {
	header("Location: logout.php");
	exit;
}
//die("ee");

?>
