44 lines
813 B
PHP
Executable File
44 lines
813 B
PHP
Executable File
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/db.php';
|
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
function currentUser(): ?array
|
|
{
|
|
if (empty($_SESSION['uid'])) {
|
|
return null;
|
|
}
|
|
$stmt = db()->prepare('SELECT id, username, role, created_at FROM users WHERE id = :id');
|
|
$stmt->execute([':id' => (int)$_SESSION['uid']]);
|
|
$u = $stmt->fetch();
|
|
return $u ?: null;
|
|
}
|
|
|
|
function isAdmin(): bool
|
|
{
|
|
$u = currentUser();
|
|
return $u && $u['role'] === 'admin';
|
|
}
|
|
|
|
function requireLogin(): void
|
|
{
|
|
if (!currentUser()) {
|
|
header('Location: index.php?action=login');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
function requireAdmin(): void
|
|
{
|
|
requireLogin();
|
|
if (!isAdmin()) {
|
|
http_response_code(403);
|
|
echo 'Forbidden';
|
|
exit;
|
|
}
|
|
}
|