Idn.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com> and Trevor Rowbotham <trevor.rowbotham@pm.me>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Intl\Idn;
  11. use Symfony\Polyfill\Intl\Idn\Resources\unidata\DisallowedRanges;
  12. use Symfony\Polyfill\Intl\Idn\Resources\unidata\Regex;
  13. /**
  14. * @see https://www.unicode.org/reports/tr46/
  15. *
  16. * @internal
  17. */
  18. final class Idn
  19. {
  20. public const ERROR_EMPTY_LABEL = 1;
  21. public const ERROR_LABEL_TOO_LONG = 2;
  22. public const ERROR_DOMAIN_NAME_TOO_LONG = 4;
  23. public const ERROR_LEADING_HYPHEN = 8;
  24. public const ERROR_TRAILING_HYPHEN = 0x10;
  25. public const ERROR_HYPHEN_3_4 = 0x20;
  26. public const ERROR_LEADING_COMBINING_MARK = 0x40;
  27. public const ERROR_DISALLOWED = 0x80;
  28. public const ERROR_PUNYCODE = 0x100;
  29. public const ERROR_LABEL_HAS_DOT = 0x200;
  30. public const ERROR_INVALID_ACE_LABEL = 0x400;
  31. public const ERROR_BIDI = 0x800;
  32. public const ERROR_CONTEXTJ = 0x1000;
  33. public const ERROR_CONTEXTO_PUNCTUATION = 0x2000;
  34. public const ERROR_CONTEXTO_DIGITS = 0x4000;
  35. public const INTL_IDNA_VARIANT_2003 = 0;
  36. public const INTL_IDNA_VARIANT_UTS46 = 1;
  37. public const IDNA_DEFAULT = 0;
  38. public const IDNA_ALLOW_UNASSIGNED = 1;
  39. public const IDNA_USE_STD3_RULES = 2;
  40. public const IDNA_CHECK_BIDI = 4;
  41. public const IDNA_CHECK_CONTEXTJ = 8;
  42. public const IDNA_NONTRANSITIONAL_TO_ASCII = 16;
  43. public const IDNA_NONTRANSITIONAL_TO_UNICODE = 32;
  44. public const MAX_DOMAIN_SIZE = 253;
  45. public const MAX_LABEL_SIZE = 63;
  46. public const BASE = 36;
  47. public const TMIN = 1;
  48. public const TMAX = 26;
  49. public const SKEW = 38;
  50. public const DAMP = 700;
  51. public const INITIAL_BIAS = 72;
  52. public const INITIAL_N = 128;
  53. public const DELIMITER = '-';
  54. public const MAX_INT = 2147483647;
  55. /**
  56. * Contains the numeric value of a basic code point (for use in representing integers) in the
  57. * range 0 to BASE-1, or -1 if b is does not represent a value.
  58. *
  59. * @var array<int, int>
  60. */
  61. private static $basicToDigit = [
  62. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  63. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  64. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  65. 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1,
  66. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  67. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  68. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  69. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  70. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  71. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  72. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  73. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  74. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  75. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  76. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  77. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  78. ];
  79. /**
  80. * @var array<int, int>
  81. */
  82. private static $virama;
  83. /**
  84. * @var array<int, string>
  85. */
  86. private static $mapped;
  87. /**
  88. * @var array<int, bool>
  89. */
  90. private static $ignored;
  91. /**
  92. * @var array<int, string>
  93. */
  94. private static $deviation;
  95. /**
  96. * @var array<int, bool>
  97. */
  98. private static $disallowed;
  99. /**
  100. * @var array<int, string>
  101. */
  102. private static $disallowed_STD3_mapped;
  103. /**
  104. * @var array<int, bool>
  105. */
  106. private static $disallowed_STD3_valid;
  107. /**
  108. * @var bool
  109. */
  110. private static $mappingTableLoaded = false;
  111. /**
  112. * @see https://www.unicode.org/reports/tr46/#ToASCII
  113. *
  114. * @param string $domainName
  115. * @param int $options
  116. * @param int $variant
  117. * @param array $idna_info
  118. *
  119. * @return string|false
  120. */
  121. public static function idn_to_ascii($domainName, $options = self::IDNA_DEFAULT, $variant = self::INTL_IDNA_VARIANT_UTS46, &$idna_info = [])
  122. {
  123. if (\PHP_VERSION_ID > 80400 && '' === $domainName) {
  124. throw new \ValueError('idn_to_ascii(): Argument #1 ($domain) cannot be empty');
  125. }
  126. if (self::INTL_IDNA_VARIANT_2003 === $variant) {
  127. @trigger_error('idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated', \E_USER_DEPRECATED);
  128. }
  129. $options = [
  130. 'CheckHyphens' => true,
  131. 'CheckBidi' => self::INTL_IDNA_VARIANT_2003 === $variant || 0 !== ($options & self::IDNA_CHECK_BIDI),
  132. 'CheckJoiners' => self::INTL_IDNA_VARIANT_UTS46 === $variant && 0 !== ($options & self::IDNA_CHECK_CONTEXTJ),
  133. 'UseSTD3ASCIIRules' => 0 !== ($options & self::IDNA_USE_STD3_RULES),
  134. 'Transitional_Processing' => self::INTL_IDNA_VARIANT_2003 === $variant || 0 === ($options & self::IDNA_NONTRANSITIONAL_TO_ASCII),
  135. 'VerifyDnsLength' => true,
  136. ];
  137. $info = new Info();
  138. $labels = self::process((string) $domainName, $options, $info);
  139. foreach ($labels as $i => $label) {
  140. // Only convert labels to punycode that contain non-ASCII code points
  141. if (1 === preg_match('/[^\x00-\x7F]/', $label)) {
  142. try {
  143. $label = 'xn--'.self::punycodeEncode($label);
  144. } catch (\Exception $e) {
  145. $info->errors |= self::ERROR_PUNYCODE;
  146. }
  147. $labels[$i] = $label;
  148. }
  149. }
  150. if ($options['VerifyDnsLength']) {
  151. self::validateDomainAndLabelLength($labels, $info);
  152. }
  153. $idna_info = [
  154. 'result' => implode('.', $labels),
  155. 'isTransitionalDifferent' => $info->transitionalDifferent,
  156. 'errors' => $info->errors,
  157. ];
  158. return 0 === $info->errors ? $idna_info['result'] : false;
  159. }
  160. /**
  161. * @see https://www.unicode.org/reports/tr46/#ToUnicode
  162. *
  163. * @param string $domainName
  164. * @param int $options
  165. * @param int $variant
  166. * @param array $idna_info
  167. *
  168. * @return string|false
  169. */
  170. public static function idn_to_utf8($domainName, $options = self::IDNA_DEFAULT, $variant = self::INTL_IDNA_VARIANT_UTS46, &$idna_info = [])
  171. {
  172. if (\PHP_VERSION_ID > 80400 && '' === $domainName) {
  173. throw new \ValueError('idn_to_utf8(): Argument #1 ($domain) cannot be empty');
  174. }
  175. if (self::INTL_IDNA_VARIANT_2003 === $variant) {
  176. @trigger_error('idn_to_utf8(): INTL_IDNA_VARIANT_2003 is deprecated', \E_USER_DEPRECATED);
  177. }
  178. $info = new Info();
  179. $labels = self::process((string) $domainName, [
  180. 'CheckHyphens' => true,
  181. 'CheckBidi' => self::INTL_IDNA_VARIANT_2003 === $variant || 0 !== ($options & self::IDNA_CHECK_BIDI),
  182. 'CheckJoiners' => self::INTL_IDNA_VARIANT_UTS46 === $variant && 0 !== ($options & self::IDNA_CHECK_CONTEXTJ),
  183. 'UseSTD3ASCIIRules' => 0 !== ($options & self::IDNA_USE_STD3_RULES),
  184. 'Transitional_Processing' => self::INTL_IDNA_VARIANT_2003 === $variant || 0 === ($options & self::IDNA_NONTRANSITIONAL_TO_UNICODE),
  185. ], $info);
  186. $idna_info = [
  187. 'result' => implode('.', $labels),
  188. 'isTransitionalDifferent' => $info->transitionalDifferent,
  189. 'errors' => $info->errors,
  190. ];
  191. return 0 === $info->errors ? $idna_info['result'] : false;
  192. }
  193. /**
  194. * @param string $label
  195. *
  196. * @return bool
  197. */
  198. private static function isValidContextJ(array $codePoints, $label)
  199. {
  200. if (!isset(self::$virama)) {
  201. self::$virama = require __DIR__.\DIRECTORY_SEPARATOR.'Resources'.\DIRECTORY_SEPARATOR.'unidata'.\DIRECTORY_SEPARATOR.'virama.php';
  202. }
  203. $offset = 0;
  204. foreach ($codePoints as $i => $codePoint) {
  205. if (0x200C !== $codePoint && 0x200D !== $codePoint) {
  206. continue;
  207. }
  208. if (!isset($codePoints[$i - 1])) {
  209. return false;
  210. }
  211. // If Canonical_Combining_Class(Before(cp)) .eq. Virama Then True;
  212. if (isset(self::$virama[$codePoints[$i - 1]])) {
  213. continue;
  214. }
  215. // If RegExpMatch((Joining_Type:{L,D})(Joining_Type:T)*\u200C(Joining_Type:T)*(Joining_Type:{R,D})) Then
  216. // True;
  217. // Generated RegExp = ([Joining_Type:{L,D}][Joining_Type:T]*\u200C[Joining_Type:T]*)[Joining_Type:{R,D}]
  218. if (0x200C === $codePoint && 1 === preg_match(Regex::ZWNJ, $label, $matches, \PREG_OFFSET_CAPTURE, $offset)) {
  219. $offset += \strlen($matches[1][0]);
  220. continue;
  221. }
  222. return false;
  223. }
  224. return true;
  225. }
  226. /**
  227. * @see https://www.unicode.org/reports/tr46/#ProcessingStepMap
  228. *
  229. * @param string $input
  230. * @param array<string, bool> $options
  231. *
  232. * @return string
  233. */
  234. private static function mapCodePoints($input, array $options, Info $info)
  235. {
  236. $str = '';
  237. $useSTD3ASCIIRules = $options['UseSTD3ASCIIRules'];
  238. $transitional = $options['Transitional_Processing'];
  239. foreach (self::utf8Decode($input) as $codePoint) {
  240. $data = self::lookupCodePointStatus($codePoint, $useSTD3ASCIIRules);
  241. switch ($data['status']) {
  242. case 'disallowed':
  243. case 'valid':
  244. $str .= mb_chr($codePoint, 'utf-8');
  245. break;
  246. case 'ignored':
  247. // Do nothing.
  248. break;
  249. case 'mapped':
  250. $str .= $transitional && 0x1E9E === $codePoint ? 'ss' : $data['mapping'];
  251. break;
  252. case 'deviation':
  253. $info->transitionalDifferent = true;
  254. $str .= ($transitional ? $data['mapping'] : mb_chr($codePoint, 'utf-8'));
  255. break;
  256. }
  257. }
  258. return $str;
  259. }
  260. /**
  261. * @see https://www.unicode.org/reports/tr46/#Processing
  262. *
  263. * @param string $domain
  264. * @param array<string, bool> $options
  265. *
  266. * @return array<int, string>
  267. */
  268. private static function process($domain, array $options, Info $info)
  269. {
  270. // If VerifyDnsLength is not set, we are doing ToUnicode otherwise we are doing ToASCII and
  271. // we need to respect the VerifyDnsLength option.
  272. $checkForEmptyLabels = !isset($options['VerifyDnsLength']) || $options['VerifyDnsLength'];
  273. if ($checkForEmptyLabels && '' === $domain) {
  274. $info->errors |= self::ERROR_EMPTY_LABEL;
  275. return [$domain];
  276. }
  277. // Step 1. Map each code point in the domain name string
  278. $domain = self::mapCodePoints($domain, $options, $info);
  279. // Step 2. Normalize the domain name string to Unicode Normalization Form C.
  280. if (!\Normalizer::isNormalized($domain, \Normalizer::FORM_C)) {
  281. $domain = \Normalizer::normalize($domain, \Normalizer::FORM_C);
  282. }
  283. // Step 3. Break the string into labels at U+002E (.) FULL STOP.
  284. $labels = explode('.', $domain);
  285. $lastLabelIndex = \count($labels) - 1;
  286. // Step 4. Convert and validate each label in the domain name string.
  287. foreach ($labels as $i => $label) {
  288. $validationOptions = $options;
  289. if ('xn--' === substr($label, 0, 4)) {
  290. // Step 4.1. If the label contains any non-ASCII code point (i.e., a code point greater than U+007F),
  291. // record that there was an error, and continue with the next label.
  292. if (preg_match('/[^\x00-\x7F]/', $label)) {
  293. $info->errors |= self::ERROR_PUNYCODE;
  294. continue;
  295. }
  296. // Step 4.2. Attempt to convert the rest of the label to Unicode according to Punycode [RFC3492]. If
  297. // that conversion fails, record that there was an error, and continue
  298. // with the next label. Otherwise replace the original label in the string by the results of the
  299. // conversion. Per UTS #46 revision 33, if the conversion succeeds but the result is empty or
  300. // contains only ASCII code points, record that there was an error and continue with the next label.
  301. try {
  302. $label = self::punycodeDecode(substr($label, 4));
  303. } catch (\Exception $e) {
  304. $info->errors |= self::ERROR_PUNYCODE;
  305. continue;
  306. }
  307. if ('' === $label || 1 !== preg_match('/[^\x00-\x7F]/', $label)) {
  308. $info->errors |= self::ERROR_INVALID_ACE_LABEL;
  309. continue;
  310. }
  311. $validationOptions['Transitional_Processing'] = false;
  312. $labels[$i] = $label;
  313. }
  314. self::validateLabel($label, $info, $validationOptions, $i > 0 && $i === $lastLabelIndex);
  315. }
  316. if ($info->bidiDomain && !$info->validBidiDomain) {
  317. $info->errors |= self::ERROR_BIDI;
  318. }
  319. // Any input domain name string that does not record an error has been successfully
  320. // processed according to this specification. Conversely, if an input domain_name string
  321. // causes an error, then the processing of the input domain_name string fails. Determining
  322. // what to do with error input is up to the caller, and not in the scope of this document.
  323. return $labels;
  324. }
  325. /**
  326. * @see https://tools.ietf.org/html/rfc5893#section-2
  327. *
  328. * @param string $label
  329. */
  330. private static function validateBidiLabel($label, Info $info)
  331. {
  332. if (1 === preg_match(Regex::RTL_LABEL, $label)) {
  333. $info->bidiDomain = true;
  334. // Step 1. The first character must be a character with Bidi property L, R, or AL.
  335. // If it has the R or AL property, it is an RTL label
  336. if (1 !== preg_match(Regex::BIDI_STEP_1_RTL, $label)) {
  337. $info->validBidiDomain = false;
  338. return;
  339. }
  340. // Step 2. In an RTL label, only characters with the Bidi properties R, AL, AN, EN, ES,
  341. // CS, ET, ON, BN, or NSM are allowed.
  342. if (1 === preg_match(Regex::BIDI_STEP_2, $label)) {
  343. $info->validBidiDomain = false;
  344. return;
  345. }
  346. // Step 3. In an RTL label, the end of the label must be a character with Bidi property
  347. // R, AL, EN, or AN, followed by zero or more characters with Bidi property NSM.
  348. if (1 !== preg_match(Regex::BIDI_STEP_3, $label)) {
  349. $info->validBidiDomain = false;
  350. return;
  351. }
  352. // Step 4. In an RTL label, if an EN is present, no AN may be present, and vice versa.
  353. if (1 === preg_match(Regex::BIDI_STEP_4_AN, $label) && 1 === preg_match(Regex::BIDI_STEP_4_EN, $label)) {
  354. $info->validBidiDomain = false;
  355. return;
  356. }
  357. return;
  358. }
  359. // We are a LTR label
  360. // Step 1. The first character must be a character with Bidi property L, R, or AL.
  361. // If it has the L property, it is an LTR label.
  362. if (1 !== preg_match(Regex::BIDI_STEP_1_LTR, $label)) {
  363. $info->validBidiDomain = false;
  364. return;
  365. }
  366. // Step 5. In an LTR label, only characters with the Bidi properties L, EN,
  367. // ES, CS, ET, ON, BN, or NSM are allowed.
  368. if (1 === preg_match(Regex::BIDI_STEP_5, $label)) {
  369. $info->validBidiDomain = false;
  370. return;
  371. }
  372. // Step 6.In an LTR label, the end of the label must be a character with Bidi property L or
  373. // EN, followed by zero or more characters with Bidi property NSM.
  374. if (1 !== preg_match(Regex::BIDI_STEP_6, $label)) {
  375. $info->validBidiDomain = false;
  376. return;
  377. }
  378. }
  379. /**
  380. * @param array<int, string> $labels
  381. */
  382. private static function validateDomainAndLabelLength(array $labels, Info $info)
  383. {
  384. $maxDomainSize = self::MAX_DOMAIN_SIZE;
  385. $length = \count($labels);
  386. // Number of "." delimiters.
  387. $domainLength = $length - 1;
  388. // If the last label is empty and it is not the first label, then it is the root label.
  389. // Increase the max size by 1, making it 254, to account for the root label's "."
  390. // delimiter. This also means we don't need to check the last label's length for being too
  391. // long.
  392. if ($length > 1 && '' === $labels[$length - 1]) {
  393. ++$maxDomainSize;
  394. --$length;
  395. }
  396. for ($i = 0; $i < $length; ++$i) {
  397. $bytes = \strlen($labels[$i]);
  398. $domainLength += $bytes;
  399. if ($bytes > self::MAX_LABEL_SIZE) {
  400. $info->errors |= self::ERROR_LABEL_TOO_LONG;
  401. }
  402. }
  403. if ($domainLength > $maxDomainSize) {
  404. $info->errors |= self::ERROR_DOMAIN_NAME_TOO_LONG;
  405. }
  406. }
  407. /**
  408. * @see https://www.unicode.org/reports/tr46/#Validity_Criteria
  409. *
  410. * @param string $label
  411. * @param array<string, bool> $options
  412. * @param bool $canBeEmpty
  413. */
  414. private static function validateLabel($label, Info $info, array $options, $canBeEmpty)
  415. {
  416. if ('' === $label) {
  417. if (!$canBeEmpty && (!isset($options['VerifyDnsLength']) || $options['VerifyDnsLength'])) {
  418. $info->errors |= self::ERROR_EMPTY_LABEL;
  419. }
  420. return;
  421. }
  422. // Step 1. The label must be in Unicode Normalization Form C.
  423. if (!\Normalizer::isNormalized($label, \Normalizer::FORM_C)) {
  424. $info->errors |= self::ERROR_INVALID_ACE_LABEL;
  425. }
  426. $codePoints = self::utf8Decode($label);
  427. if ($options['CheckHyphens']) {
  428. // Step 2. If CheckHyphens, the label must not contain a U+002D HYPHEN-MINUS character
  429. // in both the thrid and fourth positions.
  430. if (isset($codePoints[2], $codePoints[3]) && 0x002D === $codePoints[2] && 0x002D === $codePoints[3]) {
  431. $info->errors |= self::ERROR_HYPHEN_3_4;
  432. }
  433. // Step 3. If CheckHyphens, the label must neither begin nor end with a U+002D
  434. // HYPHEN-MINUS character.
  435. if ('-' === substr($label, 0, 1)) {
  436. $info->errors |= self::ERROR_LEADING_HYPHEN;
  437. }
  438. if ('-' === substr($label, -1, 1)) {
  439. $info->errors |= self::ERROR_TRAILING_HYPHEN;
  440. }
  441. } elseif ('xn--' === substr($label, 0, 4)) {
  442. $info->errors |= self::ERROR_PUNYCODE;
  443. }
  444. // Step 4. The label must not contain a U+002E (.) FULL STOP.
  445. if (false !== strpos($label, '.')) {
  446. $info->errors |= self::ERROR_LABEL_HAS_DOT;
  447. }
  448. // Step 5. The label must not begin with a combining mark, that is: General_Category=Mark.
  449. if (1 === preg_match(Regex::COMBINING_MARK, $label)) {
  450. $info->errors |= self::ERROR_LEADING_COMBINING_MARK;
  451. }
  452. // Step 6. Each code point in the label must only have certain status values according to
  453. // Section 5, IDNA Mapping Table:
  454. $transitional = $options['Transitional_Processing'];
  455. $useSTD3ASCIIRules = $options['UseSTD3ASCIIRules'];
  456. foreach ($codePoints as $codePoint) {
  457. $data = self::lookupCodePointStatus($codePoint, $useSTD3ASCIIRules);
  458. $status = $data['status'];
  459. if ('valid' === $status || (!$transitional && 'deviation' === $status)) {
  460. continue;
  461. }
  462. $info->errors |= self::ERROR_DISALLOWED;
  463. break;
  464. }
  465. // Step 7. If CheckJoiners, the label must satisify the ContextJ rules from Appendix A, in
  466. // The Unicode Code Points and Internationalized Domain Names for Applications (IDNA)
  467. // [IDNA2008].
  468. if ($options['CheckJoiners'] && !self::isValidContextJ($codePoints, $label)) {
  469. $info->errors |= self::ERROR_CONTEXTJ;
  470. }
  471. // Step 8. If CheckBidi, and if the domain name is a Bidi domain name, then the label must
  472. // satisfy all six of the numbered conditions in [IDNA2008] RFC 5893, Section 2.
  473. if ($options['CheckBidi'] && (!$info->bidiDomain || $info->validBidiDomain)) {
  474. self::validateBidiLabel($label, $info);
  475. }
  476. }
  477. /**
  478. * @see https://tools.ietf.org/html/rfc3492#section-6.2
  479. *
  480. * @param string $input
  481. *
  482. * @return string
  483. */
  484. private static function punycodeDecode($input)
  485. {
  486. $n = self::INITIAL_N;
  487. $out = 0;
  488. $i = 0;
  489. $bias = self::INITIAL_BIAS;
  490. $lastDelimIndex = strrpos($input, self::DELIMITER);
  491. $b = false === $lastDelimIndex ? 0 : $lastDelimIndex;
  492. $inputLength = \strlen($input);
  493. $output = [];
  494. $bytes = array_map('ord', str_split($input));
  495. for ($j = 0; $j < $b; ++$j) {
  496. if ($bytes[$j] > 0x7F) {
  497. throw new \Exception('Invalid input');
  498. }
  499. $output[$out++] = $input[$j];
  500. }
  501. if ($b > 0) {
  502. ++$b;
  503. }
  504. for ($in = $b; $in < $inputLength; ++$out) {
  505. $oldi = $i;
  506. $w = 1;
  507. for ($k = self::BASE; /* no condition */; $k += self::BASE) {
  508. if ($in >= $inputLength) {
  509. throw new \Exception('Invalid input');
  510. }
  511. $digit = self::$basicToDigit[$bytes[$in++] & 0xFF];
  512. if ($digit < 0) {
  513. throw new \Exception('Invalid input');
  514. }
  515. if ($digit > intdiv(self::MAX_INT - $i, $w)) {
  516. throw new \Exception('Integer overflow');
  517. }
  518. $i += $digit * $w;
  519. if ($k <= $bias) {
  520. $t = self::TMIN;
  521. } elseif ($k >= $bias + self::TMAX) {
  522. $t = self::TMAX;
  523. } else {
  524. $t = $k - $bias;
  525. }
  526. if ($digit < $t) {
  527. break;
  528. }
  529. $baseMinusT = self::BASE - $t;
  530. if ($w > intdiv(self::MAX_INT, $baseMinusT)) {
  531. throw new \Exception('Integer overflow');
  532. }
  533. $w *= $baseMinusT;
  534. }
  535. $outPlusOne = $out + 1;
  536. $bias = self::adaptBias($i - $oldi, $outPlusOne, 0 === $oldi);
  537. if (intdiv($i, $outPlusOne) > self::MAX_INT - $n) {
  538. throw new \Exception('Integer overflow');
  539. }
  540. $n += intdiv($i, $outPlusOne);
  541. $i %= $outPlusOne;
  542. array_splice($output, $i++, 0, [mb_chr($n, 'utf-8')]);
  543. }
  544. return implode('', $output);
  545. }
  546. /**
  547. * @see https://tools.ietf.org/html/rfc3492#section-6.3
  548. *
  549. * @param string $input
  550. *
  551. * @return string
  552. */
  553. private static function punycodeEncode($input)
  554. {
  555. $n = self::INITIAL_N;
  556. $delta = 0;
  557. $out = 0;
  558. $bias = self::INITIAL_BIAS;
  559. $inputLength = 0;
  560. $output = '';
  561. $iter = self::utf8Decode($input);
  562. foreach ($iter as $codePoint) {
  563. ++$inputLength;
  564. if ($codePoint < 0x80) {
  565. $output .= \chr($codePoint);
  566. ++$out;
  567. }
  568. }
  569. $h = $out;
  570. $b = $out;
  571. if ($b > 0) {
  572. $output .= self::DELIMITER;
  573. ++$out;
  574. }
  575. while ($h < $inputLength) {
  576. $m = self::MAX_INT;
  577. foreach ($iter as $codePoint) {
  578. if ($codePoint >= $n && $codePoint < $m) {
  579. $m = $codePoint;
  580. }
  581. }
  582. if ($m - $n > intdiv(self::MAX_INT - $delta, $h + 1)) {
  583. throw new \Exception('Integer overflow');
  584. }
  585. $delta += ($m - $n) * ($h + 1);
  586. $n = $m;
  587. foreach ($iter as $codePoint) {
  588. if ($codePoint < $n && 0 === ++$delta) {
  589. throw new \Exception('Integer overflow');
  590. }
  591. if ($codePoint === $n) {
  592. $q = $delta;
  593. for ($k = self::BASE; /* no condition */; $k += self::BASE) {
  594. if ($k <= $bias) {
  595. $t = self::TMIN;
  596. } elseif ($k >= $bias + self::TMAX) {
  597. $t = self::TMAX;
  598. } else {
  599. $t = $k - $bias;
  600. }
  601. if ($q < $t) {
  602. break;
  603. }
  604. $qMinusT = $q - $t;
  605. $baseMinusT = self::BASE - $t;
  606. $output .= self::encodeDigit($t + $qMinusT % $baseMinusT, false);
  607. ++$out;
  608. $q = intdiv($qMinusT, $baseMinusT);
  609. }
  610. $output .= self::encodeDigit($q, false);
  611. ++$out;
  612. $bias = self::adaptBias($delta, $h + 1, $h === $b);
  613. $delta = 0;
  614. ++$h;
  615. }
  616. }
  617. ++$delta;
  618. ++$n;
  619. }
  620. return $output;
  621. }
  622. /**
  623. * @see https://tools.ietf.org/html/rfc3492#section-6.1
  624. *
  625. * @param int $delta
  626. * @param int $numPoints
  627. * @param bool $firstTime
  628. *
  629. * @return int
  630. */
  631. private static function adaptBias($delta, $numPoints, $firstTime)
  632. {
  633. // xxx >> 1 is a faster way of doing intdiv(xxx, 2)
  634. $delta = $firstTime ? intdiv($delta, self::DAMP) : $delta >> 1;
  635. $delta += intdiv($delta, $numPoints);
  636. $k = 0;
  637. while ($delta > ((self::BASE - self::TMIN) * self::TMAX) >> 1) {
  638. $delta = intdiv($delta, self::BASE - self::TMIN);
  639. $k += self::BASE;
  640. }
  641. return $k + intdiv((self::BASE - self::TMIN + 1) * $delta, $delta + self::SKEW);
  642. }
  643. /**
  644. * @param int $d
  645. * @param bool $flag
  646. *
  647. * @return string
  648. */
  649. private static function encodeDigit($d, $flag)
  650. {
  651. return \chr($d + 22 + 75 * ($d < 26 ? 1 : 0) - (($flag ? 1 : 0) << 5));
  652. }
  653. /**
  654. * Takes a UTF-8 encoded string and converts it into a series of integer code points. Any
  655. * invalid byte sequences will be replaced by a U+FFFD replacement code point.
  656. *
  657. * @see https://encoding.spec.whatwg.org/#utf-8-decoder
  658. *
  659. * @param string $input
  660. *
  661. * @return array<int, int>
  662. */
  663. private static function utf8Decode($input)
  664. {
  665. $bytesSeen = 0;
  666. $bytesNeeded = 0;
  667. $lowerBoundary = 0x80;
  668. $upperBoundary = 0xBF;
  669. $codePoint = 0;
  670. $codePoints = [];
  671. $length = \strlen($input);
  672. for ($i = 0; $i < $length; ++$i) {
  673. $byte = \ord($input[$i]);
  674. if (0 === $bytesNeeded) {
  675. if ($byte >= 0x00 && $byte <= 0x7F) {
  676. $codePoints[] = $byte;
  677. continue;
  678. }
  679. if ($byte >= 0xC2 && $byte <= 0xDF) {
  680. $bytesNeeded = 1;
  681. $codePoint = $byte & 0x1F;
  682. } elseif ($byte >= 0xE0 && $byte <= 0xEF) {
  683. if (0xE0 === $byte) {
  684. $lowerBoundary = 0xA0;
  685. } elseif (0xED === $byte) {
  686. $upperBoundary = 0x9F;
  687. }
  688. $bytesNeeded = 2;
  689. $codePoint = $byte & 0xF;
  690. } elseif ($byte >= 0xF0 && $byte <= 0xF4) {
  691. if (0xF0 === $byte) {
  692. $lowerBoundary = 0x90;
  693. } elseif (0xF4 === $byte) {
  694. $upperBoundary = 0x8F;
  695. }
  696. $bytesNeeded = 3;
  697. $codePoint = $byte & 0x7;
  698. } else {
  699. $codePoints[] = 0xFFFD;
  700. }
  701. continue;
  702. }
  703. if ($byte < $lowerBoundary || $byte > $upperBoundary) {
  704. $codePoint = 0;
  705. $bytesNeeded = 0;
  706. $bytesSeen = 0;
  707. $lowerBoundary = 0x80;
  708. $upperBoundary = 0xBF;
  709. --$i;
  710. $codePoints[] = 0xFFFD;
  711. continue;
  712. }
  713. $lowerBoundary = 0x80;
  714. $upperBoundary = 0xBF;
  715. $codePoint = ($codePoint << 6) | ($byte & 0x3F);
  716. if (++$bytesSeen !== $bytesNeeded) {
  717. continue;
  718. }
  719. $codePoints[] = $codePoint;
  720. $codePoint = 0;
  721. $bytesNeeded = 0;
  722. $bytesSeen = 0;
  723. }
  724. // String unexpectedly ended, so append a U+FFFD code point.
  725. if (0 !== $bytesNeeded) {
  726. $codePoints[] = 0xFFFD;
  727. }
  728. return $codePoints;
  729. }
  730. /**
  731. * @param int $codePoint
  732. * @param bool $useSTD3ASCIIRules
  733. *
  734. * @return array{status: string, mapping?: string}
  735. */
  736. private static function lookupCodePointStatus($codePoint, $useSTD3ASCIIRules)
  737. {
  738. if (!self::$mappingTableLoaded) {
  739. self::$mappingTableLoaded = true;
  740. self::$mapped = require __DIR__.'/Resources/unidata/mapped.php';
  741. self::$ignored = require __DIR__.'/Resources/unidata/ignored.php';
  742. self::$deviation = require __DIR__.'/Resources/unidata/deviation.php';
  743. self::$disallowed = require __DIR__.'/Resources/unidata/disallowed.php';
  744. self::$disallowed_STD3_mapped = require __DIR__.'/Resources/unidata/disallowed_STD3_mapped.php';
  745. self::$disallowed_STD3_valid = require __DIR__.'/Resources/unidata/disallowed_STD3_valid.php';
  746. }
  747. if (isset(self::$mapped[$codePoint])) {
  748. return ['status' => 'mapped', 'mapping' => self::$mapped[$codePoint]];
  749. }
  750. if (isset(self::$ignored[$codePoint])) {
  751. return ['status' => 'ignored'];
  752. }
  753. if (isset(self::$deviation[$codePoint])) {
  754. return ['status' => 'deviation', 'mapping' => self::$deviation[$codePoint]];
  755. }
  756. if (isset(self::$disallowed[$codePoint]) || DisallowedRanges::inRange($codePoint)) {
  757. return ['status' => 'disallowed'];
  758. }
  759. $isDisallowedMapped = isset(self::$disallowed_STD3_mapped[$codePoint]);
  760. if ($isDisallowedMapped || isset(self::$disallowed_STD3_valid[$codePoint])) {
  761. $status = 'disallowed';
  762. if (!$useSTD3ASCIIRules) {
  763. $status = $isDisallowedMapped ? 'mapped' : 'valid';
  764. }
  765. if ($isDisallowedMapped) {
  766. return ['status' => $status, 'mapping' => self::$disallowed_STD3_mapped[$codePoint]];
  767. }
  768. return ['status' => $status];
  769. }
  770. return ['status' => 'valid'];
  771. }
  772. }