4 * Captcha class using the reCAPTCHA widget.
\r
5 * Stop Spam. Read Books.
\r
7 * @addtogroup Extensions
\r
8 * @author Mike Crawford <mike.crawford@gmail.com>
\r
9 * @copyright Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
\r
13 if( defined( 'MEDIAWIKI' ) ) {
\r
14 require_once( 'recaptchalib.php' );
\r
16 // make sure we have the confirm edit plugin
\r
17 require_once( 'ConfirmEdit.php' );
\r
19 // Set the default CAPTCHA to be reCAPTCHA.
\r
20 $wgCaptchaClass = 'ReCaptcha';
\r
23 global $recaptcha_public_key, $recaptcha_private_key;
\r
25 // Set these in LocalSettings.php
\r
26 $recaptcha_public_key = '';
\r
27 $recaptcha_private_key = '';
\r
30 $wgExtensionFunctions[] = 'efReCaptcha';
\r
33 * Add the reCAPTCHA messages
\r
36 function efReCaptcha() {
\r
37 global $wgMessageCache;
\r
38 global $recaptcha_public_key, $recaptcha_private_key;
\r
39 global $wgServerName;
\r
42 require_once( dirname( __FILE__ ) . '/ReCaptcha.i18n.php' );
\r
43 foreach( efReCaptchaMessages() as $lang => $messages )
\r
44 $wgMessageCache->addMessages( $messages, $lang );
\r
46 if ($recaptcha_private_key == '' || $recaptcha_public_key == '') {
\r
47 die ('You need to set $recaptcha_private_key and $recaptcha_public_key in LocalSettings.php to ' .
\r
48 "use the reCAPTCHA plugin. You can sign up for a key <a href='" .
\r
49 htmlentities(recaptcha_get_signup_url ($wgServerName, "mediawiki")) . "'>here</a>.");
\r
54 class ReCaptcha extends SimpleCaptcha {
\r
56 //reCAPTHCA error code returned from recaptcha_check_answer
\r
57 private $recaptcha_error = null;
\r
61 * Displays the reCAPTCHA widget.
\r
62 * If $this->recaptcha_error is set, it will display an error in the widget.
\r
65 function getForm() {
\r
66 global $recaptcha_public_key;
\r
67 return "<script>var RecaptchaOptions = { tabindex : 1 }; </script> " .
\r
68 recaptcha_get_html($recaptcha_public_key, $this->recaptcha_error);
\r
74 * Calls the library function recaptcha_check_answer to verify the users input.
\r
75 * Sets $this->recaptcha_error if the user is incorrect.
\r
79 function passCaptcha() {
\r
80 global $recaptcha_private_key;
\r
81 $recaptcha_response = recaptcha_check_answer ($recaptcha_private_key,
\r
83 $_POST['recaptcha_challenge_field'],
\r
84 $_POST['recaptcha_response_field']);
\r
85 if (!$recaptcha_response->is_valid) {
\r
86 $this->recaptcha_error = $recaptcha_response->error;
\r
89 $recaptcha_error = null;
\r
97 * Called on all edit page saves. (EditFilter events)
\r
98 * @return boolean - true if page save should continue, false if should display Captcha widget.
\r
100 function confirmEdit( &$editPage, $newtext, $section ) {
\r
101 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
\r
103 if (!isset($_POST['recaptcha_response_field'])) {
\r
104 //User has not yet been presented with Captcha, show the widget.
\r
105 $editPage->showEditForm( array( &$this, 'editCallback' ) );
\r
109 if( $this->passCaptcha() ) {
\r
112 //Try again - show the widget
\r
113 $editPage->showEditForm( array( &$this, 'editCallback' ) );
\r
118 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
\r
126 * Show a message asking the user to enter a captcha on edit
\r
127 * The result will be treated as wiki text
\r
129 * @param $action Action being performed
\r
132 function getMessage( $action ) {
\r
133 $name = 'recaptcha-' . $action;
\r
134 $text = wfMsg( $name );
\r
135 # Obtain a more tailored message, if possible, otherwise, fall back to
\r
136 # the default for edits
\r
137 return wfEmptyMsg( $name, $text ) ? wfMsg( 'recaptcha-edit' ) : $text;
\r
143 echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
\r