<?php
header('Content-Type: application/xml; charset=utf-8');

// Bepaal protocol en domein dynamisch
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https://" : "http://";
$host     = $_SERVER['HTTP_HOST'] ?? 'xxxalibaba.com';
$baseUrl  = $protocol . $host;

$films_file = __DIR__ . '/films.txt';
$today      = date('Y-m-d');

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  
  <!-- Hoofdpagina -->
  <url>
    <loc><?= $baseUrl; ?>/</loc>
    <lastmod><?= $today; ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>

  <!-- Statische pagina's -->
  <url>
    <loc><?= $baseUrl; ?>/dmca.php</loc>
    <lastmod><?= $today; ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.3</priority>
  </url>
  <url>
    <loc><?= $baseUrl; ?>/privacy.php</loc>
    <lastmod><?= $today; ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.3</priority>
  </url>

  <!-- Dynamische filmpagina's uit films.txt -->
<?php
if (file_exists($films_file)) {
    $lastmod_file = date('Y-m-d', filemtime($films_file));
    $lines = file($films_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    foreach ($lines as $line) {
        $parts = explode('|', $line);
        if (count($parts) >= 2) {
            $videoId = trim($parts[1]);
            $videoUrl = $baseUrl . '/watch.php?v=' . urlencode($videoId);
            ?>
  <url>
    <loc><?= htmlspecialchars($videoUrl); ?></loc>
    <lastmod><?= $lastmod_file; ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
<?php
        }
    }
}
?>
</urlset>
