<?php

if (!function_exists('ec_session_timeout_guard')) {

    function ec_session_timeout_guard(): void
    {
        if (session_status() !== PHP_SESSION_ACTIVE) {
            session_start();
        }

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

        $publicPaths = [
            '/easychair/login',
            '/easychair/logout',
            '/easychair/register',
            '/easychair/forgot-password',
            '/easychair/reset-password',
        ];

        foreach ($publicPaths as $publicPath) {
            if ($path === $publicPath || str_starts_with($path, $publicPath . '/')) {
                return;
            }
        }

        $timeout = 3600;

        if (!empty($_SESSION['last_activity']) && time() - (int)$_SESSION['last_activity'] > $timeout) {

            session_unset();
            session_destroy();

            header('Location: /easychair/login?expired=1');
            exit;
        }

        if (!empty($_SESSION['user_id'])) {
            $_SESSION['last_activity'] = time();
        }
/*
        if (empty($_SESSION['user_id']) && str_starts_with($path, '/easychair/')) {
            header('Location: /easychair/login?expired=1');
            exit;
        }
*/    
}
}
