<?php
// Badi sites ke liye time limit badha dein
set_time_limit(600); 

class CurlSitemapGenerator {
    private $visitedUrls = [];
    private $baseUrl;
    private $maxUrls;
    private $baseHost;

    public function __construct($baseUrl, $maxUrls = 100) {
        $this->baseUrl = rtrim($baseUrl, '/');
        $this->baseHost = parse_url($this->baseUrl, PHP_URL_HOST);
        $this->maxUrls = $maxUrls;
    }

    // Naya function jo cURL use karke HTML aur Final Redirected URL dono layega
    private function fetchUrl($url) {
        $ch = curl_init();
        
        // cURL Options setup karna
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // HTML return kare
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Redirects ko auto-follow kare (Main Magic!)
        curl_setopt($ch, CURLOPT_MAXREDIRS, 5); // Max 5 redirects tak follow kare
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // SSL errors ignore kare
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
        
        $html = curl_exec($ch);
        
        // Final URL pata karna (agar redirect hua hoga toh ye naya URL dega)
        $finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
        
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        // Agar page not found (404) ya server error (500) hai, toh ignore karein
        if ($httpCode >= 400 || !$html) {
            return false;
        }

        return ['html' => $html, 'finalUrl' => $finalUrl];
    }

    public function crawl($url) {
        if (count($this->visitedUrls) >= $this->maxUrls) return;
        
        // Shuruwati URL check
        $url = rtrim($url, '/'); 
        if (!filter_var($url, FILTER_VALIDATE_URL) || in_array($url, $this->visitedUrls)) {
            return;
        }

        // Host check taaki bahar na jaye
        $urlHost = parse_url($url, PHP_URL_HOST);
        if ($urlHost !== $this->baseHost) {
            return;
        }

        // cURL se page fetch karein aur redirect check karein
        $response = $this->fetchUrl($url);
        if (!$response) return;

        $html = $response['html'];
        // Yeh hai wo actual final URL jahan server ne bheja hai
        $finalUrl = rtrim($response['finalUrl'], '/'); 

        // Agar final URL bhi pehle se visited hai, toh aage na badhein
        if (in_array($finalUrl, $this->visitedUrls)) {
            return;
        }

        echo "Scanned (Final URL): " . htmlspecialchars($finalUrl) . "<br>";
        
        // Hum ab us original URL ki jagah 'Final URL' ko list mein save karenge
        $this->visitedUrls[] = $finalUrl;

        // HTML ko parse karein naye links dhoondhne ke liye
        $dom = new DOMDocument();
        @$dom->loadHTML($html);
        $links = $dom->getElementsByTagName('a');

        foreach ($links as $link) {
            $href = $link->getAttribute('href');
            $nextUrl = $this->buildUrl($href, $finalUrl);
            
            if ($nextUrl) {
                $nextUrl = rtrim($nextUrl, '/');
                if (!in_array($nextUrl, $this->visitedUrls)) {
                    $this->crawl($nextUrl);
                }
            }
        }
    }

private function buildUrl($href, $currentUrl) {
        $href = trim($href);
        $href = explode('#', $href)[0]; 
        
        // Unwanted links filter
        if (preg_match('/^(javascript|mailto|tel|whatsapp|sms|skype|viber):/i', $href)) {
            return false;
        }

        if (empty($href)) {
            return false;
        }

        // URL building
        if (!preg_match('~^(?:f|ht)tps?://~i', $href)) {
            if (strpos($href, '/') === 0) {
                return $this->baseUrl . $href; 
            } else {
                // === YAHAN FIX KIYA GAYA HAI ===
                // is_dir() hata diya gaya hai taaki open_basedir ka error na aaye
                $path = parse_url($currentUrl, PHP_URL_PATH) ?: '';
                
                // Agar path ke end mein slash (/) nahi hai, toh aakhiri hisse (file) ko hata dein
                if (substr($path, -1) !== '/') {
                    $path = dirname($path);
                }
                
                // Windows server pe dirname '\' return kar sakta hai, isliye usko '/' banayein
                $path = str_replace('\\', '/', $path); 
                
                // Extra slashes hatayein aur clean URL banayein
                $path = rtrim($path, '/');
                
                return rtrim($this->baseUrl, '/') . $path . '/' . ltrim($href, '/'); 
            }
        }

        return $href;
    }

    public function generateXml($filePath = 'sitemap.xml') {
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');

        foreach ($this->visitedUrls as $url) {
            // Sitemap me sabhi URLs ke last me slash (/) ke liye
            $finalUrl = rtrim($url, '/') . '/'; 
            if ($finalUrl === $this->baseUrl . '/') {
                $finalUrl = $this->baseUrl;
            }

            $urlElement = $xml->addChild('url');
            $urlElement->addChild('loc', htmlspecialchars($finalUrl));
            $urlElement->addChild('lastmod', date('c'));
            $urlElement->addChild('changefreq', 'daily');
            $urlElement->addChild('priority', '0.8');
        }

        $xml->asXML($filePath);
        echo "<hr><strong>Success! Auto-Redirect Sitemap generate ho gaya hai. Total Pages: " . count($this->visitedUrls) . "</strong>";
    }
}

// === BOT KO RUN KAREIN ===

$targetWebsite = "https://satta4umatka.live"; 

$bot = new CurlSitemapGenerator($targetWebsite, 100); 

$bot->crawl($targetWebsite);
$bot->generateXml('sitemap.xml');
?>