<?php
// Function to convert bytes to human-readable format
function formatSize($bytes) {
    if ($bytes >= 1073741824) {
        $bytes = number_format($bytes / 1073741824, 2) . ' GB';
    } elseif ($bytes >= 1048576) {
        $bytes = number_format($bytes / 1048576, 2) . ' MB';
    } elseif ($bytes >= 1024) {
        $bytes = number_format($bytes / 1024, 2) . ' KB';
    } else {
        $bytes = $bytes . ' bytes';
    }
    return $bytes;
}

// Get current directory path
$dirPath = __DIR__;  // This ensures the script always gets the current directory path

// Get the current directory path relative to the root
$relativeDir = isset($_GET['dir']) ? $_GET['dir'] : ''; // Get the directory from the URL parameter if available

// Define full directory path
$fullDirPath = $dirPath . '/' . $relativeDir;

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Directory Listing</title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0&icon_names=download,folder" />
    <style>
        body {
            font-family: 'Roboto', sans-serif;
            background-color: #121212;
            color: #e0e0e0;
            margin: 0;
            padding: 0;
        }

        .container {
            margin-top: 50px;
            text-align: center;
            border-radius: 12px;
            padding: 40px;
            background-color: #1d1d1d;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
        }

        h4 {
            font-size: 2.5rem;
            color: #f06292;
        }

        .file-item, .folder-item {
            margin: 15px;
            padding: 15px;
            border-radius: 8px;
            background-color: #333;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
            display: flex;
            align-items: center;
        }

        .file-item a, .folder-item a {
            display: flex;
            align-items: center;
            padding: 8px 12px;
            border-radius: 8px;
            color: white;
            text-align: center;
            transition: background-color 0.3s ease;
            width: 100%;
        }

        .file-item a {
            background-color: #81d4fa;
        }

        .file-item a:hover {
            background-color: rgb(69, 192, 250);
        }

        .folder-item a {
            background-color: #f06292;
        }

        .folder-item a:hover {
            background-color: rgb(236, 76, 129);
        }

        .file-name {
            font-size: 1.2rem;
            margin-left: 10px;
        }

        .file-size {
            font-size: 1rem;
            color: #bdbdbd;
        }

        .material-symbols-outlined {
            margin-right: 10px;
            font-size: 1.5rem;
        }

        .back-to-root {
            margin-bottom: 20px;
        }

        .back-to-root a {
            color: #f06292;
            font-size: 1.5rem;
            text-decoration: none;
        }

        .back-to-root a:hover {
            color: #d32f2f;
        }
    </style>
</head>
<body>

<div class="container">
    <!-- Display Back to Root button if in subfolder -->
    <?php if ($relativeDir !== ''): ?>
        <div class="back-to-root">
            <a href="?=" class="btn">
                <span >home</span> Back to Root
            </a>
        </div>
    <?php endif; ?>

    <h4>Personal Static Mediahosting</h4>
    <p>For any questions or complaints contact me under: mediahosting@yona.lu</p>

    <?php
    // Ensure the directory exists
    if (is_dir($fullDirPath)) {
        $files = scandir($fullDirPath);

        // Arrays to store files and folders
        $folders = [];
        $filesList = [];

        // Separate files and folders
        foreach ($files as $file) {
            if ($file !== '.' && $file !== '..') {
                if (is_dir($fullDirPath . '/' . $file)) {
                    $folders[] = $file;
                } else {
                    $filesList[] = $file;
                }
            }
        }

        // Display folders first
        foreach ($folders as $folder) {
            echo '<div class="folder-item">
                    <a href="?dir=' . urlencode($relativeDir . '/' . $folder) . '">
                        <span class="material-symbols-outlined">folder</span>
                        <span class="file-name">' . $folder . '/</span>
                    </a>
                  </div>';
        }

        // Then, display files
        foreach ($filesList as $file) {
            echo '<div class="file-item">
                    <a href="' . $relativeDir . '/' . $file . '">
                        <span class="material-symbols-outlined">download</span>
                        <span class="file-name">' . $file . '</span>
                    </a>
                    <div class="file-size">Size: ' . formatSize(filesize($fullDirPath . '/' . $file)) . '</div>
                  </div>';
        }
    } else {
        echo "The directory does not exist or is not accessible.";
    }
    ?>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</body>
</html>
