pg-act/ns/zone-add: modernize code presentation

This commit is contained in:
Miraty 2023-08-14 01:54:54 +02:00
parent 3636e181c2
commit d6ad7d66e0
1 changed files with 15 additions and 14 deletions

View File

@ -1,17 +1,18 @@
<?php declare(strict_types=1);
$_POST['domain'] = formatAbsoluteDomain($_POST['domain']);
$domain = formatAbsoluteDomain($_POST['domain']);
if (query('select', 'zones', ['zone' => $_POST['domain']], 'zone') !== [])
if (query('select', 'zones', ['zone' => $domain], 'zone') !== [])
output(403, _('This zone already exists on the service.'));
$parent_authoritatives = array_column(kdig(name: ltrim(strstr($_POST['domain'], '.'), '.'), type: 'NS', server: (CONF['ns']['local_only_check'] ? CONF['reg']['address'] : NULL))['answerRRs'], 'rdataNS');
$parent_domain = ltrim(strstr($domain, '.'), '.');
$parent_authoritatives = array_column(kdig(name: $parent_domain, type: 'NS', server: (CONF['ns']['local_only_check'] ? CONF['reg']['address'] : NULL))['answerRRs'] ?? [], 'rdataNS');
if ($parent_authoritatives === [])
output(403, _('Parent zone\'s name servers not found.'));
foreach ($parent_authoritatives as $parent_authoritative)
checkAbsoluteDomainFormat($parent_authoritative);
$ns_records = array_column(kdig(name: $_POST['domain'], type: 'NS', server: (CONF['ns']['local_only_check'] ? CONF['reg']['address'] : $parent_authoritatives[0]))['authorityRRs'], 'rdataNS');
$ns_records = array_column(kdig(name: $domain, type: 'NS', server: (CONF['ns']['local_only_check'] ? CONF['reg']['address'] : $parent_authoritatives[0]))['authorityRRs'], 'rdataNS');
if (preg_match('/^(?<salt>[0-9a-f]{8})-(?<hash>[0-9a-f]{32})\._domain-verification\.' . preg_quote(SERVER_NAME, '/') . '\.$/Dm', implode(LF, $ns_records), $matches) !== 1)
output(403, _('NS authentication record not found.'));
@ -20,13 +21,13 @@ checkAuthToken($matches['salt'], $matches['hash']);
rateLimit();
insert('zones', [
'zone' => $_POST['domain'],
'zone' => $domain,
'username' => $_SESSION['id'],
]);
$knotZonePath = CONF['ns']['knot_zones_path'] . '/' . $_POST['domain'] . 'zone';
$knotZone = implode(' ', [
$_POST['domain'],
$zone_path = CONF['ns']['knot_zones_path'] . '/' . $domain . 'zone';
$zone_content = implode(' ', [
$domain,
NS_SOA_VALUES['ttl'],
'SOA',
CONF['ns']['servers'][0],
@ -38,16 +39,16 @@ $knotZone = implode(' ', [
NS_SOA_VALUES['negative'],
]) . LF;
foreach (CONF['ns']['servers'] as $server)
$knotZone .= $_POST['domain'] . ' 86400 NS ' . $server . LF;
$knotZone .= $_POST['domain'] . ' 86400 CSYNC 0 1 NS' . LF;
if (is_int(file_put_contents($knotZonePath, $knotZone)) !== true)
$zone_content .= $domain . ' 86400 NS ' . $server . LF;
$zone_content .= $domain . ' 86400 CSYNC 0 1 NS' . LF;
if (file_put_contents($zone_path, $zone_content) === false)
output(500, 'Failed to write new zone file.');
if (chmod($knotZonePath, 0660) !== true)
if (chmod($zone_path, 0660) !== true)
output(500, 'Failed to chmod new zone file.');
knotcConfExec([
['conf-set', 'zone[' . $_POST['domain'] . ']'],
['conf-set', 'zone[' . $_POST['domain'] . '].template', 'servnest'],
['conf-set', 'zone[' . $domain . ']'],
['conf-set', 'zone[' . $domain . '].template', 'servnest'],
]);
output(200, _('Zone created.'));