PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.5.1
Defender Security – Malware Scanner, Login Security & Firewall v5.5.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-session-protection.php

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

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