| @@ -7,20 +7,18 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace WP_Defender\Traits; |
| 9 | 9 | |
| 10 | 10 | use WP_Error; |
| 11 | -use DateTime; | |
| 12 | 11 | use Exception; |
| 13 | 12 | use WP_User_Query; |
| 14 | 13 | use WPMUDEV_Dashboard; |
| 15 | 14 | use WP_Defender\Model\Scan; |
| 16 | -use WP_Defender\Model\Audit_Log; | |
| 15 | +use WPMUDEV\Hub\Connector\Data; | |
| 17 | 16 | use WP_Defender\Model\Notification; |
| 18 | 17 | use WP_Defender\Controller\Firewall; |
| 19 | -use WP_Defender\Component\Quarantine; | |
| 20 | 18 | use WP_Defender\Model\Setting\Two_Fa; |
| 21 | 19 | use WP_Defender\Component\IP\Antibot_Global_Firewall; |
| 22 | -use WP_Defender\Model\Setting\Recaptcha; | |
| 20 | +use WP_Defender\Model\Setting\Captcha; | |
| 23 | 21 | use WP_Defender\Model\Setting\Mask_Login; |
| 24 | 22 | use WP_Defender\Controller\Security_Tweaks; |
| 25 | 23 | use WP_Defender\Controller\Security_Headers; |
| 26 | 24 | use WP_Defender\Model\Setting\Audit_Logging; |
| @@ -65,25 +63,16 @@ | ||
| 65 | 63 | case self::API_SCAN_KNOWN_VULN: |
| 66 | 64 | return $base . 'api/defender/v1/vulnerabilities'; |
| 67 | 65 | case self::API_SCAN_SIGNATURE: |
| 68 | 66 | return $base . 'api/defender/v1/yara-signatures'; |
| 69 | - case self::API_AUDIT: | |
| 70 | - // This is from another endpoint. | |
| 71 | - $base = defined( 'WPMUDEV_CUSTOM_AUDIT_SERVER' ) | |
| 72 | - ? constant( 'WPMUDEV_CUSTOM_AUDIT_SERVER' ) | |
| 73 | - : 'https://audit.wpmudev.org/'; | |
| 74 | - | |
| 75 | - return $base . 'logs'; | |
| 76 | - case self::API_AUDIT_ADD: | |
| 77 | - $base = defined( 'WPMUDEV_CUSTOM_AUDIT_SERVER' ) | |
| 78 | - ? constant( 'WPMUDEV_CUSTOM_AUDIT_SERVER' ) | |
| 79 | - : 'https://audit.wpmudev.org/'; | |
| 80 | - | |
| 81 | - return $base . 'logs/add_multiple'; | |
| 82 | 67 | case self::API_BLACKLIST: |
| 83 | 68 | return $base . 'api/defender/v1/blacklist-monitoring?domain=' . network_site_url(); |
| 84 | 69 | case self::API_GLOBAL_IP_LIST: |
| 85 | 70 | return $base . 'api/hub/v1/global-ip-list'; |
| 71 | + case self::API_GLOBAL_IP_LIST_HOSTING: | |
| 72 | + $site_id = $this->get_site_id(); | |
| 73 | + | |
| 74 | + return $base . "api/hub/v1/sites/$site_id/modules/hosting/global-ip-list"; | |
| 86 | 75 | case self::API_PACKAGE_CONFIGS: |
| 87 | 76 | return $base . 'api/hub/v1/package-configs'; |
| 88 | 77 | case self::API_IP_BLOCKLIST_SUBMIT_LOGS: |
| 89 | 78 | return $base . 'api/blocklist/v1/logs'; |
| @@ -102,12 +91,20 @@ | ||
| 102 | 91 | * |
| 103 | 92 | * @return int|bool |
| 104 | 93 | */ |
| 105 | 94 | public function get_site_id() { |
| 106 | - if ( false !== $this->get_apikey() ) { | |
| 95 | + if ( 'dashboard' === $this->resolve_connection_mode() ) { | |
| 107 | 96 | return (int) WPMUDEV_Dashboard::$api->get_site_id(); |
| 108 | 97 | } |
| 109 | 98 | |
| 99 | + // Ignore stale HCM site IDs after disconnect. | |
| 100 | + if ( self::get_hcm_status() && class_exists( 'WPMUDEV\Hub\Connector\Data' ) ) { | |
| 101 | + $site_id = Data::get()->hub_site_id(); | |
| 102 | + if ( $site_id > 0 ) { | |
| 103 | + return $site_id; | |
| 104 | + } | |
| 105 | + } | |
| 106 | + | |
| 110 | 107 | return false; |
| 111 | 108 | } |
| 112 | 109 | |
| 113 | 110 | /** |
| @@ -126,9 +123,29 @@ | ||
| 126 | 123 | array $body = array(), |
| 127 | 124 | array $args = array(), |
| 128 | 125 | bool $recheck = false |
| 129 | 126 | ) { |
| 130 | - $api_key = $this->get_api_key(); | |
| 127 | + $api_key = $this->get_api_key(); | |
| 128 | + if ( '' === $api_key ) { | |
| 129 | + $link_text = sprintf( | |
| 130 | + '<a target="_blank" href="%s">%s</a>', | |
| 131 | + 'https://wpmudev.com/project/wpmu-dev-dashboard/', | |
| 132 | + esc_html__( 'here', 'defender-security' ) | |
| 133 | + ); | |
| 134 | + | |
| 135 | + return new WP_Error( | |
| 136 | + 'dashboard_required', | |
| 137 | + sprintf( | |
| 138 | + /* translators: %s - wpmudev link */ | |
| 139 | + esc_html__( | |
| 140 | + 'WPMU DEV Dashboard will be required for this action. Please visit %s and install the WPMU DEV Dashboard.', | |
| 141 | + 'defender-security' | |
| 142 | + ), | |
| 143 | + $link_text | |
| 144 | + ) | |
| 145 | + ); | |
| 146 | + } | |
| 147 | + | |
| 131 | 148 | $body['domain'] ??= network_site_url(); |
| 132 | 149 | |
| 133 | 150 | $headers = array( |
| 134 | 151 | 'Authorization' => 'Basic ' . $api_key, |
| @@ -160,15 +177,18 @@ | ||
| 160 | 177 | return $request; |
| 161 | 178 | } |
| 162 | 179 | } |
| 163 | 180 | |
| 164 | - $result = wp_remote_retrieve_body( $request ); | |
| 165 | - $result = json_decode( $result, true ); | |
| 181 | + $response_code = wp_remote_retrieve_response_code( $request ); | |
| 182 | + $result = wp_remote_retrieve_body( $request ); | |
| 183 | + $result = json_decode( $result, true ); | |
| 166 | 184 | |
| 167 | - if ( 200 !== wp_remote_retrieve_response_code( $request ) ) { | |
| 185 | + if ( $response_code < 200 || $response_code >= 300 ) { | |
| 168 | 186 | return new WP_Error( |
| 169 | - wp_remote_retrieve_response_code( $request ), | |
| 170 | - $result['message'] ?? wp_remote_retrieve_response_message( $request ) | |
| 187 | + $response_code, | |
| 188 | + isset( $result['message'] ) | |
| 189 | + ? $result['message'] | |
| 190 | + : wp_remote_retrieve_response_message( $request ) | |
| 171 | 191 | ); |
| 172 | 192 | } |
| 173 | 193 | |
| 174 | 194 | return $result; |
| @@ -189,30 +209,9 @@ | ||
| 189 | 209 | array $body = array(), |
| 190 | 210 | array $args = array(), |
| 191 | 211 | bool $recheck = false |
| 192 | 212 | ) { |
| 193 | - $api_key = $this->get_api_key(); | |
| 194 | - | |
| 195 | - if ( '' === $api_key ) { | |
| 196 | - $link_text = sprintf( | |
| 197 | - '<a target="_blank" href="%s">%s</a>', | |
| 198 | - 'https://wpmudev.com/project/wpmu-dev-dashboard/', | |
| 199 | - esc_html__( 'here', 'defender-security' ) | |
| 200 | - ); | |
| 201 | - | |
| 202 | - return new WP_Error( | |
| 203 | - 'dashboard_required', | |
| 204 | - sprintf( | |
| 205 | - /* translators: %s - wpmudev link */ | |
| 206 | - esc_html__( | |
| 207 | - 'WPMU DEV Dashboard will be required for this action. Please visit %s and install the WPMU DEV Dashboard.', | |
| 208 | - 'defender-security' | |
| 209 | - ), | |
| 210 | - $link_text | |
| 211 | - ) | |
| 212 | - ); | |
| 213 | - } | |
| 214 | - | |
| 213 | + // Checking the validity of API key inside the method. | |
| 215 | 214 | return $this->hub_api_request( $scenario, $body, $args, $recheck ); |
| 216 | 215 | } |
| 217 | 216 | |
| 218 | 217 | /** |
| @@ -304,9 +303,9 @@ | ||
| 304 | 303 | 'warning' => $total_issues, |
| 305 | 304 | 'scan_result' => $scan_result, |
| 306 | 305 | 'scan_schedule' => array( |
| 307 | 306 | // @since 2.7.0 change scheduled scan logic. |
| 308 | - 'is_activated' => $settings->scheduled_scanning, | |
| 307 | + 'is_activated' => $settings->is_enabled_scheduled_scanning(), | |
| 309 | 308 | // Example of frequency, day, time in build_notification_hub_data() method. |
| 310 | 309 | 'time' => $settings->time, |
| 311 | 310 | 'day' => $this->get_notification_day( $settings ), |
| 312 | 311 | 'frequency' => $this->backward_frequency_compatibility( $settings->frequency ), |
| @@ -372,33 +371,17 @@ | ||
| 372 | 371 | } |
| 373 | 372 | |
| 374 | 373 | /** |
| 375 | 374 | * Builds an array of audit data to be sent to the hub. |
| 375 | + * Audit log is Pro-only; returns empty stub by default. | |
| 376 | 376 | * |
| 377 | - * @return array An array containing the number of audit log entries, the timestamp of the | |
| 378 | - * last audit log entry, and a boolean indicating if audit logging is enabled. | |
| 377 | + * @return array | |
| 379 | 378 | */ |
| 380 | 379 | public function build_audit_hub_data(): array { |
| 381 | - $date_from = ( new DateTime( wp_date( 'Y-m-d', strtotime( '-30 days' ) ) ) )->setTime( | |
| 382 | - 0, | |
| 383 | - 0, | |
| 384 | - 0 | |
| 385 | - )->getTimestamp(); | |
| 386 | - $date_to = ( new DateTime( wp_date( 'Y-m-d' ) ) )->setTime( 23, 59, 59 )->getTimestamp(); | |
| 387 | - $month_count = Audit_Log::count( $date_from, $date_to ); | |
| 388 | - $last = Audit_Log::get_last(); | |
| 389 | - if ( is_object( $last ) ) { | |
| 390 | - $last = wp_date( 'Y-m-d g:i a', $last->timestamp ); | |
| 391 | - } else { | |
| 392 | - $last = 'n/a'; | |
| 393 | - } | |
| 394 | - | |
| 395 | - $settings = new Audit_Logging(); | |
| 396 | - | |
| 397 | 380 | return array( |
| 398 | - 'month' => $month_count, | |
| 399 | - 'last_event' => $last, | |
| 400 | - 'enabled' => $settings->is_active(), | |
| 381 | + 'month' => 0, | |
| 382 | + 'last_event' => 'n/a', | |
| 383 | + 'enabled' => false, | |
| 401 | 384 | ); |
| 402 | 385 | } |
| 403 | 386 | |
| 404 | 387 | /** |
| @@ -475,14 +458,14 @@ | ||
| 475 | 458 | ); |
| 476 | 459 | } |
| 477 | 460 | |
| 478 | 461 | /** |
| 479 | - * Builds an array of security headers data to be sent to the hub. | |
| 462 | + * Builds an array of Captcha data to be sent to the hub. | |
| 480 | 463 | * |
| 481 | 464 | * @return array |
| 482 | 465 | */ |
| 483 | - public function build_recaptcha_hub_data(): array { | |
| 484 | - $settings = new Recaptcha(); | |
| 466 | + public function build_captcha_hub_data(): array { | |
| 467 | + $settings = new Captcha(); | |
| 485 | 468 | |
| 486 | 469 | return array( |
| 487 | 470 | 'active' => $settings->is_active(), |
| 488 | 471 | ); |
| @@ -580,26 +563,9 @@ | ||
| 580 | 563 | * |
| 581 | 564 | * @return array |
| 582 | 565 | */ |
| 583 | 566 | public function build_firewall_notification_hub_data(): array { |
| 584 | - $firewall_notification = new Firewall_Notification(); | |
| 585 | - if ( 'enabled' === $firewall_notification->status ) { | |
| 586 | - $login_lockout = $firewall_notification->configs['login_lockout']; | |
| 587 | - $nf_lockout = $firewall_notification->configs['nf_lockout']; | |
| 588 | - $ua_lockout = $firewall_notification->configs['ua_lockout'] ?? false; | |
| 589 | - } else { | |
| 590 | - $login_lockout = false; | |
| 591 | - $nf_lockout = false; | |
| 592 | - $ua_lockout = false; | |
| 593 | - } | |
| 594 | - | |
| 595 | - return array( | |
| 596 | - 'firewall' => array( | |
| 597 | - 'login_lockout' => $login_lockout, | |
| 598 | - '404_lockout' => $nf_lockout, | |
| 599 | - 'ua_lockout' => $ua_lockout, | |
| 600 | - ), | |
| 601 | - ); | |
| 567 | + return array( 'firewall' => ( new Firewall_Notification() )->get_hub_data() ); | |
| 602 | 568 | } |
| 603 | 569 | |
| 604 | 570 | /** |
| 605 | 571 | * Build security headers hub data. |
| @@ -627,9 +593,9 @@ | ||
| 627 | 593 | $firewall_data = $this->build_lockout_hub_data(); |
| 628 | 594 | $two_fa = $this->build_2fa_hub_data(); |
| 629 | 595 | $mask_login = $this->build_mask_login_hub_data(); |
| 630 | 596 | $sec_headers = $this->build_security_headers_hub_data(); |
| 631 | - $recaptcha = $this->build_recaptcha_hub_data(); | |
| 597 | + $captcha = $this->build_captcha_hub_data(); | |
| 632 | 598 | $pwned_password = $this->build_password_protection_hub_data(); |
| 633 | 599 | $quarantined_files = $this->build_quarantined_files_hub_data(); |
| 634 | 600 | |
| 635 | 601 | return array( |
| @@ -696,10 +662,11 @@ | ||
| 696 | 662 | 'security_headers' => array( |
| 697 | 663 | 'active' => $sec_headers['active'], |
| 698 | 664 | 'inactive' => $sec_headers['inactive'], |
| 699 | 665 | ), |
| 666 | + // We'll change 'google_recaptcha'-key to 'captcha' in the future. | |
| 700 | 667 | 'google_recaptcha' => array( |
| 701 | - 'active' => $recaptcha['active'], | |
| 668 | + 'active' => $captcha['active'], | |
| 702 | 669 | ), |
| 703 | 670 | 'password_protection' => array( |
| 704 | 671 | 'active' => $pwned_password['active'], |
| 705 | 672 | ), |
| @@ -704,9 +671,9 @@ | ||
| 704 | 671 | 'active' => $pwned_password['active'], |
| 705 | 672 | ), |
| 706 | 673 | ), |
| 707 | 674 | 'reports' => $this->build_notification_hub_data(), |
| 708 | - 'notifications' => $this->build_firewall_notification_hub_data(), | |
| 675 | + 'notifications' => array( 'firewall' => ( new Firewall_Notification() )->get_hub_data() ), | |
| 709 | 676 | 'quarantined_files' => $quarantined_files, |
| 710 | 677 | ) |
| 711 | 678 | ), |
| 712 | 679 | ); |
| @@ -762,9 +729,9 @@ | ||
| 762 | 729 | if ( ! class_exists( 'WP_Defender\Component\Quarantine' ) ) { |
| 763 | 730 | return array(); |
| 764 | 731 | } |
| 765 | 732 | |
| 766 | - return wd_di()->get( Quarantine::class )->hub_list(); | |
| 733 | + return wd_di()->get( \WP_Defender\Component\Quarantine::class )->hub_list(); | |
| 767 | 734 | } |
| 768 | 735 | |
| 769 | 736 | /** |
| 770 | 737 | * Schedule Hub Synchronization event. |