diff --git a/src/Handlers/AbstractDnsHandler.php b/src/Handlers/AbstractDnsHandler.php index 2e82b71..8116078 100644 --- a/src/Handlers/AbstractDnsHandler.php +++ b/src/Handlers/AbstractDnsHandler.php @@ -88,26 +88,12 @@ protected function validateHostName(string $hostName): void $hostnameErrorInfo = 'Invalid hostname ' . json_encode($hostName); - if (strlen($hostName) < 3) { + if (!filter_var($hostName, FILTER_VALIDATE_DOMAIN)) { throw new DnsHandlerException( - $hostnameErrorInfo . ' length. It must be 3 or more!', - DnsHandlerException::HOSTNAME_LENGTH_TOO_SMALL - ); - } - - if (!preg_match(Regex::DOMAIN_OR_SUBDOMAIN, $hostName)) { - throw new DnsHandlerException( - $hostnameErrorInfo . ' format! (characters "A-Za-z0-9.-", max length 63 chars allowed)', + $hostnameErrorInfo . ' format!', DnsHandlerException::HOSTNAME_FORMAT_INVALID ); } - - if (!preg_match(Regex::HOSTNAME_LENGTH, $hostName)) { - throw new DnsHandlerException( - $hostnameErrorInfo . ' length! (min 3, max 253 characters allowed)', - DnsHandlerException::HOSTNAME_LENGTH_INVALID - ); - } } /** diff --git a/src/Records/DnsUtils.php b/src/Records/DnsUtils.php index 2e7294b..dd00840 100644 --- a/src/Records/DnsUtils.php +++ b/src/Records/DnsUtils.php @@ -13,7 +13,7 @@ public static function isValidDomainOrSubdomain(string $domain): bool if (empty($domain) || strlen($domain) < 4) { return false; } - return preg_match(Regex::DOMAIN_OR_SUBDOMAIN, $domain) === 1; + return (bool) filter_var($domain, FILTER_VALIDATE_DOMAIN); } public static function ipV6Shortener(string $ipv6): string diff --git a/src/Records/ExtendedTxtRecords.php b/src/Records/ExtendedTxtRecords.php index c2975f4..769d616 100644 --- a/src/Records/ExtendedTxtRecords.php +++ b/src/Records/ExtendedTxtRecords.php @@ -103,7 +103,7 @@ public function isParentHostName($host): bool */ private function isDomainOrSubdomainHostName(string $host): bool { - return preg_match(Regex::DOMAIN_OR_SUBDOMAIN, $host) === 1; + return (bool) filter_var($host, FILTER_VALIDATE_DOMAIN); } private function isDomainKeyHostName(string $host): bool diff --git a/src/Regex.php b/src/Regex.php index 35c8522..5e45a9f 100644 --- a/src/Regex.php +++ b/src/Regex.php @@ -4,9 +4,6 @@ class Regex { - public const DOMAIN_OR_SUBDOMAIN = '/^(([\w\d\_\-]+){1,63}\.)+(\w+){2,63}$/i'; - public const HOSTNAME_LENGTH = '/^.{3,253}$/'; - public const SPF_VALIDATION = '/^v=spf1 ([a-z0-9:.\/ ~\-_\+]+)/i'; public const DKIM_SELECTOR_VALUE = '/^([\w\_]+)\._domainkey.*/';