From 042fc8edf10bfe6f6312a258762f1e2b5d9f1b3c Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Mon, 13 Dec 2010 13:30:04 +1300 Subject: [PATCH] Initial commit of MathSpamProtection module from hackfest --- LICENSE | 17 ++++ README.md | 22 +++++ _config.php | 5 ++ code/MathSpamProtector.php | 27 ++++++ code/MathSpamProtectorField.php | 153 ++++++++++++++++++++++++++++++++ docs/en/Installing.md | 14 +++ lang/_manifest_exclude | 0 lang/en_US.php | 27 ++++++ 8 files changed, 265 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 _config.php create mode 100644 code/MathSpamProtector.php create mode 100644 code/MathSpamProtectorField.php create mode 100644 docs/en/Installing.md create mode 100644 lang/_manifest_exclude create mode 100644 lang/en_US.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..256f77d --- /dev/null +++ b/LICENSE @@ -0,0 +1,17 @@ +Copyright (c) 2007-2010, SilverStripe Limited - www.silverstripe.com +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of SilverStripe nor the names of its contributors may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..fb713c0 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# Math Spam Protection + +## Maintainers + + * Will Rossiter (Nickname: willr, wrossiter) + + +## Introduction + +This module provides a simple math protection mechanism for prevent spam from your forms. + +Includes an EditableMathSpamField to integrate with the UserForms module. + +## Requirements + + * Spam Protection 0.3+ + * SilverStripe 2.4.0+ + +## Installation + +See docs/en/Installing.md + diff --git a/_config.php b/_config.php new file mode 100644 index 0000000..c53e374 --- /dev/null +++ b/_config.php @@ -0,0 +1,5 @@ + 'text', + 'class' => 'text ' . ($this->extraClass() ? $this->extraClass() : ''), + 'id' => $this->id(), + 'name' => $this->Name(), + 'value' => $this->Value(), + 'title' => $this->Title(), + 'tabindex' => $this->getTabIndex(), + 'maxlength' => ($this->maxLength) ? $this->maxLength : null, + 'size' => ($this->maxLength) ? min( $this->maxLength, 30 ) : null + ); + + return $this->createTag('input', $attributes); + } + } + + /** + * Returns the spam question + * + * @return string + */ + 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} + * + * @return bool + */ + function validate($validator) { + if(!self::is_enabled()) return true; + + if(!self::correct_answer($this->Value())){ + return false; + } + + return true; + } + + + /** + * Creates the question from random variables, which are also saved to the session. + * + * @return String + */ + 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{ + $v1 = Session::get("mathQuestionV1"); + $v2 = Session::get("mathQuestionV2"); + } + + return sprintf( + _t('MathSpamProtection.WHATIS',"What is %s plus %s?"), + MathSpamProtectorField::digit_to_word($v1), + MathSpamProtectorField::digit_to_word($v2) + ); + } + + /** + * 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){ + $v1 = Session::get("mathQuestionV1"); + $v2 = Session::get("mathQuestionV2"); + + Session::clear('mathQuestionV1'); + Session::clear('mathQuestionV2'); + + return (MathSpamProtectorField::digit_to_word($v1 + $v2) == $answer || ($v1 + $v2) == $answer); + } + + /** + * Helper method for converting digits to their equivalent english words + * + * @return string + */ + static function digit_to_word($num){ + $numbers = array(_t('MathSpamProtection.ZERO', 'zero'), + _t('MathSpamProtection.ONE', 'one'), + _t('MathSpamProtection.TWO', 'two'), + _t('MathSpamProtection.THREE', 'three'), + _t('MathSpamProtection.FOUR', 'four'), + _t('MathSpamProtection.FIVE', 'five'), + _t('MathSpamProtection.SIX', 'six'), + _t('MathSpamProtection.SEVEN', 'seven'), + _t('MathSpamProtection.EIGHT', 'eight'), + _t('MathSpamProtection.NINE', 'nine'), + _t('MathSpamProtection.TEN', 'ten'), + _t('MathSpamProtection.ELEVEN', 'eleven'), + _t('MathSpamProtection.TWELVE', 'twelve'), + _t('MathSpamProtection.THIRTEEN', 'thirteen'), + _t('MathSpamProtection.FOURTEEN', 'fourteen'), + _t('MathSpamProtection.FIFTEEN', 'fifteen'), + _t('MathSpamProtection.SIXTEEN', 'sixteen'), + _t('MathSpamProtection.SEVENTEEN', 'seventeen'), + _t('MathSpamProtection.EIGHTEEN', 'eighteen')); + + if($num < 0) return "minus ".($numbers[-1*$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; + } +} \ No newline at end of file diff --git a/docs/en/Installing.md b/docs/en/Installing.md new file mode 100644 index 0000000..01daa4d --- /dev/null +++ b/docs/en/Installing.md @@ -0,0 +1,14 @@ +# 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 + diff --git a/lang/_manifest_exclude b/lang/_manifest_exclude new file mode 100644 index 0000000..e69de29 diff --git a/lang/en_US.php b/lang/en_US.php new file mode 100644 index 0000000..82f362e --- /dev/null +++ b/lang/en_US.php @@ -0,0 +1,27 @@ + \ No newline at end of file