Version: current

     1 <?php
     2 /**
     3  * Tools for adding an optional Maths protection question to a form.
     4  *
     5  * @package cms
     6  * @subpackage comments
     7  */
     8
     9 class MathSpamProtection {
    10
    11     private static $mathProtection = false;
    12
    13     /**
    14      * Creates the question from random variables, which are also saved to the session.
    15      * @return String
    16      */
    17     static function getMathQuestion(){
    18         if(!Session::get("mathQuestionV1")&&!Session::get("mathQuestionV2")){
    19             $v1 = rand(1,9);
    20             $v2 = rand(1,9);
    21             Session::set("mathQuestionV1",$v1);
    22             Session::set("mathQuestionV2",$v2);
    23         }
    24         else{
    25             $v1 = Session::get("mathQuestionV1");
    26             $v2 = Session::get("mathQuestionV2");
    27         }
    28
    29         return sprintf(
    30             _t('MathSpamProtection.WHATIS',"What is %s plus %s?"),
    31             MathSpamProtection::digitToWord($v1),
    32             MathSpamProtection::digitToWord($v2)
    33         );
    34     }
    35
    36     /**
    37      * Checks the given answer if it matches the addition of the saved session variables. Users can answer using words or digits.
    38      */
    39     static function correctAnswer($answer){
    40         $v1 = Session::get("mathQuestionV1");
    41         $v2 = Session::get("mathQuestionV2");
    42
    43         Session::clear('mathQuestionV1');
    44         Session::clear('mathQuestionV2');
    45
    46         if(MathSpamProtection::digitToWord($v1 + $v2) == $answer || ($v1 + $v2) == $answer){
    47             return true;
    48         }
    49         return false;
    50
    51     }
    52
    53     /**
    54      * Helper method for converting digits to their equivelant english words
    55      */
    56     static function digitToWord($num){
    57         $numbers = array(_t('MathSpamProtection.ZERO', 'zero'),
    58             _t('MathSpamProtection.ONE', 'one'),
    59             _t('MathSpamProtection.TWO', 'two'),
    60             _t('MathSpamProtection.THREE', 'three'),
    61             _t('MathSpamProtection.FOUR', 'four'),
    62             _t('MathSpamProtection.FIVE', 'five'),
    63             _t('MathSpamProtection.SIX', 'six'),
    64             _t('MathSpamProtection.SEVEN', 'seven'),
    65             _t('MathSpamProtection.EIGHT', 'eight'),
    66             _t('MathSpamProtection.NINE', 'nine'),
    67             _t('MathSpamProtection.TEN', 'ten'),
    68             _t('MathSpamProtection.ELEVEN', 'eleven'),
    69             _t('MathSpamProtection.TWELVE', 'twelve'),
    70             _t('MathSpamProtection.THIRTEEN', 'thirteen'),
    71             _t('MathSpamProtection.FOURTEEN', 'fourteen'),
    72             _t('MathSpamProtection.FIFTEEN', 'fifteen'),
    73             _t('MathSpamProtection.SIXTEEN', 'sixteen'),
    74             _t('MathSpamProtection.SEVENTEEN', 'seventeen'),
    75             _t('MathSpamProtection.EIGHTEEN', 'eighteen'));
    76
    77             if($num < 0) return "minus ".($numbers[-1*$num]);
    78
    79         return $numbers[$num];
    80     }
    81
    82
    83     static function isEnabled() {
    84         return self::$mathProtection;
    85     }
    86
    87     static function setEnabled($math = true) {
    88         self::$mathProtection = $math;
    89     }
    90
    91 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -