<?php
// Root index - for public_html/index.php
$dir = $_SERVER['DOCUMENT_ROOT'] . '/';

$items = [];
if (is_dir($dir)) {
    $handle = opendir($dir);
    while (($file = readdir($handle)) !== false) {
        if ($file === '.' || $file === '..') continue;
        // Hide dot files (.htaccess, .user.ini, .well-known etc.) and index.php itself
        if ($file[0] === '.' || $file === 'index.php') continue;
        $fullPath = $dir . $file;
        $items[] = [
            'name'     => $file,
            'isDir'    => is_dir($fullPath),
            'size'     => is_file($fullPath) ? filesize($fullPath) : '-',
            'modified' => date('Y-m-d H:i', filemtime($fullPath)),
        ];
    }
    closedir($handle);
}

usort($items, function($a, $b) {
    if ($a['isDir'] !== $b['isDir']) return $b['isDir'] - $a['isDir'];
    return strcasecmp($a['name'], $b['name']);
});

function formatSize($bytes) {
    if ($bytes === '-') return '-';
    if ($bytes < 1024) return $bytes . ' B';
    if ($bytes < 1048576) return round($bytes / 1024, 1) . ' KB';
    if ($bytes < 1073741824) return round($bytes / 1048576, 1) . ' MB';
    return round($bytes / 1073741824, 2) . ' GB';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Index of /</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: monospace; font-size: 13.3px; background: #fff; color: #000; padding: 20px 20px 40px 20px; }
        h1 { font-size: 1.17em; font-weight: bold; margin-bottom: 10px; }
        hr { border: none; border-top: 1px solid #000; margin: 6px 0; }
        table { width: 100%; border-collapse: collapse; }
        th, td { text-align: left; padding: 1px 6px 1px 0; white-space: nowrap; }
        th a { color: #000; text-decoration: none; }
        th a:hover { text-decoration: underline; }
        td.name { min-width: 300px; }
        td.last-modified { min-width: 200px; padding-right: 30px; }
        td.size { min-width: 100px; padding-right: 30px; }
        a { color: #00e; text-decoration: none; }
        a:hover { text-decoration: underline; }
        a:visited { color: #551a8b; }
        address { font-style: normal; font-size: 13.3px; color: #555; margin-top: 10px; }
    </style>
</head>
<body>
    <h1>Index of /</h1>
    <hr>
    <table>
        <thead>
            <tr>
                <th colspan="2"><a href="?C=N&O=D">Name</a></th>
                <th><a href="?C=M&O=A">Last modified</a></th>
                <th><a href="?C=S&O=A">Size</a></th>
                <th><a href="?C=D&O=A">Description</a></th>
            </tr>
        </thead>
        <tbody>
            <tr><td colspan="5"><hr></td></tr>

            <!-- Parent Directory -->
            <tr>
                <td style="width:20px; vertical-align:middle;">
                    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="16" viewBox="0 0 20 16">
                        <rect x="1" y="3" width="18" height="12" rx="1" fill="#FFD600" stroke="#cca000" stroke-width="0.5"/>
                        <rect x="1" y="1" width="8" height="4" rx="1" fill="#FFD600" stroke="#cca000" stroke-width="0.5"/>
                    </svg>
                </td>
                <td class="name"><a href="../">Parent Directory</a></td>
                <td class="last-modified">&nbsp;</td>
                <td class="size">-</td>
                <td>&nbsp;</td>
            </tr>

            <?php foreach ($items as $item): ?>
            <tr>
                <td style="width:20px; vertical-align:middle;">
                    <?php if ($item['isDir']): ?>
                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="16" viewBox="0 0 20 16">
                            <rect x="1" y="3" width="18" height="12" rx="1" fill="#FFD600" stroke="#cca000" stroke-width="0.5"/>
                            <rect x="1" y="1" width="8" height="4" rx="1" fill="#FFD600" stroke="#cca000" stroke-width="0.5"/>
                        </svg>
                    <?php else: ?>
                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="20" viewBox="0 0 16 20">
                            <rect x="0.5" y="0.5" width="11" height="19" rx="1" fill="#f5f5f5" stroke="#aaa" stroke-width="1"/>
                            <polyline points="10,0.5 15.5,6 10,6" fill="#ddd" stroke="#aaa" stroke-width="1"/>
                            <rect x="10" y="0.5" width="5.5" height="5.5" fill="#e0e0e0"/>
                        </svg>
                    <?php endif; ?>
                </td>
                <td class="name">
                    <?php if ($item['isDir']): ?>
                        <a href="/<?= htmlspecialchars($item['name']) ?>/"><?= htmlspecialchars($item['name']) ?>/</a>
                    <?php else: ?>
                        <a href="/<?= htmlspecialchars($item['name']) ?>"><?= htmlspecialchars($item['name']) ?></a>
                    <?php endif; ?>
                </td>
                <td class="last-modified"><?= $item['modified'] ?></td>
                <td class="size"><?= formatSize($item['size']) ?></td>
                <td>&nbsp;</td>
            </tr>
            <?php endforeach; ?>

            <tr><td colspan="5"><hr></td></tr>
        </tbody>
    </table>
    <address>
        Apache/2.4.62 (Win64) OpenSSL/3.3.0 PHP/8.2.12 Server at balimeen.com Port 80
    </address>
</body>
</html>
