Skip to content

Commit

Permalink
Merge pull request #1 from daniellienert/feature/also-detect-if-spamf…
Browse files Browse the repository at this point in the history
…ield-missing

FEATURE: Also detect if the spam field is missing
  • Loading branch information
daniellienert authored Oct 16, 2019
2 parents aaf92dd + 433899d commit ca70020
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
24 changes: 21 additions & 3 deletions Classes/Finishers/SpamDetectionFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class SpamDetectionFinisher extends AbstractFinisher
*/
protected $cancelSubsequentFinishersOnSpamDetection;

/**
* @Flow\InjectConfiguration(path="logSpamFormData")
* @var bool
*/
protected $logSpamFormData;

/**
* @Flow\Inject
* @var LoggerInterface
Expand All @@ -45,17 +51,29 @@ protected function executeInternal()

$isSpam = false;
$filledOutHoneypotFields = [];

$nullHoneypotFields = [];

foreach ($honeypotFieldIdentifiers as $honeypotFieldIdentifier) {
if (isset($fieldValues[$honeypotFieldIdentifier]) && (string)$fieldValues[$honeypotFieldIdentifier] !== '') {
if ($fieldValues[$honeypotFieldIdentifier] === null) {
$isSpam = true;
$nullHoneypotFields[] = $honeypotFieldIdentifier;
}

if ((string)$fieldValues[$honeypotFieldIdentifier] !== '') {
$isSpam = true;
$filledOutHoneypotFields[] = $honeypotFieldIdentifier;
}
}

if ($isSpam) {
$formRuntime->getFormState()->setFormValue('spamDetected', $isSpam);
$this->logger->info(sprintf('The submitted form was detected as spam, as the honeypot form field %s was filled.', implode(', ', $filledOutHoneypotFields)), LogEnvironment::fromMethodName(__METHOD__));

$additionalData = [];
if ($this->logSpamFormData) {
$additionalData['formData'] = $fieldValues;
}

$this->logger->info(sprintf('The submitted form was detected as spam, as the honeypot form fields was filled [%s] or null [%s]', implode(', ', $filledOutHoneypotFields), implode(', ', $nullHoneypotFields)), array_merge($additionalData, LogEnvironment::fromMethodName(__METHOD__)));

$formRuntime->getFormState()->setFormValue('spamMarker', '[SPAM]');
$formRuntime->getFormState()->setFormValue('spamFilledOutHoneypotFields', implode(', ', $filledOutHoneypotFields));
Expand Down
5 changes: 5 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ Neos:

DL:
HoneypotFormField:

# If set to true, all finishers that are configured after the SpamDetectionFinisher are skipped.
cancelSubsequentFinishersOnSpamDetection: false

# Log the form data to the system log when classified as spam.
logSpamFormData: false

0 comments on commit ca70020

Please sign in to comment.