fix(auth): logout without /logout rewrite (avoid Apache 404)

This commit is contained in:
2026-07-25 15:09:58 +08:00
parent 820fafd8a7
commit 6e71008d00
2 changed files with 54 additions and 8 deletions
+36 -5
View File
@@ -85,7 +85,7 @@ final class Router {
}
public function maybe_flush_rewrite_rules(): void {
$rewrite_version = AKSRT_VERSION . ':sign-router-v1';
$rewrite_version = AKSRT_VERSION . ':sign-router-v2-logout';
if (get_option('aksrt_auth_routes_flushed') !== $rewrite_version) {
flush_rewrite_rules(false);
update_option('aksrt_auth_routes_flushed', $rewrite_version);
@@ -187,7 +187,7 @@ final class Router {
}
public function handle_logout_route(): void {
if (!get_query_var('aksrt_logout')) {
if (!$this->is_logout_request()) {
return;
}
@@ -197,13 +197,44 @@ final class Router {
}
$nonce = isset($_GET['_wpnonce']) ? sanitize_text_field((string) $_GET['_wpnonce']) : '';
if (!wp_verify_nonce($nonce, 'aksrt-logout')) {
wp_die(__('退出登录请求无效,请重试。', 'aksrt'));
// Accept theme nonce and core log-out nonce (wp_logout_url default).
$nonce_ok = ($nonce !== '' && (
wp_verify_nonce($nonce, 'aksrt-logout')
|| wp_verify_nonce($nonce, 'log-out')
));
if (!$nonce_ok) {
wp_die(esc_html__('退出登录请求无效,请重试。', 'aksrt'), esc_html__('退出登录', 'aksrt'), ['response' => 403]);
}
$redirect_to = isset($_GET['redirect_to']) ? wp_unslash((string) $_GET['redirect_to']) : home_url('/');
$redirect_to = wp_validate_redirect($redirect_to, home_url('/'));
wp_logout();
wp_safe_redirect($redirect_to ?: home_url('/'));
wp_safe_redirect($redirect_to !== '' ? $redirect_to : home_url('/'));
exit;
}
/**
* Detect logout via rewrite query var, GET flag, or pretty path.
*/
private function is_logout_request(): bool {
if ((string) get_query_var('aksrt_logout') !== '' && (string) get_query_var('aksrt_logout') !== '0') {
return true;
}
if (isset($_GET['aksrt_logout']) && (string) $_GET['aksrt_logout'] !== '' && (string) $_GET['aksrt_logout'] !== '0') {
return true;
}
$request_uri = isset($_SERVER['REQUEST_URI']) ? (string) $_SERVER['REQUEST_URI'] : '';
if ($request_uri === '') {
return false;
}
$path = (string) wp_parse_url($request_uri, PHP_URL_PATH);
$home_path = (string) wp_parse_url(home_url('/'), PHP_URL_PATH);
if ($home_path !== '' && $home_path !== '/' && str_starts_with($path, $home_path)) {
$path = (string) substr($path, strlen(rtrim($home_path, '/')));
}
$path = '/' . trim($path, '/');
return $path === '/logout';
}
}