Merge pull request #1 from marijnkampf/master

BUGFIX: Fixed validation for mathspamprotection. MINOR: added dutch translation.
This commit is contained in:
Will Rossiter 2011-06-09 14:03:54 -07:00
commit 7d1a277815
3 changed files with 81 additions and 42 deletions

View File

@ -2,7 +2,7 @@
/** /**
* {@link FormField} for adding an optional maths protection question to a form. * {@link FormField} for adding an optional maths protection question to a form.
* *
* @package mathspamprotection * @package mathspamprotection
*/ */
@ -12,7 +12,7 @@ class MathSpamProtectorField extends SpamProtectorField {
* @var bool If MathSpamProtection is enabled * @var bool If MathSpamProtection is enabled
*/ */
private static $enabled = true; private static $enabled = true;
/** /**
* Outputs the field HTML to the the web browser * Outputs the field HTML to the the web browser
* *
@ -31,37 +31,47 @@ class MathSpamProtectorField extends SpamProtectorField {
'maxlength' => ($this->maxLength) ? $this->maxLength : null, 'maxlength' => ($this->maxLength) ? $this->maxLength : null,
'size' => ($this->maxLength) ? min( $this->maxLength, 30 ) : null 'size' => ($this->maxLength) ? min( $this->maxLength, 30 ) : null
); );
return $this->createTag('input', $attributes); return $this->createTag('input', $attributes);
} }
} }
/** /**
* Returns the spam question * Returns the spam question
* *
* @return string * @return string
*/ */
function Title() { function Title() {
return sprintf(_t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s"), self::get_math_question()); return sprintf(_t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s"), self::get_math_question());
} }
/** /**
* Validates the value submitted by the user with the one saved * Validates the value submitted by the user with the one saved
* into the {@link Session} * into the {@link Session} and then notify callback object
* with the spam checking result.
* *
* @return bool * @return bool
*/ */
function validate($validator) { function validate($validator) {
if(!self::is_enabled()) return true; if(!self::is_enabled()) return true;
if(!self::correct_answer($this->Value())){ if(!self::correct_answer($this->Value())){
$validator->validationError(
$this->name,
_t(
'MathSpamProtectionField.INCORRECTSOLUTION',
"Incorrect solution to the spam protection question, please try again.",
PR_MEDIUM
),
"error"
);
return false; 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.
* *
@ -78,16 +88,16 @@ class MathSpamProtectorField extends SpamProtectorField {
$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 variables. * Checks the given answer if it matches the addition of the saved session variables.
* Users can answer using words or digits. * Users can answer using words or digits.
* *
* @return bool * @return bool
@ -95,13 +105,13 @@ class MathSpamProtectorField extends SpamProtectorField {
public static function correct_answer($answer){ public static function correct_answer($answer){
$v1 = Session::get("mathQuestionV1"); $v1 = Session::get("mathQuestionV1");
$v2 = Session::get("mathQuestionV2"); $v2 = Session::get("mathQuestionV2");
Session::clear('mathQuestionV1'); Session::clear('mathQuestionV1');
Session::clear('mathQuestionV2'); Session::clear('mathQuestionV2');
return (MathSpamProtectorField::digit_to_word($v1 + $v2) == $answer || ($v1 + $v2) == $answer); return (MathSpamProtectorField::digit_to_word($v1 + $v2) == $answer || ($v1 + $v2) == $answer);
} }
/** /**
* Helper method for converting digits to their equivalent english words * Helper method for converting digits to their equivalent english words
* *
@ -126,13 +136,13 @@ class MathSpamProtectorField extends SpamProtectorField {
_t('MathSpamProtection.FIFTEEN', 'fifteen'), _t('MathSpamProtection.FIFTEEN', 'fifteen'),
_t('MathSpamProtection.SIXTEEN', 'sixteen'), _t('MathSpamProtection.SIXTEEN', 'sixteen'),
_t('MathSpamProtection.SEVENTEEN', 'seventeen'), _t('MathSpamProtection.SEVENTEEN', 'seventeen'),
_t('MathSpamProtection.EIGHTEEN', 'eighteen')); _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];
} }
/** /**
* Returns true when math spam protection is enabled * Returns true when math spam protection is enabled
* *
@ -141,7 +151,7 @@ class MathSpamProtectorField extends SpamProtectorField {
public static function is_enabled() { public static function is_enabled() {
return (bool) self::$enabled; return (bool) self::$enabled;
} }
/** /**
* Set whether math spam protection is enabled * Set whether math spam protection is enabled
* *

View File

@ -2,26 +2,27 @@
global $lang; global $lang;
$lang['en_US']['MathSpamProtection']['EIGHT'] = 'eight'; $lang['en_US']['MathSpamProtection']['ZERO'] = 'zero';
$lang['en_US']['MathSpamProtection']['EIGHTEEN'] = 'eighteen'; $lang['en_US']['MathSpamProtection']['ONE'] = 'one';
$lang['en_US']['MathSpamProtection']['ELEVEN'] = 'eleven'; $lang['en_US']['MathSpamProtection']['TWO'] = 'two';
$lang['en_US']['MathSpamProtection']['FIFTEEN'] = 'fifteen'; $lang['en_US']['MathSpamProtection']['THREE'] = 'three';
$lang['en_US']['MathSpamProtection']['FIVE'] = 'five'; $lang['en_US']['MathSpamProtection']['FOUR'] = 'four';
$lang['en_US']['MathSpamProtection']['FOUR'] = 'four'; $lang['en_US']['MathSpamProtection']['FIVE'] = 'five';
$lang['en_US']['MathSpamProtection']['FOURTEEN'] = 'fourteen'; $lang['en_US']['MathSpamProtection']['SIX'] = 'six';
$lang['en_US']['MathSpamProtection']['NINE'] = 'nine'; $lang['en_US']['MathSpamProtection']['SEVEN'] = 'seven';
$lang['en_US']['MathSpamProtection']['ONE'] = 'one'; $lang['en_US']['MathSpamProtection']['EIGHT'] = 'eight';
$lang['en_US']['MathSpamProtection']['SEVEN'] = 'seven'; $lang['en_US']['MathSpamProtection']['NINE'] = 'nine';
$lang['en_US']['MathSpamProtection']['TEN'] = 'ten';
$lang['en_US']['MathSpamProtection']['ELEVEN'] = 'eleven';
$lang['en_US']['MathSpamProtection']['TWELVE'] = 'twelve';
$lang['en_US']['MathSpamProtection']['THIRTEEN'] = 'thirteen';
$lang['en_US']['MathSpamProtection']['FOURTEEN'] = 'fourteen';
$lang['en_US']['MathSpamProtection']['FIFTEEN'] = 'fifteen';
$lang['en_US']['MathSpamProtection']['SIXTEEN'] = 'sixteen';
$lang['en_US']['MathSpamProtection']['SEVENTEEN'] = 'seventeen'; $lang['en_US']['MathSpamProtection']['SEVENTEEN'] = 'seventeen';
$lang['en_US']['MathSpamProtection']['SIX'] = 'six'; $lang['en_US']['MathSpamProtection']['EIGHTEEN'] = 'eighteen';
$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']['WHATIS'] = 'What is %s plus %s?';
$lang['en_US']['MathSpamProtection']['ZERO'] = 'zero';
$lang['en_US']['MathSpamProtectionField']['SPAMQUESTION'] = 'Spam protection question: %s'; $lang['en_US']['MathSpamProtectionField']['SPAMQUESTION'] = 'Spam protection question: %s';
$lang['en_US']['MathSpamProtectionField']['INCORRECTSOLUTION'] = 'Incorrect solution to the spam protection question, please try again.';
?> ?>

28
lang/nl_NL.php Normal file
View File

@ -0,0 +1,28 @@
<?php
global $lang;
$lang['en_US']['MathSpamProtection']['ZERO'] = 'nul';
$lang['en_US']['MathSpamProtection']['ONE'] = 'een';
$lang['en_US']['MathSpamProtection']['TWO'] = 'twee';
$lang['en_US']['MathSpamProtection']['THREE'] = 'drie';
$lang['en_US']['MathSpamProtection']['FOUR'] = 'vier';
$lang['en_US']['MathSpamProtection']['FIVE'] = 'vijf';
$lang['en_US']['MathSpamProtection']['SIX'] = 'zes';
$lang['en_US']['MathSpamProtection']['SEVEN'] = 'zeven';
$lang['en_US']['MathSpamProtection']['EIGHT'] = 'acht';
$lang['en_US']['MathSpamProtection']['NINE'] = 'negen';
$lang['en_US']['MathSpamProtection']['TEN'] = 'tien';
$lang['en_US']['MathSpamProtection']['ELEVEN'] = 'elf';
$lang['en_US']['MathSpamProtection']['TWELVE'] = 'twaalf';
$lang['en_US']['MathSpamProtection']['THIRTEEN'] = 'dertien';
$lang['en_US']['MathSpamProtection']['FOURTEEN'] = 'veertien';
$lang['en_US']['MathSpamProtection']['FIFTEEN'] = 'vijftien';
$lang['en_US']['MathSpamProtection']['SIXTEEN'] = 'zestien';
$lang['en_US']['MathSpamProtection']['SEVENTEEN'] = 'zeventien';
$lang['en_US']['MathSpamProtection']['EIGHTEEN'] = 'achttien';
$lang['en_US']['MathSpamProtection']['WHATIS'] = 'Hoeveel is %s plus %s?';
$lang['en_US']['MathSpamProtectionField']['SPAMQUESTION'] = 'Anti-spam vraag: %s';
$lang['en_US']['MathSpamProtectionField']['INCORRECTSOLUTION'] = 'Onjuist antwoord op de anti-spam vraag, probeer opnieuw.';
?>