<?php
require __DIR__ . '/includes/bootstrap.php';
header('Content-Type: application/xml; charset=utf-8');
header('Cache-Control: public, max-age=3600');
function sxt($value) { return htmlspecialchars((string) $value, ENT_XML1 | ENT_QUOTES, 'UTF-8'); }
$mtime = max((int) @filemtime(__FILE__), (int) @filemtime(__DIR__.'/config/tools.php'), (int) @filemtime(__DIR__.'/tools/tool.php'));
$lastmod = gmdate('Y-m-d', $mtime ?: time());
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">' . "\n";
foreach (all_tools() as $slug => $tool) {
    $priority = !empty($tool['popular']) ? '0.9' : '0.8';
    echo "  <url>\n";
    echo "    <loc>" . sxt(tool_absolute_url($slug)) . "</loc>\n";
    echo "    <lastmod>" . $lastmod . "</lastmod>\n";
    echo "    <changefreq>monthly</changefreq>\n";
    echo "    <priority>" . $priority . "</priority>\n";
    $img = tool_graphic_path($slug);
    if ($img) {
        echo "    <image:image><image:loc>" . sxt(absolute_url($img)) . "</image:loc><image:title>" . sxt($tool['name'] . ' – PDFbrix') . "</image:title></image:image>\n";
    }
    echo "  </url>\n";
}
echo "</urlset>\n";
