PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.9.0
Defender Security – Malware Scanner, Login Security & Firewall v5.9.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 / controller / class-ua-lockout.php

class-ua-lockout.php in Defender Security – Malware Scanner, Login Security & Firewall 5.9.0, at src/controller/class-ua-lockout.php

442 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles user agent lockout.
4 *
5 * @package WP_Defender\Controller
6 */
7
8 namespace WP_Defender\Controller;
9
10 use Exception;
11 use WP_Defender\Event;
12 use Calotes\Component\Request;
13 use Calotes\Component\Response;
14 use WP_Defender\Traits\Setting;
15 use WP_Defender\Model\Setting\User_Agent_Lockout;
16 use WP_Defender\Component\Config\Config_Hub_Helper;
17 use WP_Defender\Component\User_Agent as User_Agent_Service;
18
19 /**
20 * Handles user agent lockout.
21 *
22 * @since 2.6.0
23 */
24 class UA_Lockout extends Event {
25
26 use Setting;
27
28 /**
29 * The slug identifier for this controller.
30 *
31 * @var string
32 */
33 public $slug = 'wdf-ip-lockout';
34
35 /**
36 * The model for handling the data.
37 *
38 * @var User_Agent_Lockout
39 */
40 protected $model;
41
42 /**
43 * Service for handling logic.
44 *
45 * @var User_Agent_Service
46 */
47 protected $service;
48
49 /**
50 * Initializes the model and service, registers routes, and sets up scheduled events if the model is active.
51 */
52 public function __construct() {
53 $this->register_routes();
54 $this->model = $this->get_model();
55 $this->service = wd_di()->get( User_Agent_Service::class );
56 add_action( 'defender_enqueue_assets', array( $this, 'enqueue_assets' ) );
57 }
58
59 /**
60 * Returns an instance of the User_Agent_Lockout model class.
61 *
62 * @return User_Agent_Lockout The User_Agent_Lockout model class.
63 */
64 private function get_model() {
65 if ( is_object( $this->model ) ) {
66 return $this->model;
67 }
68
69 return new User_Agent_Lockout();
70 }
71
72 /**
73 * Enqueues scripts and styles for this page.
74 * Only enqueues assets if the page is active.
75 */
76 public function enqueue_assets() {
77 if ( ! $this->is_page_active() ) {
78 return;
79 }
80 wp_localize_script( 'def-iplockout', 'ua_lockout', $this->data_frontend() );
81 }
82
83 /**
84 * Save settings.
85 *
86 * @param Request $request The request object containing new settings data.
87 *
88 * @return Response
89 * @defender_route
90 * @throws Exception If the table is not defined.
91 */
92 public function save_settings( Request $request ) {
93 $data = $request->get_data_by_model( $this->model );
94 $old_enabled = $this->model->enabled;
95 $old_malicious_bot_enabled = $this->model->malicious_bot_enabled;
96 $old_fake_bots_enabled = $this->model->fake_bots_enabled;
97 $prev_data = $this->model->export();
98
99 $this->model->import( $data );
100 if ( $this->model->validate() ) {
101 $arr_blocklist = $this->model->get_lockout_list( 'blocklist' );
102 if ( is_array( $arr_blocklist ) && array() !== $arr_blocklist ) {
103 // Update 'Custom User Agents' if 'Blocklist Presets' is enabled.
104 if (
105 $data['blocklist_presets']
106 && is_array( $data['blocklist_preset_values'] )
107 && array() !== $data['blocklist_preset_values']
108 ) {
109 // Check and remove duplicates.
110 $common_result = array_intersect( $arr_blocklist, $data['blocklist_preset_values'] );
111 if ( array() !== $common_result ) {
112 $arr_blocklist = User_Agent_Service::check_and_remove_duplicates(
113 $arr_blocklist,
114 $common_result
115 );
116 $this->model->blacklist = implode( PHP_EOL, $arr_blocklist );
117 }
118 }
119 // Update 'Custom User Agents' if 'Scripts Presets' is enabled.
120 if (
121 $data['script_presets']
122 && is_array( $data['script_preset_values'] )
123 && array() !== $data['script_preset_values']
124 ) {
125 // Check and remove duplicates.
126 $common_result = array_intersect( $arr_blocklist, $data['script_preset_values'] );
127 if ( array() !== $common_result ) {
128 $arr_blocklist = User_Agent_Service::check_and_remove_duplicates(
129 $arr_blocklist,
130 $common_result
131 );
132 $this->model->blacklist = implode( PHP_EOL, $arr_blocklist );
133 }
134 }
135 }
136
137 $this->model->save();
138 Config_Hub_Helper::set_clear_active_flag();
139 // Attention: next actions after saving model changes.
140 if (
141 ( ! $this->model->enabled && $old_enabled ) ||
142 ( ! $this->model->malicious_bot_enabled && $old_malicious_bot_enabled )
143 ) {
144 wd_di()->get( Malicious_Bot::class )->remove_data();
145 } elseif (
146 ( $this->model->enabled && ! $old_enabled && $this->model->malicious_bot_enabled ) ||
147 ( $this->model->malicious_bot_enabled && ! $old_malicious_bot_enabled && $this->model->enabled )
148 ) {
149 wd_di()->get( Malicious_Bot::class )->rotate_hash();
150 }
151
152 if (
153 ( ! $this->model->enabled && $old_enabled ) ||
154 ( ! $this->model->fake_bots_enabled && $old_fake_bots_enabled )
155 ) {
156 wd_di()->get( Fake_Bot_Detection::class )->remove_data();
157 }
158
159 // Maybe track.
160 if ( ! defender_is_wp_cli() ) {
161 // Track if UA Module is activated/deactivated OR there is a change in State on Add/Remove Custom User Agents (Allowlist/Blocklist).
162 if ( ( $old_enabled !== $data['enabled'] )
163 || ( $this->is_feature_state_changed( $prev_data['blacklist'], $data['blacklist'] ) )
164 || ( $this->is_feature_state_changed( $prev_data['whitelist'], $data['whitelist'] ) )
165 ) {
166 $track_data = array(
167 'Action' => $data['enabled'] ? 'Enabled' : 'Disabled',
168 'No of Bots in the Whitelist' => count( $this->model->get_lockout_list( 'allowlist', false ) ),
169 'No of Bots in the Blocklist' => count( $this->model->get_lockout_list( 'blocklist', false ) ),
170 );
171 $this->track_feature( 'def_user_agent_banning', $track_data );
172 }
173 // New one for 'Blocklist Presets'.
174 if (
175 ( $prev_data['blocklist_presets'] !== $data['blocklist_presets'] ) ||
176 ( count( $prev_data['blocklist_preset_values'] ) !==
177 count( array_intersect( $prev_data['blocklist_preset_values'], $data['blocklist_preset_values'] ) )
178 )
179 ) {
180 $track_data = array(
181 'Action' => $data['blocklist_presets'] ? 'Enabled' : 'Disabled',
182 'List of Activated Bots' => implode( ', ', $data['blocklist_preset_values'] ),
183 );
184 $this->track_feature( 'def_ua_blocklist_preset', $track_data );
185 }
186 // New one for 'Script Presets'.
187 if (
188 ( $prev_data['script_presets'] !== $data['script_presets'] ) ||
189 ( count( $prev_data['script_preset_values'] ) !==
190 count( array_intersect( $prev_data['script_preset_values'], $data['script_preset_values'] ) )
191 )
192 ) {
193 $track_data = array(
194 'Action' => $data['script_presets'] ? 'Enabled' : 'Disabled',
195 'List of Activated Scripts' => implode( ', ', $data['script_preset_values'] ),
196 );
197 $this->track_feature( 'def_ua_scripts_preset', $track_data );
198 }
199 // Track "Fake Bots Detection".
200 if ( $prev_data['fake_bots_enabled'] !== $data['fake_bots_enabled'] ||
201 $prev_data['fake_bots_lockout_type'] !== $data['fake_bots_lockout_type']
202 ) {
203 $data = array(
204 'Action' => $data['fake_bots_enabled'] ? 'Enabled' : 'Disabled',
205 'Blocking type' => 'temporary' === $data['fake_bots_lockout_type'] ? 'Temporary' : 'Permanent',
206 );
207 $this->track_feature( 'def_fake_crawler', $data );
208 }
209 }
210
211 return new Response(
212 true,
213 array_merge(
214 array(
215 'message' => $this->get_update_message(
216 $data,
217 $old_enabled,
218 User_Agent_Lockout::get_module_name()
219 ),
220 'auto_close' => true,
221 ),
222 $this->data_frontend()
223 )
224 );
225 }
226
227 return new Response(
228 false,
229 array( 'message' => $this->model->get_formatted_errors() )
230 );
231 }
232
233 /**
234 * Removes settings for all submodules.
235 */
236 public function remove_settings() {
237 }
238
239 /**
240 * Delete all the data & the cache.
241 */
242 public function remove_data() {
243 }
244
245 /**
246 * Converts the current object state to an array.
247 *
248 * @return array The array representation of the object.
249 */
250 public function to_array(): array {
251 return array();
252 }
253
254 /**
255 * Provides data for the frontend.
256 *
257 * @return array An array of data for the frontend.
258 */
259 public function data_frontend(): array {
260 $arr_model = $this->model->export();
261
262 // Check for empty Disallow line only if Bot Trap is enabled.
263 $has_empty_disallow = false;
264 if ( $this->model->enabled && $this->model->malicious_bot_enabled ) {
265 $malicious_bot_service = wd_di()->get( \WP_Defender\Component\Malicious_Bot::class );
266 $has_empty_disallow = $malicious_bot_service->has_empty_disallow_line();
267 }
268
269 $misc = array(
270 'no_ua' => '' === $arr_model['blacklist'] && '' === $arr_model['whitelist'],
271 'module_name' => User_Agent_Lockout::get_module_name(),
272 'blocklist_presets' => User_Agent_Service::get_blocklist_presets(),
273 'script_presets' => User_Agent_Service::get_script_presets(),
274 'has_empty_disallow_line' => $has_empty_disallow,
275 );
276
277 return array_merge(
278 array(
279 'model' => $arr_model,
280 'misc' => $misc,
281 ),
282 $this->dump_routes_and_nonces()
283 );
284 }
285
286 /**
287 * Adapt the given data array by adding additional fields if necessary.
288 *
289 * @param array $data The data array to adapt.
290 *
291 * @return array The adapted data array.
292 */
293 private function adapt_data( array $data ): array {
294 $adapted_data = array();
295 if ( isset( $data['ua_banning_enabled'] ) ) {
296 $adapted_data['enabled'] = (bool) $data['ua_banning_enabled'];
297 }
298 if ( isset( $data['ua_banning_message'] ) ) {
299 $adapted_data['message'] = $data['ua_banning_message'];
300 }
301 if ( isset( $data['ua_banning_blacklist'] ) ) {
302 $adapted_data['blacklist'] = $data['ua_banning_blacklist'];
303 }
304 if ( isset( $data['ua_banning_whitelist'] ) ) {
305 $adapted_data['whitelist'] = $data['ua_banning_whitelist'];
306 }
307 if ( isset( $data['ua_banning_empty_headers'] ) ) {
308 $adapted_data['empty_headers'] = (bool) $data['ua_banning_empty_headers'];
309 }
310
311 return array_merge( $data, $adapted_data );
312 }
313
314 /**
315 * Imports data into the model.
316 *
317 * @param array $data Data to be imported into the model.
318 *
319 * @throws Exception If table is not defined.
320 */
321 public function import_data( array $data ): void {
322 $model = $this->get_model();
323 if ( array() !== $data ) {
324 $data = $this->adapt_data( $data );
325 $model->import( $data );
326 if ( $model->validate() ) {
327 $model->save();
328 }
329 } else {
330 $default_ua_values = $model->get_default_values();
331 $model->enabled = false;
332 $model->message = $default_ua_values['message'];
333 $model->blacklist = $default_ua_values['blacklist'];
334 $model->whitelist = $default_ua_values['whitelist'];
335 $model->empty_headers = false;
336 $model->save();
337 }
338 }
339
340 /**
341 * Exports User Agents to a CSV file.
342 *
343 * @return void
344 * @defender_route
345 * @since 2.6.0
346 */
347 public function export_ua(): void {
348 $data = array();
349
350 foreach ( $this->model->get_lockout_list( 'blocklist', false ) as $ua ) {
351 $data[] = array(
352 'ua' => $ua,
353 'type' => 'blocklist',
354 );
355 }
356 foreach ( $this->model->get_lockout_list( 'allowlist', false ) as $ua ) {
357 $data[] = array(
358 'ua' => $ua,
359 'type' => 'allowlist',
360 );
361 }
362 // WP_Filesystem class doesn’t directly provide a function for opening a stream to php://memory with the 'w' mode.
363 $fp = fopen( 'php://memory', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
364 foreach ( $data as $fields ) {
365 fputcsv( $fp, $fields, ',', '"', '\\' );
366 }
367 $filename = 'wdf-ua-export-' . wp_date( 'ymdHis' ) . '.csv';
368 fseek( $fp, 0 );
369 header( 'Content-Type: text/csv' );
370 header( 'Content-Disposition: attachment; filename="' . $filename . '";' );
371 // Make php send the generated csv lines to the browser.
372 fpassthru( $fp );
373 exit();
374 }
375
376 /**
377 * Importing UAs from exporter.
378 *
379 * @param Request $request The request object.
380 *
381 * @defender_route
382 * @return Response
383 */
384 public function import_ua( Request $request ) {
385 $data = $request->get_data(
386 array(
387 'id' => array(
388 'type' => 'int',
389 ),
390 )
391 );
392
393 $attached_id = $data['id'];
394 if ( ! is_object( get_post( $attached_id ) ) ) {
395 return new Response(
396 false,
397 array( 'message' => esc_html__( 'Your file is invalid!', 'defender-security' ) )
398 );
399 }
400
401 $file = get_attached_file( $attached_id );
402 if ( ! is_file( $file ) ) {
403 return new Response(
404 false,
405 array( 'message' => esc_html__( 'Your file is invalid!', 'defender-security' ) )
406 );
407 }
408
409 $data = $this->service->verify_import_file( $file );
410 if ( ! $data ) {
411 return new Response(
412 false,
413 array(
414 'message' => esc_html__( 'Your file content is invalid! Please use a CSV file format and try again.', 'defender-security' ),
415 )
416 );
417 }
418
419 // All good, start to import.
420 foreach ( $data as $line ) {
421 $this->model->add_to_list( $line[0], $line[1] );
422 }
423
424 return new Response(
425 true,
426 array(
427 'message' => esc_html__( 'Your blocklist and allowlist have been successfully imported.', 'defender-security' ),
428 'interval' => 1,
429 )
430 );
431 }
432
433 /**
434 * Exports strings.
435 *
436 * @return array An array of strings.
437 */
438 public function export_strings(): array {
439 return array();
440 }
441 }
442