| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles 2FA 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 2FA settings. |
| 14 |
*/ |
| 15 |
class Two_Fa extends Setting { |
| 16 |
|
| 17 |
public const CUSTOM_GRAPHIC_TYPE_UPLOAD = 'upload', CUSTOM_GRAPHIC_TYPE_LINK = 'link', CUSTOM_GRAPHIC_TYPE_NO = 'no'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Option name. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $table = 'wd_2auth_settings'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Feature status. |
| 28 |
* |
| 29 |
* @defender_property |
| 30 |
* @var bool |
| 31 |
*/ |
| 32 |
public $enabled = false; |
| 33 |
|
| 34 |
/** |
| 35 |
* Allow lost phone recovery option. |
| 36 |
* |
| 37 |
* @defender_property |
| 38 |
* @var bool |
| 39 |
*/ |
| 40 |
public $lost_phone = true; |
| 41 |
|
| 42 |
/** |
| 43 |
* Is 'Force Authentication' checked? |
| 44 |
* |
| 45 |
* @defender_property |
| 46 |
* @var bool |
| 47 |
*/ |
| 48 |
public $force_auth = false; |
| 49 |
|
| 50 |
/** |
| 51 |
* Force authentication message. |
| 52 |
* |
| 53 |
* @var string |
| 54 |
* @defender_property |
| 55 |
* @sanitize sanitize_textarea_field |
| 56 |
*/ |
| 57 |
public $force_auth_mess = ''; |
| 58 |
|
| 59 |
/** |
| 60 |
* Enable/disable 2FA for user roles. |
| 61 |
* |
| 62 |
* @var array |
| 63 |
* @defender_property |
| 64 |
*/ |
| 65 |
public $user_roles = array(); |
| 66 |
|
| 67 |
/** |
| 68 |
* Allowed user roles for 'Force Authentication'. |
| 69 |
* |
| 70 |
* @var array |
| 71 |
* @defender_property |
| 72 |
*/ |
| 73 |
public $force_auth_roles = array(); |
| 74 |
|
| 75 |
/** |
| 76 |
* Enable/disable custom graphic feature. |
| 77 |
* |
| 78 |
* @var bool |
| 79 |
* @defender_property |
| 80 |
*/ |
| 81 |
public $custom_graphic = false; |
| 82 |
|
| 83 |
/** |
| 84 |
* Type of graphic. For example; uploading image, using image link or none. |
| 85 |
* |
| 86 |
* @var string |
| 87 |
* @defender_property |
| 88 |
*/ |
| 89 |
public $custom_graphic_type = ''; |
| 90 |
|
| 91 |
/** |
| 92 |
* Upload image for custom graphic. |
| 93 |
* |
| 94 |
* @var string |
| 95 |
* @defender_property |
| 96 |
*/ |
| 97 |
public $custom_graphic_url = ''; |
| 98 |
|
| 99 |
/** |
| 100 |
* Use image link for custom graphic. |
| 101 |
* |
| 102 |
* @var string |
| 103 |
* @defender_property |
| 104 |
*/ |
| 105 |
public $custom_graphic_link = ''; |
| 106 |
|
| 107 |
/** |
| 108 |
* List of plugins that conflict with 2FA. |
| 109 |
* |
| 110 |
* @var array |
| 111 |
*/ |
| 112 |
public $is_conflict = array(); |
| 113 |
|
| 114 |
/** |
| 115 |
* Email subject. |
| 116 |
* |
| 117 |
* @var string |
| 118 |
* @defender_property |
| 119 |
* @sanitize sanitize_text_field |
| 120 |
*/ |
| 121 |
public $email_subject = ''; |
| 122 |
|
| 123 |
/** |
| 124 |
* Email sender. |
| 125 |
* |
| 126 |
* @var string |
| 127 |
* @defender_property |
| 128 |
* @sanitize sanitize_text_field |
| 129 |
*/ |
| 130 |
public $email_sender = ''; |
| 131 |
|
| 132 |
/** |
| 133 |
* Email body. |
| 134 |
* |
| 135 |
* @defender_property |
| 136 |
* @var string |
| 137 |
* @sanitize sanitize_textarea_field |
| 138 |
*/ |
| 139 |
public $email_body = ''; |
| 140 |
|
| 141 |
/** |
| 142 |
* App title. |
| 143 |
* |
| 144 |
* @var string |
| 145 |
* @defender_property |
| 146 |
* @sanitize sanitize_textarea_field |
| 147 |
*/ |
| 148 |
public $app_title = ''; |
| 149 |
|
| 150 |
/** |
| 151 |
* It's not for DB, no need 'defender_property' annotation. Just text view on different pages. |
| 152 |
* |
| 153 |
* @var string |
| 154 |
*/ |
| 155 |
public $app_text = ''; |
| 156 |
|
| 157 |
/** |
| 158 |
* Detect Woocommerce. |
| 159 |
* |
| 160 |
* @var bool |
| 161 |
* @defender_property |
| 162 |
*/ |
| 163 |
public $detect_woo = false; |
| 164 |
|
| 165 |
/** |
| 166 |
* Get default values. |
| 167 |
* |
| 168 |
* @return array |
| 169 |
*/ |
| 170 |
public function get_default_values(): array { |
| 171 |
return array( |
| 172 |
'custom_graphic_type' => self::CUSTOM_GRAPHIC_TYPE_UPLOAD, |
| 173 |
'custom_graphic_url' => defender_asset_url( '/assets/img/2factor-disabled.svg' ), |
| 174 |
'custom_graphic_link' => '', |
| 175 |
'email_subject' => __( 'Your OTP code', 'defender-security' ), |
| 176 |
'email_sender' => 'admin', |
| 177 |
'email_body' => 'Hi {{display_name}}, |
| 178 |
|
| 179 |
Your temporary password is {{passcode}} |
| 180 |
To complete your login, copy and paste the temporary password into the Password field on the login screen.', |
| 181 |
'app_title' => '', |
| 182 |
'message' => esc_html__( |
| 183 |
'You are required to setup two-factor authentication to use this site.', |
| 184 |
'defender-security' |
| 185 |
), |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Load default values. |
| 191 |
* |
| 192 |
* @return void |
| 193 |
*/ |
| 194 |
protected function before_load(): void { |
| 195 |
// Default we will load all rules. |
| 196 |
$default_values = $this->get_default_values(); |
| 197 |
if ( ! function_exists( 'get_editable_roles' ) ) { |
| 198 |
require_once ABSPATH . '/wp-admin/includes/user.php'; |
| 199 |
} |
| 200 |
$this->user_roles = array_keys( get_editable_roles() ); |
| 201 |
if ( is_multisite() ) { |
| 202 |
$this->user_roles[] = 'super_admin'; |
| 203 |
} |
| 204 |
// Define some other defaults. |
| 205 |
$this->custom_graphic_type = $default_values['custom_graphic_type']; |
| 206 |
$this->custom_graphic_url = $default_values['custom_graphic_url']; |
| 207 |
$this->custom_graphic_link = $default_values['custom_graphic_link']; |
| 208 |
$this->email_subject = $default_values['email_subject']; |
| 209 |
$this->email_sender = $default_values['email_sender']; |
| 210 |
$this->email_body = $default_values['email_body']; |
| 211 |
$this->app_title = '' === $default_values['app_title'] |
| 212 |
? wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) |
| 213 |
: $default_values['app_title']; |
| 214 |
$this->force_auth_mess = $default_values['message']; |
| 215 |
$this->app_text = esc_html__( |
| 216 |
'Open your authentication app and type the 6 digit passcode to log in to your account.', |
| 217 |
'defender-security' |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* After loading, reindexes the user roles array. |
| 223 |
* |
| 224 |
* @return void |
| 225 |
*/ |
| 226 |
protected function after_load(): void { |
| 227 |
$this->user_roles = array_values( $this->user_roles ); |
| 228 |
$this->force_auth_roles = array_values( array_intersect( $this->force_auth_roles, $this->user_roles ) ); |
| 229 |
$this->force_auth = array() !== $this->force_auth_roles; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Check if a plugin is in conflict with the current instance. |
| 234 |
* |
| 235 |
* @param string $plugin The plugin to check for conflict. |
| 236 |
* |
| 237 |
* @return int|bool Returns 0 if the plugin is not in conflict, true if it is in conflict, and false if it is not in conflict |
| 238 |
* but has been marked as not conflicting. |
| 239 |
*/ |
| 240 |
public function is_conflict( $plugin ) { |
| 241 |
if ( in_array( $plugin, $this->is_conflict, true ) ) { |
| 242 |
return true; |
| 243 |
} elseif ( in_array( '!' . $plugin, $this->is_conflict, true ) ) { |
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Marks a plugin as conflicting with the current instance. |
| 252 |
* |
| 253 |
* @param string $plugin The plugin to mark as conflicting. |
| 254 |
* |
| 255 |
* @return void |
| 256 |
*/ |
| 257 |
public function mark_as_conflict( $plugin ): void { |
| 258 |
if ( ! in_array( $plugin, $this->is_conflict, true ) ) { |
| 259 |
$this->is_conflict[] = $plugin; |
| 260 |
$this->save(); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Marks a plugin as not conflicting with the current instance. |
| 266 |
* |
| 267 |
* @param string $plugin The plugin to mark as not conflicting. |
| 268 |
* |
| 269 |
* @return void |
| 270 |
*/ |
| 271 |
public function mark_as_un_conflict( $plugin ): void { |
| 272 |
$i = array_search( $plugin, $this->is_conflict, true ); |
| 273 |
|
| 274 |
if ( false !== $i ) { |
| 275 |
unset( $this->is_conflict[ $i ] ); |
| 276 |
} |
| 277 |
if ( ! in_array( '!' . $plugin, $this->is_conflict, true ) ) { |
| 278 |
$this->is_conflict [] = '!' . $plugin; |
| 279 |
} |
| 280 |
$this->save(); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Define settings labels. |
| 285 |
* |
| 286 |
* @return array |
| 287 |
*/ |
| 288 |
public function labels(): array { |
| 289 |
return array( |
| 290 |
'enabled' => self::get_module_name(), |
| 291 |
'user_roles' => esc_html__( 'Enabled user roles', 'defender-security' ), |
| 292 |
'lost_phone' => esc_html__( 'Allow lost phone recovery option', 'defender-security' ), |
| 293 |
'email_subject' => esc_html__( 'Subject', 'defender-security' ), |
| 294 |
'email_body' => esc_html__( 'Body', 'defender-security' ), |
| 295 |
'email_sender' => esc_html__( 'Sender', 'defender-security' ), |
| 296 |
'force_auth' => esc_html__( 'Force 2FA on user roles', 'defender-security' ), |
| 297 |
'force_auth_roles' => esc_html__( 'Force users to log in with two-factor authentication', 'defender-security' ), |
| 298 |
'force_auth_mess' => esc_html__( 'Force 2FA login warning message', 'defender-security' ), |
| 299 |
'custom_graphic' => esc_html__( 'Custom Graphic', 'defender-security' ), |
| 300 |
'custom_graphic_type' => esc_html__( 'Enable custom graphics above login fields', 'defender-security' ), |
| 301 |
'custom_graphic_url' => esc_html__( 'Upload Graphic', 'defender-security' ), |
| 302 |
'custom_graphic_link' => esc_html__( 'Link Graphic', 'defender-security' ), |
| 303 |
'app_title' => esc_html__( 'App Title', 'defender-security' ), |
| 304 |
'detect_woo' => esc_html__( 'WooCommerce', 'defender-security' ), |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Checks if the 2FA feature is active. |
| 310 |
* |
| 311 |
* @return bool Returns true if the 2FA feature is active, false otherwise. |
| 312 |
* @since 3.12.0 |
| 313 |
*/ |
| 314 |
public function is_active(): bool { |
| 315 |
$enable = apply_filters( 'wd_2fa_enable', $this->enabled ); |
| 316 |
|
| 317 |
return is_bool( $enable ) ? $enable : (bool) $enable; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Retrieves the module name for Two-Factor Authentication. |
| 322 |
* |
| 323 |
* @return string The module name. |
| 324 |
*/ |
| 325 |
public static function get_module_name(): string { |
| 326 |
return esc_html__( 'Two-Factor Authentication', 'defender-security' ); |
| 327 |
} |
| 328 |
} |
| 329 |
|