mirror of
https://github.com/silverstripe/silverstripe-mathspamprotection
synced 2024-10-22 08:05:52 +02:00
commit
1905fa8874
50
.travis.yml
50
.travis.yml
@ -1,36 +1,40 @@
|
|||||||
# See https://github.com/silverstripe-labs/silverstripe-travis-support for setup details
|
|
||||||
|
|
||||||
sudo: false
|
|
||||||
|
|
||||||
language: php
|
language: php
|
||||||
|
|
||||||
php:
|
dist: trusty
|
||||||
- 5.3
|
|
||||||
- 5.4
|
cache:
|
||||||
- 5.5
|
directories:
|
||||||
- 5.6
|
- $HOME/.composer/cache/files
|
||||||
- 7.0
|
|
||||||
|
|
||||||
env:
|
env:
|
||||||
- DB=MYSQL CORE_RELEASE=3.2
|
global:
|
||||||
|
- COMPOSER_ROOT_VERSION=4.0.x-dev
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
- php: 5.6
|
- php: 5.6
|
||||||
env: DB=MYSQL CORE_RELEASE=3
|
env: DB=MYSQL INSTALLER_VERSION=4.0.x-dev PHPCS_TEST=1 PHPUNIT_TEST=1
|
||||||
- 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
|
- php: 7.0
|
||||||
|
env: DB=PGSQL INSTALLER_VERSION=4.1.x-dev PHPUNIT_TEST=1
|
||||||
|
- php: 7.1
|
||||||
|
env: DB=MYSQL INSTALLER_VERSION=4.2.x-dev PHPUNIT_COVERAGE_TEST=1
|
||||||
|
- php: 7.2
|
||||||
|
env: DB=MYSQL INSTALLER_VERSION=4.x-dev PHPUNIT_TEST=1
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- composer self-update || true
|
# Init PHP
|
||||||
- git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support
|
- phpenv rehash
|
||||||
- php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss
|
- phpenv config-rm xdebug.ini
|
||||||
- cd ~/builds/ss
|
|
||||||
- composer install
|
# Install composer dependencies
|
||||||
|
- composer validate
|
||||||
|
- echo $INSTALLER_VERSION
|
||||||
|
- composer require --prefer-source --no-update silverstripe/recipe-cms:1.1.x-dev
|
||||||
|
- if [[ $DB == PGSQL ]]; then composer require --no-update silverstripe/postgresql:2.0.x-dev; fi
|
||||||
|
- composer install --prefer-source --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- vendor/bin/phpunit mathspamprotection/tests
|
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml tests/; fi
|
||||||
|
|
||||||
|
after_success:
|
||||||
|
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then bash <(curl -s https://codecov.io/bash) -f coverage.xml; fi
|
||||||
|
@ -36,7 +36,7 @@ composer require "silverstripe/mathspamprotection:dev-master"
|
|||||||
Set the default spam protector in *mysite/_config/spamprotection.yml*
|
Set the default spam protector in *mysite/_config/spamprotection.yml*
|
||||||
|
|
||||||
---
|
---
|
||||||
name: spamprotection
|
name: myspamprotection
|
||||||
---
|
---
|
||||||
FormSpamProtectionExtension:
|
SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension:
|
||||||
default_spam_protector: MathSpamProtector
|
default_spam_protector: SilverStripe\MathSpamProtection\MathSpamProtector
|
||||||
|
@ -4,6 +4,10 @@
|
|||||||
* @package mathspamprotection
|
* @package mathspamprotection
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
namespace SilverStripe\MathSpamProtection;
|
||||||
|
|
||||||
|
use SilverStripe\SpamProtection\SpamProtector;
|
||||||
|
|
||||||
class MathSpamProtector implements SpamProtector
|
class MathSpamProtector implements SpamProtector
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@ -1,4 +1,10 @@
|
|||||||
<?php
|
<?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.
|
* {@link FormField} for adding an optional maths protection question to a form.
|
||||||
@ -31,7 +37,7 @@ class MathSpamProtectorField extends TextField
|
|||||||
|
|
||||||
public function Field($properties = array())
|
public function Field($properties = array())
|
||||||
{
|
{
|
||||||
if (Config::inst()->get('MathSpamProtectorField', 'enabled')) {
|
if ($this->config()->get('enabled')) {
|
||||||
return parent::Field($properties);
|
return parent::Field($properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,7 +46,7 @@ class MathSpamProtectorField extends TextField
|
|||||||
|
|
||||||
public function FieldHolder($properties = array())
|
public function FieldHolder($properties = array())
|
||||||
{
|
{
|
||||||
if (Config::inst()->get('MathSpamProtectorField', 'enabled')) {
|
if ($this->config()->get('enabled')) {
|
||||||
return parent::FieldHolder($properties);
|
return parent::FieldHolder($properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +60,7 @@ class MathSpamProtectorField extends TextField
|
|||||||
*/
|
*/
|
||||||
public function Title()
|
public function Title()
|
||||||
{
|
{
|
||||||
$prefix = Config::inst()->get('MathSpamProtection', 'question_prefix');
|
$prefix = $this->config()->get('question_prefix');
|
||||||
|
|
||||||
if (!$prefix) {
|
if (!$prefix) {
|
||||||
$prefix = _t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s");
|
$prefix = _t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s");
|
||||||
@ -62,7 +68,7 @@ class MathSpamProtectorField extends TextField
|
|||||||
|
|
||||||
return sprintf(
|
return sprintf(
|
||||||
$prefix,
|
$prefix,
|
||||||
self::get_math_question()
|
$this->getMathsQuestion()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,11 +81,11 @@ class MathSpamProtectorField extends TextField
|
|||||||
*/
|
*/
|
||||||
public function validate($validator)
|
public function validate($validator)
|
||||||
{
|
{
|
||||||
if (!Config::inst()->get('MathSpamProtectorField', 'enabled')) {
|
if (!$this->config()->get( 'enabled')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!self::correct_answer($this->Value())) {
|
if (!$this->isCorrectAnswer($this->Value())) {
|
||||||
$validator->validationError(
|
$validator->validationError(
|
||||||
$this->name,
|
$this->name,
|
||||||
_t(
|
_t(
|
||||||
@ -101,17 +107,20 @@ class MathSpamProtectorField extends TextField
|
|||||||
*
|
*
|
||||||
* @return string
|
* @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);
|
$v1 = rand(1, 9);
|
||||||
$v2 = rand(1, 9);
|
$v2 = rand(1, 9);
|
||||||
|
|
||||||
Session::set("mathQuestionV1", $v1);
|
$session->set("mathQuestionV1", $v1);
|
||||||
Session::set("mathQuestionV2", $v2);
|
$session->set("mathQuestionV2", $v2);
|
||||||
} else {
|
} else {
|
||||||
$v1 = Session::get("mathQuestionV1");
|
$v1 = $session->get("mathQuestionV1");
|
||||||
$v2 = Session::get("mathQuestionV2");
|
$v2 = $session->get("mathQuestionV2");
|
||||||
}
|
}
|
||||||
|
|
||||||
return sprintf(
|
return sprintf(
|
||||||
@ -129,17 +138,20 @@ class MathSpamProtectorField extends TextField
|
|||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function correct_answer($answer)
|
public function isCorrectAnswer($answer)
|
||||||
{
|
{
|
||||||
$v1 = Session::get("mathQuestionV1");
|
|
||||||
$v2 = Session::get("mathQuestionV2");
|
|
||||||
|
|
||||||
Session::clear('mathQuestionV1');
|
$session = Controller::curr()->getRequest()->getSession();
|
||||||
Session::clear('mathQuestionV2');
|
|
||||||
|
$v1 = $session->get("mathQuestionV1");
|
||||||
|
$v2 = $session->get("mathQuestionV2");
|
||||||
|
|
||||||
|
$session->clear('mathQuestionV1');
|
||||||
|
$session->clear('mathQuestionV2');
|
||||||
|
|
||||||
$word = MathSpamProtectorField::digit_to_word($v1 + $v2);
|
$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) == $answer)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "silverstripe/mathspamprotection",
|
"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.",
|
"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",
|
"type": "silverstripe-vendormodule",
|
||||||
"keywords": ["silverstripe", "spamprotection", "mathspamprotection"],
|
"keywords": ["silverstripe", "spamprotection", "mathspamprotection"],
|
||||||
"authors": [{
|
"authors": [{
|
||||||
"name": "Will Rossiter",
|
"name": "Will Rossiter",
|
||||||
@ -9,7 +9,12 @@
|
|||||||
}],
|
}],
|
||||||
"minimum-stability": "dev",
|
"minimum-stability": "dev",
|
||||||
"require": {
|
"require": {
|
||||||
"silverstripe/spamprotection": "^2"
|
"silverstripe/spamprotection": "^3"
|
||||||
},
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"SilverStripe\\MathSpamProtection\\": "code/"
|
||||||
|
}
|
||||||
|
},
|
||||||
"license": "BSD-3-Clause"
|
"license": "BSD-3-Clause"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user