<?php

ini_set('session.cookie_path', '/');
ini_set('session.cookie_domain', '');
ini_set('session.use_only_cookies', 1);

if (session_status() !== PHP_SESSION_ACTIVE) {

    session_start();

    $path =
        parse_url(
            $_SERVER['REQUEST_URI'] ?? '/',
            PHP_URL_PATH
        );

    $isPublicConference =
        preg_match(
            '#^/easychair/conference/[a-zA-Z0-9\-_]+$#',
            $path
        );

    $isPublicPaper =
        preg_match(
            '#^/easychair/paper/[0-9]+$#',
            $path
        );

    if (
        !$isPublicConference
        &&
        !$isPublicPaper
        &&
        !empty($_SESSION['user_id'])
        &&
        !empty($_SESSION['last_activity'])
    ) {

        $timeout = 3600;

        if (
            time() - $_SESSION['last_activity']
            > $timeout
        ) {

            session_unset();
            session_destroy();

            header(
                "Location: /easychair/login?expired=1"
            );

            exit;
        }
    }

    if (!empty($_SESSION['user_id'])) {

        $_SESSION['last_activity'] =
            time();

    }
}

/*
-----------------------------------------
Debug (safe)
-----------------------------------------
*/

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

/*
-----------------------------------------
Define BASE_PATH
-----------------------------------------
*/

if (!defined('BASE_PATH')) {

    define(
        'BASE_PATH',
        dirname(__DIR__)
    );

}

/*
-----------------------------------------
Start session
-----------------------------------------
*/

if (session_status() !== PHP_SESSION_ACTIVE) {

    session_start();

}

/*
-----------------------------------------
Autoload
-----------------------------------------
*/

require_once
BASE_PATH .
'/app/core/Database.php';

require_once
BASE_PATH .
'/app/core/App.php';


require_once
BASE_PATH .
'/app/helpers/session_timeout_guard.php';

ec_session_timeout_guard();

/*
-----------------------------------------
Run application
-----------------------------------------
*/

$app =
new \App\Core\App();

$app->run();
