stylehub/lib/sitemap.php

66 lines
3.0 KiB
PHP
Raw Normal View History

2024-02-15 10:17:09 +01:00
<?php
2024-02-15 10:23:54 +01:00
require_once "lib/account.php";
/**
* Generates an XML sitemap as a string for a website, considering only pages that the current session
* has sufficient privileges to access. It scans directories specified in the router configuration
* for .html and .php files, and constructs a sitemap entry for each accessible page based on their
* required permission levels. This function returns the sitemap as a string and
* sets the appropriate header for XML content.
*
* @global array $routerConfig The global configuration array containing directory paths and default settings.
* @return string The XML sitemap content, properly formatted in accordance with the sitemap protocol.
*/
function generateSitemap(): string{
2024-02-15 10:17:09 +01:00
global $routerConfig;
2024-02-15 10:23:22 +01:00
2024-02-15 10:17:09 +01:00
$site_dirs = array_diff(scandir($routerConfig["page_dir"]), array('.', '..'));
2024-02-15 10:39:02 +01:00
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
$domain = $_SERVER['HTTP_HOST'];
$subdomain = ""; // You may need to modify this based on your subdomain logic
2024-02-15 10:17:09 +01:00
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
2024-02-15 10:39:02 +01:00
$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
2024-02-15 10:17:09 +01:00
foreach ($site_dirs as $site_dir) {
$pages_dir = array_diff(scandir($routerConfig["page_dir"] . $site_dir), array('.', '..'));
foreach ($pages_dir as $page_file) {
$page_file_tmp = explode(".", $page_file);
$page_basename = $page_file_tmp[0];
2024-02-15 10:23:22 +01:00
$page_file_path = $routerConfig["page_dir"] . $site_dir . "/" . $page_file;
2024-02-15 10:39:02 +01:00
$page_location = $protocol . "://" . $subdomain . $domain . "/" . $site_dir . "/" . $page_basename;
2024-02-15 10:17:09 +01:00
2024-02-15 10:23:22 +01:00
if ($page_file_tmp[1] == "html") {
$page_tmp = file_get_contents($page_file_path);
$pageMetadata = parsePageTag($page_tmp);
if (!empty($pageMetadata["parameters"]["minimal_permission_level"])) {
$page_required_permission = intval($pageMetadata["parameters"]["minimal_permission_level"]);
} else {
$page_required_permission = $routerConfig["page"]["default_permissions"];
}
} elseif ($page_file_tmp[1] == "php") {
2024-02-24 10:11:18 +01:00
$pageMetadata = getDynamicMetadata($page_file_path);
$page_required_permission = getDynamicPermission($pageMetadata);
2024-02-15 10:23:22 +01:00
} else {
$page_required_permission = $routerConfig["page"]["default_permissions"];
}
// Check if the user is authorized to access the page
if ($page_required_permission <= $_SESSION["privilege_level"]) {
$sitemap .= '<url>' . PHP_EOL;
$sitemap .= '<loc>' . htmlspecialchars($page_location) . '</loc>' . PHP_EOL;
// You can add other optional tags like lastmod, changefreq, priority here if needed
$sitemap .= '</url>' . PHP_EOL;
}
2024-02-15 10:17:09 +01:00
}
}
$sitemap .= '</urlset>' . PHP_EOL;
2024-02-15 10:41:48 +01:00
header('Content-type: application/xml');
return $sitemap;
2024-02-15 10:17:09 +01:00
}