moar w/s fixups
[toast/cookiecaptcha.git] / ReCaptcha.php
index b068b86eab87a093db41451d2456682942d2707b..8f6b97b605c09df9be30f51222273c10f2c505c0 100644 (file)
@@ -1,8 +1,8 @@
 <?php
 
 /**
- * Captcha class using the reCAPTCHA widget. 
- * Stop Spam. Read Books.  
+ * Captcha class using the reCAPTCHA widget.
+ * Stop Spam. Read Books.
  *
  * @addtogroup Extensions
  * @author Mike Crawford <mike.crawford@gmail.com>
  * @licence MIT/X11
  */
 
-if( !defined( 'MEDIAWIKI' ) ) {
+if ( !defined( 'MEDIAWIKI' ) ) {
        exit;
 }
 
+require_once dirname( __FILE__ ) . '/ConfirmEdit.php';
+$wgCaptchaClass = 'ReCaptcha';
+
 $wgExtensionMessagesFiles['ReCaptcha'] = dirname( __FILE__ ) . '/ReCaptcha.i18n.php';
 
 require_once( 'recaptchalib.php' );
@@ -25,6 +28,13 @@ $wgReCaptchaPrivateKey = '';
 $recaptcha_public_key = '';
 $recaptcha_private_key = '';
 
+/**
+ * Sets the theme for ReCaptcha
+ *
+ * See http://code.google.com/apis/recaptcha/docs/customization.html
+ */
+$wgReCaptchaTheme = 'red';
+
 $wgExtensionFunctions[] = 'efReCaptcha';
 
 /**
@@ -45,9 +55,9 @@ function efReCaptcha() {
 
        if ($wgReCaptchaPublicKey == '' || $wgReCaptchaPrivateKey == '') {
                die ('You need to set $wgReCaptchaPrivateKey and $wgReCaptchaPublicKey in LocalSettings.php to ' .
-                    "use the reCAPTCHA plugin. You can sign up for a key <a href='" .
-                    htmlentities(recaptcha_get_signup_url ($wgServerName, "mediawiki")) . "'>here</a>.");
-       }       
+                       "use the reCAPTCHA plugin. You can sign up for a key <a href='" .
+                       htmlentities(recaptcha_get_signup_url ($wgServerName, "mediawiki")) . "'>here</a>.");
+       }
 }
 
 
@@ -58,34 +68,38 @@ class ReCaptcha extends SimpleCaptcha {
 
        /**
         * Displays the reCAPTCHA widget.
-         * If $this->recaptcha_error is set, it will display an error in the widget.
+        * If $this->recaptcha_error is set, it will display an error in the widget.
         *
-         */
+        */
        function getForm() {
-               global $wgReCaptchaPublicKey;
+               global $wgReCaptchaPublicKey, $wgReCaptchaTheme;
                $useHttps = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' );
-               return "<script>var RecaptchaOptions = { tabindex : 1 }; </script> " .
-                       recaptcha_get_html($wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps);
+               $js = 'var RecaptchaOptions = ' . Xml::encodeJsVar( array( 'theme' => $wgReCaptchaTheme, 'tabindex' => 1  ) );
+
+               return Html::inlineScript( $js ) . recaptcha_get_html($wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps);
        }
 
        /**
         * Calls the library function recaptcha_check_answer to verify the users input.
         * Sets $this->recaptcha_error if the user is incorrect.
-         * @return boolean
-         *
-         */
+        * @return boolean
+        *
+        */
        function passCaptcha() {
                global $wgReCaptchaPrivateKey;
-               $recaptcha_response = recaptcha_check_answer ($wgReCaptchaPrivateKey,
-                                                             wfGetIP (),
-                                                             $_POST['recaptcha_challenge_field'],
-                                                             $_POST['recaptcha_response_field']);
-                if (!$recaptcha_response->is_valid) {
+               $recaptcha_response =
+                       recaptcha_check_answer (
+                               $wgReCaptchaPrivateKey,
+                               wfGetIP (),
+                               $_POST['recaptcha_challenge_field'],
+                               $_POST['recaptcha_response_field']
+                       );
+               if (!$recaptcha_response->is_valid) {
                        $this->recaptcha_error = $recaptcha_response->error;
                        return false;
-                }
+               }
                $recaptcha_error = null;
-                return true;
+               return true;
 
        }
 
@@ -97,17 +111,17 @@ class ReCaptcha extends SimpleCaptcha {
                if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
 
                        if (!isset($_POST['recaptcha_response_field'])) {
-                                       //User has not yet been presented with Captcha, show the widget.
-                                       $editPage->showEditForm( array( &$this, 'editCallback' ) );
-                                       return false;
+                               //User has not yet been presented with Captcha, show the widget.
+                               $editPage->showEditForm( array( &$this, 'editCallback' ) );
+                               return false;
                        }
 
                        if( $this->passCaptcha() ) {
-                                       return true;
+                               return true;
                        } else {
-                                       //Try again - show the widget
-                                       $editPage->showEditForm( array( &$this, 'editCallback' ) );
-                                       return false;
+                               //Try again - show the widget
+                               $editPage->showEditForm( array( &$this, 'editCallback' ) );
+                               return false;
                        }
 
                } else {
@@ -131,4 +145,11 @@ class ReCaptcha extends SimpleCaptcha {
                return wfEmptyMsg( $name, $text ) ? wfMsg( 'recaptcha-edit' ) : $text;
        }
 
+       public function APIGetAllowedParams( &$module, &$params ) {
+               return true;
+       }
+
+       public function APIGetParamDescription( &$module, &$desc ) {
+               return true;
+       }
 }