Merge branch 'dev' into prod
CI / testsuite (mysql, 8.2, ) (push) Successful in 2m57s
CI / testsuite (mysql, 8.4, ) (push) Successful in 4m10s
CI / testsuite (pgsql, 8.2, ) (push) Successful in 2m58s
CI / testsuite (pgsql, 8.4, ) (push) Successful in 4m6s
CI / testsuite (sqlite, 8.2, ) (push) Successful in 2m58s
CI / testsuite (sqlite, 8.2, prefer-lowest) (push) Successful in 3m3s
CI / testsuite (sqlite, 8.4, ) (push) Successful in 4m14s
CI / Coding Standard & Static Analysis (push) Successful in 2m13s
Release / build (push) Successful in 10s

This commit is contained in:
bs
2026-11-15 12:07:57 -08:00
8 changed files with 99 additions and 47 deletions
+10 -13
View File
@@ -4,9 +4,10 @@
return [
'ContactUs' => [
'fields' => [
'subject' => true,
'subject' => false,
'email' => true,
'captcha' => true,
'captcha' => false,
'turnstile' => false,
],
'clientIpHeader' => null,
'email' => [
@@ -19,6 +20,7 @@ return [
'bcc' => '', // email string or array of emails
],
],
'model' => 'CakeContactUs.ContactUsFormSubmissions',
'addIdToRedirect' => true,
'redirectUrl' => [
'prefix' => 'Admin',
@@ -26,18 +28,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
],
];
+38 -26
View File
@@ -24,14 +24,17 @@ class ContactUsComponent extends Component {
/**
* @var \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable|\Cake\ORM\Table
*/
protected ContactUsFormSubmissionsTable|Table $ContactUsFormSubmissions;
protected ContactUsFormSubmissionsTable|Table $table;
/**
* Default configuration.
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [];
protected array $_defaultConfig = [
'actions' => [],
'model' => 'CakeContactUs.ContactUsFormSubmissions',
];
/**
* @var array|string[]
@@ -76,24 +79,32 @@ class ContactUsComponent extends Component {
*
*/
public function initialize(array $config): void {
$config = array_merge($config, Configure::read('ContactUs'));
$this->setConfig($config);
parent::initialize($config); // TODO: Change the autogenerated stub
$this->table = TableRegistry::getTableLocator()->get($this->getConfig('model'));
if (!Configure::read('ContactUs.fields.turnstile')) {
return;
}
if (!$this->getController()->components()->has('FormProtection')) {
return;
}
if (method_exists($this->getController()->components()->get('FormProtection'), 'unlockFields')) {
$this->getController()->components()->get('FormProtection')->unlockFields('cf-turnstile-response');
}
}
$this->ContactUsFormSubmissions = TableRegistry::getTableLocator()->get('CakeContactUs.ContactUsFormSubmissions');
$requireCaptcha = Configure::readOrFail('ContactUs.fields.captcha');
$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'));
$this->setConfig('requireCaptcha', $requireCaptcha);
$this->setConfig('turnstile', $useTurnstile);
if ($requireCaptcha) {
/**
* @param \Cake\Event\EventInterface $event
*
* @return void
*/
public function beforeRender(EventInterface $event): void {
if (Configure::read('ContactUs.fields.captcha')) {
$this->getController()->viewBuilder()->addHelpers(['Captcha.Captcha']);
}
if ($useTurnstile['enabled'] ?? false) {
// $this->getController()->viewBuilder()->addHelpers(['Turnstile']);
if (Configure::read('ContactUs.fields.turnstile')) {
$this->getController()->viewBuilder()->addHelper('CakeContactUs.Turnstile', Configure::readOrFail('Turnstile'));
}
}
@@ -102,10 +113,10 @@ class ContactUsComponent extends Component {
*/
public function newContactUsForm() {
if ($this->getConfig('requireCaptcha') && PHP_SAPI !== 'cli') {
$this->ContactUsFormSubmissions->addBehavior('Captcha.Captcha');
$this->table->addBehavior('Captcha.Captcha');
}
return $this->ContactUsFormSubmissions->newEmptyEntity();
return $this->table->newEmptyEntity();
}
/**
@@ -119,13 +130,15 @@ class ContactUsComponent extends Component {
}
$postData['client_ip'] = $this->_getIp();
$postData['submitted_at'] = DateTime::now();
$contactUsFormSubmission = $this->ContactUsFormSubmissions->patchEntity($contactUsFormSubmission, $postData);
$contactUsFormSubmission = $this->table->patchEntity($contactUsFormSubmission, $postData);
$this->getController()->set(['contactUsFormSubmission' => $contactUsFormSubmission]);
$event = $this->getController()->dispatchEvent(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED, [
'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']);
@@ -180,10 +193,9 @@ class ContactUsComponent extends Component {
return $result;
}
$this->Flash->success($message);
$redirectUrl = $this->getConfig('redirectUrl');
if ($this->getConfig('addIdToRedirect')) {
$redirectUrl[] = $contactUsFormSubmissionSaved->get($this->ContactUsFormSubmissions->getPrimaryKey());
$redirectUrl[] = $contactUsFormSubmissionSaved->get($this->table->getPrimaryKey());
}
return $this->getController()->redirect($redirectUrl);
@@ -193,11 +205,11 @@ class ContactUsComponent extends Component {
* @return void
*/
public function beforeFormSaved(EventInterface $event) {
if (!$this->getConfig('turnstile')) {
if (!Configure::read('ContactUs.fields.turnstile')) {
return;
}
$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');
$siteKey = getenv(CakeContactUsPlugin::CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SITE_KEY) ?: Configure::readOrFail('Turnstile.siteKey');
$secretKey = getenv(CakeContactUsPlugin::CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SECRET_KEY) ?: Configure::readOrFail('Turnstile.secretKey');
if (!$siteKey || !$secretKey) {
$event->stopPropagation();
$event->setResult([
@@ -247,7 +259,7 @@ class ContactUsComponent extends Component {
* @return \Cake\Datasource\EntityInterface|\Cake\Http\Response
*/
protected function _finishSave(EntityInterface $contactUsFormSubmission) {
$contactUsFormSubmissionSaved = $this->ContactUsFormSubmissions->save($contactUsFormSubmission);
$contactUsFormSubmissionSaved = $this->table->save($contactUsFormSubmission);
if ($contactUsFormSubmissionSaved) {
return $this->_afterFormSaved($contactUsFormSubmissionSaved);
@@ -268,7 +280,7 @@ class ContactUsComponent extends Component {
// }
$url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
$data = [
'secret' => $this->getConfig('turnstile.secretKey'),
'secret' => Configure::readOrFail('Turnstile.secretKey'),
'response' => $token,
];
@@ -18,7 +18,9 @@ class ContactUsFormSubmissionsController extends AppController {
*/
public function initialize(): void {
parent::initialize(); // TODO: Change the autogenerated stub
$this->loadComponent('CakeContactUs.ContactUs');
$this->loadComponent('CakeContactUs.ContactUs', [
'actions' => ['contact'],
]);
}
/**
+30 -3
View File
@@ -19,7 +19,9 @@ class TurnstileHelper extends Helper {
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [];
protected array $_defaultConfig = [
'eventListeners' => true,
];
/**
* @var array|string[]
@@ -31,7 +33,7 @@ class TurnstileHelper extends Helper {
* @param array<string, mixed> $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 .= '</script>';
return $final;
return $this->Html->scriptBlock($final);
}
/**
+1 -1
View File
@@ -12,7 +12,7 @@
<legend><?= __('Add Contact Us Form Submission') ?></legend>
<?php echo $this->element('ContactUsFormSubmissions/form'); ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->button(__('Submit'), ['id' => 'submit-btn']) ?>
<?= $this->Form->end() ?>
</div>
</div>
@@ -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() : '';
?>
@@ -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',
]);
@@ -198,6 +202,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase {
Configure::write('ContactUs.fields', [
'captcha' => false,
'email' => true,
'turnstile' => false,
]);
Configure::write('ContactUs.email', [
'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions',
@@ -244,6 +249,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase {
Configure::write('ContactUs.fields', [
'captcha' => false,
'email' => true,
'turnstile' => false,
]);
Configure::write('ContactUs.email', [
'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions',
+8 -1
View File
@@ -63,7 +63,11 @@ Configure::write('ContactUs', [
'fields' => [
'captcha' => false,
'email' => true,
'turnstile' => false,
],
'model' => 'CakeContactUs.ContactUsFormSubmissions',
'addIdToRedirect' => false,
'redirectUrl' => '/',
'email' => [
'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions',
'confirmation' => false, // true or false
@@ -72,12 +76,15 @@ 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,