diff --git a/inc/errors.php b/inc/errors.php index a6dff80..bddd71a 100644 --- a/inc/errors.php +++ b/inc/errors.php @@ -44,16 +44,21 @@ unset($fwsErrorLogDir); if (!function_exists('fws_error_wants_json')) { - /** Le client attend-il du JSON ? (sous /api/ ou requête JSON explicite) */ + /** + * Le client attend-il du JSON ? + * + * Un navigateur (navigation réelle) demande explicitement du HTML + * (« Accept: text/html, … ») → il recevra une page visuelle. + * Tout le reste — curl (Accept générique), scripts, apps, appels fetch() + * ou clients qui annoncent du JSON — reçoit du JSON. + */ function fws_error_wants_json(): bool { - $path = (string) (parse_url((string) ($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH) ?: '/'); - if (str_starts_with($path, '/api')) { - return true; + $accept = strtolower((string) ($_SERVER['HTTP_ACCEPT'] ?? '')); + if (str_contains($accept, 'text/html')) { + return false; /* navigateur → page visuelle */ } - $accept = (string) ($_SERVER['HTTP_ACCEPT'] ?? ''); - $ctype = (string) ($_SERVER['CONTENT_TYPE'] ?? ''); - return str_contains($accept, 'application/json') || str_contains($ctype, 'application/json'); + return true; /* curl, scripts, API clients → JSON */ } /** diff --git a/page.php b/page.php index 1513e21..738293e 100644 --- a/page.php +++ b/page.php @@ -70,9 +70,10 @@ if (in_array((int) ($_GET['__e'] ?? 0), [403, 404], true)) { $is404 = true; header('Access-Control-Allow-Origin: *'); header('Cache-Control: no-store'); - /* Chemins API : réponse JSON (jamais de HTML ni de détail technique). */ + /* Chemins API : JSON pour curl/scripts, page visuelle pour un navigateur + * (négociation via Accept, comme inc/errors.php). */ $reqPath = (string) (parse_url((string) ($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH) ?: '/'); - if (str_starts_with($reqPath, '/api')) { + if (str_starts_with($reqPath, '/api') && fws_error_wants_json()) { http_response_code(404); header('Content-Type: application/json; charset=utf-8'); header('X-Content-Type-Options: nosniff');