Much better IDN support, fixes #73

This commit is contained in:
Pinga 2024-01-23 12:58:01 +02:00
parent 4d57ad71a1
commit 137f8170e2
16 changed files with 235 additions and 60 deletions

View file

@ -195,7 +195,7 @@ function validate_label($label, $pdo) {
if (strlen($label) < 2) {
return 'Total lenght of your domain must be greater then 2 characters';
}
if (preg_match("/(^-|^\.|-\.|\.-|--|\.\.|-$|\.$)/", $label)) {
if (strpos($label, 'xn--') === false && preg_match("/(^-|^\.|-\.|\.-|--|\.\.|-$|\.$)/", $label)) {
return 'Invalid domain name format, cannot begin or end with a hyphen (-)';
}
@ -223,6 +223,10 @@ function validate_label($label, $pdo) {
return 'Failed to fetch domain IDN table';
}
if (strpos($parts['domain'], 'xn--') === 0) {
$label = idn_to_utf8($parts['domain'], IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
}
// Check for invalid characters using fetched regex
if (!preg_match($idnRegex, $label)) {
$server->send($fd, "Domain name invalid format");
@ -247,7 +251,7 @@ function extractDomainAndTLD($urlString) {
// Parse the URL to get the host
$parts = parse_url($urlString);
$host = $parts['host'] ?? $urlString;
// Sort test TLDs by length (longest first) to match the longest possible TLD
usort($testTlds, function ($a, $b) {
return strlen($b) - strlen($a);