Skip to content

Commit

Permalink
Mod. FS Watcher updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandergull committed Jan 17, 2024
1 parent 23d5933 commit a6c2da3
Show file tree
Hide file tree
Showing 22 changed files with 675 additions and 40 deletions.
14 changes: 14 additions & 0 deletions inc/admin-bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,20 @@ function spbc_admin__admin_bar__add_child_nodes($wp_admin_bar)
'title' => '<a href="' . $spbc->settings_link . '&spbc_tab=settings_general">' . __('Settings', 'security-malware-firewall') . '</a>' . $attention_mark,
));

// FS Watcher
if ($spbc->settings['scanner__fs_watcher']) {
$journals_count_text = '&nbsp;<b style="color: green;">Enabled</b>';
$link_suffix = '&spbc_tab=fswatcher';
} else {
$journals_count_text = '&nbsp;<b style="color: gray;">Disabled</b>';
$link_suffix = '&spbc_tab=settings_general#spbc_setting_scanner__fs_watcher';
}
$wp_admin_bar->add_node(array(
'parent' => 'spbc__parent_node',
'id' => 'spbc_admin_bar__fs_watcher',
'title' => '<a href="' . $spbc->settings_link . $link_suffix . '">' . __('File System Journal', 'security-malware-firewall') . $journals_count_text . '</a>',
));

// Summary
$wp_admin_bar->add_node(array(
'parent' => 'spbc__parent_node',
Expand Down
19 changes: 17 additions & 2 deletions inc/spbc-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,22 @@ function spbc_settings__register()
'scanner__fs_watcher' => array(
'type' => 'field',
'title' => __('File System Journal', 'security-malware-firewall'),
'description' => View::getFSWatcherDescription(new \CleantalkSP\SpbctWP\FSWatcher\View\Phrases()),
'description' => \CleantalkSP\SpbctWP\FSWatcher\View\View::getFSWatcherDescription(new \CleantalkSP\SpbctWP\FSWatcher\View\Phrases()),
'children' => array('scanner__fs_watcher__snapshots_period')
),
'scanner__fs_watcher__snapshots_period' => array(
'type' => 'field',
'input_type' => 'select',
'options' => array(
array('val' => 3600, 'label' => __('1 hour', 'security-malware-firewall'),),
array('val' => 10800, 'label' => __('3 hours', 'security-malware-firewall'),),
array('val' => 21600, 'label' => __('6 hours', 'security-malware-firewall'),),
array('val' => 43200, 'label' => __('12 hours', 'security-malware-firewall'),),
array('val' => 86400, 'label' => __('1 day', 'security-malware-firewall'),),
),
'title' => __('Snapshots period', 'security-malware-firewall'),
'description' => \CleantalkSP\SpbctWP\FSWatcher\View\View::getFSWatcherSnapshotsPeriodDescription(new \CleantalkSP\SpbctWP\FSWatcher\View\Phrases()),
'parent' => 'scanner__fs_watcher'
),
),
),
Expand Down Expand Up @@ -1605,7 +1620,7 @@ function spbc_tab__summary()
function spbc_tab__fswatcher()
{
echo "<div class='spbc_wrapper_field'>";
echo \CleantalkSP\Common\FSWatcher\View\View::renderSelectors(new \CleantalkSP\SpbctWP\FSWatcher\View\Phrases());
echo \CleantalkSP\SpbctWP\FSWatcher\View\View::renderSelectors(new \CleantalkSP\SpbctWP\FSWatcher\View\Phrases());
echo '</div>';
}

Expand Down
8 changes: 4 additions & 4 deletions lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public static function getCompareResult()
$first = $second;
$second = $tmp;
}

$first_journal = Controller::$storage::getJournal($first);
$second_journal = Controller::$storage::getJournal($second);
$storage = Controller::$storage;
$first_journal = $storage::getJournal($first);
$second_journal = $storage::getJournal($second);

