| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Controller; |
| 4 |
|
| 5 |
use Calotes\Helper\Route; |
| 6 |
use WP_Defender\Behavior\WPMUDEV; |
| 7 |
use WP_Defender\Controller; |
| 8 |
use WP_Defender\Model\Setting\Login_Lockout; |
| 9 |
use WP_Defender\Model\Setting\Notfound_Lockout; |
| 10 |
use WP_Defender\Model\Setting\User_Agent_Lockout; |
| 11 |
|
| 12 |
/** |
| 13 |
* This class is only used once, after the activation on a fresh install. |
| 14 |
* We will use this for activating & presets other module settings. |
| 15 |
* |
| 16 |
* Class Onboard |
| 17 |
* @package WP_Defender\Controller |
| 18 |
*/ |
| 19 |
class Onboard extends Controller { |
| 20 |
public $slug = 'wp-defender'; |
| 21 |
|
| 22 |
public function __construct() { |
| 23 |
$this->attach_behavior( WPMUDEV::class, WPMUDEV::class ); |
| 24 |
$this->add_main_page(); |
| 25 |
add_action( 'defender_enqueue_assets', [ &$this, 'enqueue_assets' ] ); |
| 26 |
add_filter( 'custom_menu_order', '__return_true' ); |
| 27 |
add_filter( 'menu_order', [ &$this, 'menu_order' ] ); |
| 28 |
} |
| 29 |
|
| 30 |
public function menu_order( $menu_order ) { |
| 31 |
global $submenu; |
| 32 |
// We don't need all sub menu when in activation mode. |
| 33 |
unset( $submenu['wp-defender'] ); |
| 34 |
|
| 35 |
return $menu_order; |
| 36 |
} |
| 37 |
|
| 38 |
protected function add_main_page() { |
| 39 |
$this->register_page( $this->get_menu_title(), $this->parent_slug, [ |
| 40 |
&$this, |
| 41 |
'main_view' |
| 42 |
], null, $this->get_menu_icon() ); |
| 43 |
} |
| 44 |
|
| 45 |
public function main_view() { |
| 46 |
$class = wd_di()->get( Security_Tweaks::class ); |
| 47 |
$class->refresh_tweaks_status(); |
| 48 |
$this->render( 'main' ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @defender_route |
| 53 |
*/ |
| 54 |
public function activating() { |
| 55 |
if ( ! $this->check_permission() || ! $this->verify_nonce( 'activating' . 'onboard' ) ) { |
| 56 |
wp_send_json_error( |
| 57 |
array( |
| 58 |
'message' => __( 'Invalid', 'wpdef' ), |
| 59 |
) |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
$this->attach_behavior( WPMUDEV::class, WPMUDEV::class ); |
| 64 |
|
| 65 |
update_site_option( 'wp_defender_shown_activator', true ); |
| 66 |
delete_site_option( 'wp_defender_is_free_activated' ); |
| 67 |
if ( $this->is_pro() ) { |
| 68 |
$this->preset_audit(); |
| 69 |
$this->preset_blacklist_monitor(); |
| 70 |
} |
| 71 |
$this->preset_firewall(); |
| 72 |
$this->resolve_security_tweaks(); |
| 73 |
$this->preset_scanning(); |
| 74 |
|
| 75 |
wp_send_json_success(); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Enable blacklist status. |
| 80 |
*/ |
| 81 |
private function preset_blacklist_monitor() { |
| 82 |
$this->make_wpmu_request( WPMUDEV::API_BLACKLIST, [], [ |
| 83 |
'method' => 'POST' |
| 84 |
] ); |
| 85 |
} |
| 86 |
|
| 87 |
private function preset_audit() { |
| 88 |
$audit = new \WP_Defender\Model\Setting\Audit_Logging(); |
| 89 |
$audit->enabled = true; |
| 90 |
$audit->save(); |
| 91 |
} |
| 92 |
|
| 93 |
private function preset_scanning() { |
| 94 |
$model = new \WP_Defender\Model\Setting\Scan(); |
| 95 |
$model->save(); |
| 96 |
// Create new scan. |
| 97 |
$ret = \WP_Defender\Model\Scan::create(); |
| 98 |
if ( ! is_wp_error( $ret ) ) { |
| 99 |
wd_di()->get( Scan::class )->do_async_scan( 'install' ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
private function preset_firewall() { |
| 104 |
$lockout = new Login_Lockout(); |
| 105 |
$lockout->enabled = true; |
| 106 |
$lockout->save(); |
| 107 |
$nf = new Notfound_Lockout(); |
| 108 |
$nf->enabled = true; |
| 109 |
$nf->save(); |
| 110 |
$ua = new User_Agent_Lockout(); |
| 111 |
$ua->enabled = true; |
| 112 |
$ua->save(); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Resolve all tweaks that we can. |
| 117 |
* @since 2.4.6 Remove tweaks that can be added to wp-config.php manually: 'hide-error', 'disable-file-editor'. |
| 118 |
*/ |
| 119 |
private function resolve_security_tweaks() { |
| 120 |
$slugs = [ |
| 121 |
'disable-xml-rpc', |
| 122 |
'login-duration', |
| 123 |
'disable-trackback', |
| 124 |
'prevent-enum-users', |
| 125 |
]; |
| 126 |
$class = wd_di()->get( Security_Tweaks::class ); |
| 127 |
$class->refresh_tweaks_status(); |
| 128 |
$class->security_tweaks_auto_action( $slugs, 'resolve' ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @defender_route |
| 133 |
*/ |
| 134 |
public function skip() { |
| 135 |
if ( ! $this->check_permission() || ! $this->verify_nonce( 'skip' . 'onboard' ) ) { |
| 136 |
wp_send_json_error( |
| 137 |
array( |
| 138 |
'message' => __( 'Invalid', 'wpdef' ), |
| 139 |
) |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
update_site_option( 'wp_defender_shown_activator', true ); |
| 144 |
delete_site_option( 'wp_defender_is_free_activated' ); |
| 145 |
|
| 146 |
wp_send_json_success(); |
| 147 |
} |
| 148 |
|
| 149 |
public function enqueue_assets() { |
| 150 |
if ( ! $this->is_page_active() ) { |
| 151 |
return; |
| 152 |
} |
| 153 |
[$endpoints, $nonces] = Route::export_routes( 'onboard' ); |
| 154 |
wp_localize_script( 'def-onboard', 'onboard', [ |
| 155 |
'endpoints' => $endpoints, |
| 156 |
'nonces' => $nonces |
| 157 |
] ); |
| 158 |
wp_enqueue_script( 'def-onboard' ); |
| 159 |
$this->enqueue_main_assets(); |
| 160 |
} |
| 161 |
|
| 162 |
public function remove_settings() {} |
| 163 |
|
| 164 |
public function remove_data() {} |
| 165 |
|
| 166 |
public function export_strings() {} |
| 167 |
|
| 168 |
public function to_array() {} |
| 169 |
|
| 170 |
public function import_data( $data ) {} |
| 171 |
|
| 172 |
public function data_frontend() {} |
| 173 |
} |
| 174 |
|