diff --git a/cake/libs/cake_request.php b/cake/libs/cake_request.php index f4a0a1efe..a2724a03b 100644 --- a/cake/libs/cake_request.php +++ b/cake/libs/cake_request.php @@ -48,10 +48,6 @@ class CakeRequest { if (isset($_POST)) { $this->_processPost(); } - if (isset($params['form']['data'])) { - $params['data'] = $params['form']['data']; - unset($params['form']['data']); - } if (isset($_GET)) { $this->_processGet(); } @@ -137,4 +133,32 @@ class CakeRequest { } } } + +/** + * 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 + * @return void + */ + public function getClientIp($safe = true) { + if (!$safe && env('HTTP_X_FORWARDED_FOR') != null) { + $ipaddr = preg_replace('/(?:,.*)/', '', env('HTTP_X_FORWARDED_FOR')); + } else { + if (env('HTTP_CLIENT_IP') != null) { + $ipaddr = env('HTTP_CLIENT_IP'); + } else { + $ipaddr = env('REMOTE_ADDR'); + } + } + + if (env('HTTP_CLIENTADDRESS') != null) { + $tmpipaddr = env('HTTP_CLIENTADDRESS'); + + if (!empty($tmpipaddr)) { + $ipaddr = preg_replace('/(?:,.*)/', '', $tmpipaddr); + } + } + return trim($ipaddr); + } } \ No newline at end of file diff --git a/cake/tests/cases/libs/cake_request.test.php b/cake/tests/cases/libs/cake_request.test.php index 2211b665e..914cfa031 100644 --- a/cake/tests/cases/libs/cake_request.test.php +++ b/cake/tests/cases/libs/cake_request.test.php @@ -57,4 +57,42 @@ class CakeRequestTestCase extends CakeTestCase { $request = new CakeRequest(); $this->assertEqual($request->params['form'], $_POST); } + +/** + * test method overrides coming in from POST data. + * + * @return void + */ + function testMethodOverrides() { + $_POST = array('_method' => 'POST'); + $request = new CakeRequest(); + $this->assertEqual(env('REQUEST_METHOD'), 'POST'); + + $_POST = array('_method' => 'DELETE'); + $request = new CakeRequest(); + $this->assertEqual(env('REQUEST_METHOD'), 'DELETE'); + } + +/** + * test the getClientIp method. + * + * @return void + */ + function testGetClientIp() { + $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com'; + $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2'; + $_SERVER['REMOTE_ADDR'] = '192.168.1.3'; + $request = new CakeRequest(); + $this->assertEqual($request->getClientIP(false), '192.168.1.5'); + $this->assertEqual($request->getClientIP(), '192.168.1.2'); + + unset($_SERVER['HTTP_X_FORWARDED_FOR']); + $this->assertEqual($request->getClientIP(), '192.168.1.2'); + + unset($_SERVER['HTTP_CLIENT_IP']); + $this->assertEqual($request->getClientIP(), '192.168.1.3'); + + $_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1'; + $this->assertEqual($request->getClientIP(), '10.0.1.2'); + } } \ No newline at end of file