Files
buening-website/kontakt-formular.php
T

133 lines
4.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Endpunkt für das Kontaktformular (POST /kontakt-formular.php).
*
* Ablauf:
* 1. Nur POST, sonst zurück zur Kontaktseite.
* 2. Honeypot ausgefüllt -> Bot: erfolgreich täuschen, nichts senden.
* 3. Zeitfalle: Formular muss mindestens FORM_MIN_FILL_TIME_MS alt sein
* (Zeitstempel setzt ein kleines Skript beim Laden der Kontaktseite).
* 4. ALTCHA-Lösung serverseitig nachrechnen (Proof-of-Work).
* 4. Felder prüfen.
* 4b. Betamodus (BETA_MODE in altcha-config.php): Erfolgsseite zeigen,
* aber keine Mail versenden.
* 5. Mail an info@buening-gmbh.de versenden (SMTP), Reply-To auf die
* Adresse des Absenders.
*
* Erfolg -> Weiterleitung auf /kontakt/danke/
* Fehler -> Weiterleitung auf /kontakt/fehler/
*/
require_once __DIR__ . '/altcha-config.php';
require_once __DIR__ . '/altcha-lib.php';
function redirect_to(string $path): void
{
header('Location: ' . $path, true, 303);
exit;
}
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') {
redirect_to('/kontakt/');
}
// ---------- 1) Honeypot ----------
$honeypot = trim((string) ($_POST['website'] ?? ''));
if ($honeypot !== '') {
// Bot: Erfolgsseite zeigen, aber nichts versenden.
redirect_to('/kontakt/danke/');
}
// ---------- 2) Zeitfalle ----------
$formtime = (string) ($_POST['formtime'] ?? '');
if ($formtime === '' || !ctype_digit($formtime)) {
redirect_to('/kontakt/fehler/');
}
$elapsed = round((microtime(true) * 1000)) - (int) $formtime;
if ($elapsed < FORM_MIN_FILL_TIME_MS) {
redirect_to('/kontakt/fehler/');
}
// ---------- 3) ALTCHA ----------
$altcha = (string) ($_POST['altcha'] ?? '');
if ($altcha === '' || !altcha_verify_solution(ALTCHA_HMAC_KEY, $altcha)) {
redirect_to('/kontakt/fehler/');
}
// ---------- 4) Felder prüfen ----------
$name = trim((string) ($_POST['name'] ?? ''));
$email = trim((string) ($_POST['email'] ?? ''));
$subject = trim((string) ($_POST['subject'] ?? ''));
$message = trim((string) ($_POST['message'] ?? ''));
if ($name === '' || mb_strlen($name) > 100) {
redirect_to('/kontakt/fehler/');
}
if ($email === '' || mb_strlen($email) > 254 || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
redirect_to('/kontakt/fehler/');
}
if ($subject === '' || mb_strlen($subject) > 200) {
redirect_to('/kontakt/fehler/');
}
if ($message === '' || mb_strlen($message) > 5000) {
redirect_to('/kontakt/fehler/');
}
// ---------- 4b) Betamodus ----------
// Solange BETA_MODE aktiv ist (altcha-config.php), werden gueltige
// Absendungen wie ein echter Versand behandelt (Erfolgsseite),
// es geht aber keine Mail raus – das Formular laeuft ins Leere.
// (defined()-Pruefung, damit auch eine aeltere altcha-config.php
// ohne BETA_MODE keinen Fehler wirft.)
if (defined('BETA_MODE') && BETA_MODE) {
redirect_to('/kontakt/danke/');
}
// ---------- 5) Mail versenden (SMTP über Infomaniak kSuite) ----------
// Zeilenumbrüche aus Header-relevanten Werten entfernen (Header-Injektion).
$safe_name = str_replace(["\r", "\n"], ' ', $name);
$safe_email = str_replace(["\r", "\n"], '', $email);
$body = "Name: " . $name . "\r\n"
. "E-Mail: " . $email . "\r\n"
. "Betreff: " . $subject . "\r\n"
. "Gesendet: " . date('d.m.Y H:i') . " Uhr\r\n"
. "\r\n"
. "Nachricht:\r\n"
. $message . "\r\n";
require_once __DIR__ . '/phpmailer/Exception.php';
require_once __DIR__ . '/phpmailer/PHPMailer.php';
require_once __DIR__ . '/phpmailer/SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = SMTP_HOST;
$mail->Port = SMTP_PORT;
$mail->SMTPAuth = true;
$mail->Username = SMTP_USER;
$mail->Password = SMTP_PASS;
$mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$mail->CharSet = 'UTF-8';
$mail->setFrom(FORM_SENDER, FORM_SENDER_NAME);
$mail->addAddress(FORM_RECIPIENT);
$mail->addReplyTo($safe_email, $safe_name);
$mail->Subject = FORM_SUBJECT;
$mail->Body = $body;
$mail->send();
} catch (PHPMailer\PHPMailer\Exception $e) {
redirect_to('/kontakt/fehler/');
}
redirect_to('/kontakt/danke/');