<?php
require_once __DIR__ . '/../lib/bootstrap.php';
require_once __DIR__ . '/../lib/zones.php';
require_once __DIR__ . '/../lib/serial.php';
require_once __DIR__ . '/../lib/validate.php';
require_once __DIR__ . '/../lib/backups.php';
require_once __DIR__ . '/../lib/view.php';
require_once __DIR__ . '/../lib/view_soa.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', '/tmp/soa-save-error.log');
error_reporting(E_ALL);

header('Content-Type: text/html');

$zoneDir = '/var/named/chroot/var/named';

/********************************************************************
 * 1️⃣ Validate input
 ********************************************************************/
if (!isset($_POST['editsoa'])) {
    echo "<div class='dns-error'>No zone specified.</div>";
    exit;
}

$zone = basename($_POST['editsoa']);
$zonefile = "$zoneDir/$zone.hosts";

/********************************************************************
 * 2️⃣ Load zonefile
 ********************************************************************/
$content = load_zonefile($zoneDir, $zone);
if ($content === false) {
    echo "<div class='dns-error'>Zonefile not found.</div>";
    exit;
}

/********************************************************************
 * 3️⃣ Extract new SOA fields from POST
 ********************************************************************/
$newSOA = [
    'primary'    => $_POST['soa_primary'] ?? '',
    'hostmaster' => $_POST['soa_hostmaster'] ?? '',
    'serial'     => $_POST['soa_serial'] ?? '',
    'refresh'    => $_POST['soa_refresh'] ?? '',
    'retry'      => $_POST['soa_retry'] ?? '',
    'expire'     => $_POST['soa_expire'] ?? '',
    'minimum'    => $_POST['soa_minimum'] ?? ''
];

/********************************************************************
 * 4️⃣ Replace SOA block inside zonefile
 ********************************************************************/
$pattern = '/SOA\s+\S+\s+\S+\s+\(\s*[0-9]+\s+[0-9]+\s+[0-9]+\s+[0-9]+\s+[0-9]+\s*\)/i';

$newSOAString = sprintf(
    "SOA %s %s (\n    %s ; serial\n    %s ; refresh\n    %s ; retry\n    %s ; expire\n    %s ; minimum\n)",
    $newSOA['primary'],
    $newSOA['hostmaster'],
    $newSOA['serial'],
    $newSOA['refresh'],
    $newSOA['retry'],
    $newSOA['expire'],
    $newSOA['minimum']
);

$content = preg_replace($pattern, $newSOAString, $content);

/********************************************************************
 * 5️⃣ Bump serial automatically
 ********************************************************************/
$content = bump_serial($content);

/********************************************************************
 * 6️⃣ Backup before saving
 ********************************************************************/
backup_zonefile($zoneDir, $zone);

/********************************************************************
 * 7️⃣ Save updated zonefile
 ********************************************************************/
save_zonefile($zoneDir, $zone, $content);

/********************************************************************
 * 8️⃣ Validate updated zone
 ********************************************************************/
$validation = validate_zone($zone, $zonefile);

if (strpos($validation, 'OK') === false) {
    echo "<div class='dns-error'>SOA update failed validation:<br><pre>" .
         htmlspecialchars($validation) . "</pre></div>";
    exit;
}

/********************************************************************
 * 9️⃣ Reload BIND
 ********************************************************************/
shell_exec("rndc reload " . escapeshellarg($zone));

/********************************************************************
 * 🔟 Render updated SOA + zone editor
 ********************************************************************/
$soa = parse_soa($content);

echo "<div class='dns-success'>SOA updated, serial bumped, validated, and reloaded.</div>";
echo render_soa_editor($zone, $soa);
echo render_zone_editor($zone, $content);

