<?php
declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| Secure Session Configuration
|--------------------------------------------------------------------------
*/

ini_set('session.use_strict_mode', '1');
ini_set('session.use_only_cookies', '1');
ini_set('session.cookie_httponly', '1');

if (PHP_VERSION_ID >= 70300) {

    session_set_cookie_params([
        'lifetime' => 0,
        'path'     => '/',
        'secure'   => !empty($_SERVER['HTTPS']),
        'httponly' => true,
        'samesite' => 'Lax'
    ]);

} else {

    session_set_cookie_params(
        0,
        '/; samesite=Lax',
        '',
        !empty($_SERVER['HTTPS']),
        true
    );
}

if (session_status() !== PHP_SESSION_ACTIVE) {

    session_name('OG_ADMIN_SESSION');
    session_start();

}

/*
|--------------------------------------------------------------------------
| Auto Logout After 30 Minutes
|--------------------------------------------------------------------------
*/

$timeout = 1800;

if (isset($_SESSION['LAST_ACTIVITY'])) {

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

        $_SESSION = [];

        if (ini_get('session.use_cookies')) {

            $params = session_get_cookie_params();

            setcookie(
                session_name(),
                '',
                time() - 42000,
                $params['path'],
                $params['domain'],
                $params['secure'],
                $params['httponly']
            );
        }

        session_destroy();

        header('Location: ../admin/login.php');

        exit;
    }
}

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