diff --git a/config/app.example.php b/config/app.example.php index 4a65788..ff74c32 100644 --- a/config/app.example.php +++ b/config/app.example.php @@ -7,6 +7,7 @@ return [ 'subject' => true, 'email' => true, 'captcha' => true, + 'turnstile' => true, ], 'clientIpHeader' => null, 'email' => [ @@ -26,18 +27,13 @@ return [ 'controller' => 'ContactUsFormSubmissions', 'action' => 'view', ], + ], + 'Turnstile' => [ + 'enabled' => true, /** - * @link https://developers.cloudflare.com/turnstile/troubleshooting/testing/ - * - * secretKeys for testing: - * always passes = 1x0000000000000000000000000000000AA - * always fails = 2x0000000000000000000000000000000AA - * token already spent error 3x0000000000000000000000000000000AA + * @link https://developers.cloudflare.com/turnstile/troubleshooting/testing/ for testing keys */ - 'turnstile' => [ - '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 - ], + '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 ], ]; diff --git a/src/Controller/Component/ContactUsComponent.php b/src/Controller/Component/ContactUsComponent.php index 0f18403..ba5599a 100644 --- a/src/Controller/Component/ContactUsComponent.php +++ b/src/Controller/Component/ContactUsComponent.php @@ -81,19 +81,17 @@ class ContactUsComponent extends Component { $this->ContactUsFormSubmissions = TableRegistry::getTableLocator()->get('CakeContactUs.ContactUsFormSubmissions'); $requireCaptcha = Configure::readOrFail('ContactUs.fields.captcha'); - $useTurnstile = Configure::read('ContactUs.turnstile', [ - 'enabled' => false, - ]); + $requireTurnstile = Configure::readOrFail('ContactUs.fields.turnstile'); $this->setConfig('redirectUrl', Configure::read('ContactUs.redirectUrl', '/')); $this->setConfig('addIdToRedirect', Configure::read('ContactUs.addIdToRedirect', false)); $this->setConfig('requireEmail', Configure::readOrFail('ContactUs.fields.email')); $this->setConfig('requireCaptcha', $requireCaptcha); - $this->setConfig('turnstile', $useTurnstile); if ($requireCaptcha) { $this->getController()->viewBuilder()->addHelpers(['Captcha.Captcha']); } - if ($useTurnstile['enabled'] ?? false) { -// $this->getController()->viewBuilder()->addHelpers(['Turnstile']); + if ($requireTurnstile) { + $this->setConfig('turnstile', Configure::readOrFail('Turnstile')); + $this->getController()->viewBuilder()->addHelper('CakeContactUs.Turnstile', Configure::readOrFail('Turnstile')); } } @@ -126,6 +124,8 @@ class ContactUsComponent extends Component { 'contactUsFormSubmission' => $contactUsFormSubmission, ], $this->getController()); $result = $event->getResult(); + + // only thing within this plugin that will stop processing here is if turnstile validation fails if ($event->isStopped()) { if ($result['redirect'] ?? false) { return $this->getController()->redirect($result['redirect']); diff --git a/src/View/Helper/TurnstileHelper.php b/src/View/Helper/TurnstileHelper.php index 2cb2aed..64493e8 100644 --- a/src/View/Helper/TurnstileHelper.php +++ b/src/View/Helper/TurnstileHelper.php @@ -19,7 +19,9 @@ class TurnstileHelper extends Helper { * * @var array */ - protected array $_defaultConfig = []; + protected array $_defaultConfig = [ + 'eventListeners' => true, + ]; /** * @var array|string[] @@ -31,7 +33,7 @@ class TurnstileHelper extends Helper { * @param array $config */ public function __construct(View $view, array $config = []) { - $config += (array)Configure::read('ContactUs.turnstile'); + $config += (array)Configure::read('Turnstile'); parent::__construct($view, $config); } @@ -46,6 +48,31 @@ class TurnstileHelper extends Helper { /** * @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 = []) { @@ -80,7 +107,7 @@ class TurnstileHelper extends Helper { $final .= $this->onTurnstileExpired($options); $final .= ''; - return $final; + return $this->Html->scriptBlock($final); } /** diff --git a/templates/ContactUsFormSubmissions/add.php b/templates/ContactUsFormSubmissions/add.php index 17e8219..689931e 100644 --- a/templates/ContactUsFormSubmissions/add.php +++ b/templates/ContactUsFormSubmissions/add.php @@ -12,7 +12,7 @@ element('ContactUsFormSubmissions/form'); ?> - Form->button(__('Submit')) ?> + Form->button(__('Submit'), ['id' => 'submit-btn']) ?> Form->end() ?> diff --git a/templates/element/ContactUsFormSubmissions/form.php b/templates/element/ContactUsFormSubmissions/form.php index 22c779f..5533f15 100644 --- a/templates/element/ContactUsFormSubmissions/form.php +++ b/templates/element/ContactUsFormSubmissions/form.php @@ -13,4 +13,5 @@ echo isset($fields['email']) && $fields['email'] ? $this->Form->control('email', echo isset($fields['subject']) && $fields['subject'] ? $this->Form->control('subject', ['required' => true]) : ''; echo $this->Form->control('message'); echo isset($fields['captcha']) && $fields['captcha'] ? $this->Captcha->render(['placeholder' => __('Please solve the riddle')]) : ''; +echo isset($fields['turnstile']) && $fields['turnstile'] ? $this->Turnstile->render() : ''; ?> diff --git a/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php b/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php index 02fa224..ec503c2 100644 --- a/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php +++ b/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php @@ -125,7 +125,9 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { */ public function testAddPostSuccessWithTurnstile(): void { $cntBefore = $this->ContactUsFormSubmissions->find()->count(); - Configure::write('ContactUs.turnstile', [ + Configure::write('ContactUs.fields.turnstile', true); + Configure::write('Turnstile', [ + 'enabled' => true, 'siteKey' => '1x00000000000000000000AA', 'secretKey' => '1x0000000000000000000000000000000AA', ]); @@ -161,7 +163,9 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { */ public function testAddPostInvalidTurnstile(): void { $cntBefore = $this->ContactUsFormSubmissions->find()->count(); - Configure::write('ContactUs.turnstile', [ + Configure::write('ContactUs.fields.turnstile', true); + Configure::write('Turnstile', [ + 'enabled' => true, 'siteKey' => '2x00000000000000000000BB', 'secretKey' => '2x0000000000000000000000000000000AA', ]); @@ -178,6 +182,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { 'cf-turnstile-response' => 'XXXX.DUMMY.TOKEN.XXXX', ]; $this->post($url, $data); +// dd($this->_response); $this->assertResponseCode(200); $cntAfter = $this->ContactUsFormSubmissions->find()->count(); $this->assertEquals($cntBefore, $cntAfter); @@ -198,6 +203,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { Configure::write('ContactUs.fields', [ 'captcha' => false, 'email' => true, + 'turnstile' => false, ]); Configure::write('ContactUs.email', [ 'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions', @@ -244,6 +250,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase { Configure::write('ContactUs.fields', [ 'captcha' => false, 'email' => true, + 'turnstile' => false, ]); Configure::write('ContactUs.email', [ 'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions', diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 4414081..826599a 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -63,6 +63,7 @@ Configure::write('ContactUs', [ 'fields' => [ 'captcha' => false, 'email' => true, + 'turnstile' => false, ], 'email' => [ 'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions', @@ -72,12 +73,14 @@ Configure::write('ContactUs', [ 'to' => 'test@example.com', ], ], - 'turnstile' => false, // 'turnstile' => [ // 'siteKey' => '1x00000000000000000000AA', // 'secretKey' => '1x0000000000000000000000000000000AA', // ], ]); +Configure::write('Turnstile', [ + 'enabled' => false, +]); Configure::write('EmailTransport', [ 'default' => [ 'className' => DebugTransport::class,