/agendar); the old /home/agendar // route has no ?gym and falls back to the default gym. $gym = site_gym($pdo) ?? db_gyms_default($pdo) ?? gym_fallback(); $GLOBALS['_gym'] = $gym; // Only POST is meaningful here; anything else just goes to the gym's page. if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') { header('Location: ' . gym_url($gym)); exit; } csrf_check(); // 403s and exits on a bad/missing token function agendar_redirect(string $status): void { header('Location: ' . gym_url(current_gym(), '?agendar=' . $status . '#contactos')); exit; } $nome = trim((string) ($_POST['nome'] ?? '')); $email = trim((string) ($_POST['email'] ?? '')); $telefone = trim((string) ($_POST['telefone'] ?? '')); $data = trim((string) ($_POST['data'] ?? '')); $periodo = trim((string) ($_POST['periodo'] ?? '')); $mensagem = trim((string) ($_POST['mensagem'] ?? '')); if ($nome === '' || $telefone === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) { agendar_redirect('erro'); } // Pretty-print the preferred date if it parses; otherwise leave it blank. $data_fmt = ''; if ($data !== '') { $d = DateTime::createFromFormat('Y-m-d', $data); $data_fmt = ($d && $d->format('Y-m-d') === $data) ? $d->format('d/m/Y') : ''; } $info = site_info($pdo, (int) $gym['id']); $to = $info['email'] !== '' ? $info['email'] : EMAIL_FROM; $e = static fn($v) => htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8'); $rows = [ 'Nome' => $nome, 'Telemóvel' => $telefone, 'E-mail' => $email, 'Dia preferido' => $data_fmt, 'Período preferido'=> $periodo, 'Mensagem' => $mensagem, ]; $body = '
| ' . $e($label) . ': | ' . nl2br($e($value)) . ' |
' . 'Pedido enviado através do formulário do site ' . $e($gym['name']) . '. Responde diretamente a ' . $e($email) . ' ou liga para ' . $e($telefone) . '.
'; $alt = "Novo pedido de aula experimental\n\n"; foreach ($rows as $label => $value) { if (trim((string) $value) === '') continue; $alt .= "$label: $value\n"; } $id = enqueue_email([ 'to_address' => $to, 'to_name' => $info['name'] !== '' ? $info['name'] : $gym['name'], 'subject' => 'Pedido de aula experimental — ' . $nome, 'body' => $body, 'alt_body' => $alt, 'context_type' => 'fitme_trial', ]); agendar_redirect(flush_message($id) ? 'ok' : 'erro');