if (!$first_journal || !$second_journal) {
return false;
Expand All @@ -41,7 +41,7 @@ public static function getCompareResult()
* @param $second_journal
* @return array|false
*/
private static function compare($first_journal, $second_journal)
protected static function compare($first_journal, $second_journal)
{
$result = array(
'added' => array(),
Expand Down
8 changes: 4 additions & 4 deletions lib/CleantalkSP/Common/FSWatcher/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class Controller

const STATUS_RUNNING = 'running';

const EXECUTION_MIN_INTERVAL = 6000; // 1,66 hours
const EXECUTION_MIN_INTERVAL = 3600; // 1 hour

/**
* @var \CleantalkSP\Common\FSWatcher\Storage\Storage
* @var \CleantalkSP\Common\FSWatcher\Storage\FileStorage::class
*/
public static $storage;

/**
* @var \CleantalkSP\Common\FSWatcher\Repository\Repository
* @var \CleantalkSP\Common\FSWatcher\Repository\FileRepository::class'
*/
public static $repository;

Expand Down Expand Up @@ -101,7 +101,7 @@ public static function work($params)
* @param $params
* @return void
*/
private static function run($params)
protected static function run($params)
{
self::$status = self::STATUS_RUNNING;
Scan::run($params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class FileRepository implements Repository
*/
public static function getAvailableJournals()
{
return Controller::$storage::getAvailableJournals();
$storage = Controller::$storage;
return $storage::getAvailableJournals();
}
}
16 changes: 9 additions & 7 deletions lib/CleantalkSP/Common/FSWatcher/Scan/Scan.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,39 @@

class Scan
{
private static $dir_to_watch = '';
private static $exclude_dirs = array();
private static $extensions_to_watch = array('php');
protected static $dir_to_watch = '';
protected static $exclude_dirs = array();
protected static $extensions_to_watch = array('php');

public static function run($params)
{
self::setParams($params);

$journal = Controller::$storage::makeProcessingJournal();
$storage = Controller::$storage;
$journal = $storage::makeProcessingJournal();
if (Controller::$debug) {
Logger::log('create processing journal ' . $journal);
}

self::scan();
}

private static function setParams($params)
protected static function setParams($params)
{
self::$dir_to_watch = $params['dir_to_watch'];
self::$exclude_dirs = $params['exclude_dirs'];
self::$extensions_to_watch = $params['extensions_to_watch'];
}

private static function scan()
protected static function scan()
{
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(self::$dir_to_watch, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST,
\RecursiveIteratorIterator::CATCH_GET_CHILD
);

Controller::$storage::writeJournal($iterator, self::$extensions_to_watch, self::$exclude_dirs);
$storage = Controller::$storage;
$storage::writeJournal($iterator, self::$extensions_to_watch, self::$exclude_dirs);
}
}
16 changes: 11 additions & 5 deletions lib/CleantalkSP/Common/FSWatcher/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public static function setStorage($storage = 'file')
*/
public static function isMinIntervalPassed($interval)
{
$last_exec_time = Controller::$storage::getLastJournalTime();
$storage = Controller::$storage;
$last_exec_time = $storage::getLastJournalTime();
if (is_null($last_exec_time)) {
return true;
}
Expand All @@ -58,15 +59,18 @@ public static function isMinIntervalPassed($interval)
*
* @psalm-suppress PossiblyUnusedReturnValue
*/
public static function attachJS($buffer)
public static function attachJS($buffer, $file_to_get_md5 = null)
{
if (empty($file_to_get_md5)) {
$file_to_get_md5 = __FILE__;
}
$is_ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
$is_html = preg_match('/^\s*(<!doctype|<!DOCTYPE|<html)/i', $buffer) == 1;

if (!$is_ajax && $is_html) {
$path = __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'fswatcher.js';
$addition =
'<script type="text/javascript">var fswatcherToken = "' . md5((string)filemtime(__FILE__)) . '";</script>'
'<script type="text/javascript">var fswatcherToken = "' . md5((string)filemtime($file_to_get_md5)) . '";</script>'
. '<script type="text/javascript">var fswatcherWebsiteUrl = "' . get_home_url() . '";</script>'
. '<script type="text/javascript">' . file_get_contents($path) . '</script>';

Expand Down Expand Up @@ -129,7 +133,8 @@ public static function isCreateSnapshotRequest()
*/
public static function setAllJournalsAsCompleted()
{
Controller::$storage::setAllJournalsAsCompleted();
$storage = Controller::$storage;
$storage::setAllJournalsAsCompleted();
}

/**
Expand All @@ -139,6 +144,7 @@ public static function setAllJournalsAsCompleted()
*/
public static function getProcessingJournal()
{
return Controller::$storage::getProcessingJournal();
$storage = Controller::$storage;
return $storage::getProcessingJournal();
}
}
8 changes: 7 additions & 1 deletion lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private static function getJournalsPath()
* @param $journal string
* @return void
*/
private static function compressJournalGZ($journal)
protected static function compressJournalGZ($journal)
{
if ( ! function_exists('gzopen')) {
return;
Expand All @@ -179,4 +179,10 @@ private static function compressJournalGZ($journal)

unlink($journal);
}


public static function getAssetsPath()
{
return __DIR__ . '/../assets/fswatcher-logic.js';
}
}
2 changes: 2 additions & 0 deletions lib/CleantalkSP/Common/FSWatcher/View/Phrases.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ abstract class Phrases
{
abstract public function getTitle();
abstract public function getDescription();
abstract public function getSnapshotsPeriodDescription();
abstract public function getExtendedTabDescription();
abstract public function featureNotReady1();
abstract public function featureNotReady2();
abstract public function getCompareButtonText();
Expand Down
11 changes: 6 additions & 5 deletions lib/CleantalkSP/Common/FSWatcher/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public static function renderSelectors(Phrases $phrases)
$html .= '<div style="padding: 0 0 0 10px">';
$html .= '<p>' . self::getFSWatcherDescription($phrases) . '</p>';

$dates = Controller::$repository::getAvailableJournals();
$repository = Controller::$repository;
$dates = $repository::getAvailableJournals();

$html .= self::manualSnapshotButton($phrases);

Expand Down Expand Up @@ -72,7 +73,7 @@ public static function renderSelectors(Phrases $phrases)
return $html;
}

private static function renderSelectorOptions($dates)
protected static function renderSelectorOptions($dates)
{
Logger::log($dates);

Expand All @@ -85,7 +86,7 @@ private static function renderSelectorOptions($dates)
return $html;
}

private static function manualSnapshotButton(Phrases $phrases)
protected static function manualSnapshotButton(Phrases $phrases)
{
$html = '<div>';
$html .= '<button class="spbc-icon-download" id="fswatcher__create_snapshot" onclick="return false;">' . $phrases->getCreateSnapshotButtonText() . '</button>';
Expand All @@ -94,7 +95,7 @@ private static function manualSnapshotButton(Phrases $phrases)
return $html;
}

private static function renderTableTemplate(Phrases $phrases)
protected static function renderTableTemplate(Phrases $phrases)
{
$html = '<div class="ui-accordion" id="spbc--fs-watcher-table-div">';
$html .= '<p class="spbc_short_text_field" id="spbc--fs-watcher-table-handling-selects-info" style="display: none"></p>';
Expand Down Expand Up @@ -128,7 +129,7 @@ private static function renderTableTemplate(Phrases $phrases)
return $html;
}

private static function snapshotsAreReady($dates)
protected static function snapshotsAreReady($dates)
{
return is_array($dates) && count($dates) > 1;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/CleantalkSP/Common/FSWatcher/assets/fswatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ document.addEventListener('DOMContentLoaded', function() {
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send('fswatcher_token=' + fswatcherToken);
}
});
});
16 changes: 13 additions & 3 deletions lib/CleantalkSP/SpbctWP/Deactivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class Deactivator
//
'delete_backups',
'delete_fw_dir',
'delete_frontend_meta'
'delete_frontend_meta',
//
'delete_fs_watcher_journals'
)
),
'network_wide' => array(
Expand All @@ -63,7 +65,8 @@ class Deactivator
//
'delete_network_wide_options',
'delete_each_blog_data',
'delete_backups'
'delete_backups',
'delete_fs_watcher_journals'
)
),
'network_main_site' => array(
Expand All @@ -77,7 +80,8 @@ class Deactivator
'delete_blog_options',
'delete_blog_meta',
'delete_fw_dir',
'delete_frontend_meta'
'delete_frontend_meta',
'delete_fs_watcher_journals'
)
),
);
Expand Down Expand Up @@ -156,6 +160,12 @@ private static function runActions($call_instance_config)
remove_action('admin_bar_menu', 'spbc_admin__admin_bar__add_structure', 999);
}
break;
case 'delete_fs_watcher_journals':
$journals_path = \CleantalkSP\SpbctWP\FSWatcher\Storage\SpbctWpFSWFileStorage::getJournalsPath();
$journals_path = str_replace('data' . DIRECTORY_SEPARATOR, '', $journals_path);
if (is_dir($journals_path) && is_writable($journals_path)) {
\CleantalkSP\SpbctWP\Helpers\Data::removeDirectoryRecursively($journals_path);
}
}
self::logThis($action . ' ok');
}
Expand Down
40 changes: 40 additions & 0 deletions lib/CleantalkSP/SpbctWP/FSWatcher/Analyzer/SpbctWpFSWAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace CleantalkSP\SpbctWP\FSWatcher\Analyzer;

