<?php
// crawler.php
function listDirectory($dir) {
    $files = scandir($dir);
    echo "<ul>";
    foreach ($files as $file) {
        if ($file === "." || $file === "..") continue;

        $path = $dir . DIRECTORY_SEPARATOR . $file;
        if (is_dir($path)) {
            echo "<li><strong>📁 $file</strong>";
            listDirectory($path); // recursive
            echo "</li>";
        } else {
            echo "<li>📄 <span class='file' onclick='loadFile(\"$path\")'>$file</span></li>";
        }
    }
    echo "</ul>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My Website Structure</title>
    <style>
        body { font-family: monospace; background: #f4f4f4; padding: 20px; }
        h1 { font-size: 22px; }
        ul { list-style-type: none; padding-left: 20px; }
        li { margin: 5px 0; }
        .file { color: #007acc; cursor: pointer; }
        pre {
            background: #1e1e1e;
            color: #dcdcdc;
            padding: 10px;
            margin-top: 15px;
            border-radius: 5px;
            overflow-x: auto;
            font-size: 14px;
            display: none;
        }
    </style>
</head>
<body>
    <h1>My Website Structure</h1>
    <div id="structure">
        <?php listDirectory(__DIR__); ?>
    </div>

    <h2>File Preview</h2>
    <pre id="file-content">Click on a file to view its code...</pre>

    <script>
        async function loadFile(path) {
            const response = await fetch('crawler.php?file=' + encodeURIComponent(path));
            const text = await response.text();
            const pre = document.getElementById('file-content');
            pre.style.display = 'block';
            pre.textContent = text;
        }
    </script>
</body>
</html>
<?php
// Serve file content when requested
if (isset($_GET['file'])) {
    $file = $_GET['file'];
    if (file_exists($file)) {
        header("Content-Type: text/plain");
        readfile($file);
    }
    exit;
}
?>
