<?php
// 获取订阅内容
$URL_MAP = array(
    'private' =>  'https://git.jojo21.top/shawenguan/Quantumult-X/raw/master/ResSub/private.snippet',
    'ioshkj' =>  'https://git.jojo21.top/shawenguan/Quantumult-X/raw/master/ResSub/ioshkj.snippet',
    'zhangkaiitugithub' => 'https://raw.gitmirror.com/zhangkaiitugithub/passcro/main/speednodes.yaml',
    'abshare' => 'https://raw.gitmirror.com/abshare/abshare.github.io/main/README.md',
    'mksshare' => 'https://raw.gitmirror.com/mksshare/mksshare.github.io/main/README.md',
    'aiboboxx' => 'https://raw.gitmirror.com/aiboboxx/v2rayfree/main/README.md',
    'chengaopan' => 'https://raw.gitmirror.com/chengaopan/AutoMergePublicNodes/master/list_raw.txt',
    'Pawdroid' => 'https://raw.gitmirror.com/Pawdroid/Free-servers/main/README.md'
);

function getLatestSubUrl($key){
    global $URL_MAP;
    if (isset($URL_MAP[$key])) {
        return $URL_MAP[$key];
    }
    return null;
}

function getCommonSubUrl($key){
    return sprintf('https://git.jojo21.top/shawenguan/Quantumult-X/raw/master/ResSub/%s.snippet', $key);
}

function getUrlContent($url) {
    // 初始化cURL会话
    $ch = curl_init($url);

    // 设置cURL选项
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回结果而非直接输出
    curl_setopt($ch, CURLOPT_HEADER, false); // 不返回头部信息
    // 对于HTTPS URL，如果无有效证书，需要禁止cURL验证对等证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    // 执行cURL会话
    $content = curl_exec($ch);

    // 检查是否有错误发生
    if(curl_errno($ch)) {
        return 'cURL Error: ' . curl_error($ch);
    }
    // 关闭cURL会话
    curl_close($ch);
    return $content;
}

$key = 'private';
if (isset($_REQUEST['k'])) {
    $key = $_REQUEST['k'];
}
$url = getLatestSubUrl($key);
if ($url !== null) {
    $content = getUrlContent($url);
    echo $content;
    return;
}

$url = getCommonSubUrl($key);
$content = getUrlContent($url);
if (stristr($content, 'Page Not Found') || stristr($content, '页面未找到')) {
    echo '404';
} else {
    echo $content;
}

?>