Permalink
Browse files

Cleanup to work with latest master spam, fix validation

  • Loading branch information...
1 parent 481c998 commit 2d29d309f6117fa3b5e566a23879fd6fef48fc25 @wilr wilr committed Feb 10, 2014
Showing with 70 additions and 107 deletions.
  1. +22 −5 README.md
  2. +0 −10 TODO.md
  3. +0 −5 _config.php
  4. 0 _config/.gitkeep
  5. +2 −11 code/MathSpamProtector.php
  6. +43 −55 code/MathSpamProtectorField.php
  7. +3 −7 composer.json
  8. +0 −14 docs/en/Installing.md
View
@@ -7,16 +7,33 @@
## 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.
## Requirements
- * SilverStripe 3.0
- * Spam Protection Master
+ * SilverStripe 3.1
+ * 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
View
10 TODO.md
@@ -1,10 +0,0 @@
-# TODO
-
-## Blockers
-
-* Built in validation doesn't work any more.
-
-## Medium
-
-
-## Minor
View
@@ -1,5 +0,0 @@
-<?php
-
-/**
- * Default _config file for MathSpamProtection module.
- */
View
No changes.
View
@@ -11,17 +11,8 @@ class MathSpamProtector implements SpamProtector {
*
* @return MathSpamProtectorField
*/
- public function getFormField($name = null, $title = null, $value = null, $form = null, $rightTitle = null) {
- return new MathSpamProtectorField($name, $title, $value, $form, $rightTitle);
+ public function getFormField($name = null, $title = null, $value = null) {
+ 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;
- }
}
@@ -6,55 +6,56 @@
* @package mathspamprotection
*/
-class MathSpamProtectorField extends SpamProtectorField {
+class MathSpamProtectorField extends TextField {
/**
- * @var bool If MathSpamProtection is enabled
+ * @config
+ *
+ * @var bool $enabled
*/
private static $enabled = true;
+
+ public function Field($properties = array()) {
+ if(Config::inst()->get('MathSpamProtectorField', 'enabled')) {
+ return parent::Field($properties);
+ }
- /**
- * Outputs the field HTML to the the web browser
- *
- * @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;
}
/**
* Returns the spam question
*
* @return string
*/
- function Title() {
- return sprintf(_t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s"), self::get_math_question());
+ public function Title() {
+ return sprintf(
+ _t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s"),
+ self::get_math_question()
+ );
}
/**
- * 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.
+ * 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
*/
- function validate($validator) {
- if(!self::is_enabled()) return true;
+ public function validate($validator) {
+ if(!Config::inst()->get('MathSpamProtectorField', 'enabled')) {
+ return true;
+ }
- if(!self::correct_answer($this->Value())){
+ if(!self::correct_answer($this->Value())) {
$validator->validationError(
$this->name,
_t(
@@ -63,6 +64,7 @@ function validate($validator) {
),
"error"
);
+
return false;
}
@@ -73,16 +75,16 @@ function validate($validator) {
/**
* Creates the question from random variables, which are also saved to the session.
*
- * @return String
+ * @return string
*/
- public static function get_math_question(){
- if(!Session::get("mathQuestionV1")&&!Session::get("mathQuestionV2")){
+ public static function get_math_question() {
+ if(!Session::get("mathQuestionV1") && !Session::get("mathQuestionV2")) {
$v1 = rand(1,9);
$v2 = rand(1,9);
+
Session::set("mathQuestionV1",$v1);
Session::set("mathQuestionV2",$v2);
- }
- else{
+ } else {
$v1 = Session::get("mathQuestionV1");
$v2 = Session::get("mathQuestionV2");
}
@@ -95,7 +97,9 @@ public static function get_math_question(){
}
/**
- * 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.
*
* @return bool
@@ -107,15 +111,17 @@ public static function correct_answer($answer){
Session::clear('mathQuestionV1');
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);
}
/**
* Helper method for converting digits to their equivalent english words
*
* @return string
*/
- static function digit_to_word($num){
+ public static function digit_to_word($num){
$numbers = array(_t('MathSpamProtection.ZERO', 'zero'),
_t('MathSpamProtection.ONE', 'one'),
_t('MathSpamProtection.TWO', 'two'),
@@ -140,22 +146,4 @@ static function digit_to_word($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
@@ -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.",
"type": "silverstripe-module",
"keywords": ["silverstripe", "spamprotection", "mathspamprotection"],
- "authors": [
- {
+ "authors": [{
"name": "Will Rossiter",
"email": "will@fullscreen.io"
- }
- ],
+ }],
"minimum-stability": "dev",
- "require":
- {
- "silverstripe/framework": "3.*",
+ "require": {
"silverstripe/spamprotection": "dev-master"
}
}
View
@@ -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
-

0 comments on commit 2d29d30

Please sign in to comment.