<?php

	class classSecurity
	{
		private $userID = NULL;
		private $password = NULL;
		public $userName = NULL;
		public $orginationID = NULL;
		public $locationID = NULL;
		
		private $userOb = NULL;
		
		public static $securityObject = NULL;
		
		private function __construct(){
		}
		
		public static function getUser() {
			if(is_null(self::$securityObject))
			{
				self::$securityObject = new self;
				return self::$securityObject;
			}
			else
			{
				return self::$securityObject;
			}
		}
	
		function loginUserSpp() {
			
			if(isset($_POST['user_name']) && isset($_POST['password']) && isset($_POST['referrer']))
			{
				$user_name = $_POST['user_name'];
				$password = $_POST['password'];
				$referrer = $_POST['referrer'];
			}
			if(is_null($user_name))
			{
				throw new MyException(101100);
			}
			if(is_null($password))
			{
				throw new MyException(101101);
			}
			if(is_null($referrer))
			{
				throw new MyException(101102);
			}
			
			if(!$this->checkUserNameRegistered($user_name,false))
			{
				$token = array('var1'=>$user_name);
				throw new MyException(101103,$token);
			}
			
			$this->setUserName($user_name);
			$this->setPassword($password);
			
			if(!$this->checkPassword())
			{
				throw new MyException(101104);
			}
			
			$this->setUserOb();
			$this->setSppSession();
			//$this->insertUserLoginHistory($email);
			
		}
		
		function checkUserNameRegistered($user_name = NULL) {
			
				if(!is_null($user_name))
				{
					$user_name = $user_name;
				}
				else
				{
					throw new MyException(101105);
				}
			
			if(!classFilter::validateUniqueName($user_name))
			{
				throw new MyException(101106);
			}
			
			$query = "SELECT user_name FROM users WHERE user_name = '$user_name'";
			$queryOb = new classQueryGlobal;
			$array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($array[0]['user_name'])&&$array[0]['user_name']!='')
			{
				return 1;
			}
			else
			{
				$token = array('var1'=>$user_name);
				throw new MyException(101107,$token);
			}
		
		}
		
		function setUserName($userName,$throwException = true) {
			if(!is_null($userName))
			{
				if(classFilter::validateUniqueName($userName))
				{
					$this->userName = classFilter::addMySlashes($userName);
				}
				else
				{
					$token = array('var1'=>$userName);
					throw new MyException(101108,$token);
				}
			}
			elseif($throwException)
			{
				throw new MyException(101109);
			}
		}
		
		function setPassword($password) {

				if(!is_null($password))
				{
					$this->password = md5($password);
				}
				else
				{
					throw new MyException(101110);
				}

		}
		
		function checkPassword($throwException = TRUE) {
			
			$password = classFilter::addMySlashes($this->password);
			
			$query = "SELECT user_id FROM users WHERE user_name = '" . $this->getUserName() . "' AND password = '" . $this->getPassword() . "'";
			$queryOb = new classQueryGlobal;
			$array['password_check'] = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(count($array['password_check']) == 0 && !$throwException)
			{
				return 0;
			}
			elseif(count($array['password_check']) == 0 && $throwException)
			{
				throw new MyException(101111);
			}
			elseif(count($array['password_check']) == 1)
			{
				return 1;
			}
		}
		
		function insertUserLoginHistory($email){
			$userOb = new classUser;
			$user_id = $userOb->getIDByEmail($email);
			$time = time();
			$ip_addres = $_SERVER['REMOTE_ADDR'];
			$query = "INSERT INTO user_login_history (user_id,ip_address,login_time) VALUES ('$user_id','$ip_addres','$time')";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		}
		
		
		function setUserOb() {
			$userOb = new classUser;
			$userOb->retrieveUserAllDataByUserName($this->getUserName());
		
			if(!is_null($userOb->getUserID()))
			{
				$this->userID = $userOb->getUserID();
				
				if(isset($_SESSION['security']['password']))
				{
					$this->password = $_SESSION['security']['password'];
				}
				elseif(!is_null($this->password) && isset($_SESSION['security']['password']) && $this->password != $_SESSION['security']['password'])
				{
					$this->logout();
				}
				
				if(!$this->checkPassword(false))
				{
					$this->logout();
				}
				
				if(!is_null($userOb->getUserName()))
				{
					$this->userName = $userOb->getUserName();
				}
				
				if(!is_null($userOb->getUserRoleID()))
				{
					$this->userRoleID = $userOb->getUserRoleID();
				}
				
				if(!is_null($userOb->getOrganizationID()))
				{
					$this->organizationID = $userOb->getOrganizationID();
				}
				
				if(!is_null($userOb->getLocationID()))
				{
					$this->locationID = $userOb->getLocationID();
				}
				
				
				
			}
			
			$this->userOb = $userOb;
		}
		
		private function setSppSession() {
				if(is_null($this->userName))
				{
					throw new MyException(101112);
				}


				$_SESSION['security']['user_name'] = $this->getUserName();
				$_SESSION['security']['user_id'] = $this->getUserID();
				$_SESSION['security']['password'] = $this->getPassword();
				$_SESSION['security']['userrole_id'] = $this->getUserRoleID();
				$_SESSION['security']['org_id'] = $this->getOrganizationID();
				$_SESSION['security']['location_id'] = $this->getLocationID();
				
				return 1;
		}
		
		function getUserName() {
			if(!is_null($this->userName))
			{
				return $this->userName;
			}
			else
			{
				throw new MyException(101113);
			}
		}
		
		function getUserID() {
			if(!is_null($this->userID))
			{
				return $this->userID;
			}
			else
			{
				throw new MyException(101114);
			}
		}
		
		function getPassword() {
			if(!is_null($this->password))
			{
				return $this->password;
			}
			else
			{
				throw new MyException(101115);
			}
		}
		
		function getUserRoleID() {
			
			return $this->userRoleID;
		}
		
		function getOrganizationID() {
			
			return $this->organizationID;
		}
		
		function getLocationID() {
			
			return $this->locationID;
		}
		
		
		
		function logout($referURL="") {
			if(isset($_SESSION['security']['user_id']))
			{
				unset($_SESSION['security']);
			}
			
			if($referURL!="")
			{
				header("Location:" . $referURL);
			}
			else
			{
				header("location:login");
			}
		}
		
		function checkSppSession() {
			if(isset($_SESSION['security']['user_name']) && isset($_SESSION['security']['user_id']))
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
		
		function setUserID($userID = NULL) {
			if(!is_null($userID))
			{
				$this->userID = $userID;
			}
			else
			{
				throw new MyException(101116);
			}
		}
		
		function getUserOb($throwException = NULL) {
			if(!is_null($this->userOb))
			{
				return $this->userOb;
			}
			elseif($throwException)
			{
				throw new MyException(101117);
			}
		}
		
		function redirectMeToOtherSite($referrer = NULL) {
			if(is_null($referrer))
			{
				throw new MyException(101118);
			}
			else
			{
				header("Location:" . $referrer);
			}
		}

		
		
	}

?>