| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the determination of the remote IP address based on server settings and configurations. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component\Http |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component\Http; |
| 9 |
|
| 10 |
use WP_Defender\Model\Setting\Firewall; |
| 11 |
use WP_Defender\Component\Http\Remote_Address\Classic_Remote_Address; |
| 12 |
use WP_Defender\Component\Http\Remote_Address\Remote_Address as Modern_Remote_Address; |
| 13 |
|
| 14 |
/** |
| 15 |
* Service layer decides/strategist which instance of remote address class to use. |
| 16 |
*/ |
| 17 |
class Remote_Address { |
| 18 |
|
| 19 |
/** |
| 20 |
* Firewall settings instance. |
| 21 |
* |
| 22 |
* @var Firewall |
| 23 |
*/ |
| 24 |
private $firewall; |
| 25 |
|
| 26 |
/** |
| 27 |
* HTTP IP header name. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
private $http_ip_header = ''; |
| 32 |
|
| 33 |
/** |
| 34 |
* Initializes the Remote_Address service with firewall settings. |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
$this->firewall = wd_di()->get( Firewall::class ); |
| 38 |
$this->http_ip_header = esc_html( $this->firewall->http_ip_header ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Sets the HTTP IP header key. |
| 43 |
* |
| 44 |
* @param string $http_header HTTP header key/name. |
| 45 |
*/ |
| 46 |
public function set_http_ip_header( $http_header ): void { |
| 47 |
$this->http_ip_header = $http_header; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Determines the appropriate Remote_Address instance based on the configured HTTP IP header. |
| 52 |
* |
| 53 |
* @return Remote_Address The appropriate Remote_Address instance. |
| 54 |
*/ |
| 55 |
private function instance() { |
| 56 |
/** |
| 57 |
* Filter the HTTP IP header. |
| 58 |
* |
| 59 |
* @param string $http_ip_header HTTP header for identifying client's IP. |
| 60 |
* |
| 61 |
* @since 4.5.1 |
| 62 |
*/ |
| 63 |
$http_ip_header = apply_filters( 'wpdef_firewall_ip_detection', $this->http_ip_header ); |
| 64 |
$http_ip_header = is_string( $http_ip_header ) ? $http_ip_header : (string) $http_ip_header; |
| 65 |
switch ( $http_ip_header ) { |
| 66 |
case 'HTTP_X_FORWARDED_FOR': |
| 67 |
case 'HTTP_X_REAL_IP': |
| 68 |
case 'HTTP_CF_CONNECTING_IP': |
| 69 |
$remote_address = wd_di()->get( Modern_Remote_Address::class ); |
| 70 |
$remote_address |
| 71 |
->set_use_proxy() |
| 72 |
->set_proxy_header( $http_ip_header ) |
| 73 |
->set_trusted_proxies( $this->firewall->get_trusted_proxies_ip() ) |
| 74 |
->set_trusted_proxy_preset( $this->firewall->get_trusted_proxy_preset() ); |
| 75 |
|
| 76 |
return $remote_address; |
| 77 |
|
| 78 |
case 'REMOTE_ADDR': |
| 79 |
$remote_address = wd_di()->get( Modern_Remote_Address::class ); |
| 80 |
$remote_address->set_use_proxy( false ); |
| 81 |
|
| 82 |
return $remote_address; |
| 83 |
|
| 84 |
default: |
| 85 |
return wd_di()->get( Classic_Remote_Address::class ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Retrieves the client IP address using the appropriate Remote_Address instance. |
| 91 |
* |
| 92 |
* @return string The client IP address. |
| 93 |
*/ |
| 94 |
public function get_ip_address() { |
| 95 |
return $this->instance()->get_ip_address(); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Return HTTP IP header(s) value if it presents else failed message. |
| 100 |
* |
| 101 |
* @param string $http_ip_header_key HTTP header key/name. |
| 102 |
* |
| 103 |
* @return string Header(s) value or failure message. |
| 104 |
*/ |
| 105 |
public function get_http_ip_header_value( string $http_ip_header_key ): string { |
| 106 |
$ip_array = array(); |
| 107 |
$server = defender_get_data_from_request( null, 's' ); |
| 108 |
if ( '' === $http_ip_header_key ) { |
| 109 |
$ip_array = wd_di()->get( Classic_Remote_Address::class )->get_ip_header(); |
| 110 |
} elseif ( isset( $server[ $http_ip_header_key ] ) ) { |
| 111 |
$ip_array[] = $server[ $http_ip_header_key ]; |
| 112 |
} |
| 113 |
|
| 114 |
return array() !== $ip_array ? |
| 115 |
implode( ', ', $ip_array ) : |
| 116 |
sprintf( /* translators: %s - HTTP IP header */ |
| 117 |
esc_html__( '%s header missing in $_SERVER global variable.', 'defender-security' ), |
| 118 |
$http_ip_header_key |
| 119 |
); |
| 120 |
} |
| 121 |
} |
| 122 |
|