Compare commits
27 Commits
Author | SHA1 | Date |
---|---|---|
Will Rossiter | 320e746647 | |
Will Rossiter | 68ad231dfd | |
Juanitou | 3615453cdc | |
Will Rossiter | 5dbb469c4c | |
Will Rossiter | 750b03b232 | |
Will Rossiter | cf19fe40b5 | |
Will Rossiter | ad36c19cbd | |
Tech-Maou | ce700643f5 | |
Robbie Averill | 570eec324c | |
Juanitou | 6819fac12e | |
Robbie Averill | ba0a3f51dc | |
Robbie Averill | 1905fa8874 | |
Gordon Anderson | 65fd149c72 | |
Gordon Anderson | 82d37954bb | |
Gordon Anderson | aaa4de1e95 | |
Gordon Anderson | 01b19b980c | |
Gordon Anderson | a2e6bc422d | |
Gordon Anderson | d8768c2e21 | |
Gordon Anderson | 9313f18f02 | |
Gordon Anderson | c8084968c5 | |
Gordon Anderson | fb23959f60 | |
Gordon Anderson | 7398064cd5 | |
Gordon Anderson | 83b3fc22ad | |
Gordon Anderson | bd4defe793 | |
Gordon Anderson | af1693b7ea | |
Gordon Anderson | c20bc1701f | |
Gordon Anderson | 0c96d86131 |
78
.travis.yml
78
.travis.yml
|
@ -1,36 +1,48 @@
|
|||
# See https://github.com/silverstripe-labs/silverstripe-travis-support for setup details
|
||||
version: ~> 1.0
|
||||
|
||||
sudo: false
|
||||
# Use a manual matrix as composer.json requires "silverstripe/cms": "^4.6",
|
||||
# This should be changed to "standard-jobs-range-npm" as soon as the lowest recipe
|
||||
# version in the travis shared jobs matrix is 4.6
|
||||
import:
|
||||
- silverstripe/silverstripe-travis-shared:config/provision/standard.yml
|
||||
|
||||
language: php
|
||||
|
||||
php:
|
||||
- 5.3
|
||||
- 5.4
|
||||
- 5.5
|
||||
- 5.6
|
||||
- 7.0
|
||||
|
||||
env:
|
||||
- DB=MYSQL CORE_RELEASE=3.2
|
||||
|
||||
matrix:
|
||||
jobs:
|
||||
include:
|
||||
- php: 5.6
|
||||
env: DB=MYSQL CORE_RELEASE=3
|
||||
- php: 5.6
|
||||
env: DB=MYSQL CORE_RELEASE=3.1
|
||||
- php: 5.6
|
||||
env: DB=PGSQL CORE_RELEASE=3.2
|
||||
allow_failures:
|
||||
- php: 7.0
|
||||
|
||||
before_script:
|
||||
- composer self-update || true
|
||||
- git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support
|
||||
- php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss
|
||||
- cd ~/builds/ss
|
||||
- composer install
|
||||
|
||||
script:
|
||||
- vendor/bin/phpunit mathspamprotection/tests
|
||||
- php: 7.4
|
||||
env:
|
||||
- DB=MYSQL
|
||||
- REQUIRE_INSTALLER="4.6.x-dev"
|
||||
- NPM_TEST=1
|
||||
- php: 7.1
|
||||
env:
|
||||
- DB=MYSQL
|
||||
- REQUIRE_INSTALLER="4.6.x-dev"
|
||||
- PHPUNIT_TEST=1
|
||||
- php: 7.2
|
||||
env:
|
||||
- DB=PGSQL
|
||||
- REQUIRE_INSTALLER="4.6.x-dev"
|
||||
- PHPUNIT_TEST=1
|
||||
- php: 7.3
|
||||
env:
|
||||
- DB=MYSQL
|
||||
- REQUIRE_INSTALLER="4.7.x-dev"
|
||||
- PHPUNIT_TEST=1
|
||||
- PHPCS_TEST=1
|
||||
- php: 7.3
|
||||
env:
|
||||
- DB=MYSQL
|
||||
- REQUIRE_INSTALLER="4.7.x-dev"
|
||||
- BEHAT_TEST=1
|
||||
- BEHAT_SUITE="userforms"
|
||||
- php: 7.4
|
||||
env:
|
||||
- DB=MYSQL
|
||||
- REQUIRE_INSTALLER="4.x-dev"
|
||||
- PHPUNIT_COVERAGE_TEST=1
|
||||
- php: nightly
|
||||
env:
|
||||
- DB=MYSQL
|
||||
- REQUIRE_INSTALLER="4.x-dev"
|
||||
- PHPUNIT_TEST=1
|
||||
- COMPOSER_INSTALL_ARG=--ignore-platform-reqs
|
||||
|
|
17
README.md
17
README.md
|
@ -1,12 +1,5 @@
|
|||
# Math Spam Protection
|
||||
|
||||
## Maintainers
|
||||
|
||||
* Will Rossiter (Nickname: willr, wrossiter)
|
||||
<will at fullscreen dot io>
|
||||
|
||||
## Introduction
|
||||
|
||||
This module provides a simple math protection mechanism for prevent spam on your
|
||||
forms. It will ask the user to complete an equation such as "three plus seven".
|
||||
|
||||
|
@ -17,7 +10,7 @@ Includes an EditableMathSpamField to integrate with the UserForms module.
|
|||
|
||||
## Requirements
|
||||
|
||||
* SilverStripe 3.1
|
||||
* SilverStripe 4
|
||||
* Spam Protection
|
||||
|
||||
## Install Spam Protection Module
|
||||
|
@ -36,7 +29,7 @@ composer require "silverstripe/mathspamprotection:dev-master"
|
|||
Set the default spam protector in *mysite/_config/spamprotection.yml*
|
||||
|
||||
---
|
||||
name: spamprotection
|
||||
---
|
||||
FormSpamProtectionExtension:
|
||||
default_spam_protector: MathSpamProtector
|
||||
name: myspamprotection
|
||||
---
|
||||
SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension:
|
||||
default_spam_protector: SilverStripe\MathSpamProtection\MathSpamProtector
|
||||
|
|
|
@ -1,15 +1,31 @@
|
|||
{
|
||||
"name": "silverstripe/mathspamprotection",
|
||||
"description": "This module provides a simple math protection mechanism for prevent spam from your forms.Includes an EditableMathSpamField to integrate with the UserForms module.",
|
||||
"type": "silverstripe-module",
|
||||
"keywords": ["silverstripe", "spamprotection", "mathspamprotection"],
|
||||
"authors": [{
|
||||
"name": "Will Rossiter",
|
||||
"email": "will@fullscreen.io"
|
||||
}],
|
||||
"minimum-stability": "dev",
|
||||
"require": {
|
||||
"silverstripe/spamprotection": "^2"
|
||||
},
|
||||
"license": "BSD-3-Clause"
|
||||
"name": "silverstripe/mathspamprotection",
|
||||
"description": "This module provides a simple math protection mechanism for prevent spam from your forms.Includes an EditableMathSpamField to integrate with the UserForms module.",
|
||||
"type": "silverstripe-vendormodule",
|
||||
"keywords": [
|
||||
"silverstripe",
|
||||
"spamprotection",
|
||||
"mathspamprotection"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Will Rossiter",
|
||||
"email": "will@fullscreen.io"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "dev",
|
||||
"require": {
|
||||
"silverstripe/spamprotection": "^3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"SilverStripe\\MathSpamProtection\\": "src/"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "3.x-dev"
|
||||
}
|
||||
},
|
||||
"license": "BSD-3-Clause"
|
||||
}
|
||||
|
|
|
@ -12,14 +12,14 @@ es:
|
|||
SEVEN: siete
|
||||
SEVENTEEN: diecisiete
|
||||
SIX: seis
|
||||
SIXTEEN: 'dieciséis'
|
||||
SIXTEEN: 'dieciséis'
|
||||
TEN: diez
|
||||
THIRTEEN: trece
|
||||
THREE: tres
|
||||
TWELVE: doce
|
||||
TWO: dos
|
||||
WHATIS: '¿Cuánto suman %s y %s?'
|
||||
WHATIS: '¿Cuánto suman %s y %s?'
|
||||
ZERO: cero
|
||||
MathSpamProtectionField:
|
||||
INCORRECTSOLUTION: 'Respuesta errónea, inténtelo de nuevo.'
|
||||
SPAMQUESTION: 'Pregunta anti-spam: %s'
|
||||
INCORRECTSOLUTION: 'Respuesta errónea, inténtelo de nuevo.'
|
||||
SPAMQUESTION: 'Pregunta anti-spam: %s'
|
||||
|
|
10
lang/fr.yml
10
lang/fr.yml
|
@ -10,7 +10,7 @@ fr:
|
|||
NINE: neuf
|
||||
ONE: un
|
||||
SEVEN: sept
|
||||
SEVENTEEN: seventeen
|
||||
SEVENTEEN: dix-sept
|
||||
SIX: six
|
||||
SIXTEEN: seize
|
||||
TEN: dix
|
||||
|
@ -18,8 +18,8 @@ fr:
|
|||
THREE: trois
|
||||
TWELVE: douze
|
||||
TWO: deux
|
||||
WHATIS: 'Combien font %s plus %s ?'
|
||||
ZERO: zéro
|
||||
WHATIS: 'Combien font %s plus %s ?'
|
||||
ZERO: zéro
|
||||
MathSpamProtectionField:
|
||||
INCORRECTSOLUTION: 'Mauvaise réponse, essayez à nouveau.'
|
||||
SPAMQUESTION: 'Question anti-spam : %s'
|
||||
INCORRECTSOLUTION: 'Mauvaise réponse, essayez à nouveau.'
|
||||
SPAMQUESTION: 'Question anti-spam : %s'
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
Copyright (c) 2016, SilverStripe Limited
|
||||
Copyright (c) 2021, SilverStripe Limited
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
|
|
@ -4,6 +4,10 @@
|
|||
* @package mathspamprotection
|
||||
*/
|
||||
|
||||
namespace SilverStripe\MathSpamProtection;
|
||||
|
||||
use SilverStripe\SpamProtection\SpamProtector;
|
||||
|
||||
class MathSpamProtector implements SpamProtector
|
||||
{
|
||||
/**
|
|
@ -1,5 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace SilverStripe\MathSpamProtection;
|
||||
|
||||
use SilverStripe\Control\Controller;
|
||||
use SilverStripe\Core\Config\Config;
|
||||
use SilverStripe\Control\Session;
|
||||
use SilverStripe\Forms\TextField;
|
||||
|
||||
/**
|
||||
* {@link FormField} for adding an optional maths protection question to a form.
|
||||
*
|
||||
|
@ -21,17 +28,17 @@ class MathSpamProtectorField extends TextField
|
|||
* @var string
|
||||
*/
|
||||
private static $question_prefix;
|
||||
|
||||
|
||||
/**
|
||||
* @config
|
||||
*
|
||||
* @var bool $allow_numeric_answer
|
||||
*/
|
||||
private static $allow_numeric_answer = true;
|
||||
|
||||
|
||||
public function Field($properties = array())
|
||||
{
|
||||
if (Config::inst()->get('MathSpamProtectorField', 'enabled')) {
|
||||
if ($this->config()->get('enabled')) {
|
||||
return parent::Field($properties);
|
||||
}
|
||||
|
||||
|
@ -40,7 +47,7 @@ class MathSpamProtectorField extends TextField
|
|||
|
||||
public function FieldHolder($properties = array())
|
||||
{
|
||||
if (Config::inst()->get('MathSpamProtectorField', 'enabled')) {
|
||||
if ($this->config()->get('enabled')) {
|
||||
return parent::FieldHolder($properties);
|
||||
}
|
||||
|
||||
|
@ -54,7 +61,7 @@ class MathSpamProtectorField extends TextField
|
|||
*/
|
||||
public function Title()
|
||||
{
|
||||
$prefix = Config::inst()->get('MathSpamProtection', 'question_prefix');
|
||||
$prefix = $this->config()->get('question_prefix');
|
||||
|
||||
if (!$prefix) {
|
||||
$prefix = _t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s");
|
||||
|
@ -62,24 +69,30 @@ class MathSpamProtectorField extends TextField
|
|||
|
||||
return sprintf(
|
||||
$prefix,
|
||||
self::get_math_question()
|
||||
$this->getMathsQuestion()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the value submitted by the user with the one saved into the
|
||||
* {@link Session} and then notify callback object with the spam checking
|
||||
* Validates the value submitted by the user with the one saved into the
|
||||
* {@link Session} and then notify callback object with the spam checking
|
||||
* result.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validate($validator)
|
||||
{
|
||||
if (!Config::inst()->get('MathSpamProtectorField', 'enabled')) {
|
||||
if (!$this->config()->get( 'enabled')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!self::correct_answer($this->Value())) {
|
||||
$value = Controller::curr()->getRequest()->postVar($this->getName());
|
||||
|
||||
if (!$value) {
|
||||
$value = $this->Value();
|
||||
}
|
||||
|
||||
if (!$this->isCorrectAnswer($value)) {
|
||||
$validator->validationError(
|
||||
$this->name,
|
||||
_t(
|
||||
|
@ -101,17 +114,20 @@ class MathSpamProtectorField extends TextField
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_math_question()
|
||||
public function getMathsQuestion()
|
||||
{
|
||||
if (!Session::get("mathQuestionV1") && !Session::get("mathQuestionV2")) {
|
||||
/** @var Session $session */
|
||||
$session = Controller::curr()->getRequest()->getSession();
|
||||
|
||||
if (!$session->get("mathQuestionV1") && !$session->get("mathQuestionV2")) {
|
||||
$v1 = rand(1, 9);
|
||||
$v2 = rand(1, 9);
|
||||
|
||||
Session::set("mathQuestionV1", $v1);
|
||||
Session::set("mathQuestionV2", $v2);
|
||||
$session->set("mathQuestionV1", $v1);
|
||||
$session->set("mathQuestionV2", $v2);
|
||||
} else {
|
||||
$v1 = Session::get("mathQuestionV1");
|
||||
$v2 = Session::get("mathQuestionV2");
|
||||
$v1 = $session->get("mathQuestionV1");
|
||||
$v2 = $session->get("mathQuestionV2");
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
|
@ -122,24 +138,26 @@ class MathSpamProtectorField extends TextField
|
|||
}
|
||||
|
||||
/**
|
||||
* Checks the given answer if it matches the addition of the saved session
|
||||
* Checks the given answer if it matches the addition of the saved session
|
||||
* variables.
|
||||
*
|
||||
* Users can answer using words or digits.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function correct_answer($answer)
|
||||
public function isCorrectAnswer($answer)
|
||||
{
|
||||
$v1 = Session::get("mathQuestionV1");
|
||||
$v2 = Session::get("mathQuestionV2");
|
||||
$session = Controller::curr()->getRequest()->getSession();
|
||||
|
||||
Session::clear('mathQuestionV1');
|
||||
Session::clear('mathQuestionV2');
|
||||
$v1 = $session->get("mathQuestionV1");
|
||||
$v2 = $session->get("mathQuestionV2");
|
||||
|
||||
$session->clear('mathQuestionV1');
|
||||
$session->clear('mathQuestionV2');
|
||||
|
||||
$word = MathSpamProtectorField::digit_to_word($v1 + $v2);
|
||||
|
||||
return ($word == strtolower($answer) || (Config::inst()->get('MathSpamProtectorField', 'allow_numeric_answer') && (($v1 + $v2) == $answer)));
|
||||
return ($word == strtolower($answer) || ($this->config()->get('allow_numeric_answer') && (($v1 + $v2) === intval($answer))));
|
||||
}
|
||||
|
||||
/**
|
Loading…
Reference in New Issue