| 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\Traits\IP; |
| 11 |
use WP_Defender\Traits\Setting; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Login_Lockout |
| 15 |
* @package WP_Defender\Controller |
| 16 |
*/ |
| 17 |
class Login_Lockout extends Controller { |
| 18 |
use IP, Setting; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $slug = 'wdf-ip-lockout'; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var \WP_Defender\Component\Login_Lockout |
| 27 |
*/ |
| 28 |
protected $service; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var \WP_Defender\Model\Setting\Login_Lockout |
| 32 |
*/ |
| 33 |
protected $model; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
protected $module_name; |
| 39 |
|
| 40 |
public function __construct() { |
| 41 |
$this->register_routes(); |
| 42 |
$this->module_name = __( 'Login Protection', 'wpdef' ); |
| 43 |
add_action( 'defender_enqueue_assets', array( &$this, 'enqueue_assets' ) ); |
| 44 |
$this->model = wd_di()->get( \WP_Defender\Model\Setting\Login_Lockout::class ); |
| 45 |
$this->service = wd_di()->get( \WP_Defender\Component\Login_Lockout::class ); |
| 46 |
$service = wd_di()->get( Blacklist_Lockout::class ); |
| 47 |
if ( $this->model->enabled && ! $service->is_ip_whitelisted( $this->get_user_ip() ) ) { |
| 48 |
$this->service->add_hooks(); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @param Request $request |
| 54 |
* @defender_route |
| 55 |
* @return Response |
| 56 |
*/ |
| 57 |
public function save_settings( Request $request ) { |
| 58 |
$data = $request->get_data_by_model( $this->model ); |
| 59 |
$old_enabled = (bool) $this->model->enabled; |
| 60 |
|
| 61 |
$this->model->import( $data ); |
| 62 |
if ( $this->model->validate() ) { |
| 63 |
$this->model->save(); |
| 64 |
Config_Hub_Helper::set_clear_active_flag(); |
| 65 |
|
| 66 |
return new Response( true, array_merge( [ |
| 67 |
'message' => $this->get_update_message( $data, $old_enabled, $this->module_name ), |
| 68 |
], $this->data_frontend() ) ); |
| 69 |
} |
| 70 |
|
| 71 |
return new Response( false, [ |
| 72 |
'message' => $this->model->get_formatted_errors() |
| 73 |
] ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Queue assets and require data. |
| 78 |
*/ |
| 79 |
public function enqueue_assets() { |
| 80 |
if ( ! $this->is_page_active() ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
wp_localize_script( 'def-iplockout', 'login_lockout', $this->data_frontend() ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* All the variables that we will show on frontend, both in the main page, or dashboard widget. |
| 88 |
* |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
public function data_frontend() { |
| 92 |
|
| 93 |
return array_merge( [ |
| 94 |
'model' => $this->model->export(), |
| 95 |
'misc' => [ |
| 96 |
'host' => defender_get_hostname() |
| 97 |
], |
| 98 |
'module_name' => $this->module_name, |
| 99 |
], $this->dump_routes_and_nonces() ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Export the data of this module, we will use this for export to HUB, create a preset etc. |
| 104 |
*/ |
| 105 |
public function to_array() {} |
| 106 |
|
| 107 |
private function adapt_data( $data ) { |
| 108 |
$adapted_data = array(); |
| 109 |
if ( isset( $data['login_protection'] ) ) { |
| 110 |
$adapted_data['enabled'] = $data['login_protection']; |
| 111 |
} |
| 112 |
if ( isset( $data['login_protection_login_attempt'] ) ) { |
| 113 |
$adapted_data['attempt'] = $data['login_protection_login_attempt']; |
| 114 |
} |
| 115 |
if ( isset( $data['login_protection_lockout_timeframe'] ) ) { |
| 116 |
$adapted_data['timeframe'] = $data['login_protection_lockout_timeframe']; |
| 117 |
} |
| 118 |
if ( isset( $data['login_protection_lockout_duration'] ) ) { |
| 119 |
$adapted_data['duration'] = $data['login_protection_lockout_duration']; |
| 120 |
} |
| 121 |
if ( isset( $data['login_protection_lockout_duration_unit'] ) ) { |
| 122 |
$adapted_data['duration_unit'] = $data['login_protection_lockout_duration_unit']; |
| 123 |
} |
| 124 |
if ( isset( $data['login_protection_lockout_ban'] ) ) { |
| 125 |
$adapted_data['lockout_type'] = 'permanent' === $data['login_protection_lockout_ban'] ? 'permanent' : 'timeframe'; |
| 126 |
} |
| 127 |
if ( isset( $data['login_protection_lockout_message'] ) ) { |
| 128 |
$adapted_data['lockout_message'] = $data['login_protection_lockout_message']; |
| 129 |
} |
| 130 |
if ( isset( $data['username_blacklist'] ) ) { |
| 131 |
$adapted_data['username_blacklist'] = $data['username_blacklist']; |
| 132 |
} |
| 133 |
|
| 134 |
return array_merge( $data, $adapted_data ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Import the data of other source into this, it can be when HUB trigger the import, or user apply a preset. |
| 139 |
* |
| 140 |
* @param array $data |
| 141 |
* |
| 142 |
* @return mixed |
| 143 |
*/ |
| 144 |
public function import_data( $data ) { |
| 145 |
if ( ! empty( $data ) ) { |
| 146 |
// Upgrade for old versions. |
| 147 |
$data = $this->adapt_data( $data ); |
| 148 |
} else { |
| 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 |
|