notify.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. declare(strict_types=1);
  3. use AuditExpress\Http;
  4. use AuditExpress\RateLimiter;
  5. use AuditExpress\Security;
  6. ['root' => $root, 'config' => $config] = require __DIR__ . '/_bootstrap.php';
  7. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  8. Http::methodNotAllowed(['POST']);
  9. exit;
  10. }
  11. Security::validateCsrf();
  12. (new RateLimiter($root, $config, RateLimiter::clientIp()))->check('notify');
  13. $maxBody = (int) ($config['max_body_bytes'] ?? 1_048_576);
  14. $body = Http::readJsonBody($maxBody > 0 ? $maxBody : null);
  15. $email = (string) ($body['email'] ?? '');
  16. $tool = (string) ($body['tool'] ?? '');
  17. $allowedTools = (array) ($config['allowed_notify_tools'] ?? ['process-mapper']);
  18. if ($tool === '' || !in_array($tool, $allowedTools, true)) {
  19. Http::json(['error' => 'Outil invalide ou non autorisé'], 400);
  20. exit;
  21. }
  22. if ($email === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  23. Http::json(['error' => 'Email invalide'], 400);
  24. exit;
  25. }
  26. $outboxDir = $root . '/storage/outbox';
  27. if (!is_dir($outboxDir)) {
  28. @mkdir($outboxDir, 0775, true);
  29. }
  30. $id = date('Ymd_His') . '_' . bin2hex(random_bytes(4));
  31. $meta = [
  32. 'id' => $id,
  33. 'type' => 'notify_interest',
  34. 'tool' => $tool,
  35. 'email' => $email,
  36. 'created_at' => date(DATE_ATOM),
  37. ];
  38. file_put_contents(
  39. $outboxDir . DIRECTORY_SEPARATOR . $id . '_notify.json',
  40. json_encode($meta, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
  41. );
  42. Http::json(['status' => 'ok']);