From 6b6b07aa830ce9ee04532df925c630d01467ea97 Mon Sep 17 00:00:00 2001 From: BlackAngelTVdev Date: Wed, 16 Sep 2026 11:46:34 +0200 Subject: [PATCH] Erreurs sous /api/ : page visuelle dans le navigateur, JSON pour curl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Négociation de contenu via l'en-tête Accept : un navigateur (text/html explicite) reçoit la page 404 du site, tout autre client (curl, scripts, apps) reçoit le JSON. S'applique aux 404 doux et aux réponses du filet d'erreurs PHP. --- inc/errors.php | 19 ++++++++++++------- page.php | 5 +++-- 2 files changed, 15 insertions(+), 9 deletions(-) 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');