Skip to content
Open
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
],
"require": {
"cleantalk/spbct-heuristic-analyser": "*",
"cleantalk/spbct-signatures-analyser": "*"
"cleantalk/spbct-signatures-analyser": "*",
"cleantalk/spbct-helpers": "dev-dev-logger.ab"
},
"require-dev": {
"vimeo/psalm": "^4.8",
Expand Down
5 changes: 3 additions & 2 deletions inc/spbc-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use CleantalkSP\SpbctWP\Escape;
use CleantalkSP\SpbctWP\Scanner\DBTrigger\DBTriggerService;
use CleantalkSP\SpbctWP\Settings\SettingsGeneralReact;
use CleantalkSP\SpbctWP\SpbcDevLogger;
use CleantalkSP\SpbctWP\VulnerabilityAlarm\Dto\PluginReport;
use CleantalkSP\SpbctWP\Firewall\View as FirewallView;
use CleantalkSP\SpbctWP\VulnerabilityAlarm\Dto\ThemeReport;
Expand Down Expand Up @@ -602,7 +603,7 @@ function spbc_admin_log_action()
try {
spbc_write_timer($secure_cookies);
} catch (Exception $e) {
error_log($e->getMessage());
SpbcDevLogger::write($e->getMessage());
}
}

Expand All @@ -629,7 +630,7 @@ function spbc_admin_log_action()
try {
spbc_set_secure_cookies($cookies_arr);
} catch (Exception $e) {
error_log($e->getMessage());
SpbcDevLogger::write($e->getMessage());
}
}

Expand Down
30 changes: 18 additions & 12 deletions lib/CleantalkSP/Common/Enqueue/Enqueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use CleantalkSP\Templates\Singleton;

class Enqueue
abstract class Enqueue
{
use Singleton;

Expand Down Expand Up @@ -83,12 +83,11 @@ private function load($data)
throw new \Exception(__('Unknown type of asset.', 'security-malware-firewall'));
}
} catch (\Exception $e) {
$this->errorLog($e->getMessage());
$this->errorAdd($e->getMessage());
}
// output errors to error_log
// output errors to static::errorWrite
if ($this->hasErrors()) {
error_log(__CLASS__ . __(' errors:', 'security-malware-firewall'));
error_log(implode("\n", $this->errors));
static::errorWrite(__CLASS__ . ' errors:' . "\n" . implode("\n", $this->errors));
}
}

