| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the Login Lockout settings. |
| 4 |
* |
| 5 |
* @package WP_Defender\Model\Setting |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Model\Setting; |
| 9 |
|
| 10 |
use Calotes\Model\Setting; |
| 11 |
|
| 12 |
/** |
| 13 |
* Model for Login Lockout settings. |
| 14 |
*/ |
| 15 |
class Login_Lockout extends Setting { |
| 16 |
|
| 17 |
/** |
| 18 |
* Option name. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $table = 'wd_login_lockout_settings'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Is module enabled? |
| 26 |
* |
| 27 |
* @var bool |
| 28 |
* @defender_property |
| 29 |
*/ |
| 30 |
public $enabled = false; |
| 31 |
/** |
| 32 |
* Maximum attempt before get locked. |
| 33 |
* |
| 34 |
* @var int |
| 35 |
* @defender_property |
| 36 |
* @rule required|integer |
| 37 |
*/ |
| 38 |
public $attempt = 5; |
| 39 |
/** |
| 40 |
* The timeframe we record the attempt. |
| 41 |
* |
| 42 |
* @var int |
| 43 |
* @defender_property |
| 44 |
* @rule required|integer |
| 45 |
*/ |
| 46 |
public $timeframe = 300; |
| 47 |
/** |
| 48 |
* How current lockout last. |
| 49 |
* |
| 50 |
* @var int |
| 51 |
* @defender_property |
| 52 |
* @rule required|integer |
| 53 |
*/ |
| 54 |
public $duration = 300; |
| 55 |
/** |
| 56 |
* Duration unit. |
| 57 |
* |
| 58 |
* @var string |
| 59 |
* @defender_property |
| 60 |
* @rule in[seconds,minutes,hours] |
| 61 |
*/ |
| 62 |
public $duration_unit = 'seconds'; |
| 63 |
/** |
| 64 |
* How the lock is going to be, if we choose permanent, then their IP will be blacklisted. |
| 65 |
* |
| 66 |
* @var string |
| 67 |
* @defender_property |
| 68 |
* @rule in[timeframe,permanent] |
| 69 |
*/ |
| 70 |
public $lockout_type = 'timeframe'; |
| 71 |
|
| 72 |
/** |
| 73 |
* The message to output on the lockout screen. |
| 74 |
* |
| 75 |
* @var string |
| 76 |
* @defender_property |
| 77 |
* @rule required |
| 78 |
* @sanitize sanitize_textarea_field |
| 79 |
*/ |
| 80 |
public $lockout_message = ''; |
| 81 |
|
| 82 |
/** |
| 83 |
* The blacklist username, if fail will be banned. |
| 84 |
* |
| 85 |
* @var string |
| 86 |
* @defender_property |
| 87 |
* @rule required |
| 88 |
* @sanitize sanitize_textarea_field |
| 89 |
*/ |
| 90 |
public $username_blacklist = ''; |
| 91 |
|
| 92 |
/** |
| 93 |
* Validation rules. |
| 94 |
* |
| 95 |
* @var array |
| 96 |
*/ |
| 97 |
protected $rules = array( |
| 98 |
array( array( 'enabled' ), 'boolean' ), |
| 99 |
array( array( 'attempt', 'timeframe', 'duration' ), 'integer' ), |
| 100 |
array( array( 'lockout_type' ), 'in', array( 'timeframe', 'permanent' ) ), |
| 101 |
array( array( 'duration_unit' ), 'in', array( 'seconds', 'minutes', 'hours' ) ), |
| 102 |
); |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns an array containing the default values for the function. |
| 106 |
* |
| 107 |
* @return array An array with the default values. |
| 108 |
*/ |
| 109 |
public function get_default_values(): array { |
| 110 |
return array( |
| 111 |
'message' => esc_html__( 'You have been locked out due to too many invalid login attempts.', 'defender-security' ), |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Initializes the object before loading. |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
protected function before_load(): void { |
| 121 |
$default_values = $this->get_default_values(); |
| 122 |
$this->lockout_message = $default_values['message']; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Return the blacklisted username as array. |
| 127 |
* |
| 128 |
* @return array |
| 129 |
*/ |
| 130 |
public function get_blacklisted_username(): array { |
| 131 |
/** |
| 132 |
* Filter the banned usernames. |
| 133 |
* |
| 134 |
* @filter wp_defender_banned_usernames |
| 135 |
* @since 2.4.7 |
| 136 |
*/ |
| 137 |
$usernames = apply_filters( 'wp_defender_banned_usernames', $this->username_blacklist ); |
| 138 |
if ( '' === trim( $usernames ) ) { |
| 139 |
return array(); |
| 140 |
} |
| 141 |
$usernames = str_replace( array( "\r\n", "\r", "\n" ), ' ', $this->username_blacklist ); |
| 142 |
$usernames = explode( ' ', $usernames ); |
| 143 |
|
| 144 |
return array_map( 'trim', $usernames ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Define settings labels. |
| 149 |
* |
| 150 |
* @return array |
| 151 |
*/ |
| 152 |
public function labels(): array { |
| 153 |
return array( |
| 154 |
// New key: enabled. |
| 155 |
'login_protection' => self::get_module_name(), |
| 156 |
// New key: attempt. |
| 157 |
'login_protection_login_attempt' => esc_html__( 'Login Protection - Threshold', 'defender-security' ), |
| 158 |
// New key: timeframe. |
| 159 |
'login_protection_lockout_timeframe' => esc_html__( 'Login Protection - Timeframe', 'defender-security' ), |
| 160 |
// New key: lockout_type. |
| 161 |
'login_protection_lockout_ban' => esc_html__( 'Login Protection - Duration Type', 'defender-security' ), |
| 162 |
// New key: duration. |
| 163 |
'login_protection_lockout_duration' => esc_html__( 'Login Protection - Duration', 'defender-security' ), |
| 164 |
// New key: duration_unit. |
| 165 |
'login_protection_lockout_duration_unit' => esc_html__( 'Login Protection - Duration units', 'defender-security' ), |
| 166 |
// New key: lockout_message. |
| 167 |
'login_protection_lockout_message' => esc_html__( 'Login Protection - Lockout Message', 'defender-security' ), |
| 168 |
'username_blacklist' => esc_html__( 'Login Protection - Banned Usernames', 'defender-security' ), |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Returns the module name for Login Protection. |
| 174 |
* |
| 175 |
* @return string The module name. |
| 176 |
*/ |
| 177 |
public static function get_module_name(): string { |
| 178 |
return esc_html__( 'Login Protection', 'defender-security' ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Returns the module state based on the given flag. |
| 183 |
* |
| 184 |
* @param bool $flag The flag indicating the module state. |
| 185 |
* |
| 186 |
* @return string The module state, either 'active' or 'inactive'. |
| 187 |
*/ |
| 188 |
public static function get_module_state( $flag ): string { |
| 189 |
return $flag ? esc_html__( 'active', 'defender-security' ) : esc_html__( 'inactive', 'defender-security' ); |
| 190 |
} |
| 191 |
} |
| 192 |
|