<?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('../../_stone_lib/login_system/translations/ar.php');
        require_once('../../_stone_lib/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 && $roleid != 0) {
            header('Location:' . $hosturl . '/_error/autho');
        }

        //get controll id
        $controllinfo = R::findOne('acontroll', 'name = ?', [$controllname]);
        $controllcount = R::count('acontroll', 'name = ?', [$controllname]);
        if ($controllcount <= 0) {
            header('Location:' . $hosturl . '/_error/autho');
            exit();
        }
        //get prop id
        $controllpropinfo = R::findOne('aactions', 'actionname = ? and acontrollid = ?', [$action, $controllinfo->id]);
        $controllpropcount = R::count('aactions', 'actionname = ? and acontrollid = ?', [$action, $controllinfo->id]);
        if ($controllpropcount <= 0) {
            header('Location:' . $hosturl . '/_error/autho');
            exit();
        }

        //get role and prop info
        $getroleprop = R::count('aroleaction', 'roleid = ? and actionid = ?', [$roleid, $controllpropinfo->id]);
        if ($getroleprop > 0 || $roleid == 0) {
            //there are role for this prop
        } else {
            //there are not role for this prop
            if ($controllpropinfo->status == 0) {
                header('Location:' . $hosturl . '/_error/autho');
                exit();
            } else {
                //nothing good to go
            }
        }
    }
    //check if the user is autho2 there is no header location just return number
    public static function checkauthoize2($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 && $roleid != 0) {
            //header('Location:' . $hosturl . '/_error/autho');
            return -1;
        }

        //get controll id
        $controllinfo = R::findOne('acontroll', 'name = ?', [$controllname]);
        $controllcount = R::count('acontroll', 'name = ?', [$controllname]);
        if ($controllcount <= 0) {
            //header('Location:' . $hosturl . '/_error/autho');
            return -12;
            //exit();
        }
        //get prop id
        $controllpropinfo = R::findOne('aactions', 'actionname = ? and acontrollid = ?', [$action, $controllinfo->id]);
        $controllpropcount = R::count('aactions', 'actionname = ? and acontrollid = ?', [$action, $controllinfo->id]);
        if ($controllpropcount <= 0) {
            //header('Location:' . $hosturl . '/_error/autho');
            return -13;
           // exit();
        }

        //get role and prop info
        $getroleprop = R::count('aroleaction', 'roleid = ? and actionid = ?', [$roleid, $controllpropinfo->id]);
        if ($getroleprop > 0 || $roleid == 0) {
            //there are role for this prop
        } else {
            //there are not role for this prop
            if ($controllpropinfo->status == 0) {
                //header('Location:' . $hosturl . '/_error/autho');
                return -14;
                //exit();
            } else {
                //nothing good to go
            }
        }
        
        return 1;
    }

}
