PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
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 / backup-settings.php

backup-settings.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at src/component/backup-settings.php

1,385 lines 53.0 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;
4
5 use WP_Defender\Component;
6 use WP_Defender\Controller\Audit_Logging;
7 use WP_Defender\Model\Setting\Security_Tweaks as Model_Security_Tweaks;
8 use WP_Defender\Model\Setting\Scan as Model_Scan;
9 use WP_Defender\Model\Setting\Audit_Logging as Model_Audit_Logging;
10 use WP_Defender\Model\Setting\Firewall as Model_Firewall;
11 use WP_Defender\Model\Setting\Login_Lockout as Model_Login_Lockout;
12 use WP_Defender\Model\Setting\Notfound_Lockout as Model_Notfound_Lockout;
13 use WP_Defender\Model\Setting\Blacklist_Lockout as Model_Blacklist_Lockout;
14 use WP_Defender\Model\Setting\Two_Fa as Model_Two_Fa;
15 use WP_Defender\Model\Setting\Mask_Login as Model_Mask_Login;
16 use WP_Defender\Model\Setting\Security_Headers as Model_Security_Headers;
17 use WP_Defender\Model\Setting\Main_Setting as Model_Main_Setting;
18 use WP_Defender\Model\Setting\Password_Protection as Model_Password_Protection;
19 use WP_Defender\Model\Setting\User_Agent_Lockout as Model_Ua_Lockout;
20 use WP_Defender\Model\Notification\Tweak_Reminder;
21 use WP_Defender\Model\Notification\Malware_Notification;
22 use WP_Defender\Model\Notification\Malware_Report;
23 use WP_Defender\Model\Notification\Firewall_Notification;
24 use WP_Defender\Model\Notification\Firewall_Report;
25 use WP_Defender\Model\Notification\Audit_Report;
26 use WP_Defender\Controller\Security_Tweaks as Controller_Security_Tweaks;
27 use WP_Defender\Controller\Scan as Controller_Scan;
28 use WP_Defender\Controller\Two_Factor;
29 use WP_Defender\Controller\Firewall as Controller_Firewall;
30 use WP_Defender\Controller\Mask_Login as Controller_Mask_Login;
31 use WP_Defender\Controller\Blocklist_Monitor;
32 use WP_Defender\Controller\Password_Protection as Controller_Password_Protection;
33 use WP_Defender\Controller\UA_Lockout as Controller_Ua_Lockout;
34 use WP_Defender\Component\Security_Tweaks\Prevent_Enum_Users;
35 use WP_Defender\Component\Security_Tweaks\Security_Key;
36
37 class Backup_Settings extends Component {
38 public const KEY = 'defender_last_settings', INDEXER = 'defender_config_indexer';
39
40 /**
41 * @param object $notification_object
42 *
43 * @return array
44 */
45 public function change_subscriber_format( $notification_object ) {
46 if (
47 ! is_object( $notification_object )
48 && ! is_array( $notification_object->in_house_recipients )
49 && ! is_array( $notification_object->out_house_recipients )
50 ) {
51 return array();
52 }
53 if ( empty( $notification_object->in_house_recipients ) && empty( $notification_object->out_house_recipients ) ) {
54 return array();
55 }
56
57 $subscribers = array();
58 if ( ! empty( $notification_object->in_house_recipients ) ) {
59 $subscribers['in_house_recipients'] = $notification_object->in_house_recipients;
60 }
61 if ( ! empty( $notification_object->out_house_recipients ) ) {
62 $subscribers['out_house_recipients'] = $notification_object->out_house_recipients;
63 }
64
65 return $subscribers;
66 }
67
68 /**
69 * Gather settings from all modules.
70 *
71 * @return array
72 */
73 public function gather_data() {
74 $audit = [];
75 $tweak_class = wd_di()->get( Controller_Security_Tweaks::class );
76 $tweak_class->refresh_tweaks_status();
77 $settings = new Model_Security_Tweaks();
78 $tweak_notification = new Tweak_Reminder();
79 $security_tweaks = array(
80 'notification_repeat' => $tweak_notification->configs['reminder'],
81 'subscribers' => $this->change_subscriber_format( $tweak_notification ),
82 'notification' => $tweak_notification->status,
83 'data' => $settings->data,
84 'fixed' => $settings->fixed,
85 'issues' => $settings->issues,
86 'ignore' => $settings->ignore,
87 'automate' => $settings->automate,
88 'enabled_user_enums' => $this->get_enabled_user_enums(),
89 'security_key' => $this->get_security_key_all_options(),
90 );
91 $settings = new Model_Scan();
92 $scan_report = new Malware_Report();
93 $scan_notification = new Malware_Notification();
94 $scan = array(
95 'integrity_check' => $settings->integrity_check,
96 'check_core' => $settings->check_core,
97 // Leave for migration from prev version to 2.5.0 and vice versa.
98 'check_themes' => false,
99 'check_plugins' => $settings->check_plugins,
100 'check_known_vuln' => $settings->check_known_vuln,
101 'scan_malware' => $settings->scan_malware,
102 'filesize' => $settings->filesize,
103 // @since 2.7.0 changes for Scheduled options.
104 'scheduled_scanning' => $settings->scheduled_scanning,
105 'day' => $settings->day,
106 'day_n' => $settings->day_n,
107 'time' => $settings->time,
108 'frequency' => $settings->frequency,
109 'report' => $scan_report->status,
110 'always_send' => $scan_report->configs['always_send'],
111 'report_subscribers' => $this->change_subscriber_format( $scan_report ),
112 // @since 2.7.0 for backward compatibility. We can remove it in the next version.
113 'dry_run' => false,
114 'notification' => $scan_notification->status,
115 'always_send_notification' => $scan_notification->configs['always_send'],
116 'error_send' => $scan_notification->configs['error_send'],
117 'notification_subscribers' => $this->change_subscriber_format( $scan_notification ),
118 'email_subject_issue_not_found' => $scan_notification->configs['template']['not_found']['subject'],
119 'email_subject_issue_found' => $scan_notification->configs['template']['found']['subject'],
120 'email_subject_error' => $scan_notification->configs['template']['error']['subject'],
121 'email_content_issue_not_found' => $scan_notification->configs['template']['not_found']['body'],
122 'email_content_issue_found' => $scan_notification->configs['template']['found']['body'],
123 'email_content_error' => $scan_notification->configs['template']['error']['body'],
124 );
125
126 $is_pro = ( new \WP_Defender\Behavior\WPMUDEV() )->is_pro();
127 if ( class_exists( Model_Audit_Logging::class ) ) {
128 $settings = new Model_Audit_Logging();
129 $audit_report = new Audit_Report();
130 $audit = array(
131 'enabled' => $settings->is_active(),
132 'report' => $audit_report->status,
133 'subscribers' => $this->change_subscriber_format( $audit_report ),
134 'frequency' => $audit_report->frequency,
135 'day' => $audit_report->day,
136 'day_n' => $audit_report->day_n,
137 'time' => $audit_report->time,
138 // @since 2.7.0 We can remove it in the next version.
139 'dry_run' => false,
140 'storage_days' => $settings->storage_days,
141 );
142 if ( ! $is_pro ) {
143 $audit['enabled'] = false;
144 }
145 } else {
146 $audit['enabled'] = false;
147 }
148 $settings_firewall = new Model_Firewall();
149 $settings_ll = new Model_Login_Lockout();
150 $settings_nl = new Model_Notfound_Lockout();
151 $settings_bl = new Model_Blacklist_Lockout();
152 $lockout_notification = new Firewall_Notification();
153 $lockout_report = new Firewall_Report();
154 $ua_banning_model = new Model_Ua_Lockout();
155 $iplockout = array(
156 'login_protection' => $settings_ll->enabled,
157 'login_protection_login_attempt' => $settings_ll->attempt,
158 'login_protection_lockout_timeframe' => $settings_ll->timeframe,
159 'login_protection_lockout_ban' => $settings_ll->lockout_type,
160 'login_protection_lockout_duration' => $settings_ll->duration,
161 'login_protection_lockout_duration_unit' => $settings_ll->duration_unit,
162 'login_protection_lockout_message' => $settings_ll->lockout_message,
163 'username_blacklist' => $settings_ll->username_blacklist,
164 'detect_404' => $settings_nl->enabled,
165 'detect_404_threshold' => $settings_nl->attempt,
166 'detect_404_timeframe' => $settings_nl->timeframe,
167 'detect_404_lockout_ban' => $settings_nl->lockout_type,
168 'detect_404_lockout_duration' => $settings_nl->duration,
169 'detect_404_lockout_duration_unit' => $settings_nl->duration_unit,
170 'detect_404_lockout_message' => $settings_nl->lockout_message,
171 'detect_404_blacklist' => $settings_nl->blacklist,
172 'detect_404_whitelist' => $settings_nl->whitelist,
173 'detect_404_logged' => $settings_nl->detect_logged,
174 'ip_blacklist' => $settings_bl->ip_blacklist,
175 'ip_whitelist' => $settings_bl->ip_whitelist,
176 'country_blacklist' => $settings_bl->country_blacklist,
177 'country_whitelist' => $settings_bl->country_whitelist,
178 'ip_lockout_message' => $settings_bl->ip_lockout_message,
179 'login_lockout_notification' => $lockout_notification->configs['login_lockout'],
180 'ip_lockout_notification' => $lockout_notification->configs['nf_lockout'],
181 'notification' => $lockout_notification->status,
182 'notification_subscribers' => $this->change_subscriber_format( $lockout_notification ),
183 'cooldown_enabled' => $lockout_notification->configs['limit'],
184 'cooldown_number_lockout' => $lockout_notification->configs['threshold'],
185 'cooldown_period' => $lockout_notification->configs['cool_off'],
186 'report' => $lockout_report->status,
187 'report_subscribers' => $this->change_subscriber_format( $lockout_report ),
188 'report_frequency' => $lockout_report->frequency,
189 'day' => $lockout_report->day,
190 'day_n' => $lockout_report->day_n,
191 'report_time' => $lockout_report->time,
192 // @since 2.7.0 We can remove it in the next version.
193 'dry_run' => false,
194 'storage_days' => $settings_firewall->storage_days,
195 'geoIP_db' => $settings_bl->geodb_path,
196 'maxmind_license_key' => $settings_bl->maxmind_license_key,
197 'ip_blocklist_cleanup_interval' => $settings_firewall->ip_blocklist_cleanup_interval,
198 // For UA Banning.
199 'ua_banning_enabled' => $ua_banning_model->enabled,
200 'ua_banning_message' => $ua_banning_model->message,
201 'ua_banning_blacklist' => $ua_banning_model->blacklist,
202 'ua_banning_whitelist' => $ua_banning_model->whitelist,
203 'ua_banning_empty_headers' => $ua_banning_model->empty_headers,
204 );
205 $settings_two_fa = new Model_Two_Fa();
206 $settings_mask_login = new Model_Mask_Login();
207 $advanced_tools = array(
208 'two_factor' => $settings_two_fa->export(),
209 'mask_login' => $settings_mask_login->export(),
210 );
211 $settings_main = new Model_Main_Setting();
212 $main_settings = $settings_main->export();
213 $settings_sec_headers = new Model_Security_Headers();
214 $security_headers = $settings_sec_headers->export();
215 $ret = array(
216 'security_tweaks' => $security_tweaks,
217 'scan' => $scan,
218 'iplockout' => $iplockout,
219 'two_factor' => $advanced_tools['two_factor'],
220 'mask_login' => $advanced_tools['mask_login'],
221 'settings' => $main_settings,
222 'security_headers' => $security_headers,
223 );
224 if ( isset( $audit ) ) {
225 $ret['audit'] = $audit;
226 }
227 // For Blocklist_Monitor.
228 if ( $is_pro ) {
229 $blocklist_monitor_class = wd_di()->get( Blocklist_Monitor::class );
230 $status = (string) $blocklist_monitor_class->get_status();
231 $ret['blocklist_monitor']['enabled'] = '1' === $status;
232 $ret['blocklist_monitor']['status'] = $status;
233 } else {
234 $ret['blocklist_monitor']['enabled'] = false;
235 }
236 // For Pwned passwords.
237 $pwned_password_model = wd_di()->get( Model_Password_Protection::class );
238 $ret['pwned_passwords']['enabled'] = $pwned_password_model->is_active();
239 $ret['pwned_passwords']['user_roles'] = $pwned_password_model->user_roles;
240 $ret['pwned_passwords']['custom_message'] = $pwned_password_model->pwned_actions['force_change_message'];
241
242 return $ret;
243 }
244
245 /**
246 * @return array
247 */
248 public function get_configs() {
249 $keys = get_site_option( self::INDEXER, false );
250 $results = array();
251 if ( empty( $keys ) ) {
252
253 return $results;
254 }
255
256 foreach ( $keys as $key ) {
257 $config = get_site_option( $key );
258
259 if ( false === $config ) {
260 $this->remove_index( $key );
261 } else {
262 $results[ $key ] = $config;
263 }
264 }
265
266 return $results;
267 }
268
269 /**
270 * @param string $key
271 */
272 public function make_config_active( $key ) {
273 $configs = $this->get_configs();
274 foreach ( $configs as $k => $config ) {
275 if ( $k === $key ) {
276 $config['is_active'] = true;
277 } else {
278 $config['is_active'] = false;
279 }
280 update_site_option( $k, $config );
281 }
282 }
283
284 public function clear_configs() {
285 $keys = get_site_option( self::INDEXER, false );
286 if ( is_array( $keys ) && ! empty( $keys ) ) {
287 foreach ( $keys as $key ) {
288 delete_site_option( $key );
289 }
290 }
291 delete_site_option( self::INDEXER );
292 delete_site_option( self::KEY );
293 }
294
295 /**
296 * Create a default config.
297 */
298 public function maybe_create_default_config() {
299 $keys = get_site_option( self::INDEXER, false );
300 if ( false === $keys ) {
301 $key = 'wp_defender_config_default' . time();
302 if ( ! get_site_option( $key ) ) {
303 $this->create_basic_config();
304 }
305 }
306 }
307
308 /**
309 * @since 2.6.5 Reduce differences between Basic and Default security configs.
310 */
311 private function create_basic_config() {
312 $configs = [];
313 // @since 2.6.5 Clear recipients.
314 $default_recipients = array();
315 // Init Tweaks class and refresh status.
316 $tweak_class = wd_di()->get( Controller_Security_Tweaks::class );
317 $tweak_class->refresh_tweaks_status();
318 // All required models.
319 $model_settings = new Model_Main_Setting();
320 $user_roles = array_keys( get_editable_roles() );
321 // Default values.
322 $default_scan_notification_values = ( new Malware_Notification() )->get_default_values();
323 $default_login_lockout_values = ( new Model_Login_Lockout() )->get_default_values();
324 $default_404_lockout_values = ( new Model_Notfound_Lockout() )->get_default_values();
325 $default_ip_lockout_values = ( new Model_Blacklist_Lockout() )->get_default_values();
326 $default_ua_lockout_values = ( new Model_Ua_Lockout() )->get_default_values();
327 $default_password_protection_values = ( new Model_Password_Protection() )->get_default_values();
328 $default_2fa_values = ( new Model_Two_Fa() )->get_default_values();
329 // Total data.
330 $data = array(
331 'security_tweaks' => array(
332 'notification_repeat' => 'weekly',
333 'subscribers' => $default_recipients,
334 'notification' => 'enabled',
335 'automate' => true,
336 'data' => array(),
337 // @since 2.7.0 Specific values for 4 fixed, 0 ignored and 8 actioned tweaks. 12 tweaks in total.
338 'fixed' => array(
339 'disable-xml-rpc',
340 'login-duration',
341 'disable-trackback',
342 'prevent-enum-users'
343 ),
344 'issues' => array(
345 'php-version',
346 'wp-version',
347 'prevent-php-executed',
348 'protect-information',
349 'replace-admin-username',
350 'security-key',
351 'disable-file-editor',
352 'hide-error',
353 ),
354 'ignore' => array(),
355 ),
356 'scan' => array(
357 'integrity_check' => true,
358 'check_core' => true,
359 // Leave for migration from prev version to 2.5.0 and vice versa.
360 'check_themes' => false,
361 'check_plugins' => false,
362 'check_known_vuln' => true,
363 'scan_malware' => false,
364 'filesize' => 3,
365 'report' => 'enabled',
366 'always_send' => false,
367 'report_subscribers' => $default_recipients,
368 'day' => 'sunday',
369 'day_n' => '1',
370 'time' => '4:00',
371 'frequency' => 'weekly',
372 // @since 2.7.0 We can remove it in the next version.
373 'dry_run' => false,
374 'notification' => 'enabled',
375 'always_send_notification' => false,
376 'error_send' => false,
377 'notification_subscribers' => $default_recipients,
378 'email_subject_issue_found' => $default_scan_notification_values['subject_issue_found'],
379 'email_subject_issue_not_found' => $default_scan_notification_values['subject_issue_not_found'],
380 'email_subject_error' => $default_scan_notification_values['subject_error'],
381 'email_content_issue_found' => $default_scan_notification_values['content_issue_found'],
382 'email_content_issue_not_found' => $default_scan_notification_values['content_issue_not_found'],
383 'email_content_error' => $default_scan_notification_values['content_error'],
384 // @since 2.7.0 move Scheduled options from Malware Scanning - Reporting to Malware settings.
385 // Values for frequency, day and time are above.
386 'scheduled_scanning' => true,
387 ),
388 'iplockout' => array(
389 'login_protection' => true,
390 'login_protection_login_attempt' => '5',
391 'login_protection_lockout_timeframe' => '300',
392 'login_protection_lockout_ban' => 'timeframe',
393 'login_protection_lockout_duration' => '4',
394 'login_protection_lockout_duration_unit' => 'hours',
395 'login_protection_lockout_message' => $default_login_lockout_values['message'],
396 // @since 2.7.0 New default blocklisted usernames.
397 'username_blacklist' => "adm\nadmin\nadmin1\nhostname\nmanager\nqwerty\nroot\nsupport\nsysadmin\ntest\nuser\nadministrator",
398 'detect_404' => true,
399 'detect_404_threshold' => '20',
400 'detect_404_timeframe' => '300',
401 'detect_404_lockout_ban' => 'timeframe',
402 'detect_404_lockout_duration' => '4',
403 'detect_404_lockout_duration_unit' => 'hours',
404 'detect_404_lockout_message' => $default_404_lockout_values['message'],
405 'detect_404_blacklist' => '',
406 // @since 2.7.0 New default whitelisted extensions.
407 'detect_404_whitelist' => ".css\n.js\n.jpg\n.png\n.gif\n.map",
408 'detect_404_logged' => true,
409 'ip_blacklist' => '',
410 // @since 2.6.5 Clear the current user IP.
411 'ip_whitelist' => '',
412 'country_blacklist' => '',
413 'country_whitelist' => '',
414 'ip_lockout_message' => $default_ip_lockout_values['message'],
415 'login_lockout_notification' => true,
416 'ip_lockout_notification' => true,
417 'notification' => 'enabled',
418 'notification_subscribers' => $default_recipients,
419 'cooldown_enabled' => false,
420 'cooldown_number_lockout' => '3',
421 'cooldown_period' => '24',
422 'report' => 'enabled',
423 'report_subscribers' => $default_recipients,
424 'report_frequency' => 'weekly',
425 'day' => 'sunday',
426 'day_n' => '1',
427 'report_time' => '4:00',
428 // @since 2.7.0 We can remove it in the next version.
429 'dry_run' => false,
430 'storage_days' => '180',
431 'geoIP_db' => '',
432 'ip_blocklist_cleanup_interval' => 'never',
433 'ua_banning_enabled' => false,
434 'ua_banning_message' => $default_ua_lockout_values['message'],
435 'ua_banning_blacklist' => $default_ua_lockout_values['blacklist'],
436 'ua_banning_whitelist' => $default_ua_lockout_values['whitelist'],
437 'ua_banning_empty_headers' => false,
438 // @since 2.7.1 New key of Blacklist_Lockout.
439 'maxmind_license_key' => '',
440 ),
441 'two_factor' => array(
442 // @since 2.6.5 Disabled module.
443 'enabled' => false,
444 'lost_phone' => true,
445 'force_auth' => false,
446 'force_auth_mess' => $default_2fa_values['message'],
447 'user_roles' => $user_roles,
448 'force_auth_roles' => array(),
449 'custom_graphic' => false,
450 'custom_graphic_url' => '',
451 'email_subject' => $default_2fa_values['email_subject'],
452 'email_sender' => $default_2fa_values['email_sender'],
453 'email_body' => $default_2fa_values['email_body'],
454 'app_title' => $default_2fa_values['app_title'],
455 ),
456 'mask_login' => array(
457 'enabled' => false,
458 'mask_url' => '',
459 'redirect_traffic' => 'off',
460 'redirect_traffic_url' => '',
461 'redirect_traffic_page_id' => 0,
462 ),
463 'security_headers' => array(
464 'sh_xframe' => true,
465 'sh_xframe_mode' => 'sameorigin',
466 'sh_xframe_urls' => '',
467 'sh_xss_protection' => true,
468 'sh_xss_protection_mode' => 'sanitize',
469 'sh_content_type_options' => true,
470 'sh_content_type_options_mode' => 'nosniff',
471 'sh_strict_transport' => true,
472 'hsts_preload' => false,
473 'include_subdomain' => false,
474 'hsts_cache_duration' => '30 days',
475 'sh_referrer_policy' => true,
476 'sh_referrer_policy_mode' => 'origin-when-cross-origin',
477 'sh_feature_policy' => true,
478 'sh_feature_policy_mode' => 'self',
479 'sh_feature_policy_urls' => '',
480 ),
481 'settings' => array(
482 'uninstall_data' => 'keep',
483 'uninstall_settings' => 'preserve',
484 // @since 2.7.0 Empty.
485 'translate' => '',
486 'usage_tracking' => false,
487 'high_contrast_mode' => false,
488 ),
489 'pwned_passwords' => array(
490 'enabled' => false,
491 'user_roles' => $user_roles,
492 'custom_message' => $default_password_protection_values['message'],
493 ),
494 );
495
496 $is_pro = ( new \WP_Defender\Behavior\WPMUDEV() )->is_pro();
497 if ( $is_pro ) {
498 $data['audit'] = array(
499 'enabled' => true,
500 'report' => 'enabled',
501 'subscribers' => $default_recipients,
502 'frequency' => 'weekly',
503 'day' => 'sunday',
504 'day_n' => '1',
505 'time' => '4:00',
506 // @since 2.7.0 We can remove it in the next version.
507 'dry_run' => false,
508 'storage_days' => '6 months',
509 );
510 $data['blocklist_monitor'] = array(
511 // @since 2.7.0 Enable.
512 'enabled' => true,
513 'status' => '1',
514 );
515 } else {
516 $data['audit']['enabled'] = false;
517 $data['blocklist_monitor']['enabled'] = false;
518 }
519
520 $configs['configs'] = $data;
521 $configs['strings'] = $this->create_default_module_strings( $data, $is_pro );
522 $configs['name'] = __( 'Basic Config', 'wpdef' );
523 $configs['description'] = __( 'Recommended default protection for every site', 'wpdef' );
524 $configs['immortal'] = true;
525 $key = 'wp_defender_config_default' . time();
526 update_site_option( $key, $configs );
527 $this->index_key( $key );
528 }
529
530 /**
531 * @param string $key
532 */
533 public function index_key( $key ) {
534 $keys = get_site_option( self::INDEXER, false );
535 $keys[ $key ] = $key;
536 update_site_option( self::INDEXER, $keys );
537 }
538
539 /**
540 * @param string $key
541 */
542 public function remove_index( $key ) {
543 $keys = get_site_option( self::INDEXER, false );
544 unset( $keys[ array_search( $key, $keys, true ) ] );
545 update_site_option( self::INDEXER, $keys );
546 }
547
548 /**
549 * Backup the previous data before we process new version.
550 *
551 * @return array
552 */
553 public function backup_data() {
554 $data = $this->get_prev_settings();
555 $old_backup = get_site_option( self::KEY );
556 if ( ! is_array( $old_backup ) ) {
557 $old_backup = array();
558 }
559 if ( count( $old_backup ) > 20 ) {
560 // Remove the oldest key.
561 $old_backup = array_shift( $old_backup );
562 }
563 $version = get_site_option( 'wd_db_version' );
564 $old_backup[ $version . '_' . time() ] = $data;
565 update_site_option( self::KEY, $old_backup );
566
567 return $data;
568 }
569
570 /**
571 * @param array $data Config data.
572 * @param string $request_reason Cases: plugin, hub, migration.
573 *
574 * @return bool
575 */
576 public function restore_data( $data, $request_reason = 'plugin' ) {
577 $need_reauth = false;
578 foreach ( $data as $module => $module_data ) {
579 if ( ! is_array( $module_data ) ) {
580 continue;
581 }
582
583 $controller = $this->module_to_controller( $module, true );
584 // Return array of objects if the module is IP Lockout.
585 if ( is_object( $controller ) || is_array( $controller ) ) {
586 $is_pro = ( new \WP_Defender\Behavior\WPMUDEV() )->is_pro();
587 foreach ( $module_data as &$value ) {
588 if ( ! is_array( $value ) && ! filter_var( $value, FILTER_VALIDATE_BOOLEAN ) ) {
589 $value = str_replace( '{nl}', PHP_EOL, $value );
590 }
591 }
592 // Import data.
593 if ( 'iplockout' === $module ) {
594 foreach ( $controller as $lockout_controller ) {
595 $lockout_controller->import_data( $module_data );
596 }
597 } else {
598 $controller->import_data( $module_data );
599 }
600
601 if ( 'security_tweaks' === $module ) {
602 if ( 'migration' !== $request_reason ) {
603 // There is some tweaks that require re-login. Not display error message during since 2.8.1 because it breaks the config flow on the Hub.
604 // If this is combined with mask login, then we need to redirect to new URL.
605 // The automate function should return this.
606 $tweak_class = wd_di()->get( Controller_Security_Tweaks::class );
607 $need_reauth = $tweak_class->automate( $module_data, $request_reason );
608 }
609 if ( ! empty( $module_data ) && isset( $module_data['notification'] ) ) {
610 $tweak_notification = new Tweak_Reminder();
611 if ( $tweak_notification->status !== $module_data['notification'] ) {
612 $tweak_notification->status = $module_data['notification'];
613 }
614
615 if ( isset( $module_data['notification_repeat'] ) ) {
616 // Temporary check for older versions.
617 if ( is_bool( $module_data['notification_repeat'] ) ) {
618 $tweak_notification->configs['reminder'] = $module_data['notification_repeat']
619 ? 'daily'
620 : 'weekly';
621 } elseif (
622 is_string( $module_data['notification_repeat'] )
623 && in_array( $module_data['notification_repeat'], array( 'daily', 'weekly', 'monthly' ), true )
624 ) {
625 $tweak_notification->configs['reminder'] = $module_data['notification_repeat'];
626 } else {
627 $tweak_notification->configs['reminder'] = 'weekly';
628 }
629 } else {
630 $tweak_notification->configs['reminder'] = 'weekly';
631 }
632 if ( ! empty( $module_data['subscribers'] ) ) {
633 // Reset all recipients before.
634 $tweak_notification->in_house_recipients = array();
635 $tweak_notification->out_house_recipients = array();
636 foreach ( $module_data['subscribers'] as $key => $subscribers ) {
637 $tweak_notification->$key = $subscribers;
638 }
639 }
640 if ( isset( $module_data['last_sent'] ) ) {
641 $tweak_notification->last_sent = $module_data['last_sent'];
642 }
643 $tweak_notification->save();
644 }
645 } elseif ( 'scan' === $module ) {
646 $scan_notification = new Malware_Notification();
647 $scan_report = new Malware_Report();
648 $scan_settings = new Model_Scan();
649 if ( ! empty( $module_data ) ) {
650 // For Scan notification.
651 if ( isset( $module_data['notification'] ) ) {
652 if ( $scan_notification->status !== $module_data['notification'] ) {
653 $scan_notification->status = $module_data['notification'];
654 }
655 if ( isset( $module_data['always_send_notification'] ) ) {
656 $scan_notification->configs['always_send'] = $module_data['always_send_notification'];
657 }
658 if ( isset( $module_data['error_send'] ) ) {
659 $scan_notification->configs['error_send'] = $module_data['error_send'];
660 }
661 if ( ! empty( $module_data['email_subject_issue_found'] ) ) {
662 $scan_notification->configs['template']['found']['subject'] = $module_data['email_subject_issue_found'];
663 }
664 if ( ! empty( $module_data['email_subject_issue_not_found'] ) ) {
665 $scan_notification->configs['template']['not_found']['subject'] = $module_data['email_subject_issue_not_found'];
666 }
667 if ( ! empty( $module_data['email_subject_error'] ) ) {
668 $scan_notification->configs['template']['error']['subject'] = $module_data['email_subject_error'];
669 }
670 if ( ! empty( $module_data['email_content_issue_found'] ) ) {
671 $scan_notification->configs['template']['found']['body'] = $module_data['email_content_issue_found'];
672 }
673 if ( ! empty( $module_data['email_content_issue_not_found'] ) ) {
674 $scan_notification->configs['template']['not_found']['body'] = $module_data['email_content_issue_not_found'];
675 }
676 if ( ! empty( $module_data['email_content_error'] ) ) {
677 $scan_notification->configs['template']['error']['body'] = $module_data['email_content_error'];
678 }
679 if ( ! empty( $module_data['notification_subscribers'] ) ) {
680 // Reset all recipients before.
681 $scan_notification->in_house_recipients = array();
682 $scan_notification->out_house_recipients = array();
683 foreach ( $module_data['notification_subscribers'] as $key => $subscribers ) {
684 $scan_notification->$key = $subscribers;
685 }
686 }
687 $scan_notification->save();
688 }
689 // For Scan report.
690 if (
691 $is_pro
692 && isset( $module_data['report'] )
693 && $scan_report->status !== $module_data['report']
694 ) {
695 $scan_report->status = $module_data['report'];
696 }
697 if ( isset( $module_data['always_send'] ) ) {
698 $scan_report->configs['always_send'] = $module_data['always_send'];
699 }
700 if ( ! empty( $module_data['report_subscribers'] ) ) {
701 // Reset all recipients before.
702 $scan_report->in_house_recipients = array();
703 $scan_report->out_house_recipients = array();
704 foreach ( $module_data['report_subscribers'] as $key => $subscribers ) {
705 $scan_report->$key = $subscribers;
706 }
707 }
708 // @since 2.7.0 Remove 'dry_run'-restoring.
709 // @since 2.7.0 scheduled values. Step#1 if 'report'-key exists.
710 if ( $is_pro && isset( $module_data['report'] ) ) {
711 $scan_settings->scheduled_scanning = 'enabled' === $module_data['report'];
712 }
713 // Step#2 if 'scheduled_scanning'-key exists.
714 if ( isset( $module_data['scheduled_scanning'] ) ) {
715 $scan_settings->scheduled_scanning = $module_data['scheduled_scanning'];
716 }
717 // We update the values in Model_Scan and Malware_Report so that the values are synchronized in both models.
718 if ( isset( $module_data['frequency'] ) ) {
719 $scan_settings->frequency = $module_data['frequency'];
720 $scan_report->frequency = $module_data['frequency'];
721 }
722 if ( isset( $module_data['day'] ) ) {
723 $scan_settings->day = $module_data['day'];
724 $scan_report->day = $module_data['day'];
725 }
726 if ( isset( $module_data['day_n'] ) ) {
727 $scan_settings->day_n = $module_data['day_n'];
728 $scan_report->day_n = $module_data['day_n'];
729 }
730 if ( isset( $module_data['time'] ) ) {
731 $scan_settings->time = $module_data['time'];
732 $scan_report->time = $module_data['time'];
733 }
734 $scan_report->save();
735 $scan_settings->save();
736 } else {
737 // Default data for scan notification.
738 $scan_notification->status = 'disabled';
739 // @since 2.7.0 We can remove it in the next version.
740 $scan_notification->dry_run = false;
741 $scan_notification->save();
742 // For scan report.
743 $scan_report->status = 'disabled';
744 // @since 2.7.0 We can remove it in the next version.
745 $scan_report->dry_run = false;
746 $scan_report->save();
747 // For scan settings.
748 $scan_settings->scheduled_scanning = false;
749 $scan_settings->frequency = 'weekly';
750 $scan_settings->day_n = '1';
751 $scan_settings->day = 'sunday';
752 $scan_settings->time = '4:00';
753 $scan_settings->save();
754 }
755 } elseif ( 'iplockout' === $module ) {
756 if ( ! empty( $module_data ) ) {
757 // Get string values for notification & report.
758 if ( isset( $module_data['notification'] ) ) {
759 $lockout_notification = new Firewall_Notification();
760 if ( $lockout_notification->status !== $module_data['notification'] ) {
761 $lockout_notification->status = $module_data['notification'];
762 }
763 if ( isset( $module_data['login_lockout_notification'] ) ) {
764 $lockout_notification->configs['login_lockout'] = $module_data['login_lockout_notification'];
765 }
766 if ( isset( $module_data['ip_lockout_notification'] ) ) {
767 $lockout_notification->configs['nf_lockout'] = $module_data['ip_lockout_notification'];
768 }
769 if ( isset( $module_data['cooldown_enabled'] ) ) {
770 $lockout_notification->configs['limit'] = $module_data['cooldown_enabled'];
771 }
772 if ( isset( $module_data['cooldown_number_lockout'] ) ) {
773 $lockout_notification->configs['threshold'] = $module_data['cooldown_number_lockout'];
774 }
775 if ( isset( $module_data['cooldown_period'] ) ) {
776 $lockout_notification->configs['cool_off'] = $module_data['cooldown_period'];
777 }
778 if ( ! empty( $module_data['notification_subscribers'] ) ) {
779 //Reset all recipients before
780 $lockout_notification->in_house_recipients = array();
781 $lockout_notification->out_house_recipients = array();
782 foreach ( $module_data['notification_subscribers'] as $key => $subscribers ) {
783 $lockout_notification->$key = $subscribers;
784 }
785 }
786
787 $lockout_notification->save();
788 }
789 if ( $is_pro && isset( $module_data['report'] ) ) {
790 $lockout_report = new Firewall_Report();
791 if ( $lockout_report->status !== $module_data['report'] ) {
792 $lockout_report->status = $module_data['report'];
793 }
794 if ( isset( $module_data['day'] ) ) {
795 $lockout_report->day = $module_data['day'];
796 }
797 if ( isset( $module_data['report_frequency'] ) ) {
798 $lockout_report->frequency = $module_data['report_frequency'];
799 }
800 if ( isset( $module_data['day_n'] ) ) {
801 $lockout_report->day_n = $module_data['day_n'];
802 }
803 if ( isset( $module_data['report_time'] ) ) {
804 $lockout_report->time = $module_data['report_time'];
805 }
806 if ( ! empty( $module_data['report_subscribers'] ) ) {
807 // Reset all recipients before.
808 $lockout_report->in_house_recipients = array();
809 $lockout_report->out_house_recipients = array();
810 foreach ( $module_data['report_subscribers'] as $key => $subscribers ) {
811 $lockout_report->$key = $subscribers;
812 }
813 }
814 if ( isset( $module_data['last_sent'] ) ) {
815 $lockout_report->last_sent = $module_data['last_sent'];
816 }
817 // @since 2.7.0 Remove 'dry_run'-restoring.
818 $lockout_report->save();
819 }
820 // For UA Banning.
821 if ( isset( $module_data['ua_banning_enabled'] ) ) {
822 $model_ua_lockout = new Model_Ua_Lockout();
823 $model_ua_lockout->enabled = (bool) $module_data['ua_banning_enabled'];
824 if ( isset( $module_data['ua_banning_message'] ) ) {
825 $model_ua_lockout->message = $module_data['ua_banning_message'];
826 }
827 if ( isset( $module_data['ua_banning_blacklist'] ) ) {
828 $model_ua_lockout->blacklist = $module_data['ua_banning_blacklist'];
829 }
830 if ( isset( $module_data['ua_banning_whitelist'] ) ) {
831 $model_ua_lockout->whitelist = $module_data['ua_banning_whitelist'];
832 }
833 if ( isset( $module_data['ua_banning_empty_headers'] ) ) {
834 $model_ua_lockout->empty_headers = (bool) $module_data['ua_banning_empty_headers'];
835 }
836 $model_ua_lockout->save();
837 }
838 } else {
839 // Default data for lockout notification.
840 $lockout_notification = new Firewall_Notification();
841 $lockout_notification->status = 'disabled';
842 // @since 2.7.0 We can remove it in the next version.
843 $lockout_notification->dry_run = false;
844 $lockout_notification->configs = array(
845 'login_lockout' => false,
846 'nf_lockout' => false,
847 'limit' => false,
848 'threshold' => 3,
849 'cool_off' => 24,
850 );
851 $lockout_notification->save();
852 // For lockout report.
853 $lockout_report = new Firewall_Report();
854 $lockout_report->status = 'disabled';
855 // @since 2.7.0 We can remove it in the next version.
856 $lockout_report->dry_run = false;
857 $lockout_report->frequency = 'weekly';
858 $lockout_report->day_n = '1';
859 $lockout_report->day = 'sunday';
860 $lockout_report->time = '4:00';
861 $lockout_report->save();
862 // For UA banning.
863 $model_ua_lockout = new Model_Ua_Lockout();
864 $default_ua_values = $model_ua_lockout->get_default_values();
865 $model_ua_lockout->enabled = false;
866 $model_ua_lockout->message = $default_ua_values['message'];
867 $model_ua_lockout->blacklist = $default_ua_values['blacklist'];
868 $model_ua_lockout->whitelist = $default_ua_values['whitelist'];
869 $model_ua_lockout->empty_headers = false;
870 $model_ua_lockout->save();
871 }
872 } elseif ( 'audit' === $module ) {
873 $audit_report = new Audit_Report();
874 if ( ! empty( $module_data ) ) {
875 if (
876 $is_pro
877 && isset( $module_data['report'] )
878 && $audit_report->status !== $module_data['report']
879 ) {
880 $audit_report->status = $module_data['report'];
881 }
882 if ( isset( $module_data['frequency'] ) ) {
883 $audit_report->frequency = $module_data['frequency'];
884 }
885 if ( isset( $module_data['day_n'] ) ) {
886 $audit_report->day_n = $module_data['day_n'];
887 }
888 if ( isset( $module_data['day'] ) ) {
889 $audit_report->day = $module_data['day'];
890 }
891 if ( isset( $module_data['time'] ) ) {
892 $audit_report->time = $module_data['time'];
893 }
894 if ( ! empty( $module_data['subscribers'] ) ) {
895 // Reset all recipients before.
896 $audit_report->in_house_recipients = array();
897 $audit_report->out_house_recipients = array();
898 foreach ( $module_data['subscribers'] as $key => $subscribers ) {
899 $audit_report->$key = $subscribers;
900 }
901 }
902 if ( isset( $module_data['last_sent'] ) ) {
903 $audit_report->last_sent = $module_data['last_sent'];
904 }
905 // @since 2.7.0 Remove 'dry_run'-restoring.
906 } else {
907 // Default data for audit settings.
908 $audit_settings = new Model_Audit_Logging();
909 $audit_settings->enabled = false;
910 $audit_settings->storage_days = '6 months';
911 $audit_settings->save();
912 // For audit report.
913 $audit_report->status = 'disabled';
914 // @since 2.7.0 We can remove it in the next version.
915 $audit_report->dry_run = false;
916 $audit_report->frequency = 'weekly';
917 $audit_report->day_n = '1';
918 $audit_report->day = 'sunday';
919 $audit_report->time = '4:00';
920 }
921
922 $audit_report->save();
923 /**
924 * If 'blocklist_monitor' module is activated, 'enabled' set as true.
925 */
926 } elseif (
927 'blocklist_monitor' === $module
928 && $is_pro
929 && isset( $module_data['status'] )
930 ) {
931 ( new Blocklist_Monitor() )->change_status( $module_data['status'] );
932 } elseif (
933 'pwned_passwords' === $module
934 && isset( $module_data['custom_message'] )
935 ) {
936 $model_pwned = new Model_Password_Protection();
937 $model_pwned->pwned_actions['force_change_message'] = $module_data['custom_message'];
938 $model_pwned->save();
939 }
940 }
941 }
942 // We should disable quick setup.
943 update_site_option( 'wp_defender_is_activated', 1 );
944
945 return $need_reauth;
946 }
947
948 /**
949 * Parse current data for import.
950 * @param null|array $configs
951 *
952 * @return array
953 */
954 public function parse_data_for_import( $configs = null ) {
955 if ( empty( $configs ) ) {
956 $configs = $this->gather_data();
957 }
958 $strings = array();
959 foreach ( $configs as $module => $module_data ) {
960 $controller = $this->module_to_controller( $module, false );
961 if ( ! is_object( $controller ) ) {
962 // In free, when audit not present.
963 $strings[ $module ][] = sprintf(
964 /* translators: %s: Html for Pro-tag. */
965 __( 'Inactive %s', 'wpdef' ),
966 '<span class="sui-tag sui-tag-pro">Pro</span>'
967 );
968 continue;
969 }
970 $strings[ $module ] = $controller->export_strings();
971 }
972
973 return array(
974 'configs' => $configs,
975 'strings' => $strings,
976 );
977 }
978
979 public function module_to_controller( $module, $all_firewall_lockouts = false ) {
980 switch ( $module ) {
981 case 'security_tweaks':
982 return new Controller_Security_Tweaks();
983 case 'scan':
984 return new Controller_Scan();
985 case 'audit':
986 if ( class_exists( Audit_Logging::class ) ) {
987 return new Audit_Logging();
988 }
989 break;
990 case 'iplockout':
991 if ( $all_firewall_lockouts ) {
992 return array(
993 new Controller_Firewall(),
994 new \WP_Defender\Controller\Login_Lockout(),
995 new \WP_Defender\Controller\Nf_Lockout(),
996 new \WP_Defender\Controller\Blacklist(),
997 new Controller_Ua_Lockout(),
998 );
999 } else {
1000 return new Controller_Firewall();
1001 }
1002 case 'settings':
1003 return new \WP_Defender\Controller\Main_Setting();
1004 case 'two_factor':
1005 return new Two_Factor();
1006 case 'mask_login':
1007 return new Controller_Mask_Login();
1008 case 'security_headers':
1009 return new \WP_Defender\Controller\Security_Headers();
1010 case 'blocklist_monitor':
1011 return new Blocklist_Monitor();
1012 case 'pwned_passwords':
1013 return new Controller_Password_Protection();
1014 default:
1015 break;
1016 }
1017 }
1018
1019 /**
1020 * @param array $configs
1021 *
1022 * @return bool
1023 */
1024 public function check_for_new_structure( $configs ) {
1025
1026 return array_key_exists( 'subscribers', $configs['security_tweaks'] );
1027 }
1028
1029 /**
1030 * @param array $data
1031 *
1032 * @return bool
1033 */
1034 public function verify_config_data( $data ) {
1035 if ( ! isset( $data['name'], $data['configs'], $data['strings'] )
1036 || empty( $data['name'] ) || empty( $data['strings'] )
1037 ) {
1038 return false;
1039 }
1040
1041 return true;
1042 }
1043
1044 /**
1045 * Import module 'strings'. Use during the import of configs and when upgrading the version.
1046 * @param array $data
1047 *
1048 * @return array
1049 */
1050 public function import_module_strings( $data ) {
1051 if ( empty( $data['strings'] ) ) {
1052
1053 return array();
1054 }
1055 $is_pro = ( new \WP_Defender\Behavior\WPMUDEV() )->is_pro();
1056 foreach ( $data['configs'] as $key => $config ) {
1057 if (
1058 'security_tweaks' === $key
1059 && 'enabled' === $config['notification']
1060 && 1 === (is_array($data['strings']['security_tweaks']) || $data['strings']['security_tweaks'] instanceof \Countable ? count( $data['strings']['security_tweaks'] ) : 0)
1061 ) {
1062 $data['strings']['security_tweaks'][] = __( 'Email notifications active', 'wpdef' );
1063 } elseif ( 'scan' === $key ) {
1064 $data['strings']['scan'] = wd_di()->get( Controller_Scan::class )->config_strings( $config, $is_pro );
1065 } elseif ( 'iplockout' === $key ) {
1066 $data['strings']['iplockout'] = wd_di()->get( Controller_Firewall::class )->config_strings( $config, $is_pro );
1067 } elseif ( 'audit' === $key ) {
1068 $data['strings']['audit'] = wd_di()->get( Audit_Logging::class )->config_strings( $config, $is_pro );
1069 } elseif ( 'blocklist_monitor' === $key ) {
1070 $data['strings']['blocklist_monitor'] = $this->format_blocklist_monitor_strings( $config, $is_pro );
1071 } elseif ( 'pwned_passwords' === $key ) {
1072 $data['strings']['pwned_passwords'] = wd_di()->get( Controller_Password_Protection::class )->config_strings( $config, $is_pro );
1073 }
1074 }
1075
1076 return $data['strings'];
1077 }
1078
1079 /**
1080 * Import module 'strings' for the Basic config.
1081 * @param array $configs
1082 * @param bool $is_pro
1083 *
1084 * @return array
1085 */
1086 public function create_default_module_strings( $configs, $is_pro ) {
1087 $strings = array();
1088 foreach ( $configs as $key => $config ) {
1089 if ( 'security_tweaks' === $key ) {
1090 $strings['security_tweaks'] = wd_di()->get( Controller_Security_Tweaks::class )->config_strings( $config, $is_pro );
1091 } elseif ( 'scan' === $key ) {
1092 $strings['scan'] = wd_di()->get( Controller_Scan::class )->config_strings( $config, $is_pro );
1093 } elseif ( 'iplockout' === $key ) {
1094 $strings['iplockout'] = wd_di()->get( Controller_Firewall::class )->config_strings( $config, $is_pro );
1095 } elseif ( 'audit' === $key ) {
1096 $strings['audit'] = wd_di()->get( Audit_Logging::class )->config_strings( $config, $is_pro );
1097 } elseif ( 'two_factor' === $key ) {
1098 $strings['two_factor'][] = __( 'Inactive', 'wpdef' );
1099 } elseif ( 'mask_login' === $key ) {
1100 $strings['mask_login'][] = __( 'Inactive', 'wpdef' );
1101 } elseif ( 'security_headers' === $key ) {
1102 $strings['security_headers'][] = __( 'Active', 'wpdef' );
1103 } elseif ( 'blocklist_monitor' === $key ) {
1104 $strings['blocklist_monitor'] = wd_di()->get( Blocklist_Monitor::class )->config_strings( $config, $is_pro );
1105 } elseif ( 'pwned_passwords' === $key ) {
1106 $strings['pwned_passwords'][] = __( 'Inactive', 'wpdef' );
1107 }
1108 }
1109
1110 return $strings;
1111 }
1112
1113 /**
1114 * @param string $key
1115 *
1116 * @return array
1117 */
1118 private function get_decoded_settings( $key ) {
1119 $data = get_site_option( $key );
1120 if ( $data && is_string( $data ) ) {
1121 $decoded_data = json_decode( $data, true );
1122 if ( is_array( $decoded_data ) ) {
1123 return $decoded_data;
1124 }
1125 }
1126
1127 return array();
1128 }
1129
1130 /**
1131 * Get settings of previous version.
1132 *
1133 * @return array
1134 */
1135 private function get_prev_settings() {
1136 $arr = [];
1137 if ( ( new \WP_Defender\Behavior\WPMUDEV() )->is_pro() ) {
1138 $status = (string) wd_di()->get( Blocklist_Monitor::class )->get_status();
1139 $arr['enabled'] = '1' === $status;
1140 $arr['status'] = $status;
1141 } else {
1142 $arr['enabled'] = false;
1143 }
1144
1145 return array(
1146 // Different keys.
1147 'security_tweaks' => $this->get_decoded_settings( 'wd_hardener_settings' ),
1148 'iplockout' => $this->get_decoded_settings( 'wd_lockdown_settings' ),
1149 // Identical keys.
1150 'scan' => $this->get_decoded_settings( 'wd_scan_settings' ),
1151 'audit' => $this->get_decoded_settings( 'wd_audit_settings' ),
1152 'two_factor' => $this->get_decoded_settings( 'wd_2auth_settings' ),
1153 'mask_login' => $this->get_decoded_settings( 'wd_masking_login_settings' ),
1154 'security_headers' => $this->get_decoded_settings( 'wd_security_headers_settings' ),
1155 'settings' => $this->get_decoded_settings( 'wd_main_settings' ),
1156 'blocklist_monitor' => $arr,
1157 );
1158 }
1159
1160 /**
1161 * @param string $module
1162 *
1163 * @return string
1164 */
1165 private function module_to_name( $module ) {
1166 switch ( $module ) {
1167 case 'security_tweaks':
1168 return __( 'Security Recommendations', 'wpdef' );
1169 case 'scan':
1170 return __( 'Malware Scanning', 'wpdef' );
1171 case 'audit':
1172 return __( 'Audit Logging', 'wpdef' );
1173 case 'iplockout':
1174 return __( 'Firewall', 'wpdef' );
1175 case 'settings':
1176 return __( 'Settings', 'wpdef' );
1177 case 'two_factor':
1178 return __( '2FA', 'wpdef' );
1179 case 'mask_login':
1180 return __( 'Mask Login Area', 'wpdef' );
1181 case 'security_headers':
1182 return __( 'Security Headers', 'wpdef' );
1183 case 'blocklist_monitor':
1184 return __( 'Blocklist Monitor', 'wpdef' );
1185 case 'pwned_passwords':
1186 return __( 'Pwned Passwords', 'wpdef' );
1187 default:
1188 return '';
1189 }
1190 }
1191
1192 /**
1193 * @param string $module
1194 * @return array|void
1195 */
1196 public function model_labels( $module ) {
1197 switch ( $module ) {
1198 case 'security_tweaks':
1199 return array_merge(
1200 ( new Model_Security_Tweaks() )->labels(),
1201 ( new Tweak_Reminder() )->labels()
1202 );
1203 case 'scan':
1204 return array_merge(
1205 ( new Model_Scan() )->labels(),
1206 ( new Malware_Notification() )->labels(),
1207 ( new Malware_Report() )->labels()
1208 );
1209 case 'audit':
1210 if ( class_exists( Model_Audit_Logging::class ) ) {
1211 return array_merge(
1212 ( new Model_Audit_Logging() )->labels(),
1213 ( new Audit_Report() )->labels()
1214 );
1215 }
1216 break;
1217 case 'iplockout':
1218 return array_merge(
1219 ( new Model_Firewall() )->labels(),
1220 ( new Model_Login_Lockout() )->labels(),
1221 ( new Model_Notfound_Lockout() )->labels(),
1222 ( new Model_Blacklist_Lockout() )->labels(),
1223 ( new Firewall_Notification() )->labels(),
1224 ( new Firewall_Report() )->labels(),
1225 ( new Model_Ua_Lockout() )->labels()
1226 );
1227 case 'settings':
1228 return ( new Model_Main_Setting() )->labels();
1229 case 'two_factor':
1230 return ( new Model_Two_Fa() )->labels();
1231 case 'mask_login':
1232 return ( new Model_Mask_Login() )->labels();
1233 case 'security_headers':
1234 return ( new Model_Security_Headers() )->labels();
1235 case 'blocklist_monitor':
1236 // Separate method is not for this model.
1237 return ( new Blocklist_Monitor() )->labels();
1238 case 'pwned_passwords':
1239 return ( new Model_Password_Protection() )->labels();
1240 default:
1241 break;
1242 }
1243 }
1244
1245 /**
1246 * @return array
1247 */
1248 public function parse_data_for_hub() {
1249 $configs = $this->gather_data();
1250 $labels = array();
1251 $strings = array();
1252 foreach ( $configs as $module => $module_data ) {
1253 $model_labels = $this->model_labels( $module );
1254
1255 if ( is_array( $module_data ) && is_array( $model_labels ) ) {
1256 $labels[ $module ]['name'] = $this->module_to_name( $module );
1257 foreach ( $module_data as $key => $value ) {
1258 // Todo: update logic to import/export whitelisted/blocklisted countries via maxmind_license_key.
1259 if ( in_array( $key, array( 'geoIP_db', 'geodb_path' ), true ) ) {
1260 continue;
1261 }
1262
1263 if ( array_key_exists( $key, $model_labels ) ) {
1264 $labels[ $module ]['value'][ $key ] = array(
1265 'name' => $model_labels[ $key ],
1266 'value' => $value,
1267 );
1268 }
1269 }
1270
1271 $controller = $this->module_to_controller( $module, false );
1272 if ( ! is_object( $controller ) ) {
1273 // In free, when audit not present.
1274 $strings[ $module ] = sprintf(
1275 /* translators: %s: Html for Pro-tag. */
1276 __( 'Inactive %s', 'wpdef' ),
1277 '<span class="sui-tag sui-tag-pro">Pro</span>'
1278 );
1279 continue;
1280 }
1281 $strings[ $module ] = $controller->export_strings();
1282 }
1283 }
1284
1285 return array(
1286 'configs' => $configs,
1287 'labels' => $labels,
1288 'strings' => $strings,
1289 );
1290 }
1291
1292 /**
1293 * Format strings of Blocklist Monitor config.
1294 *
1295 * @param array $config Saved config.
1296 * @param bool $is_pro User membership status.
1297 *
1298 * @return array
1299 */
1300 private function format_blocklist_monitor_strings( $config, $is_pro ) {
1301 // If Blocklist Monitor is enable.
1302 if ( isset( $config['status'] ) && '1' === (string) $config['status'] ) {
1303 if ( $is_pro ) {
1304 $monitor = array( __( 'Active', 'wpdef' ) );
1305 } else {
1306 $monitor = array(
1307 sprintf(
1308 /* translators: Status of blocklist monitor */
1309 __( 'Active %s', 'wpdef' ),
1310 '<span class="sui-tag sui-tag-pro">Pro</span>'
1311 ),
1312 );
1313 }
1314 } else {
1315 if ( $is_pro ) {
1316 $monitor = array( __( 'Inactive', 'wpdef' ) );
1317 } else {
1318 $monitor = array(
1319 sprintf(
1320 /* translators: Status of blocklist monitor */
1321 __( 'Inactive %s', 'wpdef' ),
1322 '<span class="sui-tag sui-tag-pro">Pro</span>'
1323 ),
1324 );
1325 }
1326 }
1327
1328 return $monitor;
1329 }
1330
1331 /**
1332 * Generate labels from config.
1333 *
1334 * @param $configs
1335 *
1336 * @return array
1337 */
1338 public function prepare_config_labels( $configs ) {
1339 $labels = array();
1340
1341 foreach ( $configs as $module => $module_data ) {
1342 $model_labels = $this->model_labels( $module );
1343
1344 if ( is_array( $module_data ) && is_array( $model_labels ) ) {
1345 $labels[ $module ]['name'] = $this->module_to_name( $module );
1346 foreach ( $module_data as $key => $value ) {
1347 // Todo: update logic to import/export whitelisted/blocklisted countries via maxmind_license_key.
1348 if ( in_array( $key, array( 'geoIP_db', 'geodb_path' ), true ) ) {
1349 continue;
1350 }
1351
1352 if ( array_key_exists( $key, $model_labels ) ) {
1353 $labels[ $module ]['value'][ $key ] = array(
1354 'name' => $model_labels[ $key ],
1355 'value' => $value,
1356 );
1357 }
1358 }
1359 }
1360 }
1361
1362 return $labels;
1363 }
1364
1365 /**
1366 * User enumeration options enabled list.
1367 *
1368 * @return array Return array of enabled user enumeration options.
1369 */
1370 private function get_enabled_user_enums() {
1371 $prevent_enum_users = new Prevent_Enum_Users();
1372 return $prevent_enum_users->get_enabled_user_enums();
1373 }
1374
1375 /**
1376 * Security key all option.
1377 *
1378 * @return mixed All value of the "secuirty key" feature of security tweak.
1379 */
1380 private function get_security_key_all_options() {
1381 $security_key = new Security_Key();
1382 return $security_key->get_all_option();
1383 }
1384 }
1385