2022-06-16 13:02:42 +02:00
|
|
|
<?php
|
2022-08-03 18:04:03 +02:00
|
|
|
session_start();
|
2022-06-16 13:02:42 +02:00
|
|
|
|
2022-08-21 14:14:32 +02:00
|
|
|
function sanitize_text(string $name, string $type) {
|
|
|
|
$filters = array(
|
|
|
|
'text' => FILTER_SANITIZE_SPECIAL_CHARS,
|
|
|
|
'email' => FILTER_SANITIZE_EMAIL,
|
|
|
|
);
|
2022-08-24 08:03:27 +02:00
|
|
|
$text = filter_var(trim($_POST[$name]), $filters[$type]);
|
2022-06-16 13:02:42 +02:00
|
|
|
$text = stripslashes($text);
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2022-08-24 08:03:27 +02:00
|
|
|
function prepare_message_body(string $message, string $name) {
|
2022-08-21 14:16:46 +02:00
|
|
|
// Replace HTML-Entities with actual carriage returns and line feeds
|
|
|
|
$message = str_replace(" ", "\r", $message);
|
2022-08-24 07:59:58 +02:00
|
|
|
$message = str_replace(" ", "\n", $message);
|
2022-08-21 14:16:46 +02:00
|
|
|
|
|
|
|
// Ensure line breaks via carriage return + line feed
|
|
|
|
$message = str_replace("\r\n", "\n", $message);
|
|
|
|
$message = str_replace("\n", "\r\n", $message);
|
|
|
|
|
|
|
|
$message = "Nachricht von: $name\r\n\r\n" . $message;
|
|
|
|
$message = base64_encode($message);
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
2022-06-16 13:02:42 +02:00
|
|
|
/**
|
2022-08-21 14:16:46 +02:00
|
|
|
* Sending email
|
2022-06-16 13:02:42 +02:00
|
|
|
*
|
|
|
|
* mail(): Braucht auf dem Server einen korrekt konfigurierten Mailserver
|
|
|
|
* phpmailer: Bibliothek, der per Composer installiert wird. Tut ganz gut mit SMTP.
|
|
|
|
*/
|
2022-08-24 08:03:54 +02:00
|
|
|
function send_message_to_office(string $subject, string $message, string $name, string $email) {
|
2022-08-24 14:35:01 +02:00
|
|
|
$returnPath = filter_var(getenv('WTF_RETURN_PATH'), FILTER_VALIDATE_EMAIL);
|
|
|
|
$to = filter_var(getenv('WTF_CONTACT_TO'), FILTER_VALIDATE_EMAIL);
|
|
|
|
|
|
|
|
if (!$returnPath || !$to) {
|
|
|
|
error_log('Address for "To" or "Return-Path" is invalid');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-03 19:32:59 +02:00
|
|
|
return mail(
|
2022-08-24 14:35:01 +02:00
|
|
|
$to,
|
2022-08-21 14:16:46 +02:00
|
|
|
"=?UTF-8?B?" . base64_encode($subject) . "?=",
|
2022-08-24 08:03:27 +02:00
|
|
|
prepare_message_body($message, $name),
|
2022-08-24 14:35:01 +02:00
|
|
|
array(
|
2022-08-03 19:32:59 +02:00
|
|
|
"From" => getenv('WTF_CONTACT_FROM'),
|
|
|
|
"Reply-To" => $email,
|
2022-08-21 14:16:46 +02:00
|
|
|
"Content-Type" => "text/plain; charset=utf-8",
|
|
|
|
"Content-Transfer-Encoding" => "base64",
|
2022-08-03 19:32:59 +02:00
|
|
|
),
|
2022-08-24 14:35:01 +02:00
|
|
|
"-f $returnPath"
|
2022-08-03 19:32:59 +02:00
|
|
|
);
|
2022-06-16 13:02:42 +02:00
|
|
|
}
|
|
|
|
|
2022-08-24 08:03:54 +02:00
|
|
|
function send_response(array $response_data) {
|
2022-06-16 13:02:42 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2022-08-03 18:04:03 +02:00
|
|
|
function prepare_response() {
|
2022-06-16 13:02:42 +02:00
|
|
|
$response = array();
|
|
|
|
|
2022-06-16 18:23:05 +02:00
|
|
|
if (empty($_POST['message'])) {
|
2023-08-06 19:34:21 +02:00
|
|
|
$response['errors'][] = 'Du hast keine Nachricht eingegeben.';
|
2022-06-16 18:23:05 +02:00
|
|
|
}
|
|
|
|
if (empty($_POST['email'])) {
|
2023-08-06 19:34:21 +02:00
|
|
|
$response['errors'][] = 'Du hast keine E-Mail-Adresse eingegeben.';
|
2022-06-16 18:23:05 +02:00
|
|
|
}
|
|
|
|
if (empty($_POST['name'])) {
|
2023-08-06 19:34:21 +02:00
|
|
|
$response['errors'][] = 'Du hast keinen Namen eingegeben.';
|
2022-08-17 21:56:57 +02:00
|
|
|
}
|
2022-08-17 21:57:29 +02:00
|
|
|
if (empty($_POST['subject'])) {
|
2023-08-06 19:34:21 +02:00
|
|
|
$response['errors'][] = 'Du hast keinen Betreff eingegeben.';
|
2022-06-16 18:23:05 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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
|
2022-08-03 18:04:03 +02:00
|
|
|
time() - $_SESSION['start_time'] < 5 or
|
|
|
|
time() - $_SESSION['start_time'] > 3600
|
2022-06-16 13:02:42 +02:00
|
|
|
) {
|
2023-08-06 19:34:21 +02:00
|
|
|
$response['errors'][] = 'Wir glauben Du bist ein Bot.';
|
2022-06-16 18:23:05 +02:00
|
|
|
}
|
|
|
|
if (!array_key_exists('errors', $response)) {
|
2022-08-21 14:14:32 +02:00
|
|
|
$subject = sanitize_text('subject', 'text');
|
|
|
|
$message = sanitize_text('message', 'text');
|
|
|
|
$name = sanitize_text('name', 'text');
|
|
|
|
$email = sanitize_text('email', 'email');
|
2022-06-16 13:02:42 +02:00
|
|
|
|
2022-08-03 19:31:06 +02:00
|
|
|
if (!send_message_to_office($subject, $message, $name, $email)) {
|
2023-08-06 19:34:21 +02:00
|
|
|
$response['errors'][] = 'Deine Nachricht konnte nicht übermittelt werden.';
|
2022-06-16 13:02:42 +02:00
|
|
|
} else {
|
|
|
|
$response['status'] = 'ok';
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 18:04:03 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
|
|
$response = array();
|
|
|
|
|
|
|
|
if (empty($_POST['action'])){
|
|
|
|
$response['errors'][] = 'Kann eigentlich nicht passieren :/';
|
|
|
|
} else {
|
|
|
|
if ($_POST['action'] == 'start_session') {
|
|
|
|
$_SESSION['start_time'] = time();
|
|
|
|
// $response['session_start_time'] = $_SESSION['start_time'];
|
|
|
|
// $response['session_id_before'] = session_id();
|
|
|
|
} elseif ($_POST['action'] == 'handle_form') {
|
|
|
|
$response = prepare_response();
|
|
|
|
session_destroy();
|
|
|
|
} else {
|
|
|
|
$response['errors'][] = 'Kann eigentlich auch nicht passieren :/';
|
|
|
|
}
|
|
|
|
}
|
2022-06-16 13:02:42 +02:00
|
|
|
send_response($response);
|
|
|
|
} else {
|
|
|
|
http_response_code(404);
|
|
|
|
}
|