<?php
// -----------------------------
// Grundkonfiguration
// -----------------------------
error_reporting(0);
ini_set('display_errors', 0);

// Basis-URL deiner Website
$baseUrl = "https://www.ilsystem.de";

// Webroot-Verzeichnis
$rootDir = $_SERVER['DOCUMENT_ROOT'];

// Dateitypen, die als Seite gelten
$validExtensions = ["php", "html", "htm"];

// Maximal URLs pro Sitemap (Google-Limit)
$maxUrls = 50000;

// Standardwerte für Priorität und Changefreq
$defaultPriority   = "0.7";
$defaultChangefreq = "monthly";

// -----------------------------
// Funktion: Dateien rekursiv scannen
// -----------------------------
function scanDirRecursive($dir, $rootDir, $validExtensions) {
    $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
    $files = [];
    foreach ($rii as $file) {
        if ($file->isDir()) continue;
        $ext = pathinfo($file->getFilename(), PATHINFO_EXTENSION);
        if (!in_array(strtolower($ext), $validExtensions)) continue;
        $files[] = str_replace($rootDir, "", $file->getPathname());
    }
    return $files;
}

// -----------------------------
// Dateien scannen
// -----------------------------
$files = scanDirRecursive($rootDir, $rootDir, $validExtensions);

// In Gruppen aufteilen
$sitemapCount = ceil(count($files) / $maxUrls);
$sitemaps = [];

// -----------------------------
// Teil-Sitemaps erzeugen
// -----------------------------
for ($i = 0; $i < $sitemapCount; $i++) {
    $chunk = array_slice($files, $i * $maxUrls, $maxUrls);

    $xml  = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
    $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;

    foreach ($chunk as $filePath) {
        $urlPath = str_replace(["index.php","index.html","index.htm"], "", $filePath);
        $url = rtrim($baseUrl . $urlPath, "/");

        $lastmod = date("Y-m-d", filemtime($rootDir . $filePath));

        // Priorität & Changefreq festlegen
        if ($url === $baseUrl . "/") {
            $priority = "1.0";
            $changefreq = "weekly";
        } elseif (strpos($urlPath, "/consulting") !== false || strpos($urlPath, "/engineering") !== false) {
            $priority = "0.9";
            $changefreq = "weekly";
        } else {
            $priority = $defaultPriority;
            $changefreq = $defaultChangefreq;
        }

        $xml .= "  <url>" . PHP_EOL;
        $xml .= "    <loc>" . htmlspecialchars($url, ENT_QUOTES | ENT_XML1) . "</loc>" . PHP_EOL;
        $xml .= "    <lastmod>$lastmod</lastmod>" . PHP_EOL;
        $xml .= "    <priority>$priority</priority>" . PHP_EOL;
        $xml .= "    <changefreq>$changefreq</changefreq>" . PHP_EOL;
        $xml .= "  </url>" . PHP_EOL;
    }

    $xml .= '</urlset>' . PHP_EOL;

    $sitemapFile = $rootDir . "/sitemap-" . ($i+1) . ".xml";
    file_put_contents($sitemapFile, $xml);

    $sitemaps[] = "sitemap-" . ($i+1) . ".xml";
}

// -----------------------------
// Sitemap-Index erzeugen
// -----------------------------
$xmlIndex  = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
$xmlIndex .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;

foreach ($sitemaps as $sitemap) {
    $lastmod = date("Y-m-d");
    $xmlIndex .= "  <sitemap>" . PHP_EOL;
    $xmlIndex .= "    <loc>$baseUrl/$sitemap</loc>" . PHP_EOL;
    $xmlIndex .= "    <lastmod>$lastmod</lastmod>" . PHP_EOL;
    $xmlIndex .= "  </sitemap>" . PHP_EOL;
}

$xmlIndex .= '</sitemapindex>' . PHP_EOL;

// Index-Datei speichern
file_put_contents($rootDir . "/sitemap-index.xml", $xmlIndex);

// -----------------------------
// Google & Bing Pings
// -----------------------------
$sitemapIndexUrl = $baseUrl . "/sitemap-index.xml";
$pingUrls = [
    "https://www.google.com/ping?sitemap=" . urlencode($sitemapIndexUrl),
    "https://www.bing.com/ping?sitemap="   . urlencode($sitemapIndexUrl)
];

foreach ($pingUrls as $ping) {
    @file_get_contents($ping); // Fehler werden unterdrückt
}

// -----------------------------
// Ausgabe (optional für CLI-Test)
// -----------------------------
echo "Sitemap erfolgreich generiert.\n";
foreach ($sitemaps as $sitemap) {
    echo "- $sitemap\n";
}
echo "- sitemap-index.xml\n";
echo "Google & Bing wurden gepingt.\n";
