Do not fail if IDNA<->UNICODE conversion fails

Signed-off-by: Christian König <ckoenig@posteo.de>
This commit is contained in:
Christian König
2023-01-05 23:16:53 +01:00
committed by yubiuser
parent 92a5154c79
commit 68ac05fb7e
2 changed files with 23 additions and 19 deletions

View File

@@ -640,7 +640,7 @@ function getGateway()
}
// Try to convert possible IDNA domain to Unicode
function convertIDNAToUnicode($unicode)
function convertIDNAToUnicode($IDNA)
{
if (extension_loaded('intl')) {
// we try the UTS #46 standard first
@@ -652,32 +652,42 @@ function convertIDNAToUnicode($unicode)
// to ensure sparkasse-gießen.de is not converted into
// sparkass-giessen.de but into xn--sparkasse-gieen-2ib.de
// as mandated by the UTS #46 standard
$unicode = idn_to_utf8($unicode, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
$unicode = idn_to_utf8($IDNA, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
} elseif (defined('INTL_IDNA_VARIANT_2003')) {
// If conversion failed, try with the (deprecated!) IDNA 2003 variant
// We have to check for its existence as support of this variant is
// scheduled for removal with PHP 8.0
// see https://wiki.php.net/rfc/deprecate-and-remove-intl_idna_variant_2003
$unicode = idn_to_utf8($unicode, IDNA_DEFAULT, INTL_IDNA_VARIANT_2003);
$unicode = idn_to_utf8($IDNA, IDNA_DEFAULT, INTL_IDNA_VARIANT_2003);
}
}
return $unicode;
// if the conversion failed (e.g. domain to long) return the original domain
if ($unicode == false) {
return $IDNA;
} else {
return $unicode;
}
}
// Convert a given (unicode) domain to IDNA ASCII
function convertUnicodeToIDNA($IDNA)
function convertUnicodeToIDNA($unicode)
{
if (extension_loaded('intl')) {
// Be prepared that this may fail and see our comments about convertIDNAToUnicode()
if (defined('INTL_IDNA_VARIANT_UTS46')) {
$IDNA = idn_to_ascii($IDNA, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
$IDNA = idn_to_ascii($unicode, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
} elseif (defined('INTL_IDNA_VARIANT_2003')) {
$IDNA = idn_to_ascii($IDNA, IDNA_DEFAULT, INTL_IDNA_VARIANT_2003);
$IDNA = idn_to_ascii($unicode, IDNA_DEFAULT, INTL_IDNA_VARIANT_2003);
}
}
return $IDNA;
// if the conversion failed (e.g. domain to long) return the original domain
if ($IDNA == false) {
return $unicode;
} else {
return $IDNA;
}
}
// Return PID of FTL (used in settings.php)