|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Http\Aggregator; |
|
26
|
|
|
|
|
27
|
|
|
use Brickoo\Component\Http\HttpMessageHeader; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* ClientIpAggregator |
|
31
|
|
|
* |
|
32
|
|
|
* Implements a client ip aggregator. |
|
33
|
|
|
* @author Celestino Diaz <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
class ClientIpAggregator { |
|
36
|
|
|
|
|
37
|
|
|
/** @var \Brickoo\Component\Http\HttpMessageHeader */ |
|
38
|
|
|
private $httpHeader; |
|
39
|
|
|
|
|
40
|
|
|
/** @var array */ |
|
41
|
|
|
private $proxyServers; |
|
42
|
|
|
|
|
43
|
|
|
/** @var array */ |
|
44
|
|
|
private $serverVars; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Class constructor. |
|
48
|
|
|
* @param \Brickoo\Component\Http\HttpMessageHeader $headers |
|
49
|
|
|
* @param array $serverVars the server variables |
|
50
|
|
|
* @param array $proxyServers the proxies to recognize |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function __construct(HttpMessageHeader $headers, array $serverVars = [], array $proxyServers = []) { |
|
53
|
1 |
|
$this->httpHeader = $headers; |
|
54
|
1 |
|
$this->serverVars = $serverVars; |
|
55
|
1 |
|
$this->proxyServers = $proxyServers; |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns the client ip address. |
|
60
|
|
|
* @return null|string |
|
61
|
|
|
*/ |
|
62
|
4 |
|
public function getClientIp() { |
|
63
|
4 |
|
if (($remoteAddress = $this->getServerVar("REMOTE_ADDR")) !== null |
|
64
|
4 |
|
&& (!in_array($remoteAddress, $this->proxyServers))) { |
|
65
|
1 |
|
return $remoteAddress; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
3 |
|
if ($originalClientIp = $this->getOriginalClientIp()) { |
|
69
|
2 |
|
return $originalClientIp; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Return the original client ip. |
|
77
|
|
|
* @return null|string |
|
78
|
|
|
*/ |
|
79
|
3 |
|
private function getOriginalClientIp() { |
|
80
|
3 |
|
if (($forwardedIp = $this->getForwardedClientIp()) !== null) { |
|
81
|
1 |
|
return $forwardedIp; |
|
82
|
|
|
} |
|
83
|
2 |
|
return $this->getClientIpFromHeaderField(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Return the client ip from the message header fields. |
|
88
|
|
|
* @return null|string |
|
89
|
|
|
*/ |
|
90
|
2 |
|
private function getClientIpFromHeaderField() { |
|
91
|
2 |
|
if ($this->httpHeader->contains("Client-Ip") |
|
92
|
2 |
|
&& ($headerClientIp = $this->httpHeader->getField("Client-Ip")->getValue()) |
|
93
|
2 |
|
&& filter_var($headerClientIp, FILTER_VALIDATE_IP)) { |
|
94
|
1 |
|
return $headerClientIp; |
|
95
|
|
|
} |
|
96
|
1 |
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Return the forwarded client ip. |
|
101
|
|
|
* @return null|string |
|
102
|
|
|
*/ |
|
103
|
3 |
|
private function getForwardedClientIp() { |
|
104
|
3 |
|
$clientIp = null; |
|
105
|
|
|
|
|
106
|
3 |
|
if ($this->httpHeader->contains("X-Forwarded-For") |
|
107
|
3 |
|
&& ($forwardedIps = $this->httpHeader->getField("X-Forwarded-For")->getValue())) { |
|
108
|
|
|
|
|
109
|
1 |
|
$forwardedIps = array_filter( |
|
110
|
1 |
|
preg_split("/[\\s]*,[\\s]*/", $forwardedIps), |
|
111
|
1 |
|
function($ipToValidate) { |
|
112
|
1 |
|
return filter_var($ipToValidate, FILTER_VALIDATE_IP); |
|
113
|
|
|
} |
|
114
|
1 |
|
); |
|
115
|
|
|
|
|
116
|
1 |
|
if (!empty($forwardedIps)) { |
|
117
|
1 |
|
$clientIp = array_shift($forwardedIps); |
|
118
|
1 |
|
} |
|
119
|
1 |
|
} |
|
120
|
|
|
|
|
121
|
3 |
|
return $clientIp; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Return the server variable value |
|
126
|
|
|
* or the default value if not available. |
|
127
|
|
|
* @param string $key |
|
128
|
|
|
* @param mixed $defaultValue |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
4 |
|
private function getServerVar($key, $defaultValue = null) { |
|
132
|
4 |
|
if (isset($this->serverVars[$key])) { |
|
133
|
2 |
|
return $this->serverVars[$key]; |
|
134
|
|
|
} |
|
135
|
2 |
|
return $defaultValue; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|