PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.4
Defender Security – Malware Scanner, Login Security & Firewall v6.2.4
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 / component / security-tweaks / class-login-duration.php

class-login-duration.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.4, at src/component/security-tweaks/class-login-duration.php

226 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Responsible for managing the login duration settings in a WordPress environment.
4 *
5 * @package WP_Defender\Component\Security_Tweaks
6 */
7
8 namespace WP_Defender\Component\Security_Tweaks;
9
10 use WP_Error;
11 use Calotes\Helper\HTTP;
12 use WP_Defender\Traits\Security_Tweaks_Option;
13
14 /**
15 * Manages the login duration settings.
16 */
17 class Login_Duration extends Abstract_Security_Tweaks implements Security_Key_Const_Interface {
18
19 use Security_Tweaks_Option;
20
21 public const DEFAULT_DAYS = 14;
22 public const OPTION_NAME = 'defender_security_tweeks_login-duration';
23 /**
24 * Slug identifier for the component.
25 *
26 * @var string
27 */
28 public string $slug = 'login-duration';
29 /**
30 * Check whether the issue has been resolved or not.
31 *
32 * @var bool
33 */
34 public $resolved = false;
35
36 /**
37 * Check whether the issue has been resolved or not.
38 *
39 * @return bool
40 */
41 public function check(): bool {
42 return $this->resolved;
43 }
44
45 /**
46 * Validates if the provided duration is a positive number.
47 *
48 * @param string|int $duration The duration to validate.
49 *
50 * @return bool True if the duration is incorrect, false otherwise.
51 */
52 private function is_incorect_duration( $duration ): bool {
53 return ( ! is_numeric( $duration ) || 0 >= (int) $duration );
54 }
55
56 /**
57 * Here is the code for processing, if the return is true, we add it to resolve list, WP_Error if any error.
58 *
59 * @return bool|WP_Error
60 */
61 public function process() {
62 $duration = HTTP::post( 'duration' );
63
64 if ( $this->is_incorect_duration( $duration ) ) {
65 $this->resolved = false;
66
67 return new WP_Error(
68 'defender_invalid_duration',
69 esc_html__( 'Duration can only be a number and greater than 0.', 'defender-security' )
70 );
71 }
72 $this->resolved = true;
73 // Sync with the Session feature.
74 $session_settings = wd_di()->get( \WP_Defender\Model\Setting\Session_Protection::class );
75 $session_settings->login_duration = (int) $duration;
76 $session_settings->save();
77
78 return update_site_option( self::OPTION_NAME, $duration );
79 }
80
81 /**
82 * This is for un-do stuff that were done in @process.
83 *
84 * @return bool
85 */
86 public function revert(): bool {
87 return delete_site_option( self::OPTION_NAME );
88 }
89
90 /**
91 * Set $duration as resolved if value > 0 otherwise revert value.
92 */
93 public function shield_up() {
94 $duration = $this->get_duration_in_days();
95 if ( $this->is_incorect_duration( $duration ) ) {
96 $this->resolved = false;
97
98 return $this->revert();
99 }
100 $this->resolved = true;
101 add_filter( 'auth_cookie_expiration', array( $this, 'cookie_duration' ), 10, 3 );
102 // Todo: need hook 'login_message'?
103 }
104
105 /**
106 * Cookie duration in days in seconds.
107 *
108 * @param int $duration Default duration.
109 * @param int $user_id Current user id.
110 * @param bool $remember Remember me login.
111 *
112 * @return int
113 */
114 public function cookie_duration( $duration, $user_id, $remember ) {
115 $saved_duration = $this->get_tweak_duration();
116
117 // When remember is set or saved_duration is smaller than 2 days, return 1 day in seconds.
118 if ( $remember || 2 > $saved_duration ) {
119 return $saved_duration * DAY_IN_SECONDS;
120 }
121
122 return $duration;
123 }
124
125 /**
126 * This will define a value for duration, use in bulk resolve.
127 */
128 public function bulk_process() {
129 $duration = 7;
130 update_site_option( self::OPTION_NAME, $duration );
131 }
132
133 /**
134 * Get duration in days.
135 *
136 * @return int
137 */
138 private function get_duration_in_days(): int {
139 $duration = apply_filters(
140 "defender_security_tweaks_{$this->slug}_get_duration",
141 get_site_option( self::OPTION_NAME )
142 );
143 return is_int( $duration ) ? $duration : (int) $duration;
144 }
145
146 /**
147 * Get duration in days.
148 *
149 * @return int
150 */
151 public function get_tweak_duration(): int {
152 $duration = $this->get_duration_in_days();
153 if ( $this->is_incorect_duration( $duration ) ) {
154 return self::DEFAULT_DAYS;
155 }
156
157 return $duration;
158 }
159
160 /**
161 * Update duration.
162 *
163 * @param int $duration The duration.
164 */
165 public function update_tweak_duration( int $duration ) {
166 if ( $this->is_incorect_duration( $duration ) ) {
167 return;
168 }
169
170 update_site_option( self::OPTION_NAME, $duration );
171 }
172
173 /**
174 * Retrieve the tweak's label.
175 *
176 * @return string
177 */
178 public function get_label(): string {
179 return esc_html__( 'Manage login Duration', 'defender-security' );
180 }
181
182 /**
183 * Get the error reason.
184 *
185 * @return string
186 */
187 public function get_error_reason(): string {
188 return sprintf(
189 /* translators: %d: Number of days. */
190 esc_html__( 'Your login duration is %d days.', 'defender-security' ),
191 $this->get_tweak_duration()
192 );
193 }
194
195 /**
196 * Return a summary data of this tweak.
197 *
198 * @return array
199 */
200 public function to_array(): array {
201 $duration = $this->get_tweak_duration();
202
203 return array(
204 'slug' => $this->slug,
205 'title' => $this->get_label(),
206 'errorReason' => $this->get_error_reason(),
207 'successReason' => sprintf(
208 /* translators: %d: Number of days. */
209 wp_strip_all_tags( __( 'Login duration is set to %d days.', 'defender-security' ) ),
210 $duration
211 ),
212 'misc' => array(
213 'duration' => $duration,
214 'show_action_button' => true,
215 'show_revert_button' => true,
216 ),
217 'bulk_description' => sprintf(
218 /* translators: %d: WordPress default login days. */
219 wp_strip_all_tags( __( "\"Remember Me\" can keep users logged in for %d days by default. Manage login duration to limit long running sessions and help protect accounts that don't need frequent backend access.", 'defender-security' ) ),
220 self::DEFAULT_DAYS
221 ),
222 'bulk_title' => wp_strip_all_tags( __( 'Login Duration', 'defender-security' ) ),
223 );
224 }
225 }
226