Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upd. Scan. Improved delete action. #445

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions inc/spbc-scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ function spbc_scanner_file_delete_handler($file_id)
}

$root_path = spbc_get_root_path();
$response_content_ok = false;
$response_content_admin_ok = false;

// Getting file info.
$sql = 'SELECT * FROM ' . SPBC_TBL_SCAN_FILES . ' WHERE fast_hash = %s LIMIT 1';
Expand Down Expand Up @@ -374,38 +376,36 @@ function spbc_scanner_file_delete_handler($file_id)
: array('error' => 'FILE_IS_REQUIRED_IN_PHP_INI');
}

// Getting file && API call
// Getting file and prechecking website
$remembered_file_content = file_get_contents($file_path);
$response_content_before_actions = HTTP::getContentFromURL(get_option('home'));
$response_content_admin_before_actions = HTTP::getContentFromURL(get_option('home') . '/wp-admin');
$precheck_public = wp_remote_get(get_option('home'));
$precheck_admin = wp_remote_get(get_option('home') . '/wp-admin');

$result = unlink($file_path);

if (!$result) {
return array('error' => 'FILE_COULDNT_DELETE');
}

$response_content = HTTP::getContentFromURL(get_option('home'));
if ($response_content === $response_content_before_actions) {
// if not work before and after unlink - it's ok, if both work - it's ok too and much better
$postcheck_public = wp_remote_get(get_option('home'));
if ((is_wp_error($postcheck_public) && is_wp_error($precheck_public)) ||
(!is_wp_error($postcheck_public) && !is_wp_error($precheck_public) &&
wp_remote_retrieve_response_code($postcheck_public) === wp_remote_retrieve_response_code($precheck_public))
) {
$response_content_ok = true;
} else {
if (is_string($response_content) && !spbc_search_page_errors($response_content)) {
$response_content_ok = true;
} else {
$response_content_ok = false;
}
}

$response_content_admin = HTTP::getContentFromURL(get_option('home') . '/wp-admin');
if ($response_content_admin === $response_content_admin_before_actions) {
// if not work before and after unlink - it's ok, if both work - it's ok too and much better
$postcheck_admin = wp_remote_get(get_option('home') . '/wp-admin');
if ((is_wp_error($postcheck_admin) && is_wp_error($precheck_admin)) ||
(!is_wp_error($postcheck_admin) && !is_wp_error($precheck_admin) &&
wp_remote_retrieve_response_code($postcheck_admin) === wp_remote_retrieve_response_code($precheck_admin))
) {
$response_content_admin_ok = true;
} else {
if (is_string($response_content_admin) && !spbc_search_page_errors($response_content_admin)) {
$response_content_admin_ok = true;
} else {
$response_content_admin_ok = false;
}
}

// general check to revert if something went wrong
if (!$response_content_admin_ok || !$response_content_ok) {
$result = file_put_contents($file_path, $remembered_file_content);
$output = array('error' => 'WEBSITE_RESPONSE_BAD');
Expand Down
Loading