diff --git a/Test.php b/Test.php new file mode 100644 index 0000000..bc08744 --- /dev/null +++ b/Test.php @@ -0,0 +1,170 @@ +assertEquals( $output, EncodePunycodeIDN( $input ), 'Encode: ' . $description ); + } + + /** + * @dataProvider provider_PunycodeIDN + */ + public function testDecodePunycodeIDN( $output, $input, $description ) { + $this->assertEquals( $output, DecodePunycodeIDN( $input ), 'Decode: ' . $description ); + } + + public function provider_PunycodeIDN() { + + return array( + + array( + 'я.рф', + 'xn--41a.xn--p1ai', + 'я.рф' + ), + + array( + 'симферополь-мп.рф', + 'xn----itbinddlaieei5b8h.xn--p1ai', + 'симферополь-мп.рф' + ), + + array( + 'ссмферополь-мп', + 'xn----itbscdjahedia4b8h', + 'симферополь-мп' + ), + + array( + 'яндекс.рф', + 'xn--d1acpjx3f.xn--p1ai', + 'яндекс.рф' + ), + + + array( + 'ليهمابتكلموشعربي؟', + 'xn--egbpdaj6bu4bxfgehfvwxn', + '(A) Arabic (Egyptian)' + ), + + array( + '他们为什么不说中文', + 'xn--ihqwcrb4cv8a8dqg056pqjye', + '(B) Chinese (simplified)' + ), + + array( + '他們爲什麽不說中文', + 'xn--ihqwctvzc91f659drss3x8bo0yb', + '(C) Chinese (traditional)' + ), + + array( + 'pročprostěnemluvíčesky', + 'xn--proprostnemluvesky-uyb24dma41a', + '(D) Czech' + ), + + array( + 'למההםפשוטלאמדבריםעברית', + 'xn--4dbcagdahymbxekheh6e0a7fei0b', + '(E) Hebrew' + ), + + array( + 'यहलोगहिन्दीक्योंनहींबोलसकतेहैं', + 'xn--i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd', + '(F) Hindi (Devanagari)' + ), + + array( + 'なぜみんな日本語を話してくれないのか', + 'xn--n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa', + '(G) Japanese (kanji and hiragana)' + ), + + array( + '세계의모든사람들이한국어를이해한다면얼마나좋을까', + 'xn--989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5jpsd879ccm6fea98c', + '(H) Korean (Hangul syllables)' + ), + + array( + 'почемужеонинеговорятпорусски', + 'xn--b1abfaaepdrnnbgefbadotcwatmq2g4l', + '(I) Russian (Cyrillic)' + ), + + array( + 'PorquénopuedensimplementehablarenEspañol', + 'xn--PorqunopuedensimplementehablarenEspaol-fmd56a', + '(J) Spanish' + ), + + array( + 'TạisaohọkhôngthểchỉnóitiếngViệt', + 'xn--TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g', + '(K) Vietnamese' + ), + + array( + '3年B組金八先生', + 'xn--3B-ww4c5e180e575a65lsy2b', + '(L) 3B' + ), + + array( + '安室奈美恵-with-SUPER-MONKEYS', + 'xn---with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n', + '(M) -with-SUPER-MONKEYS' + ), + + array( + 'Hello-Another-Way-それぞれの場所', + 'xn--Hello-Another-Way--fc4qua05auwb3674vfr0b', + '(N) Hello-Another-Way-' + ), + + array( + 'ひとつ屋根の下2', + 'xn--2-u9tlzr9756bt3uc0v', + '(O) 2' + ), + + array( + 'MajiでKoiする5秒前', + 'xn--MajiKoi5-783gue6qz075azm5e', + '(P) MajiKoi5' + ), + + array( + 'パフィーdeルンバ', + 'xn--de-jg4avhby1noc0d', + '(Q) de' + ), + + array( + 'そのスピードで', + 'xn--d9juau41awczczp', + '(R) ' + ), + + array( + '-> $1.00 <-', + '-> $1.00 <-', + '(S)-> $1.00 <-' + ) + + ); + + } +} diff --git a/idna.php b/idna.php index ace0adb..65f86f8 100644 --- a/idna.php +++ b/idna.php @@ -45,7 +45,7 @@ function ordUTF8($c, $index = 0, &$bytes = null) * @return string Encoded Domain name * * @author Igor V Belousov - * @copyright 2013 Igor V Belousov + * @copyright 2013, 2015 Igor V Belousov * @license http://opensource.org/licenses/LGPL-2.1 LGPL v2.1 * @link http://belousovv.ru/myscript/phpIDN */ @@ -147,7 +147,7 @@ function EncodePunycodeIDN( $value ) { break; } - $tmp_int = ( $t + ( ( $q - $t ) % ( 36 - $t ) ) ); + $tmp_int = $t + ( $q - $t ) % ( 36 - $t ); $output[] = chr( ( $tmp_int + 22 + 75 * ( $tmp_int < 26 ) ) ); $q = ( $q - $t ) / ( 36 - $t ); } @@ -161,10 +161,10 @@ function EncodePunycodeIDN( $value ) $k2 = 0; while ( $delta > 455 ) { - $delta = intval( $delta / 35 ); + $delta /= 35; $k2 += 36; } - $bias= intval( $k2 + ( ( 36 * $delta ) / ( $delta + 38 ) ) ); + $bias = intval( $k2 + 36 * $delta / ( $delta + 38 ) ); /* end section-6.1 */ $delta = 0; ++$h; @@ -183,11 +183,11 @@ function EncodePunycodeIDN( $value ) * @return string Domain name in UTF-8 charset * * @author Igor V Belousov - * @copyright 2013 Igor V Belousov + * @copyright 2013, 2015 Igor V Belousov * @license http://opensource.org/licenses/LGPL-2.1 LGPL v2.1 * @link http://belousovv.ru/myscript/phpIDN */ -function DecodePunycodeIDN($value) +function DecodePunycodeIDN( $value ) { /* search subdomains */ $sub_domain = explode( '.', $value ); @@ -234,7 +234,7 @@ function DecodePunycodeIDN($value) while ($d < strlen( $value ) ) { - $oldi = $i; + $old_i = $i; $w = 1; for ($k = 36;; $k += 36) @@ -246,7 +246,7 @@ function DecodePunycodeIDN($value) $c = $value[ $d++ ]; $c = ord( $c ); - $digit = ( $c - 48 < 10) ? $c - 22 : + $digit = ( $c - 48 < 10 ) ? $c - 22 : ( ( $c - 65 < 26 ) ? $c - 65 : ( @@ -275,17 +275,17 @@ function DecodePunycodeIDN($value) break; } - $w *= ( 36 - $t ); + $w *= 36 - $t; } - $delta = $i - $oldi; + $delta = $i - $old_i; /* http://tools.ietf.org/html/rfc3492#section-6.1 */ - $delta = ( $oldi == 0 ) ? $delta/700 : $delta>>1; + $delta = ( $old_i == 0 ) ? $delta/700 : $delta>>1; $count_output_plus_one = count( $output ) + 1; - $delta += ( $delta / ( $count_output_plus_one + 1 ) ); + $delta += intval( $delta / $count_output_plus_one ); $k2 = 0; while ( $delta > 455 ) @@ -293,14 +293,14 @@ function DecodePunycodeIDN($value) $delta /= 35; $k2 += 36; } - $bias = intval( $k2 + ( 36 * $delta ) / ( $delta + 38 ) ); + $bias = intval( $k2 + 36 * $delta / ( $delta + 38 ) ); /* end section-6.1 */ if ( $i / $count_output_plus_one > 0x10FFFF - $n ) { return $bad_input; } $n += intval( $i / $count_output_plus_one ); - $i = intval( $i % $count_output_plus_one ); + $i %= $count_output_plus_one; array_splice( $output, $i, 0, html_entity_decode( '&#' . $n . ';', ENT_NOQUOTES, 'UTF-8' ) );