<?php
// 配置短链接映射关系
$shortUrlMap = [
    'xhs1' => 'https://cdn1.ruanqing.com/qingxia/softwin/qx_export_install.exe',
    'zh1' => 'https://cdn1.ruanqing.com/qingxia/softwin/qx_export_install.exe',
    'hx' => 'https://cdn1.ruanqing.com/qingxia/softwin/qx_export_install.exe',
    'jd' => 'https://cdn1.ruanqing.com/qingxia/softwin/qx_export_install.exe',
        'old' => 'https://cdn1.ruanqing.com/qingxia/softwin/qx_export_install_old.exe',
];

// 配置自定义文件名映射关系
$customFileNameMap = [
    'xhs1' => 'xhs1_install.exe',
    'zh1' => 'zh1_install.exe',
    'hx' => 'hx_qxdczs_install.exe',
    'jd' => 'jd_qxdczs_install.exe',
    'old' => 'qx_export_install_old.exe'
];

// 获取用户请求的短链接部分
$requestPath = $_SERVER['REQUEST_URI'];
// 去除开头的斜杠
$shortCode = ltrim($requestPath, '/');
// 去除多余的路径信息
$shortCode = explode('/', $shortCode)[0];

// 检查短链接是否存在于映射表中
if (array_key_exists($shortCode, $shortUrlMap)) {
    // 获取对应的长链接
    $longUrl = $shortUrlMap[$shortCode];
    // 获取对应的自定义文件名，如果不存在则使用原文件名
    $customFileName = array_key_exists($shortCode, $customFileNameMap) ? $customFileNameMap[$shortCode] : basename(parse_url($longUrl, PHP_URL_PATH));

    // 临时文件路径
    $tempFile = tempnam(sys_get_temp_dir(), 'download_');

    // 下载文件到临时位置
    $ch = curl_init($longUrl);
    $fp = fopen($tempFile, 'wb');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);

    // 设置响应头，指定文件名
    header("Content-Disposition: attachment; filename=\"$customFileName\"");
    header("Content-Type: application/octet-stream");
    header("Content-Length: " . filesize($tempFile));

    // 输出文件内容
    readfile($tempFile);

    // 删除临时文件
    unlink($tempFile);

    exit;
} else {
    // 短链接不存在，返回 404 错误
    http_response_code(404);
    echo "404 Not Found";
}
?>    