2023-10-02 22:42:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\ConfirmEdit\Turnstile;
|
|
|
|
|
2024-06-08 21:46:45 +00:00
|
|
|
use MediaWiki\Html\Html;
|
|
|
|
use MediaWiki\HTMLForm\HTMLFormField;
|
2023-10-02 22:42:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Turnstile widget. Does not return any data; handling the data submitted by the
|
|
|
|
* widget is callers' responsibility.
|
|
|
|
*/
|
|
|
|
class HTMLTurnstileField extends HTMLFormField {
|
|
|
|
/** @var string Public key parameter to be passed to Turnstile. */
|
|
|
|
protected $key;
|
|
|
|
|
|
|
|
/** @var string Error returned by Turnstile in the previous round. */
|
|
|
|
protected $error;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parameters:
|
|
|
|
* - key: (string, required) Turnstile public key
|
2024-10-25 18:24:07 +00:00
|
|
|
* - error: (string) Turnstile error from the previous round
|
2023-10-02 22:42:57 +00:00
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function __construct( array $params ) {
|
|
|
|
$params += [ 'error' => null ];
|
|
|
|
parent::__construct( $params );
|
|
|
|
|
|
|
|
$this->key = $params['key'];
|
|
|
|
$this->error = $params['error'];
|
|
|
|
|
|
|
|
$this->mName = 'cf-turnstile-response';
|
|
|
|
}
|
|
|
|
|
2024-10-25 18:24:07 +00:00
|
|
|
/** @inheritDoc */
|
2023-10-02 22:42:57 +00:00
|
|
|
public function getInputHTML( $value ) {
|
|
|
|
$out = $this->mParent->getOutput();
|
|
|
|
$lang = htmlspecialchars( urlencode( $this->mParent->getLanguage()->getCode() ) );
|
|
|
|
|
2024-10-25 18:24:07 +00:00
|
|
|
// Insert the Turnstile script, in display language, if available.
|
2023-10-02 22:42:57 +00:00
|
|
|
// Language falls back to the browser's display language.
|
|
|
|
// See https://developers.cloudflare.com/turnstile/reference/supported-languages/
|
|
|
|
$out->addHeadItem(
|
|
|
|
'cf-turnstile-script',
|
|
|
|
"<script src=\"https://challenges.cloudflare.com/turnstile/v0/api.js?language={$lang}\" async defer>
|
|
|
|
</script>"
|
|
|
|
);
|
|
|
|
Turnstile::addCSPSources( $out->getCSP() );
|
|
|
|
|
2024-01-15 20:04:46 +00:00
|
|
|
return Html::element( 'div', [
|
2023-10-02 22:42:57 +00:00
|
|
|
'class' => [
|
|
|
|
'cf-turnstile',
|
|
|
|
'mw-confirmedit-captcha-fail' => (bool)$this->error,
|
|
|
|
],
|
|
|
|
'data-sitekey' => $this->key,
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
}
|