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-login-lockout.php

class-login-lockout.php in Defender Security – Malware Scanner, Login Security & Firewall 5.4.0, at src/controller/class-login-lockout.php

226 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles login lockout.
4 *
5 * @package WP_Defender\Controller
6 */
7
8 namespace WP_Defender\Controller;
9
10 use WP_Defender\Event;
11 use WP_Defender\Traits\IP;
12 use Calotes\Component\Request;
13 use Calotes\Component\Response;
14 use WP_Defender\Traits\Setting;
15 use WP_Defender\Component\Blacklist_Lockout;
16 use WP_Defender\Component\Config\Config_Hub_Helper;
17 use WP_Defender\Model\Setting\Login_Lockout as Model_Login_Lockout;
18
19 /**
20 * Handles login lockout.
21 */
22 class Login_Lockout extends Event {
23
24 use IP;
25 use Setting;
26
27 /**
28 * The slug identifier for this controller.
29 *
30 * @var string
31 */
32 public $slug = 'wdf-ip-lockout';
33
34 /**
35 * Service for handling logic.
36 *
37 * @var \WP_Defender\Component\Login_Lockout
38 */
39 protected $service;
40
41 /**
42 * The model for handling the data.
43 *
44 * @var Model_Login_Lockout
45 */
46 protected $model;
47
48 /**
49 * Initializes the model and service, registers routes, and sets up scheduled events if the model is active.
50 */
51 public function __construct() {
52 $this->register_routes();
53 add_action( 'defender_enqueue_assets', array( $this, 'enqueue_assets' ) );
54 $this->model = wd_di()->get( Model_Login_Lockout::class );
55 $this->service = wd_di()->get( \WP_Defender\Component\Login_Lockout::class );
56 $service = wd_di()->get( Blacklist_Lockout::class );
57 $ip = $this->get_user_ip();
58 if ( $this->model->enabled && ! $service->are_ips_whitelisted( $ip ) ) {
59 $this->service->add_hooks();
60 }
61 }
62
63 /**
64 * Save settings.
65 *
66 * @param Request $request The request object containing new settings data.
67 *
68 * @return Response
69 * @defender_route
70 */
71 public function save_settings( Request $request ) {
72 $data = $request->get_data_by_model( $this->model );
73 $old_enabled = (bool) $this->model->enabled;
74 $prev_data = $this->model->export();
75
76 $this->model->import( $data );
77 if ( $this->model->validate() ) {
78 $this->model->save();
79 Config_Hub_Helper::set_clear_active_flag();
80 // Maybe track.
81 if ( ! defender_is_wp_cli() && $this->is_feature_state_changed( $prev_data, $data ) ) {
82 $track_data = array(
83 'Action' => $data['enabled'] ? 'Enabled' : 'Disabled',
84 'Duration' => 'timeframe' === $data['lockout_type'] ? 'Temporary' : 'Permanent',
85 );
86 $this->track_feature( 'def_login_protection', $track_data );
87 }
88
89 return new Response(
90 true,
91 array_merge(
92 array(
93 'message' => $this->get_update_message(
94 $data,
95 $old_enabled,
96 Model_Login_Lockout::get_module_name()
97 ),
98 'auto_close' => true,
99 ),
100 $this->data_frontend()
101 )
102 );
103 }
104
105 return new Response(
106 false,
107 array(
108 'message' => $this->model->get_formatted_errors(),
109 )
110 );
111 }
112
113 /**
114 * Enqueues scripts and styles for this page.
115 * Only enqueues assets if the page is active.
116 */
117 public function enqueue_assets() {
118 if ( ! $this->is_page_active() ) {
119 return;
120 }
121 wp_localize_script( 'def-iplockout', 'login_lockout', $this->data_frontend() );
122 }
123
124 /**
125 * Provides data for the frontend.
126 *
127 * @return array An array of data for the frontend.
128 */
129 public function data_frontend(): array {
130 return array_merge(
131 array(
132 'model' => $this->model->export(),
133 'misc' => array(
134 'host' => defender_get_hostname(),
135 'module_name' => Model_Login_Lockout::get_module_name(),
136 ),
137 ),
138 $this->dump_routes_and_nonces()
139 );
140 }
141
142 /**
143 * Converts the current object state to an array.
144 *
145 * @return array The array representation of the object.
146 */
147 public function to_array(): array {
148 return array();
149 }
150
151 /**
152 * Adapt the given data array by adding additional fields if necessary.
153 *
154 * @param array $data The data array to adapt.
155 *
156 * @return array The adapted data array.
157 */
158 private function adapt_data( array $data ): array {
159 $adapted_data = array();
160 if ( isset( $data['login_protection'] ) ) {
161 $adapted_data['enabled'] = $data['login_protection'];
162 }
163 if ( isset( $data['login_protection_login_attempt'] ) ) {
164 $adapted_data['attempt'] = $data['login_protection_login_attempt'];
165 }
166 if ( isset( $data['login_protection_lockout_timeframe'] ) ) {
167 $adapted_data['timeframe'] = $data['login_protection_lockout_timeframe'];
168 }
169 if ( isset( $data['login_protection_lockout_duration'] ) ) {
170 $adapted_data['duration'] = $data['login_protection_lockout_duration'];
171 }
172 if ( isset( $data['login_protection_lockout_duration_unit'] ) ) {
173 $adapted_data['duration_unit'] = $data['login_protection_lockout_duration_unit'];
174 }
175 if ( isset( $data['login_protection_lockout_ban'] ) ) {
176 $adapted_data['lockout_type'] = 'permanent' === $data['login_protection_lockout_ban'] ? 'permanent' : 'timeframe';
177 }
178 if ( isset( $data['login_protection_lockout_message'] ) ) {
179 $adapted_data['lockout_message'] = $data['login_protection_lockout_message'];
180 }
181 if ( isset( $data['username_blacklist'] ) ) {
182 $adapted_data['username_blacklist'] = $data['username_blacklist'];
183 }
184
185 return array_merge( $data, $adapted_data );
186 }
187
188 /**
189 * Imports data into the model.
190 *
191 * @param array $data Data to be imported into the model.
192 */
193 public function import_data( array $data ) {
194 if ( ! empty( $data ) ) {
195 // Upgrade for old versions.
196 $data = $this->adapt_data( $data );
197 $model = $this->model;
198 $model->import( $data );
199 if ( $model->validate() ) {
200 $model->save();
201 }
202 }
203 }
204
205 /**
206 * Removes settings for all submodules.
207 */
208 public function remove_settings() {
209 }
210
211 /**
212 * Delete all the data & the cache.
213 */
214 public function remove_data() {
215 }
216
217 /**
218 * Exports strings.
219 *
220 * @return array An array of strings.
221 */
222 public function export_strings() {
223 return array();
224 }
225 }
226