Skip to content

Commit

Permalink
Merge pull request #23 from CleanTalk/backgorund_scan.ag
Browse files Browse the repository at this point in the history
Background scan implemented
  • Loading branch information
alexandergull authored Apr 8, 2024
2 parents f457034 + a25eb4b commit 1113569
Show file tree
Hide file tree
Showing 11 changed files with 413 additions and 161 deletions.
2 changes: 1 addition & 1 deletion uniforce/data/cron.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?php
$uniforce_tasks = array ();
$uniforce_tasks = array ();
17 changes: 17 additions & 0 deletions uniforce/inc/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,14 @@ function usp_install_cron(){
'executed' => 0,
'last_executed' => 0,
'params' => [],
],
'scanner_launch' => [
'handler' => 'usp_scanner__launch',
'period' => 86400,
'next_call' => time() + 86400,
'executed' => 0,
'last_executed' => 0,
'params' => [],
]
];

Expand Down Expand Up @@ -543,6 +551,15 @@ function usp_do_save_settings() {
// Recognizing new key
$new_key_is_set = $usp->settings->key !== $settings['key'];


if( $settings['scanner_auto_start'] != $usp->settings->scanner_auto_start ) {
if ($settings['scanner_auto_start'] == 1) {
Cron::updateTask( 'scanner_launch', 'usp_scanner__launch', 86400, time() + 86400 );
} else {
Cron::removeTask( 'scanner_launch');
}
}

// Set values
foreach ( $settings as $setting => $value) {
$usp->settings->$setting = $value;
Expand Down
34 changes: 20 additions & 14 deletions uniforce/inc/cron_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Cleantalk\USP\Variables\Server;

function uniforce_fw_update( $immediate = false ){

$usp = State::getInstance();

// SFW actions
if( $usp->key && $usp->settings->fw ){

Expand All @@ -35,24 +35,24 @@ function uniforce_fw_update( $immediate = false ){
function uniforce_fw_send_logs(){

$usp = State::getInstance();

// SFW actions
if( $usp->key && $usp->settings->fw ){

// Send SFW logs
$result = \Cleantalk\USP\Uniforce\Firewall\FW::send_log( $usp->key );

if( ! empty( $result['error'] ) )
Err::add( $result['error'] );

if( ! Err::check() ) {
$usp->fw_stats->logs_sent_time = time();
$usp->fw_stats->count = 0;
$usp->fw_stats->save();
}

}

return ! Err::check() ? true : false;
}

Expand Down Expand Up @@ -135,25 +135,31 @@ function usp_scanner__launch(){

$usp = State::getInstance();

if ( $usp->scanner_status === false || ! $usp->settings->scanner_auto_start )
return true;
if ( $usp->scanner_status === false || ! $usp->settings->scanner_auto_start ){
return true;
}

\Cleantalk\USP\ScannerController::clearBackgroundScanLog($usp);
$usp->data->scanner->background_scan_stop = false;
$usp->data->save();


return Helper::http__request(
CT_USP_AJAX_URI,
return Helper::http__request(
CT_USP_URI,
array(
'plugin_name' => 'security',
'spbc_remote_call_token' => md5($usp->settings->key),
'spbc_remote_call_action' => 'scanner__controller',
'state' => 'get_hashes'
'state' => 'create_db'
),
'get async'
);
}

function usp_scanner__get_signatures() {

$usp = State::getInstance();

$scanner_controller = new \Cleantalk\USP\ScannerController( CT_USP_SITE_ROOT );
$out = $scanner_controller->action__scanner__get_signatures();

Expand All @@ -164,4 +170,4 @@ function usp_get_latest_version(){
$updater = new \Cleantalk\USP\Updater\Updater( CT_USP_ROOT );
State::getInstance()->plugin_meta->latest_version = $updater->getLatestVersion();
State::getInstance()->plugin_meta->save();
}
}
90 changes: 90 additions & 0 deletions uniforce/inc/scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cleantalk\USP\Layout\ListTable;
use Cleantalk\USP\Scanner\Scanner;
use Cleantalk\USP\Uniforce\API;
use Cleantalk\USP\Uniforce\Cron;
use Cleantalk\USP\Uniforce\Helper;
use Cleantalk\USP\Variables\Post;

Expand Down Expand Up @@ -413,6 +414,17 @@ function usp_scanner__display(){
return;
}

//get next scan launch block
$scanner_next_launch = Cron::getTaskNextCall('scanner_launch', 'Y-m-d H:i:s');
if ($scanner_next_launch) {
echo '<p class="spbc_hint text-center">';
printf(
uniforce_translate('Next automatic scan is scheduled on: %s', 'security-malware-firewall'),
$scanner_next_launch
);
echo '</p>';
}

// Info about last scanning
echo '<p class="spbc_hint text-center">';
if( !$usp->data->stat->scanner->last_scan )
Expand All @@ -429,6 +441,84 @@ function usp_scanner__display(){
}
echo '</p>';

//background log layout
$background_log = is_object($usp->data->stat->scanner_background_log) && !empty($usp->data->stat->scanner_background_log->convertToArray())
? $usp->data->stat->scanner_background_log->convertToArray()
: array();
$has_ran = !empty($background_log) && !empty($background_log['last_executed']);
if ($has_ran) {
$background_log_formatted = array();
foreach ($background_log as $stage => $values) {
$additional_info = '';
if (!empty($values['processed'])) {
$additional_info = ', files processed on last iteration ' . $values['processed'];
}
if (!empty($values['updated'])) {
$additional_info = ', signatures updated ' . $values['updated'];
}
if (!empty($values['time'])) {
$additional_info = ', on ' . date('Y-m-d H:i:s', (int)$values['time']);
}
switch ($stage) {
case 'create_db':
$stage_name = 'Database creating';
break;
case 'clear_table':
$stage_name = 'Database clearing';
break;
case 'get_signatures':
$stage_name = 'Signatures getting';
break;
case 'surface_analysis':
$stage_name = 'Surface analysis';
break;
case 'get_approved':
$stage_name = 'Approved hashes get';
break;
case 'signature_analysis':
$stage_name = 'Signature analysis';
break;
case 'heuristic_analysis':
$stage_name = 'Heuristic analysis';
break;
case 'auto_cure':
$stage_name = 'Automatic cure';
break;
//'frontend_analysis',
//'outbound_links',
case 'send_results':
$stage_name = 'Sending results';
break;
case 'last_executed':
$stage_name = 'Last run';
break;
default:
$stage_name = $stage;
}
$background_log_formatted[$stage_name] = array(
'success' => isset($values['end']) && $values['end'] ? '<span style="color: green">OK</span>' : '<span style="color: red">FAIL</span>',
'additional_info' => $additional_info,
);
}
$background_templated = '';
foreach ($background_log_formatted as $stage => $states)
{
$background_templated .= '<span style="font-size: small; display: block">';
$background_templated .= $stage . ': ' . $states['success'] . $states['additional_info'];
$background_templated .= '</span>';
}
if (!empty($background_templated)) {
echo '<p id="background_scan_log_toggler" class="spbc_hint text-center"><a>Click to show/hide last background scheduled scan log</a></p>';
$template = '
<div id="background_scan_log" style="border: 1px solid lightgrey; padding: 5px; display: none">
%s
</div>
';
printf($template, $background_templated);
}
}


// Statistics link
echo '<p class="spbc_hint text-center">';
echo sprintf(
Expand Down
3 changes: 2 additions & 1 deletion uniforce/js/scanner-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ class spbc_Scanner{
plugin_name: 'spbc', // Adding security code
offset: this.offset,
no_sql: this.settings['no_sql'],
background_scan_stop: true,
};

var params = {
Expand All @@ -289,7 +290,7 @@ class spbc_Scanner{
complete: null,
errorOutput: this.errorOutput,
context: this,
timeout: 40000
timeout: 40000,
};

switch (this.state) {
Expand Down
7 changes: 6 additions & 1 deletion uniforce/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ jQuery(document).ready(function() {
update();
});

//show background scan log
$("#background_scan_log_toggler").on('click', function(event){
$("#background_scan_log").toggle('slow');
});

jQuery('.ctusp_tab_navigation').on('click', '.ctusp_tab_navigation-title', function (event) {
usp_switchTab(event.currentTarget);
});
Expand Down Expand Up @@ -293,4 +298,4 @@ function uspSettingsDependencies(settingsIDs, enable){
elem.getAttribute('disabled') === null ? do_disable() : do_enable();

});
}
}
Loading

0 comments on commit 1113569

Please sign in to comment.