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 $wgExtensionMessagesFiles['ReCaptcha'] = dirname( __FILE__ ) . '/ReCaptcha.i18n.php';
19 require_once( 'recaptchalib.php' );
21 // Set these in LocalSettings.php
22 $wgReCaptchaPublicKey = '';
23 $wgReCaptchaPrivateKey = '';
24 // For backwards compatibility
25 $recaptcha_public_key = '';
26 $recaptcha_private_key = '';
28 $wgExtensionFunctions[] = 'efReCaptcha';
31 * Make sure the keys are defined.
33 function efReCaptcha() {
34 global $wgReCaptchaPublicKey, $wgReCaptchaPrivateKey;
35 global $recaptcha_public_key, $recaptcha_private_key;
38 // Backwards compatibility
39 if ( $wgReCaptchaPublicKey == '' ) {
40 $wgReCaptchaPublicKey = $recaptcha_public_key;
42 if ( $wgReCaptchaPrivateKey == '' ) {
43 $wgReCaptchaPrivateKey = $recaptcha_private_key;
46 if ($wgReCaptchaPublicKey == '' || $wgReCaptchaPrivateKey == '') {
47 die ('You need to set $wgReCaptchaPrivateKey and $wgReCaptchaPublicKey in LocalSettings.php to ' .
48 "use the reCAPTCHA plugin. You can sign up for a key <a href='" .
49 htmlentities(recaptcha_get_signup_url ($wgServerName, "mediawiki")) . "'>here</a>.");
54 class ReCaptcha extends SimpleCaptcha {
56 //reCAPTHCA error code returned from recaptcha_check_answer
57 private $recaptcha_error = null;
61 * Displays the reCAPTCHA widget.
62 * If $this->recaptcha_error is set, it will display an error in the widget.
66 global $wgReCaptchaPublicKey;
67 return "<script>var RecaptchaOptions = { tabindex : 1 }; </script> " .
68 recaptcha_get_html($wgReCaptchaPublicKey, $this->recaptcha_error);
74 * Calls the library function recaptcha_check_answer to verify the users input.
75 * Sets $this->recaptcha_error if the user is incorrect.
79 function passCaptcha() {
80 global $wgReCaptchaPrivateKey;
81 $recaptcha_response = recaptcha_check_answer ($wgReCaptchaPrivateKey,
83 $_POST['recaptcha_challenge_field'],
84 $_POST['recaptcha_response_field']);
85 if (!$recaptcha_response->is_valid) {
86 $this->recaptcha_error = $recaptcha_response->error;
89 $recaptcha_error = null;
97 * Called on all edit page saves. (EditFilter events)
98 * @return boolean - true if page save should continue, false if should display Captcha widget.
100 function confirmEdit( $editPage, $newtext, $section ) {
101 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
103 if (!isset($_POST['recaptcha_response_field'])) {
104 //User has not yet been presented with Captcha, show the widget.
105 $editPage->showEditForm( array( &$this, 'editCallback' ) );
109 if( $this->passCaptcha() ) {
112 //Try again - show the widget
113 $editPage->showEditForm( array( &$this, 'editCallback' ) );
118 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
126 * Show a message asking the user to enter a captcha on edit
127 * The result will be treated as wiki text
129 * @param $action Action being performed
132 function getMessage( $action ) {
133 $name = 'recaptcha-' . $action;
134 $text = wfMsg( $name );
135 # Obtain a more tailored message, if possible, otherwise, fall back to
136 # the default for edits
137 return wfEmptyMsg( $name, $text ) ? wfMsg( 'recaptcha-edit' ) : $text;