PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.1.0
Defender Security – Malware Scanner, Login Security & Firewall v3.1.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 / component / security-tweaks / login-duration.php

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

152 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace WP_Defender\Component\Security_Tweaks;
6
7 use WP_Error;
8 use Calotes\Helper\HTTP;
9 use Calotes\Base\Component;
10
11 /**
12 * Class Login_Duration
13 * @package WP_Defender\Component\Security_Tweaks
14 */
15 class Login_Duration extends Component {
16 public const DEFAULT_DAYS = 14;
17 public $slug = 'login-duration';
18 public $resolved = false;
19
20 /**
21 * Check whether the issue has been resolved or not.
22 *
23 * @return bool
24 */
25 public function check(): bool {
26 return $this->resolved;
27 }
28
29 /**
30 * @param string|int $duration
31 *
32 * @return bool
33 */
34 private function is_incorect_duration( $duration ): bool {
35 return ( ! is_numeric( $duration ) || 0 >= (int) $duration );
36 }
37
38 /**
39 * Here is the code for processing, if the return is true, we add it to resolve list, WP_Error if any error.
40 *
41 * @return bool|WP_Error
42 */
43 public function process() {
44 $duration = HTTP::post( 'duration' );
45
46 if ( $this->is_incorect_duration( $duration ) ) {
47 $this->resolved = false;
48
49 return new WP_Error(
50 'defender_invalid_duration',
51 __( 'Duration can only be a number and greater than 0.', 'wpdef' )
52 );
53 }
54 $this->resolved = true;
55
56 return update_site_option( "defender_security_tweeks_{$this->slug}", $duration );
57 }
58
59 /**
60 * This is for un-do stuff that were done in @process.
61 *
62 * @return bool
63 */
64 public function revert(): bool {
65 return delete_site_option( "defender_security_tweeks_{$this->slug}" );
66 }
67
68 /**
69 * Set $duration as resolved if value > 0 otherwise revert value.
70 */
71 public function shield_up() {
72 $duration = get_site_option( "defender_security_tweeks_{$this->slug}" );
73 if ( empty( $duration ) || $this->is_incorect_duration( $duration ) ) {
74 $this->resolved = false;
75
76 return $this->revert();
77 }
78 $this->resolved = true;
79 add_filter( 'auth_cookie_expiration', [ $this, 'cookie_duration' ], 10, 3 );
80 //Todo: need hook 'login_message'?
81 }
82
83 /**
84 * Cookie duration in days in seconds.
85 *
86 * @param int $duration Default duration.
87 * @param int $user_id Current user id.
88 * @param bool $remember Remember me login.
89 *
90 * @return int
91 */
92 public function cookie_duration( $duration, $user_id, $remember ): int {
93 $saved_duration = $this->get_duration( true );
94
95 // When remember is set or saved_duration is smaller than 2 days, return saved_duration.
96 if ( $remember || 2 > $saved_duration ) {
97 return $saved_duration;
98 }
99
100 return $duration;
101 }
102
103 /**
104 * This will define a value for duration, use in bulk resolve.
105 */
106 public function bulk_process() {
107 $duration = 7;
108 update_site_option( "defender_security_tweeks_{$this->slug}", $duration );
109 }
110
111 /**
112 * Get duration in days or seconds. Returns in seconds on passing true.
113 *
114 * @param bool $in_seconds Default value: false.
115 *
116 * @return int
117 */
118 private function get_duration( $in_seconds = false ): int {
119 $duration = apply_filters(
120 "defender_security_tweaks_{$this->slug}_get_duration",
121 get_site_option( "defender_security_tweeks_{$this->slug}" ), $in_seconds
122 );
123
124 return $in_seconds ? $duration * DAY_IN_SECONDS : (int) $duration;
125 }
126
127 /**
128 * Return a summary data of this tweak.
129 *
130 * @return array
131 */
132 public function to_array(): array {
133 $duration = $this->get_duration();
134
135 return [
136 'slug' => $this->slug,
137 'title' => __( 'Manage Login Duration', 'wpdef' ),
138 'errorReason' => sprintf(
139 __( 'Your current login duration is the default %d days.', 'wpdef' ),
140 self::DEFAULT_DAYS
141 ),
142 'successReason' => sprintf(
143 __( 'You\'ve adjusted the default login duration to %d days.', 'wpdef' ),
144 $duration
145 ),
146 'misc' => [ 'duration' => $duration ],
147 'bulk_description' => __( '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.', 'wpdef' ),
148 'bulk_title' => __( 'Login Duration', 'wpdef' ),
149 ];
150 }
151 }
152