Expand Down Expand Up @@ -155,7 +154,7 @@ public function custom($handle, $asset_file_name, $deps, $version, $args, $media
)
);
} catch (\Exception $e) {
$this->errorLog($e->getMessage());
$this->errorAdd($e->getMessage());
$data = null;
}
$this->load($data);
Expand Down Expand Up @@ -194,7 +193,7 @@ private function prepareData($asset_file_name, $deps, $args, $media)
)
);
} catch (\Exception $e) {
$this->errorLog($e->getMessage());
$this->errorAdd($e->getMessage());
}
return null;
}
Expand Down Expand Up @@ -270,13 +269,13 @@ private function getFreshVersion($work_script_name)
*/
private function validateWebPath($path)
{
if (! preg_match('/^https?:\/\//', $path)) {
$this->errorLog(__('Web path for script is invalid: ' . $path, 'security-malware-firewall'));
if (!preg_match('/^https?:\/\//', $path)) {
$this->errorAdd(__('Web path for script is invalid: ' . $path, 'security-malware-firewall'));
return $path;
}
$abs_path = str_replace($this->assets_path, $this->plugin_path, $path);
if (!@file_exists($abs_path)) {
$this->errorLog(__('Script file is not accessible:' . $path, 'security-malware-firewall'));
$this->errorAdd(__('Script file is not accessible:' . $path, 'security-malware-firewall'));
return $path;
}
return $path;
Expand All @@ -287,7 +286,7 @@ private function validateWebPath($path)
* @param $message
* @return void
*/
private function errorLog($message)
private function errorAdd($message)
{
$this->errors[] = $message;
}
Expand All @@ -300,7 +299,7 @@ public function isAllHandlesQueued()
{
foreach ($this->handles_to_register as $handle) {
if (!wp_script_is($handle, 'queue') && !wp_style_is($handle, 'queue')) {
$this->errorLog(__('Script is not queued: ' . $handle, 'security-malware-firewall'));
$this->errorAdd(__('Script is not queued: ' . $handle, 'security-malware-firewall'));
return false;
}
}
Expand All @@ -315,4 +314,11 @@ public function hasErrors()
{
return !empty($this->errors);
}

/**
* @param string $error
*
* @return void
*/
abstract public static function errorWrite($error);
}
31 changes: 31 additions & 0 deletions lib/CleantalkSP/Common/Helpers/DevLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace CleantalkSP\Common\Helpers;

/**
* Class DevLogger
* Base logger class intended to be extended.
* Child classes should override the write() method to implement custom logging behavior.
*
* @package CleantalkSP\Common\Helpers
* @author Cleantalk team (welcome@cleantalk.org)
* @copyright (C) CleanTalk team (http://cleantalk.org)
* @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
* @see https://github.com/CleanTalk/security-malware-firewall
*/
class DevLogger
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a new class? We have already implemented almost the same

{
public static function write($msg, $context = null)
{
if ( ! is_string($msg) ) {
return;
}
if ( ! function_exists('error_log') ) {
return;
}
/**
* @psalm-suppress ForbiddenCode
*/
error_log($msg);
}
Comment thread
Glomberg marked this conversation as resolved.
}
2 changes: 1 addition & 1 deletion lib/CleantalkSP/Common/Helpers/IP.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ public static function isIpv6AddrInIpv6Network($ip, $network, $mask)
return false;
}
} else {
error_log('Security by CleanTalk: PHP insctance is compiled with no inet_pton() IPv6 support.');
DevLogger::write('Security by CleanTalk: PHP instance is compiled with no inet_pton() IPv6 support.');
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CleantalkSP\SpbctWP\AdjustToEnvironmentModule\AdjustToEnv;

use CleantalkSP\SpbctWP\SpbcDevLogger;

class AdjustToEnvW3TotalCache extends AdjustToEnvAbstract
{
public function __construct($info)
Expand Down Expand Up @@ -99,7 +101,10 @@ class_exists('\W3TC\Dispatcher')
$original_config = \W3TC\Dispatcher::config();
$state = (bool)$original_config->get('pgcache.cache.query');
} catch (\Exception $e) {
error_log('Security by CleanTalk error: ' . __METHOD__ . ' ' . $e->getMessage());
SpbcDevLogger::write(
'W3TC env probing failed: ' . $e->getMessage(),
['class' => __CLASS__, 'method' => __FUNCTION__]
);
}
}
return $state;
Expand Down Expand Up @@ -133,7 +138,10 @@ class_exists('\W3TC\Cdnfsd_CacheFlush')
$this->changed = !$state;
$this->keepEnvChangesByModule();
} catch (\Exception $e) {
error_log('Security by CleanTalk error: ' . __METHOD__ . ' ' . $e->getMessage());
SpbcDevLogger::write(
'W3TC config update failed: ' . $e->getMessage(),
['class' => __CLASS__, 'method' => __FUNCTION__]
);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/CleantalkSP/SpbctWP/Deactivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CleantalkSP\SpbctWP\Variables\Cookie;
use CleantalkSP\SpbctWP\UploadDirPreventPhpExecutionModule\UploadDirPreventPhpExecution;
use CleantalkSP\SpbctWP\SpbcDevLogger;

class Deactivator
{
Expand Down Expand Up @@ -230,7 +231,7 @@ public static function deactivation($is_network, $do_log_on_errors = false, $is_
}

if ( $do_log_on_errors && !self::$deactivation_result ) {
error_log("Security by CleanTalk deactivation log: \r\n" . var_export(self::getLog(), true));
SpbcDevLogger::write("Security by CleanTalk deactivation log: \r\n" . var_export(self::getLog(), true));
}

// Removing the role of an external technical specialist
Expand Down
10 changes: 8 additions & 2 deletions lib/CleantalkSP/SpbctWP/FSWatcher/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CleantalkSP\SpbctWP\FSWatcher;

use CleantalkSP\SpbctWP\SpbcDevLogger;

class Logger
{
private static $logger_dir = __DIR__ . DIRECTORY_SEPARATOR . 'logs';
Expand Down Expand Up @@ -43,7 +45,7 @@ public static function log($msg)
$current_day_log_path = self::getCurrentDayLogPath();

if ( !$current_day_log_path ) {
error_log('Cant write log.');
SpbcDevLogger::write('Can\'t write log.');
return;
}

Expand All @@ -64,7 +66,11 @@ public static function log($msg)
}
}

error_log($message, 3, $current_day_log_path);
$result = @file_put_contents($current_day_log_path, $message . PHP_EOL, FILE_APPEND | LOCK_EX);

if ($result === false) {
SpbcDevLogger::write('Failed to write to log file: ' . $current_day_log_path);
}
}

