class MathCaptcha extends SimpleCaptcha {
/** Validate a captcha response */
- function keyMatch( $req, $info ) {
- return (int)$req->getVal( 'wpCaptchaAnswer' ) == (int)$info['answer'];
+ 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>' . wfInput( 'wpCaptchaAnswer', false, false, array( 'tabindex' => '1' ) ) . '</td></tr></table>';
- $form .= wfHidden( 'wpCaptchaId', $index );
+ $form .= '<td>' . Html::input( 'wpCaptchaWord', false, false, array( 'tabindex' => '1', 'required' ) ) . '</td></tr></table>';
+ $form .= Html::hidden( 'wpCaptchaId', $index );
return $form;
}
-
+
/** Pick a random sum */
function pickSum() {
$a = mt_rand( 0, 100 );
$ans = $op == '+' ? ( $a + $b ) : ( $a - $b );
return array( $sum, $ans );
}
-
+
/** Fetch the math */
function fetchMath( $sum ) {
- $math = new MathRenderer( $sum );
+ if( MWInit::classExists( 'MathRenderer' ) ){
+ $math = new MathRenderer( $sum );
+ } else {
+ throw new MWException( 'MathCaptcha requires the Math extension for MediaWiki versions 1.18 and above.' );
+ }
$math->setOutputMode( MW_MATH_PNG );
$html = $math->render();
- return preg_replace( '/alt=".*"/', '', $html );
+ return preg_replace( '/alt=".*?"/', '', $html );
}
-
}
-?>