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