' . $title . '
' . $message . '
['title' => 'Requête invalide', 'message' => 'La requête envoyée est invalide.'], 403 => ['title' => 'Accès refusé', 'message' => 'Tu n\u2019as pas accès à cette ressource.'], 404 => ['title' => 'Page introuvable', 'message' => 'La page demandée est introuvable.'], default => ['title' => 'Erreur serveur', 'message' => 'Une erreur est survenue.'], }; } function app_bootstrap_logging(): void { $dir = rtrim((string) Base::instance()->get('LOGS'), '/\\') . DIRECTORY_SEPARATOR; app_ensure_dir($dir); ini_set('log_errors', '1'); ini_set('error_log', $dir . 'php-error.log'); ini_set('display_errors', app_is_prod() ? '0' : '1'); error_reporting(E_ALL); } function app_request_summary(): string { $f3 = Base::instance(); return sprintf( 'request=%s %s ip=%s', (string) ($f3->get('VERB') ?? 'CLI'), (string) ($f3->get('URI') ?? '/'), (string) ($f3->get('IP') ?? '0.0.0.0') ); } function app_write_log(string $fileName, string $line): void { (new Log($fileName))->write($line); } function app_log_error(int $code, string $status, string $text, ?Throwable $exception = null): void { if ($code === 404) { return; } $level = $code >= 500 ? 'error' : ($code >= 400 ? 'warning' : 'info'); $parts = [ sprintf('level=%s code=%d status="%s"', $level, $code, $status), app_request_summary(), ]; if ($text !== '') { $parts[] = 'message="' . str_replace(["\n", '"'], ['\\n', '\\"'], $text) . '"'; } if ($exception !== null) { $parts[] = sprintf('exception="%s" file="%s:%d"', $exception::class, $exception->getFile(), $exception->getLine()); } app_write_log('app.log', implode(' | ', $parts)); } function app_render_error_json(int $code): void { $f3 = Base::instance(); $meta = app_error_meta($code); while (ob_get_level() > 0) { ob_end_clean(); } $f3->status($code); $f3->expire(0); header('Content-Type: application/json; charset=UTF-8'); echo json_encode( ['error' => ['code' => $code, 'title' => $meta['title'], 'message' => $meta['message']]], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); } function app_render_error_fallback(int $code): void { $f3 = Base::instance(); $meta = app_error_meta($code); $base = rtrim((string) $f3->get('BASE'), '/'); while (ob_get_level() > 0) { ob_end_clean(); } $f3->status($code); $f3->expire(0); if (!headers_sent()) { header('Content-Type: text/html; charset=UTF-8'); header('Cache-Control: no-cache, no-store, must-revalidate'); } $title = htmlspecialchars((string) $meta['title'], ENT_QUOTES, 'UTF-8'); $message = htmlspecialchars((string) $meta['message'], ENT_QUOTES, 'UTF-8'); $href = htmlspecialchars($base . '/', ENT_QUOTES, 'UTF-8'); echo '
' . $message . '