b0c15fbda2edc1c9539acbaa08277986c29391ae
[toast/cookiecaptcha.git] / ReCaptcha.class.php
1 <?php
2
3 class ReCaptcha extends SimpleCaptcha {
4         // reCAPTHCA error code returned from recaptcha_check_answer
5         private $recaptcha_error = null;
6
7         /**
8          * Displays the reCAPTCHA widget.
9          * If $this->recaptcha_error is set, it will display an error in the widget.
10          *
11          */
12         function getForm() {
13                 global $wgReCaptchaPublicKey, $wgReCaptchaTheme;
14
15                 $useHttps = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' );
16                 $js = 'var RecaptchaOptions = ' . Xml::encodeJsVar( array( 'theme' => $wgReCaptchaTheme, 'tabindex' => 1  ) );
17
18                 return Html::inlineScript( $js ) . recaptcha_get_html( $wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps );
19         }
20
21         /**
22          * Calls the library function recaptcha_check_answer to verify the users input.
23          * Sets $this->recaptcha_error if the user is incorrect.
24          * @return boolean
25          *
26          */
27         function passCaptcha() {
28                 global $wgReCaptchaPrivateKey, $wgRequest;
29
30                 // API is hardwired to return wpCaptchaId and wpCaptchaWord, so use that if the standard two are empty
31                 $challenge = $wgRequest->getVal( 'recaptcha_challenge_field', $wgRequest->getVal( 'wpCaptchaId' ) );
32                 $response = $wgRequest->getVal( 'recaptcha_response_field', $wgRequest->getVal( 'wpCaptchaWord' ) );
33
34                 if ( $response === null ) {
35                         // new captcha session
36                         return false;
37                 }
38
39                 $recaptcha_response = recaptcha_check_answer(
40                         $wgReCaptchaPrivateKey,
41                         $wgRequest->getIP(),
42                         $challenge,
43                         $response
44                 );
45
46                 if ( !$recaptcha_response->is_valid ) {
47                         $this->recaptcha_error = $recaptcha_response->error;
48                         return false;
49                 }
50
51                 $recaptcha_error = null;
52
53                 return true;
54
55         }
56
57         function addCaptchaAPI( &$resultArr ) {
58                 global $wgReCaptchaPublicKey;
59
60                 $resultArr['captcha']['type'] = 'recaptcha';
61                 $resultArr['captcha']['mime'] = 'image/png';
62                 $resultArr['captcha']['key'] = $wgReCaptchaPublicKey;
63                 $resultArr['captcha']['error'] = $this->recaptcha_error;
64         }
65
66         /**
67          * Show a message asking the user to enter a captcha on edit
68          * The result will be treated as wiki text
69          *
70          * @param $action Action being performed
71          * @return string
72          */
73         function getMessage( $action ) {
74                 $name = 'recaptcha-' . $action;
75                 $text = wfMsg( $name );
76
77                 # Obtain a more tailored message, if possible, otherwise, fall back to
78                 # the default for edits
79                 return wfEmptyMsg( $name, $text ) ? wfMsg( 'recaptcha-edit' ) : $text;
80         }
81
82         public function APIGetAllowedParams( &$module, &$params ) {
83                 return true;
84         }
85
86         public function APIGetParamDescription( &$module, &$desc ) {
87                 return true;
88         }
89 }