turnstile helper
This commit is contained in:
+11
-2
@@ -26,9 +26,18 @@ return [
|
||||
'controller' => 'ContactUsFormSubmissions',
|
||||
'action' => 'view',
|
||||
],
|
||||
/**
|
||||
* @link https://developers.cloudflare.com/turnstile/troubleshooting/testing/
|
||||
*
|
||||
* secretKeys for testing:
|
||||
* always passes = 1x0000000000000000000000000000000AA
|
||||
* always fails = 2x0000000000000000000000000000000AA
|
||||
* token already spent error 3x0000000000000000000000000000000AA
|
||||
*/
|
||||
'turnstile' => [
|
||||
'siteKey' => '{SITE_KEY_GOES_HERE}', // define in app_local or an .env variable
|
||||
'secretKey' => '{SECRET_GOES_HERE}', // define in app_local or an .env variable
|
||||
'enabled' => true,
|
||||
'siteKey' => '{SITE_KEY_GOES_HERE}', // define in app_local or an .env variable - CLOUDFLARE_TURNSTILE_SITE_KEY
|
||||
'secretKey' => '{SECRET_GOES_HERE}', // define in app_local or an .env variable - CLOUDFLARE_TURNSTILE_SECRET_KEY
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -30,6 +30,16 @@ class CakeContactUsPlugin extends BasePlugin {
|
||||
*/
|
||||
public const CAKE_CONTACT_US_MYSQL_DATETIME = 'Y-m-d H:i:s';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SECRET_KEY = 'CLOUDFLARE_TURNSTILE_SECRET_KEY';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SITE_KEY = 'CLOUDFLARE_TURNSTILE_SITE_KEY';
|
||||
|
||||
/**
|
||||
* Load all the plugin configuration and bootstrap logic.
|
||||
*
|
||||
|
||||
@@ -81,7 +81,9 @@ class ContactUsComponent extends Component {
|
||||
$this->ContactUsFormSubmissions = TableRegistry::getTableLocator()->get('CakeContactUs.ContactUsFormSubmissions');
|
||||
|
||||
$requireCaptcha = Configure::readOrFail('ContactUs.fields.captcha');
|
||||
$useTurnstile = Configure::read('ContactUs.turnstile', []);
|
||||
$useTurnstile = Configure::read('ContactUs.turnstile', [
|
||||
'enabled' => false,
|
||||
]);
|
||||
$this->setConfig('redirectUrl', Configure::read('ContactUs.redirectUrl', '/'));
|
||||
$this->setConfig('addIdToRedirect', Configure::read('ContactUs.addIdToRedirect', false));
|
||||
$this->setConfig('requireEmail', Configure::readOrFail('ContactUs.fields.email'));
|
||||
@@ -90,8 +92,8 @@ class ContactUsComponent extends Component {
|
||||
if ($requireCaptcha) {
|
||||
$this->getController()->viewBuilder()->addHelpers(['Captcha.Captcha']);
|
||||
}
|
||||
if ($useTurnstile) {
|
||||
// $this->getController()->viewBuilder()->addHelpers(['']);
|
||||
if ($useTurnstile['enabled'] ?? false) {
|
||||
// $this->getController()->viewBuilder()->addHelpers(['Turnstile']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,7 +196,9 @@ class ContactUsComponent extends Component {
|
||||
if (!$this->getConfig('turnstile')) {
|
||||
return;
|
||||
}
|
||||
if (!$this->getConfig('turnstile.siteKey') || !$this->getConfig('turnstile.secretKey')) {
|
||||
$siteKey = getenv(CakeContactUsPlugin::CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SITE_KEY) ?: $this->getConfig('turnstile.siteKey');
|
||||
$secretKey = getenv(CakeContactUsPlugin::CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SECRET_KEY) ?: $this->getConfig('turnstile.secretKey');
|
||||
if (!$siteKey || !$secretKey) {
|
||||
$event->stopPropagation();
|
||||
$event->setResult([
|
||||
'success' => false,
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
<?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 = [];
|
||||
|
||||
/**
|
||||
* @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('ContactUs.turnstile');
|
||||
|
||||
parent::__construct($view, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function preConnect()
|
||||
{
|
||||
return '<link rel="preconnect" href="https://challenges.cloudflare.com" />';
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function eventListener($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 $final;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _onTurnstileSuccess($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 $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function onTurnstileError($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 $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function onTurnstileExpired($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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeContactUs\Test\TestCase\View\Helper;
|
||||
|
||||
use CakeContactUs\View\Helper\TurnstileHelper;
|
||||
use Cake\TestSuite\TestCase;
|
||||
use Cake\View\View;
|
||||
|
||||
/**
|
||||
* App\View\Helper\TurnstileHelper Test Case
|
||||
*/
|
||||
class TurnstileHelperTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Test subject
|
||||
*
|
||||
* @var \CakeContactUs\View\Helper\TurnstileHelper
|
||||
*/
|
||||
protected $Turnstile;
|
||||
|
||||
/**
|
||||
* setUp method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$view = new View();
|
||||
$this->Turnstile = new TurnstileHelper($view);
|
||||
}
|
||||
|
||||
/**
|
||||
* tearDown method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
unset($this->Turnstile);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testPreConnect(): void
|
||||
{
|
||||
$this->assertStringContainsString(
|
||||
'<link rel="preconnect" href="https://challenges.cloudflare.com" />',
|
||||
$this->Turnstile->preConnect()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testScriptNoEventListener(): void
|
||||
{
|
||||
$result = $this->Turnstile->script();
|
||||
$this->assertStringContainsString(
|
||||
'<script src="https://challenges.cloudflare.com/turnstile/v0/api.js"',
|
||||
$result
|
||||
);
|
||||
$this->assertStringNotContainsString('console.log(', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testScriptWithEventListener(): void
|
||||
{
|
||||
$result = $this->Turnstile->script([
|
||||
'eventListeners' => [
|
||||
'dynamicButton' => true,
|
||||
'log' => true,
|
||||
],
|
||||
]);
|
||||
$this->assertStringContainsString(
|
||||
'<script src="https://challenges.cloudflare.com/turnstile/v0/api.js"',
|
||||
$result
|
||||
);
|
||||
$this->assertStringContainsString('console.log(', $result);
|
||||
$this->assertStringContainsString('document.getElementById("submit-btn")?.disabled = false;', $result);
|
||||
$this->assertStringContainsString('document.getElementById("submit-btn")?.disabled = true;', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRender(): void
|
||||
{
|
||||
$this->assertStringContainsString(
|
||||
'<script src="https://challenges.cloudflare.com/turnstile/v0/api.js"',
|
||||
$this->Turnstile->script()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user