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

New. Scan. Update front estimates time. #380

Merged
merged 10 commits into from
Jul 1, 2024
53 changes: 34 additions & 19 deletions inc/spbc-scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,12 @@ function spbc_scanner_file_send($direct_call = false, $file_id = null, $do_resca
if ($sql_result !== false) {
$output = array('success' => true, 'result' => $api_response);
//set new cron to resend unqueued files
\CleantalkSP\SpbctWP\Cron::updateTask('scanner_resend_pscan_files', 'spbc_scanner_resend_pscan_files', SPBC_PSCAN_UPDATE_FILES_STATUS_PERIOD, time() + SPBC_PSCAN_UPDATE_FILES_STATUS_PERIOD);
\CleantalkSP\SpbctWP\Cron::updateTask(
'scanner_resend_pscan_files',
'spbc_scanner_resend_pscan_files',
SPBC_PSCAN_RESEND_FILES_STATUS_PERIOD,
time() + SPBC_PSCAN_RESEND_FILES_STATUS_PERIOD
);
//error on fail
} else {
$output = array('error' => 'DB_COULD_NOT_UPDATE pscan_pending_queue');
Expand Down Expand Up @@ -772,6 +777,7 @@ function spbc_scanner_pscan_check_analysis_status($direct_call = false, $file_id
$file_info['pscan_file_id']
);


// Validate API response
try {
$api_response = spbc_scanner_validate_pscan_status_response($api_response);
Expand All @@ -789,22 +795,14 @@ function spbc_scanner_pscan_check_analysis_status($direct_call = false, $file_id
/*
* If file process is not finished, update data
*/
// Set old processing status to compare with next
$old_processing_status = !empty($file_info['pscan_processing_status']) ? $file_info['pscan_processing_status'] : null;
// Update processing status
if ( $api_response['processing_status'] !== $old_processing_status ) {
// Keep update result
$update_result = $wpdb->query(
'UPDATE ' . SPBC_TBL_SCAN_FILES
. ' SET '
. ' pscan_pending_queue = 0, '
. ' pscan_processing_status = "' . $api_response['processing_status'] . '"'
. ' WHERE pscan_file_id = "' . $file_info['pscan_file_id'] . '"'
);
} else {
// Status have not been changed, however status process is succesfull
$update_result = true;
}
$update_result = $wpdb->query(
'UPDATE ' . SPBC_TBL_SCAN_FILES
. ' SET '
. ' pscan_pending_queue = 0, '
. ' pscan_processing_status = "' . $api_response['processing_status'] . '",'
. ' pscan_estimated_execution_time = "' . $api_response['estimated_execution_time'] . '"'
. ' WHERE pscan_file_id = "' . $file_info['pscan_file_id'] . '"'
);
} else {
if ( $api_response['file_status'] === 'SAFE' ) {
/*
Expand All @@ -818,7 +816,8 @@ function spbc_scanner_pscan_check_analysis_status($direct_call = false, $file_id
. ' pscan_pending_queue = 0, '
. ' pscan_status = "SAFE",'
. ' pscan_balls = %s,'
. ' status = "APPROVED_BY_CLOUD" '
. ' status = "APPROVED_BY_CLOUD",'
. ' pscan_estimated_execution_time = NULL'
. ' WHERE pscan_file_id = %s',
isset($api_response['file_balls']) ? $api_response['file_balls'] : '{SAFE:0}',
$file_info['pscan_file_id']
Expand All @@ -836,7 +835,8 @@ function spbc_scanner_pscan_check_analysis_status($direct_call = false, $file_id
. ' pscan_status = %s ,'
. ' severity = "CRITICAL",'
. ' pscan_balls = %s,'
. ' status = "DENIED_BY_CLOUD"'
. ' status = "DENIED_BY_CLOUD",'
. ' pscan_estimated_execution_time = NULL'
. ' WHERE pscan_file_id = %s',
$api_response['file_status'],
isset($api_response['file_balls']) ? $api_response['file_balls'] : '{DANGEROUS:0}',
Expand Down Expand Up @@ -954,6 +954,7 @@ function spbc_scanner_pscan_update_check_exclusions(array $file_info)

/**
* @param array $response API Response
* @param bool $await_estimated_data Do await estimated data set on undone files check
* @return mixed API Response
* @throws Exception if validation failed
*/
Expand Down Expand Up @@ -999,6 +1000,20 @@ function spbc_scanner_validate_pscan_status_response($response)
}
}

//estimated time validation
if ( $response['processing_status'] !== 'DONE' ) {
if ( ! isset($response['estimated_execution_time'])) {
throw new Exception('response provided no estimated scan time');
}
//todo remove on business decision
//if ( ! isset($response['number_of_files'])) {
// throw new Exception('response provided no number of estimated files');
//}
//if ( ! isset($response['number_of_files_scanned'])) {
// throw new Exception('response provided no number of already scanned files');
//}
}

return $response;
}

Expand Down
98 changes: 92 additions & 6 deletions inc/spbc-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2637,6 +2637,12 @@ function spbc_field_scanner__prepare_data__analysis_log(&$table)
$analysis_comment = __('Processing: queue is full. File will be resent in 5 minutes.', 'security-malware-firewall');
}

if ( !is_null($row->pscan_estimated_execution_time) ) {
$estimated_execution_time = $row->pscan_estimated_execution_time . ' ' . __('second(s)', 'security-malware-firewall');
} else {
$estimated_execution_time = $row->pscan_processing_status === 'DONE' ? 'Done' : 'Wait for assessing';
}

// Filter actions for approved files
if ( in_array($row->pscan_status, array('SAFE','DANGEROUS')) || $curr_time - $row->last_sent < 500 ) {
unset($row->actions['check_analysis_status']);
Expand All @@ -2657,6 +2663,7 @@ function spbc_field_scanner__prepare_data__analysis_log(&$table)
'last_sent' => is_numeric($row->last_sent) ? date('M j, Y, H:i:s', $row->last_sent) : null,
'pscan_status' => $pscan_status,
'analysis_comment' => $analysis_comment,
'pscan_estimated_execution_time' => $estimated_execution_time,
'actions' => $row->actions,
);
}
Expand Down Expand Up @@ -3065,6 +3072,85 @@ function spbc_field_scanner()
echo '</div>';
}

