PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.1.0
Defender Security – Malware Scanner, Login Security & Firewall v3.1.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 / component / config / config-adapter.php

config-adapter.php in Defender Security – Malware Scanner, Login Security & Firewall 3.1.0, at src/component/config/config-adapter.php

502 lines 20.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WP_Defender\Component\Config;
4
5 use WP_Defender\Component;
6 use WP_Defender\Model\Setting\Blacklist_Lockout as Model_Blacklist_Lockout;
7 use WP_Defender\Model\Setting\Login_Lockout as Model_Login_Lockout;
8 use WP_Defender\Model\Setting\Notfound_Lockout as Model_Notfound_Lockout;
9 use WP_Defender\Model\Setting\User_Agent_Lockout as Model_Ua_Lockout;
10
11 /**
12 * Class Config_Adapter
13 * @package WP_Defender
14 * @since 2.4.0
15 */
16 class Config_Adapter extends Component {
17
18 /**
19 * @var string
20 */
21 public $status_recipient;
22
23 /**
24 * Upgrade the structure of config data from older versions.
25 * @param array $old_data
26 *
27 * @return array
28 */
29 public function upgrade( $old_data ) {
30 $this->status_recipient = \WP_Defender\Model\Notification::USER_SUBSCRIBED;
31
32 return array(
33 'security_tweaks' => $this->update_security_tweaks( $old_data['security_tweaks'] ),
34 'scan' => empty( $old_data['scan'] ) ? array() : $this->update_scan( $old_data['scan'] ),
35 'iplockout' => empty( $old_data['iplockout'] )
36 ? array()
37 : $this->update_ip_lockout( $old_data['iplockout'] ),
38 // Empty data if Audit module is disabled.
39 'audit' => empty( $old_data['audit'] ) ? array() : $this->update_audit( $old_data['audit'] ),
40 'two_factor' => empty( $old_data['two_factor'] )
41 ? array()
42 : $this->update_two_factor( $old_data['two_factor'] ),
43 // Checks for empty values Mask Login and Security Headers inside methods.
44 'mask_login' => $this->update_mask_login( $old_data['mask_login'] ),
45 'security_headers' => $this->update_security_headers( $old_data['security_headers'] ),
46 'settings' => empty( $old_data['settings'] ) ? array() : $old_data['settings'],
47 'blocklist_monitor' => empty( $old_data['blocklist_monitor'] ) ? array() : $old_data['blocklist_monitor'],
48 'pwned_passwords' => empty( $old_data['pwned_passwords'] ) ? array() : $old_data['pwned_passwords'],
49 );
50 }
51
52 /**
53 * Convert key type from frequency to text.
54 * Attention: it's NOT translation lines.
55 *
56 * @param int $freq
57 *
58 * @return string
59 */
60 private function number_frequency_to_text( $freq ) {
61 switch ( $freq ) {
62 case 1:
63 $text = 'daily';
64 break;
65 case 7:
66 $text = 'weekly';
67 break;
68 case 30:
69 default:
70 $text = 'monthly';
71 break;
72 }
73
74 return $text;
75 }
76
77 /**
78 * @param string|int $frequency_type
79 *
80 * @return string
81 */
82 private function frequency_type( $frequency_type ) {
83
84 return is_numeric( $frequency_type )
85 ? $this->number_frequency_to_text( (int) $frequency_type )
86 : $frequency_type;
87 }
88
89 /**
90 * Report instance uses the 'day' property if a frequency type is daily and 'day_n' if a frequency type is monthly.
91 * @param string|int $frequency_type
92 * @param string $report_day
93 *
94 * @return array
95 */
96 private function frequency_day( $frequency_type, $report_day ) {
97 $days = array(
98 'day_n' => '1',
99 'day' => 'sunday',
100 );
101 if (
102 ( ( is_numeric( $frequency_type ) && 30 === (int) $frequency_type ) )
103 || 'monthly' === $frequency_type
104 ) {
105 if ( empty( $report_day ) ) {
106 return $days;
107 }
108 if ( is_numeric( $report_day ) ) {
109 $days['day_n'] = $report_day;
110 return $days;
111 }
112 // Otherwise, get a number of the first day of the month by the day of the week.
113 $format = sprintf( 'first %s of next month', $report_day );
114 $days['day_n'] = date( 'j', strtotime( $format ) );
115
116 return $days;
117 }
118 // Otherwise, we get report day for daily or weekly.
119 $days['day'] = $report_day;
120
121 return $days;
122 }
123
124 /**
125 * @param array $data
126 *
127 * @return array
128 */
129 private function get_subscribers( $data ) {
130 $subscribers = array();
131 if ( ! empty( $data ) ) {
132 foreach ( $data as $receipt ) {
133 $subscribers['out_house_recipients'][] = array(
134 'name' => $receipt['first_name'],
135 'email' => $receipt['email'],
136 'status' => $this->status_recipient,
137 'avatar' => get_avatar_url( $receipt['email'] ),
138 );
139 }
140 }
141
142 return $subscribers;
143 }
144
145 /**
146 * @param array $old_tweaks
147 *
148 * @return array
149 */
150 public function update_security_tweaks( $old_tweaks ) {
151 $security_tweaks = array(
152 'issues' => $old_tweaks['issues'] ?? array(),
153 'fixed' => $old_tweaks['fixed'] ?? array(),
154 'ignore' => $old_tweaks['ignore'] ?? array(),
155 'automate' => isset( $old_tweaks['automate'] )
156 // Sometimes string value from old version.
157 ? ( is_bool( $old_tweaks['automate'] ) ? $old_tweaks['automate'] : (bool) $old_tweaks['automate'] )
158 : true,
159 'notification' => isset( $old_tweaks['notification'] )
160 ? ( $old_tweaks['notification'] ? 'enabled' : 'disabled' )
161 : 'disabled',
162 // Prepare from 'notification_repeat' to configs['reminder'] with default value 'weekly'.
163 'notification_repeat' => empty( $old_tweaks['notification_repeat'] )
164 ? 'weekly'
165 : ( $old_tweaks['notification_repeat'] ? 'daily' : 'weekly' ),
166 'data' => $old_tweaks['data'] ?? array(),
167 );
168 if ( isset( $old_tweaks['last_sent'] ) ) {
169 $security_tweaks['last_sent'] = $old_tweaks['last_sent'];
170 }
171
172 $security_tweaks['subscribers'] = isset( $old_tweaks['receipts'] )
173 ? $this->get_subscribers( $old_tweaks['receipts'] )
174 : array();
175
176 return $security_tweaks;
177 }
178
179 /**
180 * @param array $old_data
181 *
182 * @return array
183 */
184 public function update_scan( $old_data ) {
185 $scan = array(
186 'integrity_check' => empty( $old_data['scan_core'] ) ? true : $old_data['scan_core'],
187 'check_core' => empty( $old_data['check_core'] ) ? true : $old_data['check_core'],
188 // Leave for migration from prev version to 2.5.0 and vice versa.
189 'check_themes' => empty( $old_data['check_themes'] ) ? false : $old_data['check_themes'],
190 'check_plugins' => empty( $old_data['check_plugins'] ) ? false : $old_data['check_plugins'],
191 'check_known_vuln' => empty( $old_data['scan_vuln'] ) ? true : $old_data['scan_vuln'],
192 'scan_malware' => empty( $old_data['scan_content'] ) ? false : $old_data['scan_content'],
193 'filesize' => empty( $old_data['max_filesize'] ) ? 3 : $old_data['max_filesize'],
194 // Should get bool value.
195 'report' => isset( $old_data['report'] ) && $old_data['report'] ? 'enabled' : 'disabled',
196 'always_send' => empty( $old_data['always_send'] ) ? false : $old_data['always_send'],
197 'time' => empty( $old_data['time'] ) ? '4:00' : $old_data['time'],
198 'frequency' => empty( $old_data['frequency'] )
199 ? 'weekly'
200 : $this->frequency_type( $old_data['frequency'] ),
201 // Should get bool value.
202 'notification' => isset( $old_data['notification'] ) && $old_data['notification']
203 ? 'enabled'
204 : 'disabled',
205 'always_send_notification' => empty( $old_data['always_send_notification'] )
206 ? false
207 : $old_data['always_send_notification'],
208 'error_send' => empty( $old_data['error_send'] ) ? false : $old_data['error_send'],
209 'email_subject_issue_found' => $old_data['email_subject_issue'] ?? '',
210 'email_subject_issue_not_found' => $old_data['email_subject'] ?? '',
211 'email_subject_error' => $old_data['email_subject_error'] ?? '',
212 'email_content_issue_found' => $old_data['email_has_issue'] ?? '',
213 'email_content_issue_not_found' => $old_data['email_all_ok'] ?? '',
214 'email_content_error' => $old_data['email_content_error'] ?? '',
215 // @since 2.7.0
216 'scheduled_scanning' => false,
217 );
218
219 $scan['report_subscribers'] = empty( $old_data['recipients'] )
220 ? array()
221 : $this->get_subscribers( $old_data['recipients'] );
222 $scan['notification_subscribers'] = empty( $old_data['recipients_notification'] )
223 ? array()
224 : $this->get_subscribers( $old_data['recipients_notification'] );
225 // Todo: need the key 'last_report_sent'? It's no always in the unixtime format.
226 if ( empty( $old_data['frequency'] ) ) {
227 $scan['day'] = 'sunday';
228 $scan['day_n'] = '1';
229
230 return $scan;
231 } else {
232 return array_merge( $scan, $this->frequency_day( $old_data['frequency'], $old_data['day'] ) );
233 }
234 }
235
236 /**
237 * @param array $old_data
238 *
239 * @return array
240 */
241 public function update_ip_lockout( $old_data ) {
242 $merged_bl_file_data = $old_data['detect_404_blacklist'] ?? '';
243 // Merge blacklist file & filetype data.
244 if ( isset( $old_data['detect_404_filetypes_blacklist'] ) && '' !== trim( $old_data['detect_404_filetypes_blacklist'] ) ) {
245 $merged_bl_file_data .= PHP_EOL . $old_data['detect_404_filetypes_blacklist'];
246 }
247 $merged_wl_file_data = $old_data['detect_404_whitelist'] ?? '';
248 // Merge whitelist file & filetype data.
249 if ( isset( $old_data['detect_404_whitelist'] ) && '' !== trim( $old_data['detect_404_ignored_filetypes'] ) ) {
250 $merged_wl_file_data .= PHP_EOL . $old_data['detect_404_ignored_filetypes'];
251 }
252
253 $default_login_lockout_values = ( new Model_Login_Lockout() )->get_default_values();
254 $default_404_lockout_values = ( new Model_Notfound_Lockout() )->get_default_values();
255 $default_ip_lockout_values = ( new Model_Blacklist_Lockout() )->get_default_values();
256 $default_ua_lockout_values = ( new Model_Ua_Lockout() )->get_default_values();
257
258 $iplockout = array(
259 'login_protection' => empty( $old_data['login_protection'] )
260 ? true : $old_data['login_protection'],
261 'login_protection_login_attempt' => empty( $old_data['login_protection_login_attempt'] )
262 ? '5' : $old_data['login_protection_login_attempt'],
263 'login_protection_lockout_timeframe' => empty( $old_data['login_protection_lockout_timeframe'] )
264 ? '300' :$old_data['login_protection_lockout_timeframe'],
265 'login_protection_lockout_ban' => empty( $old_data['login_protection_lockout_ban'] )
266 ? false : $old_data['login_protection_lockout_ban'],
267 'login_protection_lockout_duration' => empty( $old_data['login_protection_lockout_duration'] )
268 ? '4' : $old_data['login_protection_lockout_duration'],
269 'login_protection_lockout_duration_unit' => empty( $old_data['login_protection_lockout_duration_unit'] )
270 ? 'hours' : $old_data['login_protection_lockout_duration_unit'],
271 'login_protection_lockout_message' => empty( $old_data['login_protection_lockout_message'] )
272 ? $default_login_lockout_values['message']
273 : $old_data['login_protection_lockout_message'],
274 'username_blacklist' => empty( $old_data['username_blacklist'] )
275 ? '' : $old_data['username_blacklist'],
276 'detect_404' => empty( $old_data['detect_404'] )
277 ? true : $old_data['detect_404'],
278 'detect_404_threshold' => empty( $old_data['detect_404_threshold'] )
279 ? '20' : $old_data['detect_404_threshold'],
280 'detect_404_timeframe' => empty( $old_data['detect_404_timeframe'] )
281 ? '300' : $old_data['detect_404_timeframe'],
282 'detect_404_lockout_ban' => empty( $old_data['detect_404_lockout_ban'] )
283 ? false : $old_data['detect_404_lockout_ban'],
284 'detect_404_lockout_duration' => empty( $old_data['detect_404_lockout_duration'] )
285 ? '4' : $old_data['detect_404_lockout_duration'],
286 'detect_404_lockout_duration_unit' => empty( $old_data['detect_404_lockout_duration_unit'] )
287 ? 'hours' : $old_data['detect_404_lockout_duration_unit'],
288 'detect_404_lockout_message' => empty( $old_data['detect_404_lockout_message'] )
289 ? $default_404_lockout_values['message']
290 : $old_data['detect_404_lockout_message'],
291 'detect_404_blacklist' => $merged_bl_file_data,
292 'detect_404_whitelist' => $merged_wl_file_data,
293 'detect_404_logged' => empty( $old_data['detect_404_logged'] )
294 ? true : $old_data['detect_404_logged'],
295 'ip_blacklist' => empty( $old_data['ip_blacklist'] )
296 ? '' : $old_data['ip_blacklist'],
297 'ip_whitelist' => empty( $old_data['ip_whitelist'] )
298 ? '' : $old_data['ip_whitelist'],
299 'country_blacklist' => empty( $old_data['country_blacklist'] )
300 ? '' : $old_data['country_blacklist'],
301 'country_whitelist' => empty( $old_data['country_whitelist'] )
302 ? '' : $old_data['country_whitelist'],
303 'ip_lockout_message' => empty( $old_data['ip_lockout_message'] )
304 ? $default_ip_lockout_values['message']
305 : $old_data['ip_lockout_message'],
306 'login_lockout_notification' => empty( $old_data['login_lockout_notification'] )
307 ? true : $old_data['login_lockout_notification'],
308 'ip_lockout_notification' => empty( $old_data['ip_lockout_notification'] )
309 ? true : $old_data['ip_lockout_notification'],
310 'notification' => 'enabled',
311 'cooldown_enabled' => empty( $old_data['cooldown_enabled'] )
312 ? false : $old_data['cooldown_enabled'],
313 'cooldown_number_lockout' => empty( $old_data['cooldown_number_lockout'] )
314 ? '3' : $old_data['cooldown_number_lockout'],
315 'cooldown_period' => empty( $old_data['cooldown_period'] )
316 ? '24' : $old_data['cooldown_period'],
317 'report' => isset( $old_data['report'] ) && $old_data['report'] ? 'enabled' : 'disabled',
318 'report_frequency' => empty( $old_data['report_frequency'] )
319 ? 'weekly'
320 : $this->frequency_type( $old_data['report_frequency'] ),
321 // Data for 'day' below.
322 'report_time' => empty( $old_data['report_time'] )
323 ? '4:00' : $old_data['report_time'],
324 'storage_days' => empty( $old_data['storage_days'] )
325 ? '180' : $old_data['storage_days'],
326 'geoIP_db' => $old_data['geoIP_db'] ?? '',
327 'ip_blocklist_cleanup_interval' => empty( $old_data['ip_blocklist_cleanup_interval'] )
328 ? 'never' : $old_data['ip_blocklist_cleanup_interval'],
329 // For UA Banning.
330 'ua_banning_enabled' => $old_data['ua_banning_enabled'] ?? false,
331 'ua_banning_message' => $old_data['ua_banning_message'] ?? $default_ua_lockout_values['message'],
332 'ua_banning_blacklist' => $old_data['ua_banning_blacklist'] ?? $default_ua_lockout_values['blacklist'],
333 'ua_banning_whitelist' => $old_data['ua_banning_whitelist'] ?? $default_ua_lockout_values['whitelist'],
334 'ua_banning_empty_headers' => $old_data['ua_banning_empty_headers'] ?? false,
335 'maxmind_license_key' => $old_data['maxmind_license_key'] ?? '',
336 );
337 if ( isset( $old_data['lastReportSent'] ) && ! empty( $old_data['lastReportSent'] ) ) {
338 $iplockout['last_sent'] = $old_data['lastReportSent'];
339 }
340
341 $iplockout['report_subscribers'] = empty( $old_data['report_receipts'] )
342 ? array()
343 : $this->get_subscribers( $old_data['report_receipts'] );
344 $iplockout['notification_subscribers'] = empty( $old_data['receipts'] )
345 ? array()
346 : $this->get_subscribers( $old_data['receipts'] );
347
348 if ( empty( $old_data['report_frequency'] ) ) {
349 $iplockout['day'] = 'sunday';
350 $iplockout['day_n'] = '1';
351
352 return $iplockout;
353 } else {
354 return array_merge( $iplockout, $this->frequency_day( $old_data['report_frequency'], $old_data['report_day'] ) );
355 }
356 }
357
358 /**
359 * @param array $old_data
360 *
361 * @return array
362 */
363 public function update_audit( $old_data ) {
364 $audit = array(
365 'enabled' => is_bool( $old_data['enabled'] )
366 ? $old_data['enabled']
367 : (bool) $old_data['enabled'],
368 'report' => isset( $old_data['notification'] ) && $old_data['notification'] ? 'enabled' : 'disabled',
369 'frequency' => empty( $old_data['frequency'] )
370 ? 'weekly'
371 : $this->frequency_type( $old_data['frequency'] ),
372 'time' => empty( $old_data['time'] )
373 ? '4:00'
374 : $old_data['time'],
375 'storage_days' => empty( $old_data['storage_days'] )
376 ? '6 months'
377 : $old_data['storage_days'],
378 );
379 if ( isset( $old_data['lastReportSent'] ) && ! empty( $old_data['lastReportSent'] ) ) {
380 $audit['last_sent'] = $old_data['lastReportSent'];
381 }
382
383 $audit['subscribers'] = empty( $old_data['receipts'] )
384 ? array()
385 : $this->get_subscribers( $old_data['receipts'] );
386
387 if ( empty( $old_data['frequency'] ) ) {
388 $audit['day'] = 'sunday';
389 $audit['day_n'] = '1';
390
391 return $audit;
392 } else {
393 return array_merge( $audit, $this->frequency_day( $old_data['frequency'], $old_data['day'] ) );
394 }
395 }
396
397 /**
398 * @param array $old_data
399 *
400 * @return array
401 */
402 public function update_two_factor( $old_data ) {
403
404 return array(
405 'enabled' => $old_data['enabled'],
406 'lost_phone' => $old_data['lost_phone'],
407 'force_auth' => $old_data['force_auth'],
408 'force_auth_mess' => $old_data['force_auth_mess'],
409 'user_roles' => $old_data['user_roles'],
410 'force_auth_roles' => $old_data['force_auth_roles'],
411 'custom_graphic' => $old_data['custom_graphic'],
412 'custom_graphic_url' => $old_data['custom_graphic_url'] ?? '',
413 'email_subject' => $old_data['email_subject'] ?? '',
414 'email_sender' => $old_data['email_sender'] ?? '',
415 'email_body' => $old_data['email_body'] ?? '',
416 'app_title' => '',
417 );
418 }
419
420 /**
421 * @param array $old_data
422 *
423 * @return array
424 */
425 public function update_mask_login( $old_data ) {
426 if ( empty( $old_data ) ) {
427 // Sometimes migrated data is empty.
428 return array(
429 'mask_url' => '',
430 'redirect_traffic' => 'off',
431 'redirect_traffic_url' => '',
432 'enabled' => false,
433 'redirect_traffic_page_id' => 0,
434 );
435 } else {
436
437 return array(
438 'enabled' => $old_data['enabled'],
439 'mask_url' => $old_data['mask_url'] ?? '',
440 'redirect_traffic' => $old_data['redirect_traffic'] ? 'custom_url' : 'off',
441 'redirect_traffic_url' => $old_data['redirect_traffic_url'] ?: '',
442 'redirect_traffic_page_id' => 0,
443 );
444 }
445 }
446
447 /**
448 * @since 2.5.0 Remove 'ALLOW-FROM' directive and move to 'sameorigin' by default.
449 * Leave 'sh_xframe_urls' for config migration.
450 * @param array $old_data
451 *
452 * @return array
453 */
454 public function update_security_headers( $old_data ) {
455 if ( empty( $old_data ) ) {
456 // Sometimes migrated data is empty.
457 $model_sec_headers = new \WP_Defender\Model\Setting\Security_Headers();
458
459 return array(
460 'sh_xframe' => $model_sec_headers->sh_xframe,
461 'sh_xframe_mode' => $model_sec_headers->sh_xframe_mode,
462 // Leave for migration to 2.5.1.
463 'sh_xframe_urls' => '',
464 'sh_xss_protection' => $model_sec_headers->sh_xss_protection,
465 'sh_xss_protection_mode' => $model_sec_headers->sh_xss_protection_mode,
466 'sh_content_type_options' => $model_sec_headers->sh_content_type_options,
467 'sh_content_type_options_mode' => $model_sec_headers->sh_content_type_options_mode,
468 'sh_strict_transport' => $model_sec_headers->sh_strict_transport,
469 'hsts_preload' => $model_sec_headers->hsts_preload,
470 'include_subdomain' => $model_sec_headers->include_subdomain,
471 'hsts_cache_duration' => $model_sec_headers->hsts_cache_duration,
472 'sh_referrer_policy' => $model_sec_headers->sh_referrer_policy,
473 'sh_referrer_policy_mode' => $model_sec_headers->sh_referrer_policy_mode,
474 'sh_feature_policy' => $model_sec_headers->sh_feature_policy,
475 'sh_feature_policy_mode' => $model_sec_headers->sh_feature_policy_mode,
476 'sh_feature_policy_urls' => $model_sec_headers->sh_feature_policy_urls,
477 );
478 } else {
479
480 return array(
481 'sh_xframe' => (bool) $old_data['sh_xframe'],
482 'sh_xframe_mode' => $old_data['sh_xframe_mode'],
483 // Leave for migration to 2.5.1.
484 'sh_xframe_urls' => $old_data['sh_xframe_urls'] ?? '',
485 'sh_xss_protection' => (bool) $old_data['sh_xss_protection'],
486 'sh_xss_protection_mode' => $old_data['sh_xss_protection_mode'],
487 'sh_content_type_options' => (bool) $old_data['sh_content_type_options'],
488 'sh_content_type_options_mode' => $old_data['sh_content_type_options_mode'],
489 'sh_strict_transport' => (bool) $old_data['sh_strict_transport'],
490 'hsts_preload' => $old_data['hsts_preload'],
491 'include_subdomain' => $old_data['include_subdomain'],
492 'hsts_cache_duration' => $old_data['hsts_cache_duration'],
493 'sh_referrer_policy' => (bool) $old_data['sh_referrer_policy'],
494 'sh_referrer_policy_mode' => $old_data['sh_referrer_policy_mode'],
495 'sh_feature_policy' => (bool) $old_data['sh_feature_policy'],
496 'sh_feature_policy_mode' => $old_data['sh_feature_policy_mode'],
497 'sh_feature_policy_urls' => $old_data['sh_feature_policy_urls'],
498 );
499 }
500 }
501 }
502