Template
Fix/404 tux pattes #3
No files matched your search
+12
-7
@@ -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 */
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in new issue
Block a user