3 class MathCaptcha extends SimpleCaptcha {
5 /** Validate a captcha response */
6 function keyMatch( $answer, $info ) {
7 return (int)$answer == (int)$info['answer'];
10 function addCaptchaAPI( &$resultArr ) {
11 list( $sum, $answer ) = $this->pickSum();
12 $index = $this->storeCaptcha( array( 'answer' => $answer ) );
13 $resultArr['captcha']['type'] = 'math';
14 $resultArr['captcha']['mime'] = 'text/tex';
15 $resultArr['captcha']['id'] = $index;
16 $resultArr['captcha']['question'] = $sum;
19 /** Produce a nice little form */
21 list( $sum, $answer ) = $this->pickSum();
22 $index = $this->storeCaptcha( array( 'answer' => $answer ) );
24 $form = '<table><tr><td>' . $this->fetchMath( $sum ) . '</td>';
25 $form .= '<td>' . Html::input( 'wpCaptchaWord', false, false, array( 'tabindex' => '1', 'required' ) ) . '</td></tr></table>';
26 $form .= Html::hidden( 'wpCaptchaId', $index );
30 /** Pick a random sum */
32 $a = mt_rand( 0, 100 );
33 $b = mt_rand( 0, 10 );
34 $op = mt_rand( 0, 1 ) ? '+' : '-';
35 $sum = "{$a} {$op} {$b} = ";
36 $ans = $op == '+' ? ( $a + $b ) : ( $a - $b );
37 return array( $sum, $ans );
41 function fetchMath( $sum ) {
42 if( MWInit::classExists( 'MathRenderer' ) ){
43 $math = new MathRenderer( $sum );
45 throw new MWException( 'MathCaptcha requires the Math extension for MediaWiki versions 1.18 and above.' );
47 $math->setOutputMode( MW_MATH_PNG );
48 $html = $math->render();
49 return preg_replace( '/alt=".*?"/', '', $html );