Merge pull request #1222 from itflow-org/functions-ip-addr-leftmost

Update how functions.php gets the remote IP address for logging
This commit is contained in:
Johnny
2025-06-11 14:01:12 -04:00
committed by GitHub

View File

@@ -77,17 +77,21 @@ function getUserAgent() {
} }
function getIP() { function getIP() {
if (defined("CONST_GET_IP_METHOD")) {
if (CONST_GET_IP_METHOD == "HTTP_X_FORWARDED_FOR") { // Default way to get IP
$ip = getenv('HTTP_X_FORWARDED_FOR'); $ip = $_SERVER['REMOTE_ADDR'];
} else {
$ip = $_SERVER["HTTP_CF_CONNECTING_IP"] ?? $_SERVER['REMOTE_ADDR']; // Allow overrides via config.php in-case we use a proxy - https://docs.itflow.org/config_php
} if (defined("CONST_GET_IP_METHOD") && CONST_GET_IP_METHOD == "HTTP_X_FORWARDED_FOR") {
} else { $ip = explode(',', getenv('HTTP_X_FORWARDED_FOR'))[0] ?? $_SERVER['REMOTE_ADDR'];
} elseif (defined("CONST_GET_IP_METHOD") && CONST_GET_IP_METHOD == "HTTP_CF_CONNECTING_IP") {
$ip = $_SERVER["HTTP_CF_CONNECTING_IP"] ?? $_SERVER['REMOTE_ADDR']; $ip = $_SERVER["HTTP_CF_CONNECTING_IP"] ?? $_SERVER['REMOTE_ADDR'];
} }
// Abort if something isn't right
if (!filter_var($ip, FILTER_VALIDATE_IP)) { if (!filter_var($ip, FILTER_VALIDATE_IP)) {
error_log("ITFlow - Could not validate remote IP address");
error_log("ITFlow - IP was [$ip] using method " . CONST_GET_IP_METHOD);
exit("Potential Security Violation"); exit("Potential Security Violation");
} }