62 lines
1.7 KiB
PHP
62 lines
1.7 KiB
PHP
<?php
|
|
/**
|
|
* Endpoint de traitement du formulaire de contact
|
|
*/
|
|
|
|
require_once __DIR__ . '/../includes/config.php';
|
|
require_once __DIR__ . '/../includes/functions.php';
|
|
|
|
ini_set('display_errors', 1);
|
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
//header('Content-Type: application/json; charset=utf-8');
|
|
header('X-Content-Type-Options: nosniff');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'error' => 'Méthode non autorisée']);
|
|
exit;
|
|
}
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$input) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'Données invalides']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
if (!verifyCsrfToken($input['csrf_token'] ?? '')) {
|
|
throw new Exception('Token de sécurité invalide. Veuillez rafraîchir la page.');
|
|
}
|
|
|
|
$recaptchaScore = verifyRecaptcha($input['recaptcha_token'] ?? '');
|
|
if ($recaptchaScore < RECAPTCHA_THRESHOLD) {
|
|
error_log("reCAPTCHA score trop bas: {$recaptchaScore}");
|
|
throw new Exception('Vérification anti-spam échouée. Veuillez réessayer.');
|
|
}
|
|
|
|
$data = validateContactData($input);
|
|
|
|
$sent = sendContactEmail($data);
|
|
if (!$sent) {
|
|
throw new Exception('Erreur lors de l\'envoi du message. Veuillez réessayer plus tard.');
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'Votre message a bien été envoyé ! Je vous répondrai dans les meilleurs délais.'
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
http_response_code(400);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
}
|