untitled

Pasted Sep 29, 2026, 3:58 PM1 file361 lines

Scan to open on your phone

Point a camera at the QR code - it opens this exact paste, no app needed.

https://codepastes.com/zzgt7rc7tmjvjp4y
untitled
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

// ─────────────────────────────────────────────
//  FileManager – compatible with PHP 5.6+
// ─────────────────────────────────────────────

class FileManager
{
    public $cwd;
    private $editableExt = array('php','txt','html','css','js','json','md','sql','xml','env','yaml','yml');

    public function __construct()
    {
        $path = isset($_GET['path']) ? realpath($_GET['path']) : false;
        $this->cwd = ($path && is_dir($path)) ? $path : getcwd();
    }

    private function itemPath()
    {
        if (empty($_GET['item'])) return null;
        $path = $this->cwd . DIRECTORY_SEPARATOR . basename($_GET['item']);
        return file_exists($path) ? $path : null;
    }

    public function listItems()
    {
        $all = scandir($this->cwd);
        $entries = array();
        foreach ($all as $f) {
            if ($f === '.' || $f === '..') continue;
            $entries[] = $f;
        }

        $cwd = $this->cwd;
        usort($entries, function($a, $b) use ($cwd) {
            $aDir = is_dir($cwd . DIRECTORY_SEPARATOR . $a);
            $bDir = is_dir($cwd . DIRECTORY_SEPARATOR . $b);
            if ($aDir !== $bDir) return $bDir ? 1 : -1;
            return strcasecmp($a, $b);
        });

        return $entries;
    }

    public function isEditable($file)
    {
        $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
        return in_array($ext, $this->editableExt, true);
    }

    public function formatSize($path)
    {
        $bytes = filesize($path);
        if ($bytes >= 1048576) return round($bytes / 1048576, 1) . ' MB';
        if ($bytes >= 1024)    return round($bytes / 1024, 1) . ' KB';
        return $bytes . ' B';
    }

    public function handle()
    {
        $action = isset($_GET['action']) ? $_GET['action'] : '';

        if ($action === 'edit')   { $this->handleEdit();   }
        if ($action === 'delete') { $this->handleDelete(); }
        if ($action === 'rename') { $this->handleRename(); }

        if (isset($_GET['upload']))         { $this->showUploadPage();  }
        if (isset($_GET['upload_handler'])) { $this->handleUpload();    }
        if (isset($_POST['create_folder'])) { $this->handleCreateFolder(); }
        if (isset($_POST['create_file']))   { $this->handleCreateFile();   }
        if (isset($_GET['clear_log']))      { $this->clearLog();        }
    }

    // ── Actions ──────────────────────────────

    private function handleEdit()
    {
        $path = $this->itemPath();
        $name = isset($_GET['item']) ? basename($_GET['item']) : '';
        if (!$path) { $this->redirect(); return; }

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $content = isset($_POST['content']) ? $_POST['content'] : '';
            $ok = file_put_contents($path, $content) !== false;
            $msg = $ok ? 'Saved successfully!' : 'Save failed - check permissions.';
            echo '<script>alert("' . addslashes($msg) . '"); window.location="?path=' . urlencode($this->cwd) . '";</script>';
            exit;
        }

