39519-vm/contact_submit.php
Flatlogic Bot edfa4d1aab first
2026-04-08 10:44:00 +00:00

112 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
session_start();
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/app_helpers.php';
require_once __DIR__ . '/mail/MailService.php';
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') {
header('Location: /#contact');
exit;
}
$name = trim((string) ($_POST['name'] ?? ''));
$email = trim((string) ($_POST['email'] ?? ''));
$company = trim((string) ($_POST['company'] ?? ''));
$projectType = trim((string) ($_POST['project_type'] ?? ''));
$budget = trim((string) ($_POST['budget'] ?? ''));
$message = trim((string) ($_POST['message'] ?? ''));
$honeypot = trim((string) ($_POST['website'] ?? ''));
set_old_form([
'name' => $name,
'email' => $email,
'company' => $company,
'project_type' => $projectType,
'budget' => $budget,
'message' => $message,
]);
$errors = [];
if ($honeypot !== '') {
$errors[] = 'Spam protection was triggered.';
}
if ($name === '' || text_length($name) < 2) {
$errors[] = 'Please enter your name.';
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Please provide a valid email address.';
}
if ($message === '' || text_length($message) < 20) {
$errors[] = 'Please add at least 20 characters about your project.';
}
if ($errors !== []) {
set_flash([
'variant' => 'danger',
'title' => 'Request not sent',
'message' => $errors[0],
]);
header('Location: /#contact');
exit;
}
try {
$pdo = db();
ensure_contact_requests_table($pdo);
$stmt = $pdo->prepare(
'INSERT INTO contact_requests (name, email, company, project_type, budget, message, source_url, ip_address, user_agent)
VALUES (:name, :email, :company, :project_type, :budget, :message, :source_url, :ip_address, :user_agent)'
);
$sourceUrl = base_url() . '/#contact';
$ipAddress = substr((string) ($_SERVER['REMOTE_ADDR'] ?? ''), 0, 45);
$userAgent = substr((string) ($_SERVER['HTTP_USER_AGENT'] ?? ''), 0, 255);
$stmt->bindValue(':name', text_slice($name, 120));
$stmt->bindValue(':email', text_slice($email, 190));
$stmt->bindValue(':company', $company !== '' ? text_slice($company, 150) : null, $company !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':project_type', $projectType !== '' ? text_slice($projectType, 80) : null, $projectType !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':budget', $budget !== '' ? text_slice($budget, 80) : null, $budget !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':message', text_slice($message, 2500));
$stmt->bindValue(':source_url', $sourceUrl, PDO::PARAM_STR);
$stmt->bindValue(':ip_address', $ipAddress !== '' ? $ipAddress : null, $ipAddress !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':user_agent', $userAgent !== '' ? $userAgent : null, $userAgent !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->execute();
unset($_SESSION['old_form']);
$mailResult = MailService::sendContactMessage(
$name,
$email,
"Company: " . ($company !== '' ? $company : '—') . "\nProject type: " . ($projectType !== '' ? $projectType : '—') . "\nBudget: " . ($budget !== '' ? $budget : '—') . "\n\n" . $message,
null,
'New portfolio inquiry'
);
$messageText = 'Your request was saved to the inbox successfully.';
if (!empty($mailResult['success'])) {
$messageText .= ' A copy was also sent by email.';
} else {
$messageText .= ' Email forwarding is not configured yet, so this submission is available in the site inbox.';
}
set_flash([
'variant' => 'success',
'title' => 'Request received',
'message' => $messageText,
]);
} catch (Throwable $e) {
error_log('Contact submission failed: ' . $e->getMessage());
set_flash([
'variant' => 'danger',
'title' => 'Something went wrong',
'message' => 'The request could not be saved right now. Please try again in a moment.',
]);
}
header('Location: /#contact');
exit;