Skip to content

Commit b740506

Browse files
committed
Allow defining the HTTP headers that contain the user IP address
1 parent 9792bd0 commit b740506

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

tinyfilemanager.php

+20-8
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@
127127
// OR => Connection must be on the whitelist, or not on the blacklist
128128
$ip_ruleset = 'OFF';
129129

130+
// List of HTTP headers that may contain the real IP address of the user
131+
$ip_http_headers = array(
132+
'HTTP_CF_CONNECTING_IP',
133+
'HTTP_X_FORWARDED_FOR',
134+
'REMOTE_ADDR',
135+
'HTTP_CLIENT_IP',
136+
);
137+
130138
// Should users be notified of their block?
131139
$ip_silent = true;
132140

@@ -149,6 +157,8 @@
149157
@include($config_file);
150158
}
151159

160+
defined('FM_IP_HTTP_HEADERS') || define('FM_IP_HTTP_HEADERS', (version_compare(PHP_VERSION, '7.0.0', '<') ? serialize($ip_http_headers) : $ip_http_headers));
161+
152162
// External CDN resources that can be used in the HTML (replace for GDPR compliance)
153163
$external = array(
154164
'css-bootstrap' => '<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">',
@@ -283,14 +293,16 @@ function session_error_handling_function($code, $msg, $file, $line)
283293
if ($ip_ruleset != 'OFF') {
284294
function getClientIP()
285295
{
286-
if (array_key_exists('HTTP_CF_CONNECTING_IP', $_SERVER)) {
287-
return $_SERVER["HTTP_CF_CONNECTING_IP"];
288-
} else if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
289-
return $_SERVER["HTTP_X_FORWARDED_FOR"];
290-
} else if (array_key_exists('REMOTE_ADDR', $_SERVER)) {
291-
return $_SERVER['REMOTE_ADDR'];
292-
} else if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) {
293-
return $_SERVER['HTTP_CLIENT_IP'];
296+
$ip_http_headers = FM_IP_HTTP_HEADERS;
297+
if (is_string($ip_http_headers)) {
298+
$ip_http_headers = @unserialize($ip_http_headers);
299+
}
300+
if (is_array($ip_http_headers)) {
301+
foreach ($ip_http_headers as $header) {
302+
if (array_key_exists($header, $_SERVER)) {
303+
return $_SERVER[$header];
304+
}
305+
}
294306
}
295307
return '';
296308
}

0 commit comments

Comments
 (0)