<?php

/*
 * awframework from aw.inc present aw controller
 * it will help you develop easy and fast with secure tools
 * you can edit and add what you feel right
 * don't change in strucure of file
 */

class autho {

    //check if the user is login or not
    public static function checkautho($hosturl) {
        require_once('../../library/login_system/translations/ar.php');
        require_once('../../library/login_system/classes/Login.php');
        $login = new Login();
        if ($login->isUserLoggedIn() == true) {
            //go autho user
        } else {
            header('Location:' . $hosturl . '/login/logout');
        }
    }

    //check if the user is autho
    public static function checkauthoize($controllname, $hosturl, $roleid) {

        //see the action first
        $action = filter_input(INPUT_GET, 'ac');
        if (empty($action)) {
            $action = 'index';
        }

        //if the role id session is empty
        if (!$roleid) {
            header('Location:' . $hosturl . '/error/autho');
        }

        //get controll id
        $controllinfo = R::findOne('awcontroll', 'name = ?', [$controllname]);
       
        if (count($controllinfo) <= 0) {
            header('Location:' . $hosturl . '/error/autho');
            exit();
        }

        //get prop id
       
        $controllpropinfo = R::findOne('awcontrollprop', 'proplabel = ? and awcontroll_id = ?', [$action, $controllinfo->id]);
        
       
        if (count($controllpropinfo) <= 0) {
            header('Location:' . $hosturl . '/error/autho');
            exit();
        }

        //get role and prop info
        $getroleprop = R::findOne('awrolecontrollprop', 'role_id = ? and controllprop_id = ?', [$roleid, $controllpropinfo->id]);
      
        if (count($getroleprop) > 0) {
            //there are role for this prop
            if ($getroleprop->checkval == 0) {
                header('Location:' . $hosturl . '/error/autho');
                exit();
            } else {
                //nothing good to go
            }
        } else {
            //there are not role for this prop
            if ($controllpropinfo->checkval == 0) {
                header('Location:' . $hosturl . '/error/autho');
                exit();
            } else {
                //nothing good to go
            }
        }
    }

}
