| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Model\Setting; |
| 4 |
|
| 5 |
class Login_Lockout extends \Calotes\Model\Setting { |
| 6 |
protected $table = 'wd_login_lockout_settings'; |
| 7 |
|
| 8 |
/** |
| 9 |
* @var bool |
| 10 |
* @defender_property |
| 11 |
*/ |
| 12 |
public $enabled = false; |
| 13 |
/** |
| 14 |
* Maximum attempt before get locked. |
| 15 |
* |
| 16 |
* @var int |
| 17 |
* @defender_property |
| 18 |
* @rule required|integer |
| 19 |
*/ |
| 20 |
public $attempt = 5; |
| 21 |
/** |
| 22 |
* The timeframe we record the attempt. |
| 23 |
* |
| 24 |
* @var int |
| 25 |
* @defender_property |
| 26 |
* @rule required|integer |
| 27 |
*/ |
| 28 |
public $timeframe = 300; |
| 29 |
/** |
| 30 |
* How current lockout last. |
| 31 |
* |
| 32 |
* @var int |
| 33 |
* @defender_property |
| 34 |
* @rule required|integer |
| 35 |
*/ |
| 36 |
public $duration = 300; |
| 37 |
/** |
| 38 |
* Duration unit. |
| 39 |
* |
| 40 |
* @var string |
| 41 |
* @defender_property |
| 42 |
* @rule in[seconds,minutes,hours] |
| 43 |
*/ |
| 44 |
public $duration_unit = 'seconds'; |
| 45 |
/** |
| 46 |
* How the lock is going to be, if we choose permanent, then their IP will be blacklisted. |
| 47 |
* |
| 48 |
* @var string |
| 49 |
* @defender_property |
| 50 |
* @rule in[timeframe,permanent] |
| 51 |
*/ |
| 52 |
public $lockout_type = 'timeframe'; |
| 53 |
|
| 54 |
/** |
| 55 |
* The message to output on the lockout screen. |
| 56 |
* |
| 57 |
* @var string |
| 58 |
* @defender_property |
| 59 |
* @rule required |
| 60 |
* @sanitize sanitize_textarea_field |
| 61 |
*/ |
| 62 |
public $lockout_message = ''; |
| 63 |
|
| 64 |
/** |
| 65 |
* The blacklist username, if fail will be banned. |
| 66 |
* |
| 67 |
* @var string |
| 68 |
* @defender_property |
| 69 |
* @rule required |
| 70 |
* @sanitize sanitize_textarea_field |
| 71 |
*/ |
| 72 |
public $username_blacklist = ''; |
| 73 |
|
| 74 |
/** |
| 75 |
* Validation rules. |
| 76 |
* |
| 77 |
* @var array |
| 78 |
*/ |
| 79 |
protected $rules = array( |
| 80 |
array( array( 'enabled' ), 'boolean' ), |
| 81 |
array( array( 'attempt', 'timeframe', 'duration' ), 'integer' ), |
| 82 |
array( array( 'lockout_type' ), 'in', array( 'timeframe', 'permanent' ) ), |
| 83 |
array( array( 'duration_unit' ), 'in', array( 'seconds', 'minutes', 'hours' ) ), |
| 84 |
); |
| 85 |
|
| 86 |
/** |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
public function get_default_values() { |
| 90 |
|
| 91 |
return array( |
| 92 |
'message' => __( 'You have been locked out due to too many invalid login attempts.', 'wpdef' ), |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
protected function before_load() { |
| 97 |
$default_values = $this->get_default_values(); |
| 98 |
$this->lockout_message = $default_values['message']; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Return the blacklisted username as array. |
| 103 |
* |
| 104 |
* @return array |
| 105 |
*/ |
| 106 |
public function get_blacklisted_username() { |
| 107 |
// Since 2.4.7 |
| 108 |
$usernames = apply_filters( 'wp_defender_banned_usernames', $this->username_blacklist ); |
| 109 |
if ( empty( $usernames ) ) { |
| 110 |
return array(); |
| 111 |
} |
| 112 |
$usernames = str_replace( PHP_EOL, ' ', $this->username_blacklist ); |
| 113 |
$usernames = explode( ' ', $usernames ); |
| 114 |
$usernames = array_map( 'trim', $usernames ); |
| 115 |
|
| 116 |
return $usernames; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Define labels for settings key. |
| 121 |
* |
| 122 |
* @param string|null $key |
| 123 |
* |
| 124 |
* @return string|array|null |
| 125 |
*/ |
| 126 |
public function labels( $key = null ) { |
| 127 |
$labels = array( |
| 128 |
// New key: enabled. |
| 129 |
'login_protection' => __( 'Login Protection', 'wpdef' ), |
| 130 |
// New key: attempt. |
| 131 |
'login_protection_login_attempt' => __( 'Login Protection - Threshold', 'wpdef' ), |
| 132 |
// New key: timeframe. |
| 133 |
'login_protection_lockout_timeframe' => __( 'Login Protection - Timeframe', 'wpdef' ), |
| 134 |
// New key: lockout_type. |
| 135 |
'login_protection_lockout_ban' => __( 'Login Protection - Duration Type', 'wpdef' ), |
| 136 |
// New key: duration. |
| 137 |
'login_protection_lockout_duration' => __( 'Login Protection - Duration', 'wpdef' ), |
| 138 |
// New key: duration_unit. |
| 139 |
'login_protection_lockout_duration_unit' => __( 'Login Protection - Duration units', 'wpdef' ), |
| 140 |
// New key: lockout_message. |
| 141 |
'login_protection_lockout_message' => __( 'Login Protection - Lockout Message', 'wpdef' ), |
| 142 |
'username_blacklist' => __( 'Login Protection - Banned Usernames', 'wpdef' ), |
| 143 |
); |
| 144 |
|
| 145 |
if ( ! is_null( $key ) ) { |
| 146 |
return $labels[ $key ] ?? null; |
| 147 |
} |
| 148 |
|
| 149 |
return $labels; |
| 150 |
} |
| 151 |
} |
| 152 |
|