1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\HTTP\PhpEnvironment;
use Magento\Framework\App\RequestInterface;
/**
* Library for working with client ip address.
*/
class RemoteAddress
{
/**
* Request object.
*
* @var RequestInterface
*/
protected $request;
/**
* Remote address cache.
*
* @var string
*/
protected $remoteAddress;
/**
* @var array
*/
protected $alternativeHeaders;
/**
* @var string[]|null
*/
private $trustedProxies;
/**
* @param RequestInterface $httpRequest
* @param array $alternativeHeaders
* @param string[]|null $trustedProxies
*/
public function __construct(
RequestInterface $httpRequest,
array $alternativeHeaders = [],
array $trustedProxies = null
) {
$this->request = $httpRequest;
$this->alternativeHeaders = $alternativeHeaders;
$this->trustedProxies = $trustedProxies;
}
/**
* Read address based on settings.
*
* @return string|null
*/
private function readAddress()
{
$remoteAddress = null;
foreach ($this->alternativeHeaders as $var) {
if ($this->request->getServer($var, false)) {
$remoteAddress = $this->request->getServer($var);
break;
}
}
if (!$remoteAddress) {
$remoteAddress = $this->request->getServer('REMOTE_ADDR');
}
return $remoteAddress;
}
/**
* Filter addresses by trusted proxies list.
*
* @param string $remoteAddress
* @return string|null
*/
private function filterAddress(string $remoteAddress)
{
if (strpos($remoteAddress, ',') !== false) {
$ipList = explode(',', $remoteAddress);
} else {
$ipList = [$remoteAddress];
}
$ipList = array_filter(
$ipList,
function (string $ip) {
return filter_var(trim($ip), FILTER_VALIDATE_IP);
}
);
if ($this->trustedProxies !== null) {
$ipList = array_filter(
$ipList,
function (string $ip) {
return !in_array(trim($ip), $this->trustedProxies, true);
}
);
$remoteAddress = trim(array_pop($ipList));
} else {
$remoteAddress = trim(reset($ipList));
}
return $remoteAddress ?: null;
}
/**
* Retrieve Client Remote Address.
* If alternative headers are used and said headers allow multiple IPs
* it is suggested that trusted proxies is also used
* for more accurate IP recognition.
*
* @param bool $ipToLong converting IP to long format
*
* @return string IPv4|long
*/
public function getRemoteAddress(bool $ipToLong = false)
{
if ($this->remoteAddress !== null) {
return $this->remoteAddress;
}
$remoteAddress = $this->readAddress();
if (!$remoteAddress) {
$this->remoteAddress = false;
return false;
}
$remoteAddress = $this->filterAddress($remoteAddress);
if (!$remoteAddress) {
$this->remoteAddress = false;
return false;
} else {
$this->remoteAddress = $remoteAddress;
return $ipToLong ? ip2long($this->remoteAddress) : $this->remoteAddress;
}
}
/**
* Returns internet host name corresponding to remote server
*
* @return string|null
*/
public function getRemoteHost()
{
return $this->getRemoteAddress()
? gethostbyaddr($this->getRemoteAddress())
: null;
}
}