Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions src/DNS/Zone/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,31 @@
/**
* Import a zone from RFC 1035 master file format string.
*
* @param string $name Zone name
* @param string $content Zone file content
* @param string|null $defaultOrigin Default origin if not specified in file
* @param string|null $defaultOrigin Default origin if $ORIGIN is not specified
* @param int $defaultTTL Default TTL if not specified (default: 3600)
*
* @throws InvalidArgumentException
*/
public static function import(string $name, string $content, ?string $defaultOrigin = null, int $defaultTTL = 3600): Zone
public static function import(string $content, ?string $defaultOrigin = null, int $defaultTTL = 3600): Zone
{
$zoneName = self::canonicalizeName($name);
if ($zoneName === null) {
throw new InvalidArgumentException('Zone name must not be empty');
}

$normalizedLines = self::preprocess($content); // array<array{line:string,num:int}>
$records = [];
$soa = null;

$origin = $defaultOrigin !== null ? self::canonicalizeName($defaultOrigin) : $zoneName;
$origin = null;
$zoneName = null;
$zoneNameFromDefault = false;

if ($defaultOrigin !== null) {
$origin = self::canonicalizeName($defaultOrigin);
if ($origin === null) {
throw new InvalidArgumentException('Default origin must not be empty');
}
$zoneName = $origin;
$zoneNameFromDefault = true;
}

$lastOwner = null;
$lastTTL = $defaultTTL;
$lastClass = Record::CLASS_IN;
Expand All @@ -56,7 +62,17 @@ public static function import(string $name, string $content, ?string $defaultOri
}

// Directives
if (self::handleDirectives($line, $origin, $lastTTL)) {
$directive = self::handleDirectives($line, $origin, $lastTTL);
if ($directive !== null) {
if ($directive === 'origin') {
if ($origin === null) {
throw new InvalidArgumentException('$ORIGIN directive must not be empty');
}
if ($zoneName === null || $zoneNameFromDefault) {
$zoneName = $origin;
$zoneNameFromDefault = false;
}
}
continue;
}

Expand Down Expand Up @@ -101,6 +117,10 @@ class: $rr['class'],
throw new InvalidArgumentException('No SOA record found in zone file');
}

if ($zoneName === null) {
throw new InvalidArgumentException('Unable to determine zone name: provide an $ORIGIN directive or defaultOrigin.');
}

return new Zone($zoneName, $records, $soa);
}

Expand Down Expand Up @@ -240,28 +260,29 @@ private static function preprocess(string $content): array
}

/**
* Handle $ORIGIN / $TTL directives. Returns true if the line was a directive.
* Handle $ORIGIN / $TTL directives.
*
* @param-out string|null $origin
* @param-out int $lastTTL
* @return 'origin'|'ttl'|null
*/
private static function handleDirectives(string $line, ?string &$origin, int &$lastTTL): bool
private static function handleDirectives(string $line, ?string &$origin, int &$lastTTL): ?string
{
if (preg_match('/^\s*\$ORIGIN\s+(\S+)\s*$/i', $line, $m) === 1) {
$origin = self::canonicalizeName($m[1]);
return true;
return 'origin';
}

if (preg_match('/^\s*\$TTL\s+(\d+)\s*$/i', $line, $m) === 1) {
$lastTTL = (int) $m[1];
return true;
return 'ttl';
}

if (preg_match('/^\s*\$INCLUDE\b/i', $line) === 1) {
throw new InvalidArgumentException('$INCLUDE directive is not supported');
}

return false;
return null;
}

/**
Expand Down
Loading