PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.4.0
Defender Security – Malware Scanner, Login Security & Firewall v5.4.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.4.0, at src/controller/class-global-ip.php

295 lines 7.4 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 }
75
76 /**
77 * Save settings.
78 *
79 * @param Request $request The request object containing new settings data.
80 *
81 * @return Response
82 * @defender_route
83 */
84 public function save_settings( Request $request ) {
85 $data = $request->get_data(
86 array(
87 'enabled' => array( 'type' => 'bool' ),
88 'allow_self_unlock' => array( 'type' => 'bool' ),
89 'blocklist_autosync' => array( 'type' => 'bool' ),
90 // Temporary property.
91 'module_title' => array(
92 'type' => 'string',
93 'sanitize' => 'sanitize_text_field',
94 ),
95 // End.
96 )
97 );
98
99 $message_type = 'central_ip';
100 // Split messages.
101 if ( isset( $data['module_title'] ) && 'antibot' === $data['module_title'] ) {
102 $message_type = 'antibot';
103 unset( $data['module_title'] );
104 }
105
106 $old_enabled = (bool) $this->model->enabled;
107 $old_self_unlock = $this->model->allow_self_unlock;
108
109 $this->model->import( $data );
110 if ( $this->model->validate() ) {
111 $this->model->save();
112 Config_Hub_Helper::set_clear_active_flag();
113 if ( 'central_ip' === $message_type ) {
114 $message = $this->get_update_message( $data, $old_enabled, Global_Ip_Lockout::get_module_name() );
115 } else {
116 $message = '';
117 if ( ! empty( $data['allow_self_unlock'] ) ) {
118 $message = esc_html__( 'Temporary self unlock CAPTCHA challenge is enabled successfully.', 'defender-security' );
119 } elseif ( ! empty( $old_self_unlock ) && empty( $data['allow_self_unlock'] ) ) {
120 $message = esc_html__( 'Temporary self unlock CAPTCHA challenge is disabled successfully.', 'defender-security' );
121 }
122 }
123
124 return new Response(
125 true,
126 array_merge(
127 array(
128 'message' => $message,
129 'auto_close' => true,
130 ),
131 $this->data_frontend()
132 )
133 );
134 }
135
136 return new Response(
137 false,
138 array( 'message' => $this->model->get_formatted_errors() )
139 );
140 }
141
142 /**
143 * Enqueues scripts and styles for this page.
144 * Only enqueues assets if the page is active.
145 */
146 public function enqueue_assets() {
147 if ( $this->is_page_active() ) {
148 wp_localize_script( 'def-iplockout', 'global_ip', $this->data_frontend() );
149 }
150 }
151
152 /**
153 * Provides data for the frontend.
154 *
155 * @return array An array of data for the frontend.
156 */
157 public function data_frontend(): array {
158 return array_merge(
159 array(
160 'model' => $this->model->export(),
161 'misc' => array(
162 'show_global_ips_disable' => $this->wpmudev->is_disabled_hub_option(),
163 'is_wpmu_dev_admin' => $this->wpmudev->is_wpmu_dev_admin(),
164 'module_name' => Global_Ip_Lockout::get_module_name(),
165 'text_to_connect' => sprintf(
166 /* translators: %s: Module name. */
167 esc_html__( 'Connect to a WPMU DEV account to activate %s.', 'defender-security' ),
168 Global_Ip_Lockout::get_module_name()
169 ),
170 'is_show_dashboard_notice' => $this->service->is_show_dashboard_notice(),
171 ),
172 'hub' => array(
173 'global_ip_list' => $this->service->get_formated_global_ip_list(),
174 'global_ip_setting_url' => $this->wpmudev->get_api_base_url() . 'hub2/ip-banning',
175 ),
176 ),
177 $this->dump_routes_and_nonces()
178 );
179 }
180
181 /**
182 * Fetch Global IP list from HUB.
183 *
184 * @return void
185 * @since 3.4.0
186 */
187 public function fetch_global_ip_list(): void {
188 if ( true === $this->model->enabled ) {
189 $this->service->fetch_global_ip_list();
190 }
191 }
192
193 /**
194 * Refresh Global IP list.
195 *
196 * @param Request $request The request object.
197 *
198 * @return Response
199 * @defender_route
200 * @since 3.4.0
201 */
202 public function refresh_global_ip_list( Request $request ): Response {
203 $data = $this->service->fetch_global_ip_list();
204
205 if ( ! is_wp_error( $data ) ) {
206 return new Response(
207 true,
208 array(
209 'message' => esc_html__(
210 'The Custom IP List has been updated successfully.',
211 'defender-security'
212 ),
213 'global_ip_list' => $this->service->get_formated_global_ip_list(),
214 )
215 );
216 } else {
217 return new Response(
218 false,
219 array(
220 'message' => esc_html__(
221 'An error occurred while synchronizing the Custom IP List.',
222 'defender-security'
223 ),
224 )
225 );
226 }
227 }
228
229 /**
230 * Add an IP to blacklist.
231 *
232 * @param string $ip The IP to blacklist.
233 *
234 * @return void
235 */
236 public function blacklist_an_ip( string $ip ): void {
237 $data = array(
238 'block_list' => array( $ip ),
239 );
240 $this->service->add_to_global_ip_list( $data );
241 }
242
243 /**
244 * Converts the current object state to an array.
245 *
246 * @return array The array representation of the object.
247 */
248 public function to_array(): array {
249 return array();
250 }
251
252 /**
253 * Imports data into the model.
254 *
255 * @param array $data Data to be imported into the model.
256 */
257 public function import_data( array $data ) {
258 $model = $this->model;
259 if ( isset( $data['global_ip_list'] ) ) {
260 $model->enabled = (bool) $data['global_ip_list'];
261 if ( isset( $data['global_ip_list_blocklist_autosync'] ) ) {
262 $model->blocklist_autosync = (bool) $data['global_ip_list_blocklist_autosync'];
263 }
264 } else {
265 $model->enabled = false;
266 $model->blocklist_autosync = false;
267 }
268 $model->save();
269 }
270
271 /**
272 * Removes settings for all submodules.
273 */
274 public function remove_settings() {
275 }
276
277 /**
278 * Delete all the data & the cache.
279 */
280 public function remove_data() {
281 delete_site_transient( Global_IP_Component::LIST_KEY );
282 delete_site_option( Global_IP_Component::LIST_LAST_SYNCED_KEY );
283 $this->service->delete_dashboard_notice_reminder();
284 }
285
286 /**
287 * Exports strings.
288 *
289 * @return array An array of strings.
290 */
291 public function export_strings() {
292 return array();
293 }
294 }
295