diff --git a/README.md b/README.md index 5bde39f..9b4f51b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/TODO.md b/TODO.md deleted file mode 100644 index 2f644a1..0000000 --- a/TODO.md +++ /dev/null @@ -1,10 +0,0 @@ -# TODO - -## Blockers - -* Built in validation doesn't work any more. - -## Medium - - -## Minor \ No newline at end of file diff --git a/_config.php b/_config.php deleted file mode 100644 index c53e374..0000000 --- a/_config.php +++ /dev/null @@ -1,5 +0,0 @@ - '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); + + public function Field($properties = array()) { + if(Config::inst()->get('MathSpamProtectorField', 'enabled')) { + return parent::Field($properties); } + + 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 */ - 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 @@ class MathSpamProtectorField extends SpamProtectorField { ), "error" ); + return false; } @@ -73,16 +75,16 @@ class MathSpamProtectorField extends SpamProtectorField { /** * 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 @@ 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. * * @return bool @@ -107,7 +111,9 @@ class MathSpamProtectorField extends SpamProtectorField { 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); } /** @@ -115,7 +121,7 @@ class MathSpamProtectorField extends SpamProtectorField { * * @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 @@ class MathSpamProtectorField extends SpamProtectorField { 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; - } } diff --git a/composer.json b/composer.json index cc1388f..62d5025 100644 --- a/composer.json +++ b/composer.json @@ -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" } } \ No newline at end of file diff --git a/docs/en/Installing.md b/docs/en/Installing.md deleted file mode 100644 index 01daa4d..0000000 --- a/docs/en/Installing.md +++ /dev/null @@ -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 -