From 221089136f82f778f4f2281630cbe0a28618a402 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 12 Sep 2023 09:45:39 +1200 Subject: [PATCH] Revert "fix: if no spam protector set, fail sliently" This reverts commit 17106d8f30bfb2327a47e20c2b7b7cd21c29f423. --- .gitignore | 6 +---- README.md | 15 ++++++++---- {src => code}/EditableSpamProtectionField.php | 24 +++++++------------ .../Extension/CommentSpamProtection.php | 12 +++++----- .../Extension/FormSpamProtectionExtension.php | 10 ++++---- {src => code}/SpamProtector.php | 0 composer.json | 20 +++++++++------- phpcs.xml.dist | 2 +- phpunit.xml.dist | 2 +- 9 files changed, 45 insertions(+), 46 deletions(-) rename {src => code}/EditableSpamProtectionField.php (97%) rename {src => code}/Extension/CommentSpamProtection.php (80%) rename {src => code}/Extension/FormSpamProtectionExtension.php (92%) rename {src => code}/SpamProtector.php (100%) diff --git a/.gitignore b/.gitignore index dd02fd3..496ee2c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1 @@ -.DS_Store -/vendor -/composer.lock -/public -.phpunit.result.cache +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index c609db5..6ade41b 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,14 @@ composer require silverstripe/spamprotection ``` +## Maintainer Contact + + * Saophalkun Ponlu + + + * Will Rossiter + + ## Documentation This module provides a generic, consistent API for adding spam protection to @@ -32,7 +40,7 @@ need to rebuild your database through `dev/build` and set the default protector via SilverStripe's config system. This will update any Form instances that have spam protection hooks with that protector. -*app/_config/spamprotection.yml* +*mysite/_config/spamprotection.yml* ```yaml --- @@ -59,10 +67,10 @@ implementation client side or server side. `enableSpamProtection` takes a hash of optional configuration values. ```php -$form->enableSpamProtection([ +$form->enableSpamProtection(array( 'protector' => MathSpamProtector::class, 'name' => 'Captcha' -]); +)); ``` Options to configure are: @@ -113,7 +121,6 @@ class CustomSpamProtector implements SpamProtector } ``` - ## Using Spam Protection with User Forms This module provides an `EditableSpamProtectionField` wrapper which you can add diff --git a/src/EditableSpamProtectionField.php b/code/EditableSpamProtectionField.php similarity index 97% rename from src/EditableSpamProtectionField.php rename to code/EditableSpamProtectionField.php index 7c16851..dfbdf41 100644 --- a/src/EditableSpamProtectionField.php +++ b/code/EditableSpamProtectionField.php @@ -3,6 +3,7 @@ namespace SilverStripe\SpamProtection; use SilverStripe\Core\ClassInfo; +use SilverStripe\Core\Convert; use SilverStripe\Core\Manifest\ModuleLoader; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\FieldGroup; @@ -40,15 +41,15 @@ class EditableSpamProtectionField extends EditableFormField * @var array * @config */ - private static $check_fields = [ + private static $check_fields = array( EditableEmailField::class, EditableTextField::class, EditableNumericField::class - ]; + ); - private static $db = [ + private static $db = array( 'SpamFieldSettings' => 'Text' - ]; + ); /** * @var FormField @@ -68,14 +69,12 @@ class EditableSpamProtectionField extends EditableFormField } // Extract saved field mappings and update this field. - $fieldMapping = []; - + $fieldMapping = array(); foreach ($this->getCandidateFields() as $otherField) { $mapSetting = "Map-{$otherField->Name}"; $spamField = $this->spamMapValue($mapSetting); $fieldMapping[$otherField->Name] = $spamField; } - $protector->setFieldMapping($fieldMapping); // Generate field @@ -107,8 +106,7 @@ class EditableSpamProtectionField extends EditableFormField // Get list of all configured classes available for spam detection $types = $this->config()->get('check_fields'); - $typesInherit = []; - + $typesInherit = array(); foreach ($types as $type) { $subTypes = ClassInfo::subclassesFor($type); $typesInherit = array_merge($typesInherit, $subTypes); @@ -133,9 +131,8 @@ class EditableSpamProtectionField extends EditableFormField public function onBeforeWrite() { $fieldMap = json_decode($this->SpamFieldSettings ?? '', true); - if (empty($fieldMap)) { - $fieldMap = []; + $fieldMap = array(); } foreach ($this->record as $key => $value) { @@ -205,7 +202,7 @@ class EditableSpamProtectionField extends EditableFormField { $map = json_decode($this->SpamFieldSettings ?? '', true); if (empty($map)) { - $map = []; + $map = array(); } if (array_key_exists($mapSetting, $map ?? [])) { @@ -258,19 +255,16 @@ class EditableSpamProtectionField extends EditableFormField } } - public function getFieldValidationOptions() { return FieldList::create(); } - public function getRequired() { return false; } - public function getIcon() { $resource = ModuleLoader::getModule('silverstripe/spamprotection') diff --git a/src/Extension/CommentSpamProtection.php b/code/Extension/CommentSpamProtection.php similarity index 80% rename from src/Extension/CommentSpamProtection.php rename to code/Extension/CommentSpamProtection.php index fc5f8c9..f01e44c 100644 --- a/src/Extension/CommentSpamProtection.php +++ b/code/Extension/CommentSpamProtection.php @@ -14,19 +14,19 @@ class CommentSpamProtection extends Extension { public function alterCommentForm(&$form) { - $form->enableSpamProtection([ + $form->enableSpamProtection(array( 'name' => 'IsSpam', - 'mapping' => [ + 'mapping' => array( 'Name' => 'authorName', 'Email' => 'authorEmail', 'URL' => 'authorUrl', 'Comment' => 'body', 'ReturnURL' => 'contextUrl' - ], - 'checks' => [ + ), + 'checks' => array( 'spam', 'profanity' - ] - ]); + ) + )); } } diff --git a/src/Extension/FormSpamProtectionExtension.php b/code/Extension/FormSpamProtectionExtension.php similarity index 92% rename from src/Extension/FormSpamProtectionExtension.php rename to code/Extension/FormSpamProtectionExtension.php index 9a125fe..3ff2422 100644 --- a/src/Extension/FormSpamProtectionExtension.php +++ b/code/Extension/FormSpamProtectionExtension.php @@ -6,7 +6,6 @@ use LogicException; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Extension; use SilverStripe\Core\Injector\Injector; -use SilverStripe\View\Requirements; /** * An extension to the {@link Form} class which provides the method @@ -38,7 +37,7 @@ class FormSpamProtectionExtension extends Extension * * @var array $mappable_fields */ - private static $mappable_fields = [ + private static $mappable_fields = array( 'id', 'title', 'body', @@ -49,7 +48,7 @@ class FormSpamProtectionExtension extends Extension 'authorUrl', 'authorIp', 'authorId' - ]; + ); /** * @config @@ -89,7 +88,7 @@ class FormSpamProtectionExtension extends Extension * @throws LogicException when get_protector method returns NULL. * @return Object */ - public function enableSpamProtection($options = []) + public function enableSpamProtection($options = array()) { // captcha form field name (must be unique) @@ -110,8 +109,7 @@ class FormSpamProtectionExtension extends Extension $protector = self::get_protector($options); if ($protector === null) { - Requirements::customScript('console.error("No spam protector has been set on this form.")'); - return $this->owner; + throw new LogicException('No spam protector has been set. Null is not valid value.'); } if ($protector && isset($options['mapping'])) { diff --git a/src/SpamProtector.php b/code/SpamProtector.php similarity index 100% rename from src/SpamProtector.php rename to code/SpamProtector.php diff --git a/composer.json b/composer.json index 35b9552..13d1072 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,16 @@ "silverstripe", "spamprotection" ], + "authors": [ + { + "name": "Saophalkun Ponlu", + "email": "phalkunz@silverstripe.com" + }, + { + "name": "Will Rossiter", + "email": "will@fullscreen.io" + } + ], "require": { "php": "^8.1", "silverstripe/framework": "^5" @@ -23,17 +33,11 @@ }, "autoload": { "psr-4": { - "SilverStripe\\SpamProtection\\": "src/", + "SilverStripe\\SpamProtection\\": "code/", "SilverStripe\\SpamProtection\\Tests\\": "tests/" } }, "license": "BSD-3-Clause", "minimum-stability": "dev", - "prefer-stable": true, - "config": { - "allow-plugins": { - "composer/installers": true, - "silverstripe/vendor-plugin": true - } - } + "prefer-stable": true } diff --git a/phpcs.xml.dist b/phpcs.xml.dist index f3047d8..d76bc90 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -2,7 +2,7 @@ CodeSniffer ruleset for SilverStripe coding conventions. - src + code tests diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 57c7cdd..794aa0f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,7 +8,7 @@ - src/ + code/ tests/