2012-01-11 23:24:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ReCaptcha extends SimpleCaptcha {
|
2012-01-12 08:58:40 +00:00
|
|
|
// reCAPTHCA error code returned from recaptcha_check_answer
|
2012-01-11 23:24:21 +00:00
|
|
|
private $recaptcha_error = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays the reCAPTCHA widget.
|
|
|
|
* If $this->recaptcha_error is set, it will display an error in the widget.
|
2014-12-14 20:47:55 +00:00
|
|
|
* @param OutputPage $out
|
2012-01-11 23:24:21 +00:00
|
|
|
*/
|
2015-09-23 06:05:28 +00:00
|
|
|
function getForm( OutputPage $out, $tabIndex = 1 ) {
|
2012-01-11 23:24:21 +00:00
|
|
|
global $wgReCaptchaPublicKey, $wgReCaptchaTheme;
|
2012-01-12 08:58:40 +00:00
|
|
|
|
2012-01-11 23:24:21 +00:00
|
|
|
$useHttps = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' );
|
2015-10-28 15:52:04 +00:00
|
|
|
$js = 'var RecaptchaOptions = ' . Xml::encodeJsVar(
|
|
|
|
array( 'theme' => $wgReCaptchaTheme, 'tabindex' => $tabIndex )
|
|
|
|
);
|
2012-01-11 23:24:21 +00:00
|
|
|
|
2015-10-28 15:52:04 +00:00
|
|
|
return Html::inlineScript(
|
|
|
|
$js
|
|
|
|
) . recaptcha_get_html( $wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps );
|
2012-01-11 23:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls the library function recaptcha_check_answer to verify the users input.
|
|
|
|
* Sets $this->recaptcha_error if the user is incorrect.
|
|
|
|
* @return boolean
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function passCaptcha() {
|
|
|
|
global $wgReCaptchaPrivateKey, $wgRequest;
|
|
|
|
|
2015-10-28 15:52:04 +00:00
|
|
|
// 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' )
|
|
|
|
);
|
2012-01-11 23:24:21 +00:00
|
|
|
|
|
|
|
if ( $response === null ) {
|
2012-01-12 08:58:40 +00:00
|
|
|
// new captcha session
|
2012-01-11 23:24:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-02 12:26:25 +00:00
|
|
|
$ip = $wgRequest->getIP();
|
2012-03-18 16:07:21 +00:00
|
|
|
|
2012-01-11 23:24:21 +00:00
|
|
|
$recaptcha_response = recaptcha_check_answer(
|
|
|
|
$wgReCaptchaPrivateKey,
|
2012-03-18 16:07:21 +00:00
|
|
|
$ip,
|
2012-01-11 23:24:21 +00:00
|
|
|
$challenge,
|
|
|
|
$response
|
|
|
|
);
|
|
|
|
|
2012-01-12 08:58:40 +00:00
|
|
|
if ( !$recaptcha_response->is_valid ) {
|
2012-01-11 23:24:21 +00:00
|
|
|
$this->recaptcha_error = $recaptcha_response->error;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$recaptcha_error = null;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function addCaptchaAPI( &$resultArr ) {
|
|
|
|
global $wgReCaptchaPublicKey;
|
2012-01-12 08:58:40 +00:00
|
|
|
|
2012-01-11 23:24:21 +00:00
|
|
|
$resultArr['captcha']['type'] = 'recaptcha';
|
|
|
|
$resultArr['captcha']['mime'] = 'image/png';
|
|
|
|
$resultArr['captcha']['key'] = $wgReCaptchaPublicKey;
|
|
|
|
$resultArr['captcha']['error'] = $this->recaptcha_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a message asking the user to enter a captcha on edit
|
|
|
|
* The result will be treated as wiki text
|
|
|
|
*
|
2012-09-02 12:26:25 +00:00
|
|
|
* @param $action string Action being performed
|
2012-01-11 23:24:21 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getMessage( $action ) {
|
|
|
|
$name = 'recaptcha-' . $action;
|
2012-08-16 21:21:50 +00:00
|
|
|
$text = wfMessage( $name )->text();
|
2012-01-12 08:58:40 +00:00
|
|
|
|
2012-01-11 23:24:21 +00:00
|
|
|
# Obtain a more tailored message, if possible, otherwise, fall back to
|
|
|
|
# the default for edits
|
2012-08-16 21:21:50 +00:00
|
|
|
return wfMessage( $name, $text )->isDisabled() ? wfMessage( 'recaptcha-edit' )->text() : $text;
|
2012-01-11 23:24:21 +00:00
|
|
|
}
|
|
|
|
|
2013-03-16 15:42:51 +00:00
|
|
|
public function APIGetAllowedParams( &$module, &$params, $flags ) {
|
|
|
|
if ( $flags && $this->isAPICaptchaModule( $module ) ) {
|
|
|
|
$params['recaptcha_challenge_field'] = null;
|
|
|
|
$params['recaptcha_response_field'] = null;
|
|
|
|
}
|
|
|
|
|
2012-01-11 23:24:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function APIGetParamDescription( &$module, &$desc ) {
|
2013-03-16 15:42:51 +00:00
|
|
|
if ( $this->isAPICaptchaModule( $module ) ) {
|
|
|
|
$desc['recaptcha_challenge_field'] = 'Field from the ReCaptcha widget';
|
|
|
|
$desc['recaptcha_response_field'] = 'Field from the ReCaptcha widget';
|
|
|
|
}
|
|
|
|
|
2012-01-11 23:24:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|