        $content = htmlspecialchars(file_get_contents($path));
        $this->startPage('Edit: ' . htmlspecialchars($name));
        echo '<a href="?path=' . urlencode($this->cwd) . '" class="back-link">&larr; Back</a>';
        echo '<h2>Editing <span class="accent">' . htmlspecialchars($name) . '</span></h2>';
        echo '<form method="post">';
        echo '<textarea name="content" class="editor">' . $content . '</textarea>';
        echo '<button type="submit" class="btn btn-green">Save Changes</button>';
        echo '</form>';
        $this->endPage();
    }

    private function handleDelete()
    {
        $path = $this->itemPath();
        if ($path) {
            if (is_dir($path)) rmdir($path);
            else unlink($path);
        }
        $this->redirect();
    }

    private function handleRename()
    {
        $path = $this->itemPath();
        $name = isset($_GET['item']) ? basename($_GET['item']) : '';
        if (!$path) { $this->redirect(); return; }

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $newName = basename(isset($_POST['new_name']) ? $_POST['new_name'] : '');
            if ($newName) rename($path, $this->cwd . DIRECTORY_SEPARATOR . $newName);
            $this->redirect();
            return;
        }

        $this->startPage('Rename');
        echo '<a href="?path=' . urlencode($this->cwd) . '" class="back-link">&larr; Back</a>';
        echo '<h2>Rename <span class="accent">' . htmlspecialchars($name) . '</span></h2>';
        echo '<form method="post" class="simple-form">';
        echo '<input type="text" name="new_name" value="' . htmlspecialchars($name) . '" class="input" autofocus>';
        echo '<button type="submit" class="btn btn-blue">Rename</button>';
        echo '</form>';
        $this->endPage();
    }

    private function showUploadPage()
    {
        $this->startPage('Upload File');
        echo '<a href="?path=' . urlencode($this->cwd) . '" class="back-link">&larr; Back</a>';
        echo '<h2>Upload File</h2>';
        echo '<form method="post" enctype="multipart/form-data" action="?upload_handler=1&path=' . urlencode($this->cwd) . '" class="simple-form">';
        echo '<input type="file" name="file" class="file-input">';
        echo '<button type="submit" class="btn btn-green">Upload</button>';
        echo '</form>';
        $this->endPage();
    }

    private function handleUpload()
    {
        $targetDir = (isset($_GET['path']) && realpath($_GET['path'])) ? realpath($_GET['path']) : getcwd();
        if (!empty($_FILES['file']['name'])) {
            $dest = $targetDir . DIRECTORY_SEPARATOR . basename($_FILES['file']['name']);
            $ok   = move_uploaded_file($_FILES['file']['tmp_name'], $dest);
            $msg  = $ok ? 'Uploaded successfully!' : 'Upload failed.';
            echo '<script>alert("' . addslashes($msg) . '"); window.location="?path=' . urlencode($targetDir) . '";</script>';
        }
        exit;
    }

    private function handleCreateFolder()
    {
        $name = basename(isset($_POST['folder_name']) ? $_POST['folder_name'] : '');
        if ($name) mkdir($this->cwd . DIRECTORY_SEPARATOR . $name, 0755);
        $this->redirect();
    }

    private function handleCreateFile()
    {
        $name    = basename(isset($_POST['file_name']) ? $_POST['file_name'] : '');
        $content = isset($_POST['content']) ? $_POST['content'] : '';
        if ($name) file_put_contents($this->cwd . DIRECTORY_SEPARATOR . $name, $content);
        $this->redirect();
    }

    private function clearLog()
    {
        @unlink(__DIR__ . '/debug_log.txt');
        $this->redirect();
    }

    // ── Helpers ──────────────────────────────

    private function redirect()
    {
        header('Location: ?path=' . urlencode($this->cwd));
        exit;
    }

    private function startPage($title)
    {
        echo $this->getCSS($title);
    }

    private function endPage()
    {
        echo '</body></html>';
        exit;
    }

    // ── Main View ────────────────────────────

    public function render()
    {
        $items  = $this->listItems();
        $parent = dirname($this->cwd);
        echo $this->getCSS('File Manager');
?>
<header>
    <h1>File Manager</h1>
    <span class="path"><?php echo htmlspecialchars($this->cwd); ?></span>
</header>

<nav class="toolbar">
    <a href="?path=<?php echo urlencode($parent); ?>" class="btn">&#8679; Parent</a>
    <a href="?path=<?php echo urlencode(getcwd()); ?>" class="btn">Home</a>
    <a href="?upload=1&path=<?php echo urlencode($this->cwd); ?>" class="btn btn-green">Upload</a>
</nav>

<table class="file-table">
    <thead>
        <tr>
            <th>Name</th>
            <th>Size</th>
            <th>Modified</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($items as $file):
        $full  = $this->cwd . DIRECTORY_SEPARATOR . $file;
        $isDir = is_dir($full);
        $size  = $isDir ? '&mdash;' : $this->formatSize($full);
        $mtime = date('Y-m-d H:i', filemtime($full));
        $enc   = urlencode($file);
        $cwdE  = urlencode($this->cwd);
    ?>
        <tr>
            <td class="name-cell">
                <span class="icon"><?php echo $isDir ? '[D]' : '[F]'; ?></span>
                <?php if ($isDir): ?>
                    <a href="?path=<?php echo urlencode($full); ?>"><?php echo htmlspecialchars($file); ?></a>
                <?php else: ?>
                    <span><?php echo htmlspecialchars($file); ?></span>
                <?php endif; ?>
            </td>
            <td class="muted"><?php echo $size; ?></td>
            <td class="muted"><?php echo $mtime; ?></td>
            <td class="actions">
                <?php if (!$isDir && $this->isEditable($file)): ?>
                    <a href="?action=edit&path=<?php echo $cwdE; ?>&item=<?php echo $enc; ?>" class="act act-blue">Edit</a>
                <?php endif; ?>
                <a href="?action=rename&path=<?php echo $cwdE; ?>&item=<?php echo $enc; ?>" class="act act-yellow">Rename</a>
                <?php if (!$isDir): ?>
                    <a href="<?php echo htmlspecialchars($file); ?>" download class="act act-green">Download</a>
                <?php endif; ?>
                <a href="?action=delete&path=<?php echo $cwdE; ?>&item=<?php echo $enc; ?>"
                   onclick="return confirm('Delete this item?')"
                   class="act act-red">Delete</a>
            </td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

<div class="create-grid">
    <form method="post" class="create-card">
        <h3>New Folder</h3>
        <input type="hidden" name="create_folder" value="1">
        <input type="text" name="folder_name" placeholder="Folder name" required class="input">
        <button type="submit" class="btn btn-blue full-width">Create Folder</button>
    </form>

    <form method="post" class="create-card">
        <h3>New File</h3>
        <input type="hidden" name="create_file" value="1">
        <input type="text" name="file_name" placeholder="file.txt" required class="input">
        <textarea name="content" placeholder="Initial content (optional)" rows="3" class="input textarea"></textarea>
        <button type="submit" class="btn btn-blue full-width">Create File</button>
    </form>
</div>

<footer>
    <a href="debug_log.txt" target="_blank">Debug Log</a> &nbsp;&middot;&nbsp;
    <a href="?clear_log=1">Clear Log</a>
</footer>

</body></html>
<?php
    }

    // ── CSS Shell ────────────────────────────

    private function getCSS($title)
    {
        $t = htmlspecialchars($title);
        return '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>' . $t . '</title>
<style>
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
body{background:#0d0d0f;color:#d4d8e2;font-family:"Courier New",Courier,monospace;font-size:13px;line-height:1.6;padding:2rem}
a{color:inherit;text-decoration:none}
header{display:flex;align-items:baseline;gap:1.5rem;margin-bottom:1.5rem;flex-wrap:wrap}
header h1{font-size:1.4rem;font-weight:700}
.path{color:#6b7280;font-size:11px;word-break:break-all}
.toolbar{display:flex;gap:.5rem;margin-bottom:1.5rem;flex-wrap:wrap}
.btn{display:inline-block;padding:.45rem 1rem;background:#16181c;border:1px solid #2a2d35;border-radius:6px;cursor:pointer;font-size:12px;color:#d4d8e2;font-family:"Courier New",Courier,monospace}
.btn:hover{border-color:#4f8ef7;background:#1e2228}
.btn-blue{border-color:#4f8ef7;color:#4f8ef7}
.btn-green{border-color:#34d399;color:#34d399}
.btn-green:hover{background:#0d2b22}
.full-width{width:100%;text-align:center}
.file-table{width:100%;border-collapse:collapse;background:#16181c;border:1px solid #2a2d35;margin-bottom:2rem}
.file-table thead tr{background:#111318}
.file-table th{padding:.7rem 1rem;text-align:left;font-size:11px;text-transform:uppercase;letter-spacing:.08em;color:#6b7280;border-bottom:1px solid #2a2d35}
.file-table td{padding:.65rem 1rem;border-bottom:1px solid #2a2d35;vertical-align:middle}
.file-table tbody tr:last-child td{border-bottom:none}
.file-table tbody tr:hover{background:#1c1f26}
.name-cell{display:flex;align-items:center;gap:.6rem}
.name-cell a:hover{color:#4f8ef7}
.icon{color:#6b7280;font-size:11px}
.muted{color:#6b7280}
.actions{display:flex;gap:.4rem;align-items:center;flex-wrap:wrap}
.act{font-size:11px;padding:.2rem .5rem;border-radius:4px;border:1px solid transparent;cursor:pointer}
.act:hover{opacity:.75}
.act-blue{color:#4f8ef7;border-color:#2a3e66}
.act-yellow{color:#fbbf24;border-color:#4a3a10}
.act-green{color:#34d399;border-color:#0e3326}
.act-red{color:#f87171;border-color:#4a1c1c}
.create-grid{display:flex;gap:1.5rem;flex-wrap:wrap;margin-bottom:2rem}
.create-card{flex:1 1 280px;background:#16181c;border:1px solid #2a2d35;border-radius:6px;padding:1.25rem;display:flex;flex-direction:column;gap:.75rem}
.create-card h3{font-size:13px;font-weight:600}
.input{width:100%;background:#0d0d0f;border:1px solid #2a2d35;border-radius:6px;padding:.6rem .8rem;color:#d4d8e2;font-family:"Courier New",Courier,monospace;font-size:12px;outline:none}
.input:focus{border-color:#4f8ef7}
.textarea{resize:vertical;min-height:70px}
.editor{width:100%;height:60vh;min-height:300px;background:#0a0a0c;border:1px solid #2a2d35;border-radius:6px;padding:1rem;font-family:"Courier New",Courier,monospace;font-size:12px;color:#d4d8e2;line-height:1.7;resize:vertical;outline:none;display:block;margin-bottom:1rem}
.editor:focus{border-color:#4f8ef7}
.back-link{display:inline-block;margin-bottom:1.5rem;color:#6b7280;font-size:12px}
.back-link:hover{color:#4f8ef7}
h2{font-size:1.2rem;margin-bottom:1.2rem}
.accent{color:#fbbf24}
.simple-form{display:flex;flex-direction:column;gap:1rem;max-width:480px}
.file-input{color:#6b7280;font-family:"Courier New",Courier,monospace;font-size:12px}
footer{text-align:center;color:#6b7280;font-size:11px;padding:1rem 0}
footer a:hover{color:#d4d8e2}
</style>
</head>
<body>
';
    }
}

// ── Bootstrap ─────────────────────────────────
$fm = new FileManager();
$fm->handle();
$fm->render();
?>

14,288 chars · codepastes.com