mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
MDL-69562 admin: Allow getting IP from all sources.
This commit is contained in:
parent
9dabd071fe
commit
15527bf4a8
2 changed files with 44 additions and 1 deletions
|
@ -9284,7 +9284,7 @@ function remoteip_in_list($list) {
|
||||||
function getremoteaddr($default='0.0.0.0') {
|
function getremoteaddr($default='0.0.0.0') {
|
||||||
global $CFG;
|
global $CFG;
|
||||||
|
|
||||||
if (empty($CFG->getremoteaddrconf)) {
|
if (!isset($CFG->getremoteaddrconf)) {
|
||||||
// This will happen, for example, before just after the upgrade, as the
|
// This will happen, for example, before just after the upgrade, as the
|
||||||
// user is redirected to the admin screen.
|
// user is redirected to the admin screen.
|
||||||
$variablestoskip = GETREMOTEADDR_SKIP_DEFAULT;
|
$variablestoskip = GETREMOTEADDR_SKIP_DEFAULT;
|
||||||
|
|
|
@ -3832,6 +3832,49 @@ class core_moodlelib_testcase extends advanced_testcase {
|
||||||
global $CFG;
|
global $CFG;
|
||||||
|
|
||||||
$this->resetAfterTest();
|
$this->resetAfterTest();
|
||||||
|
|
||||||
|
$CFG->getremoteaddrconf = null; // Use default value, GETREMOTEADDR_SKIP_DEFAULT.
|
||||||
|
$noip = getremoteaddr('1.1.1.1');
|
||||||
|
$this->assertEquals('1.1.1.1', $noip);
|
||||||
|
|
||||||
|
$remoteaddr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
|
||||||
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||||||
|
$singleip = getremoteaddr();
|
||||||
|
$this->assertEquals('127.0.0.1', $singleip);
|
||||||
|
|
||||||
|
$_SERVER['REMOTE_ADDR'] = $remoteaddr; // Restore server value.
|
||||||
|
|
||||||
|
$CFG->getremoteaddrconf = 0; // Don't skip any source.
|
||||||
|
$noip = getremoteaddr('1.1.1.1');
|
||||||
|
$this->assertEquals('1.1.1.1', $noip);
|
||||||
|
|
||||||
|
// Populate all $_SERVER values to review order.
|
||||||
|
$ipsources = [
|
||||||
|
'HTTP_CLIENT_IP' => '2.2.2.2',
|
||||||
|
'HTTP_X_FORWARDED_FOR' => '3.3.3.3',
|
||||||
|
'REMOTE_ADDR' => '4.4.4.4',
|
||||||
|
];
|
||||||
|
$originalvalues = [];
|
||||||
|
foreach ($ipsources as $source => $ip) {
|
||||||
|
$originalvalues[$source] = isset($_SERVER[$source]) ? $_SERVER[$source] : null; // Saving data to restore later.
|
||||||
|
$_SERVER[$source] = $ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($ipsources as $source => $expectedip) {
|
||||||
|
$ip = getremoteaddr();
|
||||||
|
$this->assertEquals($expectedip, $ip);
|
||||||
|
unset($_SERVER[$source]); // Removing the value so next time we get the following ip.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore server values.
|
||||||
|
foreach ($originalvalues as $source => $ip) {
|
||||||
|
$_SERVER[$source] = $ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
// All $_SERVER values have been removed, we should get the default again.
|
||||||
|
$noip = getremoteaddr('1.1.1.1');
|
||||||
|
$this->assertEquals('1.1.1.1', $noip);
|
||||||
|
|
||||||
$CFG->getremoteaddrconf = GETREMOTEADDR_SKIP_HTTP_CLIENT_IP;
|
$CFG->getremoteaddrconf = GETREMOTEADDR_SKIP_HTTP_CLIENT_IP;
|
||||||
$xforwardedfor = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : null;
|
$xforwardedfor = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : null;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue