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-strong-password.php

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

236 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle strong password module.
4 *
5 * @package WP_Defender\Controller
6 */
7
8 namespace WP_Defender\Controller;
9
10 use WP_Defender\Event;
11 use Calotes\Component\Request;
12 use Calotes\Component\Response;
13 use WP_Defender\Component\Config\Config_Hub_Helper;
14 use WP_Defender\Component\Strong_Password as Service;
15 use WP_Defender\Model\Setting\Strong_Password as Settings;
16
17 /**
18 * Handle strong password module.
19 */
20 class Strong_Password extends Event {
21
22 /**
23 * The model for the strong password module.
24 *
25 * @var Settings|null
26 */
27 protected ?Settings $model = null;
28
29 /**
30 * The service for the strong password module.
31 *
32 * @var Service|null
33 */
34 protected ?Service $service = null;
35
36 /**
37 * Helper instance for reuse methods from pwned password protection.
38 *
39 * @var Password_Protection|null
40 */
41 private ?Password_Protection $helper;
42
43 /**
44 * Initializes the model and service, registers routes, and sets up scheduled events if the model is active.
45 */
46 public function __construct() {
47 $this->model = wd_di()->get( Settings::class );
48 $this->service = wd_di()->get( Service::class );
49 $this->helper = wd_di()->get( Password_Protection::class );
50 add_filter( 'wp_defender_advanced_tools_data', array( $this, 'script_data' ) );
51 $this->register_routes();
52 if ( $this->model->is_active() ) {
53 // Update site url on sub-site when MaskLogin is disabled.
54 if (
55 is_multisite() && ! is_main_site()
56 && ! wd_di()->get( \WP_Defender\Model\Setting\Mask_Login::class )->is_active()
57 ) {
58 add_filter( 'network_site_url', array( $this->helper, 'filter_site_url' ), 100, 2 );
59 }
60 remove_action( 'user_profile_update_errors', 'slt_fsp_validate_profile_update', 0 );
61 remove_action( 'resetpass_form', 'slt_fsp_validate_resetpass_form', 10 );
62 if ( is_admin() ) {
63 add_action( 'admin_enqueue_scripts', array( $this->service, 'scripts' ) );
64 add_action( 'user_profile_update_errors', array( $this->service, 'on_profile_update' ), 100, 3 );
65 }
66 add_action( 'login_enqueue_scripts', array( $this->service, 'scripts' ) );
67 add_action( 'validate_password_reset', array( $this->service, 'on_password_reset' ), 100, 2 );
68 add_action( 'wp_authenticate_user', array( $this->service, 'during_authentication' ), 100, 2 );
69 add_filter( 'random_password', array( $this->service, 'generate_password' ), 10, 2 );
70 }
71 }
72
73 /**
74 * Provide data to the frontend via localized script.
75 *
76 * @param array $data Data collection is ready to passed.
77 *
78 * @return array Modified data array with added this controller data.
79 */
80 public function script_data( array $data ): array {
81 $data['strong_password'] = $this->data_frontend();
82
83 return $data;
84 }
85
86 /**
87 * All the variables that we will show on frontend, both in the main page, or dashboard widget.
88 *
89 * @return array
90 */
91 public function data_frontend(): array {
92 return array_merge(
93 array(
94 'is_active' => $this->model->is_active(),
95 'model' => $this->model->export(),
96 'all_roles' => wp_list_pluck( get_editable_roles(), 'name' ),
97 ),
98 $this->dump_routes_and_nonces()
99 );
100 }
101
102 /**
103 * Save settings.
104 *
105 * @param Request $request The request object containing new settings data.
106 *
107 * @return Response
108 * @defender_route
109 */
110 public function save_settings( Request $request ): Response {
111 $model_data = $request->get_data_by_model( $this->model );
112 $this->model->import( $model_data );
113 if ( $this->model->validate() ) {
114 $this->model->save();
115 Config_Hub_Helper::set_clear_active_flag();
116
117 $response = array(
118 'auto_close' => true,
119 );
120 if ( $this->model->enabled && empty( $this->model->user_roles ) ) {
121 // we need to control this message in front.
122 $response['warning'] = sprintf(
123 /* translators: 1. Open tag. 2. Close tag. */
124 esc_html__( 'You need to check at least one of the %1$sStrong Passwords checks preferences below%2$s and save your settings to enable Strong Password Protection.', 'defender-security' ),
125 '<b>',
126 '</b>'
127 );
128
129 return new Response( true, array_merge( $response, $this->data_frontend() ) );
130 }
131
132 if ( $this->maybe_track() ) {
133 $prev_data = $this->model->get_old_settings();
134 if ( ! empty( $prev_data ) ) {
135 if ( $this->model->enabled && ! $prev_data['enabled'] ) {
136 $need_track = true;
137 $event = 'def_feature_activated';
138 } elseif ( ! $this->model->enabled && $prev_data['enabled'] ) {
139 $need_track = true;
140 $event = 'def_feature_deactivated';
141 } else {
142 $need_track = false;
143 }
144
145 if ( $need_track ) {
146 $data = array(
147 'Feature' => 'Strong Password',
148 'Triggered From' => 'Feature page',
149 );
150 $this->track_feature( $event, $data );
151 }
152 }
153 }
154
155 return new Response( true, array_merge( $response, $this->data_frontend() ) );
156 }
157
158 return new Response(
159 false,
160 array(
161 'message' => $this->model->get_formatted_errors(),
162 )
163 );
164 }
165
166 /**
167 * Export the data of this module, we will use this for export to HUB, create a preset etc.
168 *
169 * @return array
170 */
171 public function to_array() {
172 return array();
173 }
174
175 /**
176 * Import the data of other source into this, it can be when HUB trigger the import, or user apply a preset.
177 *
178 * @param array $data Data from other source.
179 *
180 * @return null|void
181 */
182 public function import_data( array $data ) {
183 $this->model->import( $data );
184 if ( $this->model->validate() ) {
185 $this->model->save();
186 }
187 }
188
189 /**
190 * Remove all settings, configs generated in this container runtime.
191 *
192 * @return mixed
193 */
194 public function remove_settings() {
195 }
196
197 /**
198 * Remove all data.
199 */
200 public function remove_data() {
201 $this->delete_visited_meta();
202 }
203
204 /**
205 * Export strings.
206 *
207 * @return array
208 */
209 public function export_strings(): array {
210 return array(
211 $this->model->is_active() ? esc_html__( 'Active', 'defender-security' ) : esc_html__( 'Inactive', 'defender-security' ),
212 );
213 }
214
215 /**
216 * Provides data for the dashboard widget.
217 *
218 * @return array An array of dashboard widget data.
219 */
220 public function dashboard_widget(): array {
221 return array( 'model' => $this->model->export() );
222 }
223
224 /**
225 * Delete the 'wd_password_rules_visited' meta key.
226 * We can remove it in the future releases and use delete_meta_key() of Breadcrumbs class.
227 *
228 * @return void
229 */
230 private function delete_visited_meta() {
231 global $wpdb;
232
233 $wpdb->delete( $wpdb->usermeta, array( 'meta_key' => 'wd_password_rules_visited' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.SlowDBQuery
234 }
235 }
236