<?php
// sitemap_generator.php
date_default_timezone_set('Europe/Berlin');

// Configuration
$base_url = "https://www.ig-triodos-tar-inhaber.de";
$sitemap_file = __DIR__ . '/sitemap.xml';
$desired_url_count = 5000;
$start_date = '2025-06-01';
$end_date = '2025-06-31';
$salt = 'your_secure_salt_' . md5($base_url);

// Generate sitemap - write directly to file for memory efficiency
$fp = fopen($sitemap_file, 'w');
fwrite($fp, '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL);
fwrite($fp, '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL);

$start_timestamp = strtotime($start_date);
$end_timestamp = strtotime($end_date);
$date_interval = ($end_timestamp - $start_timestamp) / $desired_url_count;

for ($i = 0; $i < $desired_url_count; $i++) {
    $hash = substr(md5($i . $salt . microtime()), 0, 10);
    $url_date = date('Y/m', $start_timestamp + ($i * $date_interval));
    $lastmod = (new DateTime('now', new DateTimeZone('+03:00')))->format('c');
    
    $url_entry = "\t<url>\n";
    $url_entry .= "\t\t<loc>" . htmlspecialchars($base_url . '/' . $url_date . '/' . $hash . '.html') . "</loc>\n";
    $url_entry .= "\t\t<lastmod>" . $lastmod . "</lastmod>\n";
    $url_entry .= "\t\t<changefreq>always</changefreq>\n";
    $url_entry .= "\t\t<priority>1.0</priority>\n";
    $url_entry .= "\t</url>\n";
    
    fwrite($fp, $url_entry);
    
    if ($i % 1000 == 0) {
        echo "Generated $i URLs...\n";
        fflush($fp); // Flush buffer to file
    }
}

fwrite($fp, '</urlset>');
fclose($fp);

echo "Sitemap generated with $desired_url_count URLs\n";
echo "Saved to: $sitemap_file\n";