<?php
/**
 * SLM Framework - Entry Point
 *
 * All requests are routed through this file.
 */

// Load configuration
$file = "../slm.framework/system/config.slm";
if (file_exists($file)) include $file;

// Load helper functions
$file = "../slm.framework/system/helpers.slm";
if (file_exists($file)) include $file;

// Show errors in development mode
if (isDev()) {
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
}

// Load session management class
$file = "../slm.framework/system/_SLM.slm";
if (file_exists($file)) include $file;

// Load system API functions
$file = "../slm.framework/system/slm_api.slm";
if (file_exists($file)) include $file;

//====================================================================
// SESSION INITIALIZATION
//====================================================================
if (isset($_COOKIE["slmwafk"])) {
    _SLM::$appToken = $_COOKIE["slmwafk"];
} else {
    $id = md5(microtime());
    setcookie("slmwafk", $id, time() + (86400 * 3000), "/");
    _SLM::$appToken = $id;
}

_SLM::loadSession();

//====================================================================
// LOGOUT HANDLER
//====================================================================
if (isset($_GET["logout"])) {
    _SLM::logout();
}

//====================================================================
// SESSION SWITCH (for admin impersonation)
//====================================================================
if (isset($_POST['acc']) && $_POST['acc'] == 'switch') {
    if (empty($_POST['switch_to'])) {
        $result = _response(true, 'Missing data');
    } else {
        $result = _SLM::switchSession($_POST['switch_to']);
    }
    _SLM::die(json_encode($result));
}

//====================================================================
// AUTHENTICATED USER FLOW
//====================================================================
if (_SLM::auth()) {
    // Set encryption key for the session
    setSecretKey(md5(_SLM::$session['user']['id'] . _SLM::$session['user']['email'] . _SLM::$appToken));

    // Handle theme mode switch
    if (isset($_GET["mode"])) {
        _SLM::$session["user"]["mode"] = $_GET["mode"];
        _SLM::saveSession();
        header("Location: ./");
        _SLM::die();
    }

    // Route: View request (?v=module.action)
    if (isset($_GET["v"])) {
        $_vista = $_GET["v"];

        // Check permissions
        $securityModule = _SLM::getSecurityAccess($_vista);
        if (count($securityModule) < 1) {
            header("Location: ./");
        }
    }
    // Route: AJAX request (?a=module.action)
    elseif (isset($_GET["a"])) {
        $ajax = $_GET["a"];

        // Check permissions
        $securityModule = _SLM::getSecurityAccess($ajax);
        if (count($securityModule) < 1) {
            _SLM::die(json_encode(_response(true, "Access denied")));
        }

        // Load module API
        $file = getApi($ajax);
        if (file_exists($file)) include $file;

        // Load AJAX handler
        $file = getAction($ajax, "ajax");
        if (file_exists($file)) include $file;

        _SLM::die();
    }
    // Default: Home page
    else {
        $_vista = "home.main";
    }

    // Load module API
    $file = getApi($_vista);
    if (file_exists($file)) include $file;

    // Pre-processing
    $file = getAction($_vista, "proc");
    if (file_exists($file)) include $file;

    // Data loading
    $file = getAction($_vista, "load");
    if (file_exists($file)) include $file;

    // Header template
    $file = "../slm.framework/modules/_header.slm";
    if (file_exists($file)) include $file;

    // View template
    $file = getAction($_vista, "view");
    if (file_exists($file)) include $file;

    // Footer template
    $file = "../slm.framework/modules/_footer.slm";
    if (file_exists($file)) include $file;

    // Save session changes
    _SLM::saveSession();
}
//====================================================================
// UNAUTHENTICATED USER FLOW
//====================================================================
else {
    // Determine which security page to show
    if (isset($_GET["login"])) {
        $file = "login";
    } elseif (isset($_GET["register"])) {
        $file = "register";
    } elseif (isset($_GET["forgot"])) {
        $file = "forgot";
    } else {
        $file = "login";
    }

    // Load the security page
    $file = "../slm.framework/security/$file.slm";
    if (file_exists($file)) include $file;
    _SLM::die();
}
