Files

90 lines
2.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// ============================================================
// ODHLÁŠENÍ
// ============================================================
// Tento soubor zruší session a remember me cookie.
// Uživatele přesměruje na přihlašovací stránku.
//
// Odhlášení se provádí přes POST formulář (ne přes odkaz GET),
// aby nemohlo dojít k nechtěnému odhlášení např. načtením
// obrázku nebo prefetchingem odkazu prohlížečem.
// ============================================================
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/db.php';
session_name(SESSION_NAZEV);
session_set_cookie_params([
'lifetime' => 0,
'path' => '/',
'httponly' => true,
'samesite' => 'Strict',
// 'secure' => true,
]);
session_start();
// Přijímáme pouze POST požadavky
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: ' . AUTH_LOGIN_URL);
exit;
}
// -- Ověření CSRF tokenu -------------------------------------
$csrf_z_formulare = $_POST['csrf_token'] ?? '';
$csrf_token = $_SESSION['csrf_token'] ?? '';
if (empty($csrf_token) || !hash_equals($csrf_token, $csrf_z_formulare)) {
// Neplatný CSRF token přesměrujeme bez odhlášení
header('Location: ' . AUTH_LOGIN_URL);
exit;
}
// -- Smazání remember me tokenu z DB -------------------------
// Smažeme pouze token pro tento konkrétní selector (toto zařízení),
// ostatní zařízení uživatele zůstanou přihlášena
if (isset($_COOKIE['auth_selector'])) {
$stmt = $pdo->prepare("
DELETE FROM `" . DB_TABULKA_TOKENY . "`
WHERE `selector` = :selector
");
$stmt->execute([':selector' => $_COOKIE['auth_selector']]);
}
// -- Smazání remember me cookies -----------------------------
setcookie('auth_selector', '', [
'expires' => time() - 3600,
'path' => '/',
'httponly' => true,
'samesite' => 'Strict',
]);
setcookie('auth_token', '', [
'expires' => time() - 3600,
'path' => '/',
'httponly' => true,
'samesite' => 'Strict',
]);
// -- Zrušení session -----------------------------------------
// Vyprázdníme pole session
$_SESSION = [];
// Smažeme session cookie z prohlížeče
if (ini_get('session.use_cookies')) {
$params = session_get_cookie_params();
setcookie(
session_name(),
'',
time() - 3600,
$params['path'],
$params['domain'],
$params['secure'],
$params['httponly']
);
}
// Zrušíme session na serveru
session_destroy();
// -- Přesměrování na přihlašovací stránku -------------------
header('Location: ' . AUTH_LOGIN_URL);
exit;