4 * Captcha class using the reCAPTCHA widget.
5 * Stop Spam. Read Books.
7 * @addtogroup Extensions
8 * @author Mike Crawford <mike.crawford@gmail.com>
9 * @copyright Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
13 if ( !defined( 'MEDIAWIKI' ) ) {
17 require_once dirname( __FILE__ ) . '/ConfirmEdit.php';
18 $wgCaptchaClass = 'ReCaptcha';
20 $wgExtensionMessagesFiles['ReCaptcha'] = dirname( __FILE__ ) . '/ReCaptcha.i18n.php';
22 require_once( 'recaptchalib.php' );
24 // Set these in LocalSettings.php
25 $wgReCaptchaPublicKey = '';
26 $wgReCaptchaPrivateKey = '';
27 // For backwards compatibility
28 $recaptcha_public_key = '';
29 $recaptcha_private_key = '';
32 * Sets the theme for ReCaptcha
34 * See http://code.google.com/apis/recaptcha/docs/customization.html
36 $wgReCaptchaTheme = 'red';
38 $wgExtensionFunctions[] = 'efReCaptcha';
41 * Make sure the keys are defined.
43 function efReCaptcha() {
44 global $wgReCaptchaPublicKey, $wgReCaptchaPrivateKey;
45 global $recaptcha_public_key, $recaptcha_private_key;
48 // Backwards compatibility
49 if ( $wgReCaptchaPublicKey == '' ) {
50 $wgReCaptchaPublicKey = $recaptcha_public_key;
52 if ( $wgReCaptchaPrivateKey == '' ) {
53 $wgReCaptchaPrivateKey = $recaptcha_private_key;
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>.");
64 class ReCaptcha extends SimpleCaptcha {
66 //reCAPTHCA error code returned from recaptcha_check_answer
67 private $recaptcha_error = null;
70 * Displays the reCAPTCHA widget.
71 * If $this->recaptcha_error is set, it will display an error in the widget.
75 global $wgReCaptchaPublicKey, $wgReCaptchaTheme;
76 $useHttps = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' );
77 $escapedTheme = Xml::escapeJsString( $wgReCaptchaTheme );
79 return "<script>var RecaptchaOptions = { theme : '$escapedTheme', tabindex : 1 }; </script> " .
80 recaptcha_get_html($wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps);
84 * Calls the library function recaptcha_check_answer to verify the users input.
85 * Sets $this->recaptcha_error if the user is incorrect.
89 function passCaptcha() {
90 global $wgReCaptchaPrivateKey;
91 $recaptcha_response = recaptcha_check_answer ($wgReCaptchaPrivateKey,
93 $_POST['recaptcha_challenge_field'],
94 $_POST['recaptcha_response_field']);
95 if (!$recaptcha_response->is_valid) {
96 $this->recaptcha_error = $recaptcha_response->error;
99 $recaptcha_error = null;
105 * Called on all edit page saves. (EditFilter events)
106 * @return boolean - true if page save should continue, false if should display Captcha widget.
108 function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
109 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
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' ) );
117 if( $this->passCaptcha() ) {
120 //Try again - show the widget
121 $editPage->showEditForm( array( &$this, 'editCallback' ) );
126 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
132 * Show a message asking the user to enter a captcha on edit
133 * The result will be treated as wiki text
135 * @param $action Action being performed
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;