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