Browse Source

Add missing urlencoding to base/webroot.

This fixes URL generation when PHP_SELF or the request path contains
special characters.
mark_story 13 years ago
parent
commit
6017db9dc8

+ 1 - 1
lib/Cake/Network/CakeRequest.php

@@ -292,7 +292,7 @@ class CakeRequest implements ArrayAccess {
 			if ($base === DS || $base === '.') {
 				$base = '';
 			}
-
+			$base = implode('/', array_map('rawurlencode', explode('/', $base)));
 			$this->webroot = $base . '/';
 			return $this->base = $base;
 		}

+ 9 - 0
lib/Cake/Test/Case/Network/CakeRequestTest.php

@@ -1101,6 +1101,15 @@ class CakeRequestTest extends CakeTestCase {
 		Configure::write('App.baseUrl', false);
 
 		$_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
+		$_SERVER['PHP_SELF'] = '/urlencode me/app/webroot/index.php';
+		$_SERVER['PATH_INFO'] = '/posts/view/1';
+
+		$request = new CakeRequest();
+		$this->assertEquals('/urlencode%20me', $request->base);
+		$this->assertEquals('/urlencode%20me/', $request->webroot);
+		$this->assertEquals('posts/view/1', $request->url);
+
+		$_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
 		$_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
 		$_SERVER['PATH_INFO'] = '/posts/view/1';
 

+ 4 - 0
lib/Cake/Test/Case/View/HelperTest.php

@@ -605,6 +605,10 @@ class HelperTest extends CakeTestCase {
 
 		Configure::write('Asset.timestamp', true);
 		Configure::write('debug', 0);
+
+		$result = $this->Helper->assetTimestamp('/%3Cb%3E/cake.generic.css');
+		$this->assertEquals('/%3Cb%3E/cake.generic.css', $result);
+
 		$result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
 		$this->assertEquals(CSS_URL . 'cake.generic.css', $result);
 

+ 8 - 5
lib/Cake/View/Helper.php

@@ -341,10 +341,9 @@ class Helper extends Object {
  */
 	protected function _encodeUrl($url) {
 		$path = parse_url($url, PHP_URL_PATH);
-		$encoded = implode('/', array_map(
-			'rawurlencode',
-			explode('/', $path)
-		));
+		$parts = array_map('urldecode', explode('/', $path));
+		$parts = array_map('rawurlencode', $parts);
+		$encoded = implode('/', $parts);
 		return h(str_replace($path, $encoded, $url));
 	}
 
@@ -360,7 +359,11 @@ class Helper extends Object {
 		$stamp = Configure::read('Asset.timestamp');
 		$timestampEnabled = $stamp === 'force' || ($stamp === true && Configure::read('debug') > 0);
 		if ($timestampEnabled && strpos($path, '?') === false) {
-			$filepath = preg_replace('/^' . preg_quote($this->request->webroot, '/') . '/', '', $path);
+			$filepath = preg_replace(
+				'/^' . preg_quote($this->request->webroot, '/') . '/',
+				'',
+				urldecode($path)
+			);
 			$webrootPath = WWW_ROOT . str_replace('/', DS, $filepath);
 			if (file_exists($webrootPath)) {
 				//@codingStandardsIgnoreStart