Improved contact form and responsive aspect
This commit is contained in:
1 parent
98d513acb7
commit
3b50843125
16 files changed
+192
-295
No files matched your search
+33
-41
@@ -31,6 +31,7 @@ if (!is_readable($configFile)) {
|
||||
reply(500, false, 'The contact form is not configured yet.');
|
||||
}
|
||||
$cfg = require $configFile;
|
||||
date_default_timezone_set($cfg['timezone'] ?? 'Europe/Paris');
|
||||
|
||||
require $private . '/PHPMailer/Exception.php';
|
||||
require $private . '/PHPMailer/PHPMailer.php';
|
||||
@@ -38,6 +39,7 @@ 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'] ?? '';
|
||||
@@ -63,12 +65,13 @@ function field(array $in, string $key, int $max): string
|
||||
}
|
||||
$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);
|
||||
$phone = field($in, 'phone', 30);
|
||||
$company = field($in, 'company', 100);
|
||||
$subject = field($in, 'subject', 150);
|
||||
$message = field($in, 'message', 4000);
|
||||
$consent = ($in['consent'] ?? false) === true;
|
||||
$name = trim($first . ' ' . $last);
|
||||
|
||||
// Header-bound fields must not contain line breaks (header injection).
|
||||
foreach ([$first, $last, $email, $phone, $company, $subject] as $v) {
|
||||
@@ -76,27 +79,28 @@ foreach ([$first, $last, $email, $phone, $company, $subject] as $v) {
|
||||
reply(400, false, 'Invalid characters in the form.');
|
||||
}
|
||||
}
|
||||
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 ($first === '' || $last === '' || $subject === '' || $message === ''
|
||||
|| !filter_var($email, FILTER_VALIDATE_EMAIL)
|
||||
|| ($phone !== '' && !preg_match('~^[0-9+()./ -]{6,30}$~', $phone))) {
|
||||
reply(422, false, 'Fill in the required fields (*), and check your email and phone number.');
|
||||
}
|
||||
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.');
|
||||
// GDPR: nothing is sent without the visitor's explicit consent.
|
||||
if (!$consent) {
|
||||
reply(422, false, 'Please tick the consent box so I can use your details to reply.');
|
||||
}
|
||||
|
||||
// ---------- Rate limit per IP ----------
|
||||
$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
|
||||
$store = sys_get_temp_dir() . '/portfolio-contact-' . hash('sha256', $ip . ($cfg['rate_salt'] ?? ''));
|
||||
$now = time();
|
||||
$hits = [];
|
||||
// Forget visitors after one hour: delete expired rate-limit files
|
||||
|
||||
// Delete fingerprints older than one hour (as stated in the legal notice).
|
||||
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) {
|
||||
@@ -112,15 +116,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);
|
||||
@@ -129,29 +133,17 @@ 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 =
|
||||
"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";
|
||||
"New message from your portfolio\n"
|
||||
. "-------------------------------\n"
|
||||
. "Name: $name\n"
|
||||
. "Email: $email\n"
|
||||
. ($phone !== '' ? "Phone: $phone\n" : '')
|
||||
. ($company !== '' ? "Company: $company\n" : '')
|
||||
. 'Date: ' . date('Y-m-d H:i:s T') . "\n"
|
||||
. "IP: $ip\n"
|
||||
. "Consent: given (form checkbox)\n\n"
|
||||
. $message . "\n";
|
||||
|
||||
$mail->send();
|
||||
|
||||
|
||||
Reference in new issue
Block a user