
<?php
$eventsDir = __DIR__ . '/data/events/';
$entriesFile = __DIR__ . '/data/promo_entries.txt';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  header('Location: /events.php'); exit;
}

$name = trim((string)($_POST['name'] ?? ''));
$email = trim((string)($_POST['email'] ?? ''));
$msg = trim((string)($_POST['message'] ?? ''));
$eventTitle = trim((string)($_POST['event_title'] ?? ''));
if ($name === '' || $email === '') {
  header('Location: /events.php?err=Missing%20name%20or%20email' . ($eventTitle !== '' ? '&event=' . urlencode($eventTitle) : '')); exit;
}
$clean = function (string $value, int $max = 240): string {
  $value = str_replace(["
", "
"], ' ', $value);
  $value = preg_replace('/\s+/', ' ', $value);
  $value = trim($value);
  if (strlen($value) > $max) $value = substr($value, 0, $max);
  return $value;
};
$line = date('Y-m-d H:i') . ' | Event: ' . $clean($eventTitle, 120) . ' | ' . $clean($name, 60) . ' | ' . $clean($email, 120) . ' | ' . $clean($msg, 400) . "
";
file_put_contents($entriesFile, $line, FILE_APPEND | LOCK_EX);
header('Location: /events.php?sent=1' . ($eventTitle !== '' ? '&event=' . urlencode($eventTitle) : ''));
exit;
