| 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 |
|
| 10 |
/** |
| 11 |
* Class Password_Protection |
| 12 |
* |
| 13 |
* @package WP_Defender\Controller |
| 14 |
*/ |
| 15 |
class Password_Protection extends Controller { |
| 16 |
/** |
| 17 |
* Use for cache. |
| 18 |
* |
| 19 |
* @var \WP_Defender\Model\Setting\Password_Protection |
| 20 |
*/ |
| 21 |
public $model; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var \WP_Defender\Component\Password_Protection |
| 25 |
*/ |
| 26 |
public $service; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public $default_msg; |
| 32 |
|
| 33 |
public function __construct() { |
| 34 |
$this->model = wd_di()->get( \WP_Defender\Model\Setting\Password_Protection::class ); |
| 35 |
$this->service = wd_di()->get( \WP_Defender\Component\Password_Protection::class ); |
| 36 |
$default_values = $this->model->get_default_values(); |
| 37 |
$this->default_msg = $default_values['message']; |
| 38 |
add_filter( 'wp_defender_advanced_tools_data', array( $this, 'script_data' ) ); |
| 39 |
$this->register_routes(); |
| 40 |
if ( $this->model->is_active() ) { |
| 41 |
// Update site url on sub-site when MaskLogin is disabled. |
| 42 |
if ( |
| 43 |
is_multisite() && ! is_main_site() |
| 44 |
&& ! wd_di()->get( \WP_Defender\Model\Setting\Mask_Login::class )->is_active() |
| 45 |
) { |
| 46 |
add_filter( 'network_site_url', array( &$this, 'filter_site_url' ), 100, 2 ); |
| 47 |
} |
| 48 |
add_action( 'wp_authenticate_user', array( $this, 'handle_login_password' ), 100, 2 ); |
| 49 |
add_action( 'validate_password_reset', array( $this, 'handle_reset_check_password' ), 100, 2 ); |
| 50 |
add_action( 'user_profile_update_errors', array( $this, 'handle_profile_update_password' ), 1, 3 ); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Update 'network_site_url' if: |
| 56 |
* not empty URL path, |
| 57 |
* it's link to reset password. |
| 58 |
* @param string $url |
| 59 |
* @param string $path |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
public function filter_site_url( $url, $path ) { |
| 64 |
if ( $path && is_string( $path ) |
| 65 |
&& ! empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'rp', 'resetpass' ), true ) |
| 66 |
&& false !== stristr( $url, 'wp-login.php' ) |
| 67 |
) { |
| 68 |
return get_option( 'siteurl' ) . '/' . ltrim( $path, '/' ); |
| 69 |
} |
| 70 |
return $url; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Handle user login password. |
| 75 |
* If pwned password found during login then redirect to reset password page to reset password. |
| 76 |
* |
| 77 |
* @param \WP_User|\WP_Error $user WP_User object or WP_Error. |
| 78 |
* @param string $password Password plain string. |
| 79 |
* |
| 80 |
* @return \WP_User|\WP_Error Return user object or error object. |
| 81 |
*/ |
| 82 |
public function handle_login_password( $user, $password ) { |
| 83 |
$this->service->do_weak_reset( $user, $password ); |
| 84 |
|
| 85 |
return $user; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Handle password update on password reset. |
| 90 |
* |
| 91 |
* @param \WP_Error $errors |
| 92 |
* |
| 93 |
* @return mixed |
| 94 |
*/ |
| 95 |
public function handle_reset_check_password( $errors, $user ) { |
| 96 |
if ( is_wp_error( $user ) ) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
if ( ! $this->service->is_enabled_by_user_role( $user, $this->model->user_roles ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
// Check if display_pwned_password_warning cookie enabled then show warning message on reset password page. |
| 105 |
if ( isset( $_COOKIE['display_pwned_password_warning'] ) ) { |
| 106 |
$message = empty( $this->model->pwned_actions['force_change_message'] ) |
| 107 |
? $this->default_msg |
| 108 |
: $this->model->pwned_actions['force_change_message']; |
| 109 |
$errors->add( 'defender_password_protection', $message ); |
| 110 |
// Remove the one time cookie notice once it's displayed. |
| 111 |
$this->service->remove_cookie_notice( 'display_pwned_password_warning', true ); |
| 112 |
} |
| 113 |
|
| 114 |
$login_password = $this->service->get_submitted_password(); |
| 115 |
if ( $login_password ) { |
| 116 |
$is_pwned = $this->service->check_pwned_password( $login_password ); |
| 117 |
if ( is_wp_error( $is_pwned ) ) { |
| 118 |
return $errors; |
| 119 |
} |
| 120 |
|
| 121 |
if ( $is_pwned ) { |
| 122 |
$message = empty( $this->model->pwned_actions['force_change_message'] ) |
| 123 |
? $this->default_msg |
| 124 |
: $this->model->pwned_actions['force_change_message']; |
| 125 |
$errors->add( 'defender_password_protection', $message ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return $errors; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Handle password update on new user registration and user profile update. |
| 134 |
* |
| 135 |
* @param \WP_Error $errors |
| 136 |
* @param string $update |
| 137 |
* @param \WP_User $user |
| 138 |
* |
| 139 |
* @return \WP_Error|void |
| 140 |
*/ |
| 141 |
public function handle_profile_update_password( $errors, $update, $user ) { |
| 142 |
if ( $errors->get_error_message( 'pass' ) || |
| 143 |
is_wp_error( $user ) || |
| 144 |
! isset( $user->user_pass ) |
| 145 |
) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
// When updating the profile check if user's role preference is enabled. |
| 150 |
if ( $update && ! $this->service->is_enabled_by_user_role( $user, $this->model->user_roles ) ) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
$login_password = $this->service->get_submitted_password(); |
| 155 |
if ( $login_password ) { |
| 156 |
$is_pwned = $this->service->check_pwned_password( $login_password ); |
| 157 |
if ( is_wp_error( $is_pwned ) ) { |
| 158 |
return $errors; |
| 159 |
} |
| 160 |
|
| 161 |
if ( $is_pwned ) { |
| 162 |
$message = empty( $this->model->pwned_actions['force_change_message'] ) |
| 163 |
? $this->default_msg |
| 164 |
: $this->model->pwned_actions['force_change_message']; |
| 165 |
$errors->add( 'defender_password_protection', $message ); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return $errors; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* @return \WP_Defender\Model\Setting\Password_Protection |
| 174 |
*/ |
| 175 |
private function get_model() { |
| 176 |
if ( is_object( $this->model ) ) { |
| 177 |
return $this->model; |
| 178 |
} |
| 179 |
|
| 180 |
return new \WP_Defender\Model\Setting\Password_Protection(); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* @param array $data |
| 185 |
* |
| 186 |
* @return array |
| 187 |
*/ |
| 188 |
public function script_data( $data ) { |
| 189 |
$data['password_protection'] = $this->data_frontend(); |
| 190 |
|
| 191 |
return $data; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Save settings. |
| 196 |
* @param Request $request |
| 197 |
* |
| 198 |
* @return Response |
| 199 |
* @defender_route |
| 200 |
*/ |
| 201 |
public function save_settings( Request $request ) { |
| 202 |
$model_data = $request->get_data_by_model( $this->model ); |
| 203 |
$data = $request->get_data( |
| 204 |
array( |
| 205 |
'intention' => array( |
| 206 |
'type' => 'string', |
| 207 |
'sanitize' => 'sanitize_text_field', |
| 208 |
), |
| 209 |
) |
| 210 |
); |
| 211 |
|
| 212 |
$this->model->import( $model_data ); |
| 213 |
if ( $this->model->validate() ) { |
| 214 |
$this->model->save(); |
| 215 |
Config_Hub_Helper::set_clear_active_flag(); |
| 216 |
|
| 217 |
$response = array( |
| 218 |
'message' => __( 'Your settings have been updated.', 'wpdef' ), |
| 219 |
); |
| 220 |
if ( $data && 'save_settings' === $data['intention'] && ! $this->model->is_active() ) { |
| 221 |
$response['type_notice'] = 'warning'; |
| 222 |
$response['message'] = __( 'You need to check at least one of the <b>Pwned checks preferences below</b> and save your settings to enable Password Protection.', 'wpdef' ); |
| 223 |
} |
| 224 |
|
| 225 |
return new Response( true, array_merge( $response, $this->data_frontend() ) ); |
| 226 |
} |
| 227 |
|
| 228 |
return new Response( |
| 229 |
false, |
| 230 |
array( |
| 231 |
'message' => $this->model->get_formatted_errors(), |
| 232 |
) |
| 233 |
); |
| 234 |
} |
| 235 |
|
| 236 |
public function remove_settings() {} |
| 237 |
|
| 238 |
public function remove_data() {} |
| 239 |
|
| 240 |
/** |
| 241 |
* @return array |
| 242 |
*/ |
| 243 |
public function data_frontend() { |
| 244 |
$model = $this->get_model(); |
| 245 |
|
| 246 |
return array_merge( |
| 247 |
array( |
| 248 |
'is_active' => $model->is_active(), |
| 249 |
'model' => $model->export(), |
| 250 |
'all_roles' => wp_list_pluck( get_editable_roles(), 'name' ), |
| 251 |
'default_message' => $this->default_msg, |
| 252 |
), |
| 253 |
$this->dump_routes_and_nonces() |
| 254 |
); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* @return array |
| 259 |
*/ |
| 260 |
public function dashboard_widget() { |
| 261 |
return array( 'model' => $this->get_model()->export() ); |
| 262 |
} |
| 263 |
|
| 264 |
public function import_data( $data ) { |
| 265 |
$model = $this->get_model(); |
| 266 |
|
| 267 |
$model->import( $data ); |
| 268 |
if ( $model->validate() ) { |
| 269 |
$model->save(); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
public function to_array() {} |
| 274 |
|
| 275 |
public function export_strings() { |
| 276 |
|
| 277 |
return array( |
| 278 |
$this->model->is_active() ? __( 'Active', 'wpdef' ) : __( 'Inactive', 'wpdef' ), |
| 279 |
); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* @param array $config |
| 284 |
* @param bool $is_pro |
| 285 |
* |
| 286 |
* @return array |
| 287 |
*/ |
| 288 |
public function config_strings( $config, $is_pro ) { |
| 289 |
if ( empty( $config['enabled'] ) || empty( $config['user_roles'] ) ) { |
| 290 |
return array( __( 'Inactive', 'wpdef' ) ); |
| 291 |
} |
| 292 |
|
| 293 |
return array( |
| 294 |
$config['enabled'] && (is_array($config['user_roles']) || $config['user_roles'] instanceof \Countable ? count( $config['user_roles'] ) : 0) > 0 |
| 295 |
? __( 'Active', 'wpdef' ) |
| 296 |
: __( 'Inactive', 'wpdef' ) |
| 297 |
); |
| 298 |
} |
| 299 |
} |
| 300 |
|