diff --git a/lib/Cake/Event/CakeEventManager.php b/lib/Cake/Event/CakeEventManager.php index 007ef521f..f302ee429 100644 --- a/lib/Cake/Event/CakeEventManager.php +++ b/lib/Cake/Event/CakeEventManager.php @@ -72,7 +72,7 @@ class CakeEventManager { self::$_generalManager = $manager; } if (empty(self::$_generalManager)) { - self::$_generalManager = new CakeEventManager; + self::$_generalManager = new CakeEventManager(); } self::$_generalManager->_isGlobal = true; @@ -249,7 +249,6 @@ class CakeEventManager { if ($result !== null) { $event->result = $result; } - continue; } } diff --git a/lib/Cake/Network/CakeRequest.php b/lib/Cake/Network/CakeRequest.php index 648c3643f..d165e8929 100644 --- a/lib/Cake/Network/CakeRequest.php +++ b/lib/Cake/Network/CakeRequest.php @@ -385,7 +385,7 @@ class CakeRequest implements ArrayAccess { * Get the IP the client is using, or says they are using. * * @param boolean $safe Use safe = false when you think the user might manipulate their HTTP_CLIENT_IP - * header. Setting $safe = false will will also look at HTTP_X_FORWARDED_FOR + * header. Setting $safe = false will also look at HTTP_X_FORWARDED_FOR * @return string The client IP. */ public function clientIp($safe = true) { @@ -417,10 +417,6 @@ class CakeRequest implements ArrayAccess { */ public function referer($local = false) { $ref = env('HTTP_REFERER'); - $forwarded = env('HTTP_X_FORWARDED_HOST'); - if ($forwarded) { - $ref = $forwarded; - } $base = Configure::read('App.fullBaseUrl') . $this->webroot; if (!empty($ref) && !empty($base)) { @@ -678,9 +674,13 @@ class CakeRequest implements ArrayAccess { /** * Get the host that the request was handled on. * + * @param boolean $trustProxy Whether or not to trust the proxy host. * @return string */ - public function host() { + public function host($trustProxy = false) { + if ($trustProxy) { + return env('HTTP_X_FORWARDED_HOST'); + } return env('HTTP_HOST'); } diff --git a/lib/Cake/Test/Case/Network/CakeRequestTest.php b/lib/Cake/Test/Case/Network/CakeRequestTest.php index 8b4d99dab..6b0499488 100644 --- a/lib/Cake/Test/Case/Network/CakeRequestTest.php +++ b/lib/Cake/Test/Case/Network/CakeRequestTest.php @@ -698,10 +698,6 @@ class CakeRequestTest extends CakeTestCase { $_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/recipes/add'; $result = $request->referer(true); $this->assertSame($result, '/recipes/add'); - - $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org'; - $result = $request->referer(); - $this->assertSame($result, 'cakephp.org'); } /** @@ -804,9 +800,11 @@ class CakeRequestTest extends CakeTestCase { */ public function testHost() { $_SERVER['HTTP_HOST'] = 'localhost'; + $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org'; $request = new CakeRequest('some/path'); $this->assertEquals('localhost', $request->host()); + $this->assertEquals('cakephp.org', $request->host(true)); } /** diff --git a/lib/Cake/Test/Case/Utility/HashTest.php b/lib/Cake/Test/Case/Utility/HashTest.php index a23b85f2c..426781f5e 100644 --- a/lib/Cake/Test/Case/Utility/HashTest.php +++ b/lib/Cake/Test/Case/Utility/HashTest.php @@ -1527,6 +1527,34 @@ class HashTest extends CakeTestCase { $this->assertEquals($expected, $result); } +/** + * test combine() giving errors on key/value length mismatches. + * + * @expectedException CakeException + * @return void + */ + public function testCombineErrorMissingValue() { + $data = array( + array('User' => array('id' => 1, 'name' => 'mark')), + array('User' => array('name' => 'jose')), + ); + Hash::combine($data, '{n}.User.id', '{n}.User.name'); + } + +/** + * test combine() giving errors on key/value length mismatches. + * + * @expectedException CakeException + * @return void + */ + public function testCombineErrorMissingKey() { + $data = array( + array('User' => array('id' => 1, 'name' => 'mark')), + array('User' => array('id' => 2)), + ); + Hash::combine($data, '{n}.User.id', '{n}.User.name'); + } + /** * test combine() with a group path. * diff --git a/lib/Cake/Utility/Hash.php b/lib/Cake/Utility/Hash.php index 3ae1767ff..7482e99d5 100644 --- a/lib/Cake/Utility/Hash.php +++ b/lib/Cake/Utility/Hash.php @@ -365,6 +365,7 @@ class Hash { * @param string $groupPath A dot-separated string. * @return array Combined array * @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::combine + * @throws CakeException CakeException When keys and values count is unequal. */ public static function combine(array $data, $keyPath, $valuePath = null, $groupPath = null) { if (empty($data)) { @@ -387,10 +388,15 @@ class Hash { } elseif (!empty($valuePath)) { $vals = self::extract($data, $valuePath); } + if (empty($vals)) { + $vals = array_fill(0, count($keys), null); + } - $count = count($keys); - for ($i = 0; $i < $count; $i++) { - $vals[$i] = isset($vals[$i]) ? $vals[$i] : null; + if (count($keys) !== count($vals)) { + throw new CakeException(__d( + 'cake_dev', + 'Hash::combine() needs an equal number of keys + values.' + )); } if ($groupPath !== null) {