<?php

require_once(__DIR__."/src/config.php");
require_once(__DIR__."/src/query.php");
require_once(__DIR__."/src/security.php");
require_once(__DIR__."/src/router.php");

require_once(__DIR__."/src/entities/account.php");
require_once(__DIR__."/src/entities/periods.php");
require_once(__DIR__."/src/entities/events.php");
require_once(__DIR__."/src/entities/context.php");
require_once(__DIR__."/src/entities/beneficiary.php");

session_start();

if (isset($_SESSION["accountId"]) || login()) {
    $route = substr($_SERVER["REQUEST_URI"], strlen($_SERVER["SCRIPT_NAME"]) -strlen(basename(__FILE__)));
    $args = null;
    $sep = strpos($route, '?');
    if ($sep !== false) {
        $argString = substr($route, $sep +1);
        $route = substr($route, 0, $sep);
        $args = array_filter(explode('&', $argString), "strlen");
    }
    $routeParts = array_filter(explode('/', $route), "strlen");
    route($routeParts, $args);
} else {
    require(__DIR__."/templates/login.php");
}