add_action('wp_ajax_spbc_analysyis_files_stats__get_html', 'spbc__analysyis_files_stats__get_html');
/**
* Retrieves HTML code block to layout files counters stats in the analysis accordion.
* @return string
*/
function spbc__analysyis_files_stats__get_html()
svfcode marked this conversation as resolved.
Show resolved Hide resolved
{
spbc_check_ajax_referer('spbc_secret_nonce', 'security');

$out = '
<div id="spbc_analysis_files_stats" style="display: block; padding-bottom: 5px">
<p>%s</p>
<p>%s: %d / %d / %d</p>
%s
</div>
';
$caption = __('List of files sent for the Cloud analysis, it takes up to 10 minutes to process a file. Refresh the page to have the results.', 'security-malware-firewall');
$files_stats_string = __('Files sent/checked/unchecked', 'security-malware-firewall');
$data = spbc__analysyis_files_stats__get_data();
$last_updated_chunk = __('Files statuses updates every', 'security-malware-firewall') . ' ' . SPBC_PSCAN_UPDATE_FILES_STATUS_PERIOD . ' seconds';
$last_updated_chunk .= '<span id="spbc_last_update_time">';
$last_updated_chunk .= $data['last_updated'] && is_int($data['last_updated'])
? ', ' . __('last update time', 'security-malware-firewall') . ': ' . date("M d Y H:i:s", $data['last_updated'])
: '.';
$last_updated_chunk .= '</span>';
$out = sprintf(
$out,
$caption,
$files_stats_string,
$data['files_sent_count'],
$data['files_checked_count'],
$data['files_unchecked_count'],
$last_updated_chunk
);

if (Post::get('sub_action') === 'give_me_html') {
echo $out;
exit;
}

return $out;
}

