From ac73086d601f448d67df6be66819e820d1ccede3 Mon Sep 17 00:00:00 2001 From: Rafal Jania Date: Thu, 10 Oct 2024 13:47:14 +0200 Subject: [PATCH] [FEATURE] FriendlyCaptcha v2 - Powermail BE Validator --- .../AbstractPowermailCaptchaValidator.php | 34 ++++++++++++++ .../FieldValidator/PowermailV11Validator.php | 26 +--------- .../PowermailV11ValidatorV2.php | 41 ++++++++++++++++ Classes/FieldValidator/PowermailValidator.php | 26 +--------- .../FieldValidator/PowermailValidatorV2.php | 47 +++++++++++++++++++ .../TypoScript/Powermail/setup.typoscript | 14 ++++++ 6 files changed, 138 insertions(+), 50 deletions(-) create mode 100644 Classes/FieldValidator/AbstractPowermailCaptchaValidator.php create mode 100644 Classes/FieldValidator/PowermailV11ValidatorV2.php create mode 100644 Classes/FieldValidator/PowermailValidatorV2.php diff --git a/Classes/FieldValidator/AbstractPowermailCaptchaValidator.php b/Classes/FieldValidator/AbstractPowermailCaptchaValidator.php new file mode 100644 index 0000000..8de6f3d --- /dev/null +++ b/Classes/FieldValidator/AbstractPowermailCaptchaValidator.php @@ -0,0 +1,34 @@ +flexForm is only available in powermail 3.9 or newer + */ + protected function isCaptchaCheckToSkip(): bool + { + if (property_exists($this, 'flexForm')) { + $confirmationActive = $this->flexForm['settings']['flexform']['main']['confirmation'] === '1'; + return $this->getActionName() === 'create' && $confirmationActive; + } + return false; + } + + /** + * @return string "confirmation" or "create" + */ + protected function getActionName(): string + { + $pluginVariables = GeneralUtility::_GPmerged('tx_powermail_pi1'); + return $pluginVariables['action']; + } +} diff --git a/Classes/FieldValidator/PowermailV11Validator.php b/Classes/FieldValidator/PowermailV11Validator.php index fee83a8..9281c4a 100644 --- a/Classes/FieldValidator/PowermailV11Validator.php +++ b/Classes/FieldValidator/PowermailV11Validator.php @@ -4,11 +4,10 @@ namespace StudioMitte\FriendlyCaptcha\FieldValidator; -use In2code\Powermail\Domain\Validator\AbstractValidator; use StudioMitte\FriendlyCaptcha\Service\Api; use TYPO3\CMS\Core\Utility\GeneralUtility; -class PowermailV11Validator extends AbstractValidator +class PowermailV11Validator extends AbstractPowermailCaptchaValidator { /** * @param Mail $mail @@ -39,27 +38,4 @@ protected function isFormWithCaptchaField($mail): bool } return false; } - - /** - * Captcha check should be skipped on createAction if there was a confirmationAction where the captcha was - * already checked before - * Note: $this->flexForm is only available in powermail 3.9 or newer - */ - protected function isCaptchaCheckToSkip(): bool - { - if (property_exists($this, 'flexForm')) { - $confirmationActive = $this->flexForm['settings']['flexform']['main']['confirmation'] === '1'; - return $this->getActionName() === 'create' && $confirmationActive; - } - return false; - } - - /** - * @return string "confirmation" or "create" - */ - protected function getActionName(): string - { - $pluginVariables = GeneralUtility::_GPmerged('tx_powermail_pi1'); - return $pluginVariables['action']; - } } diff --git a/Classes/FieldValidator/PowermailV11ValidatorV2.php b/Classes/FieldValidator/PowermailV11ValidatorV2.php new file mode 100644 index 0000000..a53c48b --- /dev/null +++ b/Classes/FieldValidator/PowermailV11ValidatorV2.php @@ -0,0 +1,41 @@ +isFormWithCaptchaField($mail) || $this->isCaptchaCheckToSkip()) { + return; + } + + $friendlyCaptchaService = GeneralUtility::makeInstance(ApiV2::class); + if (!$friendlyCaptchaService->verify()) { + $this->addError( + $this->translateErrorMessage('message.invalid', 'friendlycaptcha_official'), + 1689157219, + ); + } + } + + protected function isFormWithCaptchaField($mail): bool + { + foreach ($mail->getForm()->getPages() as $page) { + foreach ($page->getFields() as $field) { + if ($field->getType() === 'friendlycaptchaV2') { + return true; + } + } + } + return false; + } +} diff --git a/Classes/FieldValidator/PowermailValidator.php b/Classes/FieldValidator/PowermailValidator.php index 8e9e0ee..3ff2d56 100644 --- a/Classes/FieldValidator/PowermailValidator.php +++ b/Classes/FieldValidator/PowermailValidator.php @@ -4,14 +4,13 @@ namespace StudioMitte\FriendlyCaptcha\FieldValidator; -use In2code\Powermail\Domain\Validator\AbstractValidator; use StudioMitte\FriendlyCaptcha\Service\Api; use TYPO3\CMS\Core\Localization\LanguageService; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Error\Error; use TYPO3\CMS\Extbase\Error\Result; -class PowermailValidator extends AbstractValidator +class PowermailValidator extends AbstractPowermailCaptchaValidator { /** * @param Mail $mail @@ -53,29 +52,6 @@ protected function isFormWithCaptchaField($mail): bool return false; } - /** - * Captcha check should be skipped on createAction if there was a confirmationAction where the captcha was - * already checked before - * Note: $this->flexForm is only available in powermail 3.9 or newer - */ - protected function isCaptchaCheckToSkip(): bool - { - if (property_exists($this, 'flexForm')) { - $confirmationActive = $this->flexForm['settings']['flexform']['main']['confirmation'] === '1'; - return $this->getActionName() === 'create' && $confirmationActive; - } - return false; - } - - /** - * @return string "confirmation" or "create" - */ - protected function getActionName(): string - { - $pluginVariables = GeneralUtility::_GPmerged('tx_powermail_pi1'); - return $pluginVariables['action']; - } - private function getLanguageService(): LanguageService { return $GLOBALS['LANG']; diff --git a/Classes/FieldValidator/PowermailValidatorV2.php b/Classes/FieldValidator/PowermailValidatorV2.php new file mode 100644 index 0000000..4112c1d --- /dev/null +++ b/Classes/FieldValidator/PowermailValidatorV2.php @@ -0,0 +1,47 @@ +isFormWithCaptchaField($mail) || $this->isCaptchaCheckToSkip()) { + return $result; + } + + $friendlyCaptchaService = GeneralUtility::makeInstance(ApiV2::class); + if (!$friendlyCaptchaService->verify()) { + $result->addError( + new Error( + $this->getLanguageService()->sL('LLL:EXT:friendlycaptcha_official/Resources/Private/Language/locallang.xlf:message.invalid'), + 1689157219 + ) + ); + } + return $result; + } + + public function isValid(mixed $mail): void + { + return; + } + + private function getLanguageService(): LanguageService + { + return $GLOBALS['LANG']; + } +} diff --git a/Configuration/TypoScript/Powermail/setup.typoscript b/Configuration/TypoScript/Powermail/setup.typoscript index 4c0b2b8..64986cb 100644 --- a/Configuration/TypoScript/Powermail/setup.typoscript +++ b/Configuration/TypoScript/Powermail/setup.typoscript @@ -13,6 +13,13 @@ plugin.tx_powermail { dummy = 1 } } + 981819 { + class = StudioMitte\FriendlyCaptcha\FieldValidator\PowermailValidatorV2 + config { + # Until https://github.com/in2code-de/powermail/pull/941 is merged & released + dummy = 1 + } + } } } @@ -25,5 +32,12 @@ plugin.tx_powermail { dummy = 1 } } + 981819 { + class = StudioMitte\FriendlyCaptcha\FieldValidator\PowermailV11ValidatorV2 + config { + # Until https://github.com/in2code-de/powermail/pull/941 is merged & released + dummy = 1 + } + } } [global]