Converted to PSR-2

This commit is contained in:
helpfulrobot 2015-11-21 19:27:13 +13:00
parent a1a3227cb8
commit 82a51add39
2 changed files with 168 additions and 156 deletions

View File

@ -4,20 +4,22 @@
* @package mathspamprotection * @package mathspamprotection
*/ */
class MathSpamProtector implements SpamProtector { class MathSpamProtector implements SpamProtector
{
/** /**
* Returns the {@link MathSpamProtectorField} associated with this protector * Returns the {@link MathSpamProtectorField} associated with this protector
* *
* @return MathSpamProtectorField * @return MathSpamProtectorField
*/ */
public function getFormField($name = null, $title = null, $value = null) { public function getFormField($name = null, $title = null, $value = null)
return new MathSpamProtectorField($name, $title, $value); {
} return new MathSpamProtectorField($name, $title, $value);
}
/**
* Not used by MathSpamProtector
*/
public function setFieldMapping($fieldMapping) {}
/**
* Not used by MathSpamProtector
*/
public function setFieldMapping($fieldMapping)
{
}
} }

View File

@ -6,168 +6,178 @@
* @package mathspamprotection * @package mathspamprotection
*/ */
class MathSpamProtectorField extends TextField { class MathSpamProtectorField extends TextField
{
/**
* @config
*
* @var bool $enabled
*/
private static $enabled = true;
/** /**
* @config * @config
* *
* @var bool $enabled * @var string
*/ */
private static $enabled = true; private static $question_prefix;
/**
* @config
*
* @var bool $allow_numeric_answer
*/
private static $allow_numeric_answer = true;
public function Field($properties = array())
{
if (Config::inst()->get('MathSpamProtectorField', 'enabled')) {
return parent::Field($properties);
}
/** return null;
* @config }
*
* @var string
*/
private static $question_prefix;
/**
* @config
*
* @var bool $allow_numeric_answer
*/
private static $allow_numeric_answer = true;
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);
}
public function FieldHolder($properties = array()) { return null;
if(Config::inst()->get('MathSpamProtectorField', 'enabled')) { }
return parent::FieldHolder($properties);
}
return null; /**
} * Returns the spam question
*
* @return string
*/
public function Title()
{
$prefix = Config::inst()->get('MathSpamProtection', 'question_prefix');
/** if (!$prefix) {
* Returns the spam question $prefix = _t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s");
* }
* @return string
*/
public function Title() {
$prefix = Config::inst()->get('MathSpamProtection', 'question_prefix');
if(!$prefix) { return sprintf(
$prefix = _t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s"); $prefix,
} self::get_math_question()
);
}
return sprintf( /**
$prefix, * Validates the value submitted by the user with the one saved into the
self::get_math_question() * {@link Session} and then notify callback object with the spam checking
); * result.
} *
* @return bool
*/
public function validate($validator)
{
if (!Config::inst()->get('MathSpamProtectorField', 'enabled')) {
return true;
}
/** if (!self::correct_answer($this->Value())) {
* Validates the value submitted by the user with the one saved into the $validator->validationError(
* {@link Session} and then notify callback object with the spam checking $this->name,
* result. _t(
* 'MathSpamProtectionField.INCORRECTSOLUTION',
* @return bool "Incorrect solution to the spam protection question, please try again."
*/ ),
public function validate($validator) { "error"
if(!Config::inst()->get('MathSpamProtectorField', 'enabled')) { );
return true;
}
if(!self::correct_answer($this->Value())) { return false;
$validator->validationError( }
$this->name,
_t(
'MathSpamProtectionField.INCORRECTSOLUTION',
"Incorrect solution to the spam protection question, please try again."
),
"error"
);
return false; return true;
} }
return true;
}
/** /**
* Creates the question from random variables, which are also saved to the session. * Creates the question from random variables, which are also saved to the session.
* *
* @return string * @return string
*/ */
public static function get_math_question() { public static function get_math_question()
if(!Session::get("mathQuestionV1") && !Session::get("mathQuestionV2")) { {
$v1 = rand(1,9); if (!Session::get("mathQuestionV1") && !Session::get("mathQuestionV2")) {
$v2 = rand(1,9); $v1 = rand(1, 9);
$v2 = rand(1, 9);
Session::set("mathQuestionV1",$v1); Session::set("mathQuestionV1", $v1);
Session::set("mathQuestionV2",$v2); Session::set("mathQuestionV2", $v2);
} else { } else {
$v1 = Session::get("mathQuestionV1"); $v1 = Session::get("mathQuestionV1");
$v2 = Session::get("mathQuestionV2"); $v2 = Session::get("mathQuestionV2");
} }
return sprintf( return sprintf(
_t('MathSpamProtection.WHATIS',"What is %s plus %s?"), _t('MathSpamProtection.WHATIS', "What is %s plus %s?"),
MathSpamProtectorField::digit_to_word($v1), MathSpamProtectorField::digit_to_word($v1),
MathSpamProtectorField::digit_to_word($v2) MathSpamProtectorField::digit_to_word($v2)
); );
} }
/** /**
* Checks the given answer if it matches the addition of the saved session * Checks the given answer if it matches the addition of the saved session
* variables. * variables.
* *
* Users can answer using words or digits. * Users can answer using words or digits.
* *
* @return bool * @return bool
*/ */
public static function correct_answer($answer){ public static function correct_answer($answer)
$v1 = Session::get("mathQuestionV1"); {
$v2 = Session::get("mathQuestionV2"); $v1 = Session::get("mathQuestionV1");
$v2 = Session::get("mathQuestionV2");
Session::clear('mathQuestionV1'); Session::clear('mathQuestionV1');
Session::clear('mathQuestionV2'); Session::clear('mathQuestionV2');
$word = MathSpamProtectorField::digit_to_word($v1 + $v2); $word = MathSpamProtectorField::digit_to_word($v1 + $v2);
return ($word == strtolower($answer) || (Config::inst()->get('MathSpamProtectorField', 'allow_numeric_answer') && (($v1 + $v2) == $answer))); return ($word == strtolower($answer) || (Config::inst()->get('MathSpamProtectorField', 'allow_numeric_answer') && (($v1 + $v2) == $answer)));
} }
/** /**
* Helper method for converting digits to their equivalent english words * Helper method for converting digits to their equivalent english words
* *
* @return string * @return string
*/ */
public static function digit_to_word($num){ public static function digit_to_word($num)
$numbers = array(_t('MathSpamProtection.ZERO', 'zero'), {
_t('MathSpamProtection.ONE', 'one'), $numbers = array(_t('MathSpamProtection.ZERO', 'zero'),
_t('MathSpamProtection.TWO', 'two'), _t('MathSpamProtection.ONE', 'one'),
_t('MathSpamProtection.THREE', 'three'), _t('MathSpamProtection.TWO', 'two'),
_t('MathSpamProtection.FOUR', 'four'), _t('MathSpamProtection.THREE', 'three'),
_t('MathSpamProtection.FIVE', 'five'), _t('MathSpamProtection.FOUR', 'four'),
_t('MathSpamProtection.SIX', 'six'), _t('MathSpamProtection.FIVE', 'five'),
_t('MathSpamProtection.SEVEN', 'seven'), _t('MathSpamProtection.SIX', 'six'),
_t('MathSpamProtection.EIGHT', 'eight'), _t('MathSpamProtection.SEVEN', 'seven'),
_t('MathSpamProtection.NINE', 'nine'), _t('MathSpamProtection.EIGHT', 'eight'),
_t('MathSpamProtection.TEN', 'ten'), _t('MathSpamProtection.NINE', 'nine'),
_t('MathSpamProtection.ELEVEN', 'eleven'), _t('MathSpamProtection.TEN', 'ten'),
_t('MathSpamProtection.TWELVE', 'twelve'), _t('MathSpamProtection.ELEVEN', 'eleven'),
_t('MathSpamProtection.THIRTEEN', 'thirteen'), _t('MathSpamProtection.TWELVE', 'twelve'),
_t('MathSpamProtection.FOURTEEN', 'fourteen'), _t('MathSpamProtection.THIRTEEN', 'thirteen'),
_t('MathSpamProtection.FIFTEEN', 'fifteen'), _t('MathSpamProtection.FOURTEEN', 'fourteen'),
_t('MathSpamProtection.SIXTEEN', 'sixteen'), _t('MathSpamProtection.FIFTEEN', 'fifteen'),
_t('MathSpamProtection.SEVENTEEN', 'seventeen'), _t('MathSpamProtection.SIXTEEN', 'sixteen'),
_t('MathSpamProtection.EIGHTEEN', 'eighteen')); _t('MathSpamProtection.SEVENTEEN', 'seventeen'),
_t('MathSpamProtection.EIGHTEEN', 'eighteen'));
if($num < 0) return "minus ".($numbers[-1*$num]); if ($num < 0) {
return "minus ".($numbers[-1*$num]);
}
return $numbers[$num]; return $numbers[$num];
} }
public function Type() { public function Type()
return 'mathspamprotector text'; {
} return 'mathspamprotector text';
}
} }