Skip to content

Commit

Permalink
[FEATURE] FriendlyCaptcha v2 - Powermail BE Validator
Browse files Browse the repository at this point in the history
  • Loading branch information
80Quattro committed Oct 10, 2024
1 parent ba5a48a commit ac73086
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 50 deletions.
34 changes: 34 additions & 0 deletions Classes/FieldValidator/AbstractPowermailCaptchaValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace StudioMitte\FriendlyCaptcha\FieldValidator;

use In2code\Powermail\Domain\Validator\AbstractValidator;
use TYPO3\CMS\Core\Utility\GeneralUtility;

abstract class AbstractPowermailCaptchaValidator extends AbstractValidator
{
/**
* 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'];
}
}
26 changes: 1 addition & 25 deletions Classes/FieldValidator/PowermailV11Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'];
}
}
41 changes: 41 additions & 0 deletions Classes/FieldValidator/PowermailV11ValidatorV2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace StudioMitte\FriendlyCaptcha\FieldValidator;

use StudioMitte\FriendlyCaptcha\Service\ApiV2;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class PowermailV11ValidatorV2 extends AbstractPowermailCaptchaValidator
{
/**
* @param Mail $mail
*/
public function isValid($mail): void
{
if (!$this->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;
}
}
26 changes: 1 addition & 25 deletions Classes/FieldValidator/PowermailValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'];
Expand Down
47 changes: 47 additions & 0 deletions Classes/FieldValidator/PowermailValidatorV2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace StudioMitte\FriendlyCaptcha\FieldValidator;

use StudioMitte\FriendlyCaptcha\Service\ApiV2;
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 PowermailValidatorV2 extends AbstractPowermailCaptchaValidator
{
/**
* @param Mail $mail
* @return Result
*/
public function validate($mail): Result
{
$result = new Result();
if (!$this->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'];
}
}
14 changes: 14 additions & 0 deletions Configuration/TypoScript/Powermail/setup.typoscript
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}

Expand All @@ -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]

0 comments on commit ac73086

Please sign in to comment.