| 1 |
<?php |
| 2 |
/** |
| 3 |
* The MIT License (MIT) |
| 4 |
* |
| 5 |
* Copyright (c) 2013 mk-j, zedwood.com |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 |
* of this software and associated documentation files (the "Software"), to deal |
| 9 |
* in the Software without restriction, including without limitation the rights |
| 10 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
* copies of the Software, and to permit persons to whom the Software is |
| 12 |
* furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included in all |
| 15 |
* copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
* SOFTWARE. |
| 24 |
*/ |
| 25 |
function_exists('mb_internal_encoding') or die('unsupported dependency, mbstring'); |
| 26 |
class Punycode |
| 27 |
{ |
| 28 |
const TMIN = 1; |
| 29 |
const TMAX = 26; |
| 30 |
const BASE = 36; |
| 31 |
const INITIAL_N = 128; |
| 32 |
const INITIAL_BIAS = 72; |
| 33 |
const DAMP = 700; |
| 34 |
const SKEW = 38; |
| 35 |
const DELIMITER = '-'; |
| 36 |
//Punycode::::encodeHostName() corresponds to idna_toASCII('xärg.örg'); |
| 37 |
public static function encodeHostName($hostname) |
| 38 |
{ |
| 39 |
if (!self::is_valid_utf8($hostname)) |
| 40 |
{ |
| 41 |
return $hostname;//invalid |
| 42 |
} |
| 43 |
if (function_exists('idn_to_ascii') && 0) |
| 44 |
{ |
| 45 |
return idn_to_ascii($hostname);//php 5.3+ |
| 46 |
} |
| 47 |
$old_encoding = mb_internal_encoding(); |
| 48 |
mb_internal_encoding("UTF-8"); |
| 49 |
$pieces = explode(".", self::mb_strtolower($hostname) ); |
| 50 |
$punycode_pieces = array(); |
| 51 |
foreach($pieces as $piece) |
| 52 |
{ |
| 53 |
if (preg_match("/[\x{80}-\x{FFFF}]/u", $piece))//is multi byte utf8 |
| 54 |
{ |
| 55 |
$punycode_pieces[] = "xn--".self::encode($piece); |
| 56 |
} |
| 57 |
else if (preg_match('/^[a-z\d][a-z\d-]{0,62}$/i', $piece) && !preg_match('/-$/', $piece) )//is valid ascii hostname |
| 58 |
{ |
| 59 |
$punycode_pieces[] = $piece; |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
mb_internal_encoding($old_encoding); |
| 64 |
return $hostname;//invalid domain |
| 65 |
} |
| 66 |
} |
| 67 |
mb_internal_encoding($old_encoding); |
| 68 |
return implode(".", $punycode_pieces); |
| 69 |
} |
| 70 |
//Punycode::::decodeHostName() corresponds to idna_toUnicode('xn--xrg-9ka.xn--rg-eka'); |
| 71 |
public static function decodeHostName($encoded_hostname) |
| 72 |
{ |
| 73 |
if (!preg_match('/[a-z\d.-]{1,255}/', $encoded_hostname)) |
| 74 |
{ |
| 75 |
return false; |
| 76 |
} |
| 77 |
if (function_exists('idn_to_utf8') && 0) |
| 78 |
{ |
| 79 |
return idn_to_utf8($encoded_hostname); |
| 80 |
} |
| 81 |
$old_encoding = mb_internal_encoding(); |
| 82 |
mb_internal_encoding("UTF-8"); |
| 83 |
$pieces = explode(".", strtolower($encoded_hostname)); |
| 84 |
foreach($pieces as $piece) |
| 85 |
{ |
| 86 |
if (!preg_match('/^[a-z\d][a-z\d-]{0,62}$/i', $piece) || preg_match('/-$/', $piece) ) |
| 87 |
{ |
| 88 |
mb_internal_encoding($old_encoding); |
| 89 |
return $encoded_hostname;//invalid |
| 90 |
} |
| 91 |
$punycode_pieces[] = strpos($piece, "xn--")===0 ? self::decode(substr($piece,4)) : $piece; |
| 92 |
} |
| 93 |
mb_internal_encoding($old_encoding); |
| 94 |
return implode(".", $punycode_pieces); |
| 95 |
} |
| 96 |
protected static function encode($input) |
| 97 |
{ |
| 98 |
try |
| 99 |
{ |
| 100 |
$n = self::INITIAL_N; |
| 101 |
$delta = 0; |
| 102 |
$bias = self::INITIAL_BIAS; |
| 103 |
$output=''; |
| 104 |
$input_length = self::mb_strlen($input); |
| 105 |
$b=0; |
| 106 |
for($i=0; $i<$input_length; $i++) |
| 107 |
{ |
| 108 |
$chr = self::mb_substr($input,$i,1); |
| 109 |
$c = self::uniord( $chr );//autoloaded class |
| 110 |
if ($c < self::INITIAL_N) |
| 111 |
{ |
| 112 |
$output.= $chr; |
| 113 |
$b++; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if ($b==$input_length)//no international chars to convert to punycode here |
| 118 |
{ |
| 119 |
throw new Exception("PunycodeException.BAD_INPUT"); |
| 120 |
} |
| 121 |
else if ($b>0) |
| 122 |
{ |
| 123 |
$output.= self::DELIMITER; |
| 124 |
} |
| 125 |
$h = $b; |
| 126 |
while($h < $input_length) |
| 127 |
{ |
| 128 |
$m = PHP_INT_MAX; |
| 129 |
// Find the minimum code point >= n |
| 130 |
for($i=0; $i<$input_length; $i++) |
| 131 |
{ |
| 132 |
$chr = self::mb_substr($input,$i,1); |
| 133 |
$c = self::uniord( $chr ); |
| 134 |
if ($c >= $n && $c < $m) |
| 135 |
{ |
| 136 |
$m = $c; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
if (($m - $n) > (PHP_INT_MAX - $delta) / ($h+1)) |
| 142 |
{ |
| 143 |
throw new Exception("PunycodeException.OVERFLOW"); |
| 144 |
} |
| 145 |
$delta = $delta + ($m - $n) * ($h + 1); |
| 146 |
$n = $m; |
| 147 |
|
| 148 |
for($j=0; $j<$input_length; $j++) |
| 149 |
{ |
| 150 |
$chr = self::mb_substr($input,$j,1); |
| 151 |
$c = self::uniord( $chr ); |
| 152 |
if ($c < $n) |
| 153 |
{ |
| 154 |
$delta++; |
| 155 |
if (0==$delta) |
| 156 |
{ |
| 157 |
throw new Exception("PunycodeException.OVERFLOW"); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if ($c == $n) |
| 162 |
{ |
| 163 |
$q = $delta; |
| 164 |
for($k= self::BASE;; $k+=self::BASE) |
| 165 |
{ |
| 166 |
$t=0; |
| 167 |
if ($k <= $bias) |
| 168 |
{ |
| 169 |
$t= self::TMIN; |
| 170 |
} else if ($k >= $bias + self::TMAX) { |
| 171 |
$t= self::TMAX; |
| 172 |
} else { |
| 173 |
$t = $k - $bias; |
| 174 |
} |
| 175 |
if ($q < $t) |
| 176 |
{ |
| 177 |
break; |
| 178 |
} |
| 179 |
$output.= chr( self::digit2codepoint($t + ($q - $t) % (self::BASE - $t)) ); |
| 180 |
$q = floor( ($q-$t) / (self::BASE - $t) );//integer division |
| 181 |
} |
| 182 |
$output.= chr( self::digit2codepoint($q) ); |
| 183 |
$bias = self::adapt($delta, $h+1, $h==$b); |
| 184 |
$delta=0; |
| 185 |
$h++; |
| 186 |
} |
| 187 |
} |
| 188 |
$delta++; |
| 189 |
$n++; |
| 190 |
} |
| 191 |
} |
| 192 |
catch (Exception $e) |
| 193 |
{ |
| 194 |
error_log("[PUNYCODE] error ".$e->getMessage()); |
| 195 |
return $input; |
| 196 |
} |
| 197 |
return $output; |
| 198 |
} |
| 199 |
protected static function decode($input) |
| 200 |
{ |
| 201 |
try |
| 202 |
{ |
| 203 |
$n = self::INITIAL_N; |
| 204 |
$i = 0; |
| 205 |
$bias = self::INITIAL_BIAS; |
| 206 |
$output = ''; |
| 207 |
$d = self::rstrpos($input, self::DELIMITER); |
| 208 |
if ($d>0) { |
| 209 |
for($j=0; $j<$d; $j++) { |
| 210 |
$chr = self::mb_substr($input,$j,1); |
| 211 |
$c = self::uniord( $chr ); |
| 212 |
if ($c>=self::INITIAL_N) { |
| 213 |
throw new Exception("PunycodeException.BAD_INPUT"); |
| 214 |
} |
| 215 |
$output.=$chr; |
| 216 |
} |
| 217 |
$d++; |
| 218 |
} else { |
| 219 |
$d = 0; |
| 220 |
} |
| 221 |
$input_length = self::mb_strlen($input); |
| 222 |
while ($d < $input_length) { |
| 223 |
$oldi = $i; |
| 224 |
$w = 1; |
| 225 |
for($k= self::BASE;; $k += self::BASE) { |
| 226 |
if ($d == $input_length) { |
| 227 |
throw new Exception("PunycodeException.BAD_INPUT"); |
| 228 |
} |
| 229 |
$chr = self::mb_substr($input,$d++,1); |
| 230 |
$c = self::uniord( $chr ); |
| 231 |
$digit = self::codepoint2digit($c); |
| 232 |
if ($digit > (PHP_INT_MAX - $i) / $w) { |
| 233 |
throw new Exception("PunycodeException.OVERFLOW"); |
| 234 |
} |
| 235 |
$i = $i + $digit * $w; |
| 236 |
$t=0; |
| 237 |
if ($k <= $bias) { |
| 238 |
$t = self::TMIN; |
| 239 |
} else if ($k >= $bias + self::TMAX) { |
| 240 |
$t = self::TMAX; |
| 241 |
} else { |
| 242 |
$t = $k - $bias; |
| 243 |
} |
| 244 |
if ($digit < $t) { |
| 245 |
break; |
| 246 |
} |
| 247 |
$w = $w * (self::BASE - $t); |
| 248 |
} |
| 249 |
$output_length = self::mb_strlen($output); |
| 250 |
$bias = self::adapt($i - $oldi, $output_length + 1, $oldi == 0); |
| 251 |
if ($i / ($output_length + 1) > PHP_INT_MAX - $n) { |
| 252 |
throw new Exception("PunycodeException.OVERFLOW"); |
| 253 |
} |
| 254 |
$n = floor($n + $i / ($output_length + 1)); |
| 255 |
$i = $i % ($output_length + 1); |
| 256 |
$output = self::mb_strinsert($output, self::utf8($n), $i); |
| 257 |
$i++; |
| 258 |
} |
| 259 |
} |
| 260 |
catch(Exception $e) |
| 261 |
{ |
| 262 |
error_log("[PUNYCODE] error ".$e->getMessage()); |
| 263 |
return $input; |
| 264 |
} |
| 265 |
return $output; |
| 266 |
} |
| 267 |
//adapt patched from: |
| 268 |
//https://github.com/takezoh/php-PunycodeEncoder/blob/master/punycode.php |
| 269 |
protected static function adapt($delta, $numpoints, $firsttime) |
| 270 |
{ |
| 271 |
$delta = (int)($firsttime ? $delta / self::DAMP : $delta / 2); |
| 272 |
$delta += (int)($delta / $numpoints); |
| 273 |
$k = 0; |
| 274 |
while ($delta > (((self::BASE - self::TMIN) * self::TMAX) / 2)) { |
| 275 |
$delta = (int)($delta / (self::BASE - self::TMIN)); |
| 276 |
$k += self::BASE; |
| 277 |
} |
| 278 |
return $k + (int)((self::BASE - self::TMIN + 1) * $delta / ($delta + self::SKEW)); |
| 279 |
} |
| 280 |
protected static function digit2codepoint($d) |
| 281 |
{ |
| 282 |
if ($d < 26) { |
| 283 |
// 0..25 : 'a'..'z' |
| 284 |
return $d + ord('a'); |
| 285 |
} else if ($d < 36) { |
| 286 |
// 26..35 : '0'..'9'; |
| 287 |
return $d - 26 + ord('0'); |
| 288 |
} else { |
| 289 |
throw new Exception("PunycodeException.BAD_INPUT"); |
| 290 |
} |
| 291 |
} |
| 292 |
protected static function codepoint2digit($c) |
| 293 |
{ |
| 294 |
if ($c - ord('0') < 10) { |
| 295 |
// '0'..'9' : 26..35 |
| 296 |
return $c - ord('0') + 26; |
| 297 |
} else if ($c - ord('a') < 26) { |
| 298 |
// 'a'..'z' : 0..25 |
| 299 |
return $c - ord('a'); |
| 300 |
} else { |
| 301 |
throw new Exception("PunycodeException.BAD_INPUT"); |
| 302 |
} |
| 303 |
} |
| 304 |
protected static function rstrpos($haystack, $needle) |
| 305 |
{ |
| 306 |
$pos = strpos (strrev($haystack), $needle); |
| 307 |
if ($pos === false) |
| 308 |
return false; |
| 309 |
return strlen ($haystack)-1 - $pos; |
| 310 |
} |
| 311 |
protected static function mb_strinsert($haystack, $needle, $position) |
| 312 |
{ |
| 313 |
$old_encoding = mb_internal_encoding(); |
| 314 |
mb_internal_encoding("UTF-8"); |
| 315 |
$r = mb_substr($haystack,0,$position).$needle.mb_substr($haystack,$position); |
| 316 |
mb_internal_encoding($old_encoding); |
| 317 |
return $r; |
| 318 |
} |
| 319 |
|
| 320 |
protected static function mb_substr($str,$start,$length) |
| 321 |
{ |
| 322 |
$old_encoding = mb_internal_encoding(); |
| 323 |
mb_internal_encoding("UTF-8"); |
| 324 |
$r = mb_substr($str,$start,$length); |
| 325 |
mb_internal_encoding($old_encoding); |
| 326 |
return $r; |
| 327 |
} |
| 328 |
protected static function mb_strlen($str) |
| 329 |
{ |
| 330 |
$old_encoding = mb_internal_encoding(); |
| 331 |
mb_internal_encoding("UTF-8"); |
| 332 |
$r = mb_strlen($str); |
| 333 |
mb_internal_encoding($old_encoding); |
| 334 |
return $r; |
| 335 |
} |
| 336 |
protected static function mb_strtolower($str) |
| 337 |
{ |
| 338 |
$old_encoding = mb_internal_encoding(); |
| 339 |
mb_internal_encoding("UTF-8"); |
| 340 |
$r = mb_strtolower($str); |
| 341 |
mb_internal_encoding($old_encoding); |
| 342 |
return $r; |
| 343 |
} |
| 344 |
|
| 345 |
public static function uniord($c)//cousin of ord() but for unicode |
| 346 |
{ |
| 347 |
$ord0 = ord($c{0}); if ($ord0>=0 && $ord0<=127) return $ord0; |
| 348 |
$ord1 = ord($c{1}); if ($ord0>=192 && $ord0<=223) return ($ord0-192)*64 + ($ord1-128); |
| 349 |
if ($ord0==0xed && ($ord1 & 0xa0) == 0xa0) return false; //code points, 0xd800 to 0xdfff |
| 350 |
$ord2 = ord($c{2}); if ($ord0>=224 && $ord0<=239) return ($ord0-224)*4096 + ($ord1-128)*64 + ($ord2-128); |
| 351 |
$ord3 = ord($c{3}); if ($ord0>=240 && $ord0<=247) return ($ord0-240)*262144 + ($ord1-128)*4096 + ($ord2-128)*64 + ($ord3-128); |
| 352 |
return false; |
| 353 |
} |
| 354 |
public static function utf8($num)//cousin of ascii() but for utf8 |
| 355 |
{ |
| 356 |
if($num<=0x7F) return chr($num); |
| 357 |
if($num<=0x7FF) return chr(($num>>6)+192).chr(($num&63)+128); |
| 358 |
if(0xd800<=$num && $num<=0xdfff) return '';//invalid block of utf8 |
| 359 |
if($num<=0xFFFF) return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128); |
| 360 |
if($num<=0x10FFFF) return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128).chr(($num&63)+128); |
| 361 |
return ''; |
| 362 |
} |
| 363 |
|
| 364 |
public static function is_valid_utf8($string) |
| 365 |
{ |
| 366 |
for ($i=0, $ix=strlen($string); $i < $ix; $i++) |
| 367 |
{ |
| 368 |
$c = ord($string{$i}); |
| 369 |
if ($c==0x09 || $c==0x0a || $c==0x0d || (0x20 <= $c && $c < 0x7e) ) $n = 0; # 0bbbbbbb |
| 370 |
else if (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb |
| 371 |
else if ($c==0xed && (ord($string{$i+1}) & 0xa0)==0xa0) return false; //code points, 0xd800 to 0xdfff |
| 372 |
else if (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb |
| 373 |
else if (($c & 0xF8) == 0xF0) $n=3; # 11110bbb |
| 374 |
//else if (($c & 0xFC) == 0xF8) $n=4; # 111110bb //byte 5, unnecessary in 4 byte UTF-8 |
| 375 |
//else if (($c & 0xFE) == 0xFC) $n=5; # 1111110b //byte 6, unnecessary in 4 byte UTF-8 |
| 376 |
else return false; |
| 377 |
for ($j=0; $j<$n; $j++) { // n bytes matching 10bbbbbb follow ? |
| 378 |
if ((++$i == $ix) || ((ord($string{$i}) & 0xC0) != 0x80)) |
| 379 |
return false; |
| 380 |
} |
| 381 |
} |
| 382 |
return true; |
| 383 |
} |
| 384 |
} |