<?php

// Function to recursively list directories and files with exclusions
function listDirectoryTree($dir, $prefix = '', $exclusions = []) {
    $dirIterator = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
    $iterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::SELF_FIRST);
    
    $depthStack = [];
    $lastDepth = -1;

    foreach ($iterator as $path => $fileInfo) {
        $relativePath = str_replace($dir . DIRECTORY_SEPARATOR, '', $fileInfo->getPathname());

        // Skip files or directories and their subdirectories in the exclusions list
        foreach ($exclusions as $exclusion) {
            if (fnmatch($exclusion, $relativePath) || strpos($relativePath, $exclusion) === 0) {
                continue 2; // Skip to the next item in the loop
            }
        }

        $depth = $iterator->getDepth();
        $subIterator = $iterator->getSubIterator($depth + 1);

        // Check if this is the last item at the current depth
        $isLast = !$subIterator || !$subIterator->valid();

        // Adjust the depth stack for visual indentation
        if ($depth > $lastDepth) {
            $depthStack[] = $isLast ? '    ' : '│   ';
        } elseif ($depth < $lastDepth) {
            array_splice($depthStack, $depth + 1);
        }

        // Construct the indent string
        $indent = $prefix . implode('', array_slice($depthStack, 0, $depth));

        // Output the current file/directory with the appropriate tree line
        echo $indent . ($isLast ? '└── ' : '├── ') . $fileInfo->getFilename() . "\n";

        // Update the last depth
        $lastDepth = $depth;
    }
}

// Array of exclusions - can be directories or specific files
$exclusions = [
    'src',
    'packages',
    'cgi-bin',
    'icons',
    'images',
    '.well-known',
    '.htaccess',
    'tree.php'
];

// Execute the function in the current directory
header('Content-Type: text/plain; charset=utf-8');
echo "Directory Tree Structure:\n\n";
listDirectoryTree(__DIR__, '', $exclusions);