- JS setzte seine eigenen Validierungshinweise erst nach checkValidity() zurueck; eine einmal gemeldete Telefonnummer blockierte damit jeden weiteren Versuch. Hinweise werden jetzt vor der Pruefung geleert - Telefonpruefung zaehlt nur noch die Ziffern (>= 6) und akzeptiert damit Schreibweisen wie 0172 6384350, +49 (0)172 6384350 oder 0172/6384350 - Client und Server (app/inquiry.php) pruefen identisch - Asset-Query auf v=3 und Service-Worker-Cache auf dic-v19
257 lines
9.5 KiB
PHP
257 lines
9.5 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
/**
|
||
* Öffentliches Anfrageformular der Startseite (web/index.html).
|
||
*
|
||
* Die Anfrage wird serverseitig per E-Mail an den Betrieb geschickt, damit
|
||
* Besucher kein E-Mail-Programm mehr brauchen (vorher `mailto:`). Der Endpunkt
|
||
* ist ohne Anmeldung erreichbar und wird deshalb durch einen Honeypot und ein
|
||
* einfaches IP-Limit gegen Spam geschützt.
|
||
*/
|
||
|
||
/** Maximale Anfragen pro IP und Stunde. */
|
||
const INQUIRY_MAX_PER_HOUR = 5;
|
||
/** Empfänger: app.inquiry_email, sonst die E-Mail aus den Betreiberangaben. */
|
||
function inquiry_recipient(): string
|
||
{
|
||
$configured = trim((string) config('app.inquiry_email', ''));
|
||
if ($configured !== '' && filter_var($configured, FILTER_VALIDATE_EMAIL) !== false) {
|
||
return $configured;
|
||
}
|
||
$legal = legal_data();
|
||
$fallback = trim((string) ($legal['email'] ?? ''));
|
||
return filter_var($fallback, FILTER_VALIDATE_EMAIL) !== false ? $fallback : 'drinkitcold@web.de';
|
||
}
|
||
|
||
function inquiry_throttle_file(): string
|
||
{
|
||
$storage = rtrim((string) config('app.storage_path', DIC_ROOT . '/storage'), '/');
|
||
return $storage . '/inquiry-rate.json';
|
||
}
|
||
|
||
/**
|
||
* true, wenn die IP ihr Stundenkontingent verbraucht hat. Mit $count = true
|
||
* wird der Aufruf zusätzlich gezählt. Scheitert der Dateizugriff, wird nicht
|
||
* blockiert (das Formular darf nie an einer Dateirechte-Frage scheitern).
|
||
*/
|
||
function inquiry_rate_limited(string $ip, bool $count = false, int $max = INQUIRY_MAX_PER_HOUR, int $window = 3600): bool
|
||
{
|
||
// Nur ein Hash der IP landet auf der Platte (Spam-Schutz, max. eine Stunde).
|
||
$ip = hash('sha256', $ip);
|
||
$handle = @fopen(inquiry_throttle_file(), 'c+');
|
||
if ($handle === false) {
|
||
return false;
|
||
}
|
||
$limited = false;
|
||
if (flock($handle, LOCK_EX)) {
|
||
$now = time();
|
||
$decoded = json_decode((string) stream_get_contents($handle), true);
|
||
$data = is_array($decoded) ? $decoded : [];
|
||
$clean = [];
|
||
foreach ($data as $key => $stamps) {
|
||
$recent = [];
|
||
foreach ((array) $stamps as $stamp) {
|
||
if (is_int($stamp) && $stamp > $now - $window) {
|
||
$recent[] = $stamp;
|
||
}
|
||
}
|
||
if ($recent !== []) {
|
||
$clean[(string) $key] = $recent;
|
||
}
|
||
}
|
||
$stamps = $clean[$ip] ?? [];
|
||
$limited = count($stamps) >= $max;
|
||
if (!$limited && $count) {
|
||
$stamps[] = $now;
|
||
$clean[$ip] = $stamps;
|
||
}
|
||
ftruncate($handle, 0);
|
||
rewind($handle);
|
||
fwrite($handle, (string) json_encode($clean));
|
||
fflush($handle);
|
||
flock($handle, LOCK_UN);
|
||
}
|
||
fclose($handle);
|
||
return $limited;
|
||
}
|
||
|
||
/** Einzeilige Angaben ohne Steuerzeichen, damit der Mailkopf sauber bleibt. */
|
||
function inquiry_line(string $value, int $maxLength = 160): string
|
||
{
|
||
$value = preg_replace('/[\r\n\t]+/', ' ', $value) ?? '';
|
||
$value = trim(preg_replace('/\s{2,}/', ' ', $value) ?? '');
|
||
return mb_substr($value, 0, $maxLength);
|
||
}
|
||
|
||
function inquiry_date(string $value): ?string
|
||
{
|
||
$date = DateTimeImmutable::createFromFormat('!Y-m-d', $value);
|
||
if (!$date instanceof DateTimeImmutable || $date->format('Y-m-d') !== $value) {
|
||
return null;
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* Prüft und verschickt eine Anfrage.
|
||
*
|
||
* @return array{status:int, ok:bool, message:string}
|
||
*/
|
||
function inquiry_submit(array $input, string $ip): array
|
||
{
|
||
$get = static function (string $key) use ($input): string {
|
||
$value = $input[$key] ?? '';
|
||
return is_string($value) ? $value : '';
|
||
};
|
||
|
||
// Honeypot: Bots füllen das unsichtbare Feld. Antwort wie beim Erfolg,
|
||
// damit der Bot nichts über die Abwehr lernt.
|
||
if (trim($get('website')) !== '') {
|
||
error_log('Drink it cold: Anfrage-Honeypot ausgelöst (IP ' . $ip . ')');
|
||
return ['status' => 200, 'ok' => true, 'message' => inquiry_success_message()];
|
||
}
|
||
|
||
if (inquiry_rate_limited($ip)) {
|
||
return [
|
||
'status' => 429,
|
||
'ok' => false,
|
||
'message' => 'Es sind bereits mehrere Anfragen von dieser Verbindung eingegangen. '
|
||
. 'Bitte versuchen Sie es in einer Stunde erneut oder rufen Sie uns an.',
|
||
];
|
||
}
|
||
|
||
$values = [
|
||
'name' => inquiry_line($get('name'), 120),
|
||
'phone' => inquiry_line($get('phone'), 40),
|
||
'email' => inquiry_line($get('email'), 180),
|
||
'street' => inquiry_line($get('street'), 120),
|
||
'city' => inquiry_line($get('city'), 120),
|
||
'tariff' => inquiry_line($get('tariff'), 60),
|
||
'handover' => inquiry_line($get('handover'), 80),
|
||
'message' => trim(mb_substr($get('message'), 0, 4000)),
|
||
];
|
||
$from = inquiry_date(inquiry_line($get('from'), 10));
|
||
$to = inquiry_date(inquiry_line($get('to'), 10));
|
||
|
||
$errors = [];
|
||
$labels = [
|
||
'name' => 'Vor- und Nachname',
|
||
'phone' => 'Telefonnummer',
|
||
'email' => 'E-Mail-Adresse',
|
||
'street' => 'Straße',
|
||
'city' => 'PLZ & Ort',
|
||
'from' => 'Miete von',
|
||
'to' => 'Miete bis',
|
||
];
|
||
if ($values['name'] === '') {
|
||
$errors[] = $labels['name'];
|
||
}
|
||
// Nur die Ziffern zählen: 0172 6384350, +49 172 638 43 50 und
|
||
// 0172/6384350 sind alle gültig, "abc" oder "123" nicht.
|
||
if (strlen((string) preg_replace('/[^0-9]/', '', $values['phone'])) < 6) {
|
||
$errors[] = $labels['phone'];
|
||
}
|
||
if (filter_var($values['email'], FILTER_VALIDATE_EMAIL) === false) {
|
||
$errors[] = $labels['email'];
|
||
}
|
||
if ($values['street'] === '') {
|
||
$errors[] = $labels['street'];
|
||
}
|
||
if ($values['city'] === '') {
|
||
$errors[] = $labels['city'];
|
||
}
|
||
if ($from === null) {
|
||
$errors[] = $labels['from'];
|
||
}
|
||
if ($to === null) {
|
||
$errors[] = $labels['to'];
|
||
}
|
||
if ($from !== null && $to !== null && $to < $from) {
|
||
return [
|
||
'status' => 422,
|
||
'ok' => false,
|
||
'message' => 'Das Ende der Miete liegt vor dem Beginn. Bitte prüfen Sie den Zeitraum.',
|
||
];
|
||
}
|
||
if ($errors !== []) {
|
||
return [
|
||
'status' => 422,
|
||
'ok' => false,
|
||
'message' => 'Bitte prüfen Sie folgende Pflichtfelder: ' . implode(', ', $errors) . '.',
|
||
];
|
||
}
|
||
|
||
inquiry_rate_limited($ip, true);
|
||
|
||
$subject = 'Kühlanhänger-Anfrage ' . fmt_date($from) . ' – ' . fmt_date($to);
|
||
$body = "Hallo Drink it cold,\n\n"
|
||
. "über drinkitcold.de ist eine neue Terminanfrage eingegangen:\n\n"
|
||
. 'Name: ' . $values['name'] . "\n"
|
||
. 'Telefon: ' . $values['phone'] . "\n"
|
||
. 'E-Mail: ' . $values['email'] . "\n"
|
||
. 'Straße: ' . $values['street'] . "\n"
|
||
. 'PLZ & Ort: ' . $values['city'] . "\n"
|
||
. 'Miete von: ' . fmt_date($from) . "\n"
|
||
. 'Miete bis: ' . fmt_date($to) . "\n"
|
||
. 'Tarif: ' . ($values['tariff'] !== '' ? $values['tariff'] : 'keine Angabe') . "\n"
|
||
. 'Übergabe: ' . ($values['handover'] !== '' ? $values['handover'] : 'keine Angabe') . "\n";
|
||
if ($values['message'] !== '') {
|
||
$body .= "\nNachricht:\n" . $values['message'] . "\n";
|
||
}
|
||
$body .= "\nAbsender: " . $values['name'] . ' <' . $values['email'] . ">\n"
|
||
. 'IP: ' . $ip . "\n"
|
||
. 'Zeitpunkt: ' . fmt_date_with_time(date('Y-m-d'), date('H:i')) . "\n";
|
||
|
||
$replyTo = 'Reply-To: ' . mb_encode_mimeheader($values['name'], 'UTF-8', 'B')
|
||
. ' <' . $values['email'] . '>';
|
||
|
||
if (!mail_send(inquiry_recipient(), $subject, $body, [$replyTo])) {
|
||
error_log('Drink it cold: Anfrage-Versand fehlgeschlagen (' . $values['email'] . ', IP ' . $ip . ')');
|
||
return [
|
||
'status' => 502,
|
||
'ok' => false,
|
||
'message' => 'Die Anfrage konnte gerade nicht versendet werden. '
|
||
. 'Bitte schreiben Sie uns direkt an ' . inquiry_recipient() . ' oder rufen Sie an.',
|
||
];
|
||
}
|
||
|
||
return ['status' => 200, 'ok' => true, 'message' => inquiry_success_message()];
|
||
}
|
||
|
||
function inquiry_success_message(): string
|
||
{
|
||
return 'Vielen Dank! Ihre Anfrage ist bei uns eingegangen – wir melden uns per E-Mail oder Telefon bei Ihnen.';
|
||
}
|
||
|
||
/** POST /anfrage: verschickt die Anfrage und antwortet als JSON bzw. HTML. */
|
||
function page_inquiry(string $method): void
|
||
{
|
||
if ($method !== 'POST') {
|
||
redirect('/#anfrage');
|
||
}
|
||
|
||
$result = inquiry_submit($_POST, client_ip());
|
||
|
||
if (wants_json()) {
|
||
json_response([
|
||
'ok' => $result['ok'],
|
||
'message' => $result['message'],
|
||
], $result['status']);
|
||
}
|
||
|
||
http_response_code($result['status']);
|
||
header('Content-Type: text/html; charset=utf-8');
|
||
$title = $result['ok'] ? 'Anfrage gesendet' : 'Anfrage nicht gesendet';
|
||
echo '<!doctype html><html lang="de"><meta charset="utf-8">'
|
||
. '<meta name="viewport" content="width=device-width, initial-scale=1">'
|
||
. '<title>' . e($title) . ' · Drink it cold</title>'
|
||
. '<body style="font-family:system-ui,sans-serif;background:#f4f9fe;color:#0e1b26;padding:2.5rem 1.25rem">'
|
||
. '<main style="max-width:34rem;margin:0 auto;background:#fff;border:1px solid #d7e7f5;'
|
||
. 'border-radius:16px;padding:1.75rem 1.5rem">'
|
||
. '<h1 style="font-size:1.25rem;margin:0 0 .75rem">' . e($title) . '</h1>'
|
||
. '<p style="line-height:1.55;margin:0 0 1.25rem">' . e($result['message']) . '</p>'
|
||
. '<p style="margin:0;font-size:.9rem"><a style="color:#196abc" href="/#anfrage">Zurück zum Formular</a>'
|
||
. ' · <a style="color:#196abc" href="tel:+491726384350">+49 172 638 43 50</a></p>'
|
||
. '</main></body></html>';
|
||
}
|