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. #2
  • Loading branch information
alexandergull committed Nov 16, 2024
1 parent 52f3ce0 commit 9084db8
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 35 deletions.
2 changes: 1 addition & 1 deletion inc/spbc-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2921,7 +2921,7 @@ function spbc_scanner_oscron_count_found()
*/
function spbc_scanner_oscron_get_scanned()
{
return OSCronModel::getTasksFromStorage();
return OSCronModel::getTasksFromStorageAsArray();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/CleantalkSP/SpbctWP/Scanner/OSCron/OSCronController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private static function updateTask($uid, $status)
$line_to_change->setStatus($status);
$result = OSCronModel::updateTaskById($uid, $line_to_change);
if (false === $result) {
return $result;
return false;
}
return OSCronModel::rewriteCronTabFile();
}
Expand All @@ -31,7 +31,7 @@ public static function approveTask($uid)
}
/**
* @param $uid
* @return false|int
* @return bool
* @throws \Exception
*/
public static function disableTask($uid)
Expand Down
43 changes: 43 additions & 0 deletions lib/CleantalkSP/SpbctWP/Scanner/OSCron/OSCronLocale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace CleantalkSP\SpbctWP\Scanner\OSCron;


use CleantalkSP\Templates\Singleton;

class OSCronLocale
{

use Singleton;
public $task_status_approved;
public $task_status_disabled;
public $task_status_danger;
public $no_windows_support;
public $no_difference;
public $error_write_cron_storage;
public $error_write_cron_env;
public $error_load_cron_storage;
public $error_load_cron_env;
public $error_invalid_arg;
public $error_missing_property;
public $error_load_tasks_storage;
public $error_invalid_cron_expression;

public function init()
{
$this->task_status_approved = __('Approved', 'security-malware-firewall');
$this->task_status_disabled = __('Disabled', 'security-malware-firewall');
$this->task_status_danger = __('Danger', 'security-malware-firewall');
$this->no_windows_support = __('Windows OS cron handling is not supported.', 'security-malware-firewall');
$this->no_difference = __('No difference found since last check.', 'security-malware-firewall');
$this->error_write_cron_storage = __('Cannot write cron to storage', 'security-malware-firewall');
$this->error_write_cron_env = __('Cannot write cron to environment', 'security-malware-firewall');
$this->error_load_cron_storage = __('Cannot load cron content from storage', 'security-malware-firewall');
$this->error_load_cron_env = __('No crontab file found in the server environment.', 'security-malware-firewall');
$this->error_load_tasks_storage = __('Cannot load tasks from storage', 'security-malware-firewall');
$this->error_invalid_arg = __('invalid argument', 'security-malware-firewall');
$this->error_missing_property = __('property is no set', 'security-malware-firewall');
$this->error_invalid_cron_expression = __('Invalid cron expression', 'security-malware-firewall');
//$this->var = __('', 'security-malware-firewall');
}
}
76 changes: 51 additions & 25 deletions lib/CleantalkSP/SpbctWP/Scanner/OSCron/OSCronModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,35 @@ class OSCronModel
public static function run()
{
try {
if (static::isWindows()) {
throw new \Exception(OSCronLocale::getInstance()->no_windows_support);
}

$new_cron_file = static::getEnvCrontab();

if (static::loadCrontabFile() === $new_cron_file) {
throw new \Exception(__('No difference found since last check.', 'security-malware-firewall'));
if (static::loadCronFileFromStorage() === $new_cron_file) {
throw new \Exception(OSCronLocale::getInstance()->no_difference);
}
static::saveCrontabFile($new_cron_file);
static::saveCronFileToStorage($new_cron_file);
$tasks = static::parseTasks($new_cron_file);
static::saveTasksToStorage($tasks);
} catch (\Exception $error) {
return (string)$error;
return $error->getMessage();
}
return true;
}

public static function isWindows()
{
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}

/**
* @param $cron_file
* @return OSCronTask[]
* @throws \Exception
*/
private static function parseTasks($cron_file)
public static function parseTasks($cron_file)
{
$result = explode("\n", $cron_file);

Expand Down Expand Up @@ -72,34 +81,45 @@ public static function saveTasksToStorage($tasks)
}

/**
* @return OSCronTask[]|array
* @return OSCronTask[]
*/
public static function getTasksFromStorage($as_array = true)
public static function getTasksFromStorage()
{
$tasks = get_option('spbc_oscron_result', []);
$tasks = get_option(static::$tasks_storage_name, []);

if (is_null($tasks) || false === $tasks) {
return array();
}

if ($as_array) {
$result_array = array();
foreach ($tasks as $task) {
return $tasks;
}

/**
* @return array
*/
public static function getTasksFromStorageAsArray()
{
global $spbc;
$tasks = static::getTasksFromStorage();
$result_array = array();
foreach ($tasks as $task) {
if ($task instanceof OSCronTask) {
$result_array[] = $task->getArray();
} else {
$spbc->error_add('', OSCronLocale::getInstance()->error_load_cron_storage);
}
return $result_array;
}

return $tasks;
return $result_array;
}

/**
* @param string $uid
* @return OSCronTask|false
* @throws \Exception
*/
public static function getTaskById($uid)
{
$tasks = static::getTasksFromStorage(false);
$tasks = static::getTasksFromStorage();
foreach ($tasks as $task) {
if ($task->id === $uid) {
return $task;
Expand All @@ -117,7 +137,7 @@ public static function getTaskById($uid)
public static function updateTaskById($uid, $new_task)
{
$new_task->validate();
$tasks = static::getTasksFromStorage(false);
$tasks = static::getTasksFromStorage();
foreach ($tasks as $key => $task) {
if ($task->id === $uid) {
$tasks[$key] = $new_task;
Expand All @@ -134,8 +154,8 @@ public static function updateTaskById($uid, $new_task)
*/
public static function rewriteCronTabFile()
{
$tasks = static::getTasksFromStorage(false);
$new_cron_file = static::loadCrontabFile();
$tasks = static::getTasksFromStorage();
$new_cron_file = static::loadCronFileFromStorage();
foreach ($tasks as $task) {
$the_word = $task->repeats . ' ' . $task->command;
if ($task->status === 'disabled') {
Expand All @@ -145,16 +165,22 @@ public static function rewriteCronTabFile()
$new_cron_file = str_replace(static::$spbc_marker, '', $new_cron_file);
}
}
static::saveCrontabFile($new_cron_file);
static::saveCronFileToStorage($new_cron_file);
if ($new_cron_file !== static::loadCronFileFromStorage()) {
throw new \Exception(OSCronLocale::getInstance()->error_write_cron_storage);
}
static::saveEnvCronTab($new_cron_file);
if ($new_cron_file !== static::getEnvCrontab()) {
throw new \Exception(OSCronLocale::getInstance()->error_write_cron_env);
}
return true;
}

/**
* @param $cron_file
* @return void
*/
public static function saveCrontabFile($cron_file)
public static function saveCronFileToStorage($cron_file)
{
update_option(static::$file_storage_name, @base64_encode($cron_file));
}
Expand All @@ -163,16 +189,16 @@ public static function saveCrontabFile($cron_file)
* @return string
* @throws \Exception
*/
public static function loadCrontabFile()
public static function loadCronFileFromStorage()
{
$result = get_option(static::$file_storage_name);
if ($result) {
if (false !== $result) {
$result = @base64_decode($result);
if ($result) {
if (false !== $result) {
return $result;
}
}
throw new \Exception(__('Cannot load crontab file from storage', 'security-malware-firewall'));
throw new \Exception(OSCronLocale::getInstance()->error_load_cron_storage);
}

/**
Expand All @@ -190,7 +216,7 @@ private static function getEnvCrontab()
//5 * * * * bash
//#not a valid string';
if (empty($cron_file)) {
throw new \Exception(__('No crontab file found in the server environment.', 'security-malware-firewall'));
throw new \Exception(OSCronLocale::getInstance()->error_load_cron_env);
}
return $cron_file;
}
Expand Down
10 changes: 5 additions & 5 deletions lib/CleantalkSP/SpbctWP/Scanner/OSCron/OSCronTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private function setID()
public function setStatus($status)
{
if (!is_string($status) || !in_array($status, $this->statuses_list)) {
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . 'invalid arg');
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . ' ' . OSCronLocale::getInstance()->error_missing_property);
}

$this->status = $status;
Expand All @@ -65,7 +65,7 @@ public function setStatus($status)
public function setCommand($command)
{
if (!is_string($command)) {
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . 'invalid arg');
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . ' ' . OSCronLocale::getInstance()->error_missing_property);
}
$this->command = $command;
return $this;
Expand All @@ -79,7 +79,7 @@ public function setCommand($command)
public function setRepeatsPattern($repeats)
{
if (!is_string($repeats)) {
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . 'invalid arg');
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . ' ' . OSCronLocale::getInstance()->error_missing_property);
}
$this->repeats = $repeats;
return $this;
Expand All @@ -93,7 +93,7 @@ public function setRepeatsPattern($repeats)
public function setLineNumber($line_number)
{
if (!is_int($line_number)) {
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . 'invalid arg');
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . ' ' . OSCronLocale::getInstance()->error_missing_property);
}
$this->line_number = $line_number;
return $this;
Expand All @@ -107,7 +107,7 @@ public function validate()
{
foreach (get_object_vars($this) as $property => $value) {
if (is_null($value)) {
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . ' - ' . $property . ' is not set');
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . ' - ' . $property . ' ' . OSCronLocale::getInstance()->error_missing_property);
}
}
return $this;
Expand Down
4 changes: 2 additions & 2 deletions lib/CleantalkSP/SpbctWP/Scanner/OSCron/OSCronView.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function prepareTableData($table)
}

/**
* Convert cron time to human readable.
* Convert cron time to a human-readable string.
* @param $time
* @return string
*/
Expand All @@ -48,7 +48,7 @@ private static function timePatternToHumanReadable($time)
$cronParts = explode(' ', $time);

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

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

0 comments on commit 9084db8

Please sign in to comment.