$membership ? (int) $membership['id'] : null, 'membership_name' => $membership ? $membership['name'] : '', 'name' => trim((string) ($_POST['nome'] ?? '')), 'phone' => trim((string) ($_POST['telefone'] ?? '')), 'email' => trim((string) ($_POST['email'] ?? '')), 'message' => trim((string) ($_POST['mensagem'] ?? '')), 'consent' => !empty($_POST['consent']) ? 1 : 0, 'consent_text' => $consent_text, // First hop of X-Forwarded-For (behind the reverse proxy), else peer. 'ip' => trim(explode(',', (string) ($_SERVER['HTTP_X_FORWARDED_FOR'] ?? ''))[0]) ?: ($_SERVER['REMOTE_ADDR'] ?? ''), 'user_agent' => (string) ($_SERVER['HTTP_USER_AGENT'] ?? ''), ]; if ($lead['name'] === '' || $lead['phone'] === '' || !$lead['consent'] || ($lead['email'] !== '' && !filter_var($lead['email'], FILTER_VALIDATE_EMAIL))) { inscricao_redirect('erro'); } try { db_membership_lead_create($pdo, $lead); } catch (Throwable $e) { error_log('[inscricao] ' . $e->getMessage()); inscricao_redirect('erro'); } // Notify the gym. Best effort: the request is already stored. try { $info = site_info($pdo); $to = $info['email'] !== '' ? $info['email'] : EMAIL_FROM; $e = static fn($v) => htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8'); $rows = [ 'Mensalidade' => $lead['membership_name'], 'Nome' => $lead['name'], 'Telemóvel' => $lead['phone'], 'E-mail' => $lead['email'], 'Mensagem' => $lead['message'], ]; $body = '

Novo pedido de inscrição

'; $alt = "Novo pedido de inscrição\n\n"; foreach ($rows as $label => $value) { if (trim((string) $value) === '') continue; $body .= ''; $alt .= "$label: $value\n"; } $body .= '
' . $e($label) . ':' . nl2br($e($value)) . '

' . 'Pedido guardado no backoffice (Mensalidades → Pedidos de inscrição).

'; $id = enqueue_email([ 'to_address' => $to, 'to_name' => $info['name'] !== '' ? $info['name'] : APP_NAME, 'subject' => 'Pedido de inscrição' . ($lead['membership_name'] !== '' ? ' (' . $lead['membership_name'] . ')' : '') . ' — ' . $lead['name'], 'body' => $body, 'alt_body' => $alt, 'context_type' => 'fitme_inscricao', ]); flush_message($id); } catch (Throwable $e) { error_log('[inscricao] email: ' . $e->getMessage()); } inscricao_redirect('ok');