PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.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 / login-lockout.php

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

174 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WP_Defender\Controller;
4
5 use Calotes\Component\Request;
6 use Calotes\Component\Response;
7 use WP_Defender\Component\Blacklist_Lockout;
8 use WP_Defender\Component\Config\Config_Hub_Helper;
9 use WP_Defender\Controller;
10 use WP_Defender\Traits\IP;
11 use WP_Defender\Traits\Setting;
12
13 /**
14 * Class Login_Lockout
15 * @package WP_Defender\Controller
16 */
17 class Login_Lockout extends Controller {
18 use IP, Setting;
19
20 /**
21 * @var string
22 */
23 protected $slug = 'wdf-ip-lockout';
24
25 /**
26 * @var \WP_Defender\Component\Login_Lockout
27 */
28 protected $service;
29
30 /**
31 * @var \WP_Defender\Model\Setting\Login_Lockout
32 */
33 protected $model;
34
35 /**
36 * @var string
37 */
38 protected $module_name;
39
40 public function __construct() {
41 $this->register_routes();
42 $this->module_name = __( 'Login Protection', 'wpdef' );
43 add_action( 'defender_enqueue_assets', array( &$this, 'enqueue_assets' ) );
44 $this->model = wd_di()->get( \WP_Defender\Model\Setting\Login_Lockout::class );
45 $this->service = wd_di()->get( \WP_Defender\Component\Login_Lockout::class );
46 $service = wd_di()->get( Blacklist_Lockout::class );
47 if ( $this->model->enabled && ! $service->is_ip_whitelisted( $this->get_user_ip() ) ) {
48 $this->service->add_hooks();
49 }
50 }
51
52 /**
53 * @defender_route
54 */
55 public function save_settings( Request $request ) {
56 $data = $request->get_data_by_model( $this->model );
57 $old_enabled = (bool) $this->model->enabled;
58
59 $this->model->import( $data );
60 if ( $this->model->validate() ) {
61 $this->model->save();
62 Config_Hub_Helper::set_clear_active_flag();
63
64 return new Response( true, array_merge( [
65 'message' => $this->get_update_message( $data, $old_enabled, $this->module_name ),
66 ], $this->data_frontend() ) );
67 }
68
69 return new Response( false, [
70 'message' => $this->model->get_formatted_errors()
71 ] );
72 }
73
74 /**
75 * Queue assets and require data
76 */
77 public function enqueue_assets() {
78 if ( ! $this->is_page_active() ) {
79 return;
80 }
81 wp_localize_script( 'def-iplockout', 'login_lockout', $this->data_frontend() );
82 }
83
84 /**
85 * All the variables that we will show on frontend, both in the main page, or dashboard widget
86 *
87 * @return array
88 */
89 public function data_frontend() {
90
91 return array_merge( [
92 'model' => $this->model->export(),
93 'misc' => [
94 'host' => defender_get_hostname()
95 ],
96 'module_name' => $this->module_name,
97 ], $this->dump_routes_and_nonces() );
98 }
99
100 /**
101 * Export the data of this module, we will use this for export to HUB, create a preset etc.
102 */
103 public function to_array() {}
104
105 private function adapt_data( $data ) {
106 $adapted_data = array();
107 if ( isset( $data['login_protection'] ) ) {
108 $adapted_data['enabled'] = $data['login_protection'];
109 }
110 if ( isset( $data['login_protection_login_attempt'] ) ) {
111 $adapted_data['attempt'] = $data['login_protection_login_attempt'];
112 }
113 if ( isset( $data['login_protection_lockout_timeframe'] ) ) {
114 $adapted_data['timeframe'] = $data['login_protection_lockout_timeframe'];
115 }
116 if ( isset( $data['login_protection_lockout_duration'] ) ) {
117 $adapted_data['duration'] = $data['login_protection_lockout_duration'];
118 }
119 if ( isset( $data['login_protection_lockout_duration_unit'] ) ) {
120 $adapted_data['duration_unit'] = $data['login_protection_lockout_duration_unit'];
121 }
122 if ( isset( $data['login_protection_lockout_ban'] ) ) {
123 $adapted_data['lockout_type'] = 'permanent' === $data['login_protection_lockout_ban'] ? 'permanent' : 'timeframe';
124 }
125 if ( isset( $data['login_protection_lockout_message'] ) ) {
126 $adapted_data['lockout_message'] = $data['login_protection_lockout_message'];
127 }
128 if ( isset( $data['username_blacklist'] ) ) {
129 $adapted_data['username_blacklist'] = $data['username_blacklist'];
130 }
131
132 return array_merge( $data, $adapted_data );
133 }
134
135 /**
136 * Import the data of other source into this, it can be when HUB trigger the import, or user apply a preset.
137 * @param array $data
138 *
139 * @return mixed
140 */
141 public function import_data( $data ) {
142 if ( ! empty( $data ) ) {
143 //Upgrade for old versions
144 $data = $this->adapt_data( $data );
145 } else {
146
147 return;
148 }
149
150 $model = $this->model;
151 $model->import( $data );
152 if ( $model->validate() ) {
153 $model->save();
154 }
155 }
156
157 /**
158 * Remove all settings, configs generated in this container runtime.
159 */
160 public function remove_settings() {}
161
162 /**
163 * Remove all data.
164 */
165 public function remove_data() {}
166
167 /**
168 * @return array
169 */
170 public function export_strings() {
171 return [];
172 }
173 }
174