~ubuntu-branches/ubuntu/precise/mediawiki-extensions/precise

« back to all changes in this revision

Viewing changes to dist/mediawiki-extensions-confirmedit/usr/share/mediawiki-extensions/confirmedit/MathCaptcha.class.php

  • Committer: Bazaar Package Importer
  • Author(s): Romain Beauxis
  • Date: 2010-05-04 15:13:35 UTC
  • mfrom: (0.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100504151335-54qeucg3ec108q28
Tags: 2.2
* Added Replaces:/Conflicts: to allow a proper upgrade.
Closes: #580066
* Fixed package descriptions.
Closes: #579667
* Patched mediawiki-extensions-fckeditor to make it work with
  php 5.3. The fix may not be perfect but at least it work.
  Not closing the bug (#579822) for now..

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
class MathCaptcha extends SimpleCaptcha {
 
4
 
 
5
        /** Validate a captcha response */
 
6
        function keyMatch( $answer, $info ) {
 
7
                return (int)$answer == (int)$info['answer'];
 
8
        }
 
9
 
 
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;
 
17
        }
 
18
        
 
19
        /** Produce a nice little form */
 
20
        function getForm() {
 
21
                list( $sum, $answer ) = $this->pickSum();
 
22
                $index = $this->storeCaptcha( array( 'answer' => $answer ) );
 
23
                
 
24
                $form = '<table><tr><td>' . $this->fetchMath( $sum ) . '</td>';
 
25
                $form .= '<td>' . Xml::input( 'wpCaptchaWord', false, false, array( 'tabindex' => '1' ) ) . '</td></tr></table>';
 
26
                $form .= Xml::hidden( 'wpCaptchaId', $index );
 
27
                return $form;
 
28
        }
 
29
        
 
30
        /** Pick a random sum */
 
31
        function pickSum() {
 
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 );
 
38
        }
 
39
        
 
40
        /** Fetch the math */
 
41
        function fetchMath( $sum ) {
 
42
                $math = new MathRenderer( $sum );
 
43
                $math->setOutputMode( MW_MATH_PNG );
 
44
                $html = $math->render();
 
45
                return preg_replace( '/alt=".*?"/', '', $html );
 
46
        }
 
47
 
 
48
}
 
49
?>