| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles not found 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 not found lockout settings. |
| 14 |
*/ |
| 15 |
class Notfound_Lockout extends Setting { |
| 16 |
|
| 17 |
/** |
| 18 |
* Option name. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $table = 'wd_notfound_lockout_settings'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Activate this module. |
| 26 |
* |
| 27 |
* @var bool |
| 28 |
* @defender_property |
| 29 |
*/ |
| 30 |
public $enabled = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* How many 404 error happen before we lock out the IP. |
| 34 |
* |
| 35 |
* @var int |
| 36 |
* @defender_property |
| 37 |
* @rule required|integer |
| 38 |
*/ |
| 39 |
public $attempt = 20; |
| 40 |
|
| 41 |
/** |
| 42 |
* The time window we use for counting. |
| 43 |
* |
| 44 |
* @var int |
| 45 |
* @defender_property |
| 46 |
* @rule required|integer |
| 47 |
*/ |
| 48 |
public $timeframe = 300; |
| 49 |
|
| 50 |
/** |
| 51 |
* How long we block them. |
| 52 |
* |
| 53 |
* @var int |
| 54 |
* @defender_property |
| 55 |
* @rule required|integer |
| 56 |
*/ |
| 57 |
public $duration = 300; |
| 58 |
|
| 59 |
/** |
| 60 |
* Duration unit. |
| 61 |
* |
| 62 |
* @var string |
| 63 |
* @defender_property |
| 64 |
* @rule in[seconds,minutes,hours] |
| 65 |
*/ |
| 66 |
public $duration_unit = 'seconds'; |
| 67 |
|
| 68 |
/** |
| 69 |
* How the lock going to be, if we chose permanent, then their IP will be blacklisted. |
| 70 |
* |
| 71 |
* @var string |
| 72 |
* @defender_property |
| 73 |
* @rule in[timeframe,permanent] |
| 74 |
*/ |
| 75 |
public $lockout_type = 'timeframe'; |
| 76 |
|
| 77 |
/** |
| 78 |
* Data allowed: |
| 79 |
* - URL, as relative form /this-should-be-block, with or without dash would be fine |
| 80 |
* - filetype extension, with dot before .sql|.exe |
| 81 |
* - regex pattern, like \/.+\.html |
| 82 |
* This will lockout any IP if an attempt is triggered when visit the URL that in the list. |
| 83 |
* |
| 84 |
* @var string |
| 85 |
* @defender_property |
| 86 |
*/ |
| 87 |
public $blacklist = ''; |
| 88 |
|
| 89 |
/** |
| 90 |
* Refer to $blacklist, but we will ignore instead of blocking. |
| 91 |
* |
| 92 |
* @var string |
| 93 |
* @defender_property |
| 94 |
*/ |
| 95 |
public $whitelist = ''; |
| 96 |
|
| 97 |
/** |
| 98 |
* A message to display on frontend when an IP is locked out. |
| 99 |
* |
| 100 |
* @var string |
| 101 |
* @defender_property |
| 102 |
* @sanitize sanitize_textarea_field |
| 103 |
*/ |
| 104 |
public $lockout_message = ''; |
| 105 |
|
| 106 |
/** |
| 107 |
* Set to true for enabling the 404 tracking on logged-in user. |
| 108 |
* |
| 109 |
* @var bool |
| 110 |
* @defender_property |
| 111 |
*/ |
| 112 |
public $detect_logged = false; |
| 113 |
|
| 114 |
/** |
| 115 |
* Validation rules. |
| 116 |
* |
| 117 |
* @var array |
| 118 |
*/ |
| 119 |
protected $rules = array( |
| 120 |
array( array( 'enable', 'detect_logged' ), 'boolean' ), |
| 121 |
array( array( 'attempt', 'timeframe', 'duration' ), 'integer' ), |
| 122 |
array( array( 'lockout_type' ), 'in', array( 'timeframe', 'permanent' ) ), |
| 123 |
array( array( 'duration_unit' ), 'in', array( 'seconds', 'minutes', 'hours' ) ), |
| 124 |
); |
| 125 |
|
| 126 |
/** |
| 127 |
* Retrieves the default values for the function. |
| 128 |
* |
| 129 |
* @return array The default values. |
| 130 |
*/ |
| 131 |
public function get_default_values(): array { |
| 132 |
return array( |
| 133 |
'message' => esc_html__( |
| 134 |
'You have been locked out due to too many attempts to access a file that doesn`t exist.', |
| 135 |
'defender-security' |
| 136 |
), |
| 137 |
'whitelist' => ".css\n.js\n.map", |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Initializes the object before loading by setting the lockout message and whitelist properties |
| 143 |
* to their default values. |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
protected function before_load(): void { |
| 148 |
$default_values = $this->get_default_values(); |
| 149 |
$this->lockout_message = $default_values['message']; |
| 150 |
$this->whitelist = $default_values['whitelist']; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Get list of blocklisted or allowlisted data. |
| 155 |
* |
| 156 |
* @param string $type blocklist|allowlist. |
| 157 |
* |
| 158 |
* @return array |
| 159 |
*/ |
| 160 |
public function get_lockout_list( $type = 'blocklist' ): array { |
| 161 |
$data = ( 'blocklist' === $type ) ? $this->blacklist : $this->whitelist; |
| 162 |
$arr = is_array( $data ) |
| 163 |
? $data |
| 164 |
: array_filter( |
| 165 |
explode( PHP_EOL, $data ), |
| 166 |
function ( $line ) { |
| 167 |
return strlen( trim( $line ) ) > 0; |
| 168 |
} |
| 169 |
); |
| 170 |
$arr = array_map( 'trim', $arr ); |
| 171 |
$arr = array_map( 'strtolower', $arr ); |
| 172 |
|
| 173 |
return $arr; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Define settings labels. |
| 178 |
* |
| 179 |
* @return array |
| 180 |
*/ |
| 181 |
public function labels(): array { |
| 182 |
return array( |
| 183 |
// New key 'enabled'. |
| 184 |
'detect_404' => self::get_module_name(), |
| 185 |
// New key 'attempt'. |
| 186 |
'detect_404_threshold' => esc_html__( '404 Detection - Threshold', 'defender-security' ), |
| 187 |
// New key 'timeframe'. |
| 188 |
'detect_404_timeframe' => esc_html__( '404 Protection - Timeframe', 'defender-security' ), |
| 189 |
// New key 'lockout_type'. |
| 190 |
'detect_404_lockout_ban' => esc_html__( '404 Protection - Duration Type', 'defender-security' ), |
| 191 |
// New key 'duration'. |
| 192 |
'detect_404_lockout_duration' => esc_html__( '404 Detection - Duration', 'defender-security' ), |
| 193 |
// New key 'duration_unit'. |
| 194 |
'detect_404_lockout_duration_unit' => esc_html__( '404 Protection - Duration units', 'defender-security' ), |
| 195 |
// New key 'lockout_message'. |
| 196 |
'detect_404_lockout_message' => esc_html__( '404 Detection - Lockout Message', 'defender-security' ), |
| 197 |
// New key 'blacklist'. |
| 198 |
'detect_404_blacklist' => esc_html__( '404 Detection - Files and Folders Blocklist', 'defender-security' ), |
| 199 |
// New key 'whitelist'. |
| 200 |
'detect_404_whitelist' => esc_html__( '404 Detection - Files and Folders Allowlist', 'defender-security' ), |
| 201 |
// New key 'detect_logged'. |
| 202 |
'detect_404_logged' => esc_html__( '404 Detection - Monitor logged in users', 'defender-security' ), |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Returns the module name for 404 Detection. |
| 208 |
* |
| 209 |
* @return string The module name. |
| 210 |
*/ |
| 211 |
public static function get_module_name(): string { |
| 212 |
return esc_html__( '404 Detection', 'defender-security' ); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Returns the module state based on the given flag. |
| 217 |
* |
| 218 |
* @param bool $flag The flag indicating the module state. |
| 219 |
* |
| 220 |
* @return string The module state, either 'active' or 'inactive'. |
| 221 |
*/ |
| 222 |
public static function get_module_state( $flag ): string { |
| 223 |
return $flag ? esc_html__( 'active', 'defender-security' ) : esc_html__( 'inactive', 'defender-security' ); |
| 224 |
} |
| 225 |
} |
| 226 |
|