<?php
if(isset($_REQUEST['username'])) {
	$uname = trim($_REQUEST['username']);
}
else {
	$uname = "";
}
if(isset($_REQUEST['password'])) {
	$pwd = trim($_REQUEST['password']);
}
else {
	$pwd = "";
}

$_SESSION['SEAS_login_error_msg'] = "" ;

require_once($application_path . "class/seas_user.class.php");
require_once($application_path . "model/seas_user.mdl.php");

$SEAS_User = new seas_user();
$SEAS_User_Model = new seas_user_Model();

$login = $SEAS_User_Model->login($uname,$pwd,$SEAS_User);

$_SESSION['seas_user_id']		= 0;
$_SESSION['seas_user_type']		= 0;
$_SESSION['seas_user_fullname']	= "";
$_SESSION['PCD_user_opts']		= "";

if($login == false) {
	$_SESSION['SEAS_login_error_msg'] = "Login failed: incorrect username or password";
	header("location: home");
}
else {
	$_SESSION['SEAS_login_error_msg'] = "";
	
	$current_date = new DateTime(date("Y-m-d"));
	$expiry_date  = new DateTime($SEAS_User->get_date_expiry());
	$_SESSION['seas_user_date_expiry'] = $expiry_date->format("M jS, Y");
	$interval = $current_date->diff($expiry_date);  // Check date validity
	
	if($interval->format('%R%a') >0) {
		
		$_SESSION['seas_user_id'] 		= $SEAS_User->get_user_id();
		$_SESSION['seas_user_type']		= $SEAS_User->get_usertype();
		$_SESSION['seas_user_fullname']	= $SEAS_User->get_fullname();
		$_SESSION['PCD_user_opts']		= $SEAS_User->get_user_options();	
		
		// Profile pict
		$prof_pict = "profiles/" . $_SESSION['seas_user_id'] . "_img.jpg";
		if(file_exists($application_path . $prof_pict)) {
			$_SESSION['seas_user_img']		= 	$prof_pict;
		}
		else {
			$_SESSION['seas_user_img']		= 	"profiles/img.jpg";
		}
		
		$user_priv = $SEAS_User_Model->get_user_priv($_SESSION['seas_user_id']);
		
		if($user_priv != false) {
			$_SESSION['seas_user_srv_type'] = $user_priv['srv_type'];
			$_SESSION['seas_user_market']   = $user_priv['srv_market'];
		}
		else {
			$_SESSION['seas_user_srv_type'] = array();
			$_SESSION['seas_user_market']   = array();
		}
	
		header("location: dashboard");
	}
	else {
		$_SESSION['SEAS_login_error_msg'] = "Your account is expired. Kindly contact <a href='http://www.arabresearch.com' target='_blank'>PARC</a> for renewal";
		header("location: home");		
	}
}



?>