Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Glomberg committed Jul 23, 2020
2 parents b96b65f + d41be83 commit 2f88f82
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 142 deletions.
5 changes: 5 additions & 0 deletions inc/spbc-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ function spbc_plugin_links_meta($meta, $plugin_file){
* Register stylesheet and scripts.
*/
function spbc_enqueue_scripts($hook) {

// If the user is not admin
if( ! current_user_can( 'manage_options' ) ){
return;
}

global $spbc;

Expand Down
56 changes: 43 additions & 13 deletions inc/spbc-scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,11 @@ function spbc_scanner_send_results($direct_call = false, $total_scanned = 0){

}else
$spbc->error_add('scanner_result_send', $result);


if($spbc->settings['scanner_auto_start'] && !$spbc->settings['scanner_auto_start_manual_time']) {
\CleantalkSP\SpbctWp\Cron::updateTask( 'scanner__launch', 'spbc_scanner__launch', 86400 );
}

$spbc->save('data');

if($direct_call) return $result; else die(json_encode($result));
Expand Down Expand Up @@ -1501,7 +1505,7 @@ function spbc_scanner_file_approve($direct_call = false, $file_id = null){
if($file_id){

// Getting file info.
$sql = 'SELECT path, full_hash
$sql = 'SELECT path, full_hash, status, severity
FROM '.SPBC_TBL_SCAN_FILES.'
WHERE fast_hash = "'.$file_id.'"
LIMIT 1';
Expand All @@ -1517,8 +1521,11 @@ function spbc_scanner_file_approve($direct_call = false, $file_id = null){

if($md5){

$previous = json_encode( array( 'status' => $file_info['status'], 'severity' => $file_info['severity'] ) );

// Using strait query because WPDB doesn't support NULL values
$sql = 'UPDATE '.SPBC_TBL_SCAN_FILES.'
SET status = "APROVED", severity = NULL, real_full_hash = "'.$md5.'"
SET status = "APROVED", severity = NULL, real_full_hash = "'.$md5.'", previous_state = \''. $previous .'\'
WHERE fast_hash = "'.$file_id.'"';
$sql_result = $wpdb->get_results($sql, ARRAY_A);

Expand Down Expand Up @@ -1563,7 +1570,7 @@ function spbc_scanner_file_disapprove($direct_call = false, $file_id = null){
if($file_id){

// Getting file info.
$sql = 'SELECT path, full_hash
$sql = 'SELECT path, full_hash, previous_state
FROM '.SPBC_TBL_SCAN_FILES.'
WHERE fast_hash = "'.$file_id.'"
LIMIT 1';
Expand All @@ -1579,10 +1586,19 @@ function spbc_scanner_file_disapprove($direct_call = false, $file_id = null){

if($md5){

$sql = 'UPDATE '.SPBC_TBL_SCAN_FILES.'
SET status = "UNKNOWN", severity = NULL, real_full_hash = "'.$md5.'"
WHERE fast_hash = "'.$file_id.'"';
$sql_result = $wpdb->get_results($sql, ARRAY_A);
$previous = json_decode( $file_info['previous_state'], true );

$wpdb->update(
SPBC_TBL_SCAN_FILES,
array(
'status' => $previous['status'],
'severity' => $previous['severity'],
'real_full_hash' => $md5,
),
array('fast_hash' => $file_id),
array('%s', '%s', '%s'),
array('%s')
);

if($sql_result !== false){
$output = array('success' => true);
Expand Down Expand Up @@ -1931,12 +1947,20 @@ function spbc_scanner_file_quarantine($direct_call = false, $file_id = null){
if(!is_dir(SPBC_PLUGIN_DIR.'quarantine/'))
mkdir(SPBC_PLUGIN_DIR.'quarantine/');
if(copy($root_path.$file_info['path'], $q_path)){

$result = $wpdb->update(
SPBC_TBL_SCAN_FILES,
array ('status' => 'QUARANTINED', 'q_status' => $file_info['status'], 'q_path' => $q_path, 'q_time' => time(),),
array ('path' => $file_info['path']),
array ( '%s', '%s', '%s', '%d', ),
array ( '%s' )
array(
'status' => 'QUARANTINED',
'q_path' => $q_path,
'q_time' => time(),
'previous_state' => json_encode( array(
'status' => $file_info['status'],
)),
),
array( 'path' => $file_info['path'] ),
array( '%s', '%s', '%d', '%s' ),
array( '%s' )
);
if($result !== false && $result > 0){
if(unlink($root_path.$file_info['path'])){
Expand Down Expand Up @@ -1992,9 +2016,15 @@ function spbc_scanner_file_quarantine__restore($direct_call = false, $file_id =
if(file_exists($file_info['q_path'])){
if(is_writable($file_info['q_path'])){
if(copy($file_info['q_path'], $root_path.$file_info['path'])){

$previous = json_decode( $file_info['previous_state'], true );

$result = $wpdb->update(
SPBC_TBL_SCAN_FILES,
array ('status' => $file_info['q_status'], 'q_status' => null, 'q_path' => null, 'q_time' => null,),
array( 'status' => $previous['status'],
'q_path' => null,
'q_time' => null,
),
array ('fast_hash' => $file_info['fast_hash']),
array ( '%s', '%s', '%d', ),
array ( '%s' )
Expand Down
139 changes: 63 additions & 76 deletions inc/spbc-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,6 @@ function spbc_settings__register() {
'type' => 'section',
'title' => __('Authentication and Logging in', 'security-malware-firewall'),
'fields' => array(
'block_timer__1_fails' => array(
'type' => 'field',
'input_type' => 'select',
'options' => array(
array('val' => 3, 'label' => __('3 seconds', 'security-malware-firewall'), ),
array('val' => 5, 'label' => __('5 seconds', 'security-malware-firewall'), ),
array('val' => 10, 'label' => __('10 seconds', 'security-malware-firewall'), ),
array('val' => 20, 'label' => __('20 seconds', 'security-malware-firewall'), ),
array('val' => 40, 'label' => __('40 seconds', 'security-malware-firewall'), ),
array('val' => 80, 'label' => __('80 seconds', 'security-malware-firewall'), ),
array('val' => 120, 'label' => __('120 seconds', 'security-malware-firewall'), ),
),
'title' => __('If someone fails authorization they will be blocked for ', 'security-malware-firewall'),
),
'block_timer__5_fails' => array(
'type' => 'field',
'input_type' => 'select',
Expand All @@ -237,7 +223,7 @@ function spbc_settings__register() {
array('val' => 43200, 'label' => __('12 hours', 'security-malware-firewall'), ),
array('val' => 86400, 'label' => __('24 hours', 'security-malware-firewall'), ),
),
'title' => __('If someone fails 5 authorizations in a row within one hour they will be blocked for ', 'security-malware-firewall'),
'title' => __('If someone fails 5 authorizations in a row within 15 min they will be blocked for ', 'security-malware-firewall'),
),
'2fa_enable' => array(
'type' => 'field',
Expand Down Expand Up @@ -334,6 +320,19 @@ function spbc_settings__register() {
'type' => 'field',
'title' => __('Enable autoscanning', 'security-malware-firewall'),
'description' => __('Scan website automatically each 24 hours.', 'security-malware-firewall'),
'childrens' => array('scanner_auto_start_manual'),
),
'scanner_auto_start_manual' => array(
'type' => 'field',
'title' => __('Manually set the auto scan time', 'security-malware-firewall'),
'description' => __('Scan website automatically at specific time.', 'security-malware-firewall'),
'childrens' => array('scanner_auto_start_manual_time'),
'parent' => 'scanner_auto_start',
),
'scanner_auto_start_manual_time' => array(
'type' => 'field',
'input_type' => 'time',
'parent' => 'scanner_auto_start_manual',
),
'scanner_outbound_links' => array(
'type' => 'field',
Expand Down Expand Up @@ -722,7 +721,7 @@ function spbc_settings__field__draw($field){
foreach($field['options'] as $option){
echo '<option'
. ' value="' . $option['val'] . '"'
. ($spbc->settings[$field['name']] == spbc_human_time_to_seconds($option['label']) ? 'selected' : '').'>'
. ($spbc->settings[$field['name']] == $option['val'] ? 'selected' : '').'>'
. $option['label']
. '</option>';
}
Expand Down Expand Up @@ -757,6 +756,12 @@ function spbc_settings__field__draw($field){
if(isset($field['description']))
echo '<div class="spbc_settings_description">'.$field['description'].'</div>';
break;
case 'time':
echo '<input type = "time" id="spbc_setting_'.$field['name'].'" name="spbc_settings['.$field['name'].']" '.($field['parent'] && !$spbc->settings[$field['parent']] ? ' disabled="disabled"' : '').'
'.'value="'.($spbc->settings[$field['name']] ? date('H:i', $spbc->settings[$field['name']]) : '').'" '.'>';
echo '<input type = "hidden" id = "user_timezone" name = "user_timezone" value = "">';
echo '<script type = "text/javascript">var date = new Date(); document.getElementById("user_timezone").value = -(date.getTimezoneOffset() / 60); if (document.getElementById("spbc_setting_'.$field['name'].'").value == "") { var currentTime = (date.getHours() < 10 ? "0" : "") + date.getHours() + ":" + (date.getMinutes() < 10 ? "0" : "") + date.getMinutes(); document.getElementById("spbc_setting_'.$field['name'].'").value = currentTime; } else {var initialDate = document.getElementById("spbc_setting_'.$field['name'].'").value; var dateObject = new Date(1900,0,1,initialDate.split(":")[0],initialDate.split(":")[1]); dateObject.setHours(dateObject.getHours() + -(date.getTimezoneOffset() / 60)); document.getElementById("spbc_setting_'.$field['name'].'").value = dateObject.getHours() + ":" + dateObject.getMinutes();}</script>';
break;
}

echo '</div>';
Expand Down Expand Up @@ -927,47 +932,50 @@ function spbc_settings__error__output(){
// Misc
'apikey' => __('API key validating: ', 'security-malware-firewall'),
'get_key' => __('Getting access key automatically: ', 'security-malware-firewall'),
'send_logs' => __('Sending security logs: ', 'security-malware-firewall'),
'send_firewall_logs' => __('Sending firewall logs: ', 'security-malware-firewall'),
'firewall_update' => __('Updating firewall: ' , 'security-malware-firewall'),
'signatures_update' => __('Updating signatures: ' , 'security-malware-firewall'),
'notice_paid_till' => __('Checking account status: ', 'security-malware-firewall'),
'access_key_notices' => __('Checking account status2: ', 'security-malware-firewall'),
// Cron
'send_php_logs' => __('PHP error log sending: ', 'security-malware-firewall'),
'cron_scan' => __('Scheduled scanning: ', 'security-malware-firewall'),
'cron' => __('Scheduled: ', 'security-malware-firewall'),
);

// Subtypes
$sub_types = array(
'get_hashes' => __('Getting hashs: ', 'security-malware-firewall'),
'get_hashes_plug' => __('Getting plugins hashes: ', 'security-malware-firewall'),
'clear_table' => __('Clearing table: ', 'security-malware-firewall'),
'surface_scan' => __('Surface scan: ', 'security-malware-firewall'),
'signature_scan' => __('Signature scanning: ', 'security-malware-firewall'),
'heuristic_scan' => __('Heuristic scanning: ', 'security-malware-firewall'),
'cure_backup' => __('Backuping: ', 'security-malware-firewall'),
'cure' => __('Curing: ', 'security-malware-firewall'),
'links_scan' => __('Links scanning: ', 'security-malware-firewall'),
'send_results' => __('Sending result: ', 'security-malware-firewall'),
);
if ($spbc->moderate == 1) {

$types['send_logs'] = __('Sending security logs: ', 'security-malware-firewall');
$types['send_firewall_logs'] = __('Sending firewall logs: ', 'security-malware-firewall');
$types['firewall_update'] = __('Updating firewall: ' , 'security-malware-firewall');
$types['signatures_update'] = __('Updating signatures: ' , 'security-malware-firewall');
$types['send_php_logs'] = __('PHP error log sending: ', 'security-malware-firewall');

// Subtypes
$sub_types = array(
'get_hashes' => __('Getting hashs: ', 'security-malware-firewall'),
'get_hashes_plug' => __('Getting plugins hashes: ', 'security-malware-firewall'),
'clear_table' => __('Clearing table: ', 'security-malware-firewall'),
'surface_scan' => __('Surface scan: ', 'security-malware-firewall'),
'signature_scan' => __('Signature scanning: ', 'security-malware-firewall'),
'heuristic_scan' => __('Heuristic scanning: ', 'security-malware-firewall'),
'cure_backup' => __('Backuping: ', 'security-malware-firewall'),
'cure' => __('Curing: ', 'security-malware-firewall'),
'links_scan' => __('Links scanning: ', 'security-malware-firewall'),
'send_results' => __('Sending result: ', 'security-malware-firewall'),
);
}

$errors_out = array();

foreach($errors as $type => $error){
if(!empty($error)){
if(!empty($error) && isset($types[$type])) {
if(is_array(current($error))){
foreach($error as $sub_type => $error){
$text = isset($error['error_time']) ? date('Y-m-d H:i:s', $error['error_time']) . ': ' : '';
$text .= isset($types[$type]) ? $types[$type] : $type.': ';
$text .= $types[$type];
$text .= isset($sub_types[$sub_type]) ? $sub_types[$sub_type] : $sub_type.': ';
$text .= $error['error'];
$errors_out[] = $text;
}
}else{
$text = isset($error['error_time']) ? date('Y-m-d H:i:s', $error['error_time']) . ': ' : '';
$text .= isset($types[$type]) ? $types[$type] : $type.': ';
$text .= $types[$type];
$text .= $error['error'];
$errors_out[] = $text;
}
Expand Down Expand Up @@ -1178,7 +1186,7 @@ function spbc_field_key( $values = null ) {
<input style="color:#666;" type="button" class="spbc_auto_link" value="'.__('Get access key manually', 'security-malware-firewall').'" />
</a>';
echo '&nbsp;'.__('or', 'security-malware-firewall').'&nbsp;';
echo '<input id="get_key_auto" name="spbc_get_apikey_auto" type="submit" class="spbc_manual_link" value="' . __('Get access key automatically', 'security-malware-firewall') . '" />';
echo '<input id="spbc_setting_get_key_auto" name="spbc_get_apikey_auto" type="submit" class="spbc_manual_link" value="' . __('Get access key automatically', 'security-malware-firewall') . '" />';
echo '<br/><br/>';
echo '<div style="font-size: 10pt; color: #666 !important">'
.sprintf(
Expand Down Expand Up @@ -1446,6 +1454,8 @@ function spbc_field_traffic_control_logs__prepare_data(&$table){
break;
case 'DENY_BY_DOS': $status = '<span class="spbcRed">' . __('Blocked by DoS prevention system', 'security-malware-firewall').'</span>';
break;
case 'DENY_BY_BFP': $status = '<span class="spbcRed">' . __('Blocked by BruteForce protection system', 'security-malware-firewall').'</span>';
break;

// WAF
case 'DENY_BY_WAF_XSS':
Expand Down Expand Up @@ -1708,7 +1718,7 @@ function spbc_field_scanner__prepare_data__files_qurantine(&$table){
'path' => strlen($root_path.$row->path) >= 40
? '<div class="spbcShortText">...'.$row->path.'</div><div class="spbcFullText spbc_hide">'.$root_path.$row->path.'</div>'
: $root_path.$row->path,
'q_status' => $row->q_status,
'previous_state' => $row->previous_state,
'severity' => $row->severity,
'q_time' => date('M d Y H:i:s', $row->q_time),
'size' => substr(number_format($row->size, 2, ',', ' '), 0, -3),
Expand Down Expand Up @@ -2051,7 +2061,7 @@ function spbc_field_scaner($params = array()){
'columns' => array(
'cb' => array('heading' => '<input type=checkbox>', 'class' => 'check-column',),
'path' => array('heading' => 'Path','primary' => true,),
'q_status' => array('heading' => 'Status',),
'previous_state' => array('heading' => 'Status',),
'severity' => array('heading' => 'Severity',),
'q_time' => array('heading' => 'Quarantine time',),
'size' => array('heading' => 'Size',),
Expand All @@ -2075,10 +2085,10 @@ function spbc_field_scaner($params = array()){
'delete' => array('name' => 'Delete',),
),
'sql' => array(
'add_col' => array_merge($default_args['sql']['add_col'], array('q_status', 'q_path', 'q_time',)),
'add_col' => array_merge($default_args['sql']['add_col'], array('previous_state', 'q_path', 'q_time',)),
'where' => ' WHERE status = "QUARANTINED"',
),
'sortable' => array('path', 'q_status', 'severity', 'q_time', 'size',),
'sortable' => array('path', 'previous_state', 'severity', 'q_time', 'size',),
)
);
}elseif($type_name == 'outbound_links'){
Expand Down Expand Up @@ -2300,7 +2310,8 @@ function spbc_field_debug__check_connection(){
function spbc_field_debug(){
global $spbc;
if($spbc->debug){
$output = print_r($spbc->debug, true);
$debug = get_option( SPBC_DEBUG );
$output = print_r($debug, true);
$output = str_replace("\n", "<br>", $output);
$output = preg_replace("/[^\S]{4}/", "&nbsp;&nbsp;&nbsp;&nbsp;", $output);
echo "<div class='spbc_wrapper_field'>";
Expand Down Expand Up @@ -2353,7 +2364,11 @@ function spbc_sanitize_settings( $settings ){
$spbc->data['last_php_log_sent'] = time()-86400*2;
$spbc->save('data');
}

if ($settings['scanner_auto_start_manual_time']) {
$user_tz_offset = isset($_POST['user_timezone']) ? $_POST['user_timezone'] : 0;
$settings['scanner_auto_start_manual_time'] = strtotime($settings['scanner_auto_start_manual_time']) - ($user_tz_offset * 3600);
\CleantalkSP\SpbctWp\Cron::updateTask( 'scanner__launch', 'spbc_scanner__launch', 86400, $settings['scanner_auto_start_manual_time'] - time() < 0 ? (3600*24 + $settings['scanner_auto_start_manual_time'] - time()) : $settings['scanner_auto_start_manual_time'] - time());
}
// Sanitizing website mirrors
if($settings['scanner_outbound_links_mirrors']){
if(preg_match('/^[\sa-zA-Z0-9,_\.\-\~]+$/', $settings['scanner_outbound_links_mirrors'])){
Expand Down Expand Up @@ -2426,36 +2441,6 @@ function spbc_sanitize_settings( $settings ){
}

}

// Sending logs.
$result = spbc_send_logs($sanitized_key);
if(empty($result['error'])){
$spbc->data['logs_last_sent'] = current_time('timestamp');
$spbc->data['last_sent_events_count'] = $result;
$spbc->error_delete('send_logs');
}else{
$spbc->error_add('send_logs', $result);
}

// Updating FW
$result = spbc_security_firewall_update(true, $sanitized_key);
if(empty($result['error'])){
$spbc->data['last_firewall_updated'] = current_time('timestamp');
$spbc->data['firewall_entries'] = $result;
$spbc->error_delete('firewall_update');
}else{
$spbc->error_add('firewall_update', $result);
}

// Sending FW logs
$result = spbc_send_firewall_logs($sanitized_key);
if(empty($result['error'])){
$spbc->data['last_firewall_send'] = current_time('timestamp');
$spbc->data['last_firewall_send_count'] = $result;
$spbc->error_delete('send_firewall_logs');
}else{
$spbc->error_add('send_firewall_logs', $result);
}

// If key provided by super admin
if ( is_main_site() ){
Expand Down Expand Up @@ -2508,7 +2493,9 @@ function spbc_sanitize_settings( $settings ){
);
$spbc->saveNetworkSettings();
}

if (get_option('spbc_settings') && get_option('spbc_settings') == $settings) {
do_action('updated_option', 'spbc_settings', get_option('spbc_settings'), $settings);
}
return $settings;
}

Expand Down
Loading

0 comments on commit 2f88f82

Please sign in to comment.