From c4cd124d85b51f640a646eba42ebdf8d52ee46bc Mon Sep 17 00:00:00 2001 From: Christof Rodejohann Date: Wed, 6 Dec 2023 16:21:59 +0100 Subject: [PATCH 1/5] Add new Field type Captcha to use FriendlyCaptcha --- Classes/Domain/Validator/FieldValidator.php | 24 ++++++++- Classes/Service/Captcha/FriendlyCaptcha.php | 50 +++++++++++++++++++ .../TCA/tx_slubforms_domain_model_fields.php | 2 + Resources/Private/Partials/Forms/Captcha.html | 15 ++++++ Resources/Private/Templates/Email/Create.html | 4 +- 5 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 Classes/Service/Captcha/FriendlyCaptcha.php create mode 100644 Resources/Private/Partials/Forms/Captcha.html diff --git a/Classes/Domain/Validator/FieldValidator.php b/Classes/Domain/Validator/FieldValidator.php index 7626327..f475e5b 100644 --- a/Classes/Domain/Validator/FieldValidator.php +++ b/Classes/Domain/Validator/FieldValidator.php @@ -257,7 +257,7 @@ public function isValid($field) { } } break; - default: + default: if ($singleField->getRequired()) { if (empty($getfields[$singleField->getUid()])) { $error = $this->objectManager->get(\TYPO3\CMS\Extbase\Error\Error::class, 'val_default', 2000); @@ -290,6 +290,28 @@ public function isValid($field) { } } } + + if ($singleField->getType() == 'Captcha') { + + $config = \Slub\SlubForms\Helper\ArrayHelper::configToArray($singleField->getConfiguration()); + $captchaService = GeneralUtility::makeInstance(\Slub\SlubForms\Service\Captcha\FriendlyCaptcha::class); + $validCaptcha = $captchaService->verify($getfields[$singleField->getUid()], $config); + + if ($singleField->getRequired()) { + if (!$validCaptcha) { + $error = $this->objectManager->get(\TYPO3\CMS\Extbase\Error\Error::class, 'val_captcha', 2100); + $this->result->forProperty('content')->addError($error); + $this->isValid = false; + } + } + if ($fieldset->getRequired()) { + if ($validCaptcha) { + $fieldGroupOk++; + } + } + + } + } // if fieldset is required, check fields once more... diff --git a/Classes/Service/Captcha/FriendlyCaptcha.php b/Classes/Service/Captcha/FriendlyCaptcha.php new file mode 100644 index 0000000..08b3877 --- /dev/null +++ b/Classes/Service/Captcha/FriendlyCaptcha.php @@ -0,0 +1,50 @@ + ['Cache-Control' => 'no-cache'], + 'allow_redirects' => true, + 'form_params' => [ + 'secret' => $config['secret'], + 'sitekey' => $config['sitekey'], + 'solution' => $solution, + ], + ]; + + $client = new Client(); + $contents = $client->request('POST', self::API_URL, $options); + + if (!$contents) { + return false; + } + + try { + $result = json_decode($contents->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR); + + } catch (\JsonException $e) { + return false; + } + + return (bool)$result->success; + } + +} \ No newline at end of file diff --git a/Configuration/TCA/tx_slubforms_domain_model_fields.php b/Configuration/TCA/tx_slubforms_domain_model_fields.php index 7917e3f..78e221a 100644 --- a/Configuration/TCA/tx_slubforms_domain_model_fields.php +++ b/Configuration/TCA/tx_slubforms_domain_model_fields.php @@ -177,6 +177,7 @@ ['Submit', 'Submit'], ['-- special fields --', '--div--'], ['Description', 'Description'], + ['Captcha', 'Captcha'], ], 'size' => 1, 'maxitems' => 1, @@ -268,6 +269,7 @@ ['Url', 'url'], ['Checkbox', 'checkbox'], ['Radiobutton', 'radiobutton'], + ['Captcha', 'captcha'], ], 'size' => 1, 'maxitems' => 1, diff --git a/Resources/Private/Partials/Forms/Captcha.html b/Resources/Private/Partials/Forms/Captcha.html new file mode 100644 index 0000000..d8bc3b9 --- /dev/null +++ b/Resources/Private/Partials/Forms/Captcha.html @@ -0,0 +1,15 @@ + + + + +
+ +
+ + +
diff --git a/Resources/Private/Templates/Email/Create.html b/Resources/Private/Templates/Email/Create.html index e0e12de..d16515f 100644 --- a/Resources/Private/Templates/Email/Create.html +++ b/Resources/Private/Templates/Email/Create.html @@ -17,7 +17,9 @@

    -
  • {fieldname}: {line}
  • + +
  • {fieldname}: {line}
  • +
