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 $js = 'var RecaptchaOptions = ' . Xml::encodeJsVar( array( 'theme' => $wgReCaptchaTheme, 'tabindex' => 1 ) );
79 return Html::inlineScript( $js ) . recaptcha_get_html($wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps);
83 * Calls the library function recaptcha_check_answer to verify the users input.
84 * Sets $this->recaptcha_error if the user is incorrect.
88 function passCaptcha() {
89 global $wgReCaptchaPrivateKey;
90 $recaptcha_response = recaptcha_check_answer ($wgReCaptchaPrivateKey,
92 $_POST['recaptcha_challenge_field'],
93 $_POST['recaptcha_response_field']);
94 if (!$recaptcha_response->is_valid) {
95 $this->recaptcha_error = $recaptcha_response->error;
98 $recaptcha_error = null;
104 * Called on all edit page saves. (EditFilter events)
105 * @return boolean - true if page save should continue, false if should display Captcha widget.
107 function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
108 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
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' ) );
116 if( $this->passCaptcha() ) {
119 //Try again - show the widget
120 $editPage->showEditForm( array( &$this, 'editCallback' ) );
125 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
131 * Show a message asking the user to enter a captcha on edit
132 * The result will be treated as wiki text
134 * @param $action Action being performed
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;