MediaWiki REL1_34
HCaptcha.php
Go to the documentation of this file.
1<?php
2
4
5use ApiBase;
8use FormatJson;
9use Html;
11use Message;
13use RawMessage;
15use Status;
16use WebRequest;
17
18class HCaptcha extends SimpleCaptcha {
19 // used for hcaptcha-edit, hcaptcha-addurl, hcaptcha-badlogin, hcaptcha-createaccount,
20 // hcaptcha-create, hcaptcha-sendemail via getMessage()
21 protected static $messagePrefix = 'hcaptcha-';
22
23 private $error;
24
30 public function getFormInformation( $tabIndex = 1 ) {
31 global $wgHCaptchaSiteKey;
32
33 $output = Html::element( 'div', [
34 'class' => [
35 'h-captcha',
36 'mw-confirmedit-captcha-fail' => (bool)$this->error,
37 ],
38 'data-sitekey' => $wgHCaptchaSiteKey
39 ] );
40
41 return [
42 'html' => $output,
43 'headitems' => [
44 "<script src=\"https://hcaptcha.com/1/api.js\" async defer></script>"
45 ]
46 ];
47 }
48
52 protected function logCheckError( $info ) {
53 if ( $info instanceof Status ) {
54 $errors = $info->getErrorsArray();
55 $error = $errors[0][0];
56 } elseif ( is_array( $info ) ) {
57 $error = implode( ',', $info );
58 } else {
59 $error = $info;
60 }
61
62 \wfDebugLog( 'captcha', 'Unable to validate response: ' . $error );
63 }
64
69 protected function getCaptchaParamsFromRequest( WebRequest $request ) {
70 $response = $request->getVal( 'h-captcha-response' );
71 return [ '', $response ];
72 }
73
84 protected function passCaptcha( $_, $token ) {
85 global $wgRequest, $wgHCaptchaSecretKey, $wgHCaptchaSendRemoteIP;
86
87 $url = 'https://hcaptcha.com/siteverify';
88 $data = [
89 'secret' => $wgHCaptchaSecretKey,
90 'response' => $token,
91 ];
92 if ( $wgHCaptchaSendRemoteIP ) {
93 $data['remoteip'] = $wgRequest->getIP();
94 }
95 $request = MWHttpRequest::factory(
96 $url,
97 [
98 'method' => 'POST',
99 'postData' => $data,
100 ]
101 );
102 $status = $request->execute();
103 if ( !$status->isOK() ) {
104 $this->error = 'http';
105 $this->logCheckError( $status );
106 return false;
107 }
108 $response = FormatJson::decode( $request->getContent(), true );
109 if ( !$response ) {
110 $this->error = 'json';
111 $this->logCheckError( $this->error );
112 return false;
113 }
114 if ( isset( $response['error-codes'] ) ) {
115 $this->error = 'hcaptcha-api';
116 $this->logCheckError( $response['error-codes'] );
117 return false;
118 }
119
120 return $response['success'];
121 }
122
126 protected function addCaptchaAPI( &$resultArr ) {
127 }
128
132 public function describeCaptchaType() {
133 global $wgHCaptchaSiteKey;
134 return [
135 'type' => 'hcaptcha',
136 'mime' => 'application/javascript',
137 'key' => $wgHCaptchaSiteKey,
138 ];
139 }
140
148 public function getMessage( $action ) {
149 $msg = parent::getMessage( $action );
150 if ( $this->error ) {
151 $msg = new RawMessage( '<div class="error">$1</div>', [ $msg ] );
152 }
153 return $msg;
154 }
155
162 public function apiGetAllowedParams( ApiBase $module, &$params, $flags ) {
163 return true;
164 }
165
169 public function getError() {
170 return $this->error;
171 }
172
176 public function storeCaptcha( $info ) {
177 return 'not used';
178 }
179
183 public function retrieveCaptcha( $index ) {
184 // just pretend it worked
185 return [ 'index' => $index ];
186 }
187
191 public function getCaptcha() {
192 return [];
193 }
194
198 public function createAuthenticationRequest() {
200 }
201
208 public function onAuthChangeFormFields(
209 array $requests, array $fieldInfo, array &$formDescriptor, $action
210 ) {
211 global $wgHCaptchaSiteKey;
212
213 $req = AuthenticationRequest::getRequestByClass(
214 $requests,
215 CaptchaAuthenticationRequest::class,
216 true
217 );
218 if ( !$req ) {
219 return;
220 }
221
222 // ugly way to retrieve error information
224
225 $formDescriptor['captchaWord'] = [
226 'class' => HTMLHCaptchaField::class,
227 'key' => $wgHCaptchaSiteKey,
228 'error' => $captcha->getError(),
229 ] + $formDescriptor['captchaWord'];
230 }
231}
if(! $wgDBerrorLogTZ) $wgRequest
Definition Setup.php:751
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:42
Generic captcha authentication request class.
static getInstance()
Get the global Captcha instance.
JSON formatter wrapper class.
static decode( $value, $assoc=false)
Decodes a JSON string.
This class is a collection of static functions that serve two purposes:
Definition Html.php:49
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
static factory( $url, array $options=null, $caller=__METHOD__)
Generate a new request object.
This is a value object for authentication requests.
getFormInformation( $tabIndex=1)
Get the captcha form.
Definition HCaptcha.php:30
retrieveCaptcha( $index)
Fetch this session's captcha info.array|false array of info, or false if missing
Definition HCaptcha.php:183
getCaptcha()
Returns an array with 'question' and 'answer' keys.Subclasses might use different structure....
Definition HCaptcha.php:191
onAuthChangeFormFields(array $requests, array $fieldInfo, array &$formDescriptor, $action)
Definition HCaptcha.php:208
getError()
Return the error from the last passCaptcha* call.Not implemented but needed by some child classes....
Definition HCaptcha.php:169
getMessage( $action)
Show a message asking the user to enter a captcha on edit The result will be treated as wiki text.
Definition HCaptcha.php:148
storeCaptcha( $info)
Generate a captcha session ID and save the info in PHP's session storage.(Requires the user to have c...
Definition HCaptcha.php:176
apiGetAllowedParams(ApiBase $module, &$params, $flags)
Definition HCaptcha.php:162
passCaptcha( $_, $token)
Check, if the user solved the captcha.
Definition HCaptcha.php:84
The Message class provides methods which fulfil two basic services:
Definition Message.php:162
Variant of the Message class.
Demo CAPTCHA (not for production usage) and base class for real CAPTCHAs.
string $action
Used to select the right message.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:40
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
getVal( $name, $default=null)
Fetch a scalar from the input or return $default if it's not set.