use CleantalkSP\SpbctWP\FSWatcher\SpbctWpFSWController;
use CleantalkSP\Common\FSWatcher\Logger;

class SpbctWpFSWAnalyzer extends \CleantalkSP\Common\FSWatcher\Analyzer\Analyzer
{
/**
* @return array|false
*/
public static function getCompareResult()
{
$first = filter_var($_POST['fswatcher__first_date'], FILTER_VALIDATE_INT);
$second = filter_var($_POST['fswatcher__second_date'], FILTER_VALIDATE_INT);

if ($first > $second) {
$tmp = $first;
$first = $second;
$second = $tmp;
}

$storage = SpbctWpFSWController::$storage;

$first_journal = $storage::getJournal($first);
$second_journal = $storage::getJournal($second);

if (!$first_journal || !$second_journal) {
return false;
}

if (SpbctWpFSWController::$debug) {
Logger::log('first journal ' . $first_journal);
Logger::log('second journal ' . $second_journal);
}

return parent::compare($first_journal, $second_journal);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace CleantalkSP\SpbctWP\FSWatcher\Repository;

use CleantalkSP\SpbctWP\FSWatcher\SpbctWpFSWController;
use CleantalkSP\Common\FSWatcher\Repository\Repository;

/**
* @psalm-suppress UnusedClass
*/
class SpbctWpFSWFileRepository implements Repository
{
/**
* @inheritDoc
*/
public static function getAvailableJournals()
{
$storage = SpbctWpFSWController::$storage;
return $storage::getAvailableJournals();
}
}
Loading

0 comments on commit a6c2da3

Please sign in to comment.