silverstripe / spamprotection
Spam protection module for SilverStripe.
Installs: 807 386
Dependents: 44
Suggesters: 4
Security: 0
Stars: 43
Watchers: 16
Forks: 37
Open Issues: 3
Type:silverstripe-vendormodule
Requires
- php: ^8.1
- silverstripe/framework: ^5
Requires (Dev)
- 5.x-dev
- 4.x-dev
- 4.2.x-dev
- 4.2.1
- 4.2.0
- 4.2.0-rc1
- 4.2.0-beta1
- 4.1.x-dev
- 4.1.1
- 4.1.0
- 4.1.0-beta1
- 4.0.x-dev
- 4.0.2
- 4.0.1
- 4.0.0
- 4.0.0-rc1
- 4.0.0-beta1
- 3.x-dev
- 3.4.x-dev
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.4.0-rc1
- 3.4.0-beta1
- 3.3.x-dev
- 3.3.0
- 3.3.0-rc1
- 3.3.0-beta1
- 3.2.x-dev
- 3.2.1
- 3.2.0
- 3.2.0-rc1
- 3.2.0-beta1
- 3.1.x-dev
- 3.1.1
- 3.1.0
- 3.1.0-rc1
- 3.1.0-beta1
- 3.0.x-dev
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 3.0.0-beta1
- 2.0.x-dev
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.0
- dev-master
This package is auto-updated.
Last update: 2024-10-26 05:43:59 UTC
README
Installation
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 your Silverstripe Forms. This does not provide any spam protection out of the box. For that, you must also download one of the spam protection implementations. Currently available options are:
- reCAPTCHA v2 (two implementations: one, two)
- MathSpamProtection
- Akismet
- Mollom
- Cloudflare Turnstile
As a developer you can also provide your own protector by creating a class which
implements the \SilverStripe\SpamProtection\SpamProtector
interface. More on that below.
Configuring
After installing this module and a protector of your choice (i.e mollom) you'll
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.
mysite/_config/spamprotection.yml
--- name: mycustomspamprotection --- SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension: default_spam_protector: MollomSpamProtector
To add spam protection to your form instance call enableSpamProtection
.
// your existing form code $form = new Form(/* .. */); $form->enableSpamProtection();
The logic to perform the actual spam validation is controlled by each of the
individual SpamProtector
implementations since they each require a different
implementation client side or server side.
Options
enableSpamProtection
takes a hash of optional configuration values.
$form->enableSpamProtection(array( 'protector' => MathSpamProtector::class, 'name' => 'Captcha' ));
Options to configure are:
-
protector
: a class name string or class instance which implements\SilverStripe\SpamProtection\SpamProtector
. Defaults to yourSilverStripe\SpamProtection\Extension\FormSpamProtectionExtension.default_spam_protector
value. -
name
: the form field name argument for the Captcha. Defaults toCaptcha
. -
title
: title of the Captcha form field. Defaults to''
-
insertBefore
: name of existing field to insert the spam protection field prior to -
mapping
: an array mapping of the Form fields to the standardised list of field names. The list of standardised fields to pass to the spam protector are:
title
body
contextUrl
contextTitle
authorName
authorMail
authorUrl
authorIp
authorId
Defining your own SpamProtector
Any class that implements \SilverStripe\SpamProtection\SpamProtector
and the getFormField()
method can
be set as the spam protector. The getFormField()
method returns the
FormField
to be inserted into the Form
. The FormField
returned should be
in charge of the validation process.
<?php use CaptchaField; use SilverStripe\SpamProtection\SpamProtector; class CustomSpamProtector implements SpamProtector { public function getFormField($name = null, $title = null, $value = null) { // CaptchaField is an imagined class which has some functionality. // See silverstripe-mollom module for an example. return new CaptchaField($name, $title, $value); } }
Using Spam Protection with User Forms
This module provides an EditableSpamProtectionField
wrapper which you can add
to your UserForm instances. After installing this module and running /dev/build
to rebuild the database, your Form Builder interface will have an option for
Spam Protection Field
. The type of spam protection used will be based on your
currently selected SpamProtector instance.
Releasing code with Spam Protection support
Spam protection is useful to provide but in some cases we do not want to require the developer to use spam protection. In that case, modules can provide the following pattern:
use SilverStripe\Forms\Form; use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension; $form = new Form(/* .. */); if ($form->hasExtension(FormSpamProtectionExtension::class)) { $form->enableSpamProtection(); }