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 $useHttps = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' );
67 return "<script>var RecaptchaOptions = { tabindex : 1 }; </script> " .
68 recaptcha_get_html($wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps);
72 * Calls the library function recaptcha_check_answer to verify the users input.
73 * Sets $this->recaptcha_error if the user is incorrect.
77 function passCaptcha() {
78 global $wgReCaptchaPrivateKey;
79 $recaptcha_response = recaptcha_check_answer ($wgReCaptchaPrivateKey,
81 $_POST['recaptcha_challenge_field'],
82 $_POST['recaptcha_response_field']);
83 if (!$recaptcha_response->is_valid) {
84 $this->recaptcha_error = $recaptcha_response->error;
87 $recaptcha_error = null;
93 * Called on all edit page saves. (EditFilter events)
94 * @return boolean - true if page save should continue, false if should display Captcha widget.
96 function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
97 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
99 if (!isset($_POST['recaptcha_response_field'])) {
100 //User has not yet been presented with Captcha, show the widget.
101 $editPage->showEditForm( array( &$this, 'editCallback' ) );
105 if( $this->passCaptcha() ) {
108 //Try again - show the widget
109 $editPage->showEditForm( array( &$this, 'editCallback' ) );
114 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
120 * Show a message asking the user to enter a captcha on edit
121 * The result will be treated as wiki text
123 * @param $action Action being performed
126 function getMessage( $action ) {
127 $name = 'recaptcha-' . $action;
128 $text = wfMsg( $name );
129 # Obtain a more tailored message, if possible, otherwise, fall back to
130 # the default for edits
131 return wfEmptyMsg( $name, $text ) ? wfMsg( 'recaptcha-edit' ) : $text;