Polyfill idn_to_*()

This commit is contained in:
the-djmaze 2024-03-11 12:32:59 +01:00
parent f726ac7061
commit 7d66adbe3c
6 changed files with 32 additions and 29 deletions

View file

@ -74,7 +74,7 @@ class ConnectSettings implements \JsonSerializable
public static function Host() : string public static function Host() : string
{ {
return \SnappyMail\IDN::toAscii($this->host); return \idn_to_ascii($this->host);
} }
public static function fromArray(array $aSettings) : self public static function fromArray(array $aSettings) : self

View file

@ -4,9 +4,6 @@ namespace RainLoop\Model;
use MailSo\Net\Enumerations\ConnectionSecurityType; use MailSo\Net\Enumerations\ConnectionSecurityType;
// \SnappyMail\IDN::toAscii(
// \SnappyMail\IDN::toUtf8(
class Domain implements \JsonSerializable class Domain implements \JsonSerializable
{ {
private string $Name; private string $Name;
@ -187,17 +184,17 @@ class Domain implements \JsonSerializable
if (\strlen($sName) && \strlen($aDomain['imap_host'])) { if (\strlen($sName) && \strlen($aDomain['imap_host'])) {
$oDomain = new self($sName); $oDomain = new self($sName);
$oDomain->IMAP->host = \SnappyMail\IDN::toUtf8($aDomain['imap_host']); $oDomain->IMAP->host = \idn_to_utf8($aDomain['imap_host']);
$oDomain->IMAP->port = (int) $aDomain['imap_port']; $oDomain->IMAP->port = (int) $aDomain['imap_port'];
$oDomain->IMAP->type = self::StrConnectionSecurityTypeToCons($aDomain['imap_secure'] ?? ''); $oDomain->IMAP->type = self::StrConnectionSecurityTypeToCons($aDomain['imap_secure'] ?? '');
$oDomain->IMAP->shortLogin = !empty($aDomain['imap_short_login']); $oDomain->IMAP->shortLogin = !empty($aDomain['imap_short_login']);
$oDomain->Sieve->enabled = !empty($aDomain['sieve_use']); $oDomain->Sieve->enabled = !empty($aDomain['sieve_use']);
$oDomain->Sieve->host = \SnappyMail\IDN::toUtf8($aDomain['sieve_host']); $oDomain->Sieve->host = \idn_to_utf8($aDomain['sieve_host']);
$oDomain->Sieve->port = (int) ($aDomain['sieve_port'] ?? 4190);; $oDomain->Sieve->port = (int) ($aDomain['sieve_port'] ?? 4190);;
$oDomain->Sieve->type = self::StrConnectionSecurityTypeToCons($aDomain['sieve_secure'] ?? ''); $oDomain->Sieve->type = self::StrConnectionSecurityTypeToCons($aDomain['sieve_secure'] ?? '');
$oDomain->SMTP->host = \SnappyMail\IDN::toUtf8($aDomain['smtp_host']); $oDomain->SMTP->host = \idn_to_utf8($aDomain['smtp_host']);
$oDomain->SMTP->port = (int) ($aDomain['smtp_port'] ?? 25); $oDomain->SMTP->port = (int) ($aDomain['smtp_port'] ?? 25);
$oDomain->SMTP->type = self::StrConnectionSecurityTypeToCons($aDomain['smtp_secure'] ?? ''); $oDomain->SMTP->type = self::StrConnectionSecurityTypeToCons($aDomain['smtp_secure'] ?? '');
$oDomain->SMTP->shortLogin = !empty($aDomain['smtp_short_login']); $oDomain->SMTP->shortLogin = !empty($aDomain['smtp_short_login']);

View file

@ -0,0 +1,13 @@
<?php
if (!\function_exists('idn_to_ascii')) {
function idn_to_ascii(string $domain) {
return \SnappyMail\Intl\Idn::toAscii($domain);
}
}
if (!\function_exists('idn_to_utf8')) {
function idn_to_utf8(string $domain) {
return \SnappyMail\Intl\Idn::toUtf8($domain);
}
}

View file

@ -1,23 +1,12 @@
<?php <?php
/**
* Internationalized domain names
*/
namespace SnappyMail; namespace SnappyMail;
abstract class IDN abstract class IDN
{ {
public static function toAscii(string $domain) : string
{
return \function_exists('idn_to_ascii')
? \idn_to_ascii($domain)
: Intl\Idn::toAscii($domain);
}
public static function toUtf8(string $domain) : string
{
return \function_exists('idn_to_utf8')
? \idn_to_utf8($domain)
: Intl\Idn::toUtf8($domain);
}
public static function anyToAscii(string $string) : string public static function anyToAscii(string $string) : string
{ {
if (\preg_match('#[:/]#', $string)) { if (\preg_match('#[:/]#', $string)) {
@ -26,7 +15,7 @@ abstract class IDN
if (\strpos($string, '@')) { if (\strpos($string, '@')) {
return static::emailAddress($string, true); return static::emailAddress($string, true);
} }
return static::toAscii($string); return \idn_to_ascii($string);
} }
public static function anyToUtf8(string $string) : string public static function anyToUtf8(string $string) : string
@ -37,7 +26,7 @@ abstract class IDN
if (\strpos($string, '@')) { if (\strpos($string, '@')) {
return static::emailAddress($string, false); return static::emailAddress($string, false);
} }
return static::toUtf8($string); return \idn_to_utf8($string);
} }
public static function emailToAscii(string $address) : string public static function emailToAscii(string $address) : string
@ -68,12 +57,12 @@ abstract class IDN
$local = \implode('@', $local); $local = \implode('@', $local);
$arr = \explode('.', $domain); $arr = \explode('.', $domain);
foreach ($arr as $k => $v) { foreach ($arr as $k => $v) {
$conv = $toAscii ? static::toAscii($v) : static::toUtf8($v); $conv = $toAscii ? \idn_to_ascii($v) : \idn_to_utf8($v);
if ($conv) $arr[$k] = $conv; if ($conv) $arr[$k] = $conv;
} }
return $local . '@' . \implode('.', $arr); return $local . '@' . \implode('.', $arr);
} }
return $toAscii ? static::toAscii($address) : static::toUtf8($address); return $toAscii ? \idn_to_ascii($address) : \idn_to_utf8($address);
} }
private static function uri(string $address, bool $toAscii) : string private static function uri(string $address, bool $toAscii) : string
@ -83,7 +72,7 @@ abstract class IDN
if (isset($parsed['host'])) { if (isset($parsed['host'])) {
$arr = \explode('.', $parsed['host']); $arr = \explode('.', $parsed['host']);
foreach ($arr as $k => $v) { foreach ($arr as $k => $v) {
$conv = $toAscii ? static::toAscii($v) : static::toUtf8($v); $conv = $toAscii ? \idn_to_ascii($v) : \idn_to_utf8($v);
if ($conv) $arr[$k] = $conv; if ($conv) $arr[$k] = $conv;
} }
$parsed['host'] = \implode('.', $arr); $parsed['host'] = \implode('.', $arr);
@ -110,12 +99,12 @@ abstract class IDN
// parse_url seems to have failed, try without it // parse_url seems to have failed, try without it
$arr = \explode('.', $address); $arr = \explode('.', $address);
foreach ($arr as $k => $v) { foreach ($arr as $k => $v) {
$conv = $toAscii ? static::toAscii($v) : static::toUtf8($v); $conv = $toAscii ? \idn_to_ascii($v) : \idn_to_utf8($v);
if ($conv) $arr[$k] = $conv; if ($conv) $arr[$k] = $conv;
} }
return \implode('.', $arr); return \implode('.', $arr);
} }
return $toAscii ? static::toAscii($address) : static::toUtf8($address); return $toAscii ? \idn_to_ascii($address) : \idn_to_utf8($address);
} }
} }

