PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.15.5
Independent Analytics – WordPress Analytics Plugin v2.15.5
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / league / uri-interfaces / src / Idna / Idna.php
independent-analytics / vendor / league / uri-interfaces / src / Idna Last commit date
Idna.php 2 years ago IdnaInfo.php 2 years ago
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 ^(?:(?&reg_name)\\.)*(?&reg_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