| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Controller; |
| 4 |
|
| 5 |
use Calotes\Component\Request; |
| 6 |
use Calotes\Component\Response; |
| 7 |
use WP_Defender\Component\Config\Config_Hub_Helper; |
| 8 |
use WP_Defender\Controller; |
| 9 |
use WP_Defender\Traits\Setting; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class UA_Lockout. |
| 13 |
* |
| 14 |
* @package WP_Defender\Controller |
| 15 |
* @since 2.6.0 |
| 16 |
*/ |
| 17 |
class UA_Lockout extends Controller { |
| 18 |
use Setting; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $slug = 'wdf-ip-lockout'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Use for cache. |
| 27 |
* |
| 28 |
* @var \WP_Defender\Model\Setting\User_Agent_Lockout |
| 29 |
*/ |
| 30 |
protected $model; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var \WP_Defender\Component\User_Agent |
| 34 |
*/ |
| 35 |
protected $service; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
protected $module_name; |
| 41 |
|
| 42 |
public function __construct() { |
| 43 |
$this->register_routes(); |
| 44 |
$this->module_name = __( 'User Agent Banning', 'wpdef' ); |
| 45 |
$this->model = $this->get_model(); |
| 46 |
$this->service = wd_di()->get( \WP_Defender\Component\User_Agent::class ); |
| 47 |
add_action( 'defender_enqueue_assets', array( &$this, 'enqueue_assets' ) ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @return \WP_Defender\Model\Setting\User_Agent_Lockout |
| 52 |
*/ |
| 53 |
private function get_model() { |
| 54 |
if ( is_object( $this->model ) ) { |
| 55 |
return $this->model; |
| 56 |
} |
| 57 |
|
| 58 |
return new \WP_Defender\Model\Setting\User_Agent_Lockout(); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* Queue assets and require data. |
| 64 |
*/ |
| 65 |
public function enqueue_assets() { |
| 66 |
if ( ! $this->is_page_active() ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
wp_localize_script( 'def-iplockout', 'ua_lockout', $this->data_frontend() ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
/** |
| 74 |
* Save settings. |
| 75 |
* @param Request $request |
| 76 |
* |
| 77 |
* @return Response |
| 78 |
* @defender_route |
| 79 |
*/ |
| 80 |
public function save_settings( Request $request ) { |
| 81 |
$data = $request->get_data_by_model( $this->model ); |
| 82 |
$old_enabled = (bool) $this->model->enabled; |
| 83 |
|
| 84 |
$this->model->import( $data ); |
| 85 |
if ( $this->model->validate() ) { |
| 86 |
$this->model->save(); |
| 87 |
Config_Hub_Helper::set_clear_active_flag(); |
| 88 |
|
| 89 |
return new Response( |
| 90 |
true, |
| 91 |
array_merge( |
| 92 |
array( |
| 93 |
'message' => $this->get_update_message( $data, $old_enabled, $this->module_name ), |
| 94 |
), |
| 95 |
$this->data_frontend() |
| 96 |
) |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
return new Response( |
| 101 |
false, |
| 102 |
array( |
| 103 |
'message' => $this->model->get_formatted_errors(), |
| 104 |
) |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
public function remove_settings() {} |
| 109 |
|
| 110 |
public function remove_data() {} |
| 111 |
|
| 112 |
public function to_array() {} |
| 113 |
|
| 114 |
/** |
| 115 |
* @return array |
| 116 |
*/ |
| 117 |
public function data_frontend() { |
| 118 |
$arr_model = $this->model->export(); |
| 119 |
|
| 120 |
return array_merge( |
| 121 |
array( |
| 122 |
'model' => $arr_model, |
| 123 |
'module_name' => $this->module_name, |
| 124 |
'misc' => array( |
| 125 |
'no_ua' => '' === $arr_model['blacklist'] && '' === $arr_model['whitelist'], |
| 126 |
), |
| 127 |
), |
| 128 |
$this->dump_routes_and_nonces() |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
public function import_data( $data ) { |
| 133 |
$model = $this->get_model(); |
| 134 |
|
| 135 |
$model->import( $data ); |
| 136 |
if ( $model->validate() ) { |
| 137 |
$model->save(); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* @defender_route |
| 143 |
*/ |
| 144 |
public function export_ua() { |
| 145 |
$data = array(); |
| 146 |
|
| 147 |
foreach ( $this->model->get_lockout_list( 'blocklist', false ) as $ua ) { |
| 148 |
$data[] = array( |
| 149 |
'ua' => $ua, |
| 150 |
'type' => 'blocklist', |
| 151 |
); |
| 152 |
} |
| 153 |
foreach ( $this->model->get_lockout_list( 'allowlist', false ) as $ua ) { |
| 154 |
$data[] = array( |
| 155 |
'ua' => $ua, |
| 156 |
'type' => 'allowlist', |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
$fp = fopen( 'php://memory', 'w' ); |
| 161 |
foreach ( $data as $fields ) { |
| 162 |
fputcsv( $fp, $fields ); |
| 163 |
} |
| 164 |
$filename = 'wdf-ua-export-' . gmdate( 'ymdHis' ) . '.csv'; |
| 165 |
fseek( $fp, 0 ); |
| 166 |
header( 'Content-Type: text/csv' ); |
| 167 |
header( 'Content-Disposition: attachment; filename="' . $filename . '";' ); |
| 168 |
// Make php send the generated csv lines to the browser. |
| 169 |
fpassthru( $fp ); |
| 170 |
exit(); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Importing UAs from exporter. |
| 175 |
* |
| 176 |
* @param Request $request |
| 177 |
* |
| 178 |
* @defender_route |
| 179 |
* @return Response |
| 180 |
*/ |
| 181 |
public function import_ua( Request $request ) { |
| 182 |
$data = $request->get_data( |
| 183 |
array( |
| 184 |
'id' => array( |
| 185 |
'type' => 'int', |
| 186 |
), |
| 187 |
) |
| 188 |
); |
| 189 |
|
| 190 |
$attached_id = $data['id']; |
| 191 |
if ( ! is_object( get_post( $attached_id ) ) ) { |
| 192 |
return new Response( |
| 193 |
false, |
| 194 |
array( |
| 195 |
'message' => __( 'Your file is invalid!', 'wpdef' ), |
| 196 |
) |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
$file = get_attached_file( $attached_id ); |
| 201 |
if ( ! is_file( $file ) ) { |
| 202 |
return new Response( |
| 203 |
false, |
| 204 |
array( |
| 205 |
'message' => __( 'Your file is invalid!', 'wpdef' ), |
| 206 |
) |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
$data = $this->service->verify_import_file( $file ); |
| 211 |
if ( ! $data ) { |
| 212 |
return new Response( |
| 213 |
false, |
| 214 |
array( |
| 215 |
'message' => __( 'Your file content is invalid! Please use a CSV file format and try again.', 'wpdef' ), |
| 216 |
) |
| 217 |
); |
| 218 |
} |
| 219 |
|
| 220 |
// All good, start to import. |
| 221 |
foreach ( $data as $line ) { |
| 222 |
$this->model->add_to_list( $line[0], $line[1] ); |
| 223 |
} |
| 224 |
|
| 225 |
return new Response( |
| 226 |
true, |
| 227 |
array( |
| 228 |
'message' => __( 'Your blocklist and allowlist have been successfully imported.', 'wpdef' ), |
| 229 |
'interval' => 1, |
| 230 |
) |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* @return array |
| 236 |
*/ |
| 237 |
public function export_strings() { |
| 238 |
return array(); |
| 239 |
} |
| 240 |
} |
| 241 |
|