From 81c62f2696b0efb6c5eedeb68562b99c7864019d Mon Sep 17 00:00:00 2001 From: Stefan Neufeind Date: Sun, 14 Nov 2010 20:30:23 +0000 Subject: [PATCH] * fixed handling of URLs with more than a domain-name (fixed opposite of parse_url()) git-svn-id: http://svn.php.net/repository/pear/packages/Net_IDNA2/trunk@305340 c90b9560-bf6c-de11-be94-00142212c4b1 --- Net/IDNA2.php | 36 +++++++++++++++++++++++++++++++++++- tests/Net_IDNA2Test.php | 6 +++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Net/IDNA2.php b/Net/IDNA2.php index b2aa545..e72c6e4 100644 --- a/Net/IDNA2.php +++ b/Net/IDNA2.php @@ -2452,7 +2452,7 @@ public function decode($input, $one_time_encoding = false) if (isset($parsed['scheme'])) { $parsed['scheme'] .= (strtolower($parsed['scheme']) == 'mailto') ? ':' : '://'; } - $return = join('', $parsed); + $return = $this->_unparse_url($parsed); } else { // parse_url seems to have failed, try without it $arr = explode('.', $input); foreach ($arr as $k => $v) { @@ -2483,6 +2483,40 @@ public function decode($input, $one_time_encoding = false) // {{{ private + /** + * Opposite function to parse_url() + * + * Inspired by code from comments of php.net-documentation for parse_url() + * + * @array string parts as returned by parse_url() + * @return string + * @access private + */ + private function _unparse_url($parts_arr) { + if (!empty($parts_arr['scheme'])) { + $ret_url = $parts_arr['scheme']; + } + if(!empty($parts_arr['user'])) { + $ret_url .= $parts_arr['user']; + if (!empty($parts_arr['pass'])) { + $ret_url .= ':' . $parts_arr['pass']; + } + $ret_url .= '@'; + } + $ret_url .= $parts_arr['host']; + if (!empty($parts_arr['port'])) { + $ret_url .= ':' . $parts_arr['port']; + } + $ret_url .= $parts_arr['path']; + if (!empty($parts_arr['query'])) { + $ret_url .= '?' . $parts_arr['query']; + } + if (!empty($parts_arr['fragment'])) { + $ret_url .= '#' . $parts_arr['fragment']; + } + return $ret_url; + } + /** * The actual encoding algorithm. * diff --git a/tests/Net_IDNA2Test.php b/tests/Net_IDNA2Test.php index 7f0571b..d47caaa 100644 --- a/tests/Net_IDNA2Test.php +++ b/tests/Net_IDNA2Test.php @@ -9,11 +9,11 @@ public function setUp() { } public function testShouldDecodePortNumbersFragmentsAndUrisCorrectly() { - $result = $this->idn->decode('http://www.xn--ml-6kctd8d6a.org:8080/test.php?arg=1#fragment'); - // not sure where this testcase came from, but it's wrong +// $result = $this->idn->decode('http://www.xn--ml-6kctd8d6a.org:8080/test.php?arg1=1&arg2=2#fragment'); // $this->assertSame("http://www.╨╡╤à╨░m╤Çl╨╡.org:8080/test.php?arg=1#fragment", $result); - $this->assertSame("http://www.example.org:8080/test.php?arg=1#fragment", $result); + $result = $this->idn->decode('http://xn--tst-qla.example.com:8080/test.php?arg1=1&arg2=2#fragment'); + $this->assertSame("http://täst.example.com:8080/test.php?arg1=1&arg2=2#fragment", $result); } public function testEncodingForGermanEszettUsingIDNA2003() {