<?php

session_start(); // Start The Session

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Debug\ErrorHandler;
use Symfony\Component\HttpKernel\Debug\ExceptionHandler;


// ENABLE ERROR WARNING / DEBUG
ini_set("display_errors", "0");
error_reporting(-1);

date_default_timezone_set("Asia/Kuala_Lumpur"); 
ini_set( 'date.timezone', 'Asia/Kuala_Lumpur' );
// Call the necessary files
include_once 'global.variable.php';
include_once 'func.class/SignatureGenerator.php'; // Main CSRF
// Configuring the CSRF
$secret = 'aUajhskjTAkjHjkhaskLjasjkh17912Hkas'; // well chosen secret
$csrf = new Kunststube\CSRFP\SignatureGenerator($secret);

// Call Hooks
include_once 'func.class/hook.class.php';
global $hooks;
include_once 'func.class/db.class.php';
$db = new Db();
include_once 'func.class/general.func.php';
include_once 'func.class/core.func.php';

// Redirect to Login if Match the rule
if (!preg_match("/reset-password?(\w+)/", $_SERVER["REQUEST_URI"] ) && $_SERVER["REQUEST_URI"] != "/register" && $_SERVER["REQUEST_URI"] != "/register/send-verification" && $_SERVER["REQUEST_URI"] != "/register/check-verification" && $_SERVER["REQUEST_URI"] != "/register/submit" && $_SERVER["REQUEST_URI"] != "/register/check" && $_SERVER["REQUEST_URI"] != "/register/check" && $_SERVER["REQUEST_URI"] != "/forgot-password/reset" && $_SERVER["REQUEST_URI"] != "/forgot-password" && $_SERVER["REQUEST_URI"] != '/login' && $_SERVER["REQUEST_URI"] != '/login/auth' && !is_login()) {
    header('Location: /login');
} else {
    // Call the Silex after logged in
    require_once 'vendor/autoload.php';
    $app = new Silex\Application(); // <-- Declare $app as Silex
    $app->register(new Silex\Provider\SwiftmailerServiceProvider()); // Register Swiftmailer
	// $nexmo = new Nexmo\Client(new Nexmo\Client\Credentials\Basic("3c2aaf82", "4bc9f145f63e7cca"));
	
	
    $menu_array = array();
    // CSRF AND ESCAPE ANY POST REQUEST
    $protectPostsData = function (Request $request) use($app, $csrf) {
        switch ($request->getMethod()) {
            case 'POST':
                // Escape Any POST DATA
                // foreach ($_POST as $key => $val) {
                    // $_POST[$key] = $app->escape($_POST[$key]);
                // }
                // CSRF IT
                // $token = base64_decode($request->get('token'));
                // CSRF It !
                // if (!$csrf->validateSignature($token)) {
                    // return new Response("TOKEN WRONG!!", 403);
                    // exit;
                // }
        }
    };
    $protectPostsRender = function (Request $request) use($app, $csrf) {
        switch ($request->getMethod()) {
            case 'GET':
                /* Generate CSRF Token And Field */
                // $token = base64_encode($csrf->getSignature());
                // echo "<input type='hidden' id='token' name='token' value='" . $token . "'>";
        }
    };
    // $app->before($protectPostsData);
    // $app->after($protectPostsRender);
	include 'mailsetting.php';
	// Call the Login function , Mailer Function and make a conditional
	include_once 'func.class/mailer.func.php';
	include_once 'func.class/login.func.php';
	if (is_login()) {
		// Call necessary files;
		include_once 'func.class/newuser.func.php';
		include_once 'func.class/modular.func.php'; // <-- Controller Module
		include_once 'func.class/pagination.func.php'; // <-- Pagination Class
		
		$app->get('/', function() use($app) {
			return $app->redirect('/dashboard');
		});
		if ($_SESSION["role"] == "1") {
			$hooks->do_action('all_menu');
			$hooks->do_action('silex_action');
		} else {
			$hooks->do_action('admin_menu');
			$hooks->do_action('silex_action');
			$hooks->do_action('admin_action');
		}
		$_SESSION["company_id"] = getCompany($_SESSION['user_id']);
		$_SESSION["branch_id"] = getBranch($_SESSION['user_id']);
		
	}
    $app['debug'] = true;
    // Include the Mail Configurator
    $app->run();
}
