328 lines
11 KiB
PHP
328 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* Traitement du formulaire de contact.
|
|
*/
|
|
|
|
require_once __DIR__ . '/../includes/Security.php';
|
|
require_once __DIR__ . '/../config/database.php';
|
|
require_once __DIR__ . '/../config/recaptcha.php';
|
|
require_once __DIR__ . '/../includes/Mailer.php';
|
|
|
|
Security::bootstrap(true, ['api' => true, 'no_store' => true]);
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
Security::jsonResponse([
|
|
'success' => false,
|
|
'message' => 'Methode non autorisee.',
|
|
]);
|
|
}
|
|
|
|
Security::enforceSameOrigin(true);
|
|
Security::verifyCsrf($_POST['csrf_token'] ?? '', true);
|
|
|
|
if (!empty($_POST['website'])) {
|
|
Security::jsonResponse([
|
|
'success' => true,
|
|
'message' => 'Message envoye avec succes.',
|
|
]);
|
|
}
|
|
|
|
// Google reCAPTCHA v3 (si configure) : filtre les bots via un score anti-spam.
|
|
if (RECAPTCHA_ENABLED) {
|
|
$recaptchaOk = Security::verifyRecaptcha(
|
|
(string) ($_POST['recaptcha_token'] ?? ''),
|
|
RECAPTCHA_SECRET_KEY,
|
|
Security::clientIp(),
|
|
RECAPTCHA_MIN_SCORE,
|
|
'contact'
|
|
);
|
|
|
|
if (!$recaptchaOk) {
|
|
http_response_code(422);
|
|
Security::jsonResponse([
|
|
'success' => false,
|
|
'message' => 'Verification anti-robot echouee. Merci de reessayer.',
|
|
]);
|
|
}
|
|
}
|
|
|
|
$captchaInput = (int) ($_POST['captcha'] ?? -1);
|
|
$captchaAnswer = (int) ($_SESSION['captcha_answer'] ?? -99);
|
|
unset($_SESSION['captcha_answer']);
|
|
|
|
if ($captchaInput !== $captchaAnswer) {
|
|
http_response_code(422);
|
|
Security::jsonResponse([
|
|
'success' => false,
|
|
'message' => 'La reponse au CAPTCHA est incorrecte. Veuillez reessayer.',
|
|
]);
|
|
}
|
|
|
|
$ip = Security::clientIp();
|
|
|
|
try {
|
|
$db = getDatabase();
|
|
} catch (Throwable $e) {
|
|
error_log('[Contact] DB unavailable: ' . $e->getMessage());
|
|
http_response_code(500);
|
|
Security::jsonResponse([
|
|
'success' => false,
|
|
'message' => 'Une erreur est survenue. Veuillez reessayer.',
|
|
]);
|
|
}
|
|
|
|
if (Security::isRateLimited($db, 'contact', $ip, 5, 15)) {
|
|
http_response_code(429);
|
|
Security::jsonResponse([
|
|
'success' => false,
|
|
'message' => 'Trop de messages envoyes. Reessayez dans 15 minutes.',
|
|
]);
|
|
}
|
|
|
|
$name = trim((string) ($_POST['name'] ?? ''));
|
|
$email = trim((string) ($_POST['email'] ?? ''));
|
|
$subject = trim((string) ($_POST['subject'] ?? ''));
|
|
$message = trim((string) ($_POST['message'] ?? ''));
|
|
|
|
$name = preg_replace('/[\x00-\x1F\x7F]+/u', ' ', $name) ?? $name;
|
|
$subject = preg_replace('/[\x00-\x1F\x7F]+/u', ' ', $subject) ?? $subject;
|
|
$message = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/u', '', $message) ?? $message;
|
|
|
|
if (mb_strlen($name) < 2 || mb_strlen($name) > 255) {
|
|
http_response_code(422);
|
|
Security::jsonResponse([
|
|
'success' => false,
|
|
'message' => 'Le nom est requis (2 a 255 caracteres).',
|
|
]);
|
|
}
|
|
|
|
$cleanEmail = Security::sanitizeEmail($email);
|
|
if ($cleanEmail === false) {
|
|
http_response_code(422);
|
|
Security::jsonResponse([
|
|
'success' => false,
|
|
'message' => 'Une adresse email valide est requise.',
|
|
]);
|
|
}
|
|
|
|
if ($subject !== '' && mb_strlen($subject) > 255) {
|
|
http_response_code(422);
|
|
Security::jsonResponse([
|
|
'success' => false,
|
|
'message' => 'Le sujet depasse 255 caracteres.',
|
|
]);
|
|
}
|
|
|
|
if (mb_strlen($message) < 10 || mb_strlen($message) > 5000) {
|
|
http_response_code(422);
|
|
Security::jsonResponse([
|
|
'success' => false,
|
|
'message' => 'Le message doit contenir entre 10 et 5000 caracteres.',
|
|
]);
|
|
}
|
|
|
|
// Filtre anti-spam : les bots injectent souvent plusieurs liens ou du BBCode.
|
|
$haystack = mb_strtolower($name . ' ' . $subject . ' ' . $message, 'UTF-8');
|
|
$linkCount = preg_match_all('~https?://|www\.|\[url|\[link|</?a\b~i', $haystack);
|
|
|
|
if ($linkCount >= 4 || preg_match('~\[url|\[link|\[/url\]|xn--~i', $haystack)) {
|
|
// On enregistre la tentative (rate limit) mais on ne notifie pas : reponse neutre.
|
|
Security::recordAttempt($db, 'contact', $ip);
|
|
Security::jsonResponse([
|
|
'success' => true,
|
|
'message' => 'Message envoye avec succes.',
|
|
]);
|
|
}
|
|
|
|
Security::recordAttempt($db, 'contact', $ip);
|
|
|
|
try {
|
|
$stmt = $db->prepare(
|
|
'INSERT INTO contact_messages (sender_name, sender_email, subject, message)
|
|
VALUES (:name, :email, :subject, :message)'
|
|
);
|
|
$stmt->execute([
|
|
'name' => $name,
|
|
'email' => $cleanEmail,
|
|
'subject' => $subject,
|
|
'message' => $message,
|
|
]);
|
|
|
|
$contactMessageId = (int) $db->lastInsertId();
|
|
|
|
$notifyEmail = NOTIFY_EMAIL;
|
|
try {
|
|
$setting = $db->prepare('SELECT setting_value FROM site_settings WHERE setting_key = :key LIMIT 1');
|
|
$setting->execute(['key' => 'notify_email']);
|
|
$stored = $setting->fetchColumn();
|
|
if (is_string($stored) && $stored !== '') {
|
|
$notifyEmail = $stored;
|
|
}
|
|
} catch (Throwable) {
|
|
}
|
|
|
|
$mailSubject = $subject !== ''
|
|
? '[Contact] ' . mb_substr($subject, 0, 100)
|
|
: '[Contact] Nouveau message de ' . $name;
|
|
|
|
$mailer = new Mailer();
|
|
$mailer->sendHtml(
|
|
$notifyEmail,
|
|
$mailSubject,
|
|
buildAdminNotificationHtml($name, $cleanEmail, $subject, $message),
|
|
buildAdminNotificationText($name, $cleanEmail, $subject, $message),
|
|
$cleanEmail
|
|
);
|
|
|
|
$confirmMailer = new Mailer();
|
|
$confirmMailer->sendHtml(
|
|
$cleanEmail,
|
|
'Votre message a bien ete recu - Tony AVERTY',
|
|
buildClientConfirmationHtml($name, $subject, $message),
|
|
buildClientConfirmationText($name, $subject, $message),
|
|
SMTP_FROM_EMAIL
|
|
);
|
|
|
|
$confirmMsgId = $confirmMailer->getLastMessageId();
|
|
if ($confirmMsgId !== '' && $contactMessageId > 0) {
|
|
try {
|
|
$db->prepare(
|
|
'INSERT IGNORE INTO sent_message_ids (message_id, contact_message_id)
|
|
VALUES (:mid, :cid)'
|
|
)->execute([
|
|
'mid' => $confirmMsgId,
|
|
'cid' => $contactMessageId,
|
|
]);
|
|
} catch (Throwable) {
|
|
}
|
|
}
|
|
|
|
Security::jsonResponse([
|
|
'success' => true,
|
|
'message' => 'Message envoye avec succes. Je vous repondrai rapidement.',
|
|
]);
|
|
} catch (PDOException $e) {
|
|
error_log('[Contact] Erreur DB : ' . $e->getMessage());
|
|
http_response_code(500);
|
|
Security::jsonResponse([
|
|
'success' => false,
|
|
'message' => 'Une erreur est survenue. Veuillez reessayer.',
|
|
]);
|
|
}
|
|
|
|
function buildAdminNotificationHtml(string $name, string $email, string $subject, string $message): string
|
|
{
|
|
$safeName = Security::e($name);
|
|
$safeEmail = Security::e($email);
|
|
$safeSubject = Security::e($subject !== '' ? $subject : '(non renseigne)');
|
|
$safeMessage = nl2br(Security::e($message));
|
|
|
|
return <<<HTML
|
|
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head><meta charset="UTF-8"></head>
|
|
<body style="margin:0;padding:24px;background:#f4f4f5;font-family:Arial,sans-serif;">
|
|
<table width="100%" cellpadding="0" cellspacing="0">
|
|
<tr>
|
|
<td align="center">
|
|
<table width="620" cellpadding="0" cellspacing="0" style="max-width:620px;width:100%;background:#ffffff;border:1px solid #e4e4e7;border-radius:12px;overflow:hidden;">
|
|
<tr>
|
|
<td style="padding:24px 28px;background:#111827;color:#ffffff;">
|
|
<strong>Nouveau message de contact</strong><br>
|
|
<span style="font-size:13px;opacity:.8;">tony-averty.dev</span>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding:24px 28px;color:#111827;font-size:14px;line-height:1.7;">
|
|
<p style="margin:0 0 12px;"><strong>Nom :</strong> {$safeName}</p>
|
|
<p style="margin:0 0 12px;"><strong>Email :</strong> <a href="mailto:{$safeEmail}" style="color:#2563eb;text-decoration:none;">{$safeEmail}</a></p>
|
|
<p style="margin:0 0 12px;"><strong>Sujet :</strong> {$safeSubject}</p>
|
|
<div style="margin-top:18px;padding:16px;background:#f9fafb;border-left:3px solid #2563eb;">{$safeMessage}</div>
|
|
<p style="margin:24px 0 0;">
|
|
<a href="https://tony-averty.dev/admin/messages.php" style="display:inline-block;padding:11px 18px;background:#2563eb;color:#ffffff;text-decoration:none;border-radius:8px;">Ouvrir le BackOffice</a>
|
|
</p>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</body>
|
|
</html>
|
|
HTML;
|
|
}
|
|
|
|
function buildAdminNotificationText(string $name, string $email, string $subject, string $message): string
|
|
{
|
|
$lines = [
|
|
"Nouveau message de {$name} ({$email})",
|
|
'',
|
|
'Sujet : ' . ($subject !== '' ? $subject : '(non renseigne)'),
|
|
'',
|
|
$message,
|
|
'',
|
|
'BackOffice : https://tony-averty.dev/admin/messages.php',
|
|
];
|
|
|
|
return implode("\n", $lines);
|
|
}
|
|
|
|
function buildClientConfirmationHtml(string $name, string $subject, string $message): string
|
|
{
|
|
$safeName = Security::e($name);
|
|
$safeSubject = Security::e($subject !== '' ? $subject : 'votre message');
|
|
$safeMessage = nl2br(Security::e($message));
|
|
|
|
return <<<HTML
|
|
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head><meta charset="UTF-8"></head>
|
|
<body style="margin:0;padding:24px;background:#f4f4f5;font-family:Arial,sans-serif;">
|
|
<table width="100%" cellpadding="0" cellspacing="0">
|
|
<tr>
|
|
<td align="center">
|
|
<table width="620" cellpadding="0" cellspacing="0" style="max-width:620px;width:100%;background:#ffffff;border:1px solid #e4e4e7;border-radius:12px;overflow:hidden;">
|
|
<tr>
|
|
<td style="padding:24px 28px;background:#111827;color:#ffffff;">
|
|
<strong>Tony AVERTY</strong><br>
|
|
<span style="font-size:13px;opacity:.8;">Confirmation de reception</span>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding:24px 28px;color:#111827;font-size:14px;line-height:1.7;">
|
|
<p style="margin:0 0 14px;">Bonjour {$safeName},</p>
|
|
<p style="margin:0 0 14px;">Merci pour votre message. Il a bien ete recu et je vous repondrai sous 24 heures.</p>
|
|
<div style="margin-top:18px;padding:16px;background:#f9fafb;border:1px solid #e5e7eb;border-radius:10px;">
|
|
<p style="margin:0 0 10px;"><strong>Sujet :</strong> {$safeSubject}</p>
|
|
<div>{$safeMessage}</div>
|
|
</div>
|
|
<p style="margin:18px 0 0;color:#6b7280;font-size:13px;">Si vous n'etes pas a l'origine de cette demande, ignorez simplement cet email.</p>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</body>
|
|
</html>
|
|
HTML;
|
|
}
|
|
|
|
function buildClientConfirmationText(string $name, string $subject, string $message): string
|
|
{
|
|
$lines = [
|
|
"Bonjour {$name},",
|
|
'',
|
|
"Merci pour votre message. Il a bien ete recu et je vous repondrai sous 24 heures.",
|
|
'',
|
|
'Sujet : ' . ($subject !== '' ? $subject : '(non renseigne)'),
|
|
$message,
|
|
'',
|
|
'Tony AVERTY - tony-averty.dev',
|
|
];
|
|
|
|
return implode("\n", $lines);
|
|
}
|