<?php
require_once __DIR__ . '/initialize.php';
function require_login(): void {
    if (!is_logged_in()) {
        redirect('/login.php');
    }
}
function logout_user(): void {
    global $con;
    if (is_logged_in()) {
        $hash = hash('sha256', session_id());
        $stmt = $con->prepare('UPDATE jos_admin_user_sessions SET revoked_at=NOW() WHERE session_hash=?');
        if ($stmt) { $stmt->bind_param('s', $hash); $stmt->execute(); $stmt->close(); }
        audit_log('logout', 'admin_user', (string)current_user()['id']);
    }
    $_SESSION = [];
    if (ini_get('session.use_cookies')) {
        $p = session_get_cookie_params();
        setcookie(session_name(), '', time() - 42000, $p['path'], $p['domain'], (bool)$p['secure'], (bool)$p['httponly']);
    }
    session_destroy();
}
