()
| 913 | } |
| 914 | |
| 915 | public function logout(): void |
| 916 | { |
| 917 | // Retrieve headers and check CSRF token. |
| 918 | $headersArr = array_change_key_case(getallheaders(), CASE_LOWER); |
| 919 | $receivedToken = isset($headersArr['x-csrf-token']) ? trim($headersArr['x-csrf-token']) : ''; |
| 920 | |
| 921 | // Log mismatch but do not prevent logout. |
| 922 | if (isset($_SESSION['csrf_token']) && $receivedToken !== $_SESSION['csrf_token']) { |
| 923 | error_log("CSRF token mismatch on logout. Proceeding with logout."); |
| 924 | } |
| 925 | |
| 926 | // Remove the "remember_me_token" from persistent tokens. |
| 927 | if (isset($_COOKIE['remember_me_token'])) { |
| 928 | $token = $_COOKIE['remember_me_token']; |
| 929 | AuthModel::revokeRememberToken($token); |
| 930 | // Clear the cookie. |
| 931 | $secure = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'); |
| 932 | setcookie('remember_me_token', '', time() - 3600, '/', '', $secure, true); |
| 933 | } |
| 934 | |
| 935 | // Clear session data. |
| 936 | $_SESSION = []; |
| 937 | |
| 938 | // Clear the session cookie. |
| 939 | if (ini_get("session.use_cookies")) { |
| 940 | $params = session_get_cookie_params(); |
| 941 | setcookie( |
| 942 | session_name(), |
| 943 | '', |
| 944 | time() - 42000, |
| 945 | $params["path"], |
| 946 | $params["domain"], |
| 947 | $params["secure"], |
| 948 | $params["httponly"] |
| 949 | ); |
| 950 | } |
| 951 | |
| 952 | // Destroy the session. |
| 953 | session_destroy(); |
| 954 | |
| 955 | // Redirect the user to the login page (or index) with a logout flag. |
| 956 | header("Location: " . fr_with_base_path("/index.html?logout=1")); |
| 957 | exit(); |
| 958 | } |
| 959 | } |
no test coverage detected