CI / testsuite (mysql, 8.2, ) (push) Successful in 3m22s
CI / testsuite (mysql, 8.4, ) (push) Successful in 4m35s
CI / testsuite (pgsql, 8.2, ) (push) Successful in 3m9s
CI / testsuite (pgsql, 8.4, ) (push) Successful in 4m13s
CI / testsuite (sqlite, 8.2, ) (push) Successful in 3m5s
CI / testsuite (sqlite, 8.2, prefer-lowest) (push) Successful in 3m5s
CI / testsuite (sqlite, 8.4, ) (push) Successful in 4m14s
CI / Coding Standard & Static Analysis (push) Successful in 2m21s
175 lines
4.2 KiB
PHP
175 lines
4.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace CakeContactUs\View\Helper;
|
|
|
|
use Cake\Core\Configure;
|
|
use Cake\View\Helper;
|
|
use Cake\View\View;
|
|
|
|
/**
|
|
* Turnstile helper
|
|
*
|
|
* @property \Cake\View\Helper\HtmlHelper $Html
|
|
*/
|
|
class TurnstileHelper extends Helper {
|
|
|
|
/**
|
|
* Default configuration.
|
|
*
|
|
* @var array<string, mixed>
|
|
*/
|
|
protected array $_defaultConfig = [
|
|
'eventListeners' => true,
|
|
];
|
|
|
|
/**
|
|
* @var array|string[]
|
|
*/
|
|
protected array $helpers = ['Html'];
|
|
|
|
/**
|
|
* @param \Cake\View\View $view
|
|
* @param array<string, mixed> $config
|
|
*/
|
|
public function __construct(View $view, array $config = []) {
|
|
$config += (array)Configure::read('Turnstile');
|
|
|
|
parent::__construct($view, $config);
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function preConnect() {
|
|
return '<link rel="preconnect" href="https://challenges.cloudflare.com" />';
|
|
}
|
|
|
|
/**
|
|
* @param array $options
|
|
*
|
|
* @return string
|
|
*/
|
|
public function render(array $options = []) {
|
|
$options += [
|
|
'class' => 'cf-turnstile',
|
|
'data-sitekey' => $this->getConfigOrFail('siteKey'),
|
|
'data-theme' => 'auto',
|
|
'data-size' => 'flexible',
|
|
'eventListeners' => $this->getConfig('eventListeners'),
|
|
];
|
|
if ($options['eventListeners']) {
|
|
$options += [
|
|
'data-callback' => 'onTurnstileSuccess',
|
|
'data-error-callback' => 'onTurnstileError',
|
|
'data-expired-callback' => 'onTurnstileExpired',
|
|
];
|
|
}
|
|
unset($options['eventListeners']);
|
|
|
|
return $this->Html->div($options['class'], '', $options);
|
|
}
|
|
|
|
/**
|
|
* @param array $options
|
|
*
|
|
* @return string|null
|
|
*/
|
|
public function script(array $options = []) {
|
|
$options += [
|
|
'async' => true,
|
|
'defer' => true,
|
|
];
|
|
$scriptOptions = $options;
|
|
unset($scriptOptions['eventListeners']);
|
|
|
|
if ($options['eventListeners'] ?? false) {
|
|
return $this->Html->script('https://challenges.cloudflare.com/turnstile/v0/api.js', $scriptOptions) . $this->eventListener($options['eventListeners']);
|
|
}
|
|
|
|
return $this->Html->script('https://challenges.cloudflare.com/turnstile/v0/api.js', $scriptOptions);
|
|
}
|
|
|
|
/**
|
|
* @param array $options
|
|
*
|
|
* @return string
|
|
*/
|
|
public function eventListener(array $options) {
|
|
$options += [
|
|
'dynamicButton' => $this->getConfig('dynamicButton', false),
|
|
'log' => $this->getConfig('log', false),
|
|
'submitButtonId' => $this->getConfig('submitButtonId', 'submit-btn'),
|
|
];
|
|
$final = '<script>';
|
|
$final .= $this->_onTurnstileSuccess($options);
|
|
$final .= $this->onTurnstileError($options);
|
|
$final .= $this->onTurnstileExpired($options);
|
|
$final .= '</script>';
|
|
|
|
return $this->Html->scriptBlock($final);
|
|
}
|
|
|
|
/**
|
|
* @param array $options
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function _onTurnstileSuccess(array $options = []): string {
|
|
$jsFunction = 'function onTurnstileSuccess(token) {';
|
|
if ($options['log'] ?? false) {
|
|
$jsFunction .= 'console.log("Turnstile success:", token);';
|
|
}
|
|
if ($options['dynamicButton'] ?? false) {
|
|
$jsFunction .= 'document.getElementById("'
|
|
. ($options['submitButtonId'] ?? 'submit-btn')
|
|
. '")?.disabled = false;';
|
|
}
|
|
$jsFunction .= '}';
|
|
|
|
return $jsFunction;
|
|
}
|
|
|
|
/**
|
|
* @param array $options
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function onTurnstileError(array $options = []): string {
|
|
$jsFunction = 'function onTurnstileError(errorCode) {';
|
|
if ($options['log'] ?? false) {
|
|
$jsFunction .= 'console.error("Turnstile error:", errorCode);';
|
|
}
|
|
if ($options['dynamicButton'] ?? false) {
|
|
$jsFunction .= 'document.getElementById("'
|
|
. ($options['submitButtonId'] ?? 'submit-btn')
|
|
. '")?.disabled = true;';
|
|
}
|
|
$jsFunction .= '}';
|
|
|
|
return $jsFunction;
|
|
}
|
|
|
|
/**
|
|
* @param array $options
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function onTurnstileExpired(array $options = []): string {
|
|
$jsFunction = 'function onTurnstileExpired(errorCode) {';
|
|
if ($options['log'] ?? false) {
|
|
$jsFunction .= 'console.warn("Turnstile token expired");';
|
|
}
|
|
if ($options['dynamicButton'] ?? false) {
|
|
$jsFunction .= 'document.getElementById("'
|
|
. ($options['submitButtonId'] ?? 'submit-btn')
|
|
. '")?.disabled = true;';
|
|
}
|
|
|
|
$jsFunction .= '}';
|
|
|
|
return $jsFunction;
|
|
}
|
|
|
|
}
|