Your IP : 216.73.217.78


Current Path : /home/seto/888mirror.pm/
Upload File :
Current File : /home/seto/888mirror.pm/click.php

<?php
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/helpers.php';

$id = (int)($_GET['id'] ?? 0);
if (!$id) {
    http_response_code(404);
    exit('Not found');
}

$brand = db()->prepare("SELECT tracking_url, postback_enabled FROM brands WHERE id = ? AND status = 'active'");
$brand->execute([$id]);
$brand = $brand->fetch();

if (!$brand) {
    http_response_code(404);
    exit('Not found');
}

// Получить GEO/язык через api.php
$z_country = $z_lang = '';
ob_start();
if (file_exists(__DIR__ . '/api.php')) {
    include __DIR__ . '/api.php';
}
ob_end_clean();

$country    = (!empty($z_country) && $z_country !== '-') ? strtolower($z_country) : '-';
$lang       = (!empty($z_lang)    && $z_lang    !== '-') ? strtolower(substr($z_lang, 0, 2)) : '-';
$landing_id = ($l = (int)($_GET['l'] ?? 0)) ? $l : null;

// Генерация subid (16-символьный hex)
$subid      = bin2hex(random_bytes(8));
$visitor_ip = $_SERVER['REMOTE_ADDR'] ?? '';

// Запись клика (редирект происходит всегда, запись — только если не дубль)
if (!is_excluded_ip($visitor_ip) && !is_duplicate_click($visitor_ip, $id)) {
    db()->prepare("INSERT INTO stats (brand_id, country, language, event_type, landing_id, subid, ip) VALUES (?,?,?,'click',?,?,?)")
       ->execute([$id, $country, $lang, $landing_id, $subid, $visitor_ip]);
}

// Если клик из виджета — дополнительно писать в widget_stats
if (!empty($_GET['src']) && $_GET['src'] === 'widget') {
    $w_ref    = substr(preg_replace('/[^a-zA-Z0-9.\-]/', '', $_GET['ref'] ?? ''), 0, 100);
    $w_layout = in_array($_GET['wl'] ?? '', ['table','cards','ranked','liquid']) ? $_GET['wl'] : '';
    db()->prepare("INSERT INTO widget_stats (brand_id, country, language, event_type, ref_host, widget_layout) VALUES (?,?,?,'click',?,?)")
       ->execute([$id, $country, $lang, $w_ref, $w_layout]);
}

// Замена макросов в tracking_url
$url = $brand['tracking_url'];
$url = str_replace('{subid}',      $subid,               $url);
$url = str_replace('{landing_id}', (string)($landing_id ?? ''), $url);
$url = str_replace('{country}',    $country !== '-' ? $country : '', $url);

header('Location: ' . $url);
exit();