| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Model\Setting; |
| 4 |
|
| 5 |
/** |
| 6 |
* @package WP_Defender\Model\Setting |
| 7 |
*/ |
| 8 |
class Notfound_Lockout extends \Calotes\Model\Setting { |
| 9 |
protected $table = 'wd_notfound_lockout_settings'; |
| 10 |
|
| 11 |
/** |
| 12 |
* Activate this module. |
| 13 |
* |
| 14 |
* @var bool |
| 15 |
* @defender_property |
| 16 |
*/ |
| 17 |
public $enabled = false; |
| 18 |
/** |
| 19 |
* How many 404 error happen before we lock out the IP. |
| 20 |
* |
| 21 |
* @var int |
| 22 |
* @defender_property |
| 23 |
*/ |
| 24 |
public $attempt = 20; |
| 25 |
/** |
| 26 |
* The time window we use for counting. |
| 27 |
* |
| 28 |
* @var int |
| 29 |
* @defender_property |
| 30 |
*/ |
| 31 |
public $timeframe = 300; |
| 32 |
/** |
| 33 |
* How long we block them. |
| 34 |
* |
| 35 |
* @var int |
| 36 |
* @defender_property |
| 37 |
*/ |
| 38 |
public $duration = 300; |
| 39 |
/** |
| 40 |
* Duration unit. |
| 41 |
* |
| 42 |
* @var string |
| 43 |
* @defender_property |
| 44 |
*/ |
| 45 |
public $duration_unit = 'seconds'; |
| 46 |
/** |
| 47 |
* How the lock gonna be, if we chose permanent, then their IP will be blacklisted. |
| 48 |
* |
| 49 |
* @var string |
| 50 |
* @defender_property |
| 51 |
*/ |
| 52 |
public $lockout_type = 'timeframe'; |
| 53 |
/** |
| 54 |
* Data allowed: |
| 55 |
* - URL, as relative form /this-should-be-block, with or without dash would be fine |
| 56 |
* - filetype extension, with dot before .sql|.exe |
| 57 |
* - regex pattern, like \/.+\.html |
| 58 |
* |
| 59 |
* This will lockout any IP if an attempt is triggered when visit the URL that in the list. |
| 60 |
* |
| 61 |
* @var string |
| 62 |
* @defender_property |
| 63 |
*/ |
| 64 |
public $blacklist = ''; |
| 65 |
/** |
| 66 |
* Refer to $blacklist, but we will ignore instead of blocking. |
| 67 |
* |
| 68 |
* @var string |
| 69 |
* @defender_property |
| 70 |
*/ |
| 71 |
public $whitelist = ''; |
| 72 |
/** |
| 73 |
* A message to display on frontend when an IP is locked out. |
| 74 |
* |
| 75 |
* @var string |
| 76 |
* @defender_property |
| 77 |
*/ |
| 78 |
public $lockout_message = ''; |
| 79 |
|
| 80 |
/** |
| 81 |
* Set to true for enabling the 404 tracking on logged-in user. |
| 82 |
* |
| 83 |
* @var bool |
| 84 |
* @defender_property |
| 85 |
*/ |
| 86 |
public $detect_logged = false; |
| 87 |
|
| 88 |
/** |
| 89 |
* Validation rules. |
| 90 |
* |
| 91 |
* @var array |
| 92 |
*/ |
| 93 |
protected $rules = array( |
| 94 |
array( array( 'enable', 'detect_logged' ), 'boolean' ), |
| 95 |
array( array( 'attempt', 'timeframe', 'duration' ), 'integer' ), |
| 96 |
array( array( 'lockout_type' ), 'in', array( 'timeframe', 'permanent' ) ), |
| 97 |
array( array( 'duration_unit' ), 'in', array( 'seconds', 'minutes', 'hours' ) ), |
| 98 |
); |
| 99 |
|
| 100 |
/** |
| 101 |
* @return array |
| 102 |
*/ |
| 103 |
public function get_default_values() { |
| 104 |
|
| 105 |
return array( |
| 106 |
'message' => __( "You have been locked out due to too many attempts to access a file that doesn't exist.", 'wpdef' ), |
| 107 |
'whitelist' => ".css\n.js\n.map", |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
protected function before_load() { |
| 112 |
$default_values = $this->get_default_values(); |
| 113 |
$this->lockout_message = $default_values['message']; |
| 114 |
$this->whitelist = $default_values['whitelist']; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get list of blocklisted or allowlisted data. |
| 119 |
* |
| 120 |
* @param string $type blocklist|allowlist |
| 121 |
* |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
public function get_lockout_list( $type = 'blocklist' ) { |
| 125 |
$data = ( 'blocklist' === $type ) ? $this->blacklist : $this->whitelist; |
| 126 |
$arr = is_array( $data ) ? $data : array_filter( explode( PHP_EOL, $data ) ); |
| 127 |
$arr = array_map( 'trim', $arr ); |
| 128 |
$arr = array_map( 'strtolower', $arr ); |
| 129 |
|
| 130 |
return $arr; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Define labels for settings key. |
| 135 |
* |
| 136 |
* @param string|null $key |
| 137 |
* |
| 138 |
* @return string|array|null |
| 139 |
*/ |
| 140 |
public function labels( $key = null ) { |
| 141 |
$labels = array( |
| 142 |
//Todo new key: enabled |
| 143 |
'detect_404' => __( '404 Detection', 'wpdef' ), |
| 144 |
//Todo new key: attempt |
| 145 |
'detect_404_threshold' => __( '404 Detection - Threshold', 'wpdef' ), |
| 146 |
//Todo new key: timeframe |
| 147 |
'detect_404_timeframe' => __( '404 Protection - Timeframe', 'wpdef' ), |
| 148 |
//Todo new key: lockout_type |
| 149 |
'detect_404_lockout_ban' => __( '404 Protection - Duration Type', 'wpdef' ), |
| 150 |
//Todo new key: duration |
| 151 |
'detect_404_lockout_duration' => __( '404 Detection - Duration', 'wpdef' ), |
| 152 |
//Todo new key: duration_unit |
| 153 |
'detect_404_lockout_duration_unit' => __( '404 Protection - Duration units', 'wpdef' ), |
| 154 |
//Todo new key: lockout_message |
| 155 |
'detect_404_lockout_message' => __( '404 Detection - Lockout Message', 'wpdef' ), |
| 156 |
//Todo new key: blacklist |
| 157 |
'detect_404_blacklist' => __( '404 Detection - Files & Folders Blocklist', 'wpdef' ), |
| 158 |
//Todo new key: whitelist |
| 159 |
'detect_404_whitelist' => __( '404 Detection - Files & Folders Allowlist', 'wpdef' ), |
| 160 |
//Todo new key: detect_logged |
| 161 |
'detect_404_logged' => __( '404 Detection - Monitor logged in users', 'wpdef' ), |
| 162 |
); |
| 163 |
|
| 164 |
if ( ! is_null( $key ) ) { |
| 165 |
return $labels[ $key ] ?? null; |
| 166 |
} |
| 167 |
|
| 168 |
return $labels; |
| 169 |
} |
| 170 |
} |
| 171 |
|