Initial commit
This commit is contained in:
commit
18ebcef492
956 files changed
+22301
No files matched your search
+142
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
/**
|
||||
* Harvey Monster Team Page — contact form endpoint.
|
||||
* Production: https://harveymonsterteam.ru/api/contact.php
|
||||
*
|
||||
* Contract: POST JSON {name, contact, message, budget?, website?}
|
||||
* - name: required, 2–100 chars
|
||||
* - contact: required, Telegram @handle or email, ≤200 chars
|
||||
* - message: required, 10–4000 chars
|
||||
* - budget: optional, ≤100 chars
|
||||
* - website: honeypot — must be empty (bots fill it)
|
||||
* Responses: JSON {ok:bool, message|error:string}
|
||||
* - 200 ok / 400 bad JSON / 405 wrong method / 422 validation / 429 rate limit / 500 upstream
|
||||
*
|
||||
* Deploy: upload api/ to the site root, copy config.sample.php -> 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'] ?? ''));
|
||||
$budget = trim((string) ($body['budget'] ?? ''));
|
||||
$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 символов).');
|
||||
}
|
||||
$isEmail = filter_var($contact, FILTER_VALIDATE_EMAIL) !== false;
|
||||
$isTg = preg_match('/^@[A-Za-z0-9_]{3,32}$/', $contact) === 1;
|
||||
if ((!$isEmail && !$isTg) || u_len($contact) > 200) {
|
||||
json_out(422, false, 'Способ связи: @telegram или email.');
|
||||
}
|
||||
if (u_len($message) < 10 || u_len($message) > 4000) {
|
||||
json_out(422, false, 'Опишите задачу (10–4000 символов).');
|
||||
}
|
||||
if (u_len($budget) > 100) {
|
||||
json_out(422, false, 'Бюджет: не более 100 символов.');
|
||||
}
|
||||
|
||||
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"
|
||||
. 'Бюджет: ' . ($budget !== '' ? $esc($budget) : '—') . "\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, 'Сообщение отправлено. Мы ответим в ближайшее время.');
|
||||
Reference in new issue
Block a user