<?php
/*
    Simple PHP / HTML File Browser & Viewer
    ---------------------------------------
    - Scans all subfolders
    - Lists .php and .html files
    - Loads selected file inside an iframe
    - Single-file solution

    Save as:
    index.php

    Put it in the root folder you want to browse.
*/

$root = __DIR__;

$allowedExtensions = ['php', 'html', 'htm'];

function scanFiles($dir, $root, $allowedExtensions)
{
    $results = [];

    $items = scandir($dir);

    foreach ($items as $item) {

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

        // Skip hidden folders/files
        if (strpos($item, '.') === 0) {
            continue;
        }

        $fullPath = $dir . DIRECTORY_SEPARATOR . $item;

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

        if (is_dir($fullPath)) {

            // Skip common junk folders
            $skipFolders = ['node_modules', 'vendor', '.git'];

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

            $results = array_merge(
                $results,
                scanFiles($fullPath, $root, $allowedExtensions)
            );

        } else {

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

            if (in_array($ext, $allowedExtensions)) {

                $relativePath = str_replace($root . DIRECTORY_SEPARATOR, '', $fullPath);

                $results[] = str_replace('\\', '/', $relativePath);
            }
        }
    }

    return $results;
}

$files = scanFiles($root, $root, $allowedExtensions);
sort($files);

$currentFile = $_GET['file'] ?? '';

$isValid = false;

if ($currentFile) {

    $realCurrent = realpath($root . DIRECTORY_SEPARATOR . $currentFile);

    if (
        $realCurrent &&
        strpos($realCurrent, realpath($root)) === 0 &&
        is_file($realCurrent)
    ) {
        $isValid = true;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>PHP / HTML File Browser</title>

<style>
    * {
        box-sizing: border-box;
    }

    body {
        margin: 0;
        font-family: Arial, sans-serif;
        background: #111;
        color: #eee;
        height: 100vh;
        overflow: hidden;
    }

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

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

    .sidebar h1 {
        font-size: 20px;
        margin-top: 0;
        color: #fff;
    }

    .file-list {
        list-style: none;
        padding: 0;
        margin: 0;
    }

    .file-list li {
        margin-bottom: 6px;
    }

    .file-list a {
        display: block;
        padding: 10px;
        border-radius: 6px;
        text-decoration: none;
        color: #ddd;
        background: #262626;
        transition: 0.2s;
        word-break: break-word;
    }

    .file-list a:hover {
        background: #3a3a3a;
    }

    .file-list a.active {
        background: #0078ff;
        color: white;
    }

    .viewer {
        flex: 1;
        background: #000;
        position: relative;
    }

    iframe {
        width: 100%;
        height: 100%;
        border: none;
        background: white;
    }

    .empty {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100%;
        color: #888;
        font-size: 22px;
        text-align: center;
        padding: 20px;
    }

    .topbar {
        padding: 10px;
        background: #161616;
        border-bottom: 1px solid #333;
        font-size: 14px;
    }

    .topbar code {
        color: #00d0ff;
    }
</style>
</head>
<body>

<div class="container">

    <div class="sidebar">

        <h1>📂 PHP / HTML Files</h1>

        <ul class="file-list">

            <?php if (empty($files)): ?>

                <li>No PHP or HTML files found.</li>

            <?php else: ?>

                <?php foreach ($files as $file): ?>

                    <?php
                        $active = ($currentFile === $file) ? 'active' : '';
                    ?>

                    <li>
                        <a
                            class="<?= $active ?>"
                            href="?file=<?= urlencode($file) ?>"
                        >
                            <?= htmlspecialchars($file) ?>
                        </a>
                    </li>

                <?php endforeach; ?>

            <?php endif; ?>

        </ul>

    </div>

    <div class="viewer">

        <?php if ($isValid): ?>

            <div class="topbar">
                Viewing:
                <code><?= htmlspecialchars($currentFile) ?></code>
            </div>

            <iframe
                src="<?= htmlspecialchars($currentFile) ?>"
                loading="lazy"
            ></iframe>

        <?php else: ?>

            <div class="empty">
                <div>
                    <h2>📄 Select a file</h2>
                    <p>Choose a PHP or HTML file from the left sidebar.</p>
                </div>
            </div>

        <?php endif; ?>

    </div>

</div>

</body>
</html>