Initial commit of MathSpamProtection module from hackfest

This commit is contained in:
Will Rossiter 2010-12-13 13:30:04 +13:00
commit 042fc8edf1
8 changed files with 265 additions and 0 deletions

17
LICENSE Normal file
View File

@ -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.

22
README.md Normal file
View File

@ -0,0 +1,22 @@
# Math Spam Protection
## Maintainers
* Will Rossiter (Nickname: willr, wrossiter)
<will at silverstripe dot com>
## 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

5
_config.php Normal file
View File

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

View File

@ -0,0 +1,27 @@
<?php
/**
* @package mathspamprotection
*/
class MathSpamProtector implements SpamProtector {
/**
* Returns the {@link MathSpamProtectorField} associated with this protector
*
* @return MathSpamProtectorField
*/
public function getFormField($name = null, $title = null, $value = null, $form = null, $rightTitle = null) {
return new MathSpamProtectorField($name, $title, $value, $form, $rightTitle);
}
/**
* 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

@ -0,0 +1,153 @@
<?php
/**
* {@link FormField} for adding an optional maths protection question to a form.
*
* @package mathspamprotection
*/
class MathSpamProtectorField extends SpamProtectorField {
/**
* @var bool If MathSpamProtection is enabled
*/
private static $enabled = true;
/**
* Outputs the field HTML to the the web browser
*
* @return HTML
*/
function Field() {
if(self::is_enabled()) {
$attributes = array(
'type' => '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;
}
}

14
docs/en/Installing.md Normal file
View File

@ -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

0
lang/_manifest_exclude Normal file
View File

27
lang/en_US.php Normal file
View File

@ -0,0 +1,27 @@
<?php
global $lang;
$lang['en_US']['MathSpamProtection']['EIGHT'] = 'eight';
$lang['en_US']['MathSpamProtection']['EIGHTEEN'] = 'eighteen';
$lang['en_US']['MathSpamProtection']['ELEVEN'] = 'eleven';
$lang['en_US']['MathSpamProtection']['FIFTEEN'] = 'fifteen';
$lang['en_US']['MathSpamProtection']['FIVE'] = 'five';
$lang['en_US']['MathSpamProtection']['FOUR'] = 'four';
$lang['en_US']['MathSpamProtection']['FOURTEEN'] = 'fourteen';
$lang['en_US']['MathSpamProtection']['NINE'] = 'nine';
$lang['en_US']['MathSpamProtection']['ONE'] = 'one';
$lang['en_US']['MathSpamProtection']['SEVEN'] = 'seven';
$lang['en_US']['MathSpamProtection']['SEVENTEEN'] = 'seventeen';
$lang['en_US']['MathSpamProtection']['SIX'] = 'six';
$lang['en_US']['MathSpamProtection']['SIXTEEN'] = 'sixteen';
$lang['en_US']['MathSpamProtection']['TEN'] = 'ten';
$lang['en_US']['MathSpamProtection']['THIRTEEN'] = 'thirteen';
$lang['en_US']['MathSpamProtection']['THREE'] = 'three';
$lang['en_US']['MathSpamProtection']['TWELVE'] = 'twelve';
$lang['en_US']['MathSpamProtection']['TWO'] = 'two';
$lang['en_US']['MathSpamProtection']['WHATIS'] = 'What is %s plus %s?';
$lang['en_US']['MathSpamProtection']['ZERO'] = 'zero';
$lang['en_US']['MathSpamProtectionField']['SPAMQUESTION'] = 'Spam protection question: %s';
?>