Debian Codegraph @

extensions/ConfirmEdit/ReCaptchaNoCaptcha/includes/ReCaptchaNoCaptcha.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\ConfirmEdit\ReCaptchaNoCaptcha;
4
5 use MediaWiki\Api\ApiBase;
6 use MediaWiki\Auth\AuthenticationRequest;
7 use MediaWiki\Extension\ConfirmEdit\Auth\CaptchaAuthenticationRequest;
8 use MediaWiki\Extension\ConfirmEdit\Hooks;
9 use MediaWiki\Extension\ConfirmEdit\SimpleCaptcha\SimpleCaptcha;
10 use MediaWiki\Html\Html;
11 use MediaWiki\Json\FormatJson;
12 use MediaWiki\Language\RawMessage;
13 use MediaWiki\MediaWikiServices;
14 use MediaWiki\Message\Message;
15 use MediaWiki\Request\WebRequest;
16 use MediaWiki\Status\Status;
17
18 class ReCaptchaNoCaptcha extends SimpleCaptcha {
19 /**
20 * @var string used for renocaptcha-edit, renocaptcha-addurl, renocaptcha-badlogin, renocaptcha-createaccount,
21 * renocaptcha-create, renocaptcha-sendemail via getMessage()
22 */
23 protected static $messagePrefix = 'renocaptcha-';
24
25 /** @var string|null */
26 private $error = null;
27
28 /**
29 * Get the captcha form.
30 * @param int $tabIndex
31 * @return array
32 */
33 public function getFormInformation( $tabIndex = 1 ) {
34 global $wgReCaptchaSiteKey, $wgLang;
35 $lang = htmlspecialchars( urlencode( $wgLang->getCode() ) );
36
37 $output = Html::element( 'div', [
38 'class' => [
39 'g-recaptcha',
40 'mw-confirmedit-captcha-fail' => (bool)$this->error,
41 ],
42 'data-sitekey' => $wgReCaptchaSiteKey
43 ] );
44 $htmlUrlencoded = htmlspecialchars( urlencode( $wgReCaptchaSiteKey ) );
45 $output .= <<<HTML
46 <noscript>
47 <div>
48 <div style="width: 302px; height: 422px; position: relative;">
49 <div style="width: 302px; height: 422px; position: absolute;">
50 <iframe
51 src="https://www.recaptcha.net/recaptcha/api/fallback?k={$htmlUrlencoded}&hl={$lang}"
52 frameborder="0" scrolling="no"
53 style="width: 302px; height:422px; border-style: none;">
54 </iframe>
55 </div>
56 </div>
57 <div style="width: 300px; height: 60px; border-style: none;
58 bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px;
59 background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;">
60 <textarea id="g-recaptcha-response" name="g-recaptcha-response"
61 class="g-recaptcha-response"
62 style="width: 250px; height: 40px; border: 1px solid #c1c1c1;
63 margin: 10px 25px; padding: 0px; resize: none;" >
64 </textarea>
65 </div>
66 </div>
67 </noscript>
68 HTML;
69 return [
70 'html' => $output,
71 'headitems' => [
72 // Insert reCAPTCHA script, in display language, if available.
73 // Language falls back to the browser's display language.
74 // See https://developers.google.com/recaptcha/docs/faq
75 "<script src=\"https://www.recaptcha.net/recaptcha/api.js?hl={$lang}\" async defer></script>"
76 ]
77 ];
78 }
79
80 /**
81 * @return string[]
82 */
83 public static function getCSPUrls() {
84 return [ 'https://www.recaptcha.net/recaptcha/api.js' ];
85 }
86
87 /**
88 * @param Status|array|string $info
89 */
90 protected function logCheckError( $info ) {
91 if ( $info instanceof Status ) {
92 $errors = $info->getErrorsArray();
93 $error = $errors[0][0];
94 } elseif ( is_array( $info ) ) {
95 $error = implode( ',', $info );
96 } else {
97 $error = $info;
98 }
99
100 wfDebugLog( 'captcha', 'Unable to validate response: ' . $error );
101 }
102
103 /**
104 * @param WebRequest $request
105 * @return array
106 */
107 protected function getCaptchaParamsFromRequest( WebRequest $request ) {
108 // ReCaptchaNoCaptcha combines captcha ID + solution into a single value
109 // API is hardwired to return captchaWord, so use that if the standard isempty
110 // "captchaWord" is sent as "captchaword" by visual editor
111 $index = 'not used';
112 $response = $request->getVal(
113 'g-recaptcha-response',
114 $request->getVal( 'captchaWord', $request->getVal( 'captchaword' ) )
115 );
116 return [ $index, $response ];
117 }
118
119 /**
120 * Check, if the user solved the captcha.
121 *
122 * Based on reference implementation:
123 * https://github.com/google/recaptcha#php
124 *
125 * @param mixed $_ Not used (ReCaptcha v2 puts index and solution in a single string)
126 * @param string $word captcha solution
127 * @return bool
128 */
129 protected function passCaptcha( $_, $word ) {
130 global $wgRequest, $wgReCaptchaSecretKey, $wgReCaptchaSendRemoteIP;
131
132 $url = 'https://www.recaptcha.net/recaptcha/api/siteverify';
133 // Build data to append to request
134 $data = [
135 'secret' => $wgReCaptchaSecretKey,
136 'response' => $word,
137 ];
138 if ( $wgReCaptchaSendRemoteIP ) {
139 $data['remoteip'] = $wgRequest->getIP();
140 }
141 $url = wfAppendQuery( $url, $data );
142 $request = MediaWikiServices::getInstance()->getHttpRequestFactory()
143 ->create( $url, [ 'method' => 'POST' ], __METHOD__ );
144 $status = $request->execute();
145 if ( !$status->isOK() ) {
146 $this->error = 'http';
147 $this->logCheckError( $status );
148 return false;
149 }
150 $response = FormatJson::decode( $request->getContent(), true );
151 if ( !$response ) {
152 $this->error = 'json';
153 $this->logCheckError( $this->error );
154 return false;
155 }
156 if ( isset( $response['error-codes'] ) ) {
157 $this->error = 'recaptcha-api';
158 $this->logCheckError( $response['error-codes'] );
159 return false;
160 }
161
162 return $response['success'];
163 }
164
165 /**
166 * @param array &$resultArr
167 */
168 protected function addCaptchaAPI( &$resultArr ) {
169 $resultArr['captcha'] = $this->describeCaptchaType();
170 $resultArr['captcha']['error'] = $this->error;
171 }
172
173 /**
174 * @return array
175 */
176 public function describeCaptchaType() {
177 global $wgReCaptchaSiteKey;
178 return [
179 'type' => 'recaptchanocaptcha',
180 'mime' => 'application/javascript',
181 'key' => $wgReCaptchaSiteKey,
182 ];
183 }
184
185 /**
186 * Show a message asking the user to enter a captcha on edit
187 * The result will be treated as wiki text
188 *
189 * @param string $action Action being performed
190 * @return Message
191 */
192 public function getMessage( $action ) {
193 $msg = parent::getMessage( $action );
194 if ( $this->error ) {
195 $msg = new RawMessage( '<div class="error">$1</div>', [ $msg ] );
196 }
197 return $msg;
198 }
199
200 /**
201 * @param ApiBase $module
202 * @param array &$params
203 * @param int $flags
204 * @return bool
205 */
206 public function apiGetAllowedParams( ApiBase $module, &$params, $flags ) {
207 if ( $flags && $this->isAPICaptchaModule( $module ) ) {
208 $params['g-recaptcha-response'] = [
209 ApiBase::PARAM_HELP_MSG => 'renocaptcha-apihelp-param-g-recaptcha-response',
210 ];
211 }
212
213 return true;
214 }
215
216 /**
217 * @inheritDoc
218 */
219 public function getError() {
220 return $this->error;
221 }
222
223 /**
224 * @inheritDoc
225 */
226 public function storeCaptcha( $info ) {
227 // ReCaptcha is stored by Google; the ID will be generated at that time as well, and
228 // the one returned here won't be used. Just pretend this worked.
229 return 'not used';
230 }
231
232 /**
233 * @inheritDoc
234 */
235 public function retrieveCaptcha( $index ) {
236 // just pretend it worked
237 return [ 'index' => $index ];
238 }
239
240 /**
241 * @inheritDoc
242 */
243 public function getCaptcha() {
244 // ReCaptcha is handled by frontend code + an external provider; nothing to do here.
245 return [];
246 }
247
248 /**
249 * @return ReCaptchaNoCaptchaAuthenticationRequest
250 */
251 public function createAuthenticationRequest() {
252 return new ReCaptchaNoCaptchaAuthenticationRequest();
253 }
254
255 /**
256 * @param array $requests
257 * @param array $fieldInfo
258 * @param array &$formDescriptor
259 * @param string $action
260 */
261 public function onAuthChangeFormFields(
262 array $requests, array $fieldInfo, array &$formDescriptor, $action
263 ) {
264 global $wgReCaptchaSiteKey;
265
266 $req = AuthenticationRequest::getRequestByClass( $requests,
267 CaptchaAuthenticationRequest::class, true );
268 if ( !$req ) {
269 return;
270 }
271
272 // ugly way to retrieve error information
273 $captcha = Hooks::getInstance();
274
275 $formDescriptor['captchaWord'] = [
276 'class' => HTMLReCaptchaNoCaptchaField::class,
277 'key' => $wgReCaptchaSiteKey,
278 'error' => $captcha->getError(),
279 ] + $formDescriptor['captchaWord'];
280 }
281 }