<?php
// 1. 设置安全的根目录（限制只能下载该目录下的文件）
$baseDir = __DIR__ . '/downloads'; 

// 2. 获取当前请求的目录，默认为根目录
$currentDir = isset($_GET['dir']) ? $_GET['dir'] : $baseDir;
$currentDir = realpath($currentDir); // 解析真实路径

// 3. 【核心安全验证】防止路径遍历攻击 (Path Traversal)
if ($currentDir === false || strpos($currentDir, realpath($baseDir)) !== 0) {
    die("403 Forbidden: 无权访问此目录！");
}

// 4. 处理文件下载请求
if (isset($_GET['download'])) {
    $filePath = realpath($_GET['download']);
    
    // 再次校验文件是否在允许的目录内
    if ($filePath && strpos($filePath, realpath($baseDir)) === 0 && is_file($filePath)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream'); // 强制浏览器下载
        header('Content-Disposition: attachment; filename="' . basename($filePath) . '"');
        header('Content-Length: ' . filesize($filePath));
        
        ob_clean(); flush(); // 清空输出缓冲，防止乱码
        readfile($filePath);
        exit;
    } else {
        die("404 Not Found: 文件不存在或无权访问！");
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>文件下载中心</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; background: #f9f9f9; }
        .container { max-width: 600px; margin: auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
        h3 { color: #333; word-break: break-all; }
        ul { list-style-type: none; padding: 0; }
        li { padding: 10px; border-bottom: 1px solid #eee; display: flex; justify-content: space-between; align-items: center; }
        li:hover { background-color: #f1f1f1; }
        a { text-decoration: none; color: #007bff; font-weight: bold; }
        a:hover { text-decoration: underline; }
        .folder { color: #e67e22; }
        .file { color: #2ecc71; }
        .back-link { margin-bottom: 15px; display: inline-block; }
    </style>
</head>
<body>
<div class="container">
    <!-- 返回上一级按钮 -->
    <?php if ($currentDir !== realpath($baseDir)): ?>
        <a href="?dir=<?php echo urlencode(dirname($currentDir)); ?>" class="back-link">⬅️ 返回上一级</a>
    <?php endif; ?>
    
    <h3>📂 当前路径: <?php echo htmlspecialchars(str_replace(realpath($baseDir), '', $currentDir) ?: '/'); ?></h3>
    
    <ul>
        <?php
        // 遍历当前目录
        $items = scandir($currentDir);
        foreach ($items as $item):
            if ($item == '.' || $item == '..') continue;
            
            $fullPath = $currentDir . DIRECTORY_SEPARATOR . $item;
            $isDir = is_dir($fullPath);
        ?>
            <li>
                <?php if ($isDir): ?>
                    <!-- 如果是文件夹，点击继续进入 -->
                    <span class="folder">📁 <?php echo htmlspecialchars($item); ?></span>
                    <a href="?dir=<?php echo urlencode($fullPath); ?>">打开</a>
                    
                <?php else: ?>
                    <!-- 如果是文件，点击下载 -->
                    <span class="file">📄 <?php echo htmlspecialchars($item); ?></span>
                    <a href="?download=<?php echo urlencode($fullPath); ?>" download>下载</a>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
</div>
</body>
</html>
