Cleanup to work with latest master spam, fix validation

This commit is contained in:
Will Rossiter 2014-02-10 21:25:27 +13:00
parent 481c99881d
commit 2d29d309f6
8 changed files with 71 additions and 108 deletions

View File

@ -7,16 +7,33 @@
## Introduction ## Introduction
This module provides a simple math protection mechanism for prevent spam from your forms. 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 1 + 1.
Includes an EditableMathSpamField to integrate with the UserForms module. Includes an EditableMathSpamField to integrate with the UserForms module.
## Requirements ## Requirements
* SilverStripe 3.0 * SilverStripe 3.1
* Spam Protection Master * Spam Protection
## Installation ## Install Spam Protection Module
See docs/en/Installing.md The Spam Protection Module (http://silverstripe.org/spam-protection-module)
provides the basic interface for managing the spam protection so first you need
to install that module.
If you're using composer..
```
composer require "silverstripe/spamprotection:dev-master"
composer require "silverstripe/mathspamprotection:dev-master"
```
Set the default spam protector in *mysite/_config/spamprotection.yml*
---
name: spamprotection
---
FormSpamProtectionExtension:
default_spam_protector: MathSpamProtector

10
TODO.md
View File

@ -1,10 +0,0 @@
# TODO
## Blockers
* Built in validation doesn't work any more.
## Medium
## Minor

View File

@ -1,5 +0,0 @@
<?php
/**
* Default _config file for MathSpamProtection module.
*/

0
_config/.gitkeep Normal file
View File

View File

@ -11,17 +11,8 @@ class MathSpamProtector implements SpamProtector {
* *
* @return MathSpamProtectorField * @return MathSpamProtectorField
*/ */
public function getFormField($name = null, $title = null, $value = null, $form = null, $rightTitle = null) { public function getFormField($name = null, $title = null, $value = null) {
return new MathSpamProtectorField($name, $title, $value, $form, $rightTitle); return new MathSpamProtectorField($name, $title, $value);
} }
/**
* Function required to handle dynamic feedback of the system.
* if unneeded just return true
*
* @return true
*/
public function sendFeedback($object = null, $feedback = "") {
return true;
}
} }

View File

