MediaWiki  1.29.1
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  }
74 
78  function addCaptchaAPI( &$resultArr ) {
79  $resultArr['captcha'] = $this->describeCaptchaType();
80  $resultArr['captcha']['error'] = $this->recaptcha_error;
81  }
82 
86  public function describeCaptchaType() {
87  global $wgReCaptchaPublicKey;
88  return [
89  'type' => 'recaptcha',
90  'mime' => 'image/png',
91  'key' => $wgReCaptchaPublicKey,
92  ];
93  }
94 
101  public function APIGetAllowedParams( &$module, &$params, $flags ) {
102  if ( $flags && $this->isAPICaptchaModule( $module ) ) {
103  $params['recaptcha_challenge_field'] = [
104  ApiBase::PARAM_HELP_MSG => 'recaptcha-apihelp-param-recaptcha_challenge_field',
105  ];
106  $params['recaptcha_response_field'] = [
107  ApiBase::PARAM_HELP_MSG => 'recaptcha-apihelp-param-recaptcha_response_field',
108  ];
109  }
110 
111  return true;
112  }
113 
117  public function getError() {
118  // do not treat failed captcha attempts as errors
119  if ( in_array( $this->recaptcha_error, [
120  'invalid-request-cookie', 'incorrect-captcha-sol',
121  ], true ) ) {
122  return null;
123  }
124 
125  return $this->recaptcha_error;
126  }
127 
128  public function storeCaptcha( $info ) {
129  // ReCaptcha is stored by Google; the ID will be generated at that time as well, and
130  // the one returned here won't be used. Just pretend this worked.
131  return 'not used';
132  }
133 
134  public function retrieveCaptcha( $index ) {
135  // just pretend it worked
136  return [ 'index' => $index ];
137  }
138 
139  public function getCaptcha() {
140  // ReCaptcha is handled by frontend code + an external provider; nothing to do here.
141  return [];
142  }
143 
149  public function getCaptchaInfo( $captchaData, $id ) {
150  return wfMessage( 'recaptcha-info' );
151  }
152 
156  public function createAuthenticationRequest() {
157  return new ReCaptchaAuthenticationRequest();
158  }
159 
166  public function onAuthChangeFormFields(
167  array $requests, array $fieldInfo, array &$formDescriptor, $action
168  ) {
169  global $wgReCaptchaPublicKey, $wgReCaptchaTheme;
170 
171  $req = AuthenticationRequest::getRequestByClass( $requests,
173  if ( !$req ) {
174  return;
175  }
176 
177  // ugly way to retrieve error information
178  $captcha = ConfirmEditHooks::getInstance();
179 
180  $formDescriptor['captchaInfo'] = [
181  'class' => HTMLReCaptchaField::class,
182  'key' => $wgReCaptchaPublicKey,
183  'theme' => $wgReCaptchaTheme,
184  'secure' => isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on',
185  'error' => $captcha->getError(),
186  ] + $formDescriptor['captchaInfo'];
187 
188  // the custom form element cannot return multiple fields; work around that by
189  // "redirecting" ReCaptcha names to standard names
190  $formDescriptor['captchaId'] = [
192  'name' => 'recaptcha_challenge_field',
193  ];
194  $formDescriptor['captchaWord'] = [
196  'name' => 'recaptcha_response_field',
197  ];
198  }
199 }
$request
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2612
ReCaptcha
Definition: ReCaptcha.class.php:5
ReCaptcha::describeCaptchaType
describeCaptchaType()
Definition: ReCaptcha.class.php:86
ReCaptcha::onAuthChangeFormFields
onAuthChangeFormFields(array $requests, array $fieldInfo, array &$formDescriptor, $action)
Definition: ReCaptcha.class.php:166
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:990
$params
$params
Definition: styleTest.css.php:40
ReCaptcha::getCaptchaInfo
getCaptchaInfo( $captchaData, $id)
Definition: ReCaptcha.class.php:149
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:583
Xml\encodeJsVar
static encodeJsVar( $value, $pretty=false)
Encode a variable of arbitrary type to JavaScript.
Definition: Xml.php:627
ReCaptcha::retrieveCaptcha
retrieveCaptcha( $index)
Fetch this session's captcha info.
Definition: ReCaptcha.class.php:134
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:1128
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
ReCaptcha::$messagePrefix
static $messagePrefix
Definition: ReCaptcha.class.php:8
ReCaptcha::getError
getError()
Definition: ReCaptcha.class.php:117
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:783
ReCaptcha::APIGetAllowedParams
APIGetAllowedParams(&$module, &$params, $flags)
Definition: ReCaptcha.class.php:101
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:895
ReCaptcha::createAuthenticationRequest
createAuthenticationRequest()
Definition: ReCaptcha.class.php:156
ReCaptcha::addCaptchaAPI
addCaptchaAPI(&$resultArr)
Definition: ReCaptcha.class.php:78
$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:306
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:139
$wgRequest
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:639
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2749
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:128