From 97604d5b1bad1806a16e4683679dce2e555b05ae Mon Sep 17 00:00:00 2001 From: svfcode Date: Sun, 5 Nov 2023 10:42:07 +0300 Subject: [PATCH 01/24] Gathering to csv. --- .../Common/FSWatcher/Controller.php | 60 +++ lib/CleantalkSP/Common/FSWatcher/Logger.php | 35 ++ .../FSWatcher/Repository/Repository.php | 23 ++ .../Common/FSWatcher/Scan/Scan.php | 52 +++ lib/CleantalkSP/Common/FSWatcher/Service.php | 57 +++ .../Common/FSWatcher/Storage/FileStorage.php | 47 +++ .../Common/FSWatcher/WatcherWorkStat.php | 16 + .../Common/FSWatcher/assets/fswatcher.js | 8 + .../Common/FSWatcher/logs/2023-11-04.log | 345 ++++++++++++++++++ security-malware-firewall.php | 8 + 10 files changed, 651 insertions(+) create mode 100644 lib/CleantalkSP/Common/FSWatcher/Controller.php create mode 100644 lib/CleantalkSP/Common/FSWatcher/Logger.php create mode 100644 lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php create mode 100644 lib/CleantalkSP/Common/FSWatcher/Scan/Scan.php create mode 100644 lib/CleantalkSP/Common/FSWatcher/Service.php create mode 100644 lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php create mode 100644 lib/CleantalkSP/Common/FSWatcher/WatcherWorkStat.php create mode 100644 lib/CleantalkSP/Common/FSWatcher/assets/fswatcher.js create mode 100644 lib/CleantalkSP/Common/FSWatcher/logs/2023-11-04.log diff --git a/lib/CleantalkSP/Common/FSWatcher/Controller.php b/lib/CleantalkSP/Common/FSWatcher/Controller.php new file mode 100644 index 000000000..304da3425 --- /dev/null +++ b/lib/CleantalkSP/Common/FSWatcher/Controller.php @@ -0,0 +1,60 @@ + $value) { + $message .= $key . ': ' . $value . PHP_EOL; + } + } + + error_log($message, 3, $path); + } +} \ No newline at end of file diff --git a/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php b/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php new file mode 100644 index 000000000..480d77b9c --- /dev/null +++ b/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php @@ -0,0 +1,23 @@ + $dir) { + if ($dir->isDir() && !in_array($dir->getFilename(), self::$exclude_dirs)) { + $iterator->next(); + } else { + if (in_array($dir->getExtension(), self::$extensions_to_watch)) { + fputcsv($fp, [$path]); + } + } + } + fclose($fp); + } +} \ No newline at end of file diff --git a/lib/CleantalkSP/Common/FSWatcher/Service.php b/lib/CleantalkSP/Common/FSWatcher/Service.php new file mode 100644 index 000000000..472037b2d --- /dev/null +++ b/lib/CleantalkSP/Common/FSWatcher/Service.php @@ -0,0 +1,57 @@ + $interval; + } + + public static function attach_js($buffer) + { + $is_ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'; + $is_html = preg_match('/^\s*(var fswatcherToken = "' . md5(filemtime(__FILE__)) . '";' + . ''; + + $buffer = preg_replace( + '/<\/body>(\s|<.*>)*<\/html>\s*$/i', + $addition.'', + $buffer, + 1 + ); + } + + return $buffer; + } + + public static function isRC() + { + Logger::log([ + 'isRC', + isset($_POST['fswatcher_token']) ? $_POST['fswatcher_token'] : 'no token', + md5(filemtime(__FILE__)), + + ]); + if (isset($_POST['fswatcher_token']) && $_POST['fswatcher_token'] == md5(filemtime(__FILE__))) { + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php b/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php new file mode 100644 index 000000000..5df5bebbe --- /dev/null +++ b/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php @@ -0,0 +1,47 @@ +execute(); unset($spbc_cron); +$fswatch_params = array( + 'dir_to_watch' => ABSPATH, + 'exclude_dirs' => array(), + 'extensions_to_watch' => array('php'), +); +FSWatcherController::work($fswatch_params); + if ( is_admin() || is_network_admin() ) { // Async loading for JavaScript add_filter('script_loader_tag', 'spbc_admin_add_script_attribute', 10, 3); From 93e1a134c68f50c646c1a9406e74be3616035638 Mon Sep 17 00:00:00 2001 From: svfcode Date: Sat, 11 Nov 2023 11:56:26 +0300 Subject: [PATCH 02/24] New. FSWatcher. Analyze WIP. --- inc/spbc-settings.php | 16 + lib/CleantalkSP/Common/CleantalkTools.php | 0 .../Common/FSWatcher/Analyzer/Analyzer.php | 89 +++++ .../Common/FSWatcher/Controller.php | 38 +- lib/CleantalkSP/Common/FSWatcher/Logger.php | 0 .../FSWatcher/Repository/FileRepository.php | 13 + .../FSWatcher/Repository/Repository.php | 19 +- .../Common/FSWatcher/Scan/Scan.php | 37 +- lib/CleantalkSP/Common/FSWatcher/Service.php | 59 ++- .../Common/FSWatcher/Storage/FileStorage.php | 109 +++++- .../Common/FSWatcher/Storage/Storage.php | 20 + .../Common/FSWatcher/Storage/data/index.php | 1 + .../Common/FSWatcher/View/View.php | 60 +++ .../Common/FSWatcher/WatcherWorkStat.php | 0 .../FSWatcher/assets/fswatcher-compare.js | 24 ++ .../Common/FSWatcher/assets/fswatcher.js | 0 .../Common/FSWatcher/logs/2023-11-04.log | 345 ------------------ 17 files changed, 417 insertions(+), 413 deletions(-) mode change 100644 => 100755 lib/CleantalkSP/Common/CleantalkTools.php create mode 100755 lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php mode change 100644 => 100755 lib/CleantalkSP/Common/FSWatcher/Controller.php mode change 100644 => 100755 lib/CleantalkSP/Common/FSWatcher/Logger.php create mode 100755 lib/CleantalkSP/Common/FSWatcher/Repository/FileRepository.php mode change 100644 => 100755 lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php mode change 100644 => 100755 lib/CleantalkSP/Common/FSWatcher/Scan/Scan.php mode change 100644 => 100755 lib/CleantalkSP/Common/FSWatcher/Service.php mode change 100644 => 100755 lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php create mode 100755 lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php create mode 100644 lib/CleantalkSP/Common/FSWatcher/Storage/data/index.php create mode 100755 lib/CleantalkSP/Common/FSWatcher/View/View.php mode change 100644 => 100755 lib/CleantalkSP/Common/FSWatcher/WatcherWorkStat.php create mode 100755 lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js mode change 100644 => 100755 lib/CleantalkSP/Common/FSWatcher/assets/fswatcher.js delete mode 100644 lib/CleantalkSP/Common/FSWatcher/logs/2023-11-04.log diff --git a/inc/spbc-settings.php b/inc/spbc-settings.php index 1402d24b3..4546a147f 100644 --- a/inc/spbc-settings.php +++ b/inc/spbc-settings.php @@ -900,6 +900,15 @@ function spbc_settings__register() 'ajax' => false, 'callback' => 'spbc_tab__summary', ), + // FSWatcher + 'fswatcher' => array( + 'type' => 'tab', + 'title' => __('FS journal', 'security-malware-firewall'), + 'icon' => 'spbc-icon-info', + 'class_prefix' => 'spbc', + 'ajax' => true, + 'callback' => 'spbc_tab__fswatcher', + ), // Debug 'debug' => array( 'type' => 'tab', @@ -1588,6 +1597,13 @@ function spbc_tab__summary() echo '
'; } +function spbc_tab__fswatcher() +{ + echo "
"; + echo \CleantalkSP\Common\FSWatcher\View\View::renderSelectors(); + echo '
'; +} + /** * Admin callback function - Displays current statistics */ diff --git a/lib/CleantalkSP/Common/CleantalkTools.php b/lib/CleantalkSP/Common/CleantalkTools.php old mode 100644 new mode 100755 diff --git a/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php new file mode 100755 index 000000000..5064b35f4 --- /dev/null +++ b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php @@ -0,0 +1,89 @@ + $second) { + $tmp = $first; + $first = $second; + $second = $tmp; + } + + $first_journal = Controller::$storage::getJournal($first); + $second_journal = Controller::$storage::getJournal($second); + + if (!$first_journal || !$second_journal) { + return ['error' => 'No such journal']; + } + + if (Controller::debug) { + Logger::log('first journal ' . $first_journal); + Logger::log('second journal ' . $second_journal); + } + + return self::compare($first_journal, $second_journal); + } + + private static function compare($first_journal, $second_journal) + { + $result = array( + 'added' => array(), + 'deleted' => array(), + 'changed' => array(), + ); + + $first_journal = self::uncompress($first_journal); + $second_journal = self::uncompress($second_journal); + + $fp_first = fopen($first_journal, 'r'); + $fp_second = fopen($second_journal, 'r'); + + // $flag = true; + while ($first = fgetcsv($fp_first)) { + // while ($flag) { + // $first = fgetcsv($fp_first); + if ($second = fgetcsv($fp_second)) { + if ($first[0] !== $second[0]) { + $result['deleted'][] = $first; + + // $flag = false; + continue; + } + + if ($first === $second && $first[1] !== $second[1]) { + $result['changed'][] = $first; + } + } else { + $result['deleted'][] = $first; + } + + // $flag = false; + } + + fclose($fp_first); + fclose($fp_second); + + return $result; + } + + private static function uncompress($file) + { + if (substr($file, -3) === '.gz') { + $content = gzopen($file, 'r'); + file_put_contents(substr($file, 0, -3), gzread($content, 1024 * 1024 * 10)); + gzclose($content); + $file = substr($file, 0, -3); + } + + return $file; + } +} \ No newline at end of file diff --git a/lib/CleantalkSP/Common/FSWatcher/Controller.php b/lib/CleantalkSP/Common/FSWatcher/Controller.php old mode 100644 new mode 100755 index 304da3425..1435bdb34 --- a/lib/CleantalkSP/Common/FSWatcher/Controller.php +++ b/lib/CleantalkSP/Common/FSWatcher/Controller.php @@ -2,7 +2,7 @@ namespace CleantalkSP\Common\FSWatcher; -use CleantalkSP\Common\FSWatcher\Repository\Repository; +use CleantalkSP\Common\FSWatcher\Analyzer\Analyzer; use CleantalkSP\Common\FSWatcher\Scan\Scan; class Controller @@ -12,7 +12,10 @@ class Controller const STATUS_STOPPED = 'stopped'; const STATUS_RUNNING = 'running'; - const EXECUTION_MIN_INTERVAL = 60; + const EXECUTION_MIN_INTERVAL = 6000; + + public static $storage; + public static $repository; private static $status = self::STATUS_STOPPED; @@ -22,35 +25,48 @@ public static function work($params) Logger::log('check remote call = ' . Service::isRC()); } - if (self::status() === self::STATUS_STOPPED && Service::isRC()) { - Scan::setParams($params); - self::run(); + Service::setStorage(isset($params['storage']) ? $params['storage'] : 'file'); + if (self::status() === self::STATUS_STOPPED && Service::isRC() && Service::isCompareRequest()) { + if (self::debug) { + Logger::log('run compare file system'); + } + echo json_encode(Analyzer::getCompareResult()); + die(); + } + + if (self::status() === self::STATUS_STOPPED && Service::isRC()) { + if (self::debug) { + Logger::log('run scan file system'); + } + self::run($params); die('OK'); } if (self::status() === self::STATUS_STOPPED && Service::isMinIntervalPassed(self::EXECUTION_MIN_INTERVAL)) { if (self::debug) { - Logger::log('attach js to make request'); + Logger::log('attach js to make remote request'); } ob_start(['CleantalkSP\Common\FSWatcher\Service', 'attach_js']); } } - public static function run() + private static function run($params) { self::$status = self::STATUS_RUNNING; - Scan::run(); + Scan::run($params); + self::stop(); } - public static function stop() + private static function stop() { self::$status = self::STATUS_STOPPED; + Service::setAllJournalsAsComplited(); } - public static function status() + private static function status() { - $is_exist = Repository::getProcessingJournal(); + $is_exist = Service::getProcessingJournal(); if (!is_null($is_exist)) { self::$status = self::STATUS_RUNNING; } diff --git a/lib/CleantalkSP/Common/FSWatcher/Logger.php b/lib/CleantalkSP/Common/FSWatcher/Logger.php old mode 100644 new mode 100755 diff --git a/lib/CleantalkSP/Common/FSWatcher/Repository/FileRepository.php b/lib/CleantalkSP/Common/FSWatcher/Repository/FileRepository.php new file mode 100755 index 000000000..b7115c0b0 --- /dev/null +++ b/lib/CleantalkSP/Common/FSWatcher/Repository/FileRepository.php @@ -0,0 +1,13 @@ + $dir) { - if ($dir->isDir() && !in_array($dir->getFilename(), self::$exclude_dirs)) { - $iterator->next(); - } else { - if (in_array($dir->getExtension(), self::$extensions_to_watch)) { - fputcsv($fp, [$path]); - } - } - } - fclose($fp); + Controller::$storage::writeJournal($iterator, self::$extensions_to_watch, self::$exclude_dirs); } } \ No newline at end of file diff --git a/lib/CleantalkSP/Common/FSWatcher/Service.php b/lib/CleantalkSP/Common/FSWatcher/Service.php old mode 100644 new mode 100755 index 472037b2d..d2a52b0c3 --- a/lib/CleantalkSP/Common/FSWatcher/Service.php +++ b/lib/CleantalkSP/Common/FSWatcher/Service.php @@ -2,19 +2,40 @@ namespace CleantalkSP\Common\FSWatcher; -use CleantalkSP\Common\FSWatcher\Repository\Repository; +use CleantalkSP\Common\FSWatcher\Repository\FileRepository; +use CleantalkSP\Common\FSWatcher\Storage\FileStorage; class Service { + public static function setStorage($storage = 'file') + { + switch ($storage) { + case 'file': + Controller::$storage = FileStorage::class; + Controller::$repository = FileRepository::class; + break; + // case 'customdb': + // Controller::$storage = CustomDBStorage::class; + // Controller::$repository = CustomDBStorage::class; + // break; + // case 'db': + // Controller::$storage = DBStorage::class; + // Controller::$repository = DBStorage::class; + // break; + default: + Controller::$storage = FileStorage::class; + Controller::$repository = FileRepository::class; + break; + } + } + public static function isMinIntervalPassed($interval) { - $journal = Repository::getLastJournal(); - if (is_null($journal)) { + $last_exec_time = Controller::$storage::getLastJournalTime(); + if (is_null($last_exec_time)) { return true; } - $last_exec_time = (int)explode('__', basename($journal))[0]; - return (time() - $last_exec_time) > $interval; } @@ -42,16 +63,32 @@ public static function attach_js($buffer) public static function isRC() { - Logger::log([ - 'isRC', - isset($_POST['fswatcher_token']) ? $_POST['fswatcher_token'] : 'no token', - md5(filemtime(__FILE__)), - - ]); if (isset($_POST['fswatcher_token']) && $_POST['fswatcher_token'] == md5(filemtime(__FILE__))) { return true; } return false; } + + public static function isCompareRequest() + { + if (isset($_POST['fswatcher_compare']) && $_POST['fswatcher_compare'] == true && + isset($_POST['fswatcher__first_date']) && filter_var($_POST['fswatcher__first_date'], FILTER_VALIDATE_INT) !== false && + isset($_POST['fswatcher__second_date']) && filter_var($_POST['fswatcher__second_date'], FILTER_VALIDATE_INT) !== false + ) { + return true; + } + + return false; + } + + public static function setAllJournalsAsComplited() + { + Controller::$storage::setAllJournalsAsComplited(); + } + + public static function getProcessingJournal() + { + Controller::$storage::getProcessingJournal(); + } } \ No newline at end of file diff --git a/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php b/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php old mode 100644 new mode 100755 index 5df5bebbe..a08f05c65 --- a/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php +++ b/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php @@ -2,12 +2,15 @@ namespace CleantalkSP\Common\FSWatcher\Storage; -class FileStorage +class FileStorage implements Storage { + const STATUS_PROCESSING = '__processing'; + const STATUS_COMPLITED = '__complited'; + public static function makeProcessingJournal() { $journals_path = self::getJournalsPath(); - $path = $journals_path . DIRECTORY_SEPARATOR . time() . '__processing' . '.csv'; + $path = $journals_path . DIRECTORY_SEPARATOR . time() . self::STATUS_PROCESSING . '.csv'; file_put_contents($path, ''); @@ -16,7 +19,7 @@ public static function makeProcessingJournal() public static function getProcessingJournal() { - $is_processing = glob(self::getJournalsPath() . '*__processing.csv'); + $is_processing = glob(self::getJournalsPath() . '*' . self::STATUS_PROCESSING . '.csv'); if ( ! empty($is_processing)) { return $is_processing[0]; } @@ -24,16 +27,97 @@ public static function getProcessingJournal() return null; } - public static function getLastJournal() + public static function getLastJournalTime() { - $last_journal = glob(self::getJournalsPath() . '*__complited.csv'); + $pattern = self::getJournalsPath() . '*' . self::STATUS_COMPLITED . '.csv'; + if (function_exists('gzopen')) { + $pattern = self::getJournalsPath() . '*' . self::STATUS_COMPLITED . '.csv.gz'; + } + + $last_journal = glob($pattern); if ( ! empty($last_journal)) { - return $last_journal[count($last_journal) - 1]; + $journal = $last_journal[count($last_journal) - 1]; + return (int)explode('__', basename($journal))[0]; } return null; } + public static function setAllJournalsAsComplited() + { + $journals = glob(self::getJournalsPath() . '*.csv'); + foreach ($journals as $journal) { + $new_name = str_replace(self::STATUS_PROCESSING, self::STATUS_COMPLITED, $journal); + rename($journal, $new_name); + self::compressJournalGZ($new_name); + } + } + + public static function getAvailableJournals() + { + $dir = self::getJournalsPath(); + + if ( ! is_dir($dir)) { + return []; + } + + $pattern = $dir . '*.csv'; + if (function_exists('gzopen')) { + $pattern = $dir . '*.csv.gz'; + } + + $dates = []; + $journals = glob($pattern); + foreach ($journals as $journal) { + $dates[] = (int)explode('__', basename($journal))[0]; + } + + return $dates; + } + + public static function writeJournal($iterator, $extensions_to_watch, $exclude_dirs) + { + $journal = self::getProcessingJournal(); + $fp = fopen($journal, 'w'); + foreach ($iterator as $path => $dir) { + if ($dir->isDir() && !in_array($dir->getFilename(), $exclude_dirs)) { + $iterator->next(); + } else { + if (in_array($dir->getExtension(), $extensions_to_watch)) { + $mtime = @filemtime($path); + if ( empty($mtime) ) { + clearstatcache($path); + $mtime = @filemtime($path); + if ( empty($mtime) ) { + $mtime = @filectime($path); + if ( empty($mtime) ) { + $mtime = time(); + } + } + } + + fputcsv($fp, [$path, $mtime]); + } + } + } + fclose($fp); + } + + public static function getJournal($journal) + { + $journals_path = self::getJournalsPath(); + $path = $journals_path . DIRECTORY_SEPARATOR . $journal . self::STATUS_COMPLITED . '.csv'; + if (function_exists('gzopen')) { + $path = $journals_path . DIRECTORY_SEPARATOR . $journal . self::STATUS_COMPLITED . '.csv.gz'; + } + + if ( ! file_exists($path)) { + return null; + } + + return $path; + } + private static function getJournalsPath() { $dir_name = __DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR; @@ -44,4 +128,17 @@ private static function getJournalsPath() return $dir_name; } + + private static function compressJournalGZ($journal) + { + if ( ! function_exists('gzopen')) { + return; + } + + $gz = gzopen($journal . '.gz', 'w9'); + gzwrite($gz, file_get_contents($journal)); + gzclose($gz); + + unlink($journal); + } } \ No newline at end of file diff --git a/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php b/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php new file mode 100755 index 000000000..1f6232d35 --- /dev/null +++ b/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php @@ -0,0 +1,20 @@ +'; + + $html .= ''; + $html .= ''; + + $html .= '
'; + $html .= '
'; + + $html .= ''; + $html .= ''; + + $html .= ''; + + $html .= '
'; + $html .= '
'; + + $html .= '
'; + $html .= ''; + $html .= ''; + $html .= '
'; + + $html .= ''; + + return $html; + } + + private static function renderSelectorOptions() + { + $dates = Controller::$repository::getAvailableJournals(); + + Logger::log($dates); + + $html = ''; + foreach ($dates as $date) { + $formated_date = date('Y-m-d H:i:s', $date); + $html .= ''; + } + + return $html; + } +} \ No newline at end of file diff --git a/lib/CleantalkSP/Common/FSWatcher/WatcherWorkStat.php b/lib/CleantalkSP/Common/FSWatcher/WatcherWorkStat.php old mode 100644 new mode 100755 diff --git a/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js b/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js new file mode 100755 index 000000000..c3de1d7f6 --- /dev/null +++ b/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js @@ -0,0 +1,24 @@ +function fswatcherCompare(e) { + e.preventDefault(); + console.log("fswatcherCompare"); + console.log(e); + + if (typeof fswatcherToken !== 'undefined') { + let xhr = new XMLHttpRequest(); + xhr.open("POST", window.location.protocol + '//' + window.location.host); + xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + let data = [ + 'fswatcher_token=' + fswatcherToken, + 'fswatcher_compare=1', + 'fswatcher__first_date=' + document.getElementById('fswatcher__first_date').value, + 'fswatcher__second_date=' + document.getElementById('fswatcher__second_date').value, + ]; + xhr.send(data.join('&')); + } + + return false; +} + +document.getElementById('fswatcher__compare').addEventListener('click', fswatcherCompare); + + diff --git a/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher.js b/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher.js old mode 100644 new mode 100755 diff --git a/lib/CleantalkSP/Common/FSWatcher/logs/2023-11-04.log b/lib/CleantalkSP/Common/FSWatcher/logs/2023-11-04.log deleted file mode 100644 index 778d73e82..000000000 --- a/lib/CleantalkSP/Common/FSWatcher/logs/2023-11-04.log +++ /dev/null @@ -1,345 +0,0 @@ -[12:38:57] CleantalkSP\Common\FSWatcher\Controller::work -0: [] -1: "stopped" -2: 1 -[12:38:57] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:38:57] CleantalkSP\Common\FSWatcher\Controller::work -0: ob_start -1: stopped -2: 1 -[12:38:57] CleantalkSP\Common\FSWatcher\Controller::work -0: {"fswatcher_token":"c87e007ea0c38480c813899ce0a0ec0d"} -1: "stopped" -2: 1 -[12:38:57] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: 1 -2: c87e007ea0c38480c813899ce0a0ec0d -3: c87e007ea0c38480c813899ce0a0ec0d -[12:38:57] CleantalkSP\Common\FSWatcher\Scan\Scan::run -public static function run -[12:38:58] CleantalkSP\Common\FSWatcher\Controller::work -0: [] -1: "running" -2: 1 -[12:38:58] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:39:16] CleantalkSP\Common\FSWatcher\Controller::work -0: [] -1: "running" -2: 1 -[12:39:16] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:39:17] CleantalkSP\Common\FSWatcher\Controller::work -0: [] -1: "running" -2: 1 -[12:39:17] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:39:18] CleantalkSP\Common\FSWatcher\Controller::work -0: [] -1: "running" -2: 1 -[12:39:18] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:39:19] CleantalkSP\Common\FSWatcher\Controller::work -0: [] -1: "running" -2: 1 -[12:39:19] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:39:20] CleantalkSP\Common\FSWatcher\Controller::work -0: [] -1: "running" -2: 1 -[12:39:20] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:39:20] CleantalkSP\Common\FSWatcher\Controller::work -0: [] -1: "running" -2: 1 -[12:39:20] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:55:45] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:55:45] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[12:55:45] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:55:45] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:55:45] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[12:55:45] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:55:46] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:55:46] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[12:55:46] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:56:53] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:56:53] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[12:56:53] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:56:53] CleantalkSP\Common\FSWatcher\Controller::work -attach js to make request -[12:56:53] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: 1 -2: c87e007ea0c38480c813899ce0a0ec0d -3: c87e007ea0c38480c813899ce0a0ec0d -[12:56:53] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = 1 -[12:56:53] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: 1 -2: c87e007ea0c38480c813899ce0a0ec0d -3: c87e007ea0c38480c813899ce0a0ec0d -[12:56:53] CleantalkSP\Common\FSWatcher\Scan\Scan::run -create processing journal /var/www/blog.loc/wp-content/plugins/security-malware-firewall/lib/CleantalkSP/Common/FSWatcher/Storage/data//1699102613__processing.csv -[12:56:53] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:56:53] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[12:56:53] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:58:55] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:58:55] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[12:58:55] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:58:55] CleantalkSP\Common\FSWatcher\Controller::work -attach js to make request -[12:58:56] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: 1 -2: c87e007ea0c38480c813899ce0a0ec0d -3: c87e007ea0c38480c813899ce0a0ec0d -[12:58:56] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = 1 -[12:58:56] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: 1 -2: c87e007ea0c38480c813899ce0a0ec0d -3: c87e007ea0c38480c813899ce0a0ec0d -[12:58:56] CleantalkSP\Common\FSWatcher\Scan\Scan::run -create processing journal /var/www/blog.loc/wp-content/plugins/security-malware-firewall/lib/CleantalkSP/Common/FSWatcher/Storage/data//1699102736__processing.csv -[12:58:56] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[12:58:56] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[12:58:56] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: -2: -3: c87e007ea0c38480c813899ce0a0ec0d -[13:00:02] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:00:02] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[13:00:02] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:00:02] CleantalkSP\Common\FSWatcher\Controller::work -attach js to make request -[13:00:03] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: 7eb07ecd344afdbd37cec9589b12b3ba -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:00:03] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = 1 -[13:00:03] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: 7eb07ecd344afdbd37cec9589b12b3ba -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:00:03] CleantalkSP\Common\FSWatcher\Scan\Scan::run -create processing journal /var/www/blog.loc/wp-content/plugins/security-malware-firewall/lib/CleantalkSP/Common/FSWatcher/Storage/data//1699102803__processing.csv -[13:00:03] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:00:03] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[13:00:03] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:00:40] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:00:40] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[13:00:40] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:00:40] CleantalkSP\Common\FSWatcher\Controller::work -attach js to make request -[13:00:41] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: 7eb07ecd344afdbd37cec9589b12b3ba -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:00:41] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = 1 -[13:00:41] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: 7eb07ecd344afdbd37cec9589b12b3ba -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:00:41] CleantalkSP\Common\FSWatcher\Scan\Scan::run -create processing journal /var/www/blog.loc/wp-content/plugins/security-malware-firewall/lib/CleantalkSP/Common/FSWatcher/Storage/data//1699102841__processing.csv -[13:00:41] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:00:41] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[13:00:41] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:02:38] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:02:38] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[13:02:38] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:02:38] CleantalkSP\Common\FSWatcher\Controller::work -attach js to make request -[13:02:38] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: 7eb07ecd344afdbd37cec9589b12b3ba -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:02:38] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = 1 -[13:02:38] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: 7eb07ecd344afdbd37cec9589b12b3ba -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:02:38] CleantalkSP\Common\FSWatcher\Scan\Scan::run -create processing journal /var/www/blog.loc/wp-content/plugins/security-malware-firewall/lib/CleantalkSP/Common/FSWatcher/Storage/data//1699102958__processing.csv -[13:02:39] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:02:39] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[13:02:39] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:03:59] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:03:59] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[13:03:59] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:04:00] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:04:00] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[13:04:00] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:04:02] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:04:02] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[13:04:02] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:04:02] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba -[13:04:02] CleantalkSP\Common\FSWatcher\Controller::work -check remote call = -[13:04:02] CleantalkSP\Common\FSWatcher\Service::isRC -0: isRC -1: no token -2: 7eb07ecd344afdbd37cec9589b12b3ba From 437d890d4c3766bf46fefed7b0dc6afd28923276 Mon Sep 17 00:00:00 2001 From: alexandergull Date: Tue, 14 Nov 2023 14:48:53 +0500 Subject: [PATCH 03/24] Fix. Codestyle. --- .../Common/FSWatcher/Analyzer/Analyzer.php | 4 ++-- .../Common/FSWatcher/Controller.php | 4 ++-- lib/CleantalkSP/Common/FSWatcher/Service.php | 6 +++--- .../Common/FSWatcher/Storage/FileStorage.php | 18 +++++++++--------- .../Common/FSWatcher/Storage/Storage.php | 4 ++-- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php index 5064b35f4..dcd407612 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php +++ b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php @@ -54,7 +54,7 @@ private static function compare($first_journal, $second_journal) if ($second = fgetcsv($fp_second)) { if ($first[0] !== $second[0]) { $result['deleted'][] = $first; - + // $flag = false; continue; } @@ -86,4 +86,4 @@ private static function uncompress($file) return $file; } -} \ No newline at end of file +} diff --git a/lib/CleantalkSP/Common/FSWatcher/Controller.php b/lib/CleantalkSP/Common/FSWatcher/Controller.php index 1435bdb34..b57fb0583 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Controller.php +++ b/lib/CleantalkSP/Common/FSWatcher/Controller.php @@ -61,7 +61,7 @@ private static function run($params) private static function stop() { self::$status = self::STATUS_STOPPED; - Service::setAllJournalsAsComplited(); + Service::setAllJournalsAsCompleted(); } private static function status() @@ -73,4 +73,4 @@ private static function status() return self::$status; } -} \ No newline at end of file +} diff --git a/lib/CleantalkSP/Common/FSWatcher/Service.php b/lib/CleantalkSP/Common/FSWatcher/Service.php index d2a52b0c3..2b671bceb 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Service.php +++ b/lib/CleantalkSP/Common/FSWatcher/Service.php @@ -82,13 +82,13 @@ public static function isCompareRequest() return false; } - public static function setAllJournalsAsComplited() + public static function setAllJournalsAsCompleted() { - Controller::$storage::setAllJournalsAsComplited(); + Controller::$storage::setAllJournalsAsCompleted(); } public static function getProcessingJournal() { Controller::$storage::getProcessingJournal(); } -} \ No newline at end of file +} diff --git a/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php b/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php index a08f05c65..91f5c6c15 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php +++ b/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php @@ -5,7 +5,7 @@ class FileStorage implements Storage { const STATUS_PROCESSING = '__processing'; - const STATUS_COMPLITED = '__complited'; + const STATUS_COMPLETED = '__completed'; public static function makeProcessingJournal() { @@ -13,7 +13,7 @@ public static function makeProcessingJournal() $path = $journals_path . DIRECTORY_SEPARATOR . time() . self::STATUS_PROCESSING . '.csv'; file_put_contents($path, ''); - + return $path; } @@ -29,9 +29,9 @@ public static function getProcessingJournal() public static function getLastJournalTime() { - $pattern = self::getJournalsPath() . '*' . self::STATUS_COMPLITED . '.csv'; + $pattern = self::getJournalsPath() . '*' . self::STATUS_COMPLETED . '.csv'; if (function_exists('gzopen')) { - $pattern = self::getJournalsPath() . '*' . self::STATUS_COMPLITED . '.csv.gz'; + $pattern = self::getJournalsPath() . '*' . self::STATUS_COMPLETED . '.csv.gz'; } $last_journal = glob($pattern); @@ -43,11 +43,11 @@ public static function getLastJournalTime() return null; } - public static function setAllJournalsAsComplited() + public static function setAllJournalsAsCompleted() { $journals = glob(self::getJournalsPath() . '*.csv'); foreach ($journals as $journal) { - $new_name = str_replace(self::STATUS_PROCESSING, self::STATUS_COMPLITED, $journal); + $new_name = str_replace(self::STATUS_PROCESSING, self::STATUS_COMPLETED, $journal); rename($journal, $new_name); self::compressJournalGZ($new_name); } @@ -106,9 +106,9 @@ public static function writeJournal($iterator, $extensions_to_watch, $exclude_di public static function getJournal($journal) { $journals_path = self::getJournalsPath(); - $path = $journals_path . DIRECTORY_SEPARATOR . $journal . self::STATUS_COMPLITED . '.csv'; + $path = $journals_path . DIRECTORY_SEPARATOR . $journal . self::STATUS_COMPLETED . '.csv'; if (function_exists('gzopen')) { - $path = $journals_path . DIRECTORY_SEPARATOR . $journal . self::STATUS_COMPLITED . '.csv.gz'; + $path = $journals_path . DIRECTORY_SEPARATOR . $journal . self::STATUS_COMPLETED . '.csv.gz'; } if ( ! file_exists($path)) { @@ -141,4 +141,4 @@ private static function compressJournalGZ($journal) unlink($journal); } -} \ No newline at end of file +} diff --git a/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php b/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php index 1f6232d35..125b805cc 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php +++ b/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php @@ -10,11 +10,11 @@ public static function getLastJournalTime(); public static function makeProcessingJournal(); - public static function setAllJournalsAsComplited(); + public static function setAllJournalsAsCompleted(); public static function writeJournal($iterator, $extensions_to_watch, $exclude_dirs); public static function getAvailableJournals(); public static function getJournal($journal); -} \ No newline at end of file +} From 238cb0a16d55d941de93b22b553a3dec851c7f0a Mon Sep 17 00:00:00 2001 From: alexandergull Date: Tue, 14 Nov 2023 14:50:58 +0500 Subject: [PATCH 04/24] Fix. Logger. --- lib/CleantalkSP/Common/FSWatcher/Logger.php | 39 +++++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/CleantalkSP/Common/FSWatcher/Logger.php b/lib/CleantalkSP/Common/FSWatcher/Logger.php index 55c83a2d9..47c10ddbd 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Logger.php +++ b/lib/CleantalkSP/Common/FSWatcher/Logger.php @@ -4,20 +4,45 @@ class Logger { - private static function getPath() + private static $logger_dir = __DIR__ . DIRECTORY_SEPARATOR . 'logs'; + + private static function getCurrentDayLogPath() + { + $path = self::getLoggerDir(); + return $path + ? $path . DIRECTORY_SEPARATOR . date('Y-m-d') . '.log' + : false; + } + + private static function setLoggerDir() { - return __DIR__ . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . '.log'; + return mkdir(self::$logger_dir); + } + + private static function getLoggerDir() + { + if ( !is_dir(self::$logger_dir) ) { + if ( !self::setLoggerDir() ) { + return false; + } + } + return self::$logger_dir; } public static function log($msg) { - $path = self::getPath(); + $current_day_log_path = self::getCurrentDayLogPath(); + + if ( !$current_day_log_path ) { + error_log('Cant write log.'); + return; + } $message = '[' . date('H:i:s') . ']' . ' '; - $message .= debug_backtrace()[1]['class'] + $message .= debug_backtrace()[1]['class'] . debug_backtrace()[1]['type'] - . debug_backtrace()[1]['function'] + . debug_backtrace()[1]['function'] . PHP_EOL; if (is_string($msg)) { @@ -30,6 +55,6 @@ public static function log($msg) } } - error_log($message, 3, $path); + error_log($message, 3, $current_day_log_path); } -} \ No newline at end of file +} From 25c3d804915cc8345330fb369b95219fbaa47bef Mon Sep 17 00:00:00 2001 From: alexandergull Date: Tue, 14 Nov 2023 15:47:07 +0500 Subject: [PATCH 05/24] New. FSW table template. --- .../Common/FSWatcher/Controller.php | 2 +- .../Common/FSWatcher/View/View.php | 38 ++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/CleantalkSP/Common/FSWatcher/Controller.php b/lib/CleantalkSP/Common/FSWatcher/Controller.php index b57fb0583..4b35adbb6 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Controller.php +++ b/lib/CleantalkSP/Common/FSWatcher/Controller.php @@ -22,7 +22,7 @@ class Controller public static function work($params) { if (self::debug) { - Logger::log('check remote call = ' . Service::isRC()); + Logger::log('check remote call = ' . (int)Service::isRC()); } Service::setStorage(isset($params['storage']) ? $params['storage'] : 'file'); diff --git a/lib/CleantalkSP/Common/FSWatcher/View/View.php b/lib/CleantalkSP/Common/FSWatcher/View/View.php index 0e255ebb3..a0cc64329 100755 --- a/lib/CleantalkSP/Common/FSWatcher/View/View.php +++ b/lib/CleantalkSP/Common/FSWatcher/View/View.php @@ -39,6 +39,9 @@ public static function renderSelectors() $html .= 'var fswatcherToken = "' . md5(filemtime($path)) . '";'; $html .= file_get_contents(__DIR__ . '/../assets/fswatcher-compare.js'); $html .= ''; + $html .= '
'; + + $html .= self::renderTableTemplate(); return $html; } @@ -57,4 +60,37 @@ private static function renderSelectorOptions() return $html; } -} \ No newline at end of file + + private static function renderTableTemplate() + { + $html = '
'; + $html .= ''; + + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + + $html .= '
'; + $html .= 'Path'; + $html .= ''; + $html .= 'Event'; + $html .= ''; + $html .= 'Changed on date'; + $html .= '
'; + $html .= 'No logs compared yet.'; + $html .= '
'; + $html .= '
'; + + return $html; + } +} From 41305694652bb5eeec1783af7fd51c7e960e8462 Mon Sep 17 00:00:00 2001 From: alexandergull Date: Wed, 15 Nov 2023 08:57:51 +0500 Subject: [PATCH 06/24] Fix. Compare. Delete unzipped files. --- lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php index dcd407612..75d1b453a 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php +++ b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php @@ -72,6 +72,9 @@ private static function compare($first_journal, $second_journal) fclose($fp_first); fclose($fp_second); + @unlink($first_journal); + @unlink($second_journal); + return $result; } From 7cae9d87ad536b7d11fe0a0a9b571944d4eeba30 Mon Sep 17 00:00:00 2001 From: alexandergull Date: Thu, 16 Nov 2023 20:14:26 +0500 Subject: [PATCH 07/24] New. Settings. FS Watcher on/off. Enabled by defaults. --- inc/spbc-settings.php | 19 +++++++++++++------ .../Common/FSWatcher/View/View.php | 8 ++++++++ lib/CleantalkSP/SpbctWP/State.php | 1 + security-malware-firewall.php | 14 ++++++++------ 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/inc/spbc-settings.php b/inc/spbc-settings.php index 4546a147f..f05519c30 100644 --- a/inc/spbc-settings.php +++ b/inc/spbc-settings.php @@ -1,5 +1,6 @@ __('Important File Monitoring', 'security-malware-firewall'), 'description' => __('Monitoring of the individual most important files of the site.', 'security-malware-firewall'), ), + 'scanner__fs_watcher' => array( + 'type' => 'field', + 'title' => __('File System Journal', 'security-malware-firewall'), + 'description' => View::getFSWatcherDescription(), + ), ), ), @@ -908,6 +914,7 @@ function spbc_settings__register() 'class_prefix' => 'spbc', 'ajax' => true, 'callback' => 'spbc_tab__fswatcher', + 'display' => $spbc->settings['scanner__fs_watcher'] ), // Debug 'debug' => array( @@ -1948,13 +1955,13 @@ function spbc_field_security_logs__prepare_data(&$table) foreach ($table->rows as $row) { $ip = IP::reduceIPv6($row->auth_ip); - $allow_layout = '' + $allow_layout = '' . esc_html__('Allow', 'security-malware-firewall') . ''; - $ban_layout = '' + $ban_layout = '' . esc_html__('Ban', 'security-malware-firewall') . ''; $user = get_user_by('login', $row->user_login); diff --git a/lib/CleantalkSP/Common/FSWatcher/View/View.php b/lib/CleantalkSP/Common/FSWatcher/View/View.php index a0cc64329..25db0fd93 100755 --- a/lib/CleantalkSP/Common/FSWatcher/View/View.php +++ b/lib/CleantalkSP/Common/FSWatcher/View/View.php @@ -7,6 +7,14 @@ class View { + public static function getFSWatcherDescription() + { + $description = __('This feature runs filesystem snapshots every ', 'security-malware-firewall'); + $description .= Controller::EXECUTION_MIN_INTERVAL; + $description .= __(' seconds and allows you to control which of your site files has been changed between selected dates.', 'security-malware-firewall'); + return $description; + } + public static function renderSelectors() { $html = '
'; diff --git a/lib/CleantalkSP/SpbctWP/State.php b/lib/CleantalkSP/SpbctWP/State.php index e8cef86fe..ced682c42 100644 --- a/lib/CleantalkSP/SpbctWP/State.php +++ b/lib/CleantalkSP/SpbctWP/State.php @@ -74,6 +74,7 @@ class State extends \CleantalkSP\Common\State 'scanner__list_unknown__older_than' => 10, // day 'scanner__auto_start__set_period' => 86400, 'scanner__file_monitoring' => 0, + 'scanner__fs_watcher' => 1, // Frontend scanner 'scanner__frontend_analysis' => 1, diff --git a/security-malware-firewall.php b/security-malware-firewall.php index b7f36fbc2..c51bab2a4 100644 --- a/security-malware-firewall.php +++ b/security-malware-firewall.php @@ -290,12 +290,14 @@ function spbc_change_author_name($link, $_author_id, $_author_nicename) ! SpbcRemoteCalls::check() && $spbc_cron->execute(); unset($spbc_cron); -$fswatch_params = array( - 'dir_to_watch' => ABSPATH, - 'exclude_dirs' => array(), - 'extensions_to_watch' => array('php'), -); -FSWatcherController::work($fswatch_params); +if ($spbc->settings['scanner__fs_watcher']) { + $fswatch_params = array( + 'dir_to_watch' => ABSPATH, + 'exclude_dirs' => array(), + 'extensions_to_watch' => array('php'), + ); + FSWatcherController::work($fswatch_params); +} if ( is_admin() || is_network_admin() ) { // Async loading for JavaScript From c82fb7ff6a987fdaf7d6c63932f4ae58236a606f Mon Sep 17 00:00:00 2001 From: alexandergull Date: Thu, 16 Nov 2023 20:15:46 +0500 Subject: [PATCH 08/24] Ref. FWWatcher. Lot of improvements. --- .../Common/FSWatcher/Analyzer/Analyzer.php | 78 ++++--- .../Common/FSWatcher/Controller.php | 8 +- .../Common/FSWatcher/View/View.php | 32 ++- .../FSWatcher/assets/fswatcher-compare.js | 206 +++++++++++++++++- 4 files changed, 282 insertions(+), 42 deletions(-) diff --git a/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php index 75d1b453a..1a55f7134 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php +++ b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php @@ -22,7 +22,7 @@ public static function getCompareResult() $second_journal = Controller::$storage::getJournal($second); if (!$first_journal || !$second_journal) { - return ['error' => 'No such journal']; + return false; } if (Controller::debug) { @@ -41,49 +41,77 @@ private static function compare($first_journal, $second_journal) 'changed' => array(), ); + //return no diff if csv names is equal + if ( $first_journal === $second_journal) { + return $result; + } + + //return no diff if md5 sums is equal + if (md5(@file_get_contents($first_journal)) === md5(@file_get_contents($second_journal))) { + return $result; + } + $first_journal = self::uncompress($first_journal); $second_journal = self::uncompress($second_journal); - $fp_first = fopen($first_journal, 'r'); - $fp_second = fopen($second_journal, 'r'); + if ( !$first_journal || !$second_journal) { + return false; + } - // $flag = true; - while ($first = fgetcsv($fp_first)) { - // while ($flag) { - // $first = fgetcsv($fp_first); - if ($second = fgetcsv($fp_second)) { - if ($first[0] !== $second[0]) { - $result['deleted'][] = $first; + $first_array = []; + $second_array = []; - // $flag = false; - continue; - } + try { + $fp_first = fopen($first_journal, 'r'); + while ($first = fgetcsv($fp_first)) { + $first_array[$first[0]] = $first[1]; + } + fclose($fp_first); + @unlink($first_journal); + + $fp_second = fopen($second_journal, 'r'); + while ($second = fgetcsv($fp_second)) { + $second_array[$second[0]] = $second[1]; + } + fclose($fp_second); + @unlink($second_journal); - if ($first === $second && $first[1] !== $second[1]) { - $result['changed'][] = $first; + foreach ($first_array as $path => $time) { + if ((isset($second_array[$path]) && $time !== $second_array[$path])) { + $result['changed'][] = [$path, $second_array[$path]]; } - } else { - $result['deleted'][] = $first; } - // $flag = false; - } + $keys_differ = array_merge(array_diff_key($first_array, $second_array), array_diff_key($second_array, $first_array)); - fclose($fp_first); - fclose($fp_second); + foreach ($keys_differ as $path => $time) { + if ( in_array($path, array_keys($first_array)) && !in_array($path, array_keys($second_array))) { + $result['deleted'][] = [$path,$time]; + } - @unlink($first_journal); - @unlink($second_journal); + if ( !in_array($path, array_keys($first_array)) && in_array($path, array_keys($second_array))) { + $result['added'][] = [$path,$time]; + } + } - return $result; + return $result; + } catch (\Exception $e) { + return false; + } } private static function uncompress($file) { if (substr($file, -3) === '.gz') { $content = gzopen($file, 'r'); - file_put_contents(substr($file, 0, -3), gzread($content, 1024 * 1024 * 10)); + if (false === $content) { + return false; + } + $write_result = @file_put_contents(substr($file, 0, -3), gzread($content, 1024 * 1024 * 10)); gzclose($content); + if (false === $write_result) { + return false; + } $file = substr($file, 0, -3); } diff --git a/lib/CleantalkSP/Common/FSWatcher/Controller.php b/lib/CleantalkSP/Common/FSWatcher/Controller.php index 4b35adbb6..c48dcd1d3 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Controller.php +++ b/lib/CleantalkSP/Common/FSWatcher/Controller.php @@ -31,7 +31,13 @@ public static function work($params) if (self::debug) { Logger::log('run compare file system'); } - echo json_encode(Analyzer::getCompareResult()); + $compare_result = Analyzer::getCompareResult(); + if (false === $compare_result) { + Logger::log('Can not compare logs'); + echo json_encode(array('error' => 'Can not compare logs')); + } else { + echo json_encode($compare_result); + } die(); } diff --git a/lib/CleantalkSP/Common/FSWatcher/View/View.php b/lib/CleantalkSP/Common/FSWatcher/View/View.php index 25db0fd93..634f2c681 100755 --- a/lib/CleantalkSP/Common/FSWatcher/View/View.php +++ b/lib/CleantalkSP/Common/FSWatcher/View/View.php @@ -17,29 +17,34 @@ public static function getFSWatcherDescription() public static function renderSelectors() { - $html = '
'; + $html = '
'; + $html .= '
'; + $html .= '

File System Journal

'; + $html .= '
'; - $html .= ''; + $html .= '
'; + $html .= '

' . self::getFSWatcherDescription() . '

'; + $html .= '

' . __('To run comparison select dates which you want to compare and click the "Compare" button.', 'security-malware-firewall') . '

'; + $html .= ''; $html .= ''; + $html .= '
'; + $html .= '
'; - $html .= '
'; - $html .= '
'; - $html .= ''; + $html .= ''; $html .= ''; + $html .= '
'; + $html .= '
'; + $html .= '
'; + $html .= ''; $html .= '
'; + $html .= '
'; - $html .= '
'; - $html .= '
'; - - $html .= '
'; - $html .= ''; - $html .= ''; $html .= '
'; $html .= ''; + $html .= '
'; + $html .= '
'; $html .= self::renderTableTemplate(); @@ -71,7 +78,8 @@ private static function renderSelectorOptions() private static function renderTableTemplate() { - $html = '
'; + $html = '
'; + $html .= ''; $html .= ''; $html .= ''; diff --git a/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js b/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js index c3de1d7f6..0108b8a30 100755 --- a/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js +++ b/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js @@ -1,7 +1,19 @@ +let noChangesDetected = true; +let first_select = document.getElementById('fswatcher__first_date'); +let second_select = document.getElementById('fswatcher__second_date'); +const availableDataSetNames = ['added','changed','deleted'] + + function fswatcherCompare(e) { e.preventDefault(); - console.log("fswatcherCompare"); - console.log(e); + + let first_date = document.getElementById('fswatcher__first_date').value; + let second_date = document.getElementById('fswatcher__second_date').value; + + console.table('first_date',first_date) + console.table('second_date',second_date) + + toggleSelectsInfo(true); if (typeof fswatcherToken !== 'undefined') { let xhr = new XMLHttpRequest(); @@ -10,15 +22,201 @@ function fswatcherCompare(e) { let data = [ 'fswatcher_token=' + fswatcherToken, 'fswatcher_compare=1', - 'fswatcher__first_date=' + document.getElementById('fswatcher__first_date').value, - 'fswatcher__second_date=' + document.getElementById('fswatcher__second_date').value, + 'fswatcher__first_date=' + first_date, + 'fswatcher__second_date=' + second_date, ]; xhr.send(data.join('&')); + xhr.onreadystatechange = function() { + if( xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200 ) { + handleResponse(xhr.response); + } + } } return false; } +function handleResponse(response) { + let responseDataObj = {}; + noChangesDetected = true; + if (typeof response === 'string') { + responseDataObj = decodeJSON(response) + } + + let fsWatcherTableBody = document.getElementById('spbc-table-fs_watcher-comparison'); + const validate_result = validateResponse(responseDataObj) + if (true === validate_result) { + renderTableContent(fsWatcherTableBody, responseDataObj) + if (noChangesDetected) { + renderRow(fsWatcherTableBody, null, 'no_changes', null) + } + } else { + alert(validate_result + ' Please contact support@cleantalk.org'); + } + resetSelects(); +} + +function decodeJSON(response) { + return JSON.parse(response) +} + +function renderTableContent(tbody, responseDataObj) { + tbody.innerHTML = ''; + for (const dataSetName of availableDataSetNames) { + if (handleDataObject(tbody,responseDataObj, dataSetName)) + { + noChangesDetected = false; + } + } +} + +function validateResponse(responseDataObj) { + if ( + !responseDataObj || + typeof responseDataObj !== 'object' + ) { + return 'Response is invalid.' + } + + if (typeof responseDataObj.error !== 'undefined') { + return responseDataObj.error + } + + for (const dataSetName of availableDataSetNames) { + if ( + !responseDataObj.hasOwnProperty(dataSetName) + ) { + return 'Response has no required properties.' + } + } + + return true; +} + +function handleDataObject(tbody, responseDataObj, event_type) { + const events_array = responseDataObj[event_type] + if (events_array.length > 0) { + for (let i = 0; i < events_array.length; i++) { + const row = convertEventToRow(events_array[i], event_type); + renderRow(tbody, row.path, row.event_type, row.date) + } + } else { + return false; + } + return true; +} + +function convertEventToRow(event, event_type) { + let row = { + 'path': 'unknown', + 'event_type': event_type.toUpperCase(), + 'date': 'unknown' + } + + if (event.length === 2) { + if (typeof event[0] === 'string') { + row.path = event[0]; + } + if (typeof event[1] === 'string') { + row.date = new Date(Number(event[1]) * 1000).toLocaleString(); + } + } + + return row; +} + + +function renderRow(tbody, path, event_type, date) { + + if (event_type === 'no_changes') { + let tr = document.createElement('tr') + let td = document.createElement('td') + td.setAttribute('name', 'fswatcher-event-no-changes') + td.setAttribute('colspan', '3') + td.innerText = 'No changes detected on selected dates'; + tr.appendChild(td) + tbody.appendChild(tr); + return; + } + + let tr = document.createElement('tr') + + let td_path = document.createElement('td') + td_path.setAttribute('name', 'fswatcher-event-path') + td_path.innerText = path; + tr.appendChild(td_path) + + let td_type = document.createElement('td') + td_type.setAttribute('name', 'fswatcher-event-type') + td_type.innerText = event_type; + tr.appendChild(td_type) + + let td_date = document.createElement('td') + td_date.setAttribute('name', 'fswatcher-event-date') + td_date.innerText = date; + tr.appendChild(td_date) + + tbody.appendChild(tr); +} + +function filterSecondSelect() { + toggleSelectsInfo(false); + const first_select_value = first_select.options[first_select.selectedIndex].value + if (!second_select.disabled) { + if (typeof first_select_value !== 'undefined') { + for (let i = 0; i < second_select.options.length; i++) { + if (Number(second_select.options[i].value) <= Number(first_select_value)) { + second_select.options[i].style.display = 'none'; + } + } + } + first_select.setAttribute('disabled', 'disabled') + } +} + +function filterFirstSelect() { + const second_select_value = second_select.options[second_select.selectedIndex].value; + toggleSelectsInfo(false); + if (!first_select.disabled) { + if (typeof second_select_value !== 'undefined') { + for (let i = 0; i < first_select.options.length; i++) { + if (Number(first_select.options[i].value) >= Number(second_select_value)) { + first_select.options[i].style.display = 'none'; + } + } + } + } + second_select.setAttribute('disabled', 'disabled') +} + +function resetSelects() { + for (let i = 0; i < first_select.options.length; i++) { + first_select.options[i].style.display = 'inherit'; + } + for (let i = 0; i < second_select.options.length; i++) { + second_select.options[i].style.display = 'inherit'; + } + second_select.removeAttribute('disabled'); + first_select.removeAttribute('disabled'); +} + +function toggleSelectsInfo(enable) { + let infoTag = document.getElementById('spbc--fs-watcher-table-handling-selects-info') + if (enable) { + infoTag.style.display = 'inherit'; + infoTag.innerHTML= 'Comparing log ' + + '' + first_select.options[first_select.selectedIndex].text + '' + + ' with log ' + + '' + second_select.options[second_select.selectedIndex].text + '' + } else { + infoTag.innerText = ''; + infoTag.style.display = 'none'; + } +} + + document.getElementById('fswatcher__compare').addEventListener('click', fswatcherCompare); +first_select.addEventListener('change', filterSecondSelect); +second_select.addEventListener('change', filterFirstSelect); From 6905f4ca4af11e6f7d2c1663b0e6c15de63a3fb1 Mon Sep 17 00:00:00 2001 From: alexandergull Date: Thu, 16 Nov 2023 20:51:12 +0500 Subject: [PATCH 09/24] JS docs and refactoring. --- .../FSWatcher/assets/fswatcher-compare.js | 185 ++++++++++++------ 1 file changed, 120 insertions(+), 65 deletions(-) diff --git a/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js b/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js index 0108b8a30..529dc9962 100755 --- a/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js +++ b/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js @@ -1,19 +1,25 @@ -let noChangesDetected = true; -let first_select = document.getElementById('fswatcher__first_date'); -let second_select = document.getElementById('fswatcher__second_date'); -const availableDataSetNames = ['added','changed','deleted'] - - -function fswatcherCompare(e) { +// if no changes detected after comparison +let noFSWChangesDetected = true; +// first selector elem +const firstFSWSelector = document.getElementById('fswatcher__first_date'); +// second selector elem +const secondFSWSelector = document.getElementById('fswatcher__second_date'); +// FSW table body +const fsWatcherTableBody = document.getElementById('spbc-table-fs_watcher-comparison'); +// available types of events +const availableFSWDataSetNames = ['added','changed','deleted'] + +/** + * Main handler function. Run this on the button click. + * @param {Event} e click event. + */ +function FSWCompare(e) { e.preventDefault(); let first_date = document.getElementById('fswatcher__first_date').value; let second_date = document.getElementById('fswatcher__second_date').value; - console.table('first_date',first_date) - console.table('second_date',second_date) - - toggleSelectsInfo(true); + toggleFSWSelectorsInfo(true); if (typeof fswatcherToken !== 'undefined') { let xhr = new XMLHttpRequest(); @@ -28,7 +34,7 @@ function fswatcherCompare(e) { xhr.send(data.join('&')); xhr.onreadystatechange = function() { if( xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200 ) { - handleResponse(xhr.response); + handleXHRResponse(xhr.response); } } } @@ -36,41 +42,62 @@ function fswatcherCompare(e) { return false; } -function handleResponse(response) { +/** + * Handle File System Watcher XHR Response + * @param {string} response + */ +function handleXHRResponse(response) { let responseDataObj = {}; - noChangesDetected = true; + noFSWChangesDetected = true; if (typeof response === 'string') { responseDataObj = decodeJSON(response) + if (responseDataObj.hasOwnProperty('error')) { + alert('File System watcher JSON parse error: see console for details.') + console.log('File System watcher JSON parse error: ' + responseDataObj.error) + return; + } } - let fsWatcherTableBody = document.getElementById('spbc-table-fs_watcher-comparison'); - const validate_result = validateResponse(responseDataObj) + const validate_result = validateFSWResponse(responseDataObj) if (true === validate_result) { - renderTableContent(fsWatcherTableBody, responseDataObj) - if (noChangesDetected) { - renderRow(fsWatcherTableBody, null, 'no_changes', null) + renderFSWatcherTableContent(responseDataObj) + if (noFSWChangesDetected) { + renderFSWTableRow( '', 'no_changes', '') } } else { alert(validate_result + ' Please contact support@cleantalk.org'); + console.log('File System watcher response validating error: ' + validate_result) } - resetSelects(); + resetFSWSelectors(); } +/** + * Try to decode JSON string from site response. + * @param {string} response + */ function decodeJSON(response) { - return JSON.parse(response) + try { + return JSON.parse(response) + } catch (e) { + return {'error': e}; + } } -function renderTableContent(tbody, responseDataObj) { - tbody.innerHTML = ''; - for (const dataSetName of availableDataSetNames) { - if (handleDataObject(tbody,responseDataObj, dataSetName)) +/** + * Run rendering comparison table in dependence of response object + * @param {{}} responseDataObj + */ +function renderFSWatcherTableContent(responseDataObj) { + fsWatcherTableBody.innerHTML = ''; + for (const dataSetName of availableFSWDataSetNames) { + if (handleFSWDataObject(responseDataObj, dataSetName)) { - noChangesDetected = false; + noFSWChangesDetected = false; } } } -function validateResponse(responseDataObj) { +function validateFSWResponse(responseDataObj) { if ( !responseDataObj || typeof responseDataObj !== 'object' @@ -82,7 +109,7 @@ function validateResponse(responseDataObj) { return responseDataObj.error } - for (const dataSetName of availableDataSetNames) { + for (const dataSetName of availableFSWDataSetNames) { if ( !responseDataObj.hasOwnProperty(dataSetName) ) { @@ -93,12 +120,16 @@ function validateResponse(responseDataObj) { return true; } -function handleDataObject(tbody, responseDataObj, event_type) { +/** + * @param {object} responseDataObj + * @param {string|number} event_type + */ +function handleFSWDataObject(responseDataObj, event_type) { const events_array = responseDataObj[event_type] if (events_array.length > 0) { for (let i = 0; i < events_array.length; i++) { - const row = convertEventToRow(events_array[i], event_type); - renderRow(tbody, row.path, row.event_type, row.date) + const row = convertFSWEventToRow(events_array[i], event_type); + renderFSWTableRow(row.path, row.event_type, row.date) } } else { return false; @@ -106,7 +137,12 @@ function handleDataObject(tbody, responseDataObj, event_type) { return true; } -function convertEventToRow(event, event_type) { +/** + * Convert a row of site response to the formatted data. + * @param {object} event contains the date and the file path + * @param {string} event_type contains event type + */ +function convertFSWEventToRow(event, event_type) { let row = { 'path': 'unknown', 'event_type': event_type.toUpperCase(), @@ -126,7 +162,13 @@ function convertEventToRow(event, event_type) { } -function renderRow(tbody, path, event_type, date) { +/** + * Render the row of FSW table. + * @param {string} path the file path + * @param {string} event_type the event type + * @param {string} date the date of event + */ +function renderFSWTableRow(path, event_type, date) { if (event_type === 'no_changes') { let tr = document.createElement('tr') @@ -135,7 +177,7 @@ function renderRow(tbody, path, event_type, date) { td.setAttribute('colspan', '3') td.innerText = 'No changes detected on selected dates'; tr.appendChild(td) - tbody.appendChild(tr); + fsWatcherTableBody.appendChild(tr); return; } @@ -156,67 +198,80 @@ function renderRow(tbody, path, event_type, date) { td_date.innerText = date; tr.appendChild(td_date) - tbody.appendChild(tr); + fsWatcherTableBody.appendChild(tr); } -function filterSecondSelect() { - toggleSelectsInfo(false); - const first_select_value = first_select.options[first_select.selectedIndex].value - if (!second_select.disabled) { +/** + * Filter options for the first selector and disable it to keep it from changes. + */ +function filterFSWSecondSelector() { + toggleFSWSelectorsInfo(false); + const first_select_value = firstFSWSelector.options[firstFSWSelector.selectedIndex].value + if (!secondFSWSelector.disabled) { if (typeof first_select_value !== 'undefined') { - for (let i = 0; i < second_select.options.length; i++) { - if (Number(second_select.options[i].value) <= Number(first_select_value)) { - second_select.options[i].style.display = 'none'; + for (let i = 0; i < secondFSWSelector.options.length; i++) { + if (Number(secondFSWSelector.options[i].value) <= Number(first_select_value)) { + secondFSWSelector.options[i].style.display = 'none'; } } } - first_select.setAttribute('disabled', 'disabled') + firstFSWSelector.setAttribute('disabled', 'disabled') } } -function filterFirstSelect() { - const second_select_value = second_select.options[second_select.selectedIndex].value; - toggleSelectsInfo(false); - if (!first_select.disabled) { +/** + * Filter options for the second selector and disable it to keep it from changes. + */ +function filterFSWFirstSelector() { + const second_select_value = secondFSWSelector.options[secondFSWSelector.selectedIndex].value; + toggleFSWSelectorsInfo(false); + if (!firstFSWSelector.disabled) { if (typeof second_select_value !== 'undefined') { - for (let i = 0; i < first_select.options.length; i++) { - if (Number(first_select.options[i].value) >= Number(second_select_value)) { - first_select.options[i].style.display = 'none'; + for (let i = 0; i < firstFSWSelector.options.length; i++) { + if (Number(firstFSWSelector.options[i].value) >= Number(second_select_value)) { + firstFSWSelector.options[i].style.display = 'none'; } } } } - second_select.setAttribute('disabled', 'disabled') + secondFSWSelector.setAttribute('disabled', 'disabled') } -function resetSelects() { - for (let i = 0; i < first_select.options.length; i++) { - first_select.options[i].style.display = 'inherit'; +/** + * Reset selectors to its initial statements. + */ +function resetFSWSelectors() { + for (let i = 0; i < firstFSWSelector.options.length; i++) { + firstFSWSelector.options[i].style.display = 'inherit'; } - for (let i = 0; i < second_select.options.length; i++) { - second_select.options[i].style.display = 'inherit'; + for (let i = 0; i < secondFSWSelector.options.length; i++) { + secondFSWSelector.options[i].style.display = 'inherit'; } - second_select.removeAttribute('disabled'); - first_select.removeAttribute('disabled'); + secondFSWSelector.removeAttribute('disabled'); + firstFSWSelector.removeAttribute('disabled'); } -function toggleSelectsInfo(enable) { +/** + * Toggle info string. + * @param {boolean} enable Set logs names if true, disable content if false. + */ +function toggleFSWSelectorsInfo(enable) { let infoTag = document.getElementById('spbc--fs-watcher-table-handling-selects-info') if (enable) { infoTag.style.display = 'inherit'; infoTag.innerHTML= 'Comparing log ' + - '' + first_select.options[first_select.selectedIndex].text + '' + + '' + firstFSWSelector.options[firstFSWSelector.selectedIndex].text + '' + ' with log ' + - '' + second_select.options[second_select.selectedIndex].text + '' + '' + secondFSWSelector.options[secondFSWSelector.selectedIndex].text + '' } else { infoTag.innerText = ''; infoTag.style.display = 'none'; } } - -document.getElementById('fswatcher__compare').addEventListener('click', fswatcherCompare); -first_select.addEventListener('change', filterSecondSelect); -second_select.addEventListener('change', filterFirstSelect); +// listeners +document.getElementById('fswatcher__compare').addEventListener('click', FSWCompare); +firstFSWSelector.addEventListener('change', filterFSWSecondSelector); +secondFSWSelector.addEventListener('change', filterFSWFirstSelector); From f3dafde8668262613451fdb1693339ce65f890ed Mon Sep 17 00:00:00 2001 From: alexandergull Date: Thu, 16 Nov 2023 21:02:12 +0500 Subject: [PATCH 10/24] PHPCS fixes. --- inc/spbc-scanner.php | 2 +- .../Common/FSWatcher/Analyzer/Analyzer.php | 2 +- lib/CleantalkSP/Common/FSWatcher/Controller.php | 12 ++++++------ .../Common/FSWatcher/Repository/FileRepository.php | 2 +- .../Common/FSWatcher/Repository/Repository.php | 2 +- lib/CleantalkSP/Common/FSWatcher/Scan/Scan.php | 4 ++-- lib/CleantalkSP/Common/FSWatcher/Service.php | 4 ++-- lib/CleantalkSP/Common/FSWatcher/WatcherWorkStat.php | 4 +--- .../Scanner/HeuristicAnalyser/Modules/CodeStyle.php | 2 +- .../HeuristicAnalyser/Modules/Mathematics.php | 3 --- lib/CleantalkSP/SpbctWP/Scanner/ScannerQueue.php | 2 +- 11 files changed, 17 insertions(+), 22 deletions(-) diff --git a/inc/spbc-scanner.php b/inc/spbc-scanner.php index 11af7623a..21a9aaad3 100644 --- a/inc/spbc-scanner.php +++ b/inc/spbc-scanner.php @@ -1626,7 +1626,7 @@ function spbc_scanner_file_quarantine($direct_call = false, $file_id = null) $q_path = SPBC_PLUGIN_DIR . 'quarantine/' . str_replace('/', '__', str_replace('\\', '__', $file_info['path'])) . '___' . md5($file_info['path'] . rand(0, 99999999)) . '.punished'; - + $dir_name = SPBC_PLUGIN_DIR . 'quarantine/'; if ( ! is_dir($dir_name)) { mkdir($dir_name); diff --git a/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php index 1a55f7134..d66f85cd7 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php +++ b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php @@ -25,7 +25,7 @@ public static function getCompareResult() return false; } - if (Controller::debug) { + if (Controller::DEBUG) { Logger::log('first journal ' . $first_journal); Logger::log('second journal ' . $second_journal); } diff --git a/lib/CleantalkSP/Common/FSWatcher/Controller.php b/lib/CleantalkSP/Common/FSWatcher/Controller.php index c48dcd1d3..36f5f1143 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Controller.php +++ b/lib/CleantalkSP/Common/FSWatcher/Controller.php @@ -7,7 +7,7 @@ class Controller { - const debug = true; + const DEBUG = true; const STATUS_STOPPED = 'stopped'; const STATUS_RUNNING = 'running'; @@ -21,14 +21,14 @@ class Controller public static function work($params) { - if (self::debug) { + if (self::DEBUG) { Logger::log('check remote call = ' . (int)Service::isRC()); } Service::setStorage(isset($params['storage']) ? $params['storage'] : 'file'); if (self::status() === self::STATUS_STOPPED && Service::isRC() && Service::isCompareRequest()) { - if (self::debug) { + if (self::DEBUG) { Logger::log('run compare file system'); } $compare_result = Analyzer::getCompareResult(); @@ -42,7 +42,7 @@ public static function work($params) } if (self::status() === self::STATUS_STOPPED && Service::isRC()) { - if (self::debug) { + if (self::DEBUG) { Logger::log('run scan file system'); } self::run($params); @@ -50,10 +50,10 @@ public static function work($params) } if (self::status() === self::STATUS_STOPPED && Service::isMinIntervalPassed(self::EXECUTION_MIN_INTERVAL)) { - if (self::debug) { + if (self::DEBUG) { Logger::log('attach js to make remote request'); } - ob_start(['CleantalkSP\Common\FSWatcher\Service', 'attach_js']); + ob_start(['CleantalkSP\Common\FSWatcher\Service', 'attachJS']); } } diff --git a/lib/CleantalkSP/Common/FSWatcher/Repository/FileRepository.php b/lib/CleantalkSP/Common/FSWatcher/Repository/FileRepository.php index b7115c0b0..6aa853f67 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Repository/FileRepository.php +++ b/lib/CleantalkSP/Common/FSWatcher/Repository/FileRepository.php @@ -10,4 +10,4 @@ public static function getAvailableJournals() { return Controller::$storage::getAvailableJournals(); } -} \ No newline at end of file +} diff --git a/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php b/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php index 246aa865c..fff6f6f1f 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php +++ b/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php @@ -5,4 +5,4 @@ interface Repository { public static function getAvailableJournals(); -} \ No newline at end of file +} diff --git a/lib/CleantalkSP/Common/FSWatcher/Scan/Scan.php b/lib/CleantalkSP/Common/FSWatcher/Scan/Scan.php index 990ae5855..9a961fcda 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Scan/Scan.php +++ b/lib/CleantalkSP/Common/FSWatcher/Scan/Scan.php @@ -16,7 +16,7 @@ public static function run($params) self::setParams($params); $journal = Controller::$storage::makeProcessingJournal(); - if (Controller::debug) { + if (Controller::DEBUG) { Logger::log('create processing journal ' . $journal); } @@ -40,4 +40,4 @@ private static function scan() Controller::$storage::writeJournal($iterator, self::$extensions_to_watch, self::$exclude_dirs); } -} \ No newline at end of file +} diff --git a/lib/CleantalkSP/Common/FSWatcher/Service.php b/lib/CleantalkSP/Common/FSWatcher/Service.php index 2b671bceb..24a54db6e 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Service.php +++ b/lib/CleantalkSP/Common/FSWatcher/Service.php @@ -39,7 +39,7 @@ public static function isMinIntervalPassed($interval) return (time() - $last_exec_time) > $interval; } - public static function attach_js($buffer) + public static function attachJS($buffer) { $is_ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'; $is_html = preg_match('/^\s*((\s|<.*>)*<\/html>\s*$/i', - $addition.'', + $addition . '', $buffer, 1 ); diff --git a/lib/CleantalkSP/Common/FSWatcher/WatcherWorkStat.php b/lib/CleantalkSP/Common/FSWatcher/WatcherWorkStat.php index f6dbe6eb8..1908bb587 100755 --- a/lib/CleantalkSP/Common/FSWatcher/WatcherWorkStat.php +++ b/lib/CleantalkSP/Common/FSWatcher/WatcherWorkStat.php @@ -6,11 +6,9 @@ class WatcherWorkStat { public static function startScanTime() { - } public static function finishScanTime() { - } -} \ No newline at end of file +} diff --git a/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/Modules/CodeStyle.php b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/Modules/CodeStyle.php index c2a5a342f..8f36fc44c 100644 --- a/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/Modules/CodeStyle.php +++ b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/Modules/CodeStyle.php @@ -164,7 +164,7 @@ private function getWeightOfRandom($content) preg_match_all('#[a-zA-Z\d_\-\+]+#', $content, $words); $words = isset($words[0]) ? $words[0] : []; - $words = array_filter($words, function($word) { + $words = array_filter($words, function ($word) { return strlen($word) > 5; }); $words = array_values($words); diff --git a/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/Modules/Mathematics.php b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/Modules/Mathematics.php index 0f41b962b..3a6888b4c 100644 --- a/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/Modules/Mathematics.php +++ b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/Modules/Mathematics.php @@ -59,9 +59,7 @@ public function evaluateMathExpressions() $this->tokens->$index_to_insert->line, $this->tokens->$index_to_insert->key ); - } - } } } @@ -80,7 +78,6 @@ private function getTokensInsideBrackets($closing_bracket) $closing_bracket_position = $this->tokens->searchForward($start_position, $closing_bracket); $tokens_inside_brackets = $this->tokens->getRange($start_position, $closing_bracket_position - 1); if ( $closing_bracket === ')' ) { - // Loop: If there are inner brackets - continue searching end of the expression $inner_brackets = []; do { diff --git a/lib/CleantalkSP/SpbctWP/Scanner/ScannerQueue.php b/lib/CleantalkSP/SpbctWP/Scanner/ScannerQueue.php index e6e8328de..4ab973333 100755 --- a/lib/CleantalkSP/SpbctWP/Scanner/ScannerQueue.php +++ b/lib/CleantalkSP/SpbctWP/Scanner/ScannerQueue.php @@ -1214,7 +1214,7 @@ public function signature_analysis($status = 'UNKNOWN,MODIFIED,OK,INFECTED', $of if ( $weak_spots !== 'NULL' ) { // Collect signatures triggered counts if ( isset($result->weak_spots['SIGNATURES']) && is_array($result->weak_spots['SIGNATURES']) ) { - foreach($result->weak_spots['SIGNATURES'] as $signature_ids) { + foreach ($result->weak_spots['SIGNATURES'] as $signature_ids) { $signature_idx = $spbc->data['scanner']['signatures_found']; foreach ( $signature_ids as $signature_id ) { $signature_idx[$signature_id] = ! empty($signature_idx[$signature_id]) From e4a871fee323be2ece59f684a642cf8e156a98ab Mon Sep 17 00:00:00 2001 From: alexandergull Date: Thu, 16 Nov 2023 21:38:06 +0500 Subject: [PATCH 11/24] PSALM fixes. --- .../Common/FSWatcher/Analyzer/Analyzer.php | 17 +++++++---- .../Common/FSWatcher/Controller.php | 22 +++++++++++---- .../FSWatcher/Repository/Repository.php | 4 +++ .../Common/FSWatcher/Scan/Scan.php | 2 +- lib/CleantalkSP/Common/FSWatcher/Service.php | 4 +-- .../Common/FSWatcher/Storage/FileStorage.php | 14 ++++++++-- .../Common/FSWatcher/Storage/Storage.php | 28 +++++++++++++++++++ .../Common/FSWatcher/View/View.php | 2 +- .../Common/FSWatcher/WatcherWorkStat.php | 14 ---------- 9 files changed, 74 insertions(+), 33 deletions(-) delete mode 100755 lib/CleantalkSP/Common/FSWatcher/WatcherWorkStat.php diff --git a/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php index d66f85cd7..c10984b5d 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php +++ b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php @@ -25,7 +25,7 @@ public static function getCompareResult() return false; } - if (Controller::DEBUG) { + if (Controller::$debug) { Logger::log('first journal ' . $first_journal); Logger::log('second journal ' . $second_journal); } @@ -102,14 +102,19 @@ private static function compare($first_journal, $second_journal) private static function uncompress($file) { - if (substr($file, -3) === '.gz') { - $content = gzopen($file, 'r'); - if (false === $content) { + if ( substr($file, -3) === '.gz' ) { + $content = @gzopen($file, 'r'); + if ( false === $content ) { return false; } - $write_result = @file_put_contents(substr($file, 0, -3), gzread($content, 1024 * 1024 * 10)); + $gz_result = @gzread($content, 1024 * 1024 * 10); + if ( !is_string($gz_result) ) { + @gzclose($content); + return false; + } + $write_result = @file_put_contents(substr($file, 0, -3), $gz_result); gzclose($content); - if (false === $write_result) { + if ( false === $write_result ) { return false; } $file = substr($file, 0, -3); diff --git a/lib/CleantalkSP/Common/FSWatcher/Controller.php b/lib/CleantalkSP/Common/FSWatcher/Controller.php index 36f5f1143..e8357a118 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Controller.php +++ b/lib/CleantalkSP/Common/FSWatcher/Controller.php @@ -7,8 +7,7 @@ class Controller { - const DEBUG = true; - + public static $debug; const STATUS_STOPPED = 'stopped'; const STATUS_RUNNING = 'running'; @@ -19,16 +18,27 @@ class Controller private static $status = self::STATUS_STOPPED; + private static function getDebugState() + { + if ( defined('SPBC_FSWATCHER_DEBUG') ) { + return SPBC_FSWATCHER_DEBUG; + } else { + return false; + } + } + public static function work($params) { - if (self::DEBUG) { + self::getDebugState(); + + if (self::$debug) { Logger::log('check remote call = ' . (int)Service::isRC()); } Service::setStorage(isset($params['storage']) ? $params['storage'] : 'file'); if (self::status() === self::STATUS_STOPPED && Service::isRC() && Service::isCompareRequest()) { - if (self::DEBUG) { + if (self::$debug) { Logger::log('run compare file system'); } $compare_result = Analyzer::getCompareResult(); @@ -42,7 +52,7 @@ public static function work($params) } if (self::status() === self::STATUS_STOPPED && Service::isRC()) { - if (self::DEBUG) { + if (self::$debug) { Logger::log('run scan file system'); } self::run($params); @@ -50,7 +60,7 @@ public static function work($params) } if (self::status() === self::STATUS_STOPPED && Service::isMinIntervalPassed(self::EXECUTION_MIN_INTERVAL)) { - if (self::DEBUG) { + if (self::$debug) { Logger::log('attach js to make remote request'); } ob_start(['CleantalkSP\Common\FSWatcher\Service', 'attachJS']); diff --git a/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php b/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php index fff6f6f1f..02f2d6077 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php +++ b/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php @@ -4,5 +4,9 @@ interface Repository { + /** + * @return mixed + * @psalm-suppress PossiblyUnusedMethod + */ public static function getAvailableJournals(); } diff --git a/lib/CleantalkSP/Common/FSWatcher/Scan/Scan.php b/lib/CleantalkSP/Common/FSWatcher/Scan/Scan.php index 9a961fcda..ca30b9009 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Scan/Scan.php +++ b/lib/CleantalkSP/Common/FSWatcher/Scan/Scan.php @@ -16,7 +16,7 @@ public static function run($params) self::setParams($params); $journal = Controller::$storage::makeProcessingJournal(); - if (Controller::DEBUG) { + if (Controller::$debug) { Logger::log('create processing journal ' . $journal); } diff --git a/lib/CleantalkSP/Common/FSWatcher/Service.php b/lib/CleantalkSP/Common/FSWatcher/Service.php index 24a54db6e..7d7aa6568 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Service.php +++ b/lib/CleantalkSP/Common/FSWatcher/Service.php @@ -47,7 +47,7 @@ public static function attachJS($buffer) if (!$is_ajax && $is_html) { $path = __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'fswatcher.js'; $addition = - '' + '' . ''; $buffer = preg_replace( @@ -63,7 +63,7 @@ public static function attachJS($buffer) public static function isRC() { - if (isset($_POST['fswatcher_token']) && $_POST['fswatcher_token'] == md5(filemtime(__FILE__))) { + if (isset($_POST['fswatcher_token']) && $_POST['fswatcher_token'] == md5((string)filemtime(__FILE__))) { return true; } diff --git a/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php b/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php index 91f5c6c15..20e19a731 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php +++ b/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php @@ -43,6 +43,10 @@ public static function getLastJournalTime() return null; } + /** + * @return void + * @psalm-suppress InvalidReturnType + */ public static function setAllJournalsAsCompleted() { $journals = glob(self::getJournalsPath() . '*.csv'); @@ -75,6 +79,10 @@ public static function getAvailableJournals() return $dates; } + /** + * @return void + * @psalm-suppress InvalidReturnType + */ public static function writeJournal($iterator, $extensions_to_watch, $exclude_dirs) { $journal = self::getProcessingJournal(); @@ -84,12 +92,12 @@ public static function writeJournal($iterator, $extensions_to_watch, $exclude_di $iterator->next(); } else { if (in_array($dir->getExtension(), $extensions_to_watch)) { - $mtime = @filemtime($path); + $mtime = @filemtime((string)$path); if ( empty($mtime) ) { clearstatcache($path); - $mtime = @filemtime($path); + $mtime = @filemtime((string)$path); if ( empty($mtime) ) { - $mtime = @filectime($path); + $mtime = @filectime((string)$path); if ( empty($mtime) ) { $mtime = time(); } diff --git a/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php b/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php index 125b805cc..025257351 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php +++ b/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php @@ -4,17 +4,45 @@ interface Storage { + /** + * @return mixed + * @psalm-suppress PossiblyUnusedMethod + */ public static function getProcessingJournal(); + /** + * @return mixed + * @psalm-suppress PossiblyUnusedMethod + */ public static function getLastJournalTime(); + /** + * @return mixed + * @psalm-suppress PossiblyUnusedMethod + */ public static function makeProcessingJournal(); + /** + * @return mixed + * @psalm-suppress PossiblyUnusedMethod + */ public static function setAllJournalsAsCompleted(); + /** + * @return mixed + * @psalm-suppress PossiblyUnusedMethod + */ public static function writeJournal($iterator, $extensions_to_watch, $exclude_dirs); + /** + * @return mixed + * @psalm-suppress PossiblyUnusedMethod + */ public static function getAvailableJournals(); + /** + * @return mixed + * @psalm-suppress PossiblyUnusedMethod + */ public static function getJournal($journal); } diff --git a/lib/CleantalkSP/Common/FSWatcher/View/View.php b/lib/CleantalkSP/Common/FSWatcher/View/View.php index 634f2c681..bd2646dfb 100755 --- a/lib/CleantalkSP/Common/FSWatcher/View/View.php +++ b/lib/CleantalkSP/Common/FSWatcher/View/View.php @@ -49,7 +49,7 @@ public static function renderSelectors() $html .= ''; $html .= ''; diff --git a/lib/CleantalkSP/Common/FSWatcher/WatcherWorkStat.php b/lib/CleantalkSP/Common/FSWatcher/WatcherWorkStat.php deleted file mode 100755 index 1908bb587..000000000 --- a/lib/CleantalkSP/Common/FSWatcher/WatcherWorkStat.php +++ /dev/null @@ -1,14 +0,0 @@ - Date: Thu, 16 Nov 2023 21:56:45 +0500 Subject: [PATCH 12/24] PSALM Fixes after dev merge. --- inc/spbc-scanner.php | 5 +++-- inc/spbc-settings.php | 8 +++----- lib/CleantalkSP/Common/FSWatcher/Controller.php | 2 +- .../HeuristicAnalyser/Modules/Variables.php | 8 ++++---- lib/CleantalkSP/SpbctWP/Firewall/WafBlocker.php | 17 ++++++++--------- security-malware-firewall.php | 1 - 6 files changed, 19 insertions(+), 22 deletions(-) diff --git a/inc/spbc-scanner.php b/inc/spbc-scanner.php index 4ecb04a92..447135511 100644 --- a/inc/spbc-scanner.php +++ b/inc/spbc-scanner.php @@ -1836,11 +1836,12 @@ function spbc_scanner_analysis_log_delete_from_log($direct_call = false) if ( is_array($file_ids) ) { // Validate if the ID is hash (SQL-clear) - $file_ids_clean = array_map( function($id) { + $file_ids_clean = array_map(function ($id) { if ( \Cleantalk\ApbctWP\Validate::isHash($id) ) { return $id; } - }, $file_ids ); + return 'none'; + }, $file_ids); } $output = array('error' => false); diff --git a/inc/spbc-settings.php b/inc/spbc-settings.php index 507da04f1..3d09eb91d 100644 --- a/inc/spbc-settings.php +++ b/inc/spbc-settings.php @@ -2486,16 +2486,15 @@ function spbc_field_scanner__prepare_data__files(&$table) $ws_string .= '

'; } } - if ( ! empty($weak_spots['DENIED_HASH'])) { + if ( !empty($weak_spots['DENIED_HASH']) ) { // collecting all kinds of code $all_unique_weak_spots = array(); foreach ($weak_spots['DENIED_HASH'] as $_string => $weak_spot_in_string) { - $all_unique_weak_spots[] = $weak_spot_in_string[0]; + $all_unique_weak_spots[] = $weak_spot_in_string[0]; } $all_unique_weak_spots = array_unique($all_unique_weak_spots); foreach ($all_unique_weak_spots as $weak_spot_in_string) { - - $ws_string .= '

Hash: ' + $ws_string .= '

Hash: ' . 'denied'; $ws_string .= '

'; @@ -2509,7 +2508,6 @@ function spbc_field_scanner__prepare_data__files(&$table) } $all_unique_weak_spots = array_unique($all_unique_weak_spots); foreach ($all_unique_weak_spots as $weak_spot_in_string) { - $ws_string .= '

Danger: ' . (strlen($weak_spot_in_string) > 30 ? substr($weak_spot_in_string, 0, 30) . '...' diff --git a/lib/CleantalkSP/Common/FSWatcher/Controller.php b/lib/CleantalkSP/Common/FSWatcher/Controller.php index e8357a118..5100329d8 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Controller.php +++ b/lib/CleantalkSP/Common/FSWatcher/Controller.php @@ -30,7 +30,7 @@ private static function getDebugState() public static function work($params) { self::getDebugState(); - + if (self::$debug) { Logger::log('check remote call = ' . (int)Service::isRC()); } diff --git a/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/Modules/Variables.php b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/Modules/Variables.php index 5ed3c2ae7..dd68fed41 100644 --- a/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/Modules/Variables.php +++ b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/Modules/Variables.php @@ -427,10 +427,10 @@ public function concatenateVars($key) $this->tokens->searchForward($this->tokens->current[3], ';') - 1 ); - foreach ( $tokens_of_variable_for_concat as $token ) { - if ($token->type === 'T_CONSTANT_ENCAPSED_STRING') { - $last_txt_token = $var_expression[count($var_expression)-1][1]; - $var_expression[count($var_expression)-1][1] = implode('', [ + foreach ($tokens_of_variable_for_concat as $token) { + if ( $token->type === 'T_CONSTANT_ENCAPSED_STRING' ) { + $last_txt_token = $var_expression[count($var_expression) - 1][1]; + $var_expression[count($var_expression) - 1][1] = implode('', [ mb_substr($last_txt_token, 0, -1), trim((string)$token[1], '\'\"'), mb_substr($last_txt_token, -1) diff --git a/lib/CleantalkSP/SpbctWP/Firewall/WafBlocker.php b/lib/CleantalkSP/SpbctWP/Firewall/WafBlocker.php index 1682051df..8c0bf50c5 100644 --- a/lib/CleantalkSP/SpbctWP/Firewall/WafBlocker.php +++ b/lib/CleantalkSP/SpbctWP/Firewall/WafBlocker.php @@ -6,7 +6,6 @@ class WafBlocker extends FirewallModule { - public $module_name = 'WafBlocker'; protected $is_logged_in = false; @@ -29,23 +28,23 @@ public function check() { $results = array(); - if ( ! $this->is_logged_in ) { - foreach ( $this->ip_array as $current_ip ) { - $rand = rand(1, 100000); + if ( !$this->is_logged_in ) { + foreach ($this->ip_array as $current_ip) { + $rand = rand(1, 100000); $md5_ip = md5($current_ip); - $query = 'SELECT SUM(entries) as total_count + $query = 'SELECT SUM(entries) as total_count FROM ' . SPBC_TBL_TC_LOG . ' WHERE md5_ip = \'' . $md5_ip . '\' AND log_type = 2 AND ' . $rand . ';'; - $result= $this->db->fetch($query, OBJECT); + $result = $this->db->fetch($query, OBJECT); if ( isset($result->total_count) && $result->total_count > $this->waf_blocker_limit ) { $results[] = new Result( array( 'module' => $this->module_name, - 'ip' => end($this->ip_array), + 'ip' => end($this->ip_array), 'status' => 'DENY_BY_WAF_BLOCKER', ) ); @@ -97,8 +96,8 @@ public function clearTable() $query = "DELETE FROM " . SPBC_TBL_TC_LOG . " WHERE - interval_start < " . time() - $this->block_period . " AND - log_type = 2 + interval_start < " . (time() - $this->block_period) . " AND + log_type = 2 LIMIT 100000;"; $this->db->execute($query); } diff --git a/security-malware-firewall.php b/security-malware-firewall.php index 7c8f57973..4c04f64f2 100644 --- a/security-malware-firewall.php +++ b/security-malware-firewall.php @@ -515,7 +515,6 @@ function spbc_firewall__check() $firewall->loadFwModule($waf_blocker); } $firewall->loadFwModule(new WAF($waf_params)); - } //todo This rewrite could break permalinks, need to implement new logic From a31df7b538dc16135fc4f28993d8ff1efef5063a Mon Sep 17 00:00:00 2001 From: Glomberg Date: Wed, 6 Dec 2023 05:50:23 +0300 Subject: [PATCH 13/24] Fix. FS Watcher. Logs directory listing prevented. --- lib/CleantalkSP/Common/FSWatcher/logs/index.php | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 lib/CleantalkSP/Common/FSWatcher/logs/index.php diff --git a/lib/CleantalkSP/Common/FSWatcher/logs/index.php b/lib/CleantalkSP/Common/FSWatcher/logs/index.php new file mode 100644 index 000000000..62200328f --- /dev/null +++ b/lib/CleantalkSP/Common/FSWatcher/logs/index.php @@ -0,0 +1,2 @@ + Date: Wed, 6 Dec 2023 05:52:44 +0300 Subject: [PATCH 14/24] Fix. FS Watcher. Selectors rendering fixed. --- .../Common/FSWatcher/View/View.php | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/CleantalkSP/Common/FSWatcher/View/View.php b/lib/CleantalkSP/Common/FSWatcher/View/View.php index bd2646dfb..25fe2e355 100755 --- a/lib/CleantalkSP/Common/FSWatcher/View/View.php +++ b/lib/CleantalkSP/Common/FSWatcher/View/View.php @@ -24,18 +24,27 @@ public static function renderSelectors() $html .= '

'; $html .= '

' . self::getFSWatcherDescription() . '

'; + + $dates = Controller::$repository::getAvailableJournals(); + + if ( ! static::snapshotsAreReady($dates) ) { + // Snapshots were not ready, do not render selectors + $html .= '

' . __('Snapshots were not ready.', 'security-malware-firewall') . '

'; + $html .= '

' . __('Please wait while FS Journal will be ready to work.', 'security-malware-firewall') . '

'; + return $html; + } + $html .= '

' . __('To run comparison select dates which you want to compare and click the "Compare" button.', 'security-malware-firewall') . '

'; $html .= ''; $html .= ''; $html .= '
'; $html .= '
'; - $html .= ''; $html .= ''; $html .= '
'; $html .= '
'; @@ -61,10 +70,8 @@ public static function renderSelectors() return $html; } - private static function renderSelectorOptions() + private static function renderSelectorOptions($dates) { - $dates = Controller::$repository::getAvailableJournals(); - Logger::log($dates); $html = ''; @@ -109,4 +116,9 @@ private static function renderTableTemplate() return $html; } + + private static function snapshotsAreReady($dates) + { + return is_array($dates) && count($dates) > 1; + } } From bc341e960a19312b390d2abdc9d729a87b2e768a Mon Sep 17 00:00:00 2001 From: Glomberg Date: Wed, 6 Dec 2023 05:53:05 +0300 Subject: [PATCH 15/24] Upd. Code. Gitignore updated. --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index c7caf1bc9..9f01a5ddb 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ psalm.xml /lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/ /lib/CleantalkSP/Common/Scanner/SignaturesAnalyser/tests/ /lib/CleantalkSP/Common/Scanner/SignaturesAnalyser/.github/ +/lib/CleantalkSP/Common/FSWatcher/Storage/data/ +/lib/CleantalkSP/Common/FSWatcher/logs/ From d9456850eae18388439226a053a38846f82e5ef1 Mon Sep 17 00:00:00 2001 From: Glomberg Date: Wed, 6 Dec 2023 05:55:41 +0300 Subject: [PATCH 16/24] Fix. Code. Code style fixed. --- lib/CleantalkSP/Common/FSWatcher/logs/index.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/CleantalkSP/Common/FSWatcher/logs/index.php b/lib/CleantalkSP/Common/FSWatcher/logs/index.php index 62200328f..e0ee50835 100644 --- a/lib/CleantalkSP/Common/FSWatcher/logs/index.php +++ b/lib/CleantalkSP/Common/FSWatcher/logs/index.php @@ -1,2 +1,3 @@ Date: Wed, 6 Dec 2023 13:03:07 +0300 Subject: [PATCH 17/24] Fix: Code. Doc-blocks added, psalm notices fixed. --- .../Common/FSWatcher/Analyzer/Analyzer.php | 12 +++++ .../Common/FSWatcher/Controller.php | 52 +++++++++++++++++-- .../FSWatcher/Repository/FileRepository.php | 6 +++ .../FSWatcher/Repository/Repository.php | 8 ++- lib/CleantalkSP/Common/FSWatcher/Service.php | 49 +++++++++++++++-- .../Common/FSWatcher/Storage/FileStorage.php | 40 ++++++++++++-- .../Common/FSWatcher/Storage/Storage.php | 39 +++++++++----- 7 files changed, 176 insertions(+), 30 deletions(-) diff --git a/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php index c10984b5d..f1ec45312 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php +++ b/lib/CleantalkSP/Common/FSWatcher/Analyzer/Analyzer.php @@ -7,6 +7,9 @@ class Analyzer { + /** + * @return array|false + */ public static function getCompareResult() { $first = filter_var($_POST['fswatcher__first_date'], FILTER_VALIDATE_INT); @@ -33,6 +36,11 @@ public static function getCompareResult() return self::compare($first_journal, $second_journal); } + /** + * @param $first_journal + * @param $second_journal + * @return array|false + */ private static function compare($first_journal, $second_journal) { $result = array( @@ -100,6 +108,10 @@ private static function compare($first_journal, $second_journal) } } + /** + * @param $file + * @return false|string + */ private static function uncompress($file) { if ( substr($file, -3) === '.gz' ) { diff --git a/lib/CleantalkSP/Common/FSWatcher/Controller.php b/lib/CleantalkSP/Common/FSWatcher/Controller.php index 5100329d8..762254fcc 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Controller.php +++ b/lib/CleantalkSP/Common/FSWatcher/Controller.php @@ -7,26 +7,54 @@ class Controller { + /** + * @var bool + */ public static $debug; + const STATUS_STOPPED = 'stopped'; + const STATUS_RUNNING = 'running'; - const EXECUTION_MIN_INTERVAL = 6000; + const EXECUTION_MIN_INTERVAL = 6000; // 1,66 hours + /** + * @var \CleantalkSP\Common\FSWatcher\Storage\Storage + */ public static $storage; + + /** + * @var \CleantalkSP\Common\FSWatcher\Repository\Repository + */ public static $repository; private static $status = self::STATUS_STOPPED; + /** + * Initialize the `$debug` property false|true + * + * @return void + */ private static function getDebugState() { if ( defined('SPBC_FSWATCHER_DEBUG') ) { - return SPBC_FSWATCHER_DEBUG; - } else { - return false; + self::$debug = (bool) SPBC_FSWATCHER_DEBUG; } } + /** + * This is the init method. + * + * Making initialize the `$debug` property + * + * Contains Ajax handler for requests: + * 1) Comparing logs + * 2) Scanning file system + * 3) Automatically making ajax requests for 2) + * + * @param $params + * @return void + */ public static function work($params) { self::getDebugState(); @@ -67,6 +95,12 @@ public static function work($params) } } + /** + * Scanning file system handler + * + * @param $params + * @return void + */ private static function run($params) { self::$status = self::STATUS_RUNNING; @@ -74,12 +108,22 @@ private static function run($params) self::stop(); } + /** + * Scanning file system stop trigger + * + * @return void + */ private static function stop() { self::$status = self::STATUS_STOPPED; Service::setAllJournalsAsCompleted(); } + /** + * Checking status of the scanning process + * + * @return string + */ private static function status() { $is_exist = Service::getProcessingJournal(); diff --git a/lib/CleantalkSP/Common/FSWatcher/Repository/FileRepository.php b/lib/CleantalkSP/Common/FSWatcher/Repository/FileRepository.php index 6aa853f67..28105b3a2 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Repository/FileRepository.php +++ b/lib/CleantalkSP/Common/FSWatcher/Repository/FileRepository.php @@ -4,8 +4,14 @@ use CleantalkSP\Common\FSWatcher\Controller; +/** + * @psalm-suppress UnusedClass + */ class FileRepository implements Repository { + /** + * @inheritDoc + */ public static function getAvailableJournals() { return Controller::$storage::getAvailableJournals(); diff --git a/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php b/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php index 02f2d6077..04736fbac 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php +++ b/lib/CleantalkSP/Common/FSWatcher/Repository/Repository.php @@ -2,11 +2,15 @@ namespace CleantalkSP\Common\FSWatcher\Repository; +/** + * @psalm-suppress PossiblyUnusedMethod + */ interface Repository { /** - * @return mixed - * @psalm-suppress PossiblyUnusedMethod + * Get all timestamps of the available snapshots + * + * @return array */ public static function getAvailableJournals(); } diff --git a/lib/CleantalkSP/Common/FSWatcher/Service.php b/lib/CleantalkSP/Common/FSWatcher/Service.php index 7d7aa6568..1850bd23f 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Service.php +++ b/lib/CleantalkSP/Common/FSWatcher/Service.php @@ -7,10 +7,19 @@ class Service { + /** + * Factory method for initializing `Controller::$storage` and `Controller::$repository` + * + * @param $storage + * @return void + * + * @psalm-suppress InvalidPropertyAssignmentValue + */ public static function setStorage($storage = 'file') { switch ($storage) { case 'file': + default: Controller::$storage = FileStorage::class; Controller::$repository = FileRepository::class; break; @@ -22,13 +31,15 @@ public static function setStorage($storage = 'file') // Controller::$storage = DBStorage::class; // Controller::$repository = DBStorage::class; // break; - default: - Controller::$storage = FileStorage::class; - Controller::$repository = FileRepository::class; - break; } } + /** + * Is scanning interval was outdated + * + * @param $interval + * @return bool + */ public static function isMinIntervalPassed($interval) { $last_exec_time = Controller::$storage::getLastJournalTime(); @@ -39,6 +50,14 @@ public static function isMinIntervalPassed($interval) return (time() - $last_exec_time) > $interval; } + /** + * Attach JS file for start ajax call + * + * @param $buffer string + * @return string + * + * @psalm-suppress PossiblyUnusedReturnValue + */ public static function attachJS($buffer) { $is_ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'; @@ -61,6 +80,11 @@ public static function attachJS($buffer) return $buffer; } + /** + * Is ajax call is in process + * + * @return bool + */ public static function isRC() { if (isset($_POST['fswatcher_token']) && $_POST['fswatcher_token'] == md5((string)filemtime(__FILE__))) { @@ -70,6 +94,11 @@ public static function isRC() return false; } + /** + * Checking request validity: Comparing logs + * + * @return bool + */ public static function isCompareRequest() { if (isset($_POST['fswatcher_compare']) && $_POST['fswatcher_compare'] == true && @@ -82,13 +111,23 @@ public static function isCompareRequest() return false; } + /** + * Set snapshots to completed status + * + * @return void + */ public static function setAllJournalsAsCompleted() { Controller::$storage::setAllJournalsAsCompleted(); } + /** + * Get snapshots which is in process + * + * @return string|null + */ public static function getProcessingJournal() { - Controller::$storage::getProcessingJournal(); + return Controller::$storage::getProcessingJournal(); } } diff --git a/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php b/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php index 20e19a731..e1f547342 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php +++ b/lib/CleantalkSP/Common/FSWatcher/Storage/FileStorage.php @@ -5,8 +5,12 @@ class FileStorage implements Storage { const STATUS_PROCESSING = '__processing'; + const STATUS_COMPLETED = '__completed'; + /** + * @inheritDoc + */ public static function makeProcessingJournal() { $journals_path = self::getJournalsPath(); @@ -17,6 +21,9 @@ public static function makeProcessingJournal() return $path; } + /** + * @inheritDoc + */ public static function getProcessingJournal() { $is_processing = glob(self::getJournalsPath() . '*' . self::STATUS_PROCESSING . '.csv'); @@ -27,6 +34,9 @@ public static function getProcessingJournal() return null; } + /** + * @inheritDoc + */ public static function getLastJournalTime() { $pattern = self::getJournalsPath() . '*' . self::STATUS_COMPLETED . '.csv'; @@ -44,8 +54,7 @@ public static function getLastJournalTime() } /** - * @return void - * @psalm-suppress InvalidReturnType + * @inheritDoc */ public static function setAllJournalsAsCompleted() { @@ -57,6 +66,9 @@ public static function setAllJournalsAsCompleted() } } + /** + * @inheritDoc + */ public static function getAvailableJournals() { $dir = self::getJournalsPath(); @@ -80,13 +92,17 @@ public static function getAvailableJournals() } /** - * @return void - * @psalm-suppress InvalidReturnType + * @inheritDoc */ public static function writeJournal($iterator, $extensions_to_watch, $exclude_dirs) { $journal = self::getProcessingJournal(); - $fp = fopen($journal, 'w'); + + if ( ! $journal ) { + return; + } + + $fp = fopen((string)$journal, 'w'); foreach ($iterator as $path => $dir) { if ($dir->isDir() && !in_array($dir->getFilename(), $exclude_dirs)) { $iterator->next(); @@ -111,6 +127,9 @@ public static function writeJournal($iterator, $extensions_to_watch, $exclude_di fclose($fp); } + /** + * @inheritDoc + */ public static function getJournal($journal) { $journals_path = self::getJournalsPath(); @@ -126,6 +145,11 @@ public static function getJournal($journal) return $path; } + /** + * Get snapshots files directory + * + * @return string + */ private static function getJournalsPath() { $dir_name = __DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR; @@ -137,6 +161,12 @@ private static function getJournalsPath() return $dir_name; } + /** + * Archive the snapshot file + * + * @param $journal string + * @return void + */ private static function compressJournalGZ($journal) { if ( ! function_exists('gzopen')) { diff --git a/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php b/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php index 025257351..fdf7b8ffc 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php +++ b/lib/CleantalkSP/Common/FSWatcher/Storage/Storage.php @@ -2,47 +2,58 @@ namespace CleantalkSP\Common\FSWatcher\Storage; +/** + * @psalm-suppress PossiblyUnusedMethod + */ interface Storage { /** - * @return mixed - * @psalm-suppress PossiblyUnusedMethod + * Get snapshots files which is in process + * + * @return string|null */ public static function getProcessingJournal(); /** - * @return mixed - * @psalm-suppress PossiblyUnusedMethod + * Get the oldest timestamp of the available snapshots files + * + * @return int|null */ public static function getLastJournalTime(); /** - * @return mixed - * @psalm-suppress PossiblyUnusedMethod + * Creating empty snapshot file + * + * @return string */ public static function makeProcessingJournal(); /** - * @return mixed - * @psalm-suppress PossiblyUnusedMethod + * Set snapshots files to completed status - renaming these to `*_completed` + * + * @return void */ public static function setAllJournalsAsCompleted(); /** - * @return mixed - * @psalm-suppress PossiblyUnusedMethod + * Write data into snapshot file + * + * @return void */ public static function writeJournal($iterator, $extensions_to_watch, $exclude_dirs); /** - * @return mixed - * @psalm-suppress PossiblyUnusedMethod + * Get all timestamps of the available snapshots files + * + * @return array */ public static function getAvailableJournals(); /** - * @return mixed - * @psalm-suppress PossiblyUnusedMethod + * Get journal path by journal timestamp + * + * @param $journal string + * @return string|null */ public static function getJournal($journal); } From 7afe77f623652e1ec00f51bc7357adf40b8a451a Mon Sep 17 00:00:00 2001 From: Glomberg Date: Wed, 6 Dec 2023 16:43:12 +0300 Subject: [PATCH 18/24] Upd. FS Watcher. Manual creating snapshot button and logic implemented. --- .../Common/FSWatcher/Controller.php | 4 +- lib/CleantalkSP/Common/FSWatcher/Service.php | 10 +++++ .../Common/FSWatcher/View/View.php | 13 +++++- ...swatcher-compare.js => fswatcher-logic.js} | 40 +++++++++++++++++-- 4 files changed, 60 insertions(+), 7 deletions(-) rename lib/CleantalkSP/Common/FSWatcher/assets/{fswatcher-compare.js => fswatcher-logic.js} (87%) mode change 100755 => 100644 diff --git a/lib/CleantalkSP/Common/FSWatcher/Controller.php b/lib/CleantalkSP/Common/FSWatcher/Controller.php index 762254fcc..e20abc4ee 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Controller.php +++ b/lib/CleantalkSP/Common/FSWatcher/Controller.php @@ -79,12 +79,12 @@ public static function work($params) die(); } - if (self::status() === self::STATUS_STOPPED && Service::isRC()) { + if (self::status() === self::STATUS_STOPPED && ( Service::isRC() || ( Service::isRC() && Service::isCreateSnapshotRequest() ))) { if (self::$debug) { Logger::log('run scan file system'); } self::run($params); - die('OK'); + die(json_encode('OK')); } if (self::status() === self::STATUS_STOPPED && Service::isMinIntervalPassed(self::EXECUTION_MIN_INTERVAL)) { diff --git a/lib/CleantalkSP/Common/FSWatcher/Service.php b/lib/CleantalkSP/Common/FSWatcher/Service.php index 1850bd23f..e26349b45 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Service.php +++ b/lib/CleantalkSP/Common/FSWatcher/Service.php @@ -111,6 +111,16 @@ public static function isCompareRequest() return false; } + /** + * Checking request validity: Creating Snapshot + * + * @return bool + */ + public static function isCreateSnapshotRequest() + { + return isset($_POST['fswatcher_create_snapshot']) && $_POST['fswatcher_create_snapshot'] == true; + } + /** * Set snapshots to completed status * diff --git a/lib/CleantalkSP/Common/FSWatcher/View/View.php b/lib/CleantalkSP/Common/FSWatcher/View/View.php index 25fe2e355..d41bea5d0 100755 --- a/lib/CleantalkSP/Common/FSWatcher/View/View.php +++ b/lib/CleantalkSP/Common/FSWatcher/View/View.php @@ -27,6 +27,8 @@ public static function renderSelectors() $dates = Controller::$repository::getAvailableJournals(); + $html .= self::manualSnapshotButton(); + if ( ! static::snapshotsAreReady($dates) ) { // Snapshots were not ready, do not render selectors $html .= '

' . __('Snapshots were not ready.', 'security-malware-firewall') . '

'; @@ -59,7 +61,7 @@ public static function renderSelectors() $html .= ''; $html .= '
'; @@ -83,6 +85,15 @@ private static function renderSelectorOptions($dates) return $html; } + private static function manualSnapshotButton() + { + $html = '
'; + $html .= ''; + $html .= '
'; + + return $html; + } + private static function renderTableTemplate() { $html = '
'; diff --git a/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js b/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-logic.js old mode 100755 new mode 100644 similarity index 87% rename from lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js rename to lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-logic.js index 529dc9962..a45dabb00 --- a/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-compare.js +++ b/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-logic.js @@ -34,7 +34,7 @@ function FSWCompare(e) { xhr.send(data.join('&')); xhr.onreadystatechange = function() { if( xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200 ) { - handleXHRResponse(xhr.response); + FSWHandleXHRResponse(xhr.response); } } } @@ -46,11 +46,11 @@ function FSWCompare(e) { * Handle File System Watcher XHR Response * @param {string} response */ -function handleXHRResponse(response) { +function FSWHandleXHRResponse(response) { let responseDataObj = {}; noFSWChangesDetected = true; if (typeof response === 'string') { - responseDataObj = decodeJSON(response) + responseDataObj = FSWDecodeJSON(response) if (responseDataObj.hasOwnProperty('error')) { alert('File System watcher JSON parse error: see console for details.') console.log('File System watcher JSON parse error: ' + responseDataObj.error) @@ -71,11 +71,42 @@ function handleXHRResponse(response) { resetFSWSelectors(); } +function FSWCreate(e) { + e.preventDefault(); + if (typeof fswatcherToken !== 'undefined') { + let xhr = new XMLHttpRequest(); + const button = e.target; + button.disabled = true; + xhr.open("POST", window.location.protocol + '//' + window.location.host); + xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + let data = [ + 'fswatcher_token=' + fswatcherToken, + 'fswatcher_create_snapshot=1' + ]; + xhr.send(data.join('&')); + xhr.onreadystatechange = function() { + if( xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200 ) { + FSWHandleXHRResponseCreate(xhr.response, button); + } + } + } +} + +function FSWHandleXHRResponseCreate(response, button) { + let responseDataObj = {}; + if (typeof response === 'string') { + responseDataObj = FSWDecodeJSON(response); + console.log(responseDataObj); + button.disabled = false; + } + +} + /** * Try to decode JSON string from site response. * @param {string} response */ -function decodeJSON(response) { +function FSWDecodeJSON(response) { try { return JSON.parse(response) } catch (e) { @@ -271,6 +302,7 @@ function toggleFSWSelectorsInfo(enable) { // listeners document.getElementById('fswatcher__compare').addEventListener('click', FSWCompare); +document.getElementById('fswatcher__create_snapshot').addEventListener('click', FSWCreate); firstFSWSelector.addEventListener('change', filterFSWSecondSelector); secondFSWSelector.addEventListener('change', filterFSWFirstSelector); From 6acf3a98a8737491307ae5be9ed55159e29e1ca0 Mon Sep 17 00:00:00 2001 From: Glomberg Date: Wed, 6 Dec 2023 17:41:06 +0300 Subject: [PATCH 19/24] Upd. FS Watcher. Phrases moved to the separated class. --- inc/spbc-settings.php | 4 +- .../Common/FSWatcher/View/Phrases.php | 20 ++++++ .../Common/FSWatcher/View/View.php | 43 ++++++------ .../SpbctWP/FSWatcher/View/Phrases.php | 68 +++++++++++++++++++ 4 files changed, 110 insertions(+), 25 deletions(-) create mode 100644 lib/CleantalkSP/Common/FSWatcher/View/Phrases.php create mode 100644 lib/CleantalkSP/SpbctWP/FSWatcher/View/Phrases.php diff --git a/inc/spbc-settings.php b/inc/spbc-settings.php index 6f8638698..65489d483 100644 --- a/inc/spbc-settings.php +++ b/inc/spbc-settings.php @@ -678,7 +678,7 @@ function spbc_settings__register() 'scanner__fs_watcher' => array( 'type' => 'field', 'title' => __('File System Journal', 'security-malware-firewall'), - 'description' => View::getFSWatcherDescription(), + 'description' => View::getFSWatcherDescription(new \CleantalkSP\SpbctWP\FSWatcher\View\Phrases()), ), ), ), @@ -1605,7 +1605,7 @@ function spbc_tab__summary() function spbc_tab__fswatcher() { echo "
"; - echo \CleantalkSP\Common\FSWatcher\View\View::renderSelectors(); + echo \CleantalkSP\Common\FSWatcher\View\View::renderSelectors(new \CleantalkSP\SpbctWP\FSWatcher\View\Phrases()); echo '
'; } diff --git a/lib/CleantalkSP/Common/FSWatcher/View/Phrases.php b/lib/CleantalkSP/Common/FSWatcher/View/Phrases.php new file mode 100644 index 000000000..7608b5a12 --- /dev/null +++ b/lib/CleantalkSP/Common/FSWatcher/View/Phrases.php @@ -0,0 +1,20 @@ +getDescription(); } - public static function renderSelectors() + public static function renderSelectors(Phrases $phrases) { $html = '
'; $html .= '
'; - $html .= '

File System Journal

'; + $html .= '

' . $phrases->getTitle() . '

'; $html .= '
'; $html .= '
'; - $html .= '

' . self::getFSWatcherDescription() . '

'; + $html .= '

' . self::getFSWatcherDescription($phrases) . '

'; $dates = Controller::$repository::getAvailableJournals(); - $html .= self::manualSnapshotButton(); + $html .= self::manualSnapshotButton($phrases); if ( ! static::snapshotsAreReady($dates) ) { // Snapshots were not ready, do not render selectors - $html .= '

' . __('Snapshots were not ready.', 'security-malware-firewall') . '

'; - $html .= '

' . __('Please wait while FS Journal will be ready to work.', 'security-malware-firewall') . '

'; + $html .= '

' . $phrases->featureNotReady1() . '

'; + $html .= '

' . $phrases->featureNotReady2() . '

'; return $html; } - $html .= '

' . __('To run comparison select dates which you want to compare and click the "Compare" button.', 'security-malware-firewall') . '

'; - $html .= ''; + $html .= '

' . $phrases->getCompareButtonDescription() . '

'; + $html .= ''; $html .= ''; $html .= '
'; $html .= '
'; - $html .= ''; + $html .= ''; $html .= ''; @@ -52,7 +49,7 @@ public static function renderSelectors() $html .= '
'; $html .= '
'; - $html .= ''; + $html .= ''; $html .= '
'; $html .= '
'; @@ -67,7 +64,7 @@ public static function renderSelectors() $html .= '
'; - $html .= self::renderTableTemplate(); + $html .= self::renderTableTemplate($phrases); return $html; } @@ -85,16 +82,16 @@ private static function renderSelectorOptions($dates) return $html; } - private static function manualSnapshotButton() + private static function manualSnapshotButton(Phrases $phrases) { $html = '
'; - $html .= ''; + $html .= ''; $html .= '
'; return $html; } - private static function renderTableTemplate() + private static function renderTableTemplate(Phrases $phrases) { $html = '
'; $html .= ''; @@ -103,13 +100,13 @@ private static function renderTableTemplate() $html .= '
'; $html .= ''; $html .= ''; $html .= ''; $html .= ''; $html .= ''; $html .= ''; @@ -117,7 +114,7 @@ private static function renderTableTemplate() $html .= ''; $html .= ''; $html .= ''; $html .= ''; $html .= ''; diff --git a/lib/CleantalkSP/SpbctWP/FSWatcher/View/Phrases.php b/lib/CleantalkSP/SpbctWP/FSWatcher/View/Phrases.php new file mode 100644 index 000000000..1b2308bac --- /dev/null +++ b/lib/CleantalkSP/SpbctWP/FSWatcher/View/Phrases.php @@ -0,0 +1,68 @@ + Date: Thu, 7 Dec 2023 09:51:19 +0300 Subject: [PATCH 20/24] Upd. FS Watcher. Ajax url fixed. --- lib/CleantalkSP/Common/FSWatcher/Service.php | 1 + lib/CleantalkSP/Common/FSWatcher/assets/fswatcher.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/CleantalkSP/Common/FSWatcher/Service.php b/lib/CleantalkSP/Common/FSWatcher/Service.php index e26349b45..f1f0fa15e 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Service.php +++ b/lib/CleantalkSP/Common/FSWatcher/Service.php @@ -67,6 +67,7 @@ public static function attachJS($buffer) $path = __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'fswatcher.js'; $addition = '' + . '' . ''; $buffer = preg_replace( diff --git a/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher.js b/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher.js index ccdb84851..328a836c5 100755 --- a/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher.js +++ b/lib/CleantalkSP/Common/FSWatcher/assets/fswatcher.js @@ -1,7 +1,7 @@ document.addEventListener('DOMContentLoaded', function() { - if (typeof fswatcherToken !== 'undefined') { + if (typeof fswatcherToken !== 'undefined' && fswatcherWebsiteUrl !== 'undefined') { let xhr = new XMLHttpRequest(); - xhr.open("POST", window.location.protocol + '//' + window.location.host); + xhr.open("POST", fswatcherWebsiteUrl + '/'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send('fswatcher_token=' + fswatcherToken); } From 613d0c305c7787baea84c20e5346fb0cd987da69 Mon Sep 17 00:00:00 2001 From: Glomberg Date: Thu, 7 Dec 2023 09:51:38 +0300 Subject: [PATCH 21/24] Upd. FS Watcher. Layout fixed. --- lib/CleantalkSP/Common/FSWatcher/View/View.php | 4 +++- lib/CleantalkSP/Common/FSWatcher/assets/fswatcher-logic.js | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/CleantalkSP/Common/FSWatcher/View/View.php b/lib/CleantalkSP/Common/FSWatcher/View/View.php index b854ff707..5a751a040 100755 --- a/lib/CleantalkSP/Common/FSWatcher/View/View.php +++ b/lib/CleantalkSP/Common/FSWatcher/View/View.php @@ -30,9 +30,10 @@ public static function renderSelectors(Phrases $phrases) // Snapshots were not ready, do not render selectors $html .= '

' . $phrases->featureNotReady1() . '

'; $html .= '

' . $phrases->featureNotReady2() . '

'; - return $html; } + $display_selectors = static::snapshotsAreReady($dates) ? 'block' : 'none'; + $html .= '
'; $html .= '

' . $phrases->getCompareButtonDescription() . '

'; $html .= ''; $html .= ''; + $html .= parent::renderSelectorOptions($dates); + $html .= ''; + $html .= '
'; + $html .= '
'; + + $html .= ''; + $html .= ''; + $html .= '
'; + $html .= '
'; + + $html .= '
'; + $html .= ''; + $html .= '
'; + $html .= '
'; + $html .= '
'; + + $html .= ''; + + $html .= ''; + $html .= ''; + + $html .= '
'; + + $html .= parent::renderTableTemplate($phrases); + + return $html; + } + + public static function getFSWatcherDescription(Phrases $phrases) + { + return $phrases->getDescription(); + } + + public static function getFSWatcherSnapshotsPeriodDescription(Phrases $phrases) + { + return $phrases->getSnapshotsPeriodDescription(); + } + + public static function getExtendedTabDescription(Phrases $phrases) + { + return $phrases->getExtendedTabDescription(); + } +} diff --git a/lib/CleantalkSP/SpbctWP/State.php b/lib/CleantalkSP/SpbctWP/State.php index 18bc4df21..f282c98e7 100644 --- a/lib/CleantalkSP/SpbctWP/State.php +++ b/lib/CleantalkSP/SpbctWP/State.php @@ -75,6 +75,7 @@ class State extends \CleantalkSP\Common\State 'scanner__auto_start__set_period' => 86400, 'scanner__file_monitoring' => 0, 'scanner__fs_watcher' => 1, + 'scanner__fs_watcher__snapshots_period' => 43200, // Frontend scanner 'scanner__frontend_analysis' => 1, @@ -117,6 +118,7 @@ class State extends \CleantalkSP\Common\State 'admin_bar__admins_online_counter' => 1, 'admin_bar__brute_force_counter' => 1, 'admin_bar__firewall_counter' => 1, + 'admin_bar__fs_watcher' => 1, // Trusted and affiliate settings 'spbc_trusted_and_affiliate__shortcode' => 0, diff --git a/security-malware-firewall.php b/security-malware-firewall.php index 52a293e03..b74c5eca7 100644 --- a/security-malware-firewall.php +++ b/security-malware-firewall.php @@ -12,7 +12,7 @@ */ use CleantalkSP\SpbctWP\Activator; -use CleantalkSP\Common\FSWatcher\Controller as FSWatcherController; +use CleantalkSP\SpbctWP\FSWatcher\SpbctWpFSWController as FSWatcherController; use CleantalkSP\SpbctWP\DB; use CleantalkSP\SpbctWP\Firewall; use CleantalkSP\SpbctWP\Firewall\BFP; From cbb7c63ee4b8a4a33ded1a4a6f5001eede740072 Mon Sep 17 00:00:00 2001 From: alexandergull Date: Wed, 17 Jan 2024 14:54:30 +0500 Subject: [PATCH 24/24] Fix. FSWatcher. Logs path protected. --- lib/CleantalkSP/Common/FSWatcher/Logger.php | 8 ++++++-- .../SpbctWP/FSWatcher/SpbctWpFSWController.php | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/CleantalkSP/Common/FSWatcher/Logger.php b/lib/CleantalkSP/Common/FSWatcher/Logger.php index 47c10ddbd..454be3867 100755 --- a/lib/CleantalkSP/Common/FSWatcher/Logger.php +++ b/lib/CleantalkSP/Common/FSWatcher/Logger.php @@ -10,13 +10,17 @@ private static function getCurrentDayLogPath() { $path = self::getLoggerDir(); return $path - ? $path . DIRECTORY_SEPARATOR . date('Y-m-d') . '.log' + ? $path . DIRECTORY_SEPARATOR . date('Y-m-d') . md5(__FILE__) . '.log' : false; } private static function setLoggerDir() { - return mkdir(self::$logger_dir); + $result = ( + mkdir(self::$logger_dir) && + file_put_contents(self::$logger_dir . DIRECTORY_SEPARATOR . 'index.php', '
'; - $html .= 'Path'; + $html .= $phrases->getTableHeadPath(); $html .= ''; - $html .= 'Event'; + $html .= $phrases->getTableHeadEvent(); $html .= ''; - $html .= 'Changed on date'; + $html .= $phrases->getTableHeadChangeOn(); $html .= '
'; - $html .= 'No logs compared yet.'; + $html .= $phrases->getTableNoLogs(); $html .= '