excel-complete.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. declare(strict_types=1);
  3. use AuditExpress\AdminNotifier;
  4. use AuditExpress\Http;
  5. use AuditExpress\OutboxStorage;
  6. use AuditExpress\RateLimiter;
  7. use AuditExpress\Security;
  8. ['root' => $root, 'config' => $config] = require __DIR__ . '/_bootstrap.php';
  9. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  10. Http::methodNotAllowed(['POST']);
  11. exit;
  12. }
  13. Security::validateCsrf();
  14. (new RateLimiter($root, $config, RateLimiter::clientIp()))->check('excel_complete');
  15. $maxBody = (int) ($config['max_body_bytes'] ?? 1_048_576);
  16. $body = Http::readJsonBody($maxBody > 0 ? $maxBody : null);
  17. $email = isset($body['email']) ? trim((string) $body['email']) : null;
  18. $raw = is_array($body['result'] ?? null) ? $body['result'] : null;
  19. if ($raw === null || !isset($raw['level'], $raw['title'], $raw['summary'], $raw['score'], $raw['total'], $raw['actions'])) {
  20. Http::json(['error' => 'Données du verdict manquantes ou invalides'], 400);
  21. exit;
  22. }
  23. // Email optionnel : si fourni, doit être valide
  24. if ($email !== null && $email !== '') {
  25. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  26. Http::json(['error' => 'Email invalide'], 400);
  27. exit;
  28. }
  29. } else {
  30. $email = null;
  31. }
  32. $result = [
  33. 'level' => (string) $raw['level'],
  34. 'title' => (string) $raw['title'],
  35. 'summary' => (string) $raw['summary'],
  36. 'score' => (int) $raw['score'],
  37. 'total' => (int) $raw['total'],
  38. 'actions' => array_values(array_map('strval', (array) ($raw['actions'] ?? []))),
  39. 'qual' => is_array($raw['qual'] ?? null) ? $raw['qual'] : [],
  40. ];
  41. $outbox = new OutboxStorage($root . '/storage/outbox');
  42. $outbox->storeExcelComplete($result, $email);
  43. $webhookUrl = (string) ($config['admin_notify_webhook'] ?? '');
  44. if ($webhookUrl !== '' && filter_var($webhookUrl, FILTER_VALIDATE_URL)) {
  45. (new AdminNotifier($webhookUrl, $outbox))->notifyExcelComplete($result, $email);
  46. }
  47. Http::json(['status' => 'ok']);