PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.5.0
Defender Security – Malware Scanner, Login Security & Firewall v5.5.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-global-ip.php

class-global-ip.php in Defender Security – Malware Scanner, Login Security & Firewall 5.5.0, at src/controller/class-global-ip.php

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