Files
dns-web-ui/api/getzones.php
2024-06-13 09:45:05 +02:00

48 lines
1.1 KiB
PHP

<?php
header("Content-Type: content/json");
function stop($error)
{
http_response_code(513);
printf(json_encode($error));
die;
}
$_SERVER['REQUEST_METHOD'] !== "GET" && stop("Method " . $_SERVER['REQUEST_METHOD'] . " not allowed");
$result_code = 0;
passthru("/usr/bin/rndc dumpdb -zones", $result_code);
$result_code === 0 || stop("Unable to get zone");
sleep(2);
$HANDLE = fopen("/var/named/named_dump.db", "r");
$HANDLE || stop("Unable to read zone db");
$ZONE = new stdClass();
$CURRENTZONE = "";
while (($LINE = fgets($HANDLE)) !== false) {
if (str_starts_with($LINE, ";")) continue;
$LINE = preg_split("/\s+/", $LINE);
if ($LINE[3] === "SOA") {
$CURRENTZONE = $LINE[0];
$ZONE->$CURRENTZONE = [];
}
$RECORD = new stdClass();
$RECORD->name = $LINE[0];
array_shift($LINE);
$RECORD->ttl = $LINE[0];
array_shift($LINE);
$RECORD->class = $LINE[0];
array_shift($LINE);
$RECORD->type = $LINE[0];
array_shift($LINE);
$RECORD->rr = implode(" ", $LINE);
array_push($ZONE->$CURRENTZONE, $RECORD);
unset($RECORD);
}
printf(json_encode($ZONE));