<?php

function base_path(string $path = ''): string
{
    $base = '/easychair';
    return $base . '/' . ltrim($path, '/');
}

function e($value): string
{
    return htmlspecialchars((string)($value ?? ''), ENT_QUOTES, 'UTF-8');
}

function csrf_token(): string
{
    if (empty($_SESSION['_csrf'])) {
        $_SESSION['_csrf'] = bin2hex(random_bytes(32));
    }
    return $_SESSION['_csrf'];
}

function csrf_check(): void
{
    if (
        empty($_POST['_csrf']) ||
        empty($_SESSION['_csrf']) ||
        !hash_equals($_SESSION['_csrf'], $_POST['_csrf'])
    ) {
        die('Invalid CSRF token');
    }
}

function current_lang(): string {
return $_SESSION['lang'] ?? 'en';
}
