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;
60 * Displays the reCAPTCHA widget.
61 * If $this->recaptcha_error is set, it will display an error in the widget.
65 global $wgReCaptchaPublicKey;
66 return "<script>var RecaptchaOptions = { tabindex : 1 }; </script> " .
67 recaptcha_get_html($wgReCaptchaPublicKey, $this->recaptcha_error);
71 * Calls the library function recaptcha_check_answer to verify the users input.
72 * Sets $this->recaptcha_error if the user is incorrect.
76 function passCaptcha() {
77 global $wgReCaptchaPrivateKey;
78 $recaptcha_response = recaptcha_check_answer ($wgReCaptchaPrivateKey,
80 $_POST['recaptcha_challenge_field'],
81 $_POST['recaptcha_response_field']);
82 if (!$recaptcha_response->is_valid) {
83 $this->recaptcha_error = $recaptcha_response->error;
86 $recaptcha_error = null;
92 * Called on all edit page saves. (EditFilter events)
93 * @return boolean - true if page save should continue, false if should display Captcha widget.
95 function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
96 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
98 if (!isset($_POST['recaptcha_response_field'])) {
99 //User has not yet been presented with Captcha, show the widget.
100 $editPage->showEditForm( array( &$this, 'editCallback' ) );
104 if( $this->passCaptcha() ) {
107 //Try again - show the widget
108 $editPage->showEditForm( array( &$this, 'editCallback' ) );
113 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
119 * Show a message asking the user to enter a captcha on edit
120 * The result will be treated as wiki text
122 * @param $action Action being performed
125 function getMessage( $action ) {
126 $name = 'recaptcha-' . $action;
127 $text = wfMsg( $name );
128 # Obtain a more tailored message, if possible, otherwise, fall back to
129 # the default for edits
130 return wfEmptyMsg( $name, $text ) ? wfMsg( 'recaptcha-edit' ) : $text;