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. Settings. Improved scan tab render. #444

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions inc/spbc-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function spbc_admin_init()

// Scanner
add_action('wp_ajax_spbc_scanner_controller_front', array(ScannerQueue::class, 'controllerFront'));
add_action('wp_ajax_spbc_scanner_load_more_scan_logs', 'spbc_scanner_load_more_scan_logs');
add_action('wp_ajax_spbc_scanner_save_to_pdf', 'spbc_scanner_save_to_pdf');
add_action('wp_ajax_spbc_scanner_get_pdf_file_name', 'spbc_scanner_get_pdf_file_name');
add_action('wp_ajax_spbc_scanner_clear', 'spbc_scanner_clear'); // Debug. Clear the table
Expand Down
22 changes: 19 additions & 3 deletions inc/spbc-scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CleantalkSP\Common\Helpers\Arr;
use CleantalkSP\SpbctWP\DTO;
use CleantalkSP\Fpdf\Pdf;
use CleantalkSP\SpbctWP\Scanner\ScanningLog\ScanningLogFacade;
use CleantalkSP\SpbctWP\Scanner\Services\SendFileToCloudService;

/**
Expand Down Expand Up @@ -1106,21 +1107,24 @@ function spbc_get_sql_where_addiction_for_table_of_category($category)
/**
* Get all files IDs of the category.
* @param string $category Category of files category which needs to be searched for
* @param bool $count Whether to return the count of files or the files themselves
* @return array Array of IDs
*/
function spbc_scanner_get_files_by_category($category)
function spbc_scanner_get_files_by_category($category, $count = false)
svfcode marked this conversation as resolved.
Show resolved Hide resolved
{
global $wpdb;

$ids = array();

$query = 'SELECT fast_hash from ' . SPBC_TBL_SCAN_FILES . spbc_get_sql_where_addiction_for_table_of_category($category);
$query = $count ? 'SELECT COUNT(fast_hash) from ' : 'SELECT fast_hash from ';
$query .= SPBC_TBL_SCAN_FILES . spbc_get_sql_where_addiction_for_table_of_category($category);

// suppress because data is static
// @psalm-suppress WpdbUnsafeMethodsIssue
$res = $wpdb->get_results($query);

foreach ($res as $tmp) {
$ids[] = $tmp->fast_hash;
$ids[] = isset($tmp->fast_hash) ? $tmp->fast_hash : null;
svfcode marked this conversation as resolved.
Show resolved Hide resolved
}

return $ids;
Expand Down Expand Up @@ -1953,6 +1957,18 @@ function spbc_scanner_get_file_by_id($file_id)
return $file_info ?: false;
}

/**
* Load more scan logs on the scan results page
*/
function spbc_scanner_load_more_scan_logs()
svfcode marked this conversation as resolved.
Show resolved Hide resolved
{
$offset = Post::get('offset');

$scan_logs = ScanningLogFacade::render(100, (int)$offset, true);

wp_send_json_success($scan_logs);
}

/**
* Save scanner logs to pdf
*
Expand Down
13 changes: 2 additions & 11 deletions inc/spbc-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3409,7 +3409,7 @@ function spbc_field_scanner()

// Scan results log
if ( ! empty($spbc->data['scanner']['last_scan'])) {
spbc_scan_results_log_module();
echo ScanningLogFacade::render();
}

// Clear hashes
Expand Down Expand Up @@ -3666,7 +3666,7 @@ function spbc_field_scanner__show_accordion($direct_call = false)
}
//todo spbc_scanner_get_files_by_category() has internal SQL error on 'approved' column
if ( $type_name !== 'frontend_malware' && $type_name !== 'frontend_scan_results_approved' ) {
if (empty(spbc_scanner_get_files_by_category($type_name))) {
if (empty(spbc_scanner_get_files_by_category($type_name, true))) {
continue;
}
}
Expand Down Expand Up @@ -5616,15 +5616,6 @@ function spbc_bulk_actions_description()
return $description;
}

/**
* We draw a module with the scan results for all files,
* with pagination and filtering
*/
function spbc_scan_results_log_module()
{
ScanningLogFacade::render();
}

/**
* Implementation of service_update_local_settings functionality
*/
Expand Down
4 changes: 2 additions & 2 deletions js/spbc-settings_tab--scanner.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion js/spbc-settings_tab--scanner.min.js.map

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions js/src/spbc-settings_tab--scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,29 @@ jQuery(document).ready(function() {
});
});

jQuery('#spbc_load_more_scan_logs').on('click', function(e) {
e.preventDefault();

jQuery.ajax({
url: spbcSettings.ajaxurl,
type: 'POST',
data: {
action: 'spbc_scanner_load_more_scan_logs',
security: spbcSettings.ajax_nonce,
no_cache: Math.random(),
offset: jQuery(this).data('offset') + 100,
},
xhrFields: {
responseType: 'text',
},
success: function(data) {
if (data.success) {
jQuery('#spbcscan-results-log-module .panel-body').append(data.data);
}
},
});
});

if (jQuery('#spbc_scanner_copy_log_to_clipboard').length) {
jQuery('#spbc_scanner_copy_log_to_clipboard').on('click', function(e) {
e.preventDefault();
Expand Down
10 changes: 6 additions & 4 deletions lib/CleantalkSP/SpbctWP/Scanner/ScanningLog/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ public static function write($content)
)->execute();
}

public static function getAll()
public static function getAll($limit = 0, $offset = 0)
{
global $wpdb;

$sql = 'SELECT timestamp, content FROM ' . SPBC_TBL_SCAN_RESULTS_LOG . ' ORDER BY timestamp DESC LIMIT %d OFFSET %d;';

return DB::getInstance()->fetchAll(
'SELECT timestamp, content FROM '
. SPBC_TBL_SCAN_RESULTS_LOG
. ' ORDER BY timestamp DESC'
$wpdb->prepare($sql, $limit, $offset)
);
}

Expand Down
16 changes: 13 additions & 3 deletions lib/CleantalkSP/SpbctWP/Scanner/ScanningLog/ScanningLogFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@ public static function clearLog()
return Repository::clear();
}

public static function render()
/**
* Render the scanning log
*
* @param int $limit
* @param int $offset
* @return string
*/
public static function render($limit = 100, $offset = 0, $only_rows = false)
{
$data = Repository::getAll();
$data = Repository::getAll($limit, $offset);

if (!empty($data)) {
Template::render($data);
return $only_rows ? Template::generateRows($data) : Template::render($data, $offset);
}

return '';
}

/**
Expand Down
38 changes: 31 additions & 7 deletions lib/CleantalkSP/SpbctWP/Scanner/ScanningLog/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,52 @@

class Template
{
public static function render($data)
/**
* Render the scanning log
*
* @param array $data
* @param int $offset
* @return string
*/
public static function render($data, $offset)
{
global $spbc;
$template = '<div id="spbcscan-results-log-module">';
$template .= '<div class="panel-body">';

$template .= self::generateRows($data);

$template .= '</div>';
$template .= '</div>';
$template .= '<div class="spbc_log-pagination">';
$template .= '<button id="spbc_load_more_scan_logs" class="spbc_manual_link" data-offset="' . $offset . '">'
. __('Load more logs', 'security-malware-firewall')
. '</button>';
$template .= '</div>';

return $template;
}

public static function generateRows($data)
{
global $spbc;

$template = '';
$prev_item_content = '';
foreach ( $data as $item ) {

foreach ($data as $item) {
if ($prev_item_content === $item['content']) {
continue;
}

$template .= '<p class="spbc_log-line">'
. date("M d Y H:i:s", $item['timestamp'] + $spbc->data['site_utc_offset_in_seconds'])
. ' '
. $item['content']
. '</p>';

$prev_item_content = $item['content'];
}

$template .= '</div>';
$template .= '</div>';

echo $template;
return $template;
}
}
Loading