2010-12-13 01:30:04 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@link FormField} for adding an optional maths protection question to a form.
|
2011-06-09 11:35:23 +02:00
|
|
|
*
|
2010-12-13 01:30:04 +01:00
|
|
|
* @package mathspamprotection
|
|
|
|
*/
|
|
|
|
|
2014-02-10 09:25:27 +01:00
|
|
|
class MathSpamProtectorField extends TextField {
|
2010-12-13 01:30:04 +01:00
|
|
|
|
|
|
|
/**
|
2014-02-10 09:25:27 +01:00
|
|
|
* @config
|
|
|
|
*
|
|
|
|
* @var bool $enabled
|
2010-12-13 01:30:04 +01:00
|
|
|
*/
|
|
|
|
private static $enabled = true;
|
2014-02-10 09:25:27 +01:00
|
|
|
|
|
|
|
public function Field($properties = array()) {
|
|
|
|
if(Config::inst()->get('MathSpamProtectorField', 'enabled')) {
|
|
|
|
return parent::Field($properties);
|
|
|
|
}
|
2011-06-09 11:35:23 +02:00
|
|
|
|
2014-02-10 09:25:27 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function FieldHolder($properties = array()) {
|
|
|
|
if(Config::inst()->get('MathSpamProtectorField', 'enabled')) {
|
|
|
|
return parent::FieldHolder($properties);
|
2010-12-13 01:30:04 +01:00
|
|
|
}
|
2014-02-10 09:25:27 +01:00
|
|
|
|
|
|
|
return null;
|
2010-12-13 01:30:04 +01:00
|
|
|
}
|
2011-06-09 11:35:23 +02:00
|
|
|
|
2010-12-13 01:30:04 +01:00
|
|
|
/**
|
|
|
|
* Returns the spam question
|
2011-06-09 11:35:23 +02:00
|
|
|
*
|
2010-12-13 01:30:04 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-02-10 09:25:27 +01:00
|
|
|
public function Title() {
|
|
|
|
return sprintf(
|
|
|
|
_t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s"),
|
|
|
|
self::get_math_question()
|
|
|
|
);
|
2010-12-13 01:30:04 +01:00
|
|
|
}
|
2011-06-09 11:35:23 +02:00
|
|
|
|
2010-12-13 01:30:04 +01:00
|
|
|
/**
|
2014-02-10 09:25:27 +01:00
|
|
|
* 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.
|
2010-12-13 01:30:04 +01:00
|
|
|
*
|
2011-06-09 11:35:23 +02:00
|
|
|
* @return bool
|
2010-12-13 01:30:04 +01:00
|
|
|
*/
|
2014-02-10 09:25:27 +01:00
|
|
|
public function validate($validator) {
|
|
|
|
if(!Config::inst()->get('MathSpamProtectorField', 'enabled')) {
|
|
|
|
return true;
|
|
|
|
}
|
2010-12-13 01:30:04 +01:00
|
|
|
|
2014-02-10 09:25:27 +01:00
|
|
|
if(!self::correct_answer($this->Value())) {
|
2011-06-09 11:35:23 +02:00
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
|
|
|
_t(
|
|
|
|
'MathSpamProtectionField.INCORRECTSOLUTION',
|
2012-07-10 14:23:18 +02:00
|
|
|
"Incorrect solution to the spam protection question, please try again."
|
2011-06-09 11:35:23 +02:00
|
|
|
),
|
|
|
|
"error"
|
|
|
|
);
|
2014-02-10 09:25:27 +01:00
|
|
|
|
2010-12-13 01:30:04 +01:00
|
|
|
return false;
|
|
|
|
}
|
2011-06-09 11:35:23 +02:00
|
|
|
|
2010-12-13 01:30:04 +01:00
|
|
|
return true;
|
|
|
|
}
|
2011-06-09 11:35:23 +02:00
|
|
|
|
|
|
|
|
2010-12-13 01:30:04 +01:00
|
|
|
/**
|
|
|
|
* Creates the question from random variables, which are also saved to the session.
|
|
|
|
*
|
2014-02-10 09:25:27 +01:00
|
|
|
* @return string
|
2010-12-13 01:30:04 +01:00
|
|
|
*/
|
2014-02-10 09:25:27 +01:00
|
|
|
public static function get_math_question() {
|
|
|
|
if(!Session::get("mathQuestionV1") && !Session::get("mathQuestionV2")) {
|
2010-12-13 01:30:04 +01:00
|
|
|
$v1 = rand(1,9);
|
|
|
|
$v2 = rand(1,9);
|
2014-02-10 09:25:27 +01:00
|
|
|
|
2010-12-13 01:30:04 +01:00
|
|
|
Session::set("mathQuestionV1",$v1);
|
|
|
|
Session::set("mathQuestionV2",$v2);
|
2014-02-10 09:25:27 +01:00
|
|
|
} else {
|
2010-12-13 01:30:04 +01:00
|
|
|
$v1 = Session::get("mathQuestionV1");
|
|
|
|
$v2 = Session::get("mathQuestionV2");
|
|
|
|
}
|
2011-06-09 11:35:23 +02:00
|
|
|
|
2010-12-13 01:30:04 +01:00
|
|
|
return sprintf(
|
2011-06-09 11:35:23 +02:00
|
|
|
_t('MathSpamProtection.WHATIS',"What is %s plus %s?"),
|
|
|
|
MathSpamProtectorField::digit_to_word($v1),
|
2010-12-13 01:30:04 +01:00
|
|
|
MathSpamProtectorField::digit_to_word($v2)
|
|
|
|
);
|
|
|
|
}
|
2011-06-09 11:35:23 +02:00
|
|
|
|
2010-12-13 01:30:04 +01:00
|
|
|
/**
|
2014-02-10 09:25:27 +01:00
|
|
|
* Checks the given answer if it matches the addition of the saved session
|
|
|
|
* variables.
|
|
|
|
*
|
2010-12-13 01:30:04 +01:00
|
|
|
* Users can answer using words or digits.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function correct_answer($answer){
|
|
|
|
$v1 = Session::get("mathQuestionV1");
|
|
|
|
$v2 = Session::get("mathQuestionV2");
|
2011-06-09 11:35:23 +02:00
|
|
|
|
2010-12-13 01:30:04 +01:00
|
|
|
Session::clear('mathQuestionV1');
|
|
|
|
Session::clear('mathQuestionV2');
|
2011-06-09 11:35:23 +02:00
|
|
|
|
2014-02-10 09:25:27 +01:00
|
|
|
$word = MathSpamProtectorField::digit_to_word($v1 + $v2);
|
|
|
|
|
|
|
|
return ($word == strtolower($answer) || ($v1 + $v2) == $answer);
|
2010-12-13 01:30:04 +01:00
|
|
|
}
|
2011-06-09 11:35:23 +02:00
|
|
|
|
2010-12-13 01:30:04 +01:00
|
|
|
/**
|
|
|
|
* Helper method for converting digits to their equivalent english words
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-02-10 09:25:27 +01:00
|
|
|
public static function digit_to_word($num){
|
2010-12-13 01:30:04 +01:00
|
|
|
$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'),
|
2011-06-09 11:35:23 +02:00
|
|
|
_t('MathSpamProtection.EIGHTEEN', 'eighteen'));
|
|
|
|
|
2010-12-13 01:30:04 +01:00
|
|
|
if($num < 0) return "minus ".($numbers[-1*$num]);
|
2011-06-09 11:35:23 +02:00
|
|
|
|
2010-12-13 01:30:04 +01:00
|
|
|
return $numbers[$num];
|
|
|
|
}
|
2013-09-24 11:21:31 +02:00
|
|
|
}
|