diff --git a/lib/datalib.php b/lib/datalib.php index e242df05519..18ddf5005c7 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -2007,9 +2007,6 @@ function add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user } $REMOTE_ADDR = getremoteaddr(); - if (empty($REMOTE_ADDR)) { - $REMOTE_ADDR = '0.0.0.0'; - } $timenow = time(); $info = $info; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index dad6b677f18..16980c85b79 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -2526,8 +2526,8 @@ function require_user_key_login($script, $instance=null) { } if ($key->iprestriction) { - $remoteaddr = getremoteaddr(); - if ($remoteaddr == '' or !address_in_subnet($remoteaddr, $key->iprestriction)) { + $remoteaddr = getremoteaddr(null); + if (empty($remoteaddr) or !address_in_subnet($remoteaddr, $key->iprestriction)) { print_error('ipmismatch'); } } @@ -8552,6 +8552,9 @@ function make_unique_id_code($extra='') { */ function address_in_subnet($addr, $subnetstr) { + if ($addr == '0.0.0.0') { + return false; + } $subnets = explode(',', $subnetstr); $found = false; $addr = trim($addr); @@ -8813,7 +8816,7 @@ function cleardoubleslashes ($path) { */ function remoteip_in_list($list){ $inlist = false; - $client_ip = getremoteaddr(); + $client_ip = getremoteaddr(null); if(!$client_ip){ // ensure access on cli @@ -8835,9 +8838,10 @@ function remoteip_in_list($list){ * Returns most reliable client address * * @global object + * @param string $default If an address can't be determined, then return this * @return string The remote IP address */ -function getremoteaddr() { +function getremoteaddr($default='0.0.0.0') { global $CFG; if (empty($CFG->getremoteaddrconf)) { @@ -8849,18 +8853,21 @@ function getremoteaddr() { } if (!($variablestoskip & GETREMOTEADDR_SKIP_HTTP_CLIENT_IP)) { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { - return cleanremoteaddr($_SERVER['HTTP_CLIENT_IP']); + $address = cleanremoteaddr($_SERVER['HTTP_CLIENT_IP']); + return $address ? $address : $default; } } if (!($variablestoskip & GETREMOTEADDR_SKIP_HTTP_X_FORWARDED_FOR)) { if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { - return cleanremoteaddr($_SERVER['HTTP_X_FORWARDED_FOR']); + $address = cleanremoteaddr($_SERVER['HTTP_X_FORWARDED_FOR']); + return $address ? $address : $default; } } if (!empty($_SERVER['REMOTE_ADDR'])) { - return cleanremoteaddr($_SERVER['REMOTE_ADDR']); + $address = cleanremoteaddr($_SERVER['REMOTE_ADDR']); + return $address ? $address : $default; } else { - return null; + return $default; } } diff --git a/mod/chat/lib.php b/mod/chat/lib.php index ed9b6ced47c..0409dbb6f0b 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -601,9 +601,6 @@ function chat_login_user($chatid, $version, $groupid, $course) { // or provide a dummy value for the db if (empty($chatuser->ip)) { $chatuser->ip = getremoteaddr(); - if (empty($chatuser->ip)) { - $chatuser->ip = ''; - } } if (($chatuser->course != $course->id) or ($chatuser->userid != $USER->id)) { @@ -628,9 +625,6 @@ function chat_login_user($chatid, $version, $groupid, $course) { // or provide a dummy value for the db if (empty($chatuser->ip)) { $chatuser->ip = getremoteaddr(); - if (empty($chatuser->ip)) { - $chatuser->ip = ''; - } } diff --git a/mod/quiz/simpletest/testaccessrules.php b/mod/quiz/simpletest/testaccessrules.php index 12448defa12..dbcb5d22eda 100644 --- a/mod/quiz/simpletest/testaccessrules.php +++ b/mod/quiz/simpletest/testaccessrules.php @@ -50,7 +50,7 @@ class simple_rules_test extends UnitTestCase { // Test the allowed case by getting the user's IP address. However, this // does not always work, for example using the mac install package on my laptop. - $quiz->subnet = getremoteaddr(); + $quiz->subnet = getremoteaddr(null); if (!empty($quiz->subnet)) { $quiz->questions = ''; $quizobj = new quiz($quiz, $cm, null);