2022-06-16 13:02:42 +02:00
|
|
|
<?php
|
|
|
|
$message = '';
|
|
|
|
$name = '';
|
|
|
|
$email = '';
|
|
|
|
|
|
|
|
function sanitize_text(string $name) {
|
|
|
|
$text = filter_var($_POST[$name], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
|
|
|
|
$text = trim($text);
|
|
|
|
$text = stripslashes($text);
|
|
|
|
$text = htmlspecialchars($text);
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sending email (Platzhalter)
|
|
|
|
*
|
|
|
|
* mail(): Braucht auf dem Server einen korrekt konfigurierten Mailserver
|
|
|
|
* phpmailer: Bibliothek, der per Composer installiert wird. Tut ganz gut mit SMTP.
|
|
|
|
*/
|
|
|
|
function send_message_to_office($message, $name, $email) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function send_response($response_data) {
|
|
|
|
$json = json_encode($response_data);
|
|
|
|
if ($json === false) {
|
|
|
|
// Avoid echo of empty string (which is invalid JSON), and
|
|
|
|
// JSONify the error message instead:
|
|
|
|
$json = json_encode(["jsonError" => json_last_error_msg()]);
|
|
|
|
if ($json === false) {
|
|
|
|
// This should not happen, but …
|
|
|
|
$json = '{"jsonError":"unknown"}';
|
|
|
|
}
|
|
|
|
// Set HTTP response status code to: 500 - Internal Server Error
|
|
|
|
http_response_code(500);
|
|
|
|
}
|
|
|
|
header('Content-type: application/json');
|
|
|
|
echo $json;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
|
|
$response = array();
|
|
|
|
|
2022-06-16 18:23:05 +02:00
|
|
|
if (empty($_POST['message'])) {
|
|
|
|
$response['errors'][] = 'Du hast keine Nachricht eingegeben.';
|
|
|
|
}
|
|
|
|
if (empty($_POST['email'])) {
|
|
|
|
$response['errors'][] = 'Du hast keine E-Mail-Adresse eingegeben.';
|
|
|
|
}
|
|
|
|
if (empty($_POST['name'])) {
|
|
|
|
$response['errors'][] = 'Du hast keinen Namen eingegeben.';
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Idee zur Bot-Erkennung:
|
|
|
|
* 1. Ein Bot hat das Pseudocaptcha entweder leer abgeschickt, oder sich selbst etwas ausgedacht.
|
|
|
|
* 2. Ein Bot schickt die Daten in unter 5s ab.
|
|
|
|
* 3. Ein Mensch braucht nicht länger als 60min.
|
|
|
|
*/
|
2022-06-16 13:02:42 +02:00
|
|
|
if (
|
2022-06-16 18:23:05 +02:00
|
|
|
$_POST['captcha'] != 'Nudelsuppe' or
|
|
|
|
preg_match('/\d{10}/', $_POST['time_sent']) != 1 or
|
|
|
|
time() - intval($_POST['time_sent']) < 5 or
|
|
|
|
time() - intval($_POST['time_sent']) > 3600
|
2022-06-16 13:02:42 +02:00
|
|
|
) {
|
2022-06-16 18:23:05 +02:00
|
|
|
$response['errors'][] = 'Wir glauben du bist ein Bot.';
|
|
|
|
}
|
|
|
|
if (!array_key_exists('errors', $response)) {
|
2022-06-16 13:02:42 +02:00
|
|
|
$message = sanitize_text('message');
|
|
|
|
$name = sanitize_text('name');
|
|
|
|
$email = sanitize_text('email');
|
|
|
|
|
|
|
|
if (!send_message_to_office($message, $name, $email)) {
|
|
|
|
$response['errors'][] = 'Deine Nachricht konnte nicht übermittelt werden.';
|
|
|
|
} else {
|
|
|
|
$response['status'] = 'ok';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
send_response($response);
|
|
|
|
} else {
|
|
|
|
http_response_code(404);
|
|
|
|
}
|