Oups…
{$msg}
Retour à l'accueil) sont forcément à la racine. Sinon, on est servi depuis un sous-dossier (WAMP : /FWS-Website). */ if (preg_match('~/((?:inc|api|admin|cache)|p/(?:index|dl|paquets|equipe|contact|assets))(?:/|$)~', $dir, $m)) { $base = substr($dir, 0, (int) strrpos($dir, $m[0])); } else { $base = $dir; } return $base; } } if (!function_exists('fws_url')) { /** URL d'une route ou d'un asset local, préfixée par la base URL. */ function fws_url(string $path): string { if ($path === '' || preg_match('~^(?:[a-z][a-z0-9+.-]*:|//)~i', $path)) { return $path; /* absolu, mailto:, tel:, //… : inchangé */ } $base = fws_base_url(); if ($base !== '' && ($path === $base || str_starts_with($path, $base . '/'))) { return $path; /* déjà préfixé (idempotence) */ } return $base . '/' . ltrim($path, '/'); } } /* En CLI (scripts d'administration dans le conteneur), on affiche tout : * c'est interactif, l'opérateur doit voir les problèmes. */ if (PHP_SAPI === 'cli') { ini_set('display_errors', '1'); } /* Journal : api/bugs/data/ (déjà interdit au web par .htaccess + gitigné). */ $fwsErrorLogDir = dirname(__DIR__) . '/api/bugs/data'; if (!is_dir($fwsErrorLogDir)) { @mkdir($fwsErrorLogDir, 0770, true); } if (is_dir($fwsErrorLogDir) && is_writable($fwsErrorLogDir)) { ini_set('error_log', $fwsErrorLogDir . '/php-errors.log'); } unset($fwsErrorLogDir); /* ── 3. Réponses d'erreur propres ──────────────────────────────────────── */ if (!function_exists('fws_error_wants_json')) { /** * 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 { $accept = strtolower((string) ($_SERVER['HTTP_ACCEPT'] ?? '')); if (str_contains($accept, 'text/html')) { return false; /* navigateur → page visuelle */ } return true; /* curl, scripts, API clients → JSON */ } /** * Réponse d'erreur finale : JSON ou page HTML minimaliste, sans aucun * détail technique. Ne fait rien si les en-têtes sont déjà partis. */ function fws_error_render(int $status, string $message): void { if (headers_sent()) { return; /* trop tard pour propre : on referme silencieusement */ } http_response_code($status); if (fws_error_wants_json()) { header('Content-Type: application/json; charset=utf-8'); header('X-Content-Type-Options: nosniff'); header('Cache-Control: no-store'); echo json_encode(['ok' => false, 'error' => $message], JSON_UNESCAPED_UNICODE); return; } header('Content-Type: text/html; charset=utf-8'); header('Cache-Control: no-store'); $msg = htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); echo <<
{$msg}
Retour à l'accueil