6 * Note: Not fully compliant, as nameprep does nothing yet.
9 * @subpackage Utilities
10 * @see https://tools.ietf.org/html/rfc3490 IDNA specification
11 * @see https://tools.ietf.org/html/rfc3492 Punycode/Bootstrap specification
13 class Requests_IDNAEncoder {
15 * ACE prefix used for IDNA
17 * @see https://tools.ietf.org/html/rfc3490#section-5
20 const ACE_PREFIX = 'xn--';
23 * Bootstrap constant for Punycode
25 * @see https://tools.ietf.org/html/rfc3492#section-5
28 const BOOTSTRAP_BASE = 36;
29 const BOOTSTRAP_TMIN = 1;
30 const BOOTSTRAP_TMAX = 26;
31 const BOOTSTRAP_SKEW = 38;
32 const BOOTSTRAP_DAMP = 700;
33 const BOOTSTRAP_INITIAL_BIAS = 72;
34 const BOOTSTRAP_INITIAL_N = 128;
38 * Encode a hostname using Punycode
40 * @param string $string Hostname
41 * @return string Punycode-encoded hostname
43 public static function encode($string) {
44 $parts = explode('.', $string);
45 foreach ($parts as &$part) {
46 $part = self::to_ascii($part);
48 return implode('.', $parts);
52 * Convert a UTF-8 string to an ASCII string using Punycode
54 * @throws Requests_Exception Provided string longer than 64 ASCII characters (`idna.provided_too_long`)
55 * @throws Requests_Exception Prepared string longer than 64 ASCII characters (`idna.prepared_too_long`)
56 * @throws Requests_Exception Provided string already begins with xn-- (`idna.provided_is_prefixed`)
57 * @throws Requests_Exception Encoded string longer than 64 ASCII characters (`idna.encoded_too_long`)
59 * @param string $string ASCII or UTF-8 string (max length 64 characters)
60 * @return string ASCII string
62 public static function to_ascii($string) {
63 // Step 1: Check if the string is already ASCII
64 if (self::is_ascii($string)) {
66 if (strlen($string) < 64) {
70 throw new Requests_Exception('Provided string is too long', 'idna.provided_too_long', $string);
74 $string = self::nameprep($string);
76 // Step 3: UseSTD3ASCIIRules is false, continue
77 // Step 4: Check if it's ASCII now
78 if (self::is_ascii($string)) {
80 if (strlen($string) < 64) {
84 throw new Requests_Exception('Prepared string is too long', 'idna.prepared_too_long', $string);
87 // Step 5: Check ACE prefix
88 if (strpos($string, self::ACE_PREFIX) === 0) {
89 throw new Requests_Exception('Provided string begins with ACE prefix', 'idna.provided_is_prefixed', $string);
92 // Step 6: Encode with Punycode
93 $string = self::punycode_encode($string);
95 // Step 7: Prepend ACE prefix
96 $string = self::ACE_PREFIX . $string;
99 if (strlen($string) < 64) {
103 throw new Requests_Exception('Encoded string is too long', 'idna.encoded_too_long', $string);
107 * Check whether a given string contains only ASCII characters
109 * @internal (Testing found regex was the fastest implementation)
111 * @param string $string
112 * @return bool Is the string ASCII-only?
114 protected static function is_ascii($string) {
115 return (preg_match('/(?:[^\x00-\x7F])/', $string) !== 1);
119 * Prepare a string for use as an IDNA name
121 * @todo Implement this based on RFC 3491 and the newer 5891
122 * @param string $string
123 * @return string Prepared string
125 protected static function nameprep($string) {
130 * Convert a UTF-8 string to a UCS-4 codepoint array
132 * Based on Requests_IRI::replace_invalid_with_pct_encoding()
134 * @throws Requests_Exception Invalid UTF-8 codepoint (`idna.invalidcodepoint`)
135 * @param string $input
136 * @return array Unicode code points
138 protected static function utf8_to_codepoints($input) {
139 $codepoints = array();
141 // Get number of bytes
142 $strlen = strlen($input);
144 for ($position = 0; $position < $strlen; $position++) {
145 $value = ord($input[$position]);
147 // One byte sequence:
148 if ((~$value & 0x80) === 0x80) {
153 // Two byte sequence:
154 elseif (($value & 0xE0) === 0xC0) {
155 $character = ($value & 0x1F) << 6;
159 // Three byte sequence:
160 elseif (($value & 0xF0) === 0xE0) {
161 $character = ($value & 0x0F) << 12;
165 // Four byte sequence:
166 elseif (($value & 0xF8) === 0xF0) {
167 $character = ($value & 0x07) << 18;
173 throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $value);
176 if ($remaining > 0) {
177 if ($position + $length > $strlen) {
178 throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
180 for ($position++; $remaining > 0; $position++) {
181 $value = ord($input[$position]);
183 // If it is invalid, count the sequence as invalid and reprocess the current byte:
184 if (($value & 0xC0) !== 0x80) {
185 throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
188 $character |= ($value & 0x3F) << (--$remaining * 6);
194 // Non-shortest form sequences are invalid
195 $length > 1 && $character <= 0x7F
196 || $length > 2 && $character <= 0x7FF
197 || $length > 3 && $character <= 0xFFFF
198 // Outside of range of ucschar codepoints
200 || ($character & 0xFFFE) === 0xFFFE
201 || $character >= 0xFDD0 && $character <= 0xFDEF
203 // Everything else not in ucschar
204 $character > 0xD7FF && $character < 0xF900
206 || $character > 0x7E && $character < 0xA0
207 || $character > 0xEFFFD
210 throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
213 $codepoints[] = $character;
220 * RFC3492-compliant encoder
222 * @internal Pseudo-code from Section 6.3 is commented with "#" next to relevant code
223 * @throws Requests_Exception On character outside of the domain (never happens with Punycode) (`idna.character_outside_domain`)
225 * @param string $input UTF-8 encoded string to encode
226 * @return string Punycode-encoded string
228 public static function punycode_encode($input) {
231 $n = self::BOOTSTRAP_INITIAL_N;
234 # let bias = initial_bias
235 $bias = self::BOOTSTRAP_INITIAL_BIAS;
236 # let h = b = the number of basic code points in the input
237 $h = $b = 0; // see loop
238 # copy them to the output in order
239 $codepoints = self::utf8_to_codepoints($input);
242 foreach ($codepoints as $char) {
244 // Character is valid ASCII
245 // TODO: this should also check if it's valid for a URL
246 $output .= chr($char);
249 // Check if the character is non-ASCII, but below initial n
250 // This never occurs for Punycode, so ignore in coverage
251 // @codeCoverageIgnoreStart
252 elseif ($char < $n) {
253 throw new Requests_Exception('Invalid character', 'idna.character_outside_domain', $char);
255 // @codeCoverageIgnoreEnd
257 $extended[$char] = true;
260 $extended = array_keys($extended);
263 # [copy them] followed by a delimiter if b > 0
264 if (strlen($output) > 0) {
267 # {if the input contains a non-basic code point < n then fail}
268 # while h < length(input) do begin
269 while ($h < count($codepoints)) {
270 # let m = the minimum code point >= n in the input
271 $m = array_shift($extended);
272 //printf('next code point to insert is %s' . PHP_EOL, dechex($m));
273 # let delta = delta + (m - n) * (h + 1), fail on overflow
274 $delta += ($m - $n) * ($h + 1);
277 # for each code point c in the input (in order) do begin
278 for ($num = 0; $num < count($codepoints); $num++) {
279 $c = $codepoints[$num];
280 # if c < n then increment delta, fail on overflow
284 # if c == n then begin
288 # for k = base to infinity in steps of base do begin
289 for ($k = self::BOOTSTRAP_BASE; ; $k += self::BOOTSTRAP_BASE) {
290 # let t = tmin if k <= bias {+ tmin}, or
291 # tmax if k >= bias + tmax, or k - bias otherwise
292 if ($k <= ($bias + self::BOOTSTRAP_TMIN)) {
293 $t = self::BOOTSTRAP_TMIN;
295 elseif ($k >= ($bias + self::BOOTSTRAP_TMAX)) {
296 $t = self::BOOTSTRAP_TMAX;
301 # if q < t then break
305 # output the code point for digit t + ((q - t) mod (base - t))
306 $digit = $t + (($q - $t) % (self::BOOTSTRAP_BASE - $t));
307 $output .= self::digit_to_char($digit);
308 # let q = (q - t) div (base - t)
309 $q = floor(($q - $t) / (self::BOOTSTRAP_BASE - $t));
312 # output the code point for digit q
313 $output .= self::digit_to_char($q);
314 # let bias = adapt(delta, h + 1, test h equals b?)
315 $bias = self::adapt($delta, $h + 1, $h === $b);
324 # increment delta and n
334 * Convert a digit to its respective character
336 * @see https://tools.ietf.org/html/rfc3492#section-5
337 * @throws Requests_Exception On invalid digit (`idna.invalid_digit`)
339 * @param int $digit Digit in the range 0-35
340 * @return string Single character corresponding to digit
342 protected static function digit_to_char($digit) {
343 // @codeCoverageIgnoreStart
344 // As far as I know, this never happens, but still good to be sure.
345 if ($digit < 0 || $digit > 35) {
346 throw new Requests_Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit);
348 // @codeCoverageIgnoreEnd
349 $digits = 'abcdefghijklmnopqrstuvwxyz0123456789';
350 return substr($digits, $digit, 1);
356 * @see https://tools.ietf.org/html/rfc3492#section-6.1
358 * @param int $numpoints
359 * @param bool $firsttime
360 * @return int New bias
362 protected static function adapt($delta, $numpoints, $firsttime) {
363 # function adapt(delta,numpoints,firsttime):
364 # if firsttime then let delta = delta div damp
366 $delta = floor($delta / self::BOOTSTRAP_DAMP);
368 # else let delta = delta div 2
370 $delta = floor($delta / 2);
372 # let delta = delta + (delta div numpoints)
373 $delta += floor($delta / $numpoints);
376 # while delta > ((base - tmin) * tmax) div 2 do begin
377 $max = floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN) * self::BOOTSTRAP_TMAX) / 2);
378 while ($delta > $max) {
379 # let delta = delta div (base - tmin)
380 $delta = floor($delta / (self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN));
382 $k += self::BOOTSTRAP_BASE;
385 # return k + (((base - tmin + 1) * delta) div (delta + skew))
386 return $k + floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN + 1) * $delta) / ($delta + self::BOOTSTRAP_SKEW));