<?php
// IMPORTANT: Ensure this file has NO spaces or lines before "<?php"
// and is saved as UTF-8 without BOM.

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once __DIR__ . '/../config.php'; // adjust if necessary

// --- Detect site URL safely ---
$https = (
    (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ||
    (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
);

$protocol = $https ? 'https://' : 'http://';
$host     = $_SERVER['HTTP_HOST'] ?? 'localhost';

// SCRIPT_NAME might be inside a folder (e.g., /public)
$basePath = rtrim(str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])), '/');

// Build final base URL
$siteUrl = rtrim($protocol . $host . $basePath, '/');

// --- Public URLs to include ---
$urls = [
    '', 'about', 'academic_calendar', 'attendance', 'classes', 'contact',
    'faq', 'fees', 'grade_class', 'help_center', 'services',
    'students', 'teachers', 'timetable', 'user_manual',
    'privacy', 'terms', 'terms_of_use'
];

// --- Output XML ---
header('Content-Type: application/xml; charset=utf-8');

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($urls as $path): ?>
<?php
    // Build safe URL
    $loc = $siteUrl . '/' . ltrim($path, '/');
    $loc = htmlspecialchars($loc, ENT_XML1 | ENT_COMPAT, 'UTF-8');
?>
  <url>
    <loc><?= $loc ?></loc>
    <priority>0.80</priority>
  </url>
<?php endforeach; ?>
</urlset>
