<?php
// 开启会话
session_start();

// 定义根目录，这里设置为当前脚本所在目录
$rootDirectory = __DIR__;

// 模拟简单的登录验证，你可以根据实际情况修改密码
$validPassword = "your_secure_password";

// 检查用户是否已登录
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in']!== true) {
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['password'])) {
        if ($_POST['password'] === $validPassword) {
            $_SESSION['logged_in'] = true;
        } else {
            $errorMessage = "密码错误，请重试。";
        }
    }

    if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in']!== true) {
        ?>
        <!DOCTYPE html>
        <html lang="zh-CN">
        <head>
            <meta charset="UTF-8">
            <title>登录 - 靶场系统</title>
        </head>
        <body>
            <?php if (isset($errorMessage)): ?>
                <p style="color: red;"><?php echo $errorMessage; ?></p>
            <?php endif; ?>
            <form method="post">
                <label for="password">请输入密码:</label>
                <input type="password" id="password" name="password" required>
                <input type="submit" value="登录">
            </form>
        </body>
        </html>
        <?php
        exit;
    }
}

// 处理文件下载
if (isset($_GET['download'])) {
    $fileToDownload = $rootDirectory. '/' . basename($_GET['download']);
    if (file_exists($fileToDownload) && is_file($fileToDownload)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'. basename($fileToDownload). '"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: '. filesize($fileToDownload));
        readfile($fileToDownload);
        exit;
    } else {
        $message = "文件不存在或无法下载。";
    }
}

// 处理文件编辑
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_file'])) {
    $fileToEdit = $rootDirectory. '/' . basename($_POST['edit_file']);
    if (file_exists($fileToEdit) && is_file($fileToEdit) && is_writable($fileToEdit)) {
        $newContent = $_POST['file_content'];
        if (file_put_contents($fileToEdit, $newContent)!== false) {
            $message = "文件编辑成功。";
        } else {
            $message = "文件编辑失败，请检查文件权限。";
        }
    } else {
        $message = "文件不存在或不可编辑。";
    }
}

// 获取根目录下的所有文件和文件夹
$files = scandir($rootDirectory);
$files = array_diff($files, array('.', '..'));

// 获取服务器信息
$serverInfo = array(
    'PHP 版本' => phpversion(),
    '服务器软件' => $_SERVER['SERVER_SOFTWARE'],
    '服务器操作系统' => php_uname(),
    '文档根目录' => $_SERVER['DOCUMENT_ROOT']
);
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>靶场系统 - 文件管理</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ccc;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
    <h1>服务器信息</h1>
    <table>
        <?php foreach ($serverInfo as $key => $value): ?>
            <tr>
                <th><?php echo $key; ?></th>
                <td><?php echo $value; ?></td>
            </tr>
        <?php endforeach; ?>