| 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 |
$this->register_routes(); |
| 44 |
if ( $this->model->enabled ) { |
| 45 |
add_action( 'init', array( $this->service, 'handle_session_timeout' ) ); |
| 46 |
add_action( 'wp_enqueue_scripts', array( $this->service, 'enqueue_idle_scripts' ) ); |
| 47 |
add_action( 'admin_enqueue_scripts', array( $this->service, 'enqueue_idle_scripts' ) ); |
| 48 |
add_action( 'wp_ajax_wpdef_logout', array( $this->service, 'logout' ) ); |
| 49 |
add_action( 'wp_login', array( $this->service, 'update_last_activity' ) ); |
| 50 |
|
| 51 |
// Show login modal with custom message. |
| 52 |
add_filter( 'wp_login_errors', array( $this->service, 'login_modal_message' ) ); |
| 53 |
add_action( 'login_head', array( $this->service, 'login_modal_message_styles' ) ); |
| 54 |
|
| 55 |
// Attach IPs to the current user session. |
| 56 |
if ( $this->model->has_properties() ) { |
| 57 |
add_filter( 'attach_session_information', array( $this->service, 'attach_session_information' ) ); |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* All the variables that we will show on frontend, both in the main page, or dashboard widget. |
| 64 |
* |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
public function data_frontend(): array { |
| 68 |
return array_merge( |
| 69 |
array( |
| 70 |
'model' => $this->model->export(), |
| 71 |
'properties' => $this->service::session_lock_properties(), |
| 72 |
'roles' => $this->get_all_editable_roles(), |
| 73 |
), |
| 74 |
$this->dump_routes_and_nonces() |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Save settings. |
| 80 |
* |
| 81 |
* @param Request $request The request object containing new settings data. |
| 82 |
* |
| 83 |
* @return Response |
| 84 |
* @defender_route |
| 85 |
*/ |
| 86 |
public function save_settings( Request $request ): Response { |
| 87 |
$model_data = $request->get_data_by_model( $this->model ); |
| 88 |
$prev_data = $this->model->get_old_settings(); |
| 89 |
$this->model->import( $model_data ); |
| 90 |
if ( $this->model->validate() ) { |
| 91 |
$this->model->save(); |
| 92 |
// Changes for Hub. |
| 93 |
\WP_Defender\Component\Config\Config_Hub_Helper::set_clear_active_flag(); |
| 94 |
|
| 95 |
// Maybe track if any settings have changed except user roles. |
| 96 |
if ( $this->maybe_track() && array() !== $prev_data && |
| 97 |
( |
| 98 |
( $this->model->enabled !== $prev_data['enabled'] ) |
| 99 |
|| array() !== array_diff( $this->model->lock_properties, $prev_data['lock_properties'] ) |
| 100 |
|| $this->model->idle_timeout !== $prev_data['idle_timeout'] |
| 101 |
) |
| 102 |
) { |
| 103 |
$data = array( |
| 104 |
'Idle Time' => $this->model->idle_timeout, |
| 105 |
'Session Lock' => $this->service->get_session_lock_string(), |
| 106 |
'Action' => $this->model->enabled ? 'Enable' : 'Disable', |
| 107 |
); |
| 108 |
$this->track_feature( 'def_session_protection', $data ); |
| 109 |
} |
| 110 |
|
| 111 |
$message = esc_html__( 'Settings updated successfully!', 'defender-security' ); |
| 112 |
if ( ( $prev_data['enabled'] ?? false ) !== $this->model->enabled && ! $this->model->enabled ) { |
| 113 |
/* translators: 1. tag open, 2. tag close */ |
| 114 |
$message = sprintf( esc_html__( '%1$s Session Protection %2$s deactivated successfully!', 'defender-security' ), '<strong>', '</strong>' ); |
| 115 |
} elseif ( ( $prev_data['enabled'] ?? false ) !== $this->model->enabled && $this->model->enabled ) { |
| 116 |
/* translators: 1. tag open, 2. tag close */ |
| 117 |
$message = sprintf( esc_html__( '%1$s Session Protection %2$s activated successfully!', 'defender-security' ), '<strong>', '</strong>' ); |
| 118 |
// Update last activity time to prevent instant logout. |
| 119 |
$this->service->update_last_activity(); |
| 120 |
} |
| 121 |
|
| 122 |
return new Response( |
| 123 |
true, |
| 124 |
array_merge( |
| 125 |
array( |
| 126 |
'message' => $message, |
| 127 |
'auto_close' => true, |
| 128 |
), |
| 129 |
$this->data_frontend() |
| 130 |
) |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
return new Response( false, array( 'message' => $this->model->get_formatted_errors() ) ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Export the data of this module, we will use this for export to HUB, create a preset etc. |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
public function to_array() { |
| 143 |
return array(); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Import the data of other source into this, it can be when HUB trigger the import, or user apply a preset. |
| 148 |
* |
| 149 |
* @param array $data Data from other source. |
| 150 |
*/ |
| 151 |
public function import_data( array $data ) { |
| 152 |
$this->model->import( $data ); |
| 153 |
if ( $this->model->validate() ) { |
| 154 |
$this->model->save(); |
| 155 |
$this->service->update_last_activity(); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Remove all settings, configs generated in this container runtime. |
| 161 |
*/ |
| 162 |
public function remove_settings() { |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Remove all data. |
| 167 |
*/ |
| 168 |
public function remove_data() { |
| 169 |
delete_site_transient( Service::LOGOUT_MSG_TRANSIENT_KEY ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Export strings. |
| 174 |
* |
| 175 |
* @return array |
| 176 |
*/ |
| 177 |
public function export_strings() { |
| 178 |
return array( |
| 179 |
$this->model->is_active() ? esc_html__( 'Active', 'defender-security' ) : esc_html__( 'Inactive', 'defender-security' ), |
| 180 |
); |
| 181 |
} |
| 182 |
} |
| 183 |
|