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
30 * Make sure the keys are defined.
\r
32 function efReCaptcha() {
\r
33 global $wgReCaptchaPublicKey, $wgReCaptchaPrivateKey;
\r
34 global $recaptcha_public_key, $recaptcha_private_key;
\r
35 global $wgServerName;
\r
37 // Backwards compatibility
\r
38 $wgReCaptchaPublicKey == '' ) {
\r
39 $wgReCaptchaPublicKey = $recaptcha_public_key;
\r
41 if ( $wgReCaptchaPrivateKey == '' ) {
\r
42 $wgReCaptchaPrivateKey = $recaptcha_private_key;
\r
45 if ($wgReCaptchaPublicKey == '' || $wgReCaptchaPrivateKey == '') {
\r
46 die ('You need to set $wgReCaptchaPrivateKey and $wgReCaptchaPublicKey in LocalSettings.php to ' .
\r
47 "use the reCAPTCHA plugin. You can sign up for a key <a href='" .
\r
48 htmlentities(recaptcha_get_signup_url ($wgServerName, "mediawiki")) . "'>here</a>.");
\r
53 class ReCaptcha extends SimpleCaptcha {
\r
55 //reCAPTHCA error code returned from recaptcha_check_answer
\r
56 private $recaptcha_error = null;
\r
60 * Displays the reCAPTCHA widget.
\r
61 * If $this->recaptcha_error is set, it will display an error in the widget.
\r
64 function getForm() {
\r
65 global $wgReCaptchaPublicKey;
\r
66 return "<script>var RecaptchaOptions = { tabindex : 1 }; </script> " .
\r
67 recaptcha_get_html($wgReCaptchaPublicKey, $this->recaptcha_error);
\r
73 * Calls the library function recaptcha_check_answer to verify the users input.
\r
74 * Sets $this->recaptcha_error if the user is incorrect.
\r
78 function passCaptcha() {
\r
79 global $wgReCaptchaPrivateKey;
\r
80 $recaptcha_response = recaptcha_check_answer ($wgReCaptchaPrivateKey,
\r
82 $_POST['recaptcha_challenge_field'],
\r
83 $_POST['recaptcha_response_field']);
\r
84 if (!$recaptcha_response->is_valid) {
\r
85 $this->recaptcha_error = $recaptcha_response->error;
\r
88 $recaptcha_error = null;
\r
96 * Called on all edit page saves. (EditFilter events)
\r
97 * @return boolean - true if page save should continue, false if should display Captcha widget.
\r
99 function confirmEdit( $editPage, $newtext, $section ) {
\r
100 if( $this->shouldCheck( $editPage, $newtext, $section ) ) {
\r
102 if (!isset($_POST['recaptcha_response_field'])) {
\r
103 //User has not yet been presented with Captcha, show the widget.
\r
104 $editPage->showEditForm( array( &$this, 'editCallback' ) );
\r
108 if( $this->passCaptcha() ) {
\r
111 //Try again - show the widget
\r
112 $editPage->showEditForm( array( &$this, 'editCallback' ) );
\r
117 wfDebug( "ConfirmEdit: no need to show captcha.\n" );
\r
125 * Show a message asking the user to enter a captcha on edit
\r
126 * The result will be treated as wiki text
\r
128 * @param $action Action being performed
\r
131 function getMessage( $action ) {
\r
132 $name = 'recaptcha-' . $action;
\r
133 $text = wfMsg( $name );
\r
134 # Obtain a more tailored message, if possible, otherwise, fall back to
\r
135 # the default for edits
\r
136 return wfEmptyMsg( $name, $text ) ? wfMsg( 'recaptcha-edit' ) : $text;
\r