FU r102105, escaped the quotes.
[toast/cookiecaptcha.git] / ReCaptcha.php
1 <?php
2
3 /**
4  * Captcha class using the reCAPTCHA widget. 
5  * Stop Spam. Read Books.  
6  *
7  * @addtogroup Extensions
8  * @author Mike Crawford <mike.crawford@gmail.com>
9  * @copyright Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
10  * @licence MIT/X11
11  */
12
13 if ( !defined( 'MEDIAWIKI' ) ) {
14         exit;
15 }
16
17 require_once dirname( __FILE__ ) . '/ConfirmEdit.php';
18 $wgCaptchaClass = 'ReCaptcha';
19
20 $wgExtensionMessagesFiles['ReCaptcha'] = dirname( __FILE__ ) . '/ReCaptcha.i18n.php';
21
22 require_once( 'recaptchalib.php' );
23
24 // Set these in LocalSettings.php
25 $wgReCaptchaPublicKey = '';
26 $wgReCaptchaPrivateKey = '';
27 // For backwards compatibility
28 $recaptcha_public_key = '';
29 $recaptcha_private_key = '';
30
31 /**
32  * Sets the theme for ReCaptcha
33  *
34  * See http://code.google.com/apis/recaptcha/docs/customization.html
35  */
36 $wgReCaptchaTheme = 'red';
37
38 $wgExtensionFunctions[] = 'efReCaptcha';
39
40 /**
41  * Make sure the keys are defined.
42  */
43 function efReCaptcha() {
44         global $wgReCaptchaPublicKey, $wgReCaptchaPrivateKey;
45         global $recaptcha_public_key, $recaptcha_private_key;
46         global $wgServerName;
47
48         // Backwards compatibility
49         if ( $wgReCaptchaPublicKey == '' ) {
50                 $wgReCaptchaPublicKey = $recaptcha_public_key;
51         }
52         if ( $wgReCaptchaPrivateKey == '' ) {
53                 $wgReCaptchaPrivateKey = $recaptcha_private_key;
54         }
55
56         if ($wgReCaptchaPublicKey == '' || $wgReCaptchaPrivateKey == '') {
57                 die ('You need to set $wgReCaptchaPrivateKey and $wgReCaptchaPublicKey in LocalSettings.php to ' .
58                      "use the reCAPTCHA plugin. You can sign up for a key <a href='" .
59                      htmlentities(recaptcha_get_signup_url ($wgServerName, "mediawiki")) . "'>here</a>.");
60         }       
61 }
62
63
64 class ReCaptcha extends SimpleCaptcha {
65
66         //reCAPTHCA error code returned from recaptcha_check_answer
67         private $recaptcha_error = null;
68
69         /**
70          * Displays the reCAPTCHA widget.
71          * If $this->recaptcha_error is set, it will display an error in the widget.
72          *
73          */
74         function getForm() {
75                 global $wgReCaptchaPublicKey, $wgReCaptchaTheme;
76                 $useHttps = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' );
77                 $js = 'var RecaptchaOptions = ' . Xml::encodeJsVar( array( 'theme' => $wgReCaptchaTheme, 'tabindex' => 1  ) );
78
79                 return Html::inlineScript( $js ) . recaptcha_get_html($wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps);
80         }
81
82         /**
83          * Calls the library function recaptcha_check_answer to verify the users input.
84          * Sets $this->recaptcha_error if the user is incorrect.
85          * @return boolean
86          *
87          */
88         function passCaptcha() {
89                 global $wgReCaptchaPrivateKey;
90                 $recaptcha_response = recaptcha_check_answer ($wgReCaptchaPrivateKey,
91                                                               wfGetIP (),
92                                                               $_POST['recaptcha_challenge_field'],
93                                                               $_POST['recaptcha_response_field']);
94                 if (!$recaptcha_response->is_valid) {
95                         $this->recaptcha_error = $recaptcha_response->error;
96                         return false;
97                 }
98                 $recaptcha_error = null;
99                 return true;
100
101         }
102
103         /**
104          * Called on all edit page saves. (EditFilter events)
105          * @return boolean - true if page save should continue, false if should display Captcha widget.
106          */
107         function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
108                 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
109
110                         if (!isset($_POST['recaptcha_response_field'])) {
111                                         //User has not yet been presented with Captcha, show the widget.
112                                         $editPage->showEditForm( array( &$this, 'editCallback' ) );
113                                         return false;
114                         }
115
116                         if( $this->passCaptcha() ) {
117                                         return true;
118                         } else {
119                                         //Try again - show the widget
120                                         $editPage->showEditForm( array( &$this, 'editCallback' ) );
121                                         return false;
122                         }
123
124                 } else {
125                         wfDebug( "ConfirmEdit: no need to show captcha.\n" );
126                         return true;
127                 }
128         }
129
130         /**
131          * Show a message asking the user to enter a captcha on edit
132          * The result will be treated as wiki text
133          *
134          * @param $action Action being performed
135          * @return string
136          */
137         function getMessage( $action ) {
138                 $name = 'recaptcha-' . $action;
139                 $text = wfMsg( $name );
140                 # Obtain a more tailored message, if possible, otherwise, fall back to
141                 # the default for edits
142                 return wfEmptyMsg( $name, $text ) ? wfMsg( 'recaptcha-edit' ) : $text;
143         }
144
145 }