summaryrefslogtreecommitdiff
path: root/cgi/contact.php
blob: 0f9ac5f630f23e2590efb2576b03739a6e94754e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php

use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;

session_start();

function missing_field(string $name): never {
    http_response_code(400);
    echo "Missing or malformed field '$name'\n";
    exit;
}

function getenv_or_fail(string $key): ?string {
    $value = getenv($key);
    if ($value !== false)
        return $value;
    http_response_code(500);
    echo "Missing environment variable '$key'\n";
    exit;
}

if ($_SERVER["REQUEST_METHOD"] !== "POST") {
    http_response_code(405);
    echo "Invalid request method!\n";
    exit;
}

$name = $_POST["name"] ?? missing_field("name");
if (empty($name) || strlen($name) > 128)
    missing_field("name");

$email = $_POST["email"] ?? missing_field("email");
if (empty($email) || strlen($email) > 128 || !str_contains($email, "@"))
    missing_field("email");

$subject = $_POST["subject"] ?? missing_field("subject");
if (empty($email) || strlen($subject) > 256)
    missing_field("subject");

$message = $_POST["message"] ?? missing_field("message");
if (empty($email) || strlen($message) > 16384)
    missing_field("message");

$captcha = $_POST["captcha"] ?? missing_field("captcha");
if (empty($email) || strlen($captcha) > 7)
    missing_field("captcha");

$expected_captcha = $_SESSION["captcha_phrase"] ?? null;

if ($captcha !== $expected_captcha) {
    http_response_code(400);
    echo "Ung&uuml;ltiges CAPTCHA!";
    exit;
}

require_once __DIR__ . "/../vendor/autoload.php";

$mailer_dsn = getenv_or_fail("MAILER_DSN");

$transport = Transport::fromDsn($mailer_dsn);

$time = (new DateTimeImmutable("now", new DateTimeZone("Europe/Berlin")))->format("d.m.y \\u\\m H:i");

$message = implode("\n", array_map(fn($ln) => "> $ln", preg_split('~(*BSR_ANYCRLF)\R~', trim($message))));
$message = "Am $time schrieb $name via Kontaktformular:\n$message\n";

$transport->send((new Email())
    ->from(new Address("mystic-contactform+no-reply@jonaskohl.de", $name))
    ->to("mystic@jonaskohl.de")
    ->replyTo(new Address($email, $name))
    ->subject("[Mystic House Corner] $subject")
    ->text($message)
);

header("Location: /pages/sent.php");