From a811f86acbedb306983e450d3bff3fc32387813d Mon Sep 17 00:00:00 2001 From: Fabian Martin Date: Thu, 15 Feb 2024 16:09:01 +0100 Subject: [PATCH] [FEATURE] Introduce proxy support. Resolves issue #51 --- Classes/Utility/LocateUtility.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Classes/Utility/LocateUtility.php b/Classes/Utility/LocateUtility.php index da13f60..815a7d3 100644 --- a/Classes/Utility/LocateUtility.php +++ b/Classes/Utility/LocateUtility.php @@ -44,11 +44,26 @@ public function getCountryIso2FromIP(?string $ip = null): bool|string public function getNumericIp(?string $ip = null): string|bool { - $ip = $ip ?? (string)GeneralUtility::getIndpEnv('REMOTE_ADDR'); + $ip = $ip ?? $this->getRemoteAddress(); return str_contains($ip, '.') ? (string)ip2long($ip) : $this->convertIpv6($ip); } + protected function getRemoteAddress(): ?string + { + $remoteAddr = $this->getHeader('X-Forwarded-For'); + if (!empty($remoteAddr)) { + return $remoteAddr; + } + return (string)GeneralUtility::getIndpEnv('REMOTE_ADDR'); + } + + protected function getHeader(string $headerName): ?string + { + $headers = getallheaders(); + return $headers[$headerName] ?? null; + } + private function convertIpv6(string $ip): string|bool { $ip = inet_pton($ip);