messages_post.php 869 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. require_once __DIR__ . '/../lib/identity.php';
  3. require_once __DIR__ . '/../lib/render.php';
  4. [$actor, $vendor_id] = resolve_request_actor();
  5. if ($actor === 'ICG') {
  6. $slug = $_POST['vendor'] ?? '';
  7. $v = find_vendor_by_slug($slug);
  8. if (!$v) { http_response_code(400); echo 'Bad vendor'; exit; }
  9. $vendor_id = (int) $v['id'];
  10. }
  11. $body = trim((string) ($_POST['body'] ?? ''));
  12. if ($body === '') {
  13. http_response_code(400);
  14. echo 'Empty message';
  15. exit;
  16. }
  17. if (strlen($body) > 4000) {
  18. $body = substr($body, 0, 4000);
  19. }
  20. $pdo = db();
  21. $stmt = $pdo->prepare(
  22. 'INSERT INTO messages(vendor_id, author, body) VALUES(?, ?, ?)'
  23. );
  24. $stmt->execute([$vendor_id, $actor, $body]);
  25. $new_id = (int) $pdo->lastInsertId();
  26. header('Content-Type: text/html; charset=utf-8');
  27. header('X-Msg-Id: ' . $new_id);
  28. echo render_messages($vendor_id, $new_id - 1);