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
17 $wgExtensionMessagesFiles['ReCaptcha'] = dirname( __FILE__ ) . '/ReCaptcha.i18n.php';
\r
19 require_once( 'recaptchalib.php' );
\r
21 // Set these in LocalSettings.php
\r
22 $wgReCaptchaPublicKey = '';
\r
23 $wgReCaptchaPrivateKey = '';
\r
24 // For backwards compatibility
\r
25 $recaptcha_public_key = '';
\r
26 $recaptcha_private_key = '';
\r
28 $wgExtensionFunctions[] = 'efReCaptcha';
\r
31 * Make sure the keys are defined.
\r
33 function efReCaptcha() {
\r
34 global $wgReCaptchaPublicKey, $wgReCaptchaPrivateKey;
\r
35 global $recaptcha_public_key, $recaptcha_private_key;
\r
36 global $wgServerName;
\r
38 // Backwards compatibility
\r
39 if ( $wgReCaptchaPublicKey == '' ) {
\r
40 $wgReCaptchaPublicKey = $recaptcha_public_key;
\r
42 if ( $wgReCaptchaPrivateKey == '' ) {
\r
43 $wgReCaptchaPrivateKey = $recaptcha_private_key;
\r
46 if ($wgReCaptchaPublicKey == '' || $wgReCaptchaPrivateKey == '') {
\r
47 die ('You need to set $wgReCaptchaPrivateKey and $wgReCaptchaPublicKey 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 $wgReCaptchaPublicKey;
\r
67 return "<script>var RecaptchaOptions = { tabindex : 1 }; </script> " .
\r
68 recaptcha_get_html($wgReCaptchaPublicKey, $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 $wgReCaptchaPrivateKey;
\r
81 $recaptcha_response = recaptcha_check_answer ($wgReCaptchaPrivateKey,
\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