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>';
|
2008-12-20 09:32:47 +00:00
|
|
|
$form .= '<td>' . Xml::input( 'wpCaptchaWord', false, false, array( 'tabindex' => '1' ) ) . '</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 ) {
|
2011-04-24 11:11:10 +00:00
|
|
|
if( MWInit::classExists( 'MathRenderer' ) ){
|
|
|
|
$math = new MathRenderer( $sum );
|
|
|
|
} else {
|
|
|
|
throw new MWException( 'MathCaptcha requires the Math extension for MediaWiki versions 1.18 and above.' );
|
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
$math->setOutputMode( MW_MATH_PNG );
|
|
|
|
$html = $math->render();
|
2008-12-20 02:12:36 +00:00
|
|
|
return preg_replace( '/alt=".*?"/', '', $html );
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
|
|
|
}
|