mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Moving getClientIp into CakeRequest. Moving relevant tests as well.
This commit is contained in:
parent
15a4607061
commit
8f207a5f69
2 changed files with 66 additions and 4 deletions
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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');
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue