config.php * and fill BOT_TOKEN / TG_CHAT_ID. Requires PHP 7.4+ (mbstring optional). */ if (!file_exists(__DIR__ . '/config.php')) { http_response_code(500); header('Content-Type: application/json; charset=utf-8'); echo json_encode(['ok' => false, 'error' => 'Server configuration error.'], JSON_UNESCAPED_UNICODE); exit; } require __DIR__ . '/config.php'; // BOT_TOKEN, TG_CHAT_ID const MAX_REQUESTS_PER_WINDOW = 5; const WINDOW_SECONDS = 600; const RATE_LIMIT_DIR = __DIR__ . '/../var/ratelimit'; function json_out(int $status, bool $ok, string $msg): void { http_response_code($status); header('Content-Type: application/json; charset=utf-8'); echo json_encode($ok ? ['ok' => true, 'message' => $msg] : ['ok' => false, 'error' => $msg], JSON_UNESCAPED_UNICODE); exit; } function u_len(string $s): int { if (function_exists('mb_strlen')) { return mb_strlen($s, 'UTF-8'); } $n = preg_match_all('/./us', $s); return $n === false ? strlen($s) : $n; } function rate_limited(string $ip): bool { if (!is_dir(RATE_LIMIT_DIR) && !@mkdir(RATE_LIMIT_DIR, 0775, true)) { return false; // storage unavailable — accept rather than block the whole form } $file = RATE_LIMIT_DIR . '/' . md5($ip) . '.json'; $now = time(); $hits = []; if (is_file($file)) { $hits = json_decode((string) file_get_contents($file), true); if (!is_array($hits)) { $hits = []; } foreach ($hits as $i => $t) { if ($now - $t >= WINDOW_SECONDS) { unset($hits[$i]); } } } $hits[] = $now; @file_put_contents($file, json_encode($hits), LOCK_EX); return count($hits) > MAX_REQUESTS_PER_WINDOW; } if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') { header('Allow: POST'); json_out(405, false, 'Method not allowed.'); } $body = json_decode((string) file_get_contents('php://input'), true); if (!is_array($body)) { json_out(400, false, 'Expected a JSON body.'); } $name = trim((string) ($body['name'] ?? '')); $contact = trim((string) ($body['contact'] ?? '')); $message = trim((string) ($body['message'] ?? '')); $honey = trim((string) ($body['website'] ?? '')); if ($honey !== '') { json_out(200, true, 'Сообщение отправлено. Мы ответим в ближайшее время.'); // lie to bots } if (u_len($name) < 2 || u_len($name) > 100) { json_out(422, false, 'Укажите имя (2–100 символов).'); } // Канал связи не ограничен форматом (telegram, email, телефон, VK) — только длина + экранирование. if (u_len($contact) < 2 || u_len($contact) > 200) { json_out(422, false, 'Укажите предпочтительное средство связи (2–200 символов).'); } if (u_len($message) < 10 || u_len($message) > 4000) { json_out(422, false, 'Опишите задачу (10–4000 символов).'); } if (rate_limited($_SERVER['REMOTE_ADDR'] ?? '0.0.0.0')) { json_out(429, false, 'Слишком много запросов. Попробуйте через 10 минут.'); } $esc = function (string $s): string { return htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); }; $text = "🛎 Новое сообщение с harveymonsterteam.ru\n" . "Имя: {$esc($name)}\n" . "Связь: {$esc($contact)}\n\n" . $esc($message); $ctx = stream_context_create([ 'http' => [ 'method' => 'POST', 'header' => 'Content-Type: application/json', 'content' => json_encode([ 'chat_id' => TG_CHAT_ID, 'text' => $text, 'parse_mode' => 'HTML', ], JSON_UNESCAPED_UNICODE), 'timeout' => 10, 'ignore_errors' => true, ], ]); $tg = @file_get_contents('https://api.telegram.org/bot' . BOT_TOKEN . '/sendMessage', false, $ctx); $tgBody = $tg === false ? null : json_decode($tg, true); if ($tg === false || !is_array($tgBody) || empty($tgBody['ok'])) { error_log('contact.php: telegram send failed: ' . ($tg === false ? 'http error' : (string) $tg)); json_out(500, false, 'Ошибка сервера. Попробуйте позже.'); } json_out(200, true, 'Сообщение отправлено. Мы ответим в ближайшее время.');