<?php
$name = isset($_GET["name"]) ? $_GET["name"] : "running-config.xml";
$name = preg_replace('/[^A-Za-z0-9._-]/', '', $name);
if ($name === '') {
    $name = "running-config.xml";
}

if (!isset($_GET["dl"])) {
    // First hit: simulate the backend preparing the file, then redirect the
    // tab to the real download URL (redirect_count=1).
    usleep(1200000); // 1.2s "preparing snapshot..."
    header("HTTP/1.1 302 Found");
    header("Location: config_export_prepare.php?dl=1&name=$name");
    exit;
}

// Final hit (dl=1): emit the actual XML attachment.
$ts = date("Y-m-d\TH:i:s");
header("HTTP/1.1 200 OK");
header("Content-Type: application/xml");
header("Content-Disposition: attachment; filename=\"$name\"");
echo "<?xml version=\"1.0\"?>\n";
echo "<config version=\"11.0.0\" ts=\"$ts\">\n";
echo "  <devices>\n";
for ($i = 0; $i < 1500; $i += 1) {
    echo "    <entry name=\"rule-$i\">";
    echo "<from>trust</from><to>untrust</to><action>allow</action>";
    echo "</entry>\n";
}
echo "  </devices>\n";
echo "</config>\n";
?>
