| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Controller; |
| 4 |
|
| 5 |
use Calotes\Component\Request; |
| 6 |
use Calotes\Component\Response; |
| 7 |
use WP_Defender\Component\Blacklist_Lockout; |
| 8 |
use WP_Defender\Component\Config\Config_Hub_Helper; |
| 9 |
use WP_Defender\Controller; |
| 10 |
use WP_Defender\Model\Setting\Notfound_Lockout; |
| 11 |
use WP_Defender\Traits\IP; |
| 12 |
use WP_Defender\Traits\Setting; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Nf_Lockout |
| 16 |
* @package WP_Defender\Controller |
| 17 |
*/ |
| 18 |
class Nf_Lockout extends Controller { |
| 19 |
use IP, Setting; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $slug = 'wdf-ip-lockout'; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var \WP_Defender\Component\Notfound_Lockout |
| 28 |
*/ |
| 29 |
protected $service; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var Notfound_Lockout |
| 33 |
*/ |
| 34 |
protected $model; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected $module_name; |
| 40 |
|
| 41 |
public function __construct() { |
| 42 |
$this->register_routes(); |
| 43 |
$this->module_name = __( '404 Detection', 'wpdef' ); |
| 44 |
add_action( 'defender_enqueue_assets', array( &$this, 'enqueue_assets' ) ); |
| 45 |
$this->model = wd_di()->get( Notfound_Lockout::class ); |
| 46 |
$this->service = wd_di()->get( \WP_Defender\Component\Notfound_Lockout::class ); |
| 47 |
$service = wd_di()->get( Blacklist_Lockout::class ); |
| 48 |
if ( $this->model->enabled && ! $service->is_ip_whitelisted( $this->get_user_ip() ) ) { |
| 49 |
$this->service->add_hooks(); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Queue assets and require data |
| 55 |
*/ |
| 56 |
public function enqueue_assets() { |
| 57 |
if ( ! $this->is_page_active() ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
wp_localize_script( 'def-iplockout', 'nf_lockout', $this->data_frontend() ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @defender_route |
| 65 |
*/ |
| 66 |
public function save_settings( Request $request ) { |
| 67 |
$data = $request->get_data( $this->request_filter_rules() ); |
| 68 |
$old_enabled = (bool) $this->model->enabled; |
| 69 |
|
| 70 |
$this->model->import( $data ); |
| 71 |
if ( $this->model->validate() ) { |
| 72 |
$this->model->save(); |
| 73 |
Config_Hub_Helper::set_clear_active_flag(); |
| 74 |
|
| 75 |
return new Response( true, array_merge( [ |
| 76 |
'message' => $this->get_update_message( $data, $old_enabled, $this->module_name ), |
| 77 |
], $this->data_frontend() ) ); |
| 78 |
} |
| 79 |
|
| 80 |
return new Response( false, [ |
| 81 |
'message' => $this->model->get_formatted_errors() |
| 82 |
] ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* All the variables that we will show on frontend, both in the main page, or dashboard widget. |
| 87 |
* |
| 88 |
* @return array |
| 89 |
*/ |
| 90 |
public function data_frontend() { |
| 91 |
return array_merge( [ |
| 92 |
'model' => $this->model->export(), |
| 93 |
'module_name' => $this->module_name, |
| 94 |
], $this->dump_routes_and_nonces() ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Export the data of this module, we will use this for export to HUB, create a preset etc. |
| 99 |
*/ |
| 100 |
public function to_array() {} |
| 101 |
|
| 102 |
private function adapt_data( $data ) { |
| 103 |
$adapted_data = array(); |
| 104 |
if ( isset( $data['detect_404'] ) ) { |
| 105 |
$adapted_data['enabled'] = $data['detect_404']; |
| 106 |
} |
| 107 |
if ( isset( $data['detect_404_threshold'] ) ) { |
| 108 |
$adapted_data['attempt'] = $data['detect_404_threshold']; |
| 109 |
} |
| 110 |
if ( isset( $data['detect_404_timeframe'] ) ) { |
| 111 |
$adapted_data['timeframe'] = $data['detect_404_timeframe']; |
| 112 |
} |
| 113 |
if ( isset( $data['detect_404_lockout_duration'] ) ) { |
| 114 |
$adapted_data['duration'] = $data['detect_404_lockout_duration']; |
| 115 |
} |
| 116 |
if ( isset( $data['detect_404_lockout_duration_unit'] ) ) { |
| 117 |
$adapted_data['duration_unit'] = $data['detect_404_lockout_duration_unit']; |
| 118 |
} |
| 119 |
if ( isset( $data['detect_404_lockout_ban'] ) ) { |
| 120 |
$adapted_data['lockout_type'] = 'permanent' === $data['detect_404_lockout_ban'] ? 'permanent' : 'timeframe'; |
| 121 |
} |
| 122 |
if ( isset( $data['detect_404_blacklist'] ) ) { |
| 123 |
$adapted_data['blacklist'] = $data['detect_404_blacklist']; |
| 124 |
} |
| 125 |
if ( isset( $data['detect_404_whitelist'] ) ) { |
| 126 |
$adapted_data['whitelist'] = $data['detect_404_whitelist']; |
| 127 |
} |
| 128 |
if ( isset( $data['detect_404_lockout_message'] ) ) { |
| 129 |
$adapted_data['lockout_message'] = $data['detect_404_lockout_message']; |
| 130 |
} |
| 131 |
if ( isset( $data['detect_404_logged'] ) ) { |
| 132 |
$adapted_data['detect_logged'] = $data['detect_404_logged']; |
| 133 |
} |
| 134 |
|
| 135 |
return array_merge( $data, $adapted_data ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Import the data of other source into this, it can be when HUB trigger the import, or user apply a preset. |
| 140 |
* @param array $data |
| 141 |
* |
| 142 |
* @return mixed |
| 143 |
*/ |
| 144 |
public function import_data( $data ) { |
| 145 |
if ( ! empty( $data ) ) { |
| 146 |
$data = $this->adapt_data( $data ); |
| 147 |
} else { |
| 148 |
|
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
$model = $this->model; |
| 153 |
$model->import( $data ); |
| 154 |
if ( $model->validate() ) { |
| 155 |
$model->save(); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Remove all settings, configs generated in this container runtime. |
| 161 |
*/ |
| 162 |
public function remove_settings() {} |
| 163 |
|
| 164 |
/** |
| 165 |
* Remove all data. |
| 166 |
*/ |
| 167 |
public function remove_data() {} |
| 168 |
|
| 169 |
/** |
| 170 |
* @return array |
| 171 |
*/ |
| 172 |
public function export_strings() { |
| 173 |
return []; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Rules for request data |
| 178 |
* |
| 179 |
* @return array |
| 180 |
*/ |
| 181 |
private function request_filter_rules() { |
| 182 |
return [ |
| 183 |
'enabled' => [ |
| 184 |
'type' => 'boolean' |
| 185 |
], |
| 186 |
'attempt' => [ |
| 187 |
'type' => 'int', |
| 188 |
'sanitize' => 'sanitize_text_field' |
| 189 |
], |
| 190 |
'duration' => [ |
| 191 |
'type' => 'int', |
| 192 |
'sanitize' => 'sanitize_text_field' |
| 193 |
], |
| 194 |
'duration_unit' => [ |
| 195 |
'type' => 'string', |
| 196 |
'sanitize' => 'sanitize_text_field' |
| 197 |
], |
| 198 |
'lockout_message' => [ |
| 199 |
'type' => 'string', |
| 200 |
'sanitize' => 'sanitize_text_field' |
| 201 |
], |
| 202 |
'lockout_type' => [ |
| 203 |
'type' => 'string', |
| 204 |
'sanitize' => 'sanitize_text_field' |
| 205 |
], |
| 206 |
'timeframe' => [ |
| 207 |
'type' => 'int', |
| 208 |
'sanitize' => 'sanitize_text_field' |
| 209 |
], |
| 210 |
'blacklist' => [ |
| 211 |
'type' => 'string', |
| 212 |
'sanitize' => 'sanitize_textarea_field' |
| 213 |
], |
| 214 |
'whitelist' => [ |
| 215 |
'type' => 'string', |
| 216 |
'sanitize' => [ |
| 217 |
'rawurldecode', |
| 218 |
'sanitize_textarea_field' |
| 219 |
] |
| 220 |
], |
| 221 |
'detect_logged' => [ |
| 222 |
'type' => 'boolean' |
| 223 |
] |
| 224 |
]; |
| 225 |
} |
| 226 |
} |
| 227 |
|