1 | <?php |
||
44 | class Whip |
||
45 | { |
||
46 | /** The whitelist key for IPv4 addresses */ |
||
47 | const IPV4 = IpWhitelist::IPV4; |
||
48 | |||
49 | /** The whitelist key for IPv6 addresses */ |
||
50 | const IPV6 = IpWhitelist::IPV6; |
||
51 | |||
52 | /** Indicates all header methods will be used. */ |
||
53 | const ALL_METHODS = 255; |
||
54 | /** Indicates the REMOTE_ADDR method will be used. */ |
||
55 | const REMOTE_ADDR = 1; |
||
56 | /** Indicates a set of possible proxy headers will be used. */ |
||
57 | const PROXY_HEADERS = 2; |
||
58 | /** Indicates any CloudFlare specific headers will be used. */ |
||
59 | const CLOUDFLARE_HEADERS = 4; |
||
60 | /** Indicates any Incapsula specific headers will be used. */ |
||
61 | const INCAPSULA_HEADERS = 8; |
||
62 | /** Indicates custom listed headers will be used. */ |
||
63 | const CUSTOM_HEADERS = 128; |
||
64 | |||
65 | /** The array of mapped header strings. */ |
||
66 | private static $headers = array( |
||
67 | self::CUSTOM_HEADERS => array(), |
||
68 | self::INCAPSULA_HEADERS => array( |
||
69 | 'incap-client-ip' |
||
70 | ), |
||
71 | self::CLOUDFLARE_HEADERS => array( |
||
72 | 'cf-connecting-ip' |
||
73 | ), |
||
74 | self::PROXY_HEADERS => array( |
||
75 | 'client-ip', |
||
76 | 'x-forwarded-for', |
||
77 | 'x-forwarded', |
||
78 | 'x-cluster-client-ip', |
||
79 | 'forwarded-for', |
||
80 | 'forwarded', |
||
81 | 'x-real-ip', |
||
82 | ), |
||
83 | ); |
||
84 | |||
85 | /** the bitmask of enabled methods */ |
||
86 | private $enabled; |
||
87 | |||
88 | /** the array of IP whitelist ranges to check against */ |
||
89 | private $whitelist; |
||
90 | |||
91 | /** |
||
92 | * An object holding the source of addresses we will check |
||
93 | * |
||
94 | * @var RequestAdapter |
||
95 | */ |
||
96 | private $source; |
||
97 | |||
98 | /** |
||
99 | * Constructor for the class. |
||
100 | * @param int $enabled The bitmask of enabled headers. |
||
101 | * @param array $whitelists The array of IP ranges to be whitelisted. |
||
102 | * @param mixed $source A supported source of IP data. |
||
103 | */ |
||
104 | 23 | public function __construct($enabled = self::ALL_METHODS, array $whitelists = array(), $source = null) |
|
105 | { |
||
106 | 23 | $this->enabled = (int) $enabled; |
|
107 | 23 | if (isset($source)) { |
|
108 | 17 | $this->setSource($source); |
|
109 | } |
||
110 | 22 | $this->whitelist = array(); |
|
111 | 22 | foreach ($whitelists as $header => $ipRanges) { |
|
112 | 12 | $header = $this->normalizeHeaderName($header); |
|
113 | 12 | $this->whitelist[$header] = new IpWhitelist($ipRanges); |
|
114 | } |
||
115 | 22 | } |
|
116 | |||
117 | /** |
||
118 | * Adds a custom header to the list. |
||
119 | * @param string $header The custom header to add. |
||
120 | * @return Whip Returns $this. |
||
121 | */ |
||
122 | 1 | public function addCustomHeader($header) |
|
127 | |||
128 | /** |
||
129 | * Sets the source data used to lookup the addresses. |
||
130 | * |
||
131 | * @param $source The source array. |
||
132 | * @return Whip Returns $this. |
||
133 | */ |
||
134 | 21 | public function setSource($source) |
|
140 | |||
141 | /** |
||
142 | * Returns the IP address of the client using the given methods. |
||
143 | * @param mixed $source (optional) The source data. If omitted, the class |
||
144 | * will use the value passed to Whip::setSource or fallback to |
||
145 | * $_SERVER. |
||
146 | * @return string Returns the IP address as a string or false if no |
||
147 | * IP address could be found. |
||
148 | */ |
||
149 | 22 | public function getIpAddress($source = null) |
|
150 | { |
||
151 | 22 | $source = $this->getRequestAdapter($this->coalesceSources($source)); |
|
152 | 22 | $remoteAddr = $source->getRemoteAddr(); |
|
153 | 22 | $requestHeaders = $source->getHeaders(); |
|
154 | |||
155 | 22 | foreach (self::$headers as $key => $headers) { |
|
156 | 22 | if (!$this->isMethodUsable($key, $remoteAddr)) { |
|
157 | 20 | continue; |
|
158 | } |
||
159 | |||
160 | 12 | if ($ipAddress = $this->extractAddressFromHeaders($requestHeaders, $headers)) { |
|
161 | 12 | return $ipAddress; |
|
162 | } |
||
163 | } |
||
164 | |||
165 | 13 | if ($remoteAddr && ($this->enabled & self::REMOTE_ADDR)) { |
|
166 | 7 | return $remoteAddr; |
|
167 | } |
||
168 | |||
169 | 6 | return false; |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * Returns the valid IP address or false if no valid IP address was found. |
||
174 | * @param mixed $source (optional) The source data. If omitted, the class |
||
175 | * will use the value passed to Whip::setSource or fallback to |
||
176 | * $_SERVER. |
||
177 | * @return string|false Returns the IP address (as a string) of the client or false |
||
178 | * if no valid IP address was found. |
||
179 | */ |
||
180 | 4 | public function getValidIpAddress($source = null) |
|
188 | |||
189 | /** |
||
190 | * Normalizes HTTP header name representations. |
||
191 | * |
||
192 | * HTTP_MY_HEADER and My-Header would be transformed to my-header. |
||
193 | * |
||
194 | * @param string $header The original header name. |
||
195 | * @return string The normalized header name. |
||
196 | */ |
||
197 | 12 | private function normalizeHeaderName($header) |
|
198 | { |
||
199 | 12 | if (strpos($header, 'HTTP_') === 0) { |
|
200 | 1 | $header = str_replace('_', '-', substr($header, 5)); |
|
201 | } |
||
202 | 12 | return strtolower($header); |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * Finds the first element in $headers that is present in $_SERVER and |
||
207 | * returns the IP address mapped to that value. |
||
208 | * If the IP address is a list of comma separated values, the last value |
||
209 | * in the list will be returned. |
||
210 | * If no IP address is found, we return false. |
||
211 | * @param array $requestHeaders The request headers to pull data from. |
||
212 | * @param array $headers The list of headers to check. |
||
213 | * @return string|false Returns the IP address as a string or false if no IP |
||
214 | * IP address was found. |
||
215 | */ |
||
216 | 12 | private function extractAddressFromHeaders($requestHeaders, $headers) |
|
217 | { |
||
218 | 12 | foreach ($headers as $header) { |
|
219 | 12 | if (!empty($requestHeaders[$header])) { |
|
220 | 9 | $list = explode(',', $requestHeaders[$header]); |
|
221 | 12 | return trim(end($list)); |
|
222 | } |
||
223 | } |
||
224 | 3 | return false; |
|
225 | } |
||
226 | |||
227 | /** |
||
228 | * Returns whether or not the given method is enabled and usable. |
||
229 | * |
||
230 | * This method checks if the method is enabled and whether the method's data |
||
231 | * is usable given it's IP whitelist. |
||
232 | * |
||
233 | * @param string $key The source key. |
||
234 | * @param string $ipAddress The IP address. |
||
235 | * @return boolean Returns true if the IP address is whitelisted and false |
||
236 | * otherwise. Returns true if the source does not have a whitelist |
||
237 | * specified. |
||
238 | */ |
||
239 | 22 | private function isMethodUsable($key, $ipAddress) |
|
249 | |||
250 | /** |
||
251 | * Get a source/request adapter for a given source of IP data. |
||
252 | * |
||
253 | * @param mixed $source A supported source of request data. |
||
254 | * @return RequestAdapter A RequestAdapter implementation for the given source. |
||
255 | */ |
||
256 | 23 | private function getRequestAdapter($source) |
|
268 | |||
269 | /** |
||
270 | * Given available sources, get the first available source of IP data. |
||
271 | * |
||
272 | * @param mixed $source A source data argument, if available. |
||
273 | * @return mixed The best available source, after fallbacks. |
||
274 | */ |
||
275 | 22 | private function coalesceSources($source = null) |
|
285 | } |
||
286 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: