PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.7.2
Defender Security – Malware Scanner, Login Security & Firewall v5.7.2
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 5.7.2, at src/component/security-tweaks/class-login-duration.php

220 lines 5.5 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 ( empty( $duration ) || $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 return (int) apply_filters(
140 "defender_security_tweaks_{$this->slug}_get_duration",
141 get_site_option( self::OPTION_NAME )
142 );
143 }
144
145 /**
146 * Get duration in days.
147 *
148 * @return int
149 */
150 public function get_tweak_duration(): int {
151 $duration = $this->get_duration_in_days();
152 if ( $this->is_incorect_duration( $duration ) ) {
153 return self::DEFAULT_DAYS;
154 }
155
156 return $duration;
157 }
158
159 /**
160 * Update duration.
161 *
162 * @param int $duration The duration.
163 */
164 public function update_tweak_duration( int $duration ) {
165 if ( empty( $duration ) || $this->is_incorect_duration( $duration ) ) {
166 return;
167 }
168
169 update_site_option( self::OPTION_NAME, $duration );
170 }
171
172 /**
173 * Retrieve the tweak's label.
174 *
175 * @return string
176 */
177 public function get_label(): string {
178 return esc_html__( 'Manage Login Duration', 'defender-security' );
179 }
180
181 /**
182 * Get the error reason.
183 *
184 * @return string
185 */
186 public function get_error_reason(): string {
187 return sprintf(
188 /* translators: %d: Number of days. */
189 esc_html__( 'Your current login duration is the default %d days.', 'defender-security' ),
190 self::DEFAULT_DAYS
191 );
192 }
193
194 /**
195 * Return a summary data of this tweak.
196 *
197 * @return array
198 */
199 public function to_array(): array {
200 $duration = $this->get_tweak_duration();
201
202 return array(
203 'slug' => $this->slug,
204 'title' => $this->get_label(),
205 'errorReason' => $this->get_error_reason(),
206 'successReason' => sprintf(
207 /* translators: %d: Number of days. */
208 esc_html__( 'You\'ve adjusted the default login duration to %d days.', 'defender-security' ),
209 $duration
210 ),
211 'misc' => array( 'duration' => $duration ),
212 'bulk_description' => esc_html__(
213 'Users who select the \'remember me\' option will stay logged in for 14 days.It’s good practice to reduce this default time to reduce the risk of someone gaining access to your automatically logged in account. We’ll set the login duration to 7 days.',
214 'defender-security'
215 ),
216 'bulk_title' => esc_html__( 'Login Duration', 'defender-security' ),
217 );
218 }
219 }
220