Improved contact form and responsive aspect

This commit is contained in:
N0Xx committed 2026-10-22 23:21:07 +02:00
1 parent 216077cd32
commit 98d513acb7
19 files changed
+584 -733

No files matched your search

+45 -23
View File
@@ -38,7 +38,6 @@ require $private . '/PHPMailer/SMTP.php';
// ---------- Request checks ----------
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
header('Allow: POST');
reply(405, false, 'Method not allowed.');
}
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
@@ -62,34 +61,42 @@ function field(array $in, string $key, int $max): string
{
return mb_substr(trim((string) ($in[$key] ?? '')), 0, $max);
}
$name = field($in, 'name', 100);
$first = field($in, 'first_name', 60);
$last = field($in, 'last_name', 60);
$name = trim($first . ' ' . $last);
$phone = field($in, 'phone', 30);
$email = field($in, 'email', 150);
$company = field($in, 'company', 100);
$subject = field($in, 'subject', 150);
$message = field($in, 'message', 4000);
// Header-bound fields must not contain line breaks (header injection).
foreach ([$name, $email, $company, $subject] as $v) {
foreach ([$first, $last, $email, $phone, $company, $subject] as $v) {
if (preg_match('/[\r\n]/', $v)) {
reply(400, false, 'Invalid characters in the form.');
}
}
if ($name === '' || $subject === '' || $message === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
reply(422, false, 'Fill in your name, a valid email, a subject and a message.');
if ($first === '' || $last === '' || $subject === '' || $message === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
reply(422, false, 'Fill in the required fields (*) with a valid email address.');
}
if ($phone !== '' && !preg_match('/^[0-9+().\/ -]{6,30}$/', $phone)) {
reply(422, false, 'The phone number looks invalid.');
}
if (empty($in['consent'])) {
reply(422, false, 'Please tick the consent box so I can reply to you.');
}
// ---------- Rate limit per IP ----------
$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
$store = sys_get_temp_dir() . '/portfolio-contact-' . hash('sha256', $ip . ($cfg['rate_salt'] ?? ''));
$now = time();
// Delete fingerprints older than one hour (as stated in the legal notice).
$hits = [];
// Forget visitors after one hour: delete expired rate-limit files
foreach (glob(sys_get_temp_dir() . '/portfolio-contact-*') ?: [] as $old) {
if (@filemtime($old) < $now - 3600) {
@unlink($old);
}
}
$hits = [];
if (is_readable($store)) {
foreach (explode(',', (string) file_get_contents($store)) as $t) {
if ((int) $t > $now - 3600) {
@@ -105,15 +112,15 @@ if (count($hits) >= (int) ($cfg['max_per_hour'] ?? 5)) {
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = $cfg['smtp_host'] ?? 'smtp.protonmail.ch';
$mail->Port = (int) ($cfg['smtp_port'] ?? 587);
$mail->SMTPSecure = $cfg['smtp_secure'] ?? PHPMailer::ENCRYPTION_STARTTLS;
$mail->Host = $cfg['smtp_host'] ?? 'smtp.protonmail.ch';
$mail->Port = (int) ($cfg['smtp_port'] ?? 587);
$mail->SMTPSecure = $cfg['smtp_secure'] ?? PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAutoTLS = $mail->SMTPSecure !== '';
$mail->SMTPAuth = $cfg['smtp_auth'] ?? true;
$mail->Username = $cfg['smtp_user'];
$mail->Password = $cfg['smtp_token'];
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->Timeout = 15;
$mail->SMTPAuth = $cfg['smtp_auth'] ?? true;
$mail->Username = $cfg['smtp_user'];
$mail->Password = $cfg['smtp_token'];
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->Timeout = 15;
// Proton only accepts the address tied to the token as sender.
$mail->setFrom($cfg['smtp_user'], 'Portfolio: ' . $name);
@@ -122,14 +129,29 @@ try {
$mail->Subject = '[Portfolio] ' . $subject;
$mail->isHTML(false);
// Site name: from the (already validated) Origin, else the configured name
$site = $cfg['site_name'] ?? '';
if ($site === '') {
$site = (string) (parse_url($origin, PHP_URL_HOST) ?: ($_SERVER['HTTP_HOST'] ?? 'portfolio'));
}
date_default_timezone_set($cfg['timezone'] ?? 'Europe/Paris');
$line = str_repeat('-', 60);
$or = fn(string $v): string => $v !== '' ? $v : '-';
$body = str_replace(["\r\n", "\r"], "\n", $message);
$mail->Body =
"New message from your portfolio\n"
. "-------------------------------\n"
. "Name: $name\n"
. "Email: $email\n"
. ($company !== '' ? "Company: $company\n" : '')
. 'Date: ' . date('Y-m-d H:i:s T') . "\n\n"
. $message . "\n";
"Nouveau message depuis le formulaire de contact de $site\n"
. "$line\n"
. "Prénom : $first\n"
. "Nom : $last\n"
. "E-mail : $email\n"
. 'Téléphone: ' . $or($phone) . "\n"
. 'Société : ' . $or($company) . "\n"
. "Objet : $subject\n"
. "$line\n\n"
. $body . "\n\n"
. "$line\n"
. "IP: $ip — " . date('Y-m-d H:i:s') . "\n";
$mail->send();