Permalink
Browse files

rawurlencode fix

  • Loading branch information...
1 parent b706e08 commit 40c47d3175e89a30c2acd7c7a8459b521d637670 @stefanneculai committed Mar 25, 2012
Showing with 22 additions and 11 deletions.
  1. +16 −11 libraries/joomla/string/string.php
  2. +6 −0 tests/suites/unit/joomla/string/JStringTest.php
View
27 libraries/joomla/string/string.php
@@ -958,20 +958,25 @@ public static function compliant($str)
*/
public static function parse_url($url)
{
- $result = array();
- // Build arrays of values we need to decode before parsing
- $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B',
- '%5D');
- $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "$", ",", "/", "?", "%", "#", "[", "]");
+ $keys = array('scheme' => 0, 'user' => 0, 'pass' => 0, 'host' => 0, 'port' => 0, 'path' => 0, 'query' => 0, 'fragment' => 0);
- // Create encoded URL with special URL characters decoded so it can be parsed
- // All other characters will be encoded
- $encodedURL = str_replace($entities, $replacements, urlencode($url));
+ // The u parameter is the key to determine UTF-8 characters
+ $expression = '~^((?P<scheme>[^:/?#]+):(//))?((\3|//)?(?:(?P<user>[^:]+):(?P<pass>[^@]+)@)?(?P<host>[^/?:#]*))(:(?P<port>\d+))?' .
+ '(?P<path>[^?#]*)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?~u';
- // Parse the encoded URL
- $encodedParts = parse_url($encodedURL);
+ if (preg_match($expression, $url, $matches))
+ {
+ foreach ($matches as $key => $value)
+ {
+ if (!isset($keys[$key]) || empty($value))
+ {
+ unset($matches[$key]);
+ }
+ }
+ return $matches;
+ }
- return $encodedParts;
+ false;
}
}
View
6 tests/suites/unit/joomla/string/JStringTest.php
@@ -633,5 +633,11 @@ public function testParse_Url()
$expected = parse_url($url);
$actual = JString::parse_url($url);
$this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal');
+
+ // Test rawurlencode in query
+ $url = 'http://a.com/b.php?retUrl='.rawurlencode('http://a.com/b.php?a=123&b=456');
+ $expected = parse_url($url);
+ $actual = JString::parse_url($url);
+ $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal');
}
}

0 comments on commit 40c47d3

Please sign in to comment.