1 | <?php |
||
44 | class Whip |
||
45 | { |
||
46 | |||
47 | /** Indicates all header methods will be used. */ |
||
48 | const ALL_METHODS = 255; |
||
49 | /** Indicates the REMOTE_ADDR method will be used. */ |
||
50 | const REMOTE_ADDR = 1; |
||
51 | /** Indicates a set of possible proxy headers will be used. */ |
||
52 | const PROXY_HEADERS = 2; |
||
53 | /** Indicates any CloudFlare specific headers will be used. */ |
||
54 | const CLOUDFLARE_HEADERS = 4; |
||
55 | /** Indicates any Incapsula specific headers will be used. */ |
||
56 | const INCAPSULA_HEADERS = 8; |
||
57 | /** Indicates custom listed headers will be used. */ |
||
58 | const CUSTOM_HEADERS = 128; |
||
59 | |||
60 | /** The array of mapped header strings. */ |
||
61 | private static $headers = array( |
||
62 | self::CUSTOM_HEADERS => array(), |
||
63 | self::INCAPSULA_HEADERS => array( |
||
64 | 'incap-client-ip' |
||
65 | ), |
||
66 | self::CLOUDFLARE_HEADERS => array( |
||
67 | 'cf-connecting-ip' |
||
68 | ), |
||
69 | self::PROXY_HEADERS => array( |
||
70 | 'client-ip', |
||
71 | 'x-forwarded-for', |
||
72 | 'x-forwarded', |
||
73 | 'x-cluster-client-ip', |
||
74 | 'forwarded-for', |
||
75 | 'forwarded', |
||
76 | 'x-real-ip', |
||
77 | ), |
||
78 | ); |
||
79 | |||
80 | /** the bitmask of enabled methods */ |
||
81 | private $enabled; |
||
82 | |||
83 | /** the array of IP whitelist ranges to check against */ |
||
84 | private $whitelist; |
||
85 | |||
86 | /** |
||
87 | * An object holding the source of addresses we will check |
||
88 | * |
||
89 | * @var RequestAdapter |
||
90 | */ |
||
91 | private $source; |
||
92 | |||
93 | /** |
||
94 | * Constructor for the class. |
||
95 | * @param int $enabled The bitmask of enabled headers. |
||
96 | * @param array $whitelists The array of IP ranges to be whitelisted. |
||
97 | * @param mixed $source A supported source of IP data. |
||
98 | */ |
||
99 | 22 | public function __construct($enabled = self::ALL_METHODS, array $whitelists = array(), $source = null) |
|
100 | { |
||
101 | 22 | $this->enabled = (int) $enabled; |
|
102 | 22 | if (isset($source)) { |
|
103 | 16 | $this->setSource($source); |
|
104 | } |
||
105 | 21 | $this->whitelist = array(); |
|
106 | 21 | foreach ($whitelists as $header => $ipRanges) { |
|
107 | 12 | $header = $this->normalizeHeaderName($header); |
|
108 | 12 | $this->whitelist[$header] = new IpWhitelist($ipRanges); |
|
109 | } |
||
110 | 21 | } |
|
111 | |||
112 | /** |
||
113 | * Adds a custom header to the list. |
||
114 | * @param string $header The custom header to add. |
||
115 | * @return Whip Returns $this. |
||
116 | */ |
||
117 | 1 | public function addCustomHeader($header) |
|
122 | |||
123 | /** |
||
124 | * Sets the source data used to lookup the addresses. |
||
125 | * |
||
126 | * @param $source The source array. |
||
127 | * @return Whip Returns $this. |
||
128 | */ |
||
129 | 20 | public function setSource($source) |
|
135 | |||
136 | /** |
||
137 | * Returns the IP address of the client using the given methods. |
||
138 | * @param mixed $source (optional) The source data. If omitted, the class |
||
139 | * will use the value passed to Whip::setSource or fallback to |
||
140 | * $_SERVER. |
||
141 | * @return string Returns the IP address as a string or false if no |
||
142 | * IP address could be found. |
||
143 | */ |
||
144 | 21 | public function getIpAddress($source = null) |
|
145 | { |
||
146 | 21 | $source = $this->getRequestAdapter($this->coalesceSources($source)); |
|
147 | 21 | $remoteAddr = $source->getRemoteAddr(); |
|
148 | 21 | $requestHeaders = $source->getHeaders(); |
|
149 | |||
150 | 21 | foreach (self::$headers as $key => $headers) { |
|
151 | 21 | if (!$this->isMethodUsable($key, $remoteAddr)) { |
|
152 | 19 | continue; |
|
153 | } |
||
154 | 11 | return $this->extractAddressFromHeaders($requestHeaders, $headers); |
|
155 | } |
||
156 | |||
157 | 10 | return ($this->enabled & self::REMOTE_ADDR) ? $remoteAddr : false; |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * Returns the valid IP address or false if no valid IP address was found. |
||
162 | * @param mixed $source (optional) The source data. If omitted, the class |
||
163 | * will use the value passed to Whip::setSource or fallback to |
||
164 | * $_SERVER. |
||
165 | * @return string|false Returns the IP address (as a string) of the client or false |
||
166 | * if no valid IP address was found. |
||
167 | */ |
||
168 | 4 | public function getValidIpAddress($source = null) |
|
176 | |||
177 | /** |
||
178 | * Normalizes HTTP header name representations. |
||
179 | * |
||
180 | * HTTP_MY_HEADER and My-Header would be transformed to my-header. |
||
181 | * |
||
182 | * @param string $header The original header name. |
||
183 | * @return string The normalized header name. |
||
184 | */ |
||
185 | 12 | private function normalizeHeaderName($header) |
|
186 | { |
||
187 | 12 | if (strpos($header, 'HTTP_') === 0) { |
|
188 | 1 | $header = str_replace('_', '-', substr($header, 5)); |
|
189 | } |
||
190 | 12 | return strtolower($header); |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Finds the first element in $headers that is present in $_SERVER and |
||
195 | * returns the IP address mapped to that value. |
||
196 | * If the IP address is a list of comma separated values, the last value |
||
197 | * in the list will be returned. |
||
198 | * If no IP address is found, we return false. |
||
199 | * @param array $requestHeaders The request headers to pull data from. |
||
200 | * @param array $headers The list of headers to check. |
||
201 | * @return string|false Returns the IP address as a string or false if no IP |
||
202 | * IP address was found. |
||
203 | */ |
||
204 | 11 | private function extractAddressFromHeaders($requestHeaders, $headers) |
|
205 | { |
||
206 | 11 | foreach ($headers as $header) { |
|
207 | 10 | if (!empty($requestHeaders[$header])) { |
|
208 | 9 | $list = explode(',', $requestHeaders[$header]); |
|
209 | 10 | return trim(end($list)); |
|
210 | } |
||
211 | } |
||
212 | 2 | return false; |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * Returns whether or not the given method is enabled and usable. |
||
217 | * |
||
218 | * This method checks if the method is enabled and whether the method's data |
||
219 | * is usable given it's IP whitelist. |
||
220 | * |
||
221 | * @param string $key The source key. |
||
222 | * @param string $ipAddress The IP address. |
||
223 | * @return boolean Returns true if the IP address is whitelisted and false |
||
224 | * otherwise. Returns true if the source does not have a whitelist |
||
225 | * specified. |
||
226 | */ |
||
227 | 21 | private function isMethodUsable($key, $ipAddress) |
|
237 | |||
238 | /** |
||
239 | * Get a source/request adapter for a given source of IP data. |
||
240 | * |
||
241 | * @param mixed $source A supported source of request data. |
||
242 | * @return RequestAdapter A RequestAdapter implementation for the given source. |
||
243 | */ |
||
244 | 22 | private function getRequestAdapter($source) |
|
256 | |||
257 | /** |
||
258 | * Given available sources, get the first available source of IP data. |
||
259 | * |
||
260 | * @param mixed $source A source data argument, if available. |
||
261 | * @return mixed The best available source, after fallbacks. |
||
262 | */ |
||
263 | 21 | private function coalesceSources($source = null) |
|
273 | } |
||
274 |
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: