mediawiki-extensions-Confir.../MathCaptcha/MathCaptcha.class.php
Florianschmidtwelzow 36abbc6288 Implement support for Google reCAPTCHA 2.0 ("No captcha")
This change adds a new Captcha type (ReCaptchaNoCaptcha) that uses
Google reCAPTCHA 2.0.

See more:
- https://www.google.com/recaptcha/intro/
- https://developers.google.com/recaptcha/docs/display
- https://developers.google.com/recaptcha/docs/faq
- http://googleonlinesecurity.blogspot.com/2014/12/are-you-robot-introducing-no-captcha.html

Bug: T84918
Change-Id: I5908fd2716786237adb01a403d5bd1e22d95c563
2015-06-27 03:00:11 +02:00

54 lines
1.6 KiB
PHP

<?php
class MathCaptcha extends SimpleCaptcha {
/** Validate a captcha response */
function keyMatch( $answer, $info ) {
return (int)$answer == (int)$info['answer'];
}
function addCaptchaAPI( &$resultArr ) {
list( $sum, $answer ) = $this->pickSum();
$index = $this->storeCaptcha( array( 'answer' => $answer ) );
$resultArr['captcha']['type'] = 'math';
$resultArr['captcha']['mime'] = 'text/tex';
$resultArr['captcha']['id'] = $index;
$resultArr['captcha']['question'] = $sum;
}
/**
* Produce a nice little form
* @param OutputPage $out
*/
function getForm( OutputPage $out ) {
list( $sum, $answer ) = $this->pickSum();
$index = $this->storeCaptcha( array( 'answer' => $answer ) );
$form = '<table><tr><td>' . $this->fetchMath( $sum ) . '</td>';
$form .= '<td>' . Html::input( 'wpCaptchaWord', false, false, array( 'tabindex' => '1', 'autocomplete' => 'off', 'required' ) ) . '</td></tr></table>';
$form .= Html::hidden( 'wpCaptchaId', $index );
return $form;
}
/** Pick a random sum */
function pickSum() {
$a = mt_rand( 0, 100 );
$b = mt_rand( 0, 10 );
$op = mt_rand( 0, 1 ) ? '+' : '-';
$sum = "{$a} {$op} {$b} = ";
$ans = $op == '+' ? ( $a + $b ) : ( $a - $b );
return array( $sum, $ans );
}
/** Fetch the math */
function fetchMath( $sum ) {
if ( class_exists( 'MathRenderer' ) ) {
$math = MathRenderer::getRenderer( $sum, array(), MW_MATH_PNG );
} else {
throw new Exception( 'MathCaptcha requires the Math extension for MediaWiki versions 1.18 and above.' );
}
$html = $math->render();
return preg_replace( '/alt=".*?"/', '', $html );
}
}