<?php
/**
 * QCluster 蜘蛛跟踪像素
 * 通过 1x1 透明 GIF 记录蜘蛛访问
 */
define('QCLUSTER_ENTRY', true);
require_once __DIR__ . '/bootstrap.php';
require_once ENGINE_DIR . '/datastore.php';
require_once ENGINE_DIR . '/crawler_detect.php';

// 返回 1x1 透明 GIF
header('Content-Type: image/gif');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

echo base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');

// 记录蜘蛛
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'] ?? ($_SERVER['HTTP_X_FORWARDED_FOR'] ?? ($_SERVER['REMOTE_ADDR'] ?? '0.0.0.0'));
if (strpos($ip, ',') !== false) $ip = trim(strtok($ip, ','));
$referer = $_SERVER['HTTP_REFERER'] ?? '';

if (identifySpider($ua) !== null) {
    logSpiderVisit($referer ?: '/crawler_pixel', $ua, $ip);
}
