Files
site_aegisone/inc/form-handler.php
T
2026-05-17 05:22:06 +03:00

90 lines
3.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
session_start();
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax'])) {
header('Content-Type: application/json');
$response = ['success' => false, 'message' => ''];
$csrf_token = $_POST['csrf_token'] ?? '';
if (!hash_equals($_SESSION['csrf_token'], $csrf_token)) {
$response['message'] = 'Ошибка безопасности. Обновите страницу и попробуйте снова.';
echo json_encode($response);
exit;
}
$ip = $_SERVER['REMOTE_ADDR'];
$rateKey = 'rate_' . md5($ip);
$now = time();
$rateData = $_SESSION[$rateKey] ?? ['count' => 0, 'first' => $now];
if ($now - $rateData['first'] > 60) {
$rateData = ['count' => 0, 'first' => $now];
}
$rateData['count']++;
$_SESSION[$rateKey] = $rateData;
if ($rateData['count'] > 3) {
$response['message'] = 'Слишком много запросов. Попробуйте через минуту.';
echo json_encode($response);
exit;
}
$name = trim($_POST['name'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$message = trim($_POST['message'] ?? '');
$agree = isset($_POST['agree']);
if (!preg_match('/^[a-zA-Zа-яА-Я\s]{1,25}$/u', $name)) {
$response['message'] = 'Имя должно содержать только буквы и пробелы, до 25 символов.';
echo json_encode($response);
exit;
}
if (!preg_match('/^8\s\(\d{3}\)\s\d{3}-\d{2}-\d{2}$/', $phone)) {
$response['message'] = 'Введите корректный номер телефона.';
echo json_encode($response);
exit;
}
if ($message !== '' && !preg_match('/^.{1,500}$/u', $message)) {
$response['message'] = 'Сообщение не должно превышать 500 символов.';
echo json_encode($response);
exit;
}
if (!$agree) {
$response['message'] = 'Необходимо согласиться с обработкой данных.';
echo json_encode($response);
exit;
}
$dateTime = date('d.m.Y H:i:s');
$serviceTitle = trim($_POST['service'] ?? '');
$subject = SITE_MAIL_SUBJECT_PREFIX;
if ($serviceTitle !== '') {
$subject .= ' (' . $serviceTitle . ')';
}
$subject .= ' — ' . $dateTime;
$body = "Услуга: " . ($serviceTitle !== '' ? $serviceTitle : 'Главная страница') . "\n";
$body .= "Имя: " . strip_tags($name) . "\n";
$body .= "Телефон: " . strip_tags($phone) . "\n";
$body .= "Сообщение: " . strip_tags($message) . "\n";
$body .= "Согласие на обработку данных: Да\n";
$body .= "Страница: " . ($_SERVER['HTTP_REFERER'] ?? 'неизвестно') . "\n";
$headers = "From: " . SITE_MAIL_FROM . "\r\n";
$headers .= "Reply-To: " . SITE_MAIL_FROM . "\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$mailSent = mail(SITE_MAIL_TO, $subject, $body, $headers);
if ($mailSent) {
$response['success'] = true;
$response['message'] = 'Спасибо! Ваша заявка отправлена. Мы свяжемся с вами в ближайшее время.';
} else {
$response['message'] = 'Ошибка при отправке. Пожалуйста, попробуйте ещё раз.';
}
echo json_encode($response);
exit;
}