MediaWiki REL1_34
ReCaptchaNoCaptcha.php
Go to the documentation of this file.
1<?php
2
4
6 // used for renocaptcha-edit, renocaptcha-addurl, renocaptcha-badlogin, renocaptcha-createaccount,
7 // renocaptcha-create, renocaptcha-sendemail via getMessage()
8 protected static $messagePrefix = 'renocaptcha-';
9
10 private $error = null;
11
17 public function getFormInformation( $tabIndex = 1 ) {
18 global $wgReCaptchaSiteKey, $wgLang;
19 $lang = htmlspecialchars( urlencode( $wgLang->getCode() ) );
20
21 $output = Html::element( 'div', [
22 'class' => [
23 'g-recaptcha',
24 'mw-confirmedit-captcha-fail' => (bool)$this->error,
25 ],
26 'data-sitekey' => $wgReCaptchaSiteKey
27 ] );
28 $htmlUrlencoded = htmlspecialchars( urlencode( $wgReCaptchaSiteKey ) );
29 $output .= <<<HTML
30<noscript>
31 <div>
32 <div style="width: 302px; height: 422px; position: relative;">
33 <div style="width: 302px; height: 422px; position: absolute;">
34 <iframe src="https://www.google.com/recaptcha/api/fallback?k={$htmlUrlencoded}&hl={$lang}"
35 frameborder="0" scrolling="no"
36 style="width: 302px; height:422px; border-style: none;">
37 </iframe>
38 </div>
39 </div>
40 <div style="width: 300px; height: 60px; border-style: none;
41 bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px;
42 background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;">
43 <textarea id="g-recaptcha-response" name="g-recaptcha-response"
44 class="g-recaptcha-response"
45 style="width: 250px; height: 40px; border: 1px solid #c1c1c1;
46 margin: 10px 25px; padding: 0px; resize: none;" >
47 </textarea>
48 </div>
49 </div>
50</noscript>
51HTML;
52 return [
53 'html' => $output,
54 'headitems' => [
55 // Insert reCAPTCHA script, in display language, if available.
56 // Language falls back to the browser's display language.
57 // See https://developers.google.com/recaptcha/docs/faq
58 "<script src=\"https://www.google.com/recaptcha/api.js?hl={$lang}\" async defer></script>"
59 ]
60 ];
61 }
62
66 protected function logCheckError( $info ) {
67 if ( $info instanceof Status ) {
68 $errors = $info->getErrorsArray();
69 $error = $errors[0][0];
70 } elseif ( is_array( $info ) ) {
71 $error = implode( ',', $info );
72 } else {
73 $error = $info;
74 }
75
76 wfDebugLog( 'captcha', 'Unable to validate response: ' . $error );
77 }
78
83 protected function getCaptchaParamsFromRequest( WebRequest $request ) {
84 // ReCaptchaNoCaptcha combines captcha ID + solution into a single value
85 // API is hardwired to return captchaWord, so use that if the standard isempty
86 // "captchaWord" is sent as "captchaword" by visual editor
87 $index = 'not used';
88 $response = $request->getVal( 'g-recaptcha-response',
89 $request->getVal( 'captchaWord',
90 $request->getVal( 'captchaword' )
91 ) );
92 return [ $index, $response ];
93 }
94
105 protected function passCaptcha( $_, $word ) {
106 global $wgRequest, $wgReCaptchaSecretKey, $wgReCaptchaSendRemoteIP;
107
108 $url = 'https://www.google.com/recaptcha/api/siteverify';
109 // Build data to append to request
110 $data = [
111 'secret' => $wgReCaptchaSecretKey,
112 'response' => $word,
113 ];
114 if ( $wgReCaptchaSendRemoteIP ) {
115 $data['remoteip'] = $wgRequest->getIP();
116 }
117 $url = wfAppendQuery( $url, $data );
118 $request = MWHttpRequest::factory( $url, [ 'method' => 'GET' ] );
119 $status = $request->execute();
120 if ( !$status->isOK() ) {
121 $this->error = 'http';
122 $this->logCheckError( $status );
123 return false;
124 }
125 $response = FormatJson::decode( $request->getContent(), true );
126 if ( !$response ) {
127 $this->error = 'json';
128 $this->logCheckError( $this->error );
129 return false;
130 }
131 if ( isset( $response['error-codes'] ) ) {
132 $this->error = 'recaptcha-api';
133 $this->logCheckError( $response['error-codes'] );
134 return false;
135 }
136
137 return $response['success'];
138 }
139
143 protected function addCaptchaAPI( &$resultArr ) {
144 $resultArr['captcha'] = $this->describeCaptchaType();
145 $resultArr['captcha']['error'] = $this->error;
146 }
147
151 public function describeCaptchaType() {
152 global $wgReCaptchaSiteKey;
153 return [
154 'type' => 'recaptchanocaptcha',
155 'mime' => 'image/png',
156 'key' => $wgReCaptchaSiteKey,
157 ];
158 }
159
167 public function getMessage( $action ) {
168 $msg = parent::getMessage( $action );
169 if ( $this->error ) {
170 $msg = new RawMessage( '<div class="error">$1</div>', [ $msg ] );
171 }
172 return $msg;
173 }
174
181 public function apiGetAllowedParams( &$module, &$params, $flags ) {
182 if ( $flags && $this->isAPICaptchaModule( $module ) ) {
183 $params['g-recaptcha-response'] = [
184 ApiBase::PARAM_HELP_MSG => 'renocaptcha-apihelp-param-g-recaptcha-response',
185 ];
186 }
187
188 return true;
189 }
190
191 public function getError() {
192 return $this->error;
193 }
194
195 public function storeCaptcha( $info ) {
196 // ReCaptcha is stored by Google; the ID will be generated at that time as well, and
197 // the one returned here won't be used. Just pretend this worked.
198 return 'not used';
199 }
200
201 public function retrieveCaptcha( $index ) {
202 // just pretend it worked
203 return [ 'index' => $index ];
204 }
205
206 public function getCaptcha() {
207 // ReCaptcha is handled by frontend code + an external provider; nothing to do here.
208 return [];
209 }
210
216 public function getCaptchaInfo( $captchaData, $id ) {
217 return wfMessage( 'renocaptcha-info' );
218 }
219
223 public function createAuthenticationRequest() {
225 }
226
233 public function onAuthChangeFormFields(
234 array $requests, array $fieldInfo, array &$formDescriptor, $action
235 ) {
236 global $wgReCaptchaSiteKey;
237
238 $req = AuthenticationRequest::getRequestByClass( $requests,
239 CaptchaAuthenticationRequest::class, true );
240 if ( !$req ) {
241 return;
242 }
243
244 // ugly way to retrieve error information
245 $captcha = ConfirmEditHooks::getInstance();
246
247 $formDescriptor['captchaWord'] = [
248 'class' => HTMLReCaptchaNoCaptchaField::class,
249 'key' => $wgReCaptchaSiteKey,
250 'error' => $captcha->getError(),
251 ] + $formDescriptor['captchaWord'];
252 }
253}
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(! $wgDBerrorLogTZ) $wgRequest
Definition Setup.php:751
$wgLang
Definition Setup.php:880
This is a value object for authentication requests.
Variant of the Message class.
apiGetAllowedParams(&$module, &$params, $flags)
getCaptchaInfo( $captchaData, $id)
getMessage( $action)
Show a message asking the user to enter a captcha on edit The result will be treated as wiki text.
onAuthChangeFormFields(array $requests, array $fieldInfo, array &$formDescriptor, $action)
getError()
Return the error from the last passCaptcha* call.
getCaptcha()
Returns an array with 'question' and 'answer' keys.
passCaptcha( $_, $word)
Check, if the user solved the captcha.
retrieveCaptcha( $index)
Fetch this session's captcha info.
getFormInformation( $tabIndex=1)
Get the captcha form.
storeCaptcha( $info)
Generate a captcha session ID and save the info in PHP's session storage.
getCaptchaParamsFromRequest(WebRequest $request)
Demo CAPTCHA (not for production usage) and base class for real CAPTCHAs.
isAPICaptchaModule( $module)
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.
if(!isset( $args[0])) $lang