<?php

/*
    Lightweight PHP / HTML Browser
    --------------------------------
    Fixes 503 issues on weaker hosting
*/

$root = __DIR__;
$allowed = ['php', 'html', 'htm'];

function getFiles($dir, $root, $allowed, &$grouped = [])
{
    $items = @scandir($dir);

    if (!$items) {
        return $grouped;
    }

    foreach ($items as $item) {

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

        // Skip hidden/system folders
        if ($item[0] === '.') {
            continue;
        }

        $full = $dir . '/' . $item;

        // Skip heavy folders
        $skip = ['node_modules', 'vendor', 'cache', 'tmp'];

        if (is_dir($full)) {

            if (in_array($item, $skip)) {
                continue;
            }

            getFiles($full, $root, $allowed, $grouped);

        } else {

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

            if (!in_array($ext, $allowed)) {
                continue;
            }

            // Skip this file itself
            if (realpath($full) === realpath(__FILE__)) {
                continue;
            }

            $relative = str_replace($root . '/', '', $full);

            $folder = basename(dirname($relative));

            if (!$folder || $folder === '.') {
                $folder = 'Root';
            }

            $grouped[$folder][] = [
                'path' => $relative,
                'name' => basename($relative),
                'ext'  => $ext
            ];
        }
    }

    return $grouped;
}

$files = getFiles($root, $root, $allowed);

ksort($files);

$current = $_GET['file'] ?? '';
$valid = false;

if ($current) {

    $real = realpath($root . '/' . $current);

    if (
        $real &&
        strpos($real, realpath($root)) === 0 &&
        is_file($real)
    ) {
        $valid = true;
    }
}

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>File Browser</title>

<style>

body{
    margin:0;
    font-family:Arial;
    background:#111;
    color:#fff;
    overflow:hidden;
}

.wrap{
    display:flex;
    height:100vh;
}

.sidebar{
    width:320px;
    overflow:auto;
    background:#1a1a1a;
    border-right:1px solid #333;
    padding:10px;
}

.folder{
    margin-bottom:20px;
}

.folder-title{
    background:#222;
    padding:10px;
    border-radius:6px;
    font-weight:bold;
    margin-bottom:8px;
}

.file a{
    display:block;
    padding:8px;
    margin-bottom:5px;
    border-radius:5px;
    text-decoration:none;
    color:#fff;
}

.php{
    background:#4b2ca3;
}

.html{
    background:#a34a00;
}

.file a:hover{
    opacity:0.8;
}

.active{
    outline:2px solid #fff;
}

.main{
    flex:1;
    display:flex;
    flex-direction:column;
}

.top{
    height:50px;
    background:#181818;
    border-bottom:1px solid #333;
    display:flex;
    align-items:center;
    gap:10px;
    padding:0 10px;
}

button{
    background:#333;
    border:none;
    color:#fff;
    padding:8px 12px;
    border-radius:5px;
    cursor:pointer;
}

button:hover{
    background:#555;
}

iframe{
    flex:1;
    width:100%;
    border:none;
    background:#fff;
}

.empty{
    flex:1;
    display:flex;
    justify-content:center;
    align-items:center;
    color:#777;
    font-size:24px;
}

</style>
</head>
<body>

<div class="wrap">

    <div class="sidebar">

        <h2>📂 Files</h2>

        <?php foreach($files as $folder => $items): ?>

            <div class="folder">

                <div class="folder-title">
                    📁 <?= htmlspecialchars($folder) ?>
                </div>

                <?php foreach($items as $f): ?>

                    <?php
                        $class = ($f['ext'] === 'php') ? 'php' : 'html';
                        $active = ($current === $f['path']) ? 'active' : '';
                    ?>

                    <div class="file">

                        <a
                            class="<?= $class ?> <?= $active ?>"
                            href="?file=<?= urlencode($f['path']) ?>"
                        >
                            <?= htmlspecialchars($f['name']) ?>
                        </a>

                    </div>

                <?php endforeach; ?>

            </div>

        <?php endforeach; ?>

    </div>

    <div class="main">

        <div class="top">

            <button onclick="history.back()">← Back</button>

            <button onclick="history.forward()">Forward →</button>

            <div>
                <?= $valid ? htmlspecialchars($current) : 'No file selected' ?>
            </div>

        </div>

        <?php if($valid): ?>

            <iframe src="<?= htmlspecialchars($current) ?>"></iframe>

        <?php else: ?>

            <div class="empty">
                Select a file from the sidebar
            </div>

        <?php endif; ?>

    </div>

</div>

<script>

document.addEventListener('keydown', function(e){

    if(e.altKey && e.key === 'ArrowLeft'){
        history.back();
    }

    if(e.altKey && e.key === 'ArrowRight'){
        history.forward();
    }

});

</script>

</body>
</html>