PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.4.0
Defender Security – Malware Scanner, Login Security & Firewall v5.4.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / model / setting / class-firewall.php

class-firewall.php in Defender Security – Malware Scanner, Login Security & Firewall 5.4.0, at src/model/setting/class-firewall.php

180 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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( $ip_array );
104 $ip_array = array_map( 'trim', $ip_array );
105 $ip_array = array_map( 'strtolower', $ip_array );
106 }
107 }
108
109 return (array) $ip_array;
110 }
111
112 /**
113 * Get the trusted proxy preset.
114 *
115 * @return string
116 */
117 public function get_trusted_proxy_preset(): string {
118 return $this->trusted_proxy_preset;
119 }
120
121 /**
122 * Validation method.
123 *
124 * @return void
125 */
126 protected function after_validate(): void {
127 $validation_object = $this->validate_trusted_proxies();
128
129 if (
130 isset( $validation_object['error'] )
131 && true === $validation_object['error']
132 && ! empty( $validation_object['message'] )
133 ) {
134 $this->errors[] = $validation_object['message'];
135 }
136
137 if ( 'manual' === $this->ip_detection_type && '' === $this->http_ip_header ) {
138 $this->errors[] = esc_html__( 'IP Detection option should not be empty.', 'defender-security' );
139 }
140 }
141
142 /**
143 * Validation method for trusted proxies.
144 *
145 * @return array Return an array with mandatory boolean index error and optional index message which describes the
146 * error.
147 */
148 private function validate_trusted_proxies(): array {
149 if (
150 in_array(
151 $this->http_ip_header,
152 \WP_Defender\Component\Firewall::custom_http_headers(),
153 true
154 )
155 ) {
156 $trusted_proxies_ip = $this->get_trusted_proxies_ip();
157
158 if ( empty( $trusted_proxies_ip ) ) {
159 // Nothing to check.
160 return array( 'error' => false );
161 }
162
163 foreach ( $trusted_proxies_ip as $ip ) {
164 if ( ! $this->validate_ip( $ip ) ) {
165 return array(
166 'error' => true,
167 'message' => sprintf(
168 /* translators: %s: IP value. */
169 esc_html__( '%s is not a valid IP address', 'defender-security' ),
170 $ip
171 ),
172 );
173 }
174 }
175 }
176
177 return array( 'error' => false );
178 }
179 }
180