@ -6,33 +6,29 @@
* @package mathspamprotection * @package mathspamprotection
*/ */
class MathSpamProtectorField extends SpamProtectorField { class MathSpamProtectorField extends TextField {
/** /**
* @var bool If MathSpamProtection is enabled * @config
*
* @var bool $enabled
*/ */
private static $enabled = true; private static $enabled = true;
/** public function Field($properties = array()) {
* Outputs the field HTML to the the web browser if(Config::inst()->get('MathSpamProtectorField', 'enabled')) {
* return parent::Field($properties);
* @return HTML
*/
function Field($properties = array()) {
if(self::is_enabled()) {
$attributes = array(
'type' => 'text',
'class' => 'text ' . ($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id(),
'name' => $this->getName(),
'value' => $this->Value(),
'title' => $this->Title(),
'tabindex' => $this->getAttribute('tabindex'),
'maxlength' => ($this->maxLength) ? $this->maxLength : null,
'size' => ($this->maxLength) ? min( $this->maxLength, 30 ) : null
);
return $this->createTag('input', $attributes);
} }
return null;
}
public function FieldHolder($properties = array()) {
if(Config::inst()->get('MathSpamProtectorField', 'enabled')) {
return parent::FieldHolder($properties);
}
return null;
} }
/** /**
@ -40,21 +36,26 @@ class MathSpamProtectorField extends SpamProtectorField {
* *
* @return string * @return string
*/ */
function Title() { public function Title() {
return sprintf(_t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s"), self::get_math_question()); return sprintf(
_t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s"),
self::get_math_question()
);
} }
/** /**
* Validates the value submitted by the user with the one saved * Validates the value submitted by the user with the one saved into the
* into the {@link Session} and then notify callback object * {@link Session} and then notify callback object with the spam checking
* with the spam checking result. * result.
* *
* @return bool * @return bool
*/ */
function validate($validator) { public function validate($validator) {
if(!self::is_enabled()) return true; if(!Config::inst()->get('MathSpamProtectorField', 'enabled')) {
return true;
}
if(!self::correct_answer($this->Value())){ if(!self::correct_answer($this->Value())) {
$validator->validationError( $validator->validationError(
$this->name, $this->name,
_t( _t(
@ -63,6 +64,7 @@ class MathSpamProtectorField extends SpamProtectorField {
), ),
"error" "error"
); );
return false; return false;
} }
@ -73,16 +75,16 @@ class MathSpamProtectorField extends SpamProtectorField {
/** /**
* Creates the question from random variables, which are also saved to the session. * Creates the question from random variables, which are also saved to the session.
* *
* @return String * @return string
*/ */
public static function get_math_question(){ public static function get_math_question() {
if(!Session::get("mathQuestionV1")&&!Session::get("mathQuestionV2")){ 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");
} }
@ -95,7 +97,9 @@ class MathSpamProtectorField extends SpamProtectorField {
} }
/** /**
* Checks the given answer if it matches the addition of the saved session variables. * Checks the given answer if it matches the addition of the saved session
* variables.
*
* Users can answer using words or digits. * Users can answer using words or digits.
* *
* @return bool * @return bool
@ -107,7 +111,9 @@ class MathSpamProtectorField extends SpamProtectorField {
Session::clear('mathQuestionV1'); Session::clear('mathQuestionV1');
Session::clear('mathQuestionV2'); Session::clear('mathQuestionV2');
return (MathSpamProtectorField::digit_to_word($v1 + $v2) == strtolower($answer) || ($v1 + $v2) == $answer); $word = MathSpamProtectorField::digit_to_word($v1 + $v2);
return ($word == strtolower($answer) || ($v1 + $v2) == $answer);
} }
/** /**
@ -115,7 +121,7 @@ class MathSpamProtectorField extends SpamProtectorField {
* *
* @return string * @return string
*/ */
static function digit_to_word($num){ public static function digit_to_word($num){
$numbers = array(_t('MathSpamProtection.ZERO', 'zero'), $numbers = array(_t('MathSpamProtection.ZERO', 'zero'),
_t('MathSpamProtection.ONE', 'one'), _t('MathSpamProtection.ONE', 'one'),
_t('MathSpamProtection.TWO', 'two'), _t('MathSpamProtection.TWO', 'two'),
@ -140,22 +146,4 @@ class MathSpamProtectorField extends SpamProtectorField {
return $numbers[$num]; return $numbers[$num];
} }
/**
* Returns true when math spam protection is enabled
*
* @return bool
*/
public static function is_enabled() {
return (bool) self::$enabled;
}
/**
* Set whether math spam protection is enabled
*
* @param bool
*/
public static function set_enabled($enabled = true) {
self::$enabled = $enabled;
}
} }

View File

@ -3,16 +3,12 @@
"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-module",
"keywords": ["silverstripe", "spamprotection", "mathspamprotection"], "keywords": ["silverstripe", "spamprotection", "mathspamprotection"],
"authors": [ "authors": [{
{
"name": "Will Rossiter", "name": "Will Rossiter",
"email": "will@fullscreen.io" "email": "will@fullscreen.io"
} }],
],
"minimum-stability": "dev", "minimum-stability": "dev",
"require": "require": {
{
"silverstripe/framework": "3.*",
"silverstripe/spamprotection": "dev-master" "silverstripe/spamprotection": "dev-master"
} }
} }

View File

@ -1,14 +0,0 @@
# Installing
MathSpamProtection is a spam protection class for your forms.
## Install Spam Protection Module
The Spam Protection Module (http://silverstripe.org/spam-protection-module) provides the basic interface for managing the spam protection
so first you need to install that module.
## Setting up MathSpamProtection
* MathSpamProtection should be in your sites root folder and named _mathspamprotection_.
* Set the default