0) { ?>
FULLY Commented!
getFilename(), $second->getFilename())
?: strcmp($first->getFilename(), $second->getFilename());
}
// Scan and display one directory. Each included subfolder calls this
// function again. Every call updates the same running totals in $counts.
function ezmapRender(string $directory, string $url, array $settings, array &$counts): void
{
$folders = [];
$files = [];
try {
$entries = new FilesystemIterator($directory, FilesystemIterator::SKIP_DOTS);
foreach ($entries as $entry) {
$name = $entry->getFilename();
// Skip hidden entries and symbolic links.
if ($name[0] === '.' || $entry->isLink()) {
continue;
}
// Collect eligible folders for scanning after the files are shown.
if ($entry->isDir()) {
if (!isset($settings['excludedDirectories'][strtolower($name)])) {
$folders[] = $entry;
}
continue;
}
// Skip anything other than a regular file and skip excluded names.
if (!$entry->isFile() || isset($settings['excludedFiles'][strtolower($name)])) {
continue;
}
// Treat uppercase and lowercase extensions alike, such as JPG and jpg.
$extension = strtolower($entry->getExtension());
// Apply exclusions before counting or listing the file.
if (isset($settings['excludedExtensions'][$extension])) {
continue;
}
$isImage = isset($settings['imageExtensions'][$extension]);
if ($isImage) {
$counts['images']++;
}
// Include images and all other file types selected in the settings.
if ($isImage || isset($settings['extensions'][$extension])) {
$files[] = $entry;
}
}
} catch (RuntimeException $exception) {
// Report the unreadable folder and let the rest of the sitemap continue.
$counts['unreadable']++;
echo '
This directory could not be read.
';
return;
}
if (!$folders && !$files) {
echo '
No matching files or subdirectories to list.
';
return;
}
// Sort each group so the files can appear before the folders.
usort($files, 'ezmapSort');
usort($folders, 'ezmapSort');
echo '
';
// Display files first at this level.
foreach ($files as $file) {
$name = $file->getFilename();
// Encode spaces and other special characters in the link address.
$href = ezmapEscape($url . rawurlencode($name));
$extension = strtolower($file->getExtension());
// Count every clickable file once, including images.
$counts['files']++;
// Keep separate subtotals for .htm, .mp3, and .mp4.
// The .htm subtotal is specifically for the .htm extension.
if (in_array($extension, ['htm', 'mp3', 'mp4'], true)) {
$counts[$extension]++;
}
// target="_blank" opens the file in a new tab.
echo '
'
. ezmapEscape($name) . '';
// Show size and modification date only when enabled in the settings.
if ($settings['showDetails']) {
try {
$size = number_format($file->getSize() / 1024, 1);
$modified = date('M j, Y, g:i a', $file->getMTime());
echo ' (' . $size . ' KiB · Modified '
. ezmapEscape($modified) . ')';
} catch (RuntimeException $exception) {
echo ' (File details unavailable)';
}
}
echo '
';
}
// Display folders after the files. Clicking the folder name expands it.
foreach ($folders as $folder) {
$name = $folder->getFilename();
$folderUrl = $url . rawurlencode($name) . '/';
$counts['folders']++;
echo '
' . ezmapEscape($name) . '';
// This separate link opens the folder's website address in a new tab.
echo ''
. 'Open folder on website';
// Use the same files-first layout inside this subfolder.
ezmapRender($folder->getPathname(), $folderUrl, $settings, $counts);
echo '
';
}
echo '
';
}
// Turn the settings lists into lookup tables for the scan.
$settings = [
'extensions' => array_fill_keys(array_map('strtolower', $extensions), true),
'imageExtensions' => array_fill_keys($imageExtensions, true),
'excludedExtensions' => array_fill_keys(array_map('strtolower', $excludedExtensions), true),
'excludedDirectories' => array_fill_keys(array_map('strtolower', $excludedDirectories), true),
'excludedFiles' => array_fill_keys(array_map('strtolower', $excludedFiles), true),
'showDetails' => $showFileDetails,
];
// Start the running totals at zero for each page load.
$counts = [
'folders' => 0,
'files' => 0,
'htm' => 0,
'images' => 0,
'mp3' => 0,
'mp4' => 0,
'unreadable' => 0,
];
// ============================================================
// FIND AND VALIDATE THE DIRECTORY ON THE SERVER
// ============================================================
// Resolve paths from the website root, even if sitemap.php is in /common/.
$documentRoot = $_SERVER['DOCUMENT_ROOT'] ?? '';
$root = $documentRoot === '' ? false : realpath($documentRoot);
$error = '';
$directory = false;
$url = '/';
// Normalize slashes and decode the requested path before checking it.
$path = str_replace('\\', '/', $siteDirectory);
$decodedPath = rawurldecode($path);
// Reject invalid input and path segments that could navigate up a directory.
if (
!is_string($requestedPath)
|| strpos($decodedPath, "\0") !== false
|| preg_match('~(^|/)\.\.?(/|$)~', $decodedPath)
|| strpos($decodedPath, '\\') !== false
) {
$error = 'The configured directory is unavailable.';
} else {
// Break the path into folder names and discard empty segments.
$segments = explode('/', trim($decodedPath, '/'));
$segments = array_values(array_filter($segments, static function ($segment) {
return $segment !== '';
}));
// Build the website URL used as the starting point for clickable links.
$url = $segments
? '/' . implode('/', array_map('rawurlencode', $segments)) . '/'
: '/';
if ($root === false || !is_dir($root)) {
$error = 'The website document root could not be resolved.';
} else {
$directory = $root;
// Check every folder in the requested path against the exclusions.
foreach ($segments as $segment) {
if (
$segment[0] === '.'
|| isset($settings['excludedDirectories'][strtolower($segment)])
) {
$error = 'The configured directory is unavailable.';
break;
}
$directory = rtrim($directory, DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR . $segment;
// Each segment must be an existing directory. Skip symbolic links.
if (is_link($directory) || !is_dir($directory)) {
$error = 'The configured directory is unavailable.';
break;
}
}
// Confirm the final directory is the website root or is inside it.
if ($error === '') {
$realDirectory = realpath($directory);
$rootPrefix = rtrim($root, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
if (
$realDirectory === false
|| ($realDirectory !== $root && !str_starts_with($realDirectory, $rootPrefix))
) {
$error = 'The configured directory is unavailable.';
} else {
$directory = $realDirectory;
}
}
}
}
// Send the response status and character encoding before printing HTML.
if ($error !== '') {
http_response_code(500);
}
header('Content-Type: text/html; charset=UTF-8');
?>
Sitemap for = ezmapEscape($siteDirectory) ?>
This Is The Current Sitemap For = ezmapEscape($siteDirectory) ?>