-
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Replace manual IP extraction with IpAddressRetriever
- Loading branch information
Showing
6 changed files
with
158 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,28 @@ | ||
<?php | ||
|
||
$client_ip = ''; | ||
/* Poweradmin, a friendly web-based admin tool for PowerDNS. | ||
* See <https://www.poweradmin.org> for more details. | ||
* | ||
* Copyright 2007-2010 Rejo Zenger <rejo@zenger.nl> | ||
* Copyright 2010-2023 Poweradmin Development Team | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { | ||
$client_ip = filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP); | ||
} else if (isset($_SERVER['REMOTE_ADDR'])) { | ||
$client_ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP); | ||
} | ||
require dirname(__DIR__) . '/vendor/autoload.php'; | ||
|
||
echo htmlspecialchars($client_ip); | ||
use Poweradmin\Infrastructure\Web\IpAddressRetriever; | ||
|
||
$ipAddressRetriever = new IpAddressRetriever($_SERVER); | ||
echo htmlspecialchars($ipAddressRetriever->getClientIp()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Poweradmin\Infrastructure\Web; | ||
|
||
/* Poweradmin, a friendly web-based admin tool for PowerDNS. | ||
* See <https://www.poweradmin.org> for more details. | ||
* | ||
* Copyright 2007-2010 Rejo Zenger <rejo@zenger.nl> | ||
* Copyright 2010-2023 Poweradmin Development Team | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
class IpAddressRetriever | ||
{ | ||
private array $server; | ||
|
||
public function __construct(array $server) { | ||
$this->server = $server; | ||
} | ||
|
||
/** | ||
* Get the client IP address. | ||
* | ||
* @return string | ||
*/ | ||
function getClientIp(): string | ||
{ | ||
$clientIpHeaders = [ | ||
'HTTP_CLIENT_IP', | ||
'HTTP_X_FORWARDED_FOR', | ||
'REMOTE_ADDR' | ||
]; | ||
|
||
foreach ($clientIpHeaders as $header) { | ||
if (!empty($this->server[$header])) { | ||
$ips = array_filter(explode(',', $this->server[$header]), function($ip) { | ||
return filter_var($ip, FILTER_VALIDATE_IP); | ||
}); | ||
|
||
return $ips[0] ?? ''; | ||
} | ||
} | ||
|
||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Infrastructure\Web; | ||
|
||
use Poweradmin\Infrastructure\Web\IpAddressRetriever; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class IpAddressRetrieverTest extends TestCase | ||
{ | ||
public function testGetClientIpWithHttpClientIp() | ||
{ | ||
$server = ['HTTP_CLIENT_IP' => '192.168.1.1']; | ||
$ipRetriever = new IpAddressRetriever($server); | ||
$this->assertEquals('192.168.1.1', $ipRetriever->getClientIp()); | ||
} | ||
|
||
public function testGetClientIpWithHttpClientIp6() | ||
{ | ||
$server = ['HTTP_CLIENT_IP' => '2001:db8:85a3:8d3:1319:8a2e:370:7348']; | ||
$ipRetriever = new IpAddressRetriever($server); | ||
$this->assertEquals('2001:db8:85a3:8d3:1319:8a2e:370:7348', $ipRetriever->getClientIp()); | ||
} | ||
|
||
public function testGetClientIpWithMultipleIpsFirstValid() | ||
{ | ||
$server = ['HTTP_X_FORWARDED_FOR' => '192.168.1.2, 10.0.0.1']; | ||
$ipRetriever = new IpAddressRetriever($server); | ||
$this->assertEquals('192.168.1.2', $ipRetriever->getClientIp()); | ||
} | ||
|
||
public function testGetClientIpWithMultipleIpsFirstInvalid() | ||
{ | ||
$server = ['HTTP_X_FORWARDED_FOR' => 'invalid_ip, 192.168.1.3']; | ||
$ipRetriever = new IpAddressRetriever($server); | ||
$this->assertEquals('', $ipRetriever->getClientIp()); | ||
} | ||
|
||
public function testGetClientIpWithAllInvalidIps() | ||
{ | ||
$server = ['HTTP_X_FORWARDED_FOR' => 'invalid_ip1, invalid_ip2']; | ||
$ipRetriever = new IpAddressRetriever($server); | ||
$this->assertEquals('', $ipRetriever->getClientIp()); | ||
} | ||
|
||
public function testGetClientIpWithHttpXForwardedFor() | ||
{ | ||
$server = ['HTTP_X_FORWARDED_FOR' => '192.168.1.2']; | ||
$ipRetriever = new IpAddressRetriever($server); | ||
$this->assertEquals('192.168.1.2', $ipRetriever->getClientIp()); | ||
} | ||
|
||
public function testGetClientIpWithRemoteAddr() | ||
{ | ||
$server = ['REMOTE_ADDR' => '192.168.1.3']; | ||
$ipRetriever = new IpAddressRetriever($server); | ||
$this->assertEquals('192.168.1.3', $ipRetriever->getClientIp()); | ||
} | ||
|
||
public function testGetClientIpWithInvalidIp() | ||
{ | ||
$server = ['HTTP_CLIENT_IP' => 'invalid_ip']; | ||
$ipRetriever = new IpAddressRetriever($server); | ||
$this->assertEquals('', $ipRetriever->getClientIp()); | ||
} | ||
|
||
public function testGetClientIpWithNoIp() | ||
{ | ||
$server = []; | ||
$ipRetriever = new IpAddressRetriever($server); | ||
$this->assertEquals('', $ipRetriever->getClientIp()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters