<?php
require_once __DIR__ . '/includes/functions.php';

header('Content-Type: application/xml; charset=utf-8');

// Никаких пробелов и пустых строк перед этим echo
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc><?php echo SITE_URL; ?>/</loc>
    <lastmod><?php echo date('c'); ?></lastmod>   <!-- добавил, полезно -->
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>

  <?php
  $cats = getCategories();
  foreach ($cats as $cat): ?>
  <url>
    <loc><?php echo SITE_URL; ?>/?cat=<?php echo escape($cat['slug']); ?></loc>
    <lastmod><?php echo date('c'); ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
  <?php endforeach; ?>

  <?php
  $posts = db()->fetchAll("SELECT slug, updated_at FROM posts WHERE status='published' ORDER BY updated_at DESC");
  foreach ($posts as $p): ?>
  <url>
    <loc><?php echo SITE_URL; ?>/post.php?slug=<?php echo escape($p['slug']); ?></loc>
    <lastmod><?php echo date('c', strtotime($p['updated_at'])); ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.7</priority>
  </url>
  <?php endforeach; ?>
</urlset>