2011-01-31 09:50:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2011-11-23 19:09:57 +00:00
|
|
|
* Captcha class using the reCAPTCHA widget.
|
|
|
|
* Stop Spam. Read Books.
|
2011-01-31 09:50:27 +00:00
|
|
|
*
|
|
|
|
* @addtogroup Extensions
|
|
|
|
* @author Mike Crawford <mike.crawford@gmail.com>
|
|
|
|
* @copyright Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
|
|
|
|
* @licence MIT/X11
|
|
|
|
*/
|
|
|
|
|
2011-10-14 23:09:19 +00:00
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) {
|
2011-01-31 09:50:27 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2011-10-14 23:09:19 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/ConfirmEdit.php';
|
|
|
|
$wgCaptchaClass = 'ReCaptcha';
|
|
|
|
|
2011-01-31 09:50:27 +00:00
|
|
|
$wgExtensionMessagesFiles['ReCaptcha'] = dirname( __FILE__ ) . '/ReCaptcha.i18n.php';
|
|
|
|
|
|
|
|
require_once( 'recaptchalib.php' );
|
|
|
|
|
|
|
|
// Set these in LocalSettings.php
|
|
|
|
$wgReCaptchaPublicKey = '';
|
|
|
|
$wgReCaptchaPrivateKey = '';
|
|
|
|
// For backwards compatibility
|
|
|
|
$recaptcha_public_key = '';
|
|
|
|
$recaptcha_private_key = '';
|
|
|
|
|
2011-10-26 14:51:34 +00:00
|
|
|
/**
|
|
|
|
* Sets the theme for ReCaptcha
|
|
|
|
*
|
|
|
|
* See http://code.google.com/apis/recaptcha/docs/customization.html
|
|
|
|
*/
|
|
|
|
$wgReCaptchaTheme = 'red';
|
|
|
|
|
2011-01-31 09:50:27 +00:00
|
|
|
$wgExtensionFunctions[] = 'efReCaptcha';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure the keys are defined.
|
|
|
|
*/
|
|
|
|
function efReCaptcha() {
|
|
|
|
global $wgReCaptchaPublicKey, $wgReCaptchaPrivateKey;
|
|
|
|
global $recaptcha_public_key, $recaptcha_private_key;
|
|
|
|
global $wgServerName;
|
|
|
|
|
|
|
|
// Backwards compatibility
|
|
|
|
if ( $wgReCaptchaPublicKey == '' ) {
|
|
|
|
$wgReCaptchaPublicKey = $recaptcha_public_key;
|
|
|
|
}
|
|
|
|
if ( $wgReCaptchaPrivateKey == '' ) {
|
|
|
|
$wgReCaptchaPrivateKey = $recaptcha_private_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($wgReCaptchaPublicKey == '' || $wgReCaptchaPrivateKey == '') {
|
|
|
|
die ('You need to set $wgReCaptchaPrivateKey and $wgReCaptchaPublicKey in LocalSettings.php to ' .
|
2011-12-01 16:51:18 +00:00
|
|
|
"use the reCAPTCHA plugin. You can sign up for a key <a href='" .
|
|
|
|
htmlentities(recaptcha_get_signup_url ($wgServerName, "mediawiki")) . "'>here</a>.");
|
2011-11-23 19:09:57 +00:00
|
|
|
}
|
2011-01-31 09:50:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ReCaptcha extends SimpleCaptcha {
|
2011-05-02 21:26:04 +00:00
|
|
|
|
2011-01-31 09:50:27 +00:00
|
|
|
//reCAPTHCA error code returned from recaptcha_check_answer
|
|
|
|
private $recaptcha_error = null;
|
|
|
|
|
2011-05-02 21:26:04 +00:00
|
|
|
/**
|
|
|
|
* Displays the reCAPTCHA widget.
|
2011-12-01 16:51:18 +00:00
|
|
|
* If $this->recaptcha_error is set, it will display an error in the widget.
|
2011-01-31 09:50:27 +00:00
|
|
|
*
|
2011-12-01 16:51:18 +00:00
|
|
|
*/
|
2011-01-31 09:50:27 +00:00
|
|
|
function getForm() {
|
2011-10-26 14:51:34 +00:00
|
|
|
global $wgReCaptchaPublicKey, $wgReCaptchaTheme;
|
2011-09-29 21:31:11 +00:00
|
|
|
$useHttps = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' );
|
2011-10-27 11:34:34 +00:00
|
|
|
$js = 'var RecaptchaOptions = ' . Xml::encodeJsVar( array( 'theme' => $wgReCaptchaTheme, 'tabindex' => 1 ) );
|
2011-10-26 14:51:34 +00:00
|
|
|
|
2011-10-27 11:34:34 +00:00
|
|
|
return Html::inlineScript( $js ) . recaptcha_get_html($wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps);
|
2011-01-31 09:50:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls the library function recaptcha_check_answer to verify the users input.
|
|
|
|
* Sets $this->recaptcha_error if the user is incorrect.
|
2011-12-01 16:51:18 +00:00
|
|
|
* @return boolean
|
|
|
|
*
|
|
|
|
*/
|
2011-01-31 09:50:27 +00:00
|
|
|
function passCaptcha() {
|
|
|
|
global $wgReCaptchaPrivateKey;
|
2011-12-01 16:59:49 +00:00
|
|
|
global $wgRequest;
|
|
|
|
|
|
|
|
//API is hardwired to return wpCaptchaId and wpCaptchaWord, so use that if the standard two are empty
|
|
|
|
$challenge = $wgRequest->getVal('recaptcha_challenge_field',$wgRequest->getVal('wpCaptchaId'));
|
|
|
|
$response = $wgRequest->getVal('recaptcha_response_field',$wgRequest->getVal('wpCaptchaWord'));
|
|
|
|
if ( $response === null ) {
|
|
|
|
//new captcha session
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-01 16:51:18 +00:00
|
|
|
$recaptcha_response =
|
|
|
|
recaptcha_check_answer (
|
|
|
|
$wgReCaptchaPrivateKey,
|
|
|
|
wfGetIP (),
|
2011-12-01 16:59:49 +00:00
|
|
|
$challenge,
|
|
|
|
$response
|
2011-12-01 16:51:18 +00:00
|
|
|
);
|
|
|
|
if (!$recaptcha_response->is_valid) {
|
2011-01-31 09:50:27 +00:00
|
|
|
$this->recaptcha_error = $recaptcha_response->error;
|
|
|
|
return false;
|
2011-12-01 16:51:18 +00:00
|
|
|
}
|
2011-01-31 09:50:27 +00:00
|
|
|
$recaptcha_error = null;
|
2011-12-01 16:51:18 +00:00
|
|
|
return true;
|
2011-01-31 09:50:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-01 16:59:49 +00:00
|
|
|
function addCaptchaAPI( &$resultArr ) {
|
|
|
|
global $wgReCaptchaPublicKey;
|
|
|
|
$resultArr['captcha']['type'] = 'recaptcha';
|
|
|
|
$resultArr['captcha']['mime'] = 'image/png';
|
|
|
|
$resultArr['captcha']['key'] = $wgReCaptchaPublicKey;
|
|
|
|
$resultArr['captcha']['error'] = $this->recaptcha_error;
|
2011-05-02 21:26:04 +00:00
|
|
|
}
|
2011-01-31 09:50:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a message asking the user to enter a captcha on edit
|
|
|
|
* The result will be treated as wiki text
|
|
|
|
*
|
|
|
|
* @param $action Action being performed
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getMessage( $action ) {
|
|
|
|
$name = 'recaptcha-' . $action;
|
|
|
|
$text = wfMsg( $name );
|
|
|
|
# Obtain a more tailored message, if possible, otherwise, fall back to
|
|
|
|
# the default for edits
|
|
|
|
return wfEmptyMsg( $name, $text ) ? wfMsg( 'recaptcha-edit' ) : $text;
|
|
|
|
}
|
|
|
|
|
2011-11-23 19:09:57 +00:00
|
|
|
public function APIGetAllowedParams( &$module, &$params ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function APIGetParamDescription( &$module, &$desc ) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-01-31 09:50:27 +00:00
|
|
|
}
|