Requests_IDNAEncoder

IDNA URL encoder.

Defined (1)

The class is defined in the following location(s).

/wp-includes/Requests/IDNAEncoder.php  
  1. class Requests_IDNAEncoder { 
  2. /** 
  3. * ACE prefix used for IDNA 
  4. * @see https://tools.ietf.org/html/rfc3490#section-5 
  5. * @var string 
  6. */ 
  7. const ACE_PREFIX = 'xn--'; 
  8.  
  9. /**#@+ 
  10. * Bootstrap constant for Punycode 
  11. * @see https://tools.ietf.org/html/rfc3492#section-5 
  12. * @var int 
  13. */ 
  14. const BOOTSTRAP_BASE = 36; 
  15. const BOOTSTRAP_TMIN = 1; 
  16. const BOOTSTRAP_TMAX = 26; 
  17. const BOOTSTRAP_SKEW = 38; 
  18. const BOOTSTRAP_DAMP = 700; 
  19. const BOOTSTRAP_INITIAL_BIAS = 72; 
  20. const BOOTSTRAP_INITIAL_N = 128; 
  21. /**#@-*/ 
  22.  
  23. /** 
  24. * Encode a hostname using Punycode 
  25. * @param string $string Hostname 
  26. * @return string Punycode-encoded hostname 
  27. */ 
  28. public static function encode($string) { 
  29. $parts = explode('.', $string); 
  30. foreach ($parts as &$part) { 
  31. $part = self::to_ascii($part); 
  32. return implode('.', $parts); 
  33.  
  34. /** 
  35. * Convert a UTF-8 string to an ASCII string using Punycode 
  36. * @throws Requests_Exception Provided string longer than 64 ASCII characters (`idna.provided_too_long`) 
  37. * @throws Requests_Exception Prepared string longer than 64 ASCII characters (`idna.prepared_too_long`) 
  38. * @throws Requests_Exception Provided string already begins with xn-- (`idna.provided_is_prefixed`) 
  39. * @throws Requests_Exception Encoded string longer than 64 ASCII characters (`idna.encoded_too_long`) 
  40. * @param string $string ASCII or UTF-8 string (max length 64 characters) 
  41. * @return string ASCII string 
  42. */ 
  43. public static function to_ascii($string) { 
  44. // Step 1: Check if the string is already ASCII 
  45. if (self::is_ascii($string)) { 
  46. // Skip to step 7 
  47. if (strlen($string) < 64) { 
  48. return $string; 
  49.  
  50. throw new Requests_Exception('Provided string is too long', 'idna.provided_too_long', $string); 
  51.  
  52. // Step 2: nameprep 
  53. $string = self::nameprep($string); 
  54.  
  55. // Step 3: UseSTD3ASCIIRules is false, continue 
  56. // Step 4: Check if it's ASCII now 
  57. if (self::is_ascii($string)) { 
  58. // Skip to step 7 
  59. if (strlen($string) < 64) { 
  60. return $string; 
  61.  
  62. throw new Requests_Exception('Prepared string is too long', 'idna.prepared_too_long', $string); 
  63.  
  64. // Step 5: Check ACE prefix 
  65. if (strpos($string, self::ACE_PREFIX) === 0) { 
  66. throw new Requests_Exception('Provided string begins with ACE prefix', 'idna.provided_is_prefixed', $string); 
  67.  
  68. // Step 6: Encode with Punycode 
  69. $string = self::punycode_encode($string); 
  70.  
  71. // Step 7: Prepend ACE prefix 
  72. $string = self::ACE_PREFIX . $string; 
  73.  
  74. // Step 8: Check size 
  75. if (strlen($string) < 64) { 
  76. return $string; 
  77.  
  78. throw new Requests_Exception('Encoded string is too long', 'idna.encoded_too_long', $string); 
  79.  
  80. /** 
  81. * Check whether a given string contains only ASCII characters 
  82. * @internal (Testing found regex was the fastest implementation) 
  83. * @param string $string 
  84. * @return bool Is the string ASCII-only? 
  85. */ 
  86. protected static function is_ascii($string) { 
  87. return (preg_match('/(?:[^\x00-\x7F])/', $string) !== 1); 
  88.  
  89. /** 
  90. * Prepare a string for use as an IDNA name 
  91. * @todo Implement this based on RFC 3491 and the newer 5891 
  92. * @param string $string 
  93. * @return string Prepared string 
  94. */ 
  95. protected static function nameprep($string) { 
  96. return $string; 
  97.  
  98. /** 
  99. * Convert a UTF-8 string to a UCS-4 codepoint array 
  100. * Based on Requests_IRI::replace_invalid_with_pct_encoding() 
  101. * @throws Requests_Exception Invalid UTF-8 codepoint (`idna.invalidcodepoint`) 
  102. * @param string $input 
  103. * @return array Unicode code points 
  104. */ 
  105. protected static function utf8_to_codepoints($input) { 
  106. $codepoints = array(); 
  107.  
  108. // Get number of bytes 
  109. $strlen = strlen($input); 
  110.  
  111. for ($position = 0; $position < $strlen; $position++) { 
  112. $value = ord($input[$position]); 
  113.  
  114. // One byte sequence: 
  115. if ((~$value & 0x80) === 0x80) { 
  116. $character = $value; 
  117. $length = 1; 
  118. $remaining = 0; 
  119. // Two byte sequence: 
  120. elseif (($value & 0xE0) === 0xC0) { 
  121. $character = ($value & 0x1F) << 6; 
  122. $length = 2; 
  123. $remaining = 1; 
  124. // Three byte sequence: 
  125. elseif (($value & 0xF0) === 0xE0) { 
  126. $character = ($value & 0x0F) << 12; 
  127. $length = 3; 
  128. $remaining = 2; 
  129. // Four byte sequence: 
  130. elseif (($value & 0xF8) === 0xF0) { 
  131. $character = ($value & 0x07) << 18; 
  132. $length = 4; 
  133. $remaining = 3; 
  134. // Invalid byte: 
  135. else { 
  136. throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $value); 
  137.  
  138. if ($remaining > 0) { 
  139. if ($position + $length > $strlen) { 
  140. throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); 
  141. for ($position++; $remaining > 0; $position++) { 
  142. $value = ord($input[$position]); 
  143.  
  144. // If it is invalid, count the sequence as invalid and reprocess the current byte: 
  145. if (($value & 0xC0) !== 0x80) { 
  146. throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); 
  147.  
  148. $character |= ($value & 0x3F) << (--$remaining * 6); 
  149. $position--; 
  150.  
  151. if ( 
  152. // Non-shortest form sequences are invalid 
  153. $length > 1 && $character <= 0x7F 
  154. || $length > 2 && $character <= 0x7FF 
  155. || $length > 3 && $character <= 0xFFFF 
  156. // Outside of range of ucschar codepoints 
  157. // Noncharacters 
  158. || ($character & 0xFFFE) === 0xFFFE 
  159. || $character >= 0xFDD0 && $character <= 0xFDEF 
  160. || ( 
  161. // Everything else not in ucschar 
  162. $character > 0xD7FF && $character < 0xF900 
  163. || $character < 0x20 
  164. || $character > 0x7E && $character < 0xA0 
  165. || $character > 0xEFFFD 
  166. ) { 
  167. throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); 
  168.  
  169. $codepoints[] = $character; 
  170.  
  171. return $codepoints; 
  172.  
  173. /** 
  174. * RFC3492-compliant encoder 
  175. * @internal Pseudo-code from Section 6.3 is commented with "#" next to relevant code 
  176. * @throws Requests_Exception On character outside of the domain (never happens with Punycode) (`idna.character_outside_domain`) 
  177. * @param string $input UTF-8 encoded string to encode 
  178. * @return string Punycode-encoded string 
  179. */ 
  180. public static function punycode_encode($input) { 
  181. $output = ''; 
  182. # let n = initial_n 
  183. $n = self::BOOTSTRAP_INITIAL_N; 
  184. # let delta = 0 
  185. $delta = 0; 
  186. # let bias = initial_bias 
  187. $bias = self::BOOTSTRAP_INITIAL_BIAS; 
  188. # let h = b = the number of basic code points in the input 
  189. $h = $b = 0; // see loop 
  190. # copy them to the output in order 
  191. $codepoints = self::utf8_to_codepoints($input); 
  192. $extended = array(); 
  193.  
  194. foreach ($codepoints as $char) { 
  195. if ($char < 128) { 
  196. // Character is valid ASCII 
  197. // TODO: this should also check if it's valid for a URL 
  198. $output .= chr($char); 
  199. $h++; 
  200. // Check if the character is non-ASCII, but below initial n 
  201. // This never occurs for Punycode, so ignore in coverage 
  202. // @codeCoverageIgnoreStart 
  203. elseif ($char < $n) { 
  204. throw new Requests_Exception('Invalid character', 'idna.character_outside_domain', $char); 
  205. // @codeCoverageIgnoreEnd 
  206. else { 
  207. $extended[$char] = true; 
  208. $extended = array_keys($extended); 
  209. sort($extended); 
  210. $b = $h; 
  211. # [copy them] followed by a delimiter if b > 0 
  212. if (strlen($output) > 0) { 
  213. $output .= '-'; 
  214. # {if the input contains a non-basic code point < n then fail} 
  215. # while h < length(input) do begin 
  216. while ($h < count($codepoints)) { 
  217. # let m = the minimum code point >= n in the input 
  218. $m = array_shift($extended); 
  219. //printf('next code point to insert is %s' . PHP_EOL, dechex($m)); 
  220. # let delta = delta + (m - n) * (h + 1), fail on overflow 
  221. $delta += ($m - $n) * ($h + 1); 
  222. # let n = m 
  223. $n = $m; 
  224. # for each code point c in the input (in order) do begin 
  225. for ($num = 0; $num < count($codepoints); $num++) { 
  226. $c = $codepoints[$num]; 
  227. # if c < n then increment delta, fail on overflow 
  228. if ($c < $n) { 
  229. $delta++; 
  230. # if c == n then begin 
  231. elseif ($c === $n) { 
  232. # let q = delta 
  233. $q = $delta; 
  234. # for k = base to infinity in steps of base do begin 
  235. for ($k = self::BOOTSTRAP_BASE; ; $k += self::BOOTSTRAP_BASE) { 
  236. # let t = tmin if k <= bias {+ tmin}, or 
  237. # tmax if k >= bias + tmax, or k - bias otherwise 
  238. if ($k <= ($bias + self::BOOTSTRAP_TMIN)) { 
  239. $t = self::BOOTSTRAP_TMIN; 
  240. elseif ($k >= ($bias + self::BOOTSTRAP_TMAX)) { 
  241. $t = self::BOOTSTRAP_TMAX; 
  242. else { 
  243. $t = $k - $bias; 
  244. # if q < t then break 
  245. if ($q < $t) { 
  246. break; 
  247. # output the code point for digit t + ((q - t) mod (base - t)) 
  248. $digit = $t + (($q - $t) % (self::BOOTSTRAP_BASE - $t)); 
  249. $output .= self::digit_to_char($digit); 
  250. # let q = (q - t) div (base - t) 
  251. $q = floor(($q - $t) / (self::BOOTSTRAP_BASE - $t)); 
  252. # end 
  253. # output the code point for digit q 
  254. $output .= self::digit_to_char($q); 
  255. # let bias = adapt(delta, h + 1, test h equals b?) 
  256. $bias = self::adapt($delta, $h + 1, $h === $b); 
  257. # let delta = 0 
  258. $delta = 0; 
  259. # increment h 
  260. $h++; 
  261. # end 
  262. # end 
  263. # increment delta and n 
  264. $delta++; 
  265. $n++; 
  266. # end 
  267.  
  268. return $output; 
  269.  
  270. /** 
  271. * Convert a digit to its respective character 
  272. * @see https://tools.ietf.org/html/rfc3492#section-5 
  273. * @throws Requests_Exception On invalid digit (`idna.invalid_digit`) 
  274. * @param int $digit Digit in the range 0-35 
  275. * @return string Single character corresponding to digit 
  276. */ 
  277. protected static function digit_to_char($digit) { 
  278. // @codeCoverageIgnoreStart 
  279. // As far as I know, this never happens, but still good to be sure. 
  280. if ($digit < 0 || $digit > 35) { 
  281. throw new Requests_Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit); 
  282. // @codeCoverageIgnoreEnd 
  283. $digits = 'abcdefghijklmnopqrstuvwxyz0123456789'; 
  284. return substr($digits, $digit, 1); 
  285.  
  286. /** 
  287. * Adapt the bias 
  288. * @see https://tools.ietf.org/html/rfc3492#section-6.1 
  289. * @param int $delta 
  290. * @param int $numpoints 
  291. * @param bool $firsttime 
  292. * @return int New bias 
  293. */ 
  294. protected static function adapt($delta, $numpoints, $firsttime) { 
  295. # function adapt(delta, numpoints, firsttime): 
  296. # if firsttime then let delta = delta div damp 
  297. if ($firsttime) { 
  298. $delta = floor($delta / self::BOOTSTRAP_DAMP); 
  299. # else let delta = delta div 2 
  300. else { 
  301. $delta = floor($delta / 2); 
  302. # let delta = delta + (delta div numpoints) 
  303. $delta += floor($delta / $numpoints); 
  304. # let k = 0 
  305. $k = 0; 
  306. # while delta > ((base - tmin) * tmax) div 2 do begin 
  307. $max = floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN) * self::BOOTSTRAP_TMAX) / 2); 
  308. while ($delta > $max) { 
  309. # let delta = delta div (base - tmin) 
  310. $delta = floor($delta / (self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN)); 
  311. # let k = k + base 
  312. $k += self::BOOTSTRAP_BASE; 
  313. # end 
  314. # return k + (((base - tmin + 1) * delta) div (delta + skew)) 
  315. return $k + floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN + 1) * $delta) / ($delta + self::BOOTSTRAP_SKEW));