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 = '';
31 $wgExtensionFunctions[] = 'efReCaptcha';
34 * Make sure the keys are defined.
36 function efReCaptcha() {
37 global $wgReCaptchaPublicKey, $wgReCaptchaPrivateKey;
38 global $recaptcha_public_key, $recaptcha_private_key;
41 // Backwards compatibility
42 if ( $wgReCaptchaPublicKey == '' ) {
43 $wgReCaptchaPublicKey = $recaptcha_public_key;
45 if ( $wgReCaptchaPrivateKey == '' ) {
46 $wgReCaptchaPrivateKey = $recaptcha_private_key;
49 if ($wgReCaptchaPublicKey == '' || $wgReCaptchaPrivateKey == '') {
50 die ('You need to set $wgReCaptchaPrivateKey and $wgReCaptchaPublicKey in LocalSettings.php to ' .
51 "use the reCAPTCHA plugin. You can sign up for a key <a href='" .
52 htmlentities(recaptcha_get_signup_url ($wgServerName, "mediawiki")) . "'>here</a>.");
57 class ReCaptcha extends SimpleCaptcha {
59 //reCAPTHCA error code returned from recaptcha_check_answer
60 private $recaptcha_error = null;
63 * Displays the reCAPTCHA widget.
64 * If $this->recaptcha_error is set, it will display an error in the widget.
68 global $wgReCaptchaPublicKey;
69 $useHttps = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' );
70 return "<script>var RecaptchaOptions = { tabindex : 1 }; </script> " .
71 recaptcha_get_html($wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps);
75 * Calls the library function recaptcha_check_answer to verify the users input.
76 * Sets $this->recaptcha_error if the user is incorrect.
80 function passCaptcha() {
81 global $wgReCaptchaPrivateKey;
82 $recaptcha_response = recaptcha_check_answer ($wgReCaptchaPrivateKey,
84 $_POST['recaptcha_challenge_field'],
85 $_POST['recaptcha_response_field']);
86 if (!$recaptcha_response->is_valid) {
87 $this->recaptcha_error = $recaptcha_response->error;
90 $recaptcha_error = null;
96 * Called on all edit page saves. (EditFilter events)
97 * @return boolean - true if page save should continue, false if should display Captcha widget.
99 function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
100 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
102 if (!isset($_POST['recaptcha_response_field'])) {
103 //User has not yet been presented with Captcha, show the widget.
104 $editPage->showEditForm( array( &$this, 'editCallback' ) );
108 if( $this->passCaptcha() ) {
111 //Try again - show the widget
112 $editPage->showEditForm( array( &$this, 'editCallback' ) );
117 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
123 * Show a message asking the user to enter a captcha on edit
124 * The result will be treated as wiki text
126 * @param $action Action being performed
129 function getMessage( $action ) {
130 $name = 'recaptcha-' . $action;
131 $text = wfMsg( $name );
132 # Obtain a more tailored message, if possible, otherwise, fall back to
133 # the default for edits
134 return wfEmptyMsg( $name, $text ) ? wfMsg( 'recaptcha-edit' ) : $text;