<?php

set_time_limit(0);

$baseUrl = "https://satta-king-daily.com";
$sitemapFile = "sitemap.xml";

$visited = [];
$urls = [];

/* ----------------------------
   ADD URL (NO DUPLICATES)
---------------------------- */
function addUrl(&$urls, $url)
{
    if (!in_array($url, $urls)) {
        $urls[] = $url;
    }
}

/* ----------------------------
   VALID URL CHECK
---------------------------- */
function isValidUrl($url, $baseUrl)
{
    $url = strtok($url, '#');
    $url = strtok($url, '?');

    if (strpos($url, $baseUrl) !== 0) {
        return false;
    }

    $path = parse_url($url, PHP_URL_PATH);

    if ($path === '/' || $path === '') return true;
    if ($path === '/game/' || $path === '/game') return true;

    return preg_match('/\.html$/i', $path);
}

/* ----------------------------
   GET LINKS
---------------------------- */
function getLinks($url)
{
    $html = @file_get_contents($url);
    if (!$html) return [];

    preg_match_all('/href=["\']([^"\']+)["\']/i', $html, $m);

    return $m[1] ?? [];
}

/* ----------------------------
   NORMALIZE URL
---------------------------- */
function normalize($baseUrl, $href)
{
    $href = strtok($href, '#');
    $href = strtok($href, '?');

    if (strpos($href, 'http') === 0) return $href;

    return rtrim($baseUrl, '/') . '/' . ltrim($href, '/');
}

/* ----------------------------
   CRAWLER
---------------------------- */
function crawl($url, $baseUrl, &$visited, &$urls)
{
    if (isset($visited[$url])) return;

    $visited[$url] = true;

    $links = getLinks($url);

    foreach ($links as $href) {

        if (
            strpos($href, 'javascript:') === 0 ||
            strpos($href, 'mailto:') === 0 ||
            strpos($href, 'tel:') === 0
        ) continue;

        $full = normalize($baseUrl, $href);

        if (!isValidUrl($full, $baseUrl)) continue;

        addUrl($urls, $full);

        $path = parse_url($full, PHP_URL_PATH);

        if (
            $path === '/' ||
            $path === '/game/' ||
            preg_match('/\.html$/i', $path)
        ) {
            crawl($full, $baseUrl, $visited, $urls);
        }
    }
}

/* ----------------------------
   START CRAWLING
---------------------------- */
crawl($baseUrl . '/', $baseUrl, $visited, $urls);
crawl($baseUrl . '/game/', $baseUrl, $visited, $urls);

/* FORCE IMPORTANT URLS */
addUrl($urls, $baseUrl . '/');
addUrl($urls, $baseUrl . '/game/');

$urls = array_unique($urls);

/* ----------------------------
   GENERATE XML
---------------------------- */
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;

$urlset = $xml->createElement('urlset');
$urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');

foreach ($urls as $url) {

    $isHome = rtrim($url, '/') === rtrim($baseUrl, '/');
    $priority = $isHome ? "1.0" : "0.9";

    $node = $xml->createElement('url');

    $node->appendChild($xml->createElement('loc', htmlspecialchars($url)));
    $node->appendChild($xml->createElement('lastmod', date('Y-m-d')));
    $node->appendChild($xml->createElement('changefreq', 'daily'));
    $node->appendChild($xml->createElement('priority', $priority));

    $urlset->appendChild($node);
}

$xml->appendChild($urlset);

/* SAVE FILE (NO OUTPUT!) */
$xml->save($sitemapFile);

?>