Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions inc/spbc-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2978,6 +2978,7 @@ function spbc_field_scanner()

echo
'<div id="spbc_scaner_progress_overall" class="spbc_hide" style="padding-bottom: 10px; text-align: center;">'
. '<span class="spbc_overall_scan_status_self_check">' . __('Self check', 'security-malware-firewall') . '</span> -> '
. '<span class="spbc_overall_scan_status_get_cms_hashes">' . __('Receiving core hashes', 'security-malware-firewall') . '</span> -> '
. '<span class="spbc_overall_scan_status_get_modules_hashes">' . __('Receiving plugin and theme hashes', 'security-malware-firewall') . '</span> -> '
. '<span class="spbc_overall_scan_status_clean_results">' . __('Preparing', 'security-malware-firewall') . '</span> -> '
Expand Down
10 changes: 10 additions & 0 deletions lib/CleantalkSP/SpbctWP/Scanner/ScannerQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use CleantalkSP\SpbctWP\Helpers\Helper as QueueHelper;
use CleantalkSP\SpbctWP\Helpers\CSV;
use CleantalkSP\SpbctWP\RemoteCalls;
use CleantalkSP\SpbctWP\Scanner\Stages\SelfCheckStage;
use CleantalkSP\SpbctWP\Scanner\Stages\SendResultsStage;

class ScannerQueue
Expand All @@ -39,6 +40,7 @@ class ScannerQueue
* @var string[] List of scan stages
*/
public static $stages = array(
'self_check',
'get_cms_hashes',
'get_modules_hashes',
'clean_results',
Expand Down Expand Up @@ -306,6 +308,14 @@ public static function controllerFront()
wp_send_json($out);
}

public function self_check() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
$selfCheck = new SelfCheckStage();
$selfCheck->run();

return ['end' => true];
}

/**
* Receive CMS hash
*
Expand Down
185 changes: 185 additions & 0 deletions lib/CleantalkSP/SpbctWP/Scanner/Stages/SelfCheckStage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php

namespace CleantalkSP\SpbctWP\Scanner\Stages;

class SelfCheckStage
{
/**
* @var string
*/
private $url = '';

/**
* @var int
*/
private $seal = 0;


/**
* @var string
*/
private $dir = SPBC_PLUGIN_DIR;

/**
* Set url and seal
*/
public function __construct()
{
$this->url = $this->setUrl();
$this->seal = $this->calculateSeal();
}

/**
* Run self check and cure
*/
public function run()
{
$check = $this->check();

if (count($check) > 0) {
// TODO: Send to Cleantalk
// TODO: log

// $this->sendToCleantalk($check);
// $this->writeLog($check);
$this->cure($check);
}

return true;
}

/**
* Check files
*/
private function check()
{
$files = $this->getFiles('check');

foreach ($files as $key) {
if ($key === $this->seal) {
unset($files[$key]);
}
}

return $files;
}

/**
* Set url
*/
private function setUrl()
{
$url = 'https://plugins.svn.wordpress.org/security-malware-firewall/tags/';
$version = preg_replace('/[^\d.]/', '', SPBC_VERSION);
$url .= $version . '/';

$response = wp_remote_get($url . 'security-malware-firewall.php');
$code = wp_remote_retrieve_response_code($response);
if ($code !== 200) {
$url = 'https://plugins.svn.wordpress.org/security-malware-firewall/trunk/';
}

return $url;
}

/**
* Calculate seal
*/
private function calculateSeal()
{
$files = $this->getFiles('calculate');

$frequency = array_count_values($files);
arsort($frequency);

reset($frequency);
return key($frequency);
}

/**
* Get files
*/
private function getFiles($type)
{
$result = [];

$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->dir, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST,
\RecursiveIteratorIterator::CATCH_GET_CHILD
);

foreach ($iterator as $fileinfo) {
if ($fileinfo->getFilename() === '.' || $fileinfo->getFilename() === '..') {
continue;
}

if ($fileinfo->getExtension() !== 'php') {
continue;
}

if ($fileinfo->getFilename() === 'SelfCheck.php') {
continue;
}

if (strpos($fileinfo->getPathname(), $this->dir . 'vendor') !== false) {
continue;
}

if (strpos($fileinfo->getPathname(), $this->dir . 'node_modules') !== false) {
continue;
}

if (strpos($fileinfo->getPathname(), $this->dir . 'backups') !== false) {
continue;
}

if ($type === 'calculate') {
$result[] = $fileinfo->getMTime();
continue;
}

if ($type === 'check') {
$result[$fileinfo->getMTime()] = str_replace($this->dir, '', $fileinfo->getPathname());
continue;
}
}

return $result;
}

/**
* Cure
*/
private function cure($check)
{
foreach ($check as $file) {
$response = wp_remote_get($this->url . $file);
$code = wp_remote_retrieve_response_code($response);

if ($code !== 200) {
// TODO: alient file, handle it
continue;
}

if (is_wp_error($response)) {
// TODO: handle error
continue;
}

$body = wp_remote_retrieve_body($response);
if (empty($body)) {
// TODO: handle error
continue;
}

$success = file_put_contents($this->dir . $file, $body);
if (!$success) {
// TODO: handle error
continue;
}

// TODO: log
}
}
}