Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "fix: if no spam protector set, fail sliently" #104

Merged
merged 1 commit into from
Sep 11, 2023
Merged
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
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
.DS_Store
/vendor
/composer.lock
/public
.phpunit.result.cache
.DS_Store
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
composer require silverstripe/spamprotection
```

## Maintainer Contact

* Saophalkun Ponlu
<phalkunz (at) silverstripe (dot) com>

* Will Rossiter
<will (at) fullscreen (dot) io>

## Documentation

This module provides a generic, consistent API for adding spam protection to
Expand All @@ -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
---
Expand All @@ -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:
Expand Down Expand Up @@ -113,7 +121,6 @@ class CustomSpamProtector implements SpamProtector
}
```


## Using Spam Protection with User Forms

This module provides an `EditableSpamProtectionField` wrapper which you can add
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -68,14 +69,12 @@ public function getFormField()
}

// 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
Expand Down Expand Up @@ -107,8 +106,7 @@ protected function getCandidateFields()

// 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);
Expand All @@ -133,9 +131,8 @@ protected function getCandidateFields()
public function onBeforeWrite()
{
$fieldMap = json_decode($this->SpamFieldSettings ?? '', true);

if (empty($fieldMap)) {
$fieldMap = [];
$fieldMap = array();
}

foreach ($this->record as $key => $value) {
Expand Down Expand Up @@ -205,7 +202,7 @@ public function spamMapValue($mapSetting)
{
$map = json_decode($this->SpamFieldSettings ?? '', true);
if (empty($map)) {
$map = [];
$map = array();
}

if (array_key_exists($mapSetting, $map ?? [])) {
Expand Down Expand Up @@ -258,19 +255,16 @@ public function validateField($data, $form)
}
}


public function getFieldValidationOptions()
{
return FieldList::create();
}


public function getRequired()
{
return false;
}


public function getIcon()
{
$resource = ModuleLoader::getModule('silverstripe/spamprotection')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
]);
)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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
Expand Down Expand Up @@ -38,7 +37,7 @@ class FormSpamProtectionExtension extends Extension
*
* @var array $mappable_fields
*/
private static $mappable_fields = [
private static $mappable_fields = array(
'id',
'title',
'body',
Expand All @@ -49,7 +48,7 @@ class FormSpamProtectionExtension extends Extension
'authorUrl',
'authorIp',
'authorId'
];
);

/**
* @config
Expand Down Expand Up @@ -89,7 +88,7 @@ public static function get_protector($options = null)
* @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)
Expand All @@ -110,8 +109,7 @@ public function enableSpamProtection($options = [])
$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'])) {
Expand Down
File renamed without changes.
20 changes: 12 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
"silverstripe",
"spamprotection"
],
"authors": [
{
"name": "Saophalkun Ponlu",
"email": "[email protected]"
},
{
"name": "Will Rossiter",
"email": "[email protected]"
}
],
"require": {
"php": "^8.1",
"silverstripe/framework": "^5"
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>

<file>src</file>
<file>code</file>
<file>tests</file>

<rule ref="PSR2" >
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<directory suffix=".php">code/</directory>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
Expand Down