Idna.php
174 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * League.Uri (https://uri.thephpleague.com) |
| 5 | * |
| 6 | * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | declare (strict_types=1); |
| 12 | namespace IAWPSCOPED\League\Uri\Idna; |
| 13 | |
| 14 | use IAWPSCOPED\League\Uri\Exceptions\IdnaConversionFailed; |
| 15 | use IAWPSCOPED\League\Uri\Exceptions\IdnSupportMissing; |
| 16 | use IAWPSCOPED\League\Uri\Exceptions\SyntaxError; |
| 17 | use function defined; |
| 18 | use function function_exists; |
| 19 | use function idn_to_ascii; |
| 20 | use function idn_to_utf8; |
| 21 | use function rawurldecode; |
| 22 | use const INTL_IDNA_VARIANT_UTS46; |
| 23 | /** |
| 24 | * @see https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/uidna_8h.html |
| 25 | * @internal |
| 26 | */ |
| 27 | final class Idna |
| 28 | { |
| 29 | private const REGEXP_IDNA_PATTERN = '/[^\\x20-\\x7f]/'; |
| 30 | private const MAX_DOMAIN_LENGTH = 253; |
| 31 | private const MAX_LABEL_LENGTH = 63; |
| 32 | /** |
| 33 | * General registered name regular expression. |
| 34 | * |
| 35 | * @see https://tools.ietf.org/html/rfc3986#section-3.2.2 |
| 36 | * @see https://regex101.com/r/fptU8V/1 |
| 37 | */ |
| 38 | private const REGEXP_REGISTERED_NAME = '/ |
| 39 | (?(DEFINE) |
| 40 | (?<unreserved>[a-z0-9_~\\-]) # . is missing as it is used to separate labels |
| 41 | (?<sub_delims>[!$&\'()*+,;=]) |
| 42 | (?<encoded>%[A-F0-9]{2}) |
| 43 | (?<reg_name>(?:(?&unreserved)|(?&sub_delims)|(?&encoded))*) |
| 44 | ) |
| 45 | ^(?:(?®_name)\\.)*(?®_name)\\.?$ |
| 46 | /ix'; |
| 47 | /** |
| 48 | * IDNA options. |
| 49 | */ |
| 50 | public const IDNA_DEFAULT = 0; |
| 51 | public const IDNA_ALLOW_UNASSIGNED = 1; |
| 52 | public const IDNA_USE_STD3_RULES = 2; |
| 53 | public const IDNA_CHECK_BIDI = 4; |
| 54 | public const IDNA_CHECK_CONTEXTJ = 8; |
| 55 | public const IDNA_NONTRANSITIONAL_TO_ASCII = 0x10; |
| 56 | public const IDNA_NONTRANSITIONAL_TO_UNICODE = 0x20; |
| 57 | public const IDNA_CHECK_CONTEXTO = 0x40; |
| 58 | /** |
| 59 | * IDNA errors. |
| 60 | */ |
| 61 | public const ERROR_NONE = 0; |
| 62 | public const ERROR_EMPTY_LABEL = 1; |
| 63 | public const ERROR_LABEL_TOO_LONG = 2; |
| 64 | public const ERROR_DOMAIN_NAME_TOO_LONG = 4; |
| 65 | public const ERROR_LEADING_HYPHEN = 8; |
| 66 | public const ERROR_TRAILING_HYPHEN = 0x10; |
| 67 | public const ERROR_HYPHEN_3_4 = 0x20; |
| 68 | public const ERROR_LEADING_COMBINING_MARK = 0x40; |
| 69 | public const ERROR_DISALLOWED = 0x80; |
| 70 | public const ERROR_PUNYCODE = 0x100; |
| 71 | public const ERROR_LABEL_HAS_DOT = 0x200; |
| 72 | public const ERROR_INVALID_ACE_LABEL = 0x400; |
| 73 | public const ERROR_BIDI = 0x800; |
| 74 | public const ERROR_CONTEXTJ = 0x1000; |
| 75 | public const ERROR_CONTEXTO_PUNCTUATION = 0x2000; |
| 76 | public const ERROR_CONTEXTO_DIGITS = 0x4000; |
| 77 | /** |
| 78 | * IDNA default options. |
| 79 | */ |
| 80 | public const IDNA2008_ASCII = self::IDNA_NONTRANSITIONAL_TO_ASCII | self::IDNA_CHECK_BIDI | self::IDNA_USE_STD3_RULES | self::IDNA_CHECK_CONTEXTJ; |
| 81 | public const IDNA2008_UNICODE = self::IDNA_NONTRANSITIONAL_TO_UNICODE | self::IDNA_CHECK_BIDI | self::IDNA_USE_STD3_RULES | self::IDNA_CHECK_CONTEXTJ; |
| 82 | /** |
| 83 | * @codeCoverageIgnore |
| 84 | */ |
| 85 | private static function supportsIdna() : void |
| 86 | { |
| 87 | static $idnSupport; |
| 88 | if (null === $idnSupport) { |
| 89 | $idnSupport = function_exists('\\idn_to_ascii') && defined('\\INTL_IDNA_VARIANT_UTS46'); |
| 90 | } |
| 91 | if (!$idnSupport) { |
| 92 | throw new IdnSupportMissing('IDN host can not be processed. Verify that ext/intl is installed for IDN support and that ICU is at least version 4.6.'); |
| 93 | } |
| 94 | } |
| 95 | /** |
| 96 | * Converts the input to its IDNA ASCII form. |
| 97 | * |
| 98 | * This method returns the string converted to IDN ASCII form |
| 99 | * |
| 100 | * @throws SyntaxError if the string can not be converted to ASCII using IDN UTS46 algorithm |
| 101 | */ |
| 102 | public static function toAscii(string $domain, int $options) : IdnaInfo |
| 103 | { |
| 104 | $domain = rawurldecode($domain); |
| 105 | if (1 === \preg_match(self::REGEXP_IDNA_PATTERN, $domain)) { |
| 106 | self::supportsIdna(); |
| 107 | /* @param-out array{errors: int, isTransitionalDifferent: bool, result: string} $idnaInfo */ |
| 108 | idn_to_ascii($domain, $options, INTL_IDNA_VARIANT_UTS46, $idnaInfo); |
| 109 | if ([] === $idnaInfo) { |
| 110 | return IdnaInfo::fromIntl(['result' => \strtolower($domain), 'isTransitionalDifferent' => \false, 'errors' => self::validateDomainAndLabelLength($domain)]); |
| 111 | } |
| 112 | /* @var array{errors: int, isTransitionalDifferent: bool, result: string} $idnaInfo */ |
| 113 | return IdnaInfo::fromIntl($idnaInfo); |
| 114 | } |
| 115 | $error = self::ERROR_NONE; |
| 116 | if (1 !== \preg_match(self::REGEXP_REGISTERED_NAME, $domain)) { |
| 117 | $error |= self::ERROR_DISALLOWED; |
| 118 | } |
| 119 | return IdnaInfo::fromIntl(['result' => \strtolower($domain), 'isTransitionalDifferent' => \false, 'errors' => self::validateDomainAndLabelLength($domain) | $error]); |
| 120 | } |
| 121 | /** |
| 122 | * Converts the input to its IDNA UNICODE form. |
| 123 | * |
| 124 | * This method returns the string converted to IDN UNICODE form |
| 125 | * |
| 126 | * @throws SyntaxError if the string can not be converted to UNICODE using IDN UTS46 algorithm |
| 127 | */ |
| 128 | public static function toUnicode(string $domain, int $options) : IdnaInfo |
| 129 | { |
| 130 | $domain = rawurldecode($domain); |
| 131 | if (\false === \stripos($domain, 'xn--')) { |
| 132 | return IdnaInfo::fromIntl(['result' => $domain, 'isTransitionalDifferent' => \false, 'errors' => self::ERROR_NONE]); |
| 133 | } |
| 134 | self::supportsIdna(); |
| 135 | /* @param-out array{errors: int, isTransitionalDifferent: bool, result: string} $idnaInfo */ |
| 136 | idn_to_utf8($domain, $options, INTL_IDNA_VARIANT_UTS46, $idnaInfo); |
| 137 | if ([] === $idnaInfo) { |
| 138 | throw IdnaConversionFailed::dueToInvalidHost($domain); |
| 139 | } |
| 140 | /* @var array{errors: int, isTransitionalDifferent: bool, result: string} $idnaInfo */ |
| 141 | return IdnaInfo::fromIntl($idnaInfo); |
| 142 | } |
| 143 | /** |
| 144 | * Adapted from https://github.com/TRowbotham/idna. |
| 145 | * |
| 146 | * @see https://github.com/TRowbotham/idna/blob/master/src/Idna.php#L236 |
| 147 | */ |
| 148 | private static function validateDomainAndLabelLength(string $domain) : int |
| 149 | { |
| 150 | $error = self::ERROR_NONE; |
| 151 | $labels = \explode('.', $domain); |
| 152 | $maxDomainSize = self::MAX_DOMAIN_LENGTH; |
| 153 | $length = \count($labels); |
| 154 | // If the last label is empty and it is not the first label, then it is the root label. |
| 155 | // Increase the max size by 1, making it 254, to account for the root label's "." |
| 156 | // delimiter. This also means we don't need to check the last label's length for being too |
| 157 | // long. |
| 158 | if ($length > 1 && $labels[$length - 1] === '') { |
| 159 | ++$maxDomainSize; |
| 160 | \array_pop($labels); |
| 161 | } |
| 162 | if (\strlen($domain) > $maxDomainSize) { |
| 163 | $error |= self::ERROR_DOMAIN_NAME_TOO_LONG; |
| 164 | } |
| 165 | foreach ($labels as $label) { |
| 166 | if (\strlen($label) > self::MAX_LABEL_LENGTH) { |
| 167 | $error |= self::ERROR_LABEL_TOO_LONG; |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | return $error; |
| 172 | } |
| 173 | } |
| 174 |