diff --git a/system/HTTP/RequestTrait.php b/system/HTTP/RequestTrait.php index 418da59865b2..cec53f9b3b0b 100644 --- a/system/HTTP/RequestTrait.php +++ b/system/HTTP/RequestTrait.php @@ -78,76 +78,74 @@ public function getIPAddress(): string return $this->ipAddress = '0.0.0.0'; } - if ($proxyIPs) { - // @TODO Extract all this IP address logic to another class. - foreach ($proxyIPs as $proxyIP => $header) { - // Check if we have an IP address or a subnet - if (strpos($proxyIP, '/') === false) { - // An IP address (and not a subnet) is specified. - // We can compare right away. - if ($proxyIP === $this->ipAddress) { - $spoof = $this->getClientIP($header); - - if ($spoof !== null) { - $this->ipAddress = $spoof; - break; - } - } + // @TODO Extract all this IP address logic to another class. + foreach ($proxyIPs as $proxyIP => $header) { + // Check if we have an IP address or a subnet + if (strpos($proxyIP, '/') === false) { + // An IP address (and not a subnet) is specified. + // We can compare right away. + if ($proxyIP === $this->ipAddress) { + $spoof = $this->getClientIP($header); - continue; + if ($spoof !== null) { + $this->ipAddress = $spoof; + break; + } } - // We have a subnet ... now the heavy lifting begins - if (! isset($separator)) { - $separator = $ipValidator($this->ipAddress, 'ipv6') ? ':' : '.'; - } + continue; + } - // If the proxy entry doesn't match the IP protocol - skip it - if (strpos($proxyIP, $separator) === false) { - continue; - } + // We have a subnet ... now the heavy lifting begins + if (! isset($separator)) { + $separator = $ipValidator($this->ipAddress, 'ipv6') ? ':' : '.'; + } - // Convert the REMOTE_ADDR IP address to binary, if needed - if (! isset($ip, $sprintf)) { - if ($separator === ':') { - // Make sure we're having the "full" IPv6 format - $ip = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($this->ipAddress, ':')), $this->ipAddress)); + // If the proxy entry doesn't match the IP protocol - skip it + if (strpos($proxyIP, $separator) === false) { + continue; + } - for ($j = 0; $j < 8; $j++) { - $ip[$j] = intval($ip[$j], 16); - } + // Convert the REMOTE_ADDR IP address to binary, if needed + if (! isset($ip, $sprintf)) { + if ($separator === ':') { + // Make sure we're having the "full" IPv6 format + $ip = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($this->ipAddress, ':')), $this->ipAddress)); - $sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b'; - } else { - $ip = explode('.', $this->ipAddress); - $sprintf = '%08b%08b%08b%08b'; + for ($j = 0; $j < 8; $j++) { + $ip[$j] = intval($ip[$j], 16); } - $ip = vsprintf($sprintf, $ip); + $sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b'; + } else { + $ip = explode('.', $this->ipAddress); + $sprintf = '%08b%08b%08b%08b'; } - // Split the netmask length off the network address - sscanf($proxyIP, '%[^/]/%d', $netaddr, $masklen); + $ip = vsprintf($sprintf, $ip); + } - // Again, an IPv6 address is most likely in a compressed form - if ($separator === ':') { - $netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr)); + // Split the netmask length off the network address + sscanf($proxyIP, '%[^/]/%d', $netaddr, $masklen); - for ($i = 0; $i < 8; $i++) { - $netaddr[$i] = intval($netaddr[$i], 16); - } - } else { - $netaddr = explode('.', $netaddr); + // Again, an IPv6 address is most likely in a compressed form + if ($separator === ':') { + $netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr)); + + for ($i = 0; $i < 8; $i++) { + $netaddr[$i] = intval($netaddr[$i], 16); } + } else { + $netaddr = explode('.', $netaddr); + } - // Convert to binary and finally compare - if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0) { - $spoof = $this->getClientIP($header); + // Convert to binary and finally compare + if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0) { + $spoof = $this->getClientIP($header); - if ($spoof !== null) { - $this->ipAddress = $spoof; - break; - } + if ($spoof !== null) { + $this->ipAddress = $spoof; + break; } } }