4 * Captcha class using simple sums and the math renderer
5 * Not brilliant, but enough to dissuade casual spam bots
8 * @subpackage Extensions
9 * @author Rob Church <robchur@gmail.com>
10 * @copyright © 2006 Rob Church
11 * @licence GNU General Public Licence 2.0
14 if( defined( 'MEDIAWIKI' ) ) {
16 class MathCaptcha extends SimpleCaptcha {
18 /** Validate a captcha response */
19 function keyMatch( $req, $info ) {
20 return (int)$req->getVal( 'wpCaptchaAnswer' ) == (int)$info['answer'];
23 /** Produce a nice little form */
25 list( $sum, $answer ) = $this->pickSum();
26 $index = $this->storeCaptcha( array( 'answer' => $answer ) );
28 $form = '<table><tr><td>' . $this->fetchMath( $sum ) . '</td>';
29 $form .= '<td>' . wfInput( 'wpCaptchaAnswer', false, false, array( 'tabindex' => '1' ) ) . '</td></tr></table>';
30 $form .= wfHidden( 'wpCaptchaId', $index );
34 /** Pick a random sum */
36 $a = mt_rand( 0, 100 );
37 $b = mt_rand( 0, 10 );
38 $op = mt_rand( 0, 1 ) ? '+' : '-';
39 $sum = "{$a} {$op} {$b} = ";
40 $ans = $op == '+' ? ( $a + $b ) : ( $a - $b );
41 return array( $sum, $ans );
45 function fetchMath( $sum ) {
46 $math = new MathRenderer( $sum );
47 $math->setOutputMode( MW_MATH_PNG );
48 $html = $math->render();
49 return preg_replace( '/alt=".*"/', '', $html );
55 echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );