| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the Firewall settings. |
| 4 |
* |
| 5 |
* @package WP_Defender\Model\Setting |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Model\Setting; |
| 9 |
|
| 10 |
use Calotes\Model\Setting; |
| 11 |
use WP_Defender\Traits\IP; |
| 12 |
|
| 13 |
/** |
| 14 |
* Model for Firewall settings. |
| 15 |
*/ |
| 16 |
class Firewall extends Setting { |
| 17 |
|
| 18 |
use IP; |
| 19 |
|
| 20 |
/** |
| 21 |
* Option name. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $table = 'wd_lockdown_settings'; |
| 26 |
|
| 27 |
/** |
| 28 |
* IP Detection Type. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
* @defender_property |
| 32 |
*/ |
| 33 |
public $ip_detection_type = 'automatic'; |
| 34 |
|
| 35 |
/** |
| 36 |
* IP Blocklist Cleanup interval. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
* @defender_property |
| 40 |
*/ |
| 41 |
public $ip_blocklist_cleanup_interval = 'never'; |
| 42 |
|
| 43 |
/** |
| 44 |
* Storage days. |
| 45 |
* |
| 46 |
* @var int |
| 47 |
* @defender_property |
| 48 |
*/ |
| 49 |
public $storage_days = 30; |
| 50 |
|
| 51 |
/** |
| 52 |
* HTTP IP header. |
| 53 |
* |
| 54 |
* @var string |
| 55 |
* @defender_property |
| 56 |
*/ |
| 57 |
public $http_ip_header = 'REMOTE_ADDR'; |
| 58 |
|
| 59 |
/** |
| 60 |
* Trusted proxies IP. |
| 61 |
* |
| 62 |
* @var string |
| 63 |
* @defender_property |
| 64 |
*/ |
| 65 |
public $trusted_proxies_ip = ''; |
| 66 |
|
| 67 |
/** |
| 68 |
* Trusted proxy preset. |
| 69 |
* |
| 70 |
* @var string |
| 71 |
* @defender_property |
| 72 |
*/ |
| 73 |
public $trusted_proxy_preset = ''; |
| 74 |
|
| 75 |
/** |
| 76 |
* Define settings labels. |
| 77 |
* |
| 78 |
* @return array |
| 79 |
*/ |
| 80 |
public function labels(): array { |
| 81 |
return array( |
| 82 |
'storage_days' => esc_html__( 'Days to keep logs', 'defender-security' ), |
| 83 |
'ip_blocklist_cleanup_interval' => esc_html__( 'Clear Temporary IP Block List', 'defender-security' ), |
| 84 |
'http_ip_header' => esc_html__( 'Detect IP Addresses', 'defender-security' ), |
| 85 |
'trusted_proxies_ip' => esc_html__( 'Edit Trusted Proxies', 'defender-security' ), |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get the trusted proxies as an array of IPs. |
| 91 |
* |
| 92 |
* @return array Array of IPs. |
| 93 |
*/ |
| 94 |
public function get_trusted_proxies_ip(): array { |
| 95 |
$ip = $this->trusted_proxies_ip; |
| 96 |
|
| 97 |
$ip_array = array(); |
| 98 |
|
| 99 |
if ( is_string( $ip ) ) { |
| 100 |
$ip_array = preg_split( '#\r\n|[\r\n]#', $ip ); |
| 101 |
|
| 102 |
if ( is_array( $ip_array ) ) { |
| 103 |
$ip_array = array_filter( |
| 104 |
$ip_array, |
| 105 |
function ( $ip ) { |
| 106 |
return strlen( trim( $ip ) ) > 0; |
| 107 |
} |
| 108 |
); |
| 109 |
$ip_array = array_map( 'strtolower', $ip_array ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return (array) $ip_array; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get the trusted proxy preset. |
| 118 |
* |
| 119 |
* @return string |
| 120 |
*/ |
| 121 |
public function get_trusted_proxy_preset(): string { |
| 122 |
return $this->trusted_proxy_preset; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Validation method. |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
protected function after_validate(): void { |
| 131 |
$validation_object = $this->validate_trusted_proxies(); |
| 132 |
|
| 133 |
if ( |
| 134 |
isset( $validation_object['error'] ) |
| 135 |
&& true === $validation_object['error'] |
| 136 |
&& '' !== $validation_object['message'] |
| 137 |
) { |
| 138 |
$this->errors[] = $validation_object['message']; |
| 139 |
} |
| 140 |
|
| 141 |
if ( 'manual' === $this->ip_detection_type && '' === $this->http_ip_header ) { |
| 142 |
$this->errors[] = esc_html__( 'IP Detection option should not be empty.', 'defender-security' ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Validation method for trusted proxies. |
| 148 |
* |
| 149 |
* @return array Return an array with mandatory boolean index error and optional index message which describes the |
| 150 |
* error. |
| 151 |
*/ |
| 152 |
private function validate_trusted_proxies(): array { |
| 153 |
if ( |
| 154 |
in_array( |
| 155 |
$this->http_ip_header, |
| 156 |
\WP_Defender\Component\Firewall::custom_http_headers(), |
| 157 |
true |
| 158 |
) |
| 159 |
) { |
| 160 |
$trusted_proxies_ip = $this->get_trusted_proxies_ip(); |
| 161 |
|
| 162 |
if ( array() === $trusted_proxies_ip ) { |
| 163 |
// Nothing to check. |
| 164 |
return array( 'error' => false ); |
| 165 |
} |
| 166 |
|
| 167 |
foreach ( $trusted_proxies_ip as $ip ) { |
| 168 |
if ( ! $this->validate_ip( $ip ) ) { |
| 169 |
return array( |
| 170 |
'error' => true, |
| 171 |
'message' => sprintf( |
| 172 |
/* translators: %s: IP value. */ |
| 173 |
esc_html__( '%s is not a valid IP address', 'defender-security' ), |
| 174 |
$ip |
| 175 |
), |
| 176 |
); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return array( 'error' => false ); |
| 182 |
} |
| 183 |
} |
| 184 |
|