<?php
session_start();

define('SESSION_TIMEOUT', 900); // 15 minutes

$current = $_SERVER['REQUEST_URI'];

/* =========================
   NOT LOGGED IN
========================= */
if (!isset($_SESSION['admin_logged_in'])) {
    header(
        'Location: /admin/login.php?from=' . urlencode($current)
    );
    exit;
}

/* =========================
   SESSION TIMEOUT
========================= */
if (
    isset($_SESSION['LAST_ACTIVITY']) &&
    (time() - $_SESSION['LAST_ACTIVITY']) > SESSION_TIMEOUT
) {
    session_unset();
    session_destroy();

    header(
        'Location: /admin/login.php?timeout=1&from=' . urlencode($current)
    );
    exit;
}

/* =========================
   UPDATE ACTIVITY
========================= */
$_SESSION['LAST_ACTIVITY'] = time();
