PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.0
Defender Security – Malware Scanner, Login Security & Firewall v6.2.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-hub-connector.php

class-hub-connector.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.0, at src/controller/class-hub-connector.php

260 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles Hub Connector feature.
4 *
5 * @package WP_Defender\Controller
6 */
7
8 namespace WP_Defender\Controller;
9
10 use Calotes\Component\Response;
11 use WP_Defender\Controller;
12 use WP_Defender\Component\Hub_Connector as Hub_Connector_Component;
13 use WP_Defender\Model\Setting\Antibot_Global_Firewall_Setting;
14 use WP_Defender\Traits\Defender_Dashboard_Client;
15
16 /**
17 * Handles Hub Connector feature.
18 */
19 class Hub_Connector extends Controller {
20 use Defender_Dashboard_Client;
21
22 public const TRANSIENT_KEY = 'wpdef_maybe_hub_connection_attempt';
23
24 /**
25 * Service for handling logic.
26 *
27 * @var Hub_Connector_Component
28 */
29 public $service;
30
31 /**
32 * Constructor for the Hub_Connector class.
33 *
34 * @param Hub_Connector_Component $service The service object.
35 */
36 public function __construct( Hub_Connector_Component $service ) {
37 $this->service = $service;
38 $this->register_routes();
39
40 add_action( 'admin_init', array( $this, 'maybe_hcm_connection_attempt' ) );
41 add_action( 'wp_ajax_wpdef_check_hub_sync_status', array( $this, 'ajax_check_hub_sync_status' ) );
42 add_action( 'wpmudev_hub_connector_first_sync_completed', array( $this, 'clear_syncing_state' ) );
43 }
44
45 /**
46 * Maybe member is trying to connect via Hub Connection Module.
47 */
48 public function maybe_hcm_connection_attempt() {
49 $module_slug = defender_get_data_from_request( 'module_slug', 'g' );
50 $is_callback = defender_get_data_from_request( 'hub_connector_callback', 'g' );
51
52 if ( is_string( $module_slug ) && '' !== $module_slug && is_string( $is_callback ) && '' !== $is_callback ) {
53 set_site_transient(
54 self::TRANSIENT_KEY,
55 array(
56 'module_slug' => $module_slug,
57 ),
58 MINUTE_IN_SECONDS
59 );
60 }
61 }
62
63 /**
64 * Disconnect site from Hub.
65 *
66 * @return Response
67 * @defender_route
68 */
69 public function disconnect_site(): Response {
70 $this->logout();
71
72 return new Response(
73 true,
74 array(
75 'message' => esc_html__( 'Site disconnected successfully!', 'defender-security' ),
76 'auto_close' => true,
77 )
78 );
79 }
80
81 /**
82 * Activate Dashboard plugin.
83 *
84 * @return Response
85 * @defender_route
86 */
87 public function activate_dashboard_plugin(): Response {
88 $plugin_path = 'wpmudev-updates/update-notifications.php';
89 $result = activate_plugin( $plugin_path );
90 if ( is_wp_error( $result ) ) {
91 return new Response(
92 false,
93 array(
94 'message' => $result->get_error_message(),
95 )
96 );
97 }
98
99 return new Response(
100 true,
101 array(
102 'message' => esc_html__( 'Dashboard plugin has been activated. You should be redirected to sign-in page shortly.', 'defender-security' ),
103 'redirect' => $this->service->get_url(),
104 )
105 );
106 }
107
108 /**
109 * AJAX handler to check Hub sync status.
110 *
111 * @return void
112 */
113 public function ajax_check_hub_sync_status() {
114 check_ajax_referer( 'auth_nonce', 'nonce' );
115
116 if ( ! current_user_can( 'manage_options' ) ) {
117 wp_send_json_error( array( 'message' => esc_html__( 'Unauthorized', 'defender-security' ) ), 403 );
118 }
119
120 $sync = $this->service->sync();
121 if ( is_wp_error( $sync ) && 'not_logged_in' !== $sync->get_error_code() ) {
122 $this->clear_syncing_state();
123
124 wp_send_json_error(
125 array(
126 'is_synced' => false,
127 'error_code' => $sync->get_error_code(),
128 'error_message' => $sync->get_error_message(),
129 ),
130 403
131 );
132 }
133
134 $is_logged_in = Hub_Connector_Component::is_logged_in();
135 $this->clear_syncing_state();
136
137 if ( $is_logged_in ) {
138 // Lets feature controllers auto-enable themselves via the module_slug transient.
139 do_action( 'wpdef_hub_connector_synced' );
140 }
141
142 wp_send_json_success(
143 array(
144 'is_synced' => $is_logged_in,
145 )
146 );
147 }
148
149 /**
150 * Clears the temporary Hub syncing state.
151 *
152 * @return void
153 */
154 public function clear_syncing_state(): void {
155 delete_site_transient( Hub_Connector_Component::SYNCING_TRANSIENT_KEY );
156 }
157
158 /**
159 * Provides data for the frontend.
160 *
161 * @return array An array of data for the frontend.
162 */
163 public function data_frontend(): array {
164 // Current admin page slug, used to build callbacks that return to the originating page.
165 $current_page = sanitize_text_field( wp_unslash( $_GET['page'] ?? 'wp-defender' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
166 $is_hub_connector_logged_in = Hub_Connector_Component::is_logged_in();
167
168 return array_merge(
169 array(
170 'button_label' => $this->service->get_button_label(),
171 'is_dash_installed' => $this->is_dash_installed(),
172 'is_dash_activated' => $this->is_dash_activated(),
173 'dash_url' => add_query_arg( 'page', 'wpmudev', is_multisite() ? network_admin_url( 'admin.php' ) : admin_url( 'admin.php' ) ),
174 'is_hub_connected' => $this->is_site_connected_to_hub_via_hcm_or_dash(),
175 'is_logged_in' => $is_hub_connector_logged_in || Hub_Connector_Component::is_wpmudev_dashboard_connected(),
176 'is_hub_connector_logged_in' => $is_hub_connector_logged_in,
177 'is_dashboard_connected' => Hub_Connector_Component::is_wpmudev_dashboard_connected(),
178 'has_login_error' => Hub_Connector_Component::has_error_in_login(),
179 'login_error_msg' => Hub_Connector_Component::get_auth_error(),
180 'hub_auth_url' => Hub_Connector_Component::get_hub_google_login_url(),
181 'redirect_url' => Hub_Connector_Component::get_hub_connector_callback_url( $current_page ),
182 'domain' => Hub_Connector_Component::get_site_domain(),
183 'auth_nonce' => wp_create_nonce( 'auth_nonce' ),
184 'login_auth_url' => Hub_Connector_Component::get_hub_site_login_auth_url(),
185 'hub_signup_url' => Hub_Connector_Component::get_hub_register_url( $current_page ),
186 'forgot_password_url' => Hub_Connector_Component::get_hub_forgot_password_url(),
187 'is_syncing' => Hub_Connector_Component::is_syncing(),
188 'logout_url' => rest_url( 'hub-connector/v1/logout' ),
189 'logout_nonce' => wp_create_nonce( 'wp_rest' ),
190 'hub_connector_url' => array(
191 'default' => $this->service->get_url(),
192 'global-ip' => $this->service->get_url( 'wdf-ip-lockout', 'global-ip' ),
193 'blocklist' => $this->service->get_url( 'wdf-ip-lockout', 'blocklist' ),
194 'onboard' => $this->service->get_url( 'wp-defender', 'onboard' ),
195 'dashboard' => $this->service->get_url( 'wp-defender', 'dashboard' ),
196 'setting' => $this->service->get_url( 'wdf-setting', 'general' ),
197 // Settings > Tools redirect for scan-item HC features (Known Vuln, Suspicious Code).
198 'settings-redirect' => $this->service->get_url( 'wdf-setting', 'tools' ),
199 // Settings > Tools redirect; module slug auto-enables Blocklist Monitor.
200 'settings-blocklist' => $this->service->get_url( 'wdf-setting', 'tools', '', '', 'blocklist-monitor' ),
201 // Scan page redirect for the Safe Repair HC feature.
202 'scan' => $this->service->get_url( 'wdf-scan' ),
203 // Global Firewall redirect; module slug auto-enables AntiBot.
204 'global-firewall' => $this->service->get_url( 'wdf-ip-lockout', 'global-ip', '', '', Antibot_Global_Firewall_Setting::MODULE_SLUG ),
205 // Custom Rules redirect; module slug auto-enables Custom IP list sync.
206 'custom-rules' => $this->service->get_url( 'wdf-ip-lockout', 'custom-rules', '', '', Global_Ip::MODULE_SLUG ),
207 // Custom one if a trigger is the Summary section.
208 'summary-box' => $this->service->get_url( 'wdf-ip-lockout', 'summary-box' ),
209 // Custom one for Antibot notice shown on all Defender pages.
210 'antibot-notice' => $this->service->get_url( 'wdf-ip-lockout', '', 'def_antibot_survey_notice', 'hub_connector' ),
211 // Signup URLs embed post_sync_redirect so the callback lands on the current page, then redirects to the target.
212 'settings-redirect-signup' => Hub_Connector_Component::get_hub_register_url( $current_page, $this->service->get_url( 'wdf-setting', 'tools' ) ),
213 'settings-blocklist-signup' => Hub_Connector_Component::get_hub_register_url( $current_page, $this->service->get_url( 'wdf-setting', 'tools', '', '', 'blocklist-monitor' ) ),
214 'scan-signup' => Hub_Connector_Component::get_hub_register_url( $current_page, $this->service->get_url( 'wdf-scan' ) ),
215 'global-firewall-signup' => Hub_Connector_Component::get_hub_register_url( $current_page, $this->service->get_url( 'wdf-ip-lockout', 'global-ip', '', '', Antibot_Global_Firewall_Setting::MODULE_SLUG ) ),
216 'custom-rules-signup' => Hub_Connector_Component::get_hub_register_url( $current_page, $this->service->get_url( 'wdf-ip-lockout', 'custom-rules', '', '', Global_Ip::MODULE_SLUG ) ),
217 ),
218 ),
219 $this->dump_routes_and_nonces()
220 );
221 }
222
223 /**
224 * Export to array
225 *
226 * @return array
227 */
228 public function to_array() {
229 return array();
230 }
231
232 /**
233 * Import data
234 *
235 * @param array $data The data to import.
236 */
237 public function import_data( $data ) {}
238
239 /**
240 * Export strings
241 *
242 * @return array
243 */
244 public function export_strings() {
245 return array();
246 }
247
248 /**
249 * Removes settings.
250 */
251 public function remove_settings() {
252 }
253
254 /**
255 * Delete all the data & the cache.
256 */
257 public function remove_data() {
258 }
259 }
260