55 lines
1.2 KiB
PHP
55 lines
1.2 KiB
PHP
<?php
|
|
|
|
const ADMIN_USERNAME = 'admin';
|
|
const ADMIN_PASSWORD = 'karczma2026!';
|
|
|
|
function startAdminSession(): void
|
|
{
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
}
|
|
|
|
function isAdminLoggedIn(): bool
|
|
{
|
|
startAdminSession();
|
|
return !empty($_SESSION['staff_logged_in']) && $_SESSION['staff_logged_in'] === true;
|
|
}
|
|
|
|
function requireAdminAuth(bool $redirectToLogin = true): void
|
|
{
|
|
if (isAdminLoggedIn()) {
|
|
return;
|
|
}
|
|
|
|
if ($redirectToLogin) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
function attemptAdminLogin(string $username, string $password): bool
|
|
{
|
|
startAdminSession();
|
|
|
|
if (hash_equals(ADMIN_USERNAME, $username) && hash_equals(ADMIN_PASSWORD, $password)) {
|
|
$_SESSION['staff_logged_in'] = true;
|
|
$_SESSION['staff_username'] = ADMIN_USERNAME;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function adminLogout(): void
|
|
{
|
|
startAdminSession();
|
|
$_SESSION = [];
|
|
if (ini_get('session.use_cookies')) {
|
|
$params = session_get_cookie_params();
|
|
setcookie(session_name(), '', time() - 42000, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
|
|
}
|
|
session_destroy();
|
|
}
|
|
|