/**
* Retrieves the data for analysis stats block.
* @return array
*/
function spbc__analysyis_files_stats__get_data()
{
global $wpdb, $spbc;
$out = array(
'files_sent_count' => 'N/D',
'files_checked_count' => 'N/D',
'files_unchecked_count' => 'N/D',
'last_updated' => false,
);
$files_sent_count = $wpdb->get_var('
SELECT COUNT(*) from ' . SPBC_TBL_SCAN_FILES . '
WHERE last_sent IS NOT NULL;
');
$files_checked_count = $wpdb->get_var('
SELECT COUNT(*) from ' . SPBC_TBL_SCAN_FILES . '
WHERE last_sent IS NOT NULL AND pscan_processing_status = \'DONE\';
');
$files_unchecked_count = !is_null($files_sent_count) && !is_null($files_checked_count)
? (int)$files_sent_count - (int)$files_checked_count
: false;
$last_updated = \CleantalkSP\SpbctWP\Cron::getTask('scanner_update_pscan_files_status');
//next call checking is a trick - the last_call key does not work properly
$last_updated = $last_updated && !empty($last_updated['next_call'])
? $last_updated['next_call'] - $last_updated['period'] + $spbc->data['site_utc_offset_in_seconds']
: false;
$out['files_sent_count'] = !is_null($files_sent_count) ? (int)$files_sent_count : $out['files_sent_count'];
$out['files_checked_count'] = !is_null($files_checked_count) ? (int)$files_checked_count : $out['files_checked_count'];
$out['files_unchecked_count'] = $files_unchecked_count ? : $out['files_unchecked_count'];
$out['last_updated'] = $last_updated ? : $out['last_updated'];
return $out;
}

function spbc_field_scanner__show_accordion($direct_call = false)
{
if ( ! $direct_call) {
Expand All @@ -3079,16 +3165,14 @@ function spbc_field_scanner__show_accordion($direct_call = false)
'<a href="https://cleantalk.org/my/support/open?subject=Cloud%20Malware%20scanner,%20results%20question" target="_blank">',
'</a>'
) : '';
$analysis_log_description = '<div>' .
__('List of files sent for the Cloud analysis, it takes up to 10 minutes to process a file. Refresh the page to have the results.', 'security-malware-firewall') .
$analysis_log_description = spbc__analysyis_files_stats__get_html() .
'<div id="spbc_notice_cloud_analysis_feedback" class="notice is-dismissible">' .
'<p>' .
'<img src="' . SPBC_PATH . '/images/att_triangle.png" alt="attention" style="margin-bottom:-1px">' .
' ' .
__('If you feel that the Cloud verdict is incorrect, please click the link "Copy file info" near the file name and contact us', 'security-malware-firewall') . ' ' .
$dashboard_link .
'</p>' .
'</div>' .
'</div>';
if ($spbc->data['display_scanner_warnings']['analysis'] && !$spbc->data['wl_mode_enabled']) {
$analysis_log_description .= spbc__get_accordion_tab_info_block_html('analysis');
Expand Down Expand Up @@ -3600,21 +3684,23 @@ function spbc_list_table__get_args_by_type($table_type)
'pscan_processing_status',
'fast_hash',
'pscan_status',
'pscan_pending_queue'
'pscan_pending_queue',
'pscan_estimated_execution_time'
),
'where' => spbc_get_sql_where_addiction_for_table_of_category('analysis_log'),
),
'order_by' => array('pscan_status' => 'desc'),
'sortable' => array('path', 'last_sent', 'pscan_status'),
'sortable' => array('path', 'last_sent', 'pscan_status', 'pscan_estimated_execution_time'),
)
);

$args['columns'] = array(
'cb' => array('heading' => '<input type=checkbox>', 'class' => 'check-column', 'width_percent' => 2),
'path' => array('heading' => 'Path', 'primary' => true, 'width_percent' => 38),
'path' => array('heading' => 'Path', 'primary' => true, 'width_percent' => 28),
'detected_at' => array('heading' => 'Detected at', 'width_percent' => 15),
'last_sent' => array('heading' => 'Sent for analysis at', 'width_percent' => 15),
'pscan_status' => array('heading' => 'Cloud verdict', 'width_percent' => 10),
'pscan_estimated_execution_time' => array('heading' => 'Estimated time', 'width_percent' => 10),
//'analysis_comment' => array('heading' => 'Comment', 'width_percent' => 20),
);

Expand Down
Loading
Loading