MediaWiki  1.30.0
ReCaptcha.class.php
Go to the documentation of this file.
1 <?php
2 
3 use MediaWiki\Auth\AuthenticationRequest;
4 
5 class ReCaptcha extends SimpleCaptcha {
6  // used for recaptcha-edit, recaptcha-addurl, recaptcha-badlogin, recaptcha-createaccount,
7  // recaptcha-create, recaptcha-sendemail via getMessage()
8  protected static $messagePrefix = 'recaptcha-';
9 
10  // reCAPTHCA error code returned from recaptcha_check_answer
11  private $recaptcha_error = null;
12 
17  function getFormInformation( $tabIndex = 1 ) {
18  global $wgReCaptchaPublicKey, $wgReCaptchaTheme;
19 
20  wfDeprecated( 'ConfirmEdit module ReCaptcha', '1.28' );
21  $useHttps = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' );
22  $js = 'var RecaptchaOptions = ' . Xml::encodeJsVar(
23  [ 'theme' => $wgReCaptchaTheme, 'tabindex' => $tabIndex ]
24  );
25 
26  return [
27  'html' => Html::inlineScript( $js ) .
28  recaptcha_get_html( $wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps )
29  ];
30  }
31 
37  // API is hardwired to return captchaId and captchaWord,
38  // so use that if the standard two are empty
39  $challenge = $request->getVal( 'recaptcha_challenge_field', $request->getVal( 'captchaId' ) );
40  $response = $request->getVal( 'recaptcha_response_field', $request->getVal( 'captchaWord' ) );
41  return [ $challenge, $response ];
42  }
43 
51  function passCaptcha( $challenge, $response ) {
52  global $wgReCaptchaPrivateKey, $wgRequest;
53 
54  if ( $response === null ) {
55  // new captcha session
56  return false;
57  }
58 
59  $ip = $wgRequest->getIP();
60 
61  $recaptcha_response =
62  recaptcha_check_answer( $wgReCaptchaPrivateKey, $ip, $challenge, $response );
63 
64  if ( !$recaptcha_response->is_valid ) {
65  $this->recaptcha_error = $recaptcha_response->error;
66  return false;
67  }
68 
69  $recaptcha_error = null;
70 
71  return true;
72  }
73 
77  function addCaptchaAPI( &$resultArr ) {
78  $resultArr['captcha'] = $this->describeCaptchaType();
79  $resultArr['captcha']['error'] = $this->recaptcha_error;
80  }
81 
85  public function describeCaptchaType() {
86  global $wgReCaptchaPublicKey;
87  return [
88  'type' => 'recaptcha',
89  'mime' => 'image/png',
90  'key' => $wgReCaptchaPublicKey,
91  ];
92  }
93 
100  public function APIGetAllowedParams( &$module, &$params, $flags ) {
101  if ( $flags && $this->isAPICaptchaModule( $module ) ) {
102  $params['recaptcha_challenge_field'] = [
103  ApiBase::PARAM_HELP_MSG => 'recaptcha-apihelp-param-recaptcha_challenge_field',
104  ];
105  $params['recaptcha_response_field'] = [
106  ApiBase::PARAM_HELP_MSG => 'recaptcha-apihelp-param-recaptcha_response_field',
107  ];
108  }
109 
110  return true;
111  }
112 
116  public function getError() {
117  // do not treat failed captcha attempts as errors
118  if ( in_array( $this->recaptcha_error, [
119  'invalid-request-cookie', 'incorrect-captcha-sol',
120  ], true ) ) {
121  return null;
122  }
123 
124  return $this->recaptcha_error;
125  }
126 
127  public function storeCaptcha( $info ) {
128  // ReCaptcha is stored by Google; the ID will be generated at that time as well, and
129  // the one returned here won't be used. Just pretend this worked.
130  return 'not used';
131  }
132 
133  public function retrieveCaptcha( $index ) {
134  // just pretend it worked
135  return [ 'index' => $index ];
136  }
137 
138  public function getCaptcha() {
139  // ReCaptcha is handled by frontend code + an external provider; nothing to do here.
140  return [];
141  }
142 
148  public function getCaptchaInfo( $captchaData, $id ) {
149  return wfMessage( 'recaptcha-info' );
150  }
151 
155  public function createAuthenticationRequest() {
156  return new ReCaptchaAuthenticationRequest();
157  }
158 
165  public function onAuthChangeFormFields(
166  array $requests, array $fieldInfo, array &$formDescriptor, $action
167  ) {
168  global $wgReCaptchaPublicKey, $wgReCaptchaTheme;
169 
170  $req = AuthenticationRequest::getRequestByClass( $requests,
172  if ( !$req ) {
173  return;
174  }
175 
176  // ugly way to retrieve error information
177  $captcha = ConfirmEditHooks::getInstance();
178 
179  $formDescriptor['captchaInfo'] = [
180  'class' => HTMLReCaptchaField::class,
181  'key' => $wgReCaptchaPublicKey,
182  'theme' => $wgReCaptchaTheme,
183  'secure' => isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on',
184  'error' => $captcha->getError(),
185  ] + $formDescriptor['captchaInfo'];
186 
187  // the custom form element cannot return multiple fields; work around that by
188  // "redirecting" ReCaptcha names to standard names
189  $formDescriptor['captchaId'] = [
191  'name' => 'recaptcha_challenge_field',
192  ];
193  $formDescriptor['captchaWord'] = [
195  'name' => 'recaptcha_response_field',
196  ];
197  }
198 }
ReCaptcha
Definition: ReCaptcha.class.php:5
ReCaptcha::describeCaptchaType
describeCaptchaType()
Definition: ReCaptcha.class.php:85
ReCaptcha::onAuthChangeFormFields
onAuthChangeFormFields(array $requests, array $fieldInfo, array &$formDescriptor, $action)
Definition: ReCaptcha.class.php:165
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:128
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$req
this hook is for auditing only $req
Definition: hooks.txt:988
$params
$params
Definition: styleTest.css.php:40
ReCaptcha::getCaptchaInfo
getCaptchaInfo( $captchaData, $id)
Definition: ReCaptcha.class.php:148
SimpleCaptcha\$action
string $action
Used to select the right message.
Definition: Captcha.php:20
ConfirmEditHooks\getInstance
static getInstance()
Get the global Captcha instance.
Definition: ConfirmEditHooks.php:13
Html\inlineScript
static inlineScript( $contents)
Output a "<script>" tag with the given contents.
Definition: Html.php:562
Xml\encodeJsVar
static encodeJsVar( $value, $pretty=false)
Encode a variable of arbitrary type to JavaScript.
Definition: Xml.php:659
ReCaptcha::retrieveCaptcha
retrieveCaptcha( $index)
Fetch this session's captcha info.
Definition: ReCaptcha.class.php:133
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ReCaptcha::passCaptcha
passCaptcha( $challenge, $response)
Calls the library function recaptcha_check_answer to verify the users input.
Definition: ReCaptcha.class.php:51
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1176
recaptcha_get_html
recaptcha_get_html( $pubkey, $error=null, $use_ssl=false)
Gets the challenge HTML (javascript and non-javascript version).
Definition: recaptchalib.php:106
ReCaptchaAuthenticationRequest
Authentication request for ReCaptcha v1.
Definition: ReCaptchaAuthenticationRequest.php:9
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
ReCaptcha::getCaptchaParamsFromRequest
getCaptchaParamsFromRequest(WebRequest $request)
Definition: ReCaptcha.class.php:36
$request
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2581
ReCaptcha::$messagePrefix
static $messagePrefix
Definition: ReCaptcha.class.php:8
ReCaptcha::getError
getError()
Definition: ReCaptcha.class.php:116
SimpleCaptcha
Demo CAPTCHA (not for production usage) and base class for real CAPTCHAs.
Definition: Captcha.php:9
$response
this hook is for auditing only $response
Definition: hooks.txt:781
ReCaptcha::APIGetAllowedParams
APIGetAllowedParams(&$module, &$params, $flags)
Definition: ReCaptcha.class.php:100
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:38
SimpleCaptcha\isAPICaptchaModule
isAPICaptchaModule( $module)
Definition: Captcha.php:893
ReCaptcha::createAuthenticationRequest
createAuthenticationRequest()
Definition: ReCaptcha.class.php:155
ReCaptcha::addCaptchaAPI
addCaptchaAPI(&$resultArr)
Definition: ReCaptcha.class.php:77
$requests
Allows to change the fields on the form that will be generated are created Can be used to omit specific feeds from being outputted You must not use this hook to add use OutputPage::addFeedLink() instead. & $feedLinks hooks can tweak the array to change how login etc forms should look $requests
Definition: hooks.txt:304
ReCaptcha::getFormInformation
getFormInformation( $tabIndex=1)
Displays the reCAPTCHA widget.
Definition: ReCaptcha.class.php:17
wfMessage
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
ReCaptcha::getCaptcha
getCaptcha()
Returns an array with 'question' and 'answer' keys.
Definition: ReCaptcha.class.php:138
$wgRequest
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:662
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2801
ReCaptcha::$recaptcha_error
$recaptcha_error
Definition: ReCaptcha.class.php:11
array
the array() calling protocol came about after MediaWiki 1.4rc1.
ReCaptcha::storeCaptcha
storeCaptcha( $info)
Generate a captcha session ID and save the info in PHP's session storage.
Definition: ReCaptcha.class.php:127