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-session-protection.php

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

216 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle session protection module.
4 *
5 * @package WP_Defender\Controller
6 */
7
8 namespace WP_Defender\Controller;
9
10 use WP_Defender\Event;
11 use WP_Defender\Traits\User;
12 use Calotes\Component\Request;
13 use Calotes\Component\Response;
14 use WP_Defender\Component\Session_Protection as Service;
15 use WP_Defender\Model\Setting\Session_Protection as Settings;
16 use WP_Defender\Component\Breadcrumbs;
17
18 /**
19 * Handle session protection module.
20 */
21 class Session_Protection extends Event {
22 use User;
23
24 /**
25 * The model for the session protection module.
26 *
27 * @var Settings|null
28 */
29 protected ?Settings $model = null;
30
31 /**
32 * The service for the session protection module.
33 *
34 * @var Service|null
35 */
36 protected ?Service $service = null;
37
38 /**
39 * Initializes the model and service, registers routes, and sets up scheduled events if the model is active.
40 */
41 public function __construct() {
42 $this->model = wd_di()->get( Settings::class );
43 $this->service = wd_di()->get( Service::class );
44 add_filter( 'wp_defender_advanced_tools_data', array( $this, 'script_data' ) );
45 $this->register_routes();
46 if ( $this->model->enabled ) {
47 add_action( 'init', array( $this->service, 'handle_session_timeout' ) );
48 add_action( 'wp_enqueue_scripts', array( $this->service, 'enqueue_idle_scripts' ) );
49 add_action( 'admin_enqueue_scripts', array( $this->service, 'enqueue_idle_scripts' ) );
50 add_action( 'wp_ajax_wpdef_logout', array( $this->service, 'logout' ) );
51 add_action( 'wp_login', array( $this->service, 'update_last_activity' ) );
52
53 // Show login modal with custom message.
54 add_filter( 'wp_login_errors', array( $this->service, 'login_modal_message' ) );
55 add_action( 'login_head', array( $this->service, 'login_modal_message_styles' ) );
56
57 // Attach IPs to the current user session.
58 if ( $this->model->has_properties() ) {
59 add_filter( 'attach_session_information', array( $this->service, 'attach_session_information' ) );
60 }
61 }
62 }
63
64 /**
65 * Provide data to the frontend via localized script.
66 *
67 * @param array $data Data collection is ready to passed.
68 *
69 * @return array Modified data array with added this controller data.
70 */
71 public function script_data( array $data ): array {
72 $data['session_protection'] = $this->data_frontend();
73
74 return $data;
75 }
76
77 /**
78 * All the variables that we will show on frontend, both in the main page, or dashboard widget.
79 *
80 * @return array
81 */
82 public function data_frontend(): array {
83 $is_visited = wd_di()->get( Breadcrumbs::class )->get_meta_key();
84
85 return array_merge(
86 array(
87 'model' => $this->model->export(),
88 'properties' => $this->service::session_lock_properties(),
89 'show_feature_dot' => wd_di()->get( \WP_Defender\Behavior\WPMUDEV::class )->is_pro() && ! $is_visited,
90 'roles' => $this->get_all_editable_roles(),
91 ),
92 $this->dump_routes_and_nonces()
93 );
94 }
95
96 /**
97 * Save settings.
98 *
99 * @param Request $request The request object containing new settings data.
100 *
101 * @return Response
102 * @defender_route
103 */
104 public function save_settings( Request $request ): Response {
105 $model_data = $request->get_data_by_model( $this->model );
106 $prev_data = $this->model->get_old_settings();
107 $this->model->import( $model_data );
108 if ( $this->model->validate() ) {
109 $this->model->save();
110 // Changes for Hub.
111 \WP_Defender\Component\Config\Config_Hub_Helper::set_clear_active_flag();
112
113 // Maybe track if any settings have changed except user roles.
114 if ( $this->maybe_track() && ! empty( $prev_data ) &&
115 (
116 ( $this->model->enabled !== $prev_data['enabled'] )
117 || ! empty( array_diff( $this->model->lock_properties, $prev_data['lock_properties'] ) )
118 || $this->model->idle_timeout !== $prev_data['idle_timeout']
119 )
120 ) {
121 $data = array(
122 'Idle Time' => $this->model->idle_timeout,
123 'Session Lock' => $this->service->get_session_lock_string(),
124 'Action' => $this->model->enabled ? 'Enable' : 'Disable',
125 );
126 $this->track_feature( 'def_session_protection', $data );
127 }
128
129 $message = esc_html__( 'Settings updated successfully!', 'defender-security' );
130 if ( ( $prev_data['enabled'] ?? false ) !== $this->model->enabled && ! $this->model->enabled ) {
131 /* translators: 1. tag open, 2. tag close */
132 $message = sprintf( esc_html__( '%1$s Session Protection %2$s deactivated successfully!', 'defender-security' ), '<strong>', '</strong>' );
133 } elseif ( ( $prev_data['enabled'] ?? false ) !== $this->model->enabled && $this->model->enabled ) {
134 /* translators: 1. tag open, 2. tag close */
135 $message = sprintf( esc_html__( '%1$s Session Protection %2$s activated successfully!', 'defender-security' ), '<strong>', '</strong>' );
136 // Update last activity time to prevent instant logout.
137 $this->service->update_last_activity();
138 }
139
140 return new Response(
141 true,
142 array_merge(
143 array(
144 'message' => $message,
145 'auto_close' => true,
146 ),
147 $this->data_frontend()
148 )
149 );
150 }
151
152 return new Response( false, array( 'message' => $this->model->get_formatted_errors() ) );
153 }
154
155 /**
156 * Export the data of this module, we will use this for export to HUB, create a preset etc.
157 *
158 * @return array
159 */
160 public function to_array() {
161 return array();
162 }
163
164 /**
165 * Import the data of other source into this, it can be when HUB trigger the import, or user apply a preset.
166 *
167 * @param array $data Data from other source.
168 *
169 * @return null|void
170 */
171 public function import_data( array $data ) {
172 $this->model->import( $data );
173 if ( $this->model->validate() ) {
174 $this->model->save();
175 $this->service->update_last_activity();
176 return;
177 }
178 }
179
180 /**
181 * Remove all settings, configs generated in this container runtime.
182 *
183 * @return mixed
184 */
185 public function remove_settings() {
186 }
187
188 /**
189 * Remove all data.
190 */
191 public function remove_data() {
192 wd_di()->get( Breadcrumbs::class )->delete_meta_key();
193 delete_site_transient( Service::LOGOUT_MSG_TRANSIENT_KEY );
194 }
195
196 /**
197 * Export strings.
198 *
199 * @return array
200 */
201 public function export_strings() {
202 return array(
203 $this->model->is_active() ? esc_html__( 'Active', 'defender-security' ) : esc_html__( 'Inactive', 'defender-security' ),
204 );
205 }
206
207 /**
208 * Provides data for the dashboard widget.
209 *
210 * @return array An array of dashboard widget data.
211 */
212 public function dashboard_widget(): array {
213 return array( 'model' => $this->model->export() );
214 }
215 }
216