Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| HTMLTurnstileField | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
6 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getInputHTML | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\ConfirmEdit\Turnstile; |
| 4 | |
| 5 | use MediaWiki\Html\Html; |
| 6 | use MediaWiki\HTMLForm\HTMLFormField; |
| 7 | |
| 8 | /** |
| 9 | * Creates a Turnstile widget. Does not return any data; handling the data submitted by the |
| 10 | * widget is callers' responsibility. |
| 11 | */ |
| 12 | class HTMLTurnstileField extends HTMLFormField { |
| 13 | /** @var string Public key parameter to be passed to Turnstile. */ |
| 14 | protected $key; |
| 15 | |
| 16 | /** @var string Error returned by Turnstile in the previous round. */ |
| 17 | protected $error; |
| 18 | |
| 19 | /** |
| 20 | * Parameters: |
| 21 | * - key: (string, required) Turnstile public key |
| 22 | * - error: (string) Turnstile error from the 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 = 'cf-turnstile-response'; |
| 33 | } |
| 34 | |
| 35 | /** @inheritDoc */ |
| 36 | public function getInputHTML( $value ) { |
| 37 | $out = $this->mParent->getOutput(); |
| 38 | $lang = htmlspecialchars( urlencode( $this->mParent->getLanguage()->getCode() ) ); |
| 39 | |
| 40 | // Insert the Turnstile script, in display language, if available. |
| 41 | // Language falls back to the browser's display language. |
| 42 | // See https://developers.cloudflare.com/turnstile/reference/supported-languages/ |
| 43 | $out->addHeadItem( |
| 44 | 'cf-turnstile-script', |
| 45 | "<script src=\"https://challenges.cloudflare.com/turnstile/v0/api.js?language={$lang}\" async defer> |
| 46 | </script>" |
| 47 | ); |
| 48 | Turnstile::addCSPSources( $out->getCSP() ); |
| 49 | |
| 50 | return Html::element( 'div', [ |
| 51 | 'class' => [ |
| 52 | 'cf-turnstile', |
| 53 | 'mw-confirmedit-captcha-fail' => (bool)$this->error, |
| 54 | ], |
| 55 | 'data-sitekey' => $this->key, |
| 56 | ] ); |
| 57 | } |
| 58 | } |