Compare commits

...

27 Commits

Author SHA1 Message Date
Will Rossiter 320e746647
Correct version 2021-10-07 06:49:28 +13:00
Will Rossiter 68ad231dfd
Merge pull request #54 from Juanitou/pr
Actually enforce strict numeric comparison for answer
2021-10-07 06:48:01 +13:00
Juanitou 3615453cdc Actually enforce strict numeric comparison for answer 2021-10-06 16:09:42 +02:00
Will Rossiter 5dbb469c4c
Add upgrade.yml 2021-04-01 08:59:18 +13:00
Will Rossiter 750b03b232
Update travis, fix #42, #32 2021-04-01 08:57:39 +13:00
Will Rossiter cf19fe40b5
Format 2021-04-01 08:53:28 +13:00
Will Rossiter ad36c19cbd
Merge pull request #52 from Tech-Maou/patch-1
Update for strict comparison with ===
2021-04-01 08:48:42 +13:00
Tech-Maou ce700643f5
Update for strict comparison with ===
problem: loose comparison with == -> Null = "0"
solved: strict comparison with === -> Null = false
new session with $v1 = 0 and $v2 = 0 did lead to true answer with actually no answer given (Null -> "0")
 0 + 0 = 0 -> true
ref: https://www.php.net/manual/en/types.comparisons.php
2021-03-31 17:18:39 +02:00
Robbie Averill 570eec324c
Merge pull request #50 from Juanitou/lang
Update ES and FR lang files
2018-06-29 09:15:27 +12:00
Juanitou 6819fac12e Update ES and FR lang files 2018-06-28 15:28:35 +02:00
Robbie Averill ba0a3f51dc
Add branch alias for dev-master 2018-05-25 09:32:19 +12:00
Robbie Averill 1905fa8874
Merge pull request #48 from gordonbanderson/upgradess4
SS4 Upgrade
2018-04-23 08:57:06 +12:00
Gordon Anderson 65fd149c72 FIX: Docs were incorrect post namespace change 2018-04-20 14:35:28 +07:00
Gordon Anderson 82d37954bb FIX: Change namespace as requested 2018-04-20 14:19:30 +07:00
Gordon Anderson aaa4de1e95 FIX: Remove redundant lines 2018-04-20 14:19:15 +07:00
Gordon Anderson 01b19b980c MINOR: Remove debug 2018-04-20 11:53:26 +07:00
Gordon Anderson a2e6bc422d WIP: Debugging travis, just confirming no tests exist 2018-04-20 11:49:42 +07:00
Gordon Anderson d8768c2e21 FIX: Lack of trailing slashes on namespace 2018-04-20 11:33:30 +07:00
Gordon Anderson 9313f18f02 WIP: Upgrade of Travis file 2018-04-20 11:18:27 +07:00
Gordon Anderson c8084968c5 FIX: Fixes suggested in PR, mostly minor 2018-04-20 11:13:48 +07:00
Gordon Anderson fb23959f60 FIX: Correct version of parent spam protection module 2018-04-20 11:13:33 +07:00
Gordon Anderson 7398064cd5 FIX: Config and session 2018-04-20 10:18:25 +07:00
Gordon Anderson 83b3fc22ad FIX: Namespace as a sub namespace of the spam protection module 2018-04-19 22:07:50 +07:00
Gordon Anderson bd4defe793 MINOR: IDE whitespace tidy 2018-04-19 22:07:24 +07:00
Gordon Anderson af1693b7ea FIX: Missing use statement 2018-04-19 22:06:41 +07:00
Gordon Anderson c20bc1701f FIX: Namespace in documentation 2018-04-19 22:06:16 +07:00
Gordon Anderson 0c96d86131 UPGRADE: Namespace fixes after running upgrade tool 2018-04-19 20:26:38 +07:00
9 changed files with 134 additions and 91 deletions

View File

@ -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

0
.upgrade.yml Normal file
View File

View File

@ -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

View File

@ -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"
}

View File

@ -12,14 +12,14 @@ es:
SEVEN: siete
SEVENTEEN: diecisiete
SIX: seis
SIXTEEN: 'diecis&eacute;is'
SIXTEEN: 'dieciséis'
TEN: diez
THIRTEEN: trece
THREE: tres
TWELVE: doce
TWO: dos
WHATIS: '&iquest;Cu&aacute;nto suman %s y %s?'
WHATIS: '¿Cuánto suman %s y %s?'
ZERO: cero
MathSpamProtectionField:
INCORRECTSOLUTION: 'Respuesta err&oacute;nea, int&eacute;ntelo de nuevo.'
SPAMQUESTION: 'Pregunta anti-spam: %s'
INCORRECTSOLUTION: 'Respuesta errónea, inténtelo de nuevo.'
SPAMQUESTION: 'Pregunta anti-spam: %s'

View File

@ -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&nbsp?'
ZERO: z&eacute;ro
WHATIS: 'Combien font %s plus %s ?'
ZERO: zéro
MathSpamProtectionField:
INCORRECTSOLUTION: 'Mauvaise r&eacute;ponse, essayez &agrave; nouveau.'
SPAMQUESTION: 'Question anti-spam&nbsp;: %s'
INCORRECTSOLUTION: 'Mauvaise réponse, essayez à nouveau.'
SPAMQUESTION: 'Question anti-spam : %s'

View File

@ -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:

View File

@ -4,6 +4,10 @@
* @package mathspamprotection
*/
namespace SilverStripe\MathSpamProtection;
use SilverStripe\SpamProtection\SpamProtector;
class MathSpamProtector implements SpamProtector
{
/**

View File

@ -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))));
}
/**