View file

@ -12,6 +12,10 @@ if (!extension_loaded('ctype')) {
require __DIR__ . '/app/libraries/polyfill/ctype.php'; require __DIR__ . '/app/libraries/polyfill/ctype.php';
} }
if (!extension_loaded('intl')) {
require __DIR__ . '/app/libraries/polyfill/intl.php';
}
if (!defined('APP_VERSION')) { if (!defined('APP_VERSION')) {
define('APP_VERSION', basename(__DIR__)); define('APP_VERSION', basename(__DIR__));
} }

View file

@ -107,7 +107,7 @@ if (defined('APP_VERSION')) {
} }
} }
$sName = \SnappyMail\IDN::toAscii(mb_strtolower(gethostname())); $sName = idn_to_ascii(mb_strtolower(gethostname()));
$sFile = APP_PRIVATE_DATA.'domains/'.$sName.'.json'; $sFile = APP_PRIVATE_DATA.'domains/'.$sName.'.json';
if (!file_exists($sFile) && !file_exists(APP_PRIVATE_DATA.'domains/'.$sName.'.ini')) { if (!file_exists($sFile) && !file_exists(APP_PRIVATE_DATA.'domains/'.$sName.'.ini')) {
$config = json_decode(file_get_contents(__DIR__ . '/app/domains/default.json'), true); $config = json_decode(file_get_contents(__DIR__ . '/app/domains/default.json'), true);