Skip to content

Commit

Permalink
chore: add debug logging to rules handling and honeypot precheck
Browse files Browse the repository at this point in the history
  • Loading branch information
florianbrinkmann committed Dec 15, 2023
1 parent 0b4f8e7 commit adb250e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion antispam_bee.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Plugin Name: Antispam Bee
* Plugin URI: https://antispambee.pluginkollektiv.org/
* Description: Antispam plugin with a sophisticated toolset for effective day-to-day comment and trackback spam-fighting. Built with data protection and privacy in mind.
* Version: 3.0.0-alpha.9
* Version: 3.0.0-alpha.10
* Author: pluginkollektiv
* Author URI: https://pluginkollektiv.org
* Text Domain: antispam-bee
Expand Down
12 changes: 12 additions & 0 deletions src/Handlers/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AntispamBee\Handlers;

use AntispamBee\Helpers\ComponentsHelper;
use AntispamBee\Helpers\DebugMode;
use AntispamBee\Interfaces\Controllable;
use AntispamBee\Interfaces\SpamReason;
use AntispamBee\Interfaces\Verifiable;
Expand All @@ -24,8 +25,15 @@ public function apply( $item ) {
$spam_threshold = (float) apply_filters( 'antispam_bee_spam_threshold', 0.0 );

$score = 0.0;

DebugMode::log( 'Looping through spam rules for reaction with the following data: ' . print_r( $item, true ) );

foreach ( $rules as $rule ) {
DebugMode::log( "Checking »{$rule::get_name()}« rule" );

$rule_score = $rule::verify( $item ) * $rule::get_weight();

DebugMode::log( "Score: {$rule_score}" );

if ( $rule_score > 0.0 ) {
$this->spam_reasons[] = $rule::get_slug();
Expand All @@ -34,8 +42,12 @@ public function apply( $item ) {
}

$score += $rule_score;

DebugMode::log( "Overall score after checking the rule: {$score}" );
}

DebugMode::log( "Overall score after checking all rules: {$score}" );

if ( $no_spam_threshold < 0.0 && $score <= $no_spam_threshold ) {
return false;
}
Expand Down
25 changes: 25 additions & 0 deletions src/Helpers/DebugMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace AntispamBee\Helpers;

class DebugMode {
protected static $debug_mode_enabled = null;

public static function enabled() {
if ( static::$debug_mode_enabled === null ) {
static::$debug_mode_enabled = defined( 'ANTISPAM_BEE_DEBUG_MODE_ENABLED' ) ? \ANTISPAM_BEE_DEBUG_MODE_ENABLED : false;
}

return static::$debug_mode_enabled;
}

public static function log( string $message ) {
if ( ! static::enabled() ) {
return;
}

$date = date( 'Y-m-d' );
$content_dir = \WP_CONTENT_DIR;
error_log( "Antispam Bee – $message\n", 3, "{$content_dir}/asb-debug.{$date}.log" );
}
}
21 changes: 10 additions & 11 deletions src/Rules/Honeypot.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use AntispamBee\Helpers\Honeypot as HoneypotField;
use AntispamBee\Helpers\ContentTypeHelper;
use AntispamBee\Helpers\DataHelper;
use AntispamBee\Helpers\DebugMode;
use AntispamBee\Helpers\Settings;
use AntispamBee\Interfaces\SpamReason;

Expand Down Expand Up @@ -49,14 +50,7 @@ public static function precheck() {
if ( is_feed() || is_trackback() || empty( $_POST ) ) {
return;
}

/**
* @todo errors from my error log (Flo).
* [13-Jun-2023 11:36:50 UTC] PHP Warning: Undefined array key "hidden_field" in /html/wp-content/plugins/antispam-bee/src/Rules/Honeypot.php on line 69
[13-Jun-2023 11:36:50 UTC] PHP Warning: Undefined array key "hidden_field" in /html/wp-content/plugins/antispam-bee/src/Rules/Honeypot.php on line 73
[13-Jun-2023 11:36:50 UTC] PHP Warning: Undefined array key "plugin_field" in /html/wp-content/plugins/antispam-bee/src/Rules/Honeypot.php on line 73
[13-Jun-2023 11:36:50 UTC] PHP Warning: Undefined array key "" in /html/wp-content/plugins/antispam-bee/src/Rules/Honeypot.php on line 73
*/

$request_uri = Settings::get_key( $_SERVER, 'REQUEST_URI' );
$request_path = DataHelper::parse_url( $request_uri, 'path' );

Expand All @@ -65,14 +59,19 @@ public static function precheck() {
}
$fields = [];
foreach ( $_POST as $key => $value ) {
if ( $key === HoneypotField::get_secret_name_for_post() ) {
$fields['plugin_field'] = $key;
}
if ( isset( $fields['plugin_field'] ) ) {
$fields['hidden_field'] = $key;
break;
}
if ( $key === HoneypotField::get_secret_name_for_post() ) {
$fields['plugin_field'] = $key;
}
}

if ( ! isset( $fields['plugin_field'] ) ) {
DebugMode::log( 'Missing `plugin_field` key in HoneyPot precheck method for the following $_POST data: ' . print_r( $_POST, true ) );
}

if ( ! empty( $_POST[ $fields['hidden_field'] ] ) ) {
$_POST['ab_spam__hidden_field'] = 1;
} else {
Expand Down

0 comments on commit adb250e

Please sign in to comment.