$root, 'config' => $config] = require __DIR__ . '/_bootstrap.php'; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { Http::methodNotAllowed(['POST']); exit; } Security::validateCsrf(); (new RateLimiter($root, $config, RateLimiter::clientIp()))->check('score'); $maxBody = (int) ($config['max_body_bytes'] ?? 1_048_576); $body = Http::readJsonBody($maxBody > 0 ? $maxBody : null); $packId = InputValidator::sanitizePackId((string) ($body['pack_id'] ?? 'onboarding-v1')); $answers = InputValidator::validateAnswers(is_array($body['answers'] ?? null) ? $body['answers'] : []); $optional = InputValidator::validateOptional(is_array($body['optional'] ?? null) ? $body['optional'] : []); try { $packRepo = new PackRepository($root); $rulesRepo = new RulesRepository($root); $pack = $packRepo->getPack($packId); $rules = $rulesRepo->getRules(); $result = Engine::compute($pack, $answers, $optional, $rules); (new Analytics($root))->track(Analytics::EVENT_DIA_COMPLETED, $packId); // Stockage local (toujours) + webhook si configuré $pdfBytes = (new PdfRenderer($root))->renderPdfBytes($pack, $result, [ 'booking_url' => '', 'date' => date('Y-m-d'), ]); $filename = 'controle-technique-' . ($pack['pack_id'] ?? 'resultat') . '.pdf'; $ctx = ['pack_id' => $packId, 'answers' => $answers, 'optional' => $optional, 'result' => $result]; $outbox = new OutboxStorage($root . '/storage/outbox'); $webhookUrl = (string) ($config['admin_notify_webhook'] ?? ''); if ($webhookUrl !== '' && filter_var($webhookUrl, FILTER_VALIDATE_URL)) { (new AdminNotifier($webhookUrl, $outbox))->notify('score', $ctx, $filename, $pdfBytes, null); } else { $outbox->store('score', $ctx, $filename, $pdfBytes, null); } Http::json(['result' => $result]); } catch (Throwable $e) { Http::json(['error' => $e->getMessage()], 400); }