Skip to content

Commit

Permalink
Mod. Refactoring. Tasks saving, and loading refactored. Disable and a…
Browse files Browse the repository at this point in the history
…pprove actions implemented. #1
  • Loading branch information
alexandergull committed Nov 15, 2024
1 parent a30b65e commit 6c99e7d
Show file tree
Hide file tree
Showing 12 changed files with 605 additions and 145 deletions.
108 changes: 18 additions & 90 deletions inc/spbc-settings.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<?php

use CleantalkSP\SpbctWP\AdjustToEnvironmentModule\AdjustToEnvironmentSettings;
use CleantalkSP\SpbctWP\API;
use CleantalkSP\SpbctWP\Cron as SpbcCron;
use CleantalkSP\SpbctWP\Escape;
use CleantalkSP\SpbctWP\Helpers\Arr;
use CleantalkSP\SpbctWP\Helpers\IP;
use CleantalkSP\SpbctWP\HTTP\CDNHeadersChecker;
use CleantalkSP\SpbctWP\Scanner\ScanningLog\ScanningLogFacade;
use CleantalkSP\Variables\Post;
use CleantalkSP\Variables\Server;
use CleantalkSP\SpbctWP\API;
use CleantalkSP\SpbctWP\LinkConstructor;
use CleantalkSP\SpbctWP\ListTable;
use CleantalkSP\SpbctWP\Scanner;
use CleantalkSP\SpbctWP\Helpers\IP;
use CleantalkSP\SpbctWP\Helpers\Arr;
use CleantalkSP\SpbctWP\Helpers\CSV;
use CleantalkSP\SpbctWP\Escape;
use CleantalkSP\SpbctWP\Scanner\OSCron\OSCronModel;
use CleantalkSP\SpbctWP\Scanner\OSCron\OSCronView;
use CleantalkSP\SpbctWP\Scanner\ScanningLog\ScanningLogFacade;
use CleantalkSP\SpbctWP\Variables\Cookie;
use CleantalkSP\SpbctWP\Views\Settings;
use CleantalkSP\SpbctWP\VulnerabilityAlarm\VulnerabilityAlarmView;
use CleantalkSP\SpbctWP\Variables\Cookie;
use CleantalkSP\SpbctWP\LinkConstructor;
use CleantalkSP\Variables\Post;
use CleantalkSP\Variables\Server;

// Scanner AJAX actions
require_once(SPBC_PLUGIN_DIR . 'inc/spbc-scanner.php');
Expand Down Expand Up @@ -2911,7 +2912,7 @@ function spbc_field_scanner__prepare_data__analysis_log(&$table)
*/
function spbc_scanner_oscron_count_found()
{
return count(spbc_scanner_oscron_get_scanned());
return OSCronView::getCountOfTasksScanned();
}

/**
Expand All @@ -2920,18 +2921,7 @@ function spbc_scanner_oscron_count_found()
*/
function spbc_scanner_oscron_get_scanned()
{
$result = get_option('spbc_oscron_result', []);

if (!empty($result)) {
$result = json_decode($result, true);
}

if (is_null($result)) {
$result = [];
}


return $result;
return OSCronModel::getTasksFromStorage();
}

/**
Expand All @@ -2940,63 +2930,7 @@ function spbc_scanner_oscron_get_scanned()
*/
function spbc_scanner_oscron_prepare_data(&$table)
{
foreach ($table->rows as $key => $row) {
$table->items[$key] = array(
'uid' => $row['id'],
'cb' => $row['id'],
'id' => $row['id'],
'repeat' => spbc_scanner_oscron_time_to_human_readable($row['repeat']),
'command' => $row['command'],
// 'actions' => $row['actions'],
);
}
}

/**
* Convert cron time to human readable.
* @param $time
* @return string
*/
function spbc_scanner_oscron_time_to_human_readable($time)
{
$cronParts = explode(' ', $time);

if (count($cronParts) !== 5) {
return __('Invalid cron expression', 'security-malware-firewall');
}

list($minute, $hour, $dayOfMonth, $month, $dayOfWeek) = $cronParts;

$humanReadable = __('At ', 'security-malware-firewall');

// Handle minutes
if ($minute === '*') {
$humanReadable .= __('every minute', 'security-malware-firewall');
} else {
$humanReadable .= __('minute', 'security-malware-firewall') . ' ' . $minute;
}

// Handle hours
if ($hour !== '*') {
$humanReadable .= __(' past hour ', 'security-malware-firewall') . $hour;
}

// Handle days of the month
if ($dayOfMonth !== '*') {
$humanReadable .= __(' on day ', 'security-malware-firewall') . $dayOfMonth;
}

// Handle months
if ($month !== '*') {
$humanReadable .= __(' of month ', 'security-malware-firewall') . $month;
}

// Handle days of the week
if ($dayOfWeek !== '*') {
$humanReadable .= __(' on day of the week ', 'security-malware-firewall') . $dayOfWeek;
}

return $humanReadable;
$table = OSCronView::prepareTableData($table);
}

function spbc_field_scanner__prepare_data__files_quarantine(&$table)
Expand Down Expand Up @@ -4118,20 +4052,14 @@ function spbc_list_table__get_args_by_type($table_type)
'func_data_prepare' => 'spbc_scanner_oscron_prepare_data',
'if_empty_items' => '<p class="spbc_hint">' . __('Crontab not found.', 'security-malware-firewall') . '</p>',
'columns' => array(
'cb' => array('heading' => '<input type=checkbox>', 'class' => 'check-column', 'width_percent' => 5),
'id' => array('heading' => 'id', 'primary' => true, 'width_percent' => 15),
'repeat' => array('heading' => 'repeat', 'primary' => true, 'width_percent' => 15),
'command' => array('heading' => 'command', 'primary' => true, 'width_percent' => 65),
'line_number' => array('heading' => 'line_number', 'primary' => true, 'width_percent' => 15),
'command' => array('heading' => 'command', 'primary' => true, 'width_percent' => 50),
),
'actions' => array(
'delete' => array('name' => 'Delete',),
'quarantine' => array('name' => 'Quarantine',),
'approve' => array('name' => 'Approve',),
),
'bulk_actions' => array(
'delete' => array('name' => 'Delete',),
'quarantine' => array('name' => 'Quarantine',),
'approve' => array('name' => 'Approve',),
'disable_oscron_task' => array('name' => 'Disable task',),
'approve_oscron_task' => array('name' => 'Approve task',),
),
)
);
Expand Down
2 changes: 1 addition & 1 deletion js/spbc-scanner-plugin.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/spbc-scanner-plugin.min.js.map

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions lib/CleantalkSP/Common/Helpers/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ public static function sanitizeFromEmptyLines($buffer)
return $buffer;
}

/**
* Parse Comma-separated values, without formatting to csv
*
* @param $buffer
*
* @return false|string[]
*/
public static function parseCSVLite($buffer)
{
$buffer = explode("\n", $buffer);
$buffer = self::sanitizeFromEmptyLines($buffer);

foreach ($buffer as &$line) {
if ($line !== '') {
$line = substr($line, 6);
}
}

return $buffer;
}

/**
* Parse Comma-separated values
*
Expand Down
25 changes: 25 additions & 0 deletions lib/CleantalkSP/SpbctWP/ListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CleantalkSP\SpbctWP;

use CleantalkSP\SpbctWP\Scanner\Cure;
use CleantalkSP\SpbctWP\Scanner\OSCron\OSCronController;
use CleantalkSP\Variables\Post;

class ListTable
Expand Down Expand Up @@ -689,6 +690,12 @@ public static function ajaxRowActionHandler()
case 'copy_file_info':
self::ajaxRowActionHandlerCopyFileInfo();
break;
case 'disable_oscron_task':
self::ajaxRowActionHandlerDisableOSCronTask();
break;
case 'approve_oscron_task':
self::ajaxRowActionHandlerApproveOSCronTask();
break;
default:
wp_send_json(array('temp_html' => '<div class="spbc-popup-msg popup--red">UNKNOWN ACTION</div>'));
}
Expand All @@ -697,6 +704,24 @@ public static function ajaxRowActionHandler()
}
}

public static function ajaxRowActionHandlerApproveOSCronTask()
{
$result = OSCronController::approveLine(Post::get('id', null, 'word'));
if ($result) {
wp_send_json_success();
}
wp_send_json_error();
}

public static function ajaxRowActionHandlerDisableOSCronTask()
{
$result = OSCronController::disableLine(Post::get('id', null, 'word'));
if ($result) {
wp_send_json_success();
}
wp_send_json_error();
}

public static function ajaxRowActionHandlerApprove()
{
$out = spbc_scanner_file_approve(true, Post::get('id', null, 'word'));
Expand Down
52 changes: 52 additions & 0 deletions lib/CleantalkSP/SpbctWP/Scanner/OSCron/OSCronController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace CleantalkSP\SpbctWP\Scanner\OSCron;

class OSCronController
{
/**
* @param $uid
* @param $status
* @return bool
* @throws \Exception
*/
private static function updateLine($uid, $status)
{
$line_to_change = OSCronModel::getTaskById($uid);
$line_to_change->setStatus($status);
$result = OSCronModel::updateTaskById($uid, $line_to_change);
if (false === $result) {
return $result;
}
return OSCronModel::rewriteCronTabFile();
}
/**
* @param $uid
* @return bool
* @throws \Exception
*/
public static function approveLine($uid)
{
return static::updateLine($uid, 'approved');
}
/**
* @param $uid
* @return false|int
* @throws \Exception
*/
public static function disableLine($uid)
{
return static::updateLine($uid, 'disabled');
}
/**
* @param $uid
* @return false|int
* @throws \Exception
*/
public static function dangerLine($uid)
{
$line_to_change = OSCronModel::getTaskById($uid);
$line_to_change->setStatus('dangerous');
return OSCronModel::updateTaskById($uid, $line_to_change);
}
}
17 changes: 17 additions & 0 deletions lib/CleantalkSP/SpbctWP/Scanner/OSCron/OSCronGetEnvCron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace CleantalkSP\SpbctWP\Scanner\OSCron;

class OSCronGetEnvCron
{
private static $read_command = 'crontab -l';

/**
* @return string
* @psalm-suppress ForbiddenCode
*/
public static function get()
{
return (string)@shell_exec(static::$read_command);
}
}
Loading

0 comments on commit 6c99e7d

Please sign in to comment.