Skip to content

Commit

Permalink
Upd. Scan. Improved delete action. (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
svfcode authored Dec 12, 2024
1 parent 95ba53c commit ed93adb
Showing 1 changed file with 19 additions and 19 deletions.
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

0 comments on commit ed93adb

Please sign in to comment.