2007-11-12 07:42:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class MathCaptcha extends SimpleCaptcha {
|
|
|
|
|
|
|
|
/** Validate a captcha response */
|
2008-01-24 21:42:21 +00:00
|
|
|
function keyMatch( $answer, $info ) {
|
|
|
|
return (int)$answer == (int)$info['answer'];
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
2008-02-28 17:42:23 +00:00
|
|
|
|
2009-07-19 15:13:01 +00:00
|
|
|
function addCaptchaAPI( &$resultArr ) {
|
2008-02-28 17:42:23 +00:00
|
|
|
list( $sum, $answer ) = $this->pickSum();
|
2009-07-19 15:13:01 +00:00
|
|
|
$index = $this->storeCaptcha( array( 'answer' => $answer ) );
|
2008-02-28 17:42:23 +00:00
|
|
|
$resultArr['captcha']['type'] = 'math';
|
2008-02-29 21:44:21 +00:00
|
|
|
$resultArr['captcha']['mime'] = 'text/tex';
|
2008-02-28 17:42:23 +00:00
|
|
|
$resultArr['captcha']['id'] = $index;
|
2008-02-28 21:23:26 +00:00
|
|
|
$resultArr['captcha']['question'] = $sum;
|
2008-02-28 17:42:23 +00:00
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
/** Produce a nice little form */
|
|
|
|
function getForm() {
|
|
|
|
list( $sum, $answer ) = $this->pickSum();
|
|
|
|
$index = $this->storeCaptcha( array( 'answer' => $answer ) );
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
$form = '<table><tr><td>' . $this->fetchMath( $sum ) . '</td>';
|
2013-05-03 01:09:51 +00:00
|
|
|
$form .= '<td>' . Html::input( 'wpCaptchaWord', false, false, array( 'tabindex' => '1', 'autocomplete' => 'off', 'required' ) ) . '</td></tr></table>';
|
2010-10-29 15:14:44 +00:00
|
|
|
$form .= Html::hidden( 'wpCaptchaId', $index );
|
2007-11-12 07:42:25 +00:00
|
|
|
return $form;
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
/** 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 );
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
/** Fetch the math */
|
|
|
|
function fetchMath( $sum ) {
|
2013-07-24 07:59:13 +00:00
|
|
|
if ( class_exists( 'MathRenderer' ) ) {
|
2013-04-18 00:26:56 +00:00
|
|
|
$math = MathRenderer::getRenderer( $sum, array(), MW_MATH_PNG );
|
2011-07-22 21:51:06 +00:00
|
|
|
} else {
|
2011-04-24 11:11:10 +00:00
|
|
|
throw new MWException( 'MathCaptcha requires the Math extension for MediaWiki versions 1.18 and above.' );
|
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
$html = $math->render();
|
2008-12-20 02:12:36 +00:00
|
|
|
return preg_replace( '/alt=".*?"/', '', $html );
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
|
|
|
}
|