Something went wrong. Try again.
Website template for structured, accessible static websites, built with HTML, CSS, TypeScript and Vite. spa-template.liberacode.dev
Something went wrong. Try again.
spa-template contact.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271<?phpdeclare(strict_types=1);
ini_set("display_errors", "0");
header("Content-Type: application/json; charset=utf-8");header("X-Content-Type-Options: nosniff");header("X-Frame-Options: SAMEORIGIN");header("Referrer-Policy: strict-origin-when-cross-origin");header("Cache-Control: no-store");
require_once __DIR__ . "/includes/utils.php";require_once __DIR__ . "/includes/altcha.php";
$config = load_contact_config(__DIR__ . "/contact-config.php");
if (!file_exists(__DIR__ . "/vendor/autoload.php")) { server_error($config, "Missing vendor/autoload.php");}
require_once __DIR__ . "/vendor/autoload.php";
if ($_SERVER["REQUEST_METHOD"] !== "POST") { json_error(405, [ "success" => false, "error" => "This request method is not allowed.", ]);}
$contentType = $_SERVER["CONTENT_TYPE"] ?? "";$input = null;
if (stripos($contentType, "application/json") !== false) { $raw = file_get_contents("php://input"); check_input_size($config, ["raw" => $raw]); $decoded = json_decode($raw, true);
if (is_array($decoded)) { $input = $decoded; }} else { if (!empty($_POST) && is_array($_POST)) { check_input_size($config, $_POST); $input = $_POST; }}
if (!$input || !is_array($input)) { json_error(400, [ "success" => false, "error" => "Invalid request format.", ]);}
$ip = get_client_ip();rate_limit_or_fail($config, $ip);
$honeypot = normalize_text($input["website"] ?? "");
if ($honeypot !== "") { log_event($config, "Honeypot triggered", ["ip" => $ip]);
json_error(400, [ "success" => false, "error" => "Invalid form submission.", ]);}
$name = normalize_text($input["name"] ?? "");$telephone = normalize_text($input["telephone"] ?? "");$email = normalize_text($input["email"] ?? "");$message = normalize_text($input["message"] ?? "");$privacy = trim((string) ($input["privacy"] ?? ""));$altchaPayload = trim((string) ($input["altcha"] ?? ""));
$name = str_replace(["\r", "\n"], " ", $name);$email = str_replace(["\r", "\n"], "", $email);
$errors = [];
if (strlen($name) < 2) { $errors["name"] = "Please enter a valid name.";} elseif (text_length($name) > 100) { $errors["name"] = "Please enter no more than 100 characters.";}
if (!is_valid_email($email)) { $errors["email"] = "Please enter a valid email address.";}
if ($telephone !== "" && !preg_match('/^[0-9+()\\-\\.\\s]{3,30}$/', $telephone)) { $errors["telephone"] = "Please enter a valid telephone number.";}
if (strlen($message) < 10) { $errors["message"] = "The message must contain at least 10 characters.";} elseif (text_length($message) > 5000) { $errors["message"] = "Please enter no more than 5000 characters.";}
$privacyAccepted = in_array( strtolower($privacy), ["yes", "on", "1", "true"], true,);
if (!$privacyAccepted) { $errors["privacy"] = "Please confirm that you have read the privacy policy.";}
if ($altchaPayload === "") { $errors["altcha"] = "Invalid captcha verification.";}
if (!empty($errors)) { json_error(400, [ "success" => false, "errors" => $errors, ]);}
if (!verify_altcha_payload($altchaPayload, $config)) { json_error(400, [ "success" => false, "errors" => [ "altcha" => "Captcha verification failed.", ], ]);}
use PHPMailer\PHPMailer\Exception;use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);$claimed = false;
try { $smtpHost = trim((string) ($config["smtp_host"] ?? "")); $smtpUsername = trim((string) ($config["smtp_username"] ?? "")); $smtpPassword = (string) ($config["smtp_password"] ?? ""); $smtpPort = (int) ($config["smtp_port"] ?? 587); $smtpEncryption = strtolower( trim((string) ($config["smtp_encryption"] ?? "starttls")), ); $fromEmail = trim((string) ($config["from_email"] ?? "")); $fromName = trim((string) ($config["from_name"] ?? "Website Contact Form")); $toEmail = trim((string) ($config["to_email"] ?? "")); $toName = trim((string) ($config["to_name"] ?? "Admin"));
if ( $smtpHost === "" || $smtpUsername === "" || $smtpPassword === "" || $fromEmail === "" || $toEmail === "" ) { server_error($config, "Missing SMTP configuration"); }
$mail->isSMTP(); $mail->Host = $smtpHost; $mail->SMTPAuth = true; $mail->Username = $smtpUsername; $mail->Password = $smtpPassword; $mail->Port = $smtpPort; $mail->CharSet = "UTF-8";
if ($smtpEncryption === "ssl") { $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; } elseif ($smtpEncryption === "tls" || $smtpEncryption === "starttls") { $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; } elseif ($smtpEncryption === "none") { $mail->SMTPSecure = false; $mail->SMTPAutoTLS = false; } else { server_error($config, "Invalid SMTP encryption setting", ["value" => $smtpEncryption]); }
$mail->setFrom($fromEmail, $fromName); $mail->addAddress($toEmail, $toName); $mail->addReplyTo($email, $name);
$safeName = sanitize_html($name); $safeEmail = sanitize_html($email); $safeTelephone = sanitize_html($telephone); $safeMessage = nl2br(sanitize_html($message)); $headerColor = mail_header_color($config);
$htmlMessage = ' <table width="100%" cellpadding="0" cellspacing="0" style="font-family:Arial, sans-serif; background:#f7f7f7; padding:20px;"> <tr><td align="center"> <table width="600" cellpadding="20" cellspacing="0" style="background:white; border-radius:8px;"> <tr> <td style="text-align:center; background:' . $headerColor . '; color:white; border-radius:8px 8px 0 0;"> <h2 style="margin:0; padding:10px 0;">New website contact message</h2> </td> </tr> <tr> <td> <p><strong>Name:</strong> ' . $safeName . '</p> <p><strong>Email:</strong> ' . $safeEmail . '</p> <p><strong>Telephone:</strong> ' . $safeTelephone . '</p> <p><strong>Message:</strong><br><br>' . $safeMessage . '</p> </td> </tr> <tr> <td style="font-size:12px; color:#666; text-align:center;"> This email was sent automatically via the contact form on your website. </td> </tr> </table> </td></tr> </table>';
$plainTextMessage = "New website contact message\n\n" . "Name: {$name}\n" . "Email: {$email}\n" . "Telephone: {$telephone}\n\n" . "Message:\n{$message}\n";
$mail->isHTML(true); $mail->Subject = "New contact request from {$name}"; $mail->Body = $htmlMessage; $mail->AltBody = $plainTextMessage;
$claimed = claim_altcha_payload($altchaPayload, $config);
if (!$claimed) { json_error(400, [ "success" => false, "errors" => [ "altcha" => "Captcha verification has already been used.", ], ]); }
$mail->send();
echo json_encode( [ "success" => true, "message" => "Your message was sent successfully.", ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES, ); exit();} catch (StorageException $e) { server_error($config, "ALTCHA replay store not writable", ["error" => $e->getMessage()]);} catch (Throwable $e) { if ($claimed) { release_altcha_payload($altchaPayload, $config); }
server_error($config, "Email delivery failed", [ "ip" => $ip, "error" => $e->getMessage(), ]);}