-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] FriendlyCaptcha v2 - Powermail BE Validator
- Loading branch information
Showing
6 changed files
with
138 additions
and
50 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
Classes/FieldValidator/AbstractPowermailCaptchaValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters