-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mod. Refactoring. Tasks saving, and loading refactored. Disable and a…
…pprove actions implemented. #1
1 parent
a30b65e
commit 52f3ce0
Showing
12 changed files
with
605 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
lib/CleantalkSP/SpbctWP/Scanner/OSCron/OSCronController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 updateTask($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 approveTask($uid) | ||
{ | ||
return static::updateTask($uid, 'approved'); | ||
} | ||
/** | ||
* @param $uid | ||
* @return false|int | ||
* @throws \Exception | ||
*/ | ||
public static function disableTask($uid) | ||
{ | ||
return static::updateTask($uid, 'disabled'); | ||
} | ||
/** | ||
* @param $uid | ||
* @return false|int | ||
* @throws \Exception | ||
*/ | ||
public static function dangerTask($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
17
lib/CleantalkSP/SpbctWP/Scanner/OSCron/OSCronGetEnvCron.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
<?php | ||
|
||
namespace CleantalkSP\SpbctWP\Scanner\OSCron; | ||
|
||
class OSCronModel | ||
{ | ||
private static $tasks_storage_name = 'spbc_oscron_tasks'; | ||
private static $file_storage_name = 'spbc_oscron_file'; | ||
private static $spbc_marker = '# Disabled by Security by CleanTalk #'; | ||
|
||
/** | ||
* @return string|true | ||
*/ | ||
public static function run() | ||
{ | ||
try { | ||
$new_cron_file = static::getEnvCrontab(); | ||
|
||
if (static::loadCrontabFile() === $new_cron_file) { | ||
throw new \Exception(__('No difference found since last check.', 'security-malware-firewall')); | ||
} | ||
static::saveCrontabFile($new_cron_file); | ||
$tasks = static::parseTasks($new_cron_file); | ||
static::saveTasksToStorage($tasks); | ||
} catch (\Exception $error) { | ||
return (string)$error; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @param $cron_file | ||
* @return OSCronTask[] | ||
* @throws \Exception | ||
*/ | ||
private static function parseTasks($cron_file) | ||
{ | ||
$result = explode("\n", $cron_file); | ||
|
||
$result = array_map('trim', $result); | ||
|
||
$tasks = array(); | ||
|
||
$line_number = 0; | ||
|
||
foreach ($result as $item) { | ||
$line_number++; | ||
if (empty($item) || strpos($item, '#') === 0) { | ||
continue; | ||
} | ||
$parts = preg_split('/\s+/', $item, 6); | ||
$time_pattern = implode(' ', array_slice($parts, 0, 5)); | ||
$command = $parts[5]; | ||
$task = new OSCronTask(); | ||
$tasks[] = $task->setCommand($command) | ||
->setStatus('found') | ||
->setLineNumber($line_number) | ||
->setRepeatsPattern($time_pattern) | ||
->validate(); | ||
} | ||
|
||
return $tasks; | ||
} | ||
|
||
/** | ||
* @param $tasks | ||
* @return void | ||
*/ | ||
public static function saveTasksToStorage($tasks) | ||
{ | ||
update_option(static::$tasks_storage_name, $tasks); | ||
} | ||
|
||
/** | ||
* @return OSCronTask[]|array | ||
*/ | ||
public static function getTasksFromStorage($as_array = true) | ||
{ | ||
$tasks = get_option('spbc_oscron_result', []); | ||
|
||
if (is_null($tasks) || false === $tasks) { | ||
return array(); | ||
} | ||
|
||
if ($as_array) { | ||
$result_array = array(); | ||
foreach ($tasks as $task) { | ||
$result_array[] = $task->getArray(); | ||
} | ||
return $result_array; | ||
} | ||
|
||
return $tasks; | ||
} | ||
|
||
/** | ||
* @param string $uid | ||
* @return OSCronTask|false | ||
*/ | ||
public static function getTaskById($uid) | ||
{ | ||
$tasks = static::getTasksFromStorage(false); | ||
foreach ($tasks as $task) { | ||
if ($task->id === $uid) { | ||
return $task; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @param string $uid | ||
* @param OSCronTask $new_task | ||
* @return int|false | ||
* @throws \Exception | ||
*/ | ||
public static function updateTaskById($uid, $new_task) | ||
{ | ||
$new_task->validate(); | ||
$tasks = static::getTasksFromStorage(false); | ||
foreach ($tasks as $key => $task) { | ||
if ($task->id === $uid) { | ||
$tasks[$key] = $new_task; | ||
static::saveTasksToStorage($tasks); | ||
return $key; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @return bool | ||
* @throws \Exception | ||
*/ | ||
public static function rewriteCronTabFile() | ||
{ | ||
$tasks = static::getTasksFromStorage(false); | ||
$new_cron_file = static::loadCrontabFile(); | ||
foreach ($tasks as $task) { | ||
$the_word = $task->repeats . ' ' . $task->command; | ||
if ($task->status === 'disabled') { | ||
$new_cron_file = str_replace($the_word, static::$spbc_marker . $the_word, $new_cron_file); | ||
} | ||
if ($task->status === 'approved') { | ||
$new_cron_file = str_replace(static::$spbc_marker, '', $new_cron_file); | ||
} | ||
} | ||
static::saveCrontabFile($new_cron_file); | ||
static::saveEnvCronTab($new_cron_file); | ||
return true; | ||
} | ||
|
||
/** | ||
* @param $cron_file | ||
* @return void | ||
*/ | ||
public static function saveCrontabFile($cron_file) | ||
{ | ||
update_option(static::$file_storage_name, @base64_encode($cron_file)); | ||
} | ||
|
||
/** | ||
* @return string | ||
* @throws \Exception | ||
*/ | ||
public static function loadCrontabFile() | ||
{ | ||
$result = get_option(static::$file_storage_name); | ||
if ($result) { | ||
$result = @base64_decode($result); | ||
if ($result) { | ||
return $result; | ||
} | ||
} | ||
throw new \Exception(__('Cannot load crontab file from storage', 'security-malware-firewall')); | ||
} | ||
|
||
/** | ||
* @return string | ||
* @throws \Exception | ||
*/ | ||
private static function getEnvCrontab() | ||
{ | ||
$cron_file = OSCronGetEnvCron::get(); | ||
//test | ||
// $cron_file = '1 * * * * echo | ||
// | ||
// | ||
// | ||
//5 * * * * bash | ||
//#not a valid string'; | ||
if (empty($cron_file)) { | ||
throw new \Exception(__('No crontab file found in the server environment.', 'security-malware-firewall')); | ||
} | ||
return $cron_file; | ||
} | ||
|
||
private static function saveEnvCronTab($changed_file_content) | ||
{ | ||
OSCronSetEnvCron::set($changed_file_content); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
lib/CleantalkSP/SpbctWP/Scanner/OSCron/OSCronSetEnvCron.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace CleantalkSP\SpbctWP\Scanner\OSCron; | ||
|
||
class OSCronSetEnvCron | ||
{ | ||
private static $write_command_template = 'echo "%s"| crontab -'; | ||
|
||
/** | ||
* @return string | ||
* @psalm-suppress ForbiddenCode | ||
*/ | ||
public static function set($command) | ||
{ | ||
//probably need to sanitize $command | ||
//$command = escapeshellarg($command); | ||
$command = sprintf(static::$write_command_template, $command); | ||
return (string)@shell_exec($command); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace CleantalkSP\SpbctWP\Scanner\OSCron; | ||
|
||
class OSCronTask | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $id; | ||
/** | ||
* @var string | ||
*/ | ||
public $status; | ||
/** | ||
* @var int | ||
*/ | ||
public $repeats; | ||
/** | ||
* @var int | ||
*/ | ||
public $command; | ||
/** | ||
* @var int | ||
*/ | ||
public $line_number; | ||
|
||
private $statuses_list = array( | ||
'found', | ||
'approved', | ||
'disabled', | ||
'dangerous', | ||
); | ||
|
||
public function __construct() | ||
{ | ||
$this->setID(); | ||
} | ||
|
||
private function setID() | ||
{ | ||
$this->id = uniqid(); | ||
} | ||
|
||
/** | ||
* @param string $status | ||
* @return OSCronTask | ||
* @throws \Exception | ||
*/ | ||
public function setStatus($status) | ||
{ | ||
if (!is_string($status) || !in_array($status, $this->statuses_list)) { | ||
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . 'invalid arg'); | ||
} | ||
|
||
$this->status = $status; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $command | ||
* @return OSCronTask | ||
* @throws \Exception | ||
*/ | ||
public function setCommand($command) | ||
{ | ||
if (!is_string($command)) { | ||
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . 'invalid arg'); | ||
} | ||
$this->command = $command; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $repeats | ||
* @return OSCronTask | ||
* @throws \Exception | ||
*/ | ||
public function setRepeatsPattern($repeats) | ||
{ | ||
if (!is_string($repeats)) { | ||
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . 'invalid arg'); | ||
} | ||
$this->repeats = $repeats; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param int $line_number | ||
* @return OSCronTask | ||
* @throws \Exception | ||
*/ | ||
public function setLineNumber($line_number) | ||
{ | ||
if (!is_int($line_number)) { | ||
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . 'invalid arg'); | ||
} | ||
$this->line_number = $line_number; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return OSCronTask | ||
* @throws \Exception | ||
*/ | ||
public function validate() | ||
{ | ||
foreach (get_object_vars($this) as $property => $value) { | ||
if (is_null($value)) { | ||
throw new \Exception(__CLASS__ . '::' . __FUNCTION__ . ' - ' . $property . ' is not set'); | ||
} | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getArray() | ||
{ | ||
$result = array(); | ||
foreach (get_object_vars($this) as $property => $value) { | ||
$result[$property] = $value; | ||
} | ||
unset($result['statuses_list']); | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace CleantalkSP\SpbctWP\Scanner\OSCron; | ||
|
||
class OSCronView | ||
{ | ||
/** | ||
* @return int | ||
*/ | ||
public static function getCountOfTasksScanned() | ||
{ | ||
return count(OSCronModel::getTasksFromStorage()); | ||
} | ||
|
||
/** | ||
* @param object $table | ||
* @return object | ||
*/ | ||
public static function prepareTableData($table) | ||
{ | ||
foreach ($table->rows as $key => $row) { | ||
$actions = $row['actions']; | ||
if ($row['status'] === 'approved') { | ||
unset($actions['approve_oscron_task']); | ||
} | ||
if ($row['status'] === 'disabled') { | ||
unset($actions['disable_oscron_task']); | ||
} | ||
$table->items[$key] = array( | ||
'uid' => $row['id'], | ||
'cb' => $row['id'], | ||
'id' => $row['id'], | ||
'repeat' => static::timePatternToHumanReadable($row['repeats']), | ||
'command' => $row['command'], | ||
'actions' => $actions, | ||
); | ||
} | ||
return $table; | ||
} | ||
|
||
/** | ||
* Convert cron time to human readable. | ||
* @param $time | ||
* @return string | ||
*/ | ||
private static function timePatternToHumanReadable($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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters