79 lines
3.3 KiB
PHP
79 lines
3.3 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/app.php';
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header('Location: index.php#lead-form');
|
|
exit;
|
|
}
|
|
|
|
function back_with_error(string $message): never
|
|
{
|
|
header('Location: index.php?error=' . urlencode($message) . '#lead-form');
|
|
exit;
|
|
}
|
|
|
|
$name = trim((string)($_POST['name'] ?? ''));
|
|
$email = trim((string)($_POST['email'] ?? ''));
|
|
$company = trim((string)($_POST['company'] ?? ''));
|
|
$budget = trim((string)($_POST['budget'] ?? ''));
|
|
$message = trim((string)($_POST['message'] ?? ''));
|
|
$honeypot = trim((string)($_POST['website'] ?? ''));
|
|
|
|
if ($honeypot !== '') {
|
|
header('Location: thank-you.php');
|
|
exit;
|
|
}
|
|
if ($name === '' || strlen($name) > 120) {
|
|
back_with_error('Name is required and must be under 120 characters.');
|
|
}
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($email) > 190) {
|
|
back_with_error('A valid email address is required.');
|
|
}
|
|
if (strlen($message) < 10 || strlen($message) > 2000) {
|
|
back_with_error('Message must be between 10 and 2000 characters.');
|
|
}
|
|
if (strlen($company) > 160 || strlen($budget) > 80) {
|
|
back_with_error('One of the optional fields is too long.');
|
|
}
|
|
|
|
try {
|
|
ensure_leads_table();
|
|
$token = bin2hex(random_bytes(16));
|
|
$ip = $_SERVER['REMOTE_ADDR'] ?? null;
|
|
$agent = substr((string)($_SERVER['HTTP_USER_AGENT'] ?? ''), 0, 255);
|
|
|
|
$stmt = db()->prepare('INSERT INTO leads (public_token, name, email, company, budget, message, source, ip_address, user_agent) VALUES (:token, :name, :email, :company, :budget, :message, :source, :ip, :agent)');
|
|
$stmt->bindValue(':token', $token);
|
|
$stmt->bindValue(':name', $name);
|
|
$stmt->bindValue(':email', $email);
|
|
$stmt->bindValue(':company', $company !== '' ? $company : null);
|
|
$stmt->bindValue(':budget', $budget !== '' ? $budget : null);
|
|
$stmt->bindValue(':message', $message);
|
|
$stmt->bindValue(':source', 'Landing page');
|
|
$stmt->bindValue(':ip', $ip);
|
|
$stmt->bindValue(':agent', $agent);
|
|
$stmt->execute();
|
|
$leadId = (int)db()->lastInsertId();
|
|
|
|
$safeName = e($name);
|
|
$safeEmail = e($email);
|
|
$safeMessage = nl2br(e($message));
|
|
$html = "<h2>New landing page lead</h2><p><strong>Name:</strong> {$safeName}</p><p><strong>Email:</strong> {$safeEmail}</p><p><strong>Company:</strong> " . e($company ?: 'Not provided') . "</p><p><strong>Budget:</strong> " . e($budget ?: 'Not sure') . "</p><p><strong>Message:</strong><br>{$safeMessage}</p>";
|
|
$text = "New landing page lead\nName: {$name}\nEmail: {$email}\nCompany: " . ($company ?: 'Not provided') . "\nBudget: " . ($budget ?: 'Not sure') . "\n\n{$message}";
|
|
$mailResult = MailService::sendMail(null, 'New landing page lead from ' . $name, $html, $text, ['reply_to' => $email]);
|
|
if (!empty($mailResult['success'])) {
|
|
$update = db()->prepare('UPDATE leads SET email_sent = 1 WHERE id = :id');
|
|
$update->bindValue(':id', $leadId, PDO::PARAM_INT);
|
|
$update->execute();
|
|
} else {
|
|
error_log('Lead notification email failed: ' . ($mailResult['error'] ?? 'unknown error'));
|
|
}
|
|
|
|
header('Location: thank-you.php?token=' . urlencode($token));
|
|
exit;
|
|
} catch (Throwable $exception) {
|
|
error_log('Lead submission failed: ' . $exception->getMessage());
|
|
back_with_error('We could not save your request right now. Please try again in a moment.');
|
|
}
|