Debian Codegraph @

extensions/ConfirmEdit/ReCaptchaNoCaptcha/includes/HTMLReCaptchaNoCaptchaField.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\ConfirmEdit\ReCaptchaNoCaptcha;
4
5 use MediaWiki\Html\Html;
6 use MediaWiki\HTMLForm\HTMLFormField;
7
8 /**
9 * Creates a ReCaptcha v2 widget. Does not return any data; handling the data submitted by the
10 * widget is callers' responsibility.
11 */
12 class HTMLReCaptchaNoCaptchaField extends HTMLFormField {
13 /** @var string Public key parameter to be passed to ReCaptcha. */
14 protected $key;
15
16 /** @var string Error returned by ReCaptcha in the previous round. */
17 protected $error;
18
19 /**
20 * Parameters:
21 * - key: (string, required) ReCaptcha public key
22 * - error: (string) ReCaptcha error from previous round
23 * @param array $params
24 */
25 public function __construct( array $params ) {
26 $params += [ 'error' => null ];
27 parent::__construct( $params );
28
29 $this->key = $params['key'];
30 $this->error = $params['error'];
31
32 $this->mName = 'g-recaptcha-response';
33 }
34
35 /**
36 * @inheritDoc
37 */
38 public function getInputHTML( $value ) {
39 $out = $this->mParent->getOutput();
40 $lang = htmlspecialchars( urlencode( $this->mParent->getLanguage()->getCode() ) );
41
42 // Insert reCAPTCHA script, in display language, if available.
43 // Language falls back to the browser's display language.
44 // See https://developers.google.com/recaptcha/docs/faq
45 $out->addHeadItem(
46 'g-recaptchascript',
47 "<script src=\"https://www.recaptcha.net/recaptcha/api.js?hl={$lang}\" async defer></script>"
48 );
49 ReCaptchaNoCaptcha::addCSPSources( $out->getCSP() );
50
51 $output = Html::element( 'div', [
52 'class' => [
53 'g-recaptcha',
54 'mw-confirmedit-captcha-fail' => (bool)$this->error,
55 ],
56 'data-sitekey' => $this->key,
57 ] );
58 $htmlUrlencoded = htmlspecialchars( urlencode( $this->key ) );
59 $output .= <<<HTML
60 <noscript>
61 <div>
62 <div style="width: 302px; height: 422px; position: relative;">
63 <div style="width: 302px; height: 422px; position: absolute;">
64 <iframe
65 src="https://www.recaptcha.net/recaptcha/api/fallback?k={$htmlUrlencoded}&hl={$lang}"
66 frameborder="0" scrolling="no"
67 style="width: 302px; height:422px; border-style: none;">
68 </iframe>
69 </div>
70 </div>
71 <div style="width: 300px; height: 60px; border-style: none;
72 bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px;
73 background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;">
74 <textarea id="g-recaptcha-response" name="g-recaptcha-response"
75 class="g-recaptcha-response"
76 style="width: 250px; height: 40px; border: 1px solid #c1c1c1;
77 margin: 10px 25px; padding: 0px; resize: none;" >
78 </textarea>
79 </div>
80 </div>
81 </noscript>
82 HTML;
83 return $output;
84 }
85 }