<?php
// -------------------------- 星光网盘 - 5用户专属版 --------------------------
session_start();

// 网盘核心配置
$config = [
    'app_name' => '星光网盘',
    'app_slogan' => 'files文件夹内5用户独立空间版',
    'root_storage_path' => __DIR__ . '/files/', // 所有文件强制限制在此文件夹内
    'user_count' => 5, // 固定区分1~5共5个用户
    'disable_file_ext_check' => true, // 完全关闭后缀白名单限制，所有类型文件都支持上传
    'max_single_file_size' => 0, // 0代表不限制单文件大小
];

// 初始化全局files根目录
if (!is_dir($config['root_storage_path'])) {
    mkdir($config['root_storage_path'], 0755, true);
    // 放置空index.html禁止直接通过web浏览目录
    file_put_contents($config['root_storage_path'] . 'index.html', '');
}

// 初始化1~5每个用户的独立子目录
for ($uid = 1; $uid <= $config['user_count']; $uid++) {
    $user_dir = $config['root_storage_path'] . 'user_' . $uid . '/';
    if (!is_dir($user_dir)) {
        mkdir($user_dir, 0755, true);
        file_put_contents($user_dir . 'index.html', '');
    }
}

// 身份选择/验证流程
$current_uid = isset($_SESSION['current_star_user_id']) ? $_SESSION['current_star_user_id'] : 0;
$allowed_uid_range = range(1, $config['user_count']);

// 处理用户切换
if (isset($_GET['switch_user']) && in_array((int)$_GET['switch_user'], $allowed_uid_range)) {
    $_SESSION['current_star_user_id'] = (int)$_GET['switch_user'];
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit;
}

// 用户未选择身份时，只展示用户选择界面
if (!in_array($current_uid, $allowed_uid_range)) {
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $config['app_name'] ?></title>
    <style>
        *{margin:0;padding:0;box-sizing:border-box}
        body{background:#121212;color:#fff;font-family:system-ui;height:100vh;display:flex;align-items:center;justify-content:center;padding:20px}
        .user-select-wrap{background:#1e1e1e;padding:40px 30px;border-radius:16px;width:90%;max-width:480px;text-align:center}
        h1{color:#87ceeb;margin-bottom:10px}
        .tip-text{color:#94a3b8;margin-bottom:30px;font-size:14px}
        .user-list{display:grid;grid-template-columns:repeat(auto-fit, minmax(100px, 1fr));gap:16px}
        .user-btn{padding:18px 10px;border-radius:12px;background:#2563eb;color:white;border:none;font-size:18px;font-weight:bold;cursor:pointer}
        .user-btn:hover{background:#1d4ed8;transform:translateY(-2px);transition:.2s}
    </style>
</head>
<body>
    <div class="user-select-wrap">
        <h1>欢迎使用星光网盘</h1>
        <p class="tip-text">请选择你的用户编号，所有文件将存放在files文件夹内</p>
        <div class="user-list">
            <?php for ($i = 1; $i <= $config['user_count']; $i++): ?>
                <a href="?switch_user=<?= $i ?>"><button class="user-btn">用户 <?= $i ?></button></a>
            <?php endfor; ?>
        </div>
    </div>
</body>
</html>
<?php
exit;
}

// 当前用户专属工作目录，严格限定在files/user_x路径内，禁止越权
$current_user_dir = realpath($config['root_storage_path'] . 'user_' . $current_uid) . '/';

// 安全防护：强制限制操作范围，禁止跳出files文件夹
$safe_check_base_path = realpath($config['root_storage_path']);
if (strpos($current_user_dir, $safe_check_base_path) !== 0) {
    die('操作越权，已被安全拦截');
}

// 处理文件上传
if (isset($_FILES['up_file']) && $_FILES['up_file']['error'] === UPLOAD_ERR_OK) {
    $target_path = $current_user_dir . basename($_FILES['up_file']['name']);
    move_uploaded_file($_FILES['up_file']['tmp_name'], $target_path);
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit;
}

// 处理文件删除
if (isset($_GET['del_file'])) {
    $del_target = $current_user_dir . basename($_GET['del_file']);
    // 再次校验删除路径在允许范围内
    if (strpos(realpath($del_target), $safe_check_base_path) === 0 && is_file($del_target)) {
        unlink($del_target);
    }
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit;
}

// 处理文件下载
if (isset($_GET['dl_file'])) {
    $dl_target = $current_user_dir . basename($_GET['dl_file']);
    // 再次校验下载路径在允许范围内
    if (strpos(realpath($dl_target), $safe_check_base_path) === 0 && is_file($dl_target)) {
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . rawurlencode(basename($dl_target)) . '"');
        readfile($dl_target);
        exit;
    }
}

// 列出当前用户名下的所有文件
$user_file_list = array_diff(scandir($current_user_dir), ['.', '..', 'index.html']);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $config['app_name'] ?> - 用户<?= $current_uid ?></title>
    <style>
        *{margin:0;padding:0;box-sizing:border-box}
        body{background:#121212;color:#fff;font-family:system-ui;padding:30px 20px;max-width:1000px;margin:auto}
        .top-bar{display:flex;justify-content:space-between;align-items:center;margin-bottom:30px;padding-bottom:20px;border-bottom:1px solid #333}
        h2{color:#87ceeb}
        .switch-link{color:#94a3b8;text-decoration:none}
        .upload-area{background:#1e1e1e;padding:24px;border-radius:12px;margin-bottom:24px}
        .file-list{background:#1e1e1e;border-radius:12px;padding:20px}
        .file-item{display:flex;justify-content:space-between;align-items:center;padding:14px 0;border-bottom:1px solid #333}
        .action-group a{color:#60a5fa;margin-left:12px;text-decoration:none}
    </style>
</head>
<body>
    <div class="top-bar">
        <h2>星光网盘 - 用户 <?= $current_uid ?></h2>
        <a href="?switch_user=0" class="switch-link">切换用户编号</a>
    </div>

    <div class="upload-area">
        <h3 style="margin-bottom:15px;color:#87ceeb;">上传文件</h3>
        <form method="post" enctype="multipart/form-data">
            <input type="file" name="up_file" required>
            <button style="padding:8px 20px;background:#2563eb;color:white;border:none;border-radius:8px;cursor:pointer;margin-left:10px">上传到我的空间</button>
        </form>
    </div>

    <div class="file-list">
        <h3 style="margin-bottom:15px;color:#87ceeb;">我的文件 (<?= count($user_file_list) ?> 个)</h3>
        <?php if (empty($user_file_list)): ?>
            <p style="color:#64748b">暂无上传的文件</p>
        <?php else: ?>
            <?php foreach ($user_file_list as $file): ?>
                <div class="file-item">
                    <span><?= htmlspecialchars($file) ?></span>
                    <div class="action-group">
                        <a href="?dl_file=<?= urlencode($file) ?>">下载</a>
                        <a href="?del_file=<?= urlencode($file) ?>" onclick="return confirm('确认删除这个文件吗？')">删除</a>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</body>
</html>
0