protected static function generateLogHash()
Expand Down
3 changes: 2 additions & 1 deletion lib/CleantalkSP/SpbctWP/Firewall/FW.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CleantalkSP\SpbctWP\API;
use CleantalkSP\SpbctWP\DB;
use CleantalkSP\SpbctWP\SpbcDevLogger;
use CleantalkSP\Variables\Get;
use CleantalkSP\Variables\Server;
use CleantalkSP\Security\Firewall\Result;
Expand Down Expand Up @@ -73,7 +74,7 @@ public function check()
throw new \Exception('IP address record is invalid.');
}
} catch (\Exception $e) {
error_log('Security by CleanTalk. Firewall IP handling error: ' . $e->getMessage());
SpbcDevLogger::write('Security by CleanTalk. Firewall IP handling error: ' . $e->getMessage());
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/CleantalkSP/SpbctWP/Scanner/FrontendScan.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public function setContent($content = '')
try {
@$this->dom->loadHTML($this->content);
} catch (\Error $e) { // phpcs:ignore PHPCompatibility.Classes.NewClasses.errorFound
error_log($e->getMessage());
// Malformed HTML is expected during frontend scans — no point logging it.
}

return $this;
Expand Down
19 changes: 9 additions & 10 deletions lib/CleantalkSP/SpbctWP/Scanner/ScannerQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use CleantalkSP\SpbctWP\Scanner\Stages\SendResultsStage;
use CleantalkSP\SpbctWP\Scanner\Stages\SignatureAnalysis\SignatureAnalysisFacade;
use CleantalkSP\SpbctWP\Scanner\UnsafePermissionsModule\UnsafePermissionsHandler;
use CleantalkSP\SpbctWP\SpbcDevLogger;
use CleantalkSP\SpbctWP\State;
use CleantalkSP\SpbctWP\Transaction;
use CleantalkSP\Variables\Request;
Expand Down Expand Up @@ -603,7 +604,7 @@ public function get_modules_hashes($amount = null, $offset = null) // phpcs:igno
array('slug' => $module_slug, 'fields' => array('Version' => true))
);
} catch (\Exception $e) {
error_log('Security Scanner - plugins_api exception for ' . $module_slug . ': ' . $e->getMessage());
SpbcDevLogger::write('Security Scanner - plugins_api exception for ' . $module_slug . ': ' . $e->getMessage());
$result_wp_api_modules = null;
}
if ($prev_error_handler !== null) {
Expand All @@ -612,7 +613,7 @@ public function get_modules_hashes($amount = null, $offset = null) // phpcs:igno
restore_error_handler();
}
if ($php_errormsg) {
error_log('Security Scanner - plugins_api error for ' . $module_slug . ': ' . $php_errormsg);
SpbcDevLogger::write('Security Scanner - plugins_api error for ' . $module_slug . ': ' . $php_errormsg);
$result_wp_api_modules = null;
}
if ($stored_plugins_api_filters !== null) {
Expand Down Expand Up @@ -644,7 +645,7 @@ public function get_modules_hashes($amount = null, $offset = null) // phpcs:igno
$wp_filter['themes_api'] = $stored_filters;
}
} catch (\Exception $e) {
error_log('Security Scanner - Theme API error for ' . $module_slug . ': ' . $e->getMessage());
SpbcDevLogger::write('Security Scanner - Theme API error for ' . $module_slug . ': ' . $e->getMessage());
$result_wp_api_modules = new \WP_Error('themes_api_failed', 'Theme API call failed');
}
}
Expand Down Expand Up @@ -851,11 +852,9 @@ public function countFilesByStatusAndChecked($status = '', $caller = '')
{
$status = stripslashes(Request::getString('status')) ?: $status;
if ( Request::getString('checked') ) {
error_log(
'countFilesByStatusAndChecked: $_GET[\'checked\'] parameter found, but not handled in the method ' . var_export(
$_GET,
true
)
SpbcDevLogger::write(
'countFilesByStatusAndChecked: $_GET[\'checked\'] parameter found, but not handled in the method',
['get' => $_GET]
);
Comment thread
Glomberg marked this conversation as resolved.
}

Expand Down Expand Up @@ -1432,7 +1431,7 @@ public function signature_analysis($status = 'UNKNOWN,MODIFIED,OK,INFECTED,ERROR
$spbc->data['scanner']['scanned_total'] = 0;
$total = $this->countFilesByStatusAndChecked($status_raw, 'SIGNATURE_ANALYSIS');
if ( ! isset($total['total']) ) {
error_log('countFilesByStatusAndChecked: ' . $total['error'] . ' ' . $total['comment']);
SpbcDevLogger::write('countFilesByStatusAndChecked: ' . $total['error'] . ' ' . $total['comment']);
}
$total = $total['total'];
$stage_data_obj->set('total_count_files_for_analysis', $total);
Expand Down Expand Up @@ -1633,7 +1632,7 @@ public function heuristic_analysis($status = 'UNKNOWN,MODIFIED,OK,INFECTED,ERROR
if ( $offset === 0 ) {
$total = $this->countFilesByStatusAndChecked($status_raw, 'HEURISTIC_ANALYSIS');
if ( ! isset($total['total']) ) {
error_log('countFilesByStatusAndChecked() ERROR: ' . $total['error'] . ' ' . $total['comment']);
SpbcDevLogger::write('countFilesByStatusAndChecked() ERROR: ' . $total['error'] . ' ' . $total['comment']);
}
$total = $total['total'];
$stage_data_obj->set('total_count_files_for_analysis', $total);
Expand Down
4 changes: 3 additions & 1 deletion lib/CleantalkSP/SpbctWP/Scanner/Surface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CleantalkSP\SpbctWP\Scanner;

use CleantalkSP\SpbctWP\SpbcDevLogger;

class Surface
{
/**
Expand Down Expand Up @@ -695,7 +697,7 @@ public function getFileStructure($main_path, $iterator_result, $is_root_dir)
}
$this->dirs[]['path'] = $path;
} elseif ( is_link($path) ) {
error_log('LINK FOUND: ' . $path);
SpbcDevLogger::write('LINK FOUND: ' . $path);
}
Comment thread
Glomberg marked this conversation as resolved.
}
// foreach is finished - iterator completed with no interrupts, save the dir to the completed set
Expand Down
Loading
Loading