<?php
// Simple auth helper: protect pages and fetch current user row.
if (session_status() === PHP_SESSION_NONE) { session_start(); }
require_once __DIR__.'/connexion.php';

function require_login(){
    if (empty($_SESSION['user_id'])) {
        header('Location: /hiba/login.php');
        exit; }
}

function current_user(PDO $pdo){
    if (empty($_SESSION['user_id'])) return null;
    static $cache=null; if($cache) return $cache;
    $stmt=$pdo->prepare('SELECT id, first_name, last_name, user_email, phone_num, user_role, status FROM users_table WHERE id=? LIMIT 1');
    $stmt->execute([$_SESSION['user_id']]);
    $cache=$stmt->fetch(PDO::FETCH_ASSOC);
    return $cache;
}
?>
