Skip to content

Commit

Permalink
add change resolve bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator committed Nov 10, 2023
1 parent 3ba6a7f commit 65f3067
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/Utils/Addresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ public static function filterDomain(mixed $domainName): ?string
// get domain name if the given argument is an url
// protocol://user:[email protected]:port/path
if (preg_match(
'~^(?:(?:[a-z]+:)?//)?(?:[^:]*(?::[^@]*)?@)?([^/]+)(?::\d+|[#?/]|$)~',
'~^(?:(?:[a-z]+:)?//)?(?:[^:]*(?::[^@]*)?@)?([^/:]+)(?::\d+|[#?/]|$)~',
$domainName,
$match
)) {
$domainName = $match[1];
}

if ($domainName === 'localhost') {
return $domainName;
}
Expand Down Expand Up @@ -187,31 +188,36 @@ public static function filterDomain(mixed $domainName): ?string
if ($intl_idn) {
$domainName = $isAscii ? idn_to_utf8($domainName) : idn_to_ascii($domainName);
// revert
return ($isAscii ? idn_to_ascii($domainName) : idn_to_utf8($domainName)) ?: null;
}

foreach ($labels as &$label) {
if (preg_match('/[^x00-x7F]/', $domainName)) {
$label = 'xn--'.self::punycodeEncode($label);
if (!$label || strlen($label) > 63) {
return null;
$domainName = ($isAscii ? idn_to_ascii($domainName) : idn_to_utf8($domainName)) ?: null;
if (!$domainName) {
return null;
}
$labels = $domainName;
} else {
foreach ($labels as &$label) {
if (preg_match('/[^x00-x7F]/', $domainName)) {
$label = 'xn--' . self::punycodeEncode($label);
if (!$label || strlen($label) > 63) {
return null;
}
}
}
unset($label);
$labels = implode('.', $labels);
}

unset($label);
$labels = implode('.', $labels);
if (strlen($labels) > 255
|| strlen($labels) < 2
// domain except localhost should contain extension
// xn-- // (?i)[a-z0-9-]
|| !preg_match(
'~[a-z0-9\-](?:[a-z0-9-]*[a-z0-9]+(?:\.[a-z0-9-]*[a-z0-9]+)*)?$~',
'~[a-z0-9\-](?:[a-z0-9-]*[a-z0-9]+(?:\.[a-z0-9-]*[a-z0-9]+)*)?\.(?:xn--[a-z0-9_]+|[a-z]+)$~',
$labels
)
) {
return null;
}


return $domainName;
}

Expand Down

0 comments on commit 65f3067

Please sign in to comment.