54 lines
1.9 KiB
PHP
54 lines
1.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
session_start();
|
|
|
|
require_once __DIR__ . '/includes/leads.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header('Location: /#quote');
|
|
exit;
|
|
}
|
|
|
|
$token = (string)($_POST['csrf'] ?? '');
|
|
if ($token === '' || empty($_SESSION['lead_csrf']) || !hash_equals((string)$_SESSION['lead_csrf'], $token)) {
|
|
$_SESSION['lead_flash'] = ['type' => 'danger', 'message' => 'The form expired. Please try again.'];
|
|
header('Location: /#quote');
|
|
exit;
|
|
}
|
|
|
|
[$data, $errors] = validate_lead_payload($_POST);
|
|
if ($errors) {
|
|
$_SESSION['lead_errors'] = $errors;
|
|
$_SESSION['lead_old'] = $data;
|
|
header('Location: /#quote');
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$leadId = create_lead($data);
|
|
$_SESSION['lead_csrf'] = bin2hex(random_bytes(32));
|
|
|
|
$mailStatus = 'not_configured';
|
|
$mailPath = __DIR__ . '/mail/MailService.php';
|
|
if (is_file($mailPath)) {
|
|
require_once $mailPath;
|
|
$emailBody = "Service: {$data['service']}\nBudget: {$data['budget']}\nTimeline: {$data['timeline']}\nCompany: {$data['company']}\nPhone: {$data['phone']}\n\n{$data['message']}\n\nLead ID: {$leadId}";
|
|
$res = MailService::sendContactMessage($data['name'], $data['email'], $emailBody, null, 'New agency quote request');
|
|
$mailStatus = !empty($res['success']) ? 'sent' : 'not_configured';
|
|
if (empty($res['success'])) {
|
|
error_log('Lead email notification skipped/failed: ' . ($res['error'] ?? 'unknown'));
|
|
}
|
|
}
|
|
|
|
$_SESSION['last_lead_id'] = $leadId;
|
|
$_SESSION['last_mail_status'] = $mailStatus;
|
|
header('Location: thank-you.php?id=' . $leadId);
|
|
exit;
|
|
} catch (Throwable $e) {
|
|
error_log('Lead save failed: ' . $e->getMessage());
|
|
$_SESSION['lead_old'] = $data;
|
|
$_SESSION['lead_flash'] = ['type' => 'danger', 'message' => 'We could not save your request yet. Please try again in a moment.'];
|
|
header('Location: /#quote');
|
|
exit;
|
|
}
|