PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.1
Defender Security – Malware Scanner, Login Security & Firewall v6.2.1
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 6.2.1, at src/controller/class-login-lockout.php

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