| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles retrieval of client IP addresses from HTTP headers. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component\Http\Remote_Address |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component\Http\Remote_Address; |
| 9 |
|
| 10 |
use WP_Defender\Traits\IP; |
| 11 |
|
| 12 |
/** |
| 13 |
* Older way of getting the client/Remote IP address on HTTP request. |
| 14 |
*/ |
| 15 |
class Classic_Remote_Address { |
| 16 |
|
| 17 |
use IP; |
| 18 |
|
| 19 |
/** |
| 20 |
* List of HTTP headers that may contain the client IP address. |
| 21 |
* |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
private $accepted_header = array( |
| 25 |
'HTTP_CLIENT_IP', |
| 26 |
'HTTP_X_REAL_IP', |
| 27 |
'HTTP_X_FORWARDED_FOR', |
| 28 |
'HTTP_X_FORWARDED', |
| 29 |
'HTTP_X_CLUSTER_CLIENT_IP', |
| 30 |
'HTTP_FORWARDED_FOR', |
| 31 |
'HTTP_FORWARDED', |
| 32 |
'HTTP_CF_CONNECTING_IP', |
| 33 |
'REMOTE_ADDR', |
| 34 |
); |
| 35 |
|
| 36 |
/** |
| 37 |
* Returns client IP addresses. |
| 38 |
* |
| 39 |
* @return array IP addresses |
| 40 |
*/ |
| 41 |
public function get_ip_address(): array { |
| 42 |
$ip_list = array(); |
| 43 |
$server = defender_get_data_from_request( null, 's' ); |
| 44 |
foreach ( $this->accepted_header as $key ) { |
| 45 |
if ( array_key_exists( $key, $server ) && is_string( $server[ $key ] ) && '' !== $server[ $key ] ) { |
| 46 |
$ip_array = explode( ',', $server[ $key ] ); |
| 47 |
foreach ( $ip_array as $ip ) { |
| 48 |
$ip = trim( $ip ); |
| 49 |
if ( $this->check_validate_ip( $ip ) ) { |
| 50 |
$ip_list[] = $ip; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return $ip_list; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Return all the headers found. |
| 61 |
* |
| 62 |
* @return array Header(s) key/name. |
| 63 |
*/ |
| 64 |
public function get_ip_header(): array { |
| 65 |
$header_array = array(); |
| 66 |
$server = defender_get_data_from_request( null, 's' ); |
| 67 |
foreach ( $this->accepted_header as $key ) { |
| 68 |
if ( array_key_exists( $key, $server ) && is_string( $server[ $key ] ) && '' !== $server[ $key ] ) { |
| 69 |
$ip_array = explode( ',', $server[ $key ] ); |
| 70 |
foreach ( $ip_array as $ip ) { |
| 71 |
$ip = trim( $ip ); |
| 72 |
if ( $this->check_validate_ip( $ip ) ) { |
| 73 |
$header_array[] = $key; |
| 74 |
break; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return $header_array; |
| 81 |
} |
| 82 |
} |
| 83 |
|