From 7663fdab772ec88442ca5e5510cefefbfbb25be1 Mon Sep 17 00:00:00 2001 From: stepan Date: Mon, 16 Mar 2026 23:48:56 +0100 Subject: [PATCH] =?UTF-8?q?[F=C3=81ZE-1][auth]=20P=C5=99id=C3=A1no=20odhl?= =?UTF-8?q?=C3=A1=C5=A1en=C3=AD=20s=20maz=C3=A1n=C3=ADm=20session=20a=20re?= =?UTF-8?q?member=20me=20cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auth/logout.php | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 auth/logout.php diff --git a/auth/logout.php b/auth/logout.php new file mode 100644 index 0000000..2eb94b1 --- /dev/null +++ b/auth/logout.php @@ -0,0 +1,90 @@ + 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; \ No newline at end of file