<?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
 */

require_once("imp/_imp.php");

class Controllers {

    //public vars
    public $smarty;
    public $hosturl;

    // it will run in everytime with
    // default requried
    public function __construct() {
        require_once("../public/aw_config.php");
        $this->smarty = $smarty;
        $this->hosturl = $hosturl;

        $this->smarty->assign('thisdisply', 0);
    }

    //here the frist page and
    //the frist action in this class
    public function index() {
        //her login sucesssfull try to login in erp too
        // include the to-be-used language
        require_once('../../_library/login_system/translations/en.php');
        // include the PHPMailer _library
        require_once('../../_library/plugins/PHPMailer/PHPMailer.php');
        // load the registration class
        require_once('../../_library/login_system/classes/Login.php');
        // create the registration object. when this object is created, it will do all registration stuff automatically
        // so this single line handles the entire registration process.
        $login = new Login();
        if ($login->isUserLoggedIn() == true) {
            // the user is logged in. you can do whatever you want here.
            // for demonstration purposes, we simply show the "you are logged in" view.
            header('Location:' . $this->hosturl . '/controllers/index.php');
        } else {
            // the user is not logged in. you can do whatever you want here.
            // for demonstration purposes, we simply show the "you are not logged in" view.
            if (isset($login)) {
                if ($login->errors) {
                    foreach ($login->errors as $error) {
                        $this->smarty->assign('error', $error);
                    }
                }
            }
            #######################check program security #####################################
            $Programesetting = R::findone('excelinfo');
            $deviceId = $this->deviceId();

            //print_r("++++++++++++++++++++++++++++++" . $deviceId . "++++++++++++++++++++++++++++++");
            if (count($Programesetting) < 1) {
                $Programesetting = R::dispense('excelinfo');
                $Programesetting->serial = '';
                $Programesetting->time = date('Y-m-d G:i:s');
                R::store($Programesetting);
            }

 $this->smarty->display('_login/head.html');
                    $this->smarty->display('_login/login.html');
                    $this->smarty->display('_login/js.html');

  /* if (empty($Programesetting->serial)) {
                $this->smarty->assign('deviceId', $deviceId);
                $this->smarty->display('_login/head.html');
                $this->smarty->display('error.html');
                $this->smarty->display('_login/js.html');
            } else {
                $serialCal = $this->serialcheck();
                //print_r($serialCal);
                if ($Programesetting->serial == $serialCal) {
                   
                } else {
                    $this->smarty->assign('deviceId', $deviceId);
                    $this->smarty->display('_login/head.html');
                    $this->smarty->display('error.html');
                    $this->smarty->display('_login/js.html');
                }
            }*/
            ###################################################################################
        }
    }

    public function logout() {
        // include the to-be-used language
        require_once('../../_library/login_system/translations/ar.php');
        // include the PHPMailer _library
        require_once('../../_library/plugins/PHPMailer/PHPMailer.php');
        // load the registration class
        require_once('../../_library/login_system/classes/Login.php');
        // create the registration object. when this object is created, it will do all registration stuff automatically
        // so this single line handles the entire registration process.
        $login = new Login();

        if ($login->isUserLoggedIn() == true) {
            // the user is logged in. you can do whatever you want here.
            // for demonstration purposes, we simply show the "you are logged in" view.
            header('Location:' . $this->hosturl . '/controllers/index.php');
        } else {
            // the user is not logged in. you can do whatever you want here.
            // for demonstration purposes, we simply show the "you are not logged in" view.
            header('Location:' . $this->hosturl . '/controllers/login.php');
        }
    }

    public function fgpass() {
        // include the to-be-used language
        require_once('../../_library/login_system/translations/ar.php');
        // include the PHPMailer _library
        require_once('../../_library/plugins/PHPMailer/PHPMailer.php');
        // load the registration class
        require_once('../../_library/login_system/classes/Login.php');
        // create the registration object. when this object is created, it will do all registration stuff automatically
        // so this single line handles the entire registration process.
        $login = new Login();


        if ($login->passwordResetWasSuccessful() == true && $login->passwordResetLinkIsValid() != true) {
            //not use just redirect if happend
            header('Location:' . $this->hosturl . '/controllers/login.php');
        } else {
            //sucess  good
            header('Location:' . $this->hosturl . '/controllers/login.php');
        }
    }

    public function passrest() {
        // include the to-be-used language
        require_once('../../_library/login_system/translations/ar.php');
        // include the PHPMailer _library
        require_once('../../_library/plugins/PHPMailer/PHPMailer.php');
        // load the registration class
        require_once('../../_library/login_system/classes/Login.php');
        // create the registration object. when this object is created, it will do all registration stuff automatically
        // so this single line handles the entire registration process.
        $login = new Login();

        if ($login->passwordResetWasSuccessful() == true && $login->passwordResetLinkIsValid() != true) {
            //not use just redirect if happend
            header('Location:' . $this->hosturl . '/controllers/login.php');
        } else {
            //sucess  good
            $this->smarty->assign('user_name', $_GET['id']);
            $this->smarty->assign('verification_code', $_GET['other']);

            $this->smarty->display('_login/head.html');
            $this->smarty->display('_login/resetpass.html');
            $this->smarty->display('_login/js.html');
        }
    }

    private function deviceId() {

        // Turn on output buffering
        ob_start();

//Get the ipconfig details using system commond
        system('ipconfig /all');

// Capture the output into a variable
        $mycomsys = ob_get_contents();

// Clean (erase) the output buffer
        ob_clean();

        $find_mac = "Physical";
//find the "Physical" & Find the position of Physical text

        $pmac = strpos($mycomsys, $find_mac);
// Get Physical Address

        $macaddress = substr($mycomsys, ($pmac + 36), 17);

        $mac = str_replace(array('-'), '', $macaddress);

//Display Mac Address

        return $mac;
    }

    private function serialcheck() {
        // Turn on output buffering
        ob_start();

//Get the ipconfig details using system commond
        system('ipconfig /all');

// Capture the output into a variable
        $mycomsys = ob_get_contents();

// Clean (erase) the output buffer
        ob_clean();

        $find_mac = "Physical";
//find the "Physical" & Find the position of Physical text

        $pmac = strpos($mycomsys, $find_mac);
// Get Physical Address

        $macaddress = substr($mycomsys, ($pmac + 36), 17);

        $mac = str_replace(array('-'), '', $macaddress);


        $random_salt = hash('sha512', '1208');
        // Create salted password
        $hashedMac = hash('sha512', $mac . $random_salt);
        return $hashedMac;
    }

    public function activate() {
        $id = filter_input(INPUT_POST, "serial");
        $serialCal = $this->serialcheck();

        if ($id == $serialCal) {
            $Programesetting = R::findone('excelinfo');
            $Programesetting->serial = $id;
            $Programesetting->time = date('Y-m-d G:i:s');
            R::store($Programesetting);

            $this->smarty->display('_login/head.html');
            $this->smarty->display('_login/login.html');
            $this->smarty->display('_login/js.html');
        } else {
            $deviceId = $this->deviceId();
            $this->smarty->assign('deviceId', $deviceId);
            $this->smarty->display('_login/head.html');
            $this->smarty->display('error.html');
            $this->smarty->display('_login/js.html');
        }
    }

    public function __destruct() {
        //colse connection
        R::close();
    }

}
