b7c3d27db74e8df7ed7ceb060cfa2299a4df5f14
[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                 $escapedTheme = Xml::escapeJsString( $wgReCaptchaTheme );
78
79                 return "<script>var RecaptchaOptions = { theme : '$escapedTheme', tabindex : 1 }; </script> " .
80                         recaptcha_get_html($wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps);
81         }
82
83         /**
84          * Calls the library function recaptcha_check_answer to verify the users input.
85          * Sets $this->recaptcha_error if the user is incorrect.
86          * @return boolean
87          *
88          */
89         function passCaptcha() {
90                 global $wgReCaptchaPrivateKey;
91                 $recaptcha_response = recaptcha_check_answer ($wgReCaptchaPrivateKey,
92                                                               wfGetIP (),
93                                                               $_POST['recaptcha_challenge_field'],
94                                                               $_POST['recaptcha_response_field']);
95                 if (!$recaptcha_response->is_valid) {
96                         $this->recaptcha_error = $recaptcha_response->error;
97                         return false;
98                 }
99                 $recaptcha_error = null;
100                 return true;
101
102         }
103
104         /**
105          * Called on all edit page saves. (EditFilter events)
106          * @return boolean - true if page save should continue, false if should display Captcha widget.
107          */
108         function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
109                 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
110
111                         if (!isset($_POST['recaptcha_response_field'])) {
112                                         //User has not yet been presented with Captcha, show the widget.
113                                         $editPage->showEditForm( array( &$this, 'editCallback' ) );
114                                         return false;
115                         }
116
117                         if( $this->passCaptcha() ) {
118                                         return true;
119                         } else {
120                                         //Try again - show the widget
121                                         $editPage->showEditForm( array( &$this, 'editCallback' ) );
122                                         return false;
123                         }
124
125                 } else {
126                         wfDebug( "ConfirmEdit: no need to show captcha.\n" );
127                         return true;
128                 }
129         }
130
131         /**
132          * Show a message asking the user to enter a captcha on edit
133          * The result will be treated as wiki text
134          *
135          * @param $action Action being performed
136          * @return string
137          */
138         function getMessage( $action ) {
139                 $name = 'recaptcha-' . $action;
140                 $text = wfMsg( $name );
141                 # Obtain a more tailored message, if possible, otherwise, fall back to
142                 # the default for edits
143                 return wfEmptyMsg( $name, $text ) ? wfMsg( 'recaptcha-edit' ) : $text;
144         }
145
146 }