Skip to content

Commit 70e3162

Browse files
author
Daniel Neto
committed
Refactor IP address retrieval functions for improved clarity and validation
1 parent 2c02025 commit 70e3162

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

objects/Encoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ public static function run($try = 0)
13141314

13151315
// Create the lock file with the current time
13161316
file_put_contents($lockFile, time());
1317-
_error_log("Encoder::run: Lock file created $lockFile");
1317+
//_error_log("Encoder::run: Lock file created $lockFile");
13181318

13191319
if ($try > $maxTries) {
13201320
_error_log("Encoder::run: Lock file deleted maxTries $lockFile");
@@ -1505,7 +1505,7 @@ public static function run($try = 0)
15051505
$msg .= (count($rows) == 1) ? " is encoding" : " are encoding";
15061506
$obj->msg = $msg;
15071507
}
1508-
_error_log("Encoder::run: Lock file deleted $lockFile");
1508+
//_error_log("Encoder::run: Lock file deleted $lockFile");
15091509
// Remove the lock file before returning
15101510
unlink($lockFile);
15111511
return $obj;

objects/functions.php

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,16 +386,72 @@ function base64DataToImage($imgBase64)
386386
return base64_decode($img);
387387
}
388388

389+
function getRemoteAddrFromServerArray($server)
390+
{
391+
if (empty($server['REMOTE_ADDR'])) {
392+
return '';
393+
}
394+
395+
$remoteAddr = trim($server['REMOTE_ADDR']);
396+
if (!filter_var($remoteAddr, FILTER_VALIDATE_IP)) {
397+
return '';
398+
}
399+
400+
return $remoteAddr;
401+
}
402+
403+
function isPrivateOrLoopbackIP($ip)
404+
{
405+
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
406+
return false;
407+
}
408+
409+
return !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
410+
}
411+
412+
function getForwardedClientIpFromServerArray($server)
413+
{
414+
$ipv6 = '';
415+
$headers = [
416+
'HTTP_X_REAL_IP',
417+
'HTTP_X_FORWARDED_FOR',
418+
];
419+
420+
foreach ($headers as $header) {
421+
if (empty($server[$header])) {
422+
continue;
423+
}
424+
425+
$ips = explode(',', $server[$header]);
426+
foreach ($ips as $ipCandidate) {
427+
$ipCandidate = trim($ipCandidate);
428+
if (filter_var($ipCandidate, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
429+
return $ipCandidate;
430+
}
431+
if (empty($ipv6) && filter_var($ipCandidate, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
432+
$ipv6 = $ipCandidate;
433+
}
434+
}
435+
}
436+
437+
return $ipv6;
438+
}
439+
389440
function getRealIpAddr()
390441
{
391-
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { //check ip from share internet
392-
$ip = $_SERVER['HTTP_CLIENT_IP'];
393-
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy
394-
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
395-
} else {
396-
$ip = $_SERVER['REMOTE_ADDR'];
442+
$remoteAddr = getRemoteAddrFromServerArray($_SERVER);
443+
if (empty($remoteAddr)) {
444+
return '127.0.0.1';
397445
}
398-
return $ip;
446+
447+
if (isPrivateOrLoopbackIP($remoteAddr)) {
448+
$forwardedIp = getForwardedClientIpFromServerArray($_SERVER);
449+
if (!empty($forwardedIp)) {
450+
return $forwardedIp;
451+
}
452+
}
453+
454+
return $remoteAddr;
399455
}
400456

401457
function cleanString($text)

0 commit comments

Comments
 (0)