| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\ConfirmEdit\Turnstile; |
| 4 |
|
| 5 |
use MediaWiki\Html\Html; |
| 6 |
use MediaWiki\HTMLForm\HTMLFormField; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class HTMLTurnstileField extends HTMLFormField { |
| 13 |
|
| 14 |
protected $key; |
| 15 |
|
| 16 |
|
| 17 |
protected $error; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
public function getInputHTML( $value ) { |
| 39 |
$out = $this->mParent->getOutput(); |
| 40 |
$lang = htmlspecialchars( urlencode( $this->mParent->getLanguage()->getCode() ) ); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
$out->addHeadItem( |
| 46 |
'cf-turnstile-script', |
| 47 |
"<script src=\"https://challenges.cloudflare.com/turnstile/v0/api.js?language={$lang}\" async defer> |
| 48 |
</script>" |
| 49 |
); |
| 50 |
Turnstile::addCSPSources( $out->getCSP() ); |
| 51 |
|
| 52 |
return Html::element( 'div', [ |
| 53 |
'class' => [ |
| 54 |
'cf-turnstile', |
| 55 |
'mw-confirmedit-captcha-fail' => (bool)$this->error, |
| 56 |
], |
| 57 |
'data-sitekey' => $this->key, |
| 58 |
] ); |
| 59 |
} |
| 60 |
} |