<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Admin_Controller extends MX_Controller {

    function __construct() {
        parent::__construct();
		if(!$this->checkSession())
		{
			redirect('authentication/login');
			exit();
		}
		date_default_timezone_set('Asia/Phnom_Penh');
    }
	
	function _allowedIP()
	{
		$ips = array("185.215.188.170","105.215.178.165","119.115.108.100","203.189.159.52");
		$currentUserIp = $this->input->ip_address();
		$data = array(
					'ipAddress'=>$currentUserIp,
		);
		
		$this->form_validation->set_data($data);
		$this->form_validation->set_rules('ipAddress', 'IP Address', 'required|valid_ip');
		if ($this->form_validation->run() == TRUE)
		{
			if(!in_array($currentUserIp, $ips))
			{
				$this->_clearSession();
				$this->session->set_userdata('ms_error','Your IpAddress had block, Please Contact Administrator!');
				redirect('authentication/login');	
			}
		}
		else
		{
			//$this->session->set_flashdata('message','Invalid account!');
			redirect('authentication/login');
			exit();	
		}
	}
	
	private function checkSession()
	{
		if($this->session->userdata('admin'))
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
	private function _clearSession()
	{
		$this->session->unset_userdata('admin');
        $this->session->unset_userdata('userName');
		$this->session->unset_userdata('STATUS_ACTIVATED');
        $this->session->unset_userdata('userId');
    }
}
