| 1 |
<?php |
| 2 |
/** |
| 3 |
* The advanced tools class. |
| 4 |
* |
| 5 |
* @package WP_Defender\Controller |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Controller; |
| 9 |
|
| 10 |
use WP_Defender\Event; |
| 11 |
use WP_Defender\Behavior\WPMUDEV; |
| 12 |
use WP_Defender\Component\Breadcrumbs; |
| 13 |
use WP_Defender\Integrations\MaxMind_Geolocation; |
| 14 |
use WP_Defender\Model\Setting\Session_Protection as Model_Session_Protection; |
| 15 |
|
| 16 |
/** |
| 17 |
* Since advanced tools will have many submodules, this just using for render. |
| 18 |
* |
| 19 |
* Class Advanced_Tools |
| 20 |
*/ |
| 21 |
class Advanced_Tools extends Event { |
| 22 |
/** |
| 23 |
* Menu slug name. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
public $slug = 'wdf-advanced-tools'; |
| 28 |
/** |
| 29 |
* The WPMUDEV instance used for interacting with WPMUDEV services. |
| 30 |
* |
| 31 |
* @var WPMUDEV |
| 32 |
*/ |
| 33 |
private $wpmudev; |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor method |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
$this->wpmudev = wd_di()->get( WPMUDEV::class ); |
| 40 |
|
| 41 |
$this->register_page( |
| 42 |
$this->get_title(), |
| 43 |
$this->slug, |
| 44 |
array( $this, 'main_view' ), |
| 45 |
$this->parent_slug |
| 46 |
); |
| 47 |
$this->register_routes(); |
| 48 |
add_action( 'defender_enqueue_assets', array( $this, 'enqueue_assets' ) ); |
| 49 |
add_action( 'admin_init', array( $this, 'mark_page_visited' ) ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Enqueue assets. |
| 54 |
*/ |
| 55 |
public function enqueue_assets() { |
| 56 |
if ( ! $this->is_page_active() ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
$data = $this->dump_routes_and_nonces(); |
| 61 |
wp_enqueue_script( 'clipboard' ); |
| 62 |
$data = (array) apply_filters( 'wp_defender_advanced_tools_data', $data ); |
| 63 |
wp_localize_script( 'def-advancedtools', 'advanced_tools', $data ); |
| 64 |
wp_enqueue_script( 'def-advancedtools' ); |
| 65 |
$this->enqueue_main_assets(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Render the main view for this page. |
| 70 |
*/ |
| 71 |
public function main_view() { |
| 72 |
$this->render( 'main' ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Remove settings for all submodules. |
| 77 |
*/ |
| 78 |
public function remove_settings() { |
| 79 |
( new \WP_Defender\Model\Setting\Mask_Login() )->delete(); |
| 80 |
( new \WP_Defender\Model\Setting\Security_Headers() )->delete(); |
| 81 |
( new \WP_Defender\Model\Setting\Password_Protection() )->delete(); |
| 82 |
( new \WP_Defender\Model\Setting\Password_Reset() )->delete(); |
| 83 |
( new \WP_Defender\Model\Setting\Recaptcha() )->delete(); |
| 84 |
( new \WP_Defender\Model\Setting\Strong_Password() )->delete(); |
| 85 |
( new Model_Session_Protection() )->delete(); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Delete all the data & the cache. |
| 90 |
* |
| 91 |
* @since 2.4.6 |
| 92 |
*/ |
| 93 |
public function remove_data() { |
| 94 |
wd_di()->get( \WP_Defender\Controller\Mask_Login::class )->remove_data(); |
| 95 |
// Remove data of all Password features. |
| 96 |
wd_di()->get( \WP_Defender\Controller\Password_Protection::class )->remove_data(); |
| 97 |
wd_di()->get( \WP_Defender\Controller\Password_Reset::class )->remove_data(); |
| 98 |
wd_di()->get( \WP_Defender\Controller\Strong_Password::class )->remove_data(); |
| 99 |
wd_di()->get( \WP_Defender\Controller\Session_Protection::class )->remove_data(); |
| 100 |
// End. |
| 101 |
wd_di()->get( \WP_Defender\Controller\Recaptcha::class )->remove_data(); |
| 102 |
|
| 103 |
global $wp_filesystem; |
| 104 |
// Initialize the WP filesystem, no more using 'file-put-contents' function. |
| 105 |
if ( empty( $wp_filesystem ) ) { |
| 106 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 107 |
WP_Filesystem(); |
| 108 |
} |
| 109 |
|
| 110 |
$service_geo = wd_di()->get( MaxMind_Geolocation::class ); |
| 111 |
$maxmind_dir = $service_geo->get_db_base_path(); |
| 112 |
$wp_filesystem->delete( $maxmind_dir, true ); |
| 113 |
$arr_deleted_files = array( |
| 114 |
\WP_Defender\Component\Audit::AUDIT_LOG, |
| 115 |
\WP_Defender\Controller\Firewall::FIREWALL_LOG, |
| 116 |
wd_internal_log(), |
| 117 |
\WP_Defender\Behavior\Scan\Malware_Scan::MALWARE_LOG, |
| 118 |
\WP_Defender\Controller\Scan::SCAN_LOG, |
| 119 |
\WP_Defender\Component\Password_Protection::PASSWORD_LOG, |
| 120 |
\WP_Defender\Component\IP\Antibot_Global_Firewall::LOG_FILE_NAME, |
| 121 |
\WP_Defender\Component\Security_Tweak::LOG_FILE_NAME, |
| 122 |
// Outdated logs. |
| 123 |
'defender.log', |
| 124 |
); |
| 125 |
|
| 126 |
foreach ( $arr_deleted_files as $deleted_file ) { |
| 127 |
$wp_filesystem->delete( $deleted_file ); |
| 128 |
} |
| 129 |
|
| 130 |
$this->handle_log_file_deletion(); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Handle log file deletion. |
| 135 |
* |
| 136 |
* @since 4.7.2 |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
public function handle_log_file_deletion(): void { |
| 140 |
if ( is_multisite() ) { |
| 141 |
global $wpdb; |
| 142 |
|
| 143 |
$offset = 0; |
| 144 |
$limit = 100; |
| 145 |
$blogs = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 146 |
$wpdb->prepare( |
| 147 |
"SELECT blog_id FROM {$wpdb->blogs} LIMIT %d, %d", |
| 148 |
$offset, |
| 149 |
$limit |
| 150 |
), |
| 151 |
ARRAY_A |
| 152 |
); |
| 153 |
while ( ! empty( $blogs ) && is_array( $blogs ) ) { |
| 154 |
foreach ( $blogs as $blog ) { |
| 155 |
switch_to_blog( $blog['blog_id'] ); |
| 156 |
|
| 157 |
$this->delete_log_files(); |
| 158 |
|
| 159 |
restore_current_blog(); |
| 160 |
} |
| 161 |
$offset += $limit; |
| 162 |
$blogs = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 163 |
$wpdb->prepare( |
| 164 |
"SELECT blog_id FROM {$wpdb->blogs} LIMIT %d, %d", |
| 165 |
$offset, |
| 166 |
$limit |
| 167 |
), |
| 168 |
ARRAY_A |
| 169 |
); |
| 170 |
} |
| 171 |
} else { |
| 172 |
$this->delete_log_files(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Delete log files. |
| 178 |
* |
| 179 |
* @since 4.7.2 |
| 180 |
* @return void |
| 181 |
*/ |
| 182 |
private function delete_log_files(): void { |
| 183 |
global $wp_filesystem; |
| 184 |
|
| 185 |
// Initialize the WP filesystem, no more using 'file-put-contents' function. |
| 186 |
if ( empty( $wp_filesystem ) ) { |
| 187 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 188 |
WP_Filesystem(); |
| 189 |
} |
| 190 |
|
| 191 |
$upload_dir = wp_upload_dir(); |
| 192 |
$upload_path = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'wp-defender'; |
| 193 |
|
| 194 |
if ( is_dir( $upload_path ) ) { |
| 195 |
$files = glob( $upload_path . '/*.log' ); |
| 196 |
|
| 197 |
foreach ( $files as $file ) { |
| 198 |
if ( $wp_filesystem->is_file( $file ) ) { |
| 199 |
$wp_filesystem->delete( $file ); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Get data for frontend |
| 207 |
* |
| 208 |
* @return array |
| 209 |
*/ |
| 210 |
public function data_frontend(): array { |
| 211 |
return array( |
| 212 |
'mask_login' => wd_di()->get( Mask_Login::class )->data_frontend(), |
| 213 |
'security_headers' => wd_di()->get( Security_Headers::class )->data_frontend(), |
| 214 |
'pwned_passwords' => wd_di()->get( Password_Protection::class )->data_frontend(), |
| 215 |
'recaptcha' => wd_di()->get( Recaptcha::class )->data_frontend(), |
| 216 |
'strong_password' => wd_di()->get( Strong_Password::class )->data_frontend(), |
| 217 |
'session_protection' => wd_di()->get( Session_Protection::class )->data_frontend(), |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Export to array |
| 223 |
*/ |
| 224 |
public function to_array() {} |
| 225 |
|
| 226 |
/** |
| 227 |
* Import data |
| 228 |
* |
| 229 |
* @param array $data The data to import. |
| 230 |
*/ |
| 231 |
public function import_data( $data ) {} |
| 232 |
|
| 233 |
/** |
| 234 |
* Export strings |
| 235 |
* |
| 236 |
* @return array |
| 237 |
*/ |
| 238 |
public function export_strings() { |
| 239 |
return array(); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Return the title of the page. |
| 244 |
* |
| 245 |
* @return string The title of the page. |
| 246 |
*/ |
| 247 |
public function get_title(): string { |
| 248 |
$default = esc_html__( 'Tools', 'defender-security' ); |
| 249 |
// Breadcrumbs are only for Pro features. |
| 250 |
if ( ! $this->wpmudev->is_pro() ) { |
| 251 |
return $default; |
| 252 |
} |
| 253 |
// Check if the user has already visited the feature page. |
| 254 |
if ( wd_di()->get( Breadcrumbs::class )->get_meta_key() ) { |
| 255 |
return $default; |
| 256 |
} |
| 257 |
|
| 258 |
return $default . '<span class=wd-new-feature-dot></span>'; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Marks the feature page as visited. |
| 263 |
* |
| 264 |
* @return void |
| 265 |
*/ |
| 266 |
public function mark_page_visited(): void { |
| 267 |
// Breadcrumbs are only for Pro features. |
| 268 |
if ( ! $this->wpmudev->is_pro() ) { |
| 269 |
return; |
| 270 |
} |
| 271 |
if ( 'wdf-advanced-tools' !== defender_get_current_page() || |
| 272 |
Model_Session_Protection::get_module_slug() !== defender_get_data_from_request( 'view', 'g' ) |
| 273 |
) { |
| 274 |
return; |
| 275 |
} |
| 276 |
wd_di()->get( Breadcrumbs::class )->update_meta_key(); |
| 277 |
} |
| 278 |
} |
| 279 |
|