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

$db = mysqli_connect("localhost","ChitICR","sI56_Kse-fpI(zznvR","ChitICR") or die('Error');

$sql = "(
    SELECT
        DomainDetail.`UTCLastVerified`,
        DomainDetail.`UTCLevelLastChanged`,
        DomainDetail.`Rating`,
        DomainDetail.`Target`,
        Domain.Host
    FROM `DomainDetail`
    LEFT JOIN Domain ON Domain.ID = DomainDetail.DomainID
    WHERE DomainDetail.`Rating` != 'Unknown'
      AND DomainDetail.`UTCLastVerified` > NOW() - INTERVAL 1 DAY
    ORDER BY RAND()
    LIMIT 50
)
UNION
(
    SELECT
        DomainDetail.`UTCLastVerified`,
        DomainDetail.`UTCLevelLastChanged`,
        DomainDetail.`Rating`,
        DomainDetail.`Target`,
        Domain.Host
    FROM `DomainDetail`
    LEFT JOIN Domain ON Domain.ID = DomainDetail.DomainID
    WHERE DomainDetail.`Rating` != 'Unknown'
      AND DomainDetail.`UTCLastVerified` <= NOW() - INTERVAL 1 DAY
    ORDER BY RAND()
    LIMIT 2700
)
ORDER BY RAND();";

$r = $db->query($sql);

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

if($r) {
    while($row = $r->fetch_assoc()){
        $ts = strtotime($row['UTCLastVerified'] ?? '');
        $lastmod = ($ts > 0) ? date('Y-m-d', $ts) : date('Y-m-d');
        
        echo '<url>';
        echo '<loc>https://icr.chit.eu/check/' . htmlspecialchars($row['Host']) . '</loc>';
        echo '<lastmod>' . $lastmod . '</lastmod>';
        echo '<changefreq>monthly</changefreq>';
        echo '<priority>0.3</priority>';
        echo '</url>';
    }
}

echo '</urlset>';
$db->close();
?>
