mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/ConfirmEdit
synced 2024-11-14 11:16:50 +00:00
8765b88fbf
Bug: 48030 Change-Id: Id0eed4797ab5649fc5bb965b5d94fba21f120d9f
51 lines
1.5 KiB
PHP
51 lines
1.5 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 */
|
|
function getForm() {
|
|
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 ( MWInit::classExists( 'MathRenderer' ) ) {
|
|
$math = MathRenderer::getRenderer( $sum, array(), MW_MATH_PNG );
|
|
} else {
|
|
throw new MWException( 'MathCaptcha requires the Math extension for MediaWiki versions 1.18 and above.' );
|
|
}
|
|
$html = $math->render();
|
|
return preg_replace( '/alt=".*?"/', '', $html );
|
|
}
|
|
}
|