From 5ef2e41d4401ee57fb6dc6450cd006f91e5768fc Mon Sep 17 00:00:00 2001 From: Christof Rodejohann Date: Fri, 24 May 2024 15:07:34 +0200 Subject: [PATCH 2/5] Add ability to store sender IP in DB --- Classes/Controller/EmailController.php | 5 ++++ Classes/Domain/Model/Email.php | 26 +++++++++++++++++++ .../TCA/tx_slubforms_domain_model_email.php | 9 +++++++ ext_tables.sql | 1 + 4 files changed, 41 insertions(+) diff --git a/Classes/Controller/EmailController.php b/Classes/Controller/EmailController.php index 68203ef..1047088 100644 --- a/Classes/Controller/EmailController.php +++ b/Classes/Controller/EmailController.php @@ -302,6 +302,11 @@ public function createAction(\Slub\SlubForms\Domain\Model\Email $newEmail, array $newEmail->setSenderName('-'); } + // set sender IP when config is set + if ($this->settings['storeSenderIP']) { + $newEmail->setSenderIp(GeneralUtility::getIndpEnv('REMOTE_ADDR')); + } + $settings = array(); // add signal before sending Email $this->signalSlotDispatcher->dispatch( diff --git a/Classes/Domain/Model/Email.php b/Classes/Domain/Model/Email.php index 5a6aeb5..1e9038e 100644 --- a/Classes/Domain/Model/Email.php +++ b/Classes/Domain/Model/Email.php @@ -49,6 +49,13 @@ class Email extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity { */ protected $senderEmail; + /** + * senderIp + * + * @var string + */ + protected $senderIp; + /** * content * @@ -165,4 +172,23 @@ public function setEditcode($editcode) { $this->editcode = $editcode; } + /** + * Returns the senderIp + * + * @return string $senderIp + */ + public function getSenderIp() { + return $this->senderIp; + } + + /** + * Sets the senderIp + * + * @param string $senderIp + * @return void + */ + public function setSenderIp($senderIp) { + $this->senderIp = $senderIp; + } + } diff --git a/Configuration/TCA/tx_slubforms_domain_model_email.php b/Configuration/TCA/tx_slubforms_domain_model_email.php index a229c10..a7b8b8f 100644 --- a/Configuration/TCA/tx_slubforms_domain_model_email.php +++ b/Configuration/TCA/tx_slubforms_domain_model_email.php @@ -147,6 +147,15 @@ 'eval' => 'trim' ], ], + 'sender_ip' => [ + 'exclude' => 0, + 'label' => 'LLL:EXT:slub_forms/Resources/Private/Language/locallang_db.xlf:tx_slubforms_domain_model_email.sender_ip', + 'config' => [ + 'type' => 'input', + 'size' => 255, + 'eval' => 'trim' + ], + ], 'content' => [ 'exclude' => 0, 'label' => 'LLL:EXT:slub_forms/Resources/Private/Language/locallang_db.xlf:tx_slubforms_domain_model_email.content', diff --git a/ext_tables.sql b/ext_tables.sql index 5b1d9b5..cfb6643 100644 --- a/ext_tables.sql +++ b/ext_tables.sql @@ -148,6 +148,7 @@ CREATE TABLE tx_slubforms_domain_model_email ( sender_name varchar(255) DEFAULT '' NOT NULL, sender_email varchar(255) DEFAULT '' NOT NULL, + sender_ip varchar(255) DEFAULT '' NOT NULL, content text NOT NULL, editcode varchar(255) DEFAULT '' NOT NULL, form int(11) unsigned DEFAULT '0', From 730465aeb6dea862cae89b4441960e061ff8078a Mon Sep 17 00:00:00 2001 From: Christof Rodejohann Date: Tue, 9 Jul 2024 15:20:27 +0200 Subject: [PATCH 3/5] Set new defaults --- Configuration/TypoScript/setup.typoscript | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Configuration/TypoScript/setup.typoscript b/Configuration/TypoScript/setup.typoscript index a44a778..1dd8eed 100644 --- a/Configuration/TypoScript/setup.typoscript +++ b/Configuration/TypoScript/setup.typoscript @@ -11,7 +11,7 @@ plugin.tx_slubforms { skipDefaultArguments = 1 } settings { - senderEmailAddress = webmaster@slub-dresden.de + senderEmailAddress = noreply@slub-dresden.de // allow sending anonymous mails #anonymEmails { @@ -21,6 +21,8 @@ plugin.tx_slubforms { // send email to customer as confirmation sendConfirmationEmailToCustomer = 1 + + storeSenderIP = 0 } } From 4e72dd563d06510fa6d1d41f9326520d62be46f0 Mon Sep 17 00:00:00 2001 From: Christof Rodejohann Date: Tue, 9 Jul 2024 16:17:15 +0200 Subject: [PATCH 4/5] Add option to overwriteFromEmailAdressToOwner --- Classes/Controller/EmailController.php | 12 +++++++++++- Configuration/TypoScript/setup.typoscript | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Classes/Controller/EmailController.php b/Classes/Controller/EmailController.php index 1047088..3bf2e3a 100644 --- a/Classes/Controller/EmailController.php +++ b/Classes/Controller/EmailController.php @@ -220,6 +220,10 @@ public function createAction(\Slub\SlubForms\Domain\Model\Email $newEmail, array } + } else if ($field->getType() == 'Captcha') { + + $content[$field->getTitle()] = '-'; + } else { $content[$field->getTitle()] = empty($getfields[$field->getUid()]) ? '-' : $getfields[$field->getUid()]; @@ -341,9 +345,15 @@ public function createAction(\Slub\SlubForms\Domain\Model\Email $newEmail, array } // email to form owner + + $senderEmail = $newEmail->getSenderEmail(); + if($this->settings['overwriteFromEmailAdressToOwner'] && strlen($this->settings['overwriteFromEmailAdressToOwner']) > 0) { + $senderEmail = $this->settings['overwriteFromEmailAdressToOwner']; + } + $this->sendTemplateEmail( array($form->getRecipient() => ''), - array($newEmail->getSenderEmail() => $newEmail->getSenderName()), + array($senderEmail => $newEmail->getSenderName()), LocalizationUtility::translate('tx_slubforms_domain_model_email.form', 'slub_forms') . ': ' . $form->getTitle() . ': '. $newEmail->getSenderName(). ', '. $newEmail->getSenderEmail() , 'FormEmail', array( 'email' => $newEmail, diff --git a/Configuration/TypoScript/setup.typoscript b/Configuration/TypoScript/setup.typoscript index 1dd8eed..ac70b60 100644 --- a/Configuration/TypoScript/setup.typoscript +++ b/Configuration/TypoScript/setup.typoscript @@ -13,6 +13,8 @@ plugin.tx_slubforms { settings { senderEmailAddress = noreply@slub-dresden.de + overwriteFromEmailAdressToOwner = noreply@slub-dresden.de + // allow sending anonymous mails #anonymEmails { # allow = 1 From 060a6251213ed9912910d445d63fc5f9ba2af9dc Mon Sep 17 00:00:00 2001 From: Christof Rodejohann Date: Wed, 10 Jul 2024 14:40:23 +0200 Subject: [PATCH 5/5] Read captcha config from Backend Field config --- .../Form/ParseConfigurationViewHelper.php | 42 +++++++++++++++++++ Resources/Private/Partials/Forms/Captcha.html | 5 ++- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 Classes/ViewHelpers/Form/ParseConfigurationViewHelper.php diff --git a/Classes/ViewHelpers/Form/ParseConfigurationViewHelper.php b/Classes/ViewHelpers/Form/ParseConfigurationViewHelper.php new file mode 100644 index 0000000..2a9b516 --- /dev/null +++ b/Classes/ViewHelpers/Form/ParseConfigurationViewHelper.php @@ -0,0 +1,42 @@ +registerArgument('configurationString', 'string', 'Config string', true); + + } + + /** + * @return string + */ + public static function renderStatic( + array $arguments, + \Closure $renderChildrenClosure, + RenderingContextInterface $renderingContext + ) { + + $templateVariableContainer = $renderingContext->getVariableProvider(); + + if ($templateVariableContainer->exists('configuration')) { + $templateVariableContainer->remove('configuration'); + } + $templateVariableContainer->add('configuration', \Slub\SlubForms\Helper\ArrayHelper::configToArray($arguments['configurationString'])); + + return $renderChildrenClosure(); + + } + + +} \ No newline at end of file diff --git a/Resources/Private/Partials/Forms/Captcha.html b/Resources/Private/Partials/Forms/Captcha.html index d8bc3b9..1192a02 100644 --- a/Resources/Private/Partials/Forms/Captcha.html +++ b/Resources/Private/Partials/Forms/Captcha.html @@ -1,3 +1,4 @@ +{namespace sf=Slub\SlubForms\ViewHelpers}