<?php
header('Content-Type: application/xml; charset=utf-8');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/db.php';

$domain = 'https://pixelwarezplay.pw';
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

$static_pages = [
    '/' => ['changefreq' => 'daily', 'priority' => '1.0'],
    '/forum.php' => ['changefreq' => 'daily', 'priority' => '0.9'],
];

foreach ($static_pages as $url => $meta) {
    $xml .= '<url>' . "\n";
    $xml .= '  <loc>' . $domain . $url . '</loc>' . "\n";
    $xml .= '  <changefreq>' . $meta['changefreq'] . '</changefreq>' . "\n";
    $xml .= '  <priority>' . $meta['priority'] . '</priority>' . "\n";
    $xml .= '</url>' . "\n";
}

$cat_prios = [
    'films' => '0.9', 'animation' => '0.9', 'series' => '0.9',
    'jeux-pc' => '0.9', 'jeux-pc-ancien' => '0.8',
    'xbox' => '0.8', 'xbox360' => '0.8', 'xboxone' => '0.8',
    'ps1' => '0.8', 'ps2' => '0.8', 'ps3' => '0.8', 'ps4' => '0.8', 'psp' => '0.8',
    'gba' => '0.8', 'switch' => '0.8', 'gamecube' => '0.8', '3ds' => '0.8',
    'musique' => '0.8', 'livres' => '0.8', 'bd' => '0.8',
    'magazines' => '0.7', 'mangas' => '0.8', 'audiobook' => '0.7', 'journaux' => '0.7',
];

foreach (FORUM_CATEGORIES as $slug => $cat) {
    $prio = $cat_prios[$slug] ?? '0.8';
    $xml .= '<url>' . "\n";
    $xml .= '  <loc>' . $domain . '/categorie.php?slug=' . urlencode($slug) . '</loc>' . "\n";
    $xml .= '  <changefreq>daily</changefreq>' . "\n";
    $xml .= '  <priority>' . $prio . '</priority>' . "\n";
    $xml .= '</url>' . "\n";
}

try {
    $forums = db()->query("SELECT id FROM nezo_forums WHERE parent_forum_id = 0 ORDER BY id LIMIT 100");
    while ($f = $forums->fetch()) {
        $xml .= '<url>' . "\n";
        $xml .= '  <loc>' . $domain . '/forum.php?fid=' . (int)$f['id'] . '</loc>' . "\n";
        $xml .= '  <changefreq>daily</changefreq>' . "\n";
        $xml .= '  <priority>0.8</priority>' . "\n";
        $xml .= '</url>' . "\n";
    }
} catch (Exception $e) {}

try {
    $topics = db()->query("SELECT id FROM nezo_topics WHERE moved_to IS NULL ORDER BY last_post DESC LIMIT 1000");
    while ($t = $topics->fetch()) {
        $xml .= '<url>' . "\n";
        $xml .= '  <loc>' . $domain . '/fiche.php?id=' . (int)$t['id'] . '</loc>' . "\n";
        $xml .= '  <changefreq>weekly</changefreq>' . "\n";
        $xml .= '  <priority>0.6</priority>' . "\n";
        $xml .= '</url>' . "\n";
    }
} catch (Exception $e) {}

$xml .= '</urlset>';
echo $xml;