2014-12-14 20:47:55 +00:00
|
|
|
<?php
|
2016-05-03 16:42:00 +00:00
|
|
|
|
|
|
|
use MediaWiki\Auth\AuthenticationRequest;
|
|
|
|
|
2014-12-14 20:47:55 +00:00
|
|
|
class ReCaptchaNoCaptcha extends SimpleCaptcha {
|
2016-04-25 20:58:18 +00:00
|
|
|
// used for renocaptcha-edit, renocaptcha-addurl, renocaptcha-badlogin, renocaptcha-createaccount,
|
|
|
|
// renocaptcha-create, renocaptcha-sendemail via getMessage()
|
|
|
|
protected static $messagePrefix = 'renocaptcha-';
|
|
|
|
|
2014-12-14 20:47:55 +00:00
|
|
|
private $error = null;
|
|
|
|
/**
|
|
|
|
* Get the captcha form.
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-09-23 06:05:28 +00:00
|
|
|
function getForm( OutputPage $out, $tabIndex = 1 ) {
|
2014-12-14 20:47:55 +00:00
|
|
|
global $wgReCaptchaSiteKey;
|
2015-12-26 19:39:19 +00:00
|
|
|
$lang = htmlspecialchars( urlencode( $out->getLanguage()->getCode() ) );
|
2014-12-14 20:47:55 +00:00
|
|
|
|
2015-12-26 19:39:19 +00:00
|
|
|
// Insert reCAPTCHA script, in display language, if available.
|
|
|
|
// Language falls back to the browser's display language.
|
2014-12-14 20:47:55 +00:00
|
|
|
// See https://developers.google.com/recaptcha/docs/faq
|
|
|
|
$out->addHeadItem(
|
|
|
|
'g-recaptchascript',
|
2015-12-26 19:39:19 +00:00
|
|
|
"<script src=\"https://www.google.com/recaptcha/api.js?hl={$lang}\" async defer></script>"
|
2014-12-14 20:47:55 +00:00
|
|
|
);
|
2016-05-09 23:41:17 +00:00
|
|
|
$output = Html::element( 'div', [
|
|
|
|
'class' => [
|
2014-12-14 20:47:55 +00:00
|
|
|
'g-recaptcha',
|
|
|
|
'mw-confirmedit-captcha-fail' => !!$this->error,
|
2016-05-09 23:41:17 +00:00
|
|
|
],
|
2014-12-14 20:47:55 +00:00
|
|
|
'data-sitekey' => $wgReCaptchaSiteKey
|
2016-05-09 23:41:17 +00:00
|
|
|
] );
|
2014-12-14 20:47:55 +00:00
|
|
|
$htmlUrlencoded = htmlspecialchars( urlencode( $wgReCaptchaSiteKey ) );
|
|
|
|
$output .= <<<HTML
|
|
|
|
<noscript>
|
2016-05-03 16:42:00 +00:00
|
|
|
<div>
|
2014-12-14 20:47:55 +00:00
|
|
|
<div style="width: 302px; height: 422px; position: relative;">
|
|
|
|
<div style="width: 302px; height: 422px; position: absolute;">
|
2015-12-26 19:39:19 +00:00
|
|
|
<iframe src="https://www.google.com/recaptcha/api/fallback?k={$htmlUrlencoded}&hl={$lang}"
|
2014-12-14 20:47:55 +00:00
|
|
|
frameborder="0" scrolling="no"
|
|
|
|
style="width: 302px; height:422px; border-style: none;">
|
|
|
|
</iframe>
|
|
|
|
</div>
|
2016-05-03 16:42:00 +00:00
|
|
|
</div>
|
|
|
|
<div style="width: 300px; height: 60px; border-style: none;
|
|
|
|
bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px;
|
|
|
|
background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;">
|
|
|
|
<textarea id="g-recaptcha-response" name="g-recaptcha-response"
|
|
|
|
class="g-recaptcha-response"
|
|
|
|
style="width: 250px; height: 40px; border: 1px solid #c1c1c1;
|
|
|
|
margin: 10px 25px; padding: 0px; resize: none;" >
|
|
|
|
</textarea>
|
2014-12-14 20:47:55 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</noscript>
|
|
|
|
HTML;
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function logCheckError( $info ) {
|
|
|
|
if ( $info instanceof Status ) {
|
2015-10-19 10:49:56 +00:00
|
|
|
$errors = $info->getErrorsArray();
|
2014-12-14 20:47:55 +00:00
|
|
|
$error = $errors[0][0];
|
|
|
|
} elseif ( is_array( $info ) ) {
|
|
|
|
$error = implode( ',', $info );
|
|
|
|
} else {
|
|
|
|
$error = $info;
|
|
|
|
}
|
2015-10-19 10:49:56 +00:00
|
|
|
|
2014-12-14 20:47:55 +00:00
|
|
|
wfDebugLog( 'captcha', 'Unable to validate response: ' . $error );
|
|
|
|
}
|
|
|
|
|
2016-05-03 16:42:00 +00:00
|
|
|
public function passCaptchaLimitedFromRequest( WebRequest $request, User $user ) {
|
|
|
|
$index = 'not used'; // ReCaptchaNoCaptcha combines captcha ID + solution into a single value
|
|
|
|
// API is hardwired to return captchaWord, so use that if the standard isempty
|
|
|
|
$response = $request->getVal( 'g-recaptcha-response', $request->getVal( 'captchaWord' ) );
|
|
|
|
return $this->passCaptchaLimited( $index, $response, $user );
|
|
|
|
}
|
|
|
|
|
2014-12-14 20:47:55 +00:00
|
|
|
/**
|
|
|
|
* Check, if the user solved the captcha.
|
|
|
|
*
|
|
|
|
* Based on reference implementation:
|
|
|
|
* https://github.com/google/recaptcha#php
|
|
|
|
*
|
2016-05-03 16:42:00 +00:00
|
|
|
* @param $_ mixed Not used (ReCaptcha v2 puts index and solution in a single string)
|
|
|
|
* @param $word string captcha solution
|
2014-12-14 20:47:55 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-05-03 16:42:00 +00:00
|
|
|
function passCaptcha( $_, $word ) {
|
2014-12-14 20:47:55 +00:00
|
|
|
global $wgRequest, $wgReCaptchaSecretKey, $wgReCaptchaSendRemoteIP;
|
|
|
|
|
|
|
|
$url = 'https://www.google.com/recaptcha/api/siteverify';
|
|
|
|
// Build data to append to request
|
2016-05-09 23:41:17 +00:00
|
|
|
$data = [
|
2014-12-14 20:47:55 +00:00
|
|
|
'secret' => $wgReCaptchaSecretKey,
|
2016-05-03 16:42:00 +00:00
|
|
|
'response' => $word,
|
2016-05-09 23:41:17 +00:00
|
|
|
];
|
2014-12-14 20:47:55 +00:00
|
|
|
if ( $wgReCaptchaSendRemoteIP ) {
|
|
|
|
$data['remoteip'] = $wgRequest->getIP();
|
|
|
|
}
|
|
|
|
$url = wfAppendQuery( $url, $data );
|
2016-05-09 23:41:17 +00:00
|
|
|
$request = MWHttpRequest::factory( $url, [ 'method' => 'GET' ] );
|
2014-12-14 20:47:55 +00:00
|
|
|
$status = $request->execute();
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
$this->error = 'http';
|
2015-10-14 12:42:38 +00:00
|
|
|
$this->logCheckError( $status );
|
2014-12-14 20:47:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$response = FormatJson::decode( $request->getContent(), true );
|
|
|
|
if ( !$response ) {
|
|
|
|
$this->error = 'json';
|
2015-10-14 12:42:38 +00:00
|
|
|
$this->logCheckError( $this->error );
|
2014-12-14 20:47:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( isset( $response['error-codes'] ) ) {
|
|
|
|
$this->error = 'recaptcha-api';
|
|
|
|
$this->logCheckError( $response['error-codes'] );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response['success'];
|
|
|
|
}
|
|
|
|
|
|
|
|
function addCaptchaAPI( &$resultArr ) {
|
2016-04-25 20:58:18 +00:00
|
|
|
$resultArr['captcha'] = $this->describeCaptchaType();
|
2014-12-14 20:47:55 +00:00
|
|
|
$resultArr['captcha']['error'] = $this->error;
|
|
|
|
}
|
|
|
|
|
2016-04-25 20:58:18 +00:00
|
|
|
public function describeCaptchaType() {
|
|
|
|
global $wgReCaptchaSiteKey;
|
|
|
|
return [
|
|
|
|
'type' => 'recaptchanocaptcha',
|
|
|
|
'mime' => 'mage/png',
|
|
|
|
'key' => $wgReCaptchaSiteKey,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2014-12-14 20:47:55 +00:00
|
|
|
/**
|
|
|
|
* Show a message asking the user to enter a captcha on edit
|
|
|
|
* The result will be treated as wiki text
|
|
|
|
*
|
|
|
|
* @param $action string Action being performed
|
|
|
|
* @return string Wikitext
|
|
|
|
*/
|
2016-04-25 20:58:18 +00:00
|
|
|
public function getMessage( $action ) {
|
|
|
|
$msg = parent::getMessage( $action );
|
2014-12-14 20:47:55 +00:00
|
|
|
if ( $this->error ) {
|
2016-04-25 20:58:18 +00:00
|
|
|
$msg = new RawMessage( '<div class="error">$1</div>', [ $msg ] );
|
2014-12-14 20:47:55 +00:00
|
|
|
}
|
2016-04-25 20:58:18 +00:00
|
|
|
return $msg;
|
2014-12-14 20:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function APIGetAllowedParams( &$module, &$params, $flags ) {
|
|
|
|
if ( $flags && $this->isAPICaptchaModule( $module ) ) {
|
2015-12-18 16:44:37 +00:00
|
|
|
if ( defined( 'ApiBase::PARAM_HELP_MSG' ) ) {
|
2016-05-09 23:41:17 +00:00
|
|
|
$params['g-recaptcha-response'] = [
|
2015-12-18 16:44:37 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG => 'renocaptcha-apihelp-param-g-recaptcha-response',
|
2016-05-09 23:41:17 +00:00
|
|
|
];
|
2015-12-18 16:44:37 +00:00
|
|
|
} else {
|
|
|
|
// @todo: Remove this branch when support for MediaWiki < 1.25 is dropped
|
|
|
|
$params['g-recaptcha-response'] = null;
|
|
|
|
}
|
2014-12-14 20:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-18 16:44:37 +00:00
|
|
|
/**
|
|
|
|
* @deprecated since MediaWiki 1.25
|
|
|
|
*/
|
2014-12-14 20:47:55 +00:00
|
|
|
public function APIGetParamDescription( &$module, &$desc ) {
|
|
|
|
if ( $this->isAPICaptchaModule( $module ) ) {
|
|
|
|
$desc['g-recaptcha-response'] = 'Field from the ReCaptcha widget';
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-03 16:42:00 +00:00
|
|
|
|
|
|
|
public function getError() {
|
|
|
|
return $this->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function storeCaptcha( $info ) {
|
|
|
|
// ReCaptcha is stored by Google; the ID will be generated at that time as well, and
|
|
|
|
// the one returned here won't be used. Just pretend this worked.
|
|
|
|
return 'not used';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function retrieveCaptcha( $index ) {
|
|
|
|
// just pretend it worked
|
|
|
|
return [ 'index' => $index ];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCaptcha() {
|
|
|
|
// ReCaptcha is handled by frontend code + an external provider; nothing to do here.
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCaptchaInfo( $captchaData, $id ) {
|
|
|
|
return wfMessage( 'renocaptcha-info' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createAuthenticationRequest() {
|
|
|
|
return new ReCaptchaNoCaptchaAuthenticationRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAuthChangeFormFields(
|
|
|
|
array $requests, array $fieldInfo, array &$formDescriptor, $action
|
|
|
|
) {
|
|
|
|
global $wgReCaptchaSiteKey;
|
|
|
|
|
|
|
|
$req = AuthenticationRequest::getRequestByClass( $requests,
|
|
|
|
CaptchaAuthenticationRequest::class, true );
|
|
|
|
if ( !$req ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ugly way to retrieve error information
|
|
|
|
$captcha = ConfirmEditHooks::getInstance();
|
|
|
|
|
|
|
|
$formDescriptor['captchaWord'] = [
|
|
|
|
'class' => HTMLReCaptchaNoCaptchaField::class,
|
|
|
|
'key' => $wgReCaptchaSiteKey,
|
|
|
|
'error' => $captcha->getError(),
|
|
|
|
] + $formDescriptor['captchaWord'];
|
|
|
|
}
|
2014-12-14 20:47:55 +00:00
|
|
|
}
|