<?php
header('Content-Type: application/json');

$domain = $_POST['domain'] ?? '';

if (!$domain) {
    echo json_encode(['ok' => false, 'error' => 'Missing domain parameter']);
    exit;
}

// Paths
$vhostApache = "/etc/httpd/conf.d/$domain.conf";
$vhostNginx  = "/etc/nginx/conf.d/$domain.conf";
$webRoot     = "/var/www/$domain";
$mailDir     = "/var/mail/$domain";
$zoneFile    = "/var/named/chroot/var/named/$domain.hosts";

// Delete Apache/Nginx vhost
if (file_exists($vhostApache)) unlink($vhostApache);
if (file_exists($vhostNginx)) unlink($vhostNginx);

// Delete web root
if (is_dir($webRoot)) {
    exec("rm -rf " . escapeshellarg($webRoot));
}

// Delete mail directory
if (is_dir($mailDir)) {
    exec("rm -rf " . escapeshellarg($mailDir));
}

// Delete DNS zone
if (file_exists($zoneFile)) unlink($zoneFile);

echo json_encode([
    'ok' => true,
    'output' => "Domain $domain fully deleted: web root, mail, vhost, and zone removed."
]);
?>

