Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions Classes/Validation/Validator/AbstractCaptchaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;

abstract class AbstractCaptchaValidator extends AbstractValidator
Expand All @@ -23,31 +22,26 @@ abstract class AbstractCaptchaValidator extends AbstractValidator
/**
* @var array Extension TypoScript
*/
protected $settings;
protected array $settings;

public function __construct(array $options = array())
public function __construct(ConfigurationManagerInterface $configurationManager)
{
parent::__construct($options);
/** @var ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var ConfigurationManagerInterface $configurationManager */
$configurationManager = $objectManager->get(ConfigurationManagerInterface::class);
// /** @var ConfigurationManagerInterface $configurationManager */
// $configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
$this->settings = $configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'JhCaptcha'
'JhCaptcha',
);
}

/**
* Creates a new validation error object and adds it to $this->results.
*
* @param string $translateKey
* @param int $code The error code (a unix timestamp)
* @param array $arguments Arguments to be replaced in message
* @param string $title title of the error
* @param int $code The error code (a unix timestamp)
*/
protected function addError($translateKey, $code, array $arguments = [], $title = '')
protected function addTranslatedError(string $translateKey, int $code): void
{
parent::addError($this->translateErrorMessage($translateKey, 'jh_captcha'), $code);
$this->addError($this->translateErrorMessage($translateKey, 'jh_captcha'), $code);
}
}
20 changes: 10 additions & 10 deletions Classes/Validation/Validator/ReCaptchaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ReCaptchaValidator extends AbstractCaptchaValidator
*
* @param mixed $value
*/
protected function isValid($value)
protected function isValid(mixed $value): void
{
if ($this->settings['reCaptcha']['version'] == 2) {
$secret = htmlspecialchars($this->settings['reCaptcha']['v2']['secretKey']);
Expand All @@ -31,34 +31,34 @@ protected function isValid($value)
foreach ($apiResponse['error-codes'] as $errorCode) {
switch ($errorCode) {
case 'missing-input-secret':
$this->addError('missingInputSecret', 1426877004);
$this->addTranslatedError('missingInputSecret', 1426877004);
break;
case 'invalid-input-secret':
$this->addError('invalidInputSecret', 1426877455);
$this->addTranslatedError('invalidInputSecret', 1426877455);
break;
case 'missing-input-response':
$this->addError('missingInputResponse', 1426877525);
$this->addTranslatedError('missingInputResponse', 1426877525);
break;
case 'invalid-input-response':
$this->addError('invalidInputResponse', 1426877590);
$this->addTranslatedError('invalidInputResponse', 1426877590);
break;
case 'bad-request':
$this->addError('badRequest', 1426877490);
$this->addTranslatedError('badRequest', 1426877490);
break;
case 'timeout-or-duplicate':
$this->addError('timeoutOrDuplicate', 1426877420);
$this->addTranslatedError('timeoutOrDuplicate', 1426877420);
break;
default:
$this->addError('defaultError', 1427031929);
$this->addTranslatedError('defaultError', 1427031929);
}
}
} else {
$this->addError('defaultError', 1427031929);
$this->addTranslatedError('defaultError', 1427031929);
}
} else {
if ($this->settings['reCaptcha']['version'] != 2 && isset($apiResponse['score'])) {
if ($apiResponse['score'] < $this->settings['reCaptcha']['v3']['minimumScore']) {
$this->addError('scoreError', 1541173838);
$this->addTranslatedError('scoreError', 1541173838);
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions Classes/ViewHelpers/ReCaptchaViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@ class ReCaptchaViewHelper extends \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractView
*/
protected $escapeOutput = false;

/**
* @var ConfigurationManagerInterface
*/
protected $configurationManager;
protected ConfigurationManagerInterface $configurationManager;

/**
* @param ConfigurationManagerInterface $configurationManager
*/
public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
public function __construct(ConfigurationManagerInterface $configurationManager)
{
$this->configurationManager = $configurationManager;
}
Expand Down
11 changes: 11 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

Haffner\JhCaptcha\:
resource: '../Classes/*'
exclude:
- '../Classes/**/{Model,Exception}/*'

1 change: 0 additions & 1 deletion Configuration/TCA/Overrides/sys_template.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
defined('TYPO3_MODE') or die();

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile(
'jh_captcha',
Expand Down
32 changes: 1 addition & 31 deletions Configuration/TypoScript/constants.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1 @@

plugin.tx_jhcaptcha.settings {
reCaptcha {
# cat=plugin.tx_jhcaptcha/recaptcha; type=options[2,3]; label=reCAPTCHA version (2 or 3)
version = 3
v2 {
# cat=plugin.tx_jhcaptcha/recaptcha/v2; type=options[dark,light]; label=The color theme of the widget
theme = light
# cat=plugin.tx_jhcaptcha/recaptcha/v2; type=string; label=The language of the widget: See https://developers.google.com/recaptcha/docs/language
lang = en
# cat=plugin.tx_jhcaptcha/recaptcha/v2; type=options[normal,compact]; label=The size of the widget
size = normal
# cat=plugin.tx_jhcaptcha/recaptcha/v2; type=string; label=Site key (v2): See https://www.google.com/recaptcha/admin
siteKey =
# cat=plugin.tx_jhcaptcha/recaptcha/v2; type=string; label=Secret key (v2): See https://www.google.com/recaptcha/admin
secretKey =
}
v3 {
# cat=plugin.tx_jhcaptcha/recaptcha/v3; type=string; label=Site key (v3): See https://www.google.com/recaptcha/admin
siteKey =
# cat=plugin.tx_jhcaptcha/recaptcha/v3; type=string; label=Secret key (v3): See https://www.google.com/recaptcha/admin
secretKey =
# cat=plugin.tx_jhcaptcha/recaptcha/v3; type=string; label=Minimum Score (0.0 - 1.0)
minimumScore = 0.5
# cat=plugin.tx_jhcaptcha/recaptcha/v3; type=string; label=Action: See https://developers.google.com/recaptcha/docs/v3
action = homepage
}
}
# cat=plugin.tx_jhcaptcha/powermail; type=int; label= If this check fails - add other indication value (0 disables this check completely)
powermail.recaptchaId = 11
}
@import 'EXT:jh_captcha/Configuration/TypoScript/constants.typoscript'
31 changes: 31 additions & 0 deletions Configuration/TypoScript/constants.typoscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

plugin.tx_jhcaptcha.settings {
reCaptcha {
# cat=plugin.tx_jhcaptcha/recaptcha; type=options[2,3]; label=reCAPTCHA version (2 or 3)
version = 3
v2 {
# cat=plugin.tx_jhcaptcha/recaptcha/v2; type=options[dark,light]; label=The color theme of the widget
theme = light
# cat=plugin.tx_jhcaptcha/recaptcha/v2; type=string; label=The language of the widget: See https://developers.google.com/recaptcha/docs/language
lang = en
# cat=plugin.tx_jhcaptcha/recaptcha/v2; type=options[normal,compact]; label=The size of the widget
size = normal
# cat=plugin.tx_jhcaptcha/recaptcha/v2; type=string; label=Site key (v2): See https://www.google.com/recaptcha/admin
siteKey =
# cat=plugin.tx_jhcaptcha/recaptcha/v2; type=string; label=Secret key (v2): See https://www.google.com/recaptcha/admin
secretKey =
}
v3 {
# cat=plugin.tx_jhcaptcha/recaptcha/v3; type=string; label=Site key (v3): See https://www.google.com/recaptcha/admin
siteKey =
# cat=plugin.tx_jhcaptcha/recaptcha/v3; type=string; label=Secret key (v3): See https://www.google.com/recaptcha/admin
secretKey =
# cat=plugin.tx_jhcaptcha/recaptcha/v3; type=string; label=Minimum Score (0.0 - 1.0)
minimumScore = 0.5
# cat=plugin.tx_jhcaptcha/recaptcha/v3; type=string; label=Action: See https://developers.google.com/recaptcha/docs/v3
action = homepage
}
}
# cat=plugin.tx_jhcaptcha/powermail; type=int; label= If this check fails - add other indication value (0 disables this check completely)
powermail.recaptchaId = 11
}
56 changes: 1 addition & 55 deletions Configuration/TypoScript/setup.txt
Original file line number Diff line number Diff line change
@@ -1,55 +1 @@
plugin.tx_jhcaptcha {
settings {
reCaptcha {
version = {$plugin.tx_jhcaptcha.settings.reCaptcha.version}
v2 {
theme = {$plugin.tx_jhcaptcha.settings.reCaptcha.v2.theme}
lang = {$plugin.tx_jhcaptcha.settings.reCaptcha.v2.lang}
size = {$plugin.tx_jhcaptcha.settings.reCaptcha.v2.size}
siteKey = {$plugin.tx_jhcaptcha.settings.reCaptcha.v2.siteKey}
secretKey = {$plugin.tx_jhcaptcha.settings.reCaptcha.v2.secretKey}
}
v3 {
siteKey = {$plugin.tx_jhcaptcha.settings.reCaptcha.v3.siteKey}
secretKey = {$plugin.tx_jhcaptcha.settings.reCaptcha.v3.secretKey}
minimumScore = {$plugin.tx_jhcaptcha.settings.reCaptcha.v3.minimumScore}
action = {$plugin.tx_jhcaptcha.settings.reCaptcha.v3.action}
}
}
}
}


# EXT:formhandler
plugin.Tx_Formhandler.settings {
markers.jh_captcha_recaptcha = FLUIDTEMPLATE
markers.jh_captcha_recaptcha.file = EXT:jh_captcha/Resources/Private/Partials/Formhandler/ReCaptcha.html
}


# EXT:powermail
plugin.tx_powermail {
view.partialRootPaths.JhCaptcha = EXT:jh_captcha/Resources/Private/Powermail/Partials
settings.setup.spamshield {
_enable = 1
methods.{$plugin.tx_jhcaptcha.settings.powermail.recaptchaId} {
_enable = 1
name = reCAPTCHA (jh_captcha)
class = Haffner\JhCaptcha\Validation\Validator\Powermail
indication = {$plugin.tx_jhcaptcha.settings.powermail.recaptchaId}
configuration.secretkey = {$plugin.tx_jhcaptcha.settings.reCaptcha.v3.secretKey}
}
}
}
[{$plugin.tx_jhcaptcha.settings.reCaptcha.version} == "2"]
plugin.tx_powermail.settings.setup.spamshield.methods.{$plugin.tx_jhcaptcha.settings.powermail.recaptchaId} {
configuration.secretkey = {$plugin.tx_jhcaptcha.settings.reCaptcha.v2.secretKey}
}
[global]


# EXT:form
plugin.tx_form {
settings.yamlConfigurations.JhCaptchaRecaptcha = EXT:jh_captcha/Configuration/Yaml/ReCaptcha/BaseSetup.yaml
view.partialRootPaths.JhCaptchaRecaptcha = EXT:jh_captcha/Resources/Private/Form/Frontend/Partials
}
@import 'EXT:jh_captcha/Configuration/TypoScript/setup.typoscript'
55 changes: 55 additions & 0 deletions Configuration/TypoScript/setup.typoscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
plugin.tx_jhcaptcha {
settings {
reCaptcha {
version = {$plugin.tx_jhcaptcha.settings.reCaptcha.version}
v2 {
theme = {$plugin.tx_jhcaptcha.settings.reCaptcha.v2.theme}
lang = {$plugin.tx_jhcaptcha.settings.reCaptcha.v2.lang}
size = {$plugin.tx_jhcaptcha.settings.reCaptcha.v2.size}
siteKey = {$plugin.tx_jhcaptcha.settings.reCaptcha.v2.siteKey}
secretKey = {$plugin.tx_jhcaptcha.settings.reCaptcha.v2.secretKey}
}
v3 {
siteKey = {$plugin.tx_jhcaptcha.settings.reCaptcha.v3.siteKey}
secretKey = {$plugin.tx_jhcaptcha.settings.reCaptcha.v3.secretKey}
minimumScore = {$plugin.tx_jhcaptcha.settings.reCaptcha.v3.minimumScore}
action = {$plugin.tx_jhcaptcha.settings.reCaptcha.v3.action}
}
}
}
}


# EXT:formhandler
plugin.Tx_Formhandler.settings {
markers.jh_captcha_recaptcha = FLUIDTEMPLATE
markers.jh_captcha_recaptcha.file = EXT:jh_captcha/Resources/Private/Partials/Formhandler/ReCaptcha.html
}


# EXT:powermail
plugin.tx_powermail {
view.partialRootPaths.JhCaptcha = EXT:jh_captcha/Resources/Private/Powermail/Partials
settings.setup.spamshield {
_enable = 1
methods.{$plugin.tx_jhcaptcha.settings.powermail.recaptchaId} {
_enable = 1
name = reCAPTCHA (jh_captcha)
class = Haffner\JhCaptcha\Validation\Validator\Powermail
indication = {$plugin.tx_jhcaptcha.settings.powermail.recaptchaId}
configuration.secretkey = {$plugin.tx_jhcaptcha.settings.reCaptcha.v3.secretKey}
}
}
}
[{$plugin.tx_jhcaptcha.settings.reCaptcha.version} == "2"]
plugin.tx_powermail.settings.setup.spamshield.methods.{$plugin.tx_jhcaptcha.settings.powermail.recaptchaId} {
configuration.secretkey = {$plugin.tx_jhcaptcha.settings.reCaptcha.v2.secretKey}
}
[global]


# EXT:form
plugin.tx_form {
settings.yamlConfigurations.JhCaptchaRecaptcha = EXT:jh_captcha/Configuration/Yaml/ReCaptcha/BaseSetup.yaml
view.partialRootPaths.JhCaptchaRecaptcha = EXT:jh_captcha/Resources/Private/Form/Frontend/Partials
}
1 change: 1 addition & 0 deletions Resources/Public/Icons/google.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"docs": "https://docs.typo3.org/typo3cms/extensions/jh_captcha/"
},
"require": {
"typo3/cms-core": "^10.4 || ^11.5"
"typo3/cms-core": "^10.4 || ^11.5 || ^12.4"
},
"replace": {
"typo3-ter/jh_captcha": "self.version"
Expand Down
6 changes: 3 additions & 3 deletions ext_localconf.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

defined('TYPO3_MODE') or die();
defined('TYPO3') or die();

############
# EXT:form #
Expand All @@ -15,8 +15,8 @@
);
$iconRegistry->registerIcon(
't3-form-icon-jhcaptcha-recaptcha',
\TYPO3\CMS\Core\Imaging\IconProvider\FontawesomeIconProvider::class,
['name' => 'google']
\TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider::class,
['source' => 'EXT:jh_captcha/Resources/Public/Icons/google.svg']
);

# EXT:powermail
Expand Down