<?php
error_reporting(E_ALL ^ E_NOTICE);
class Hooks_class {

    public $ci;
    public $router;
    private $arr_action = array('index', 'add', 'edit', 'delete');
	

    function __construct() {
        $this->ci = & get_instance();
        $this->ci->load->library('session'); //If not loaded, then load it here
        $this->router = & load_class('Router', 'core');
    }


    /**
     * authenticate
     *
     * This function is used for user authentication for ACL 
     * 
     * @author	Harshada Kakade
     * @access	public
     * @param	null
     * @return	void
     */
    function authenticate() {

        # Get the routing information.
	    $router = & load_class('Router', 'core');
        $class_name = $router->fetch_class();
        $method_name = $router->fetch_method();

        # Get the required information such as user access level etc.
        $this->ci = & get_instance();
        $arr_session = $this->ci->session->all_userdata();
        $level_id = $arr_session['level'];
        $arr_result = array();
        $this->ci->load->model('manage_permissions', 'auth');

        $strModule = $method_name;
        if ($strModule == "index") {
            $strModule = "";
        } else {
            $strModule = "/".$strModule;
        }

        $strTotalUrlToCheck = $class_name.$strModule;
        //echo $strTotalUrlToCheck;exit;
        if ($strTotalUrlToCheck == 'users/login' || $strTotalUrlToCheck == 'users/logout' || $strTotalUrlToCheck == 'users/fnSendChangePasswordEmail'|| $strTotalUrlToCheck == 'users/fnChangePassword') {
            return;
        }

        # Check whether the session is set or not.
        if(! $arr_session['username'] ) {
            $strRedirectpath = base_url().'index.php/users/login';
            redirect($strRedirectpath);
        }

        $this->ci->load->model('user');
        $arrGetAllUrl = (array)$this->ci->user->fnGetAllMethods(1);
        if (in_array($strTotalUrlToCheck, $arrGetAllUrl)) {
            $this->ci = & get_instance();
            $level_id = $arr_session['level'];
            $arr_result = $this->ci->user->fnCheckAccessIsAllowed($level_id, $strTotalUrlToCheck);
            if(!empty($arr_result))
            {
                return;

            }  else {
                if ($strModule == '/subscriber_list') {
                    return;
                    exit;
                }
                if ($strModule == '/add_comment_subscriber') {
                    echo '<script>alert("You do not have enough permission to access this function !\n Please contact to system administrator")</script>';
                    echo '<script>close();</script>';
                    exit;
                }
                $strPreviousUrl =$_SERVER['HTTP_REFERER'];
                echo '<script> $(".modal-content .modal-dialog").css("border", "2px solid red")</script>';
                echo '<script>alert("You do not have enough permission to access this function !\n Please contact to system administrator")</script>';
                echo '<script>window.location = "'.$strPreviousUrl.'"</script>';
            }
        } else {
            return;
        }

		
		
    }
	
	
	

}

?>
