From d6ad7d66e0186761126a7da37907413e0dfbdd89 Mon Sep 17 00:00:00 2001 From: Miraty Date: Mon, 14 Aug 2023 01:54:54 +0200 Subject: [PATCH] pg-act/ns/zone-add: modernize code presentation --- pg-act/ns/zone-add.php | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/pg-act/ns/zone-add.php b/pg-act/ns/zone-add.php index 97d6c98..fb1fcb5 100644 --- a/pg-act/ns/zone-add.php +++ b/pg-act/ns/zone-add.php @@ -1,17 +1,18 @@ $_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('/^(?[0-9a-f]{8})-(?[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.'));