| 1 |
<?php |
| 2 |
/** |
| 3 |
* It handles functionalities such as saving settings, fetching and syncing global IP lists from the HUB. |
| 4 |
* |
| 5 |
* @package WP_Defender\Controller |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Controller; |
| 9 |
|
| 10 |
use WP_Defender\Controller; |
| 11 |
use Calotes\Component\Request; |
| 12 |
use Calotes\Component\Response; |
| 13 |
use WP_Defender\Traits\Setting; |
| 14 |
use WP_Defender\Traits\Formats; |
| 15 |
use WP_Defender\Behavior\WPMUDEV; |
| 16 |
use WP_Defender\Component\Network_Cron_Manager; |
| 17 |
use WP_Defender\Model\Setting\Global_Ip_Lockout; |
| 18 |
use WP_Defender\Component\Config\Config_Hub_Helper; |
| 19 |
use WP_Defender\Component\IP\Global_IP as Global_IP_Component; |
| 20 |
|
| 21 |
/** |
| 22 |
* Handles global IP lockout settings. |
| 23 |
*/ |
| 24 |
class Global_Ip extends Controller { |
| 25 |
|
| 26 |
use Setting; |
| 27 |
use Formats; |
| 28 |
|
| 29 |
/** |
| 30 |
* The slug identifier for this controller. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $slug = 'wdf-ip-lockout'; |
| 35 |
|
| 36 |
/** |
| 37 |
* The model for handling the data. |
| 38 |
* |
| 39 |
* @var Global_Ip_Lockout |
| 40 |
*/ |
| 41 |
protected $model; |
| 42 |
|
| 43 |
/** |
| 44 |
* Service for handling logic. |
| 45 |
* |
| 46 |
* @var Global_IP_Component |
| 47 |
*/ |
| 48 |
protected $service; |
| 49 |
|
| 50 |
/** |
| 51 |
* The WPMUDEV instance used for interacting with WPMUDEV services. |
| 52 |
* |
| 53 |
* @var WPMUDEV |
| 54 |
*/ |
| 55 |
private $wpmudev; |
| 56 |
|
| 57 |
/** |
| 58 |
* Initializes the model and service, registers routes, and sets up scheduled events if the model is active. |
| 59 |
*/ |
| 60 |
public function __construct() { |
| 61 |
$this->register_routes(); |
| 62 |
add_action( 'defender_enqueue_assets', array( $this, 'enqueue_assets' ) ); |
| 63 |
$this->model = wd_di()->get( Global_Ip_Lockout::class ); |
| 64 |
$this->service = wd_di()->get( Global_IP_Component::class ); |
| 65 |
$this->wpmudev = wd_di()->get( WPMUDEV::class ); |
| 66 |
|
| 67 |
/** |
| 68 |
* Network Cron Manager |
| 69 |
* |
| 70 |
* @var Network_Cron_Manager $network_cron_manager |
| 71 |
*/ |
| 72 |
$network_cron_manager = wd_di()->get( Network_Cron_Manager::class ); |
| 73 |
$network_cron_manager->register_callback( |
| 74 |
'wpdef_fetch_global_ip_list', |
| 75 |
array( $this, 'fetch_global_ip_list' ), |
| 76 |
HOUR_IN_SECONDS |
| 77 |
); |
| 78 |
|
| 79 |
if ( $this->service->can_blocklist_autosync() ) { |
| 80 |
// No need to run Rate mechanism for IP lockouts because we do it in Blacklist class. |
| 81 |
add_action( 'wd_blacklist_this_ip', array( $this, 'blacklist_an_ip' ) ); |
| 82 |
} |
| 83 |
add_action( 'init', array( $this->service, 'handle_expired_membership' ) ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Save settings. |
| 88 |
* |
| 89 |
* @param Request $request The request object containing new settings data. |
| 90 |
* |
| 91 |
* @return Response |
| 92 |
* @defender_route |
| 93 |
*/ |
| 94 |
public function save_settings( Request $request ) { |
| 95 |
$data = $request->get_data( |
| 96 |
array( |
| 97 |
'enabled' => array( 'type' => 'bool' ), |
| 98 |
'allow_self_unlock' => array( 'type' => 'bool' ), |
| 99 |
'blocklist_autosync' => array( 'type' => 'bool' ), |
| 100 |
// Temporary property. |
| 101 |
'module_title' => array( |
| 102 |
'type' => 'string', |
| 103 |
'sanitize' => 'sanitize_text_field', |
| 104 |
), |
| 105 |
// End. |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
$message_type = 'central_ip'; |
| 110 |
// Split messages. |
| 111 |
if ( isset( $data['module_title'] ) && 'antibot' === $data['module_title'] ) { |
| 112 |
$message_type = 'antibot'; |
| 113 |
unset( $data['module_title'] ); |
| 114 |
} |
| 115 |
|
| 116 |
$old_enabled = (bool) $this->model->enabled; |
| 117 |
$old_self_unlock = $this->model->allow_self_unlock; |
| 118 |
|
| 119 |
$this->model->import( $data ); |
| 120 |
if ( $this->model->validate() ) { |
| 121 |
$this->model->save(); |
| 122 |
Config_Hub_Helper::set_clear_active_flag(); |
| 123 |
if ( 'central_ip' === $message_type ) { |
| 124 |
$message = $this->get_update_message( $data, $old_enabled, Global_Ip_Lockout::get_module_name() ); |
| 125 |
} else { |
| 126 |
$message = ''; |
| 127 |
if ( ! empty( $data['allow_self_unlock'] ) ) { |
| 128 |
$message = esc_html__( 'Temporary self unlock CAPTCHA challenge is enabled successfully.', 'defender-security' ); |
| 129 |
} elseif ( ! empty( $old_self_unlock ) && empty( $data['allow_self_unlock'] ) ) { |
| 130 |
$message = esc_html__( 'Temporary self unlock CAPTCHA challenge is disabled successfully.', 'defender-security' ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return new Response( |
| 135 |
true, |
| 136 |
array_merge( |
| 137 |
array( |
| 138 |
'message' => $message, |
| 139 |
'auto_close' => true, |
| 140 |
), |
| 141 |
$this->data_frontend() |
| 142 |
) |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
return new Response( |
| 147 |
false, |
| 148 |
array( 'message' => $this->model->get_formatted_errors() ) |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Enqueues scripts and styles for this page. |
| 154 |
* Only enqueues assets if the page is active. |
| 155 |
*/ |
| 156 |
public function enqueue_assets() { |
| 157 |
if ( $this->is_page_active() ) { |
| 158 |
wp_localize_script( 'def-iplockout', 'global_ip', $this->data_frontend() ); |
| 159 |
} |
| 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 |
return array_merge( |
| 169 |
array( |
| 170 |
'model' => $this->model->export(), |
| 171 |
'misc' => array( |
| 172 |
'show_global_ips_disable' => $this->wpmudev->is_disabled_hub_option(), |
| 173 |
'is_wpmu_dev_admin' => $this->wpmudev->is_wpmu_dev_admin(), |
| 174 |
'module_name' => Global_Ip_Lockout::get_module_name(), |
| 175 |
'text_to_connect' => sprintf( |
| 176 |
/* translators: %s: Module name. */ |
| 177 |
esc_html__( 'Connect to a WPMU DEV account to activate %s.', 'defender-security' ), |
| 178 |
Global_Ip_Lockout::get_module_name() |
| 179 |
), |
| 180 |
'is_show_dashboard_notice' => $this->service->is_show_dashboard_notice(), |
| 181 |
'current_plan' => $this->service->get_membership_type(), |
| 182 |
'is_expired_membership' => $this->service->is_expired_membership_type(), |
| 183 |
), |
| 184 |
'hub' => array( |
| 185 |
'global_ip_list' => $this->service->get_formated_global_ip_list(), |
| 186 |
'global_ip_setting_url' => $this->wpmudev->get_api_base_url() . 'hub2/ip-banning', |
| 187 |
), |
| 188 |
), |
| 189 |
$this->dump_routes_and_nonces() |
| 190 |
); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Fetch Global IP list from HUB. |
| 195 |
* |
| 196 |
* @return void |
| 197 |
* @since 3.4.0 |
| 198 |
*/ |
| 199 |
public function fetch_global_ip_list(): void { |
| 200 |
if ( true === $this->model->enabled ) { |
| 201 |
$this->service->fetch_global_ip_list(); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Refresh Global IP list. |
| 207 |
* |
| 208 |
* @return Response |
| 209 |
* @defender_route |
| 210 |
* @since 3.4.0 |
| 211 |
*/ |
| 212 |
public function refresh_global_ip_list(): Response { |
| 213 |
$data = $this->service->fetch_global_ip_list(); |
| 214 |
|
| 215 |
if ( ! is_wp_error( $data ) ) { |
| 216 |
return new Response( |
| 217 |
true, |
| 218 |
array( |
| 219 |
'message' => esc_html__( |
| 220 |
'The Custom IP List has been updated successfully.', |
| 221 |
'defender-security' |
| 222 |
), |
| 223 |
'global_ip_list' => $this->service->get_formated_global_ip_list(), |
| 224 |
) |
| 225 |
); |
| 226 |
} else { |
| 227 |
return new Response( |
| 228 |
false, |
| 229 |
array( |
| 230 |
'message' => esc_html__( |
| 231 |
'An error occurred while synchronizing the Custom IP List.', |
| 232 |
'defender-security' |
| 233 |
), |
| 234 |
) |
| 235 |
); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Add an IP to blacklist. |
| 241 |
* |
| 242 |
* @param string $ip The IP to blacklist. |
| 243 |
* |
| 244 |
* @return void |
| 245 |
*/ |
| 246 |
public function blacklist_an_ip( string $ip ): void { |
| 247 |
$data = array( |
| 248 |
'block_list' => array( $ip ), |
| 249 |
); |
| 250 |
$this->service->add_to_global_ip_list( $data ); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Converts the current object state to an array. |
| 255 |
* |
| 256 |
* @return array The array representation of the object. |
| 257 |
*/ |
| 258 |
public function to_array(): array { |
| 259 |
return array(); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Imports data into the model. |
| 264 |
* |
| 265 |
* @param array $data Data to be imported into the model. |
| 266 |
*/ |
| 267 |
public function import_data( array $data ) { |
| 268 |
$model = $this->model; |
| 269 |
if ( isset( $data['global_ip_list'] ) ) { |
| 270 |
$model->enabled = (bool) $data['global_ip_list']; |
| 271 |
if ( isset( $data['global_ip_list_blocklist_autosync'] ) ) { |
| 272 |
$model->blocklist_autosync = (bool) $data['global_ip_list_blocklist_autosync']; |
| 273 |
} |
| 274 |
} else { |
| 275 |
$model->enabled = false; |
| 276 |
$model->blocklist_autosync = false; |
| 277 |
} |
| 278 |
$model->save(); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Removes settings for all submodules. |
| 283 |
*/ |
| 284 |
public function remove_settings() { |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Delete all the data & the cache. |
| 289 |
*/ |
| 290 |
public function remove_data() { |
| 291 |
delete_site_transient( Global_IP_Component::LIST_KEY ); |
| 292 |
delete_site_option( Global_IP_Component::LIST_LAST_SYNCED_KEY ); |
| 293 |
$this->service->delete_dashboard_notice_reminder(); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Exports strings. |
| 298 |
* |
| 299 |
* @return array An array of strings. |
| 300 |
*/ |
| 301 |
public function export_strings() { |
| 302 |
return array(); |
| 303 |
} |
| 304 |
} |
| 305 |
|