Implement 'stop forum spam' api to prevent form spam

This commit is contained in:
Björn Fromme
2022-08-06 22:13:49 +02:00
parent 0cc08e0a30
commit 7cc7fa4b69
5 changed files with 1916 additions and 30 deletions
+2 -1
View File
@@ -79,7 +79,8 @@
"require-dev": {
"deployer/deployer": "^6.8",
"deployer/recipes": "^6.2",
"ichhabrecht/filefill": "^3.3"
"ichhabrecht/filefill": "^3.3",
"typo3/testing-framework": "^6.16"
},
"config": {
"sort-packages": true,
Generated
+1799 -1
View File
File diff suppressed because it is too large Load Diff
@@ -30,6 +30,7 @@ namespace EP\EpEvents\Controller;
use EP\EpEvents\Domain\Model\Inquiry;
use EP\EpEvents\Service\ConfigurationService;
use EP\EpEvents\Service\EmailService;
use EP\EpEvents\Service\SpamProtectService;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
@@ -46,10 +47,20 @@ class AjaxFormController extends ActionController
*/
protected $configurationService;
public function __construct(EmailService $emailService, ConfigurationService $configurationService)
{
/**
* @var SpamProtectService
*/
protected $spamProtectService;
public function __construct
(
EmailService $emailService,
ConfigurationService $configurationService,
SpamProtectService $spamProtectService
) {
$this->emailService = $emailService;
$this->configurationService = $configurationService;
$this->spamProtectService = $spamProtectService;
}
/**
@@ -59,15 +70,17 @@ class AjaxFormController extends ActionController
*/
public function processInquiryFormAction(Inquiry $inquiry)
{
$response['status'] = 'ok';
$this->emailService->send([
'toEmail' => $this->settings['inquiryToEmail'],
'toName' => $this->settings['inquiryToName'],
'subject' => 'Anfrage E&P Events',
'templateName' => 'Inquiry',
'variables' => [ 'inquiry' => $inquiry ]
]);
return json_encode($response);
if (false === $this->spamProtectService->isSpamEmail($inquiry->getEmail())) {
$this->emailService->send([
'toEmail' => $this->settings['inquiryToEmail'],
'toName' => $this->settings['inquiryToName'],
'subject' => 'Anfrage E&P Events',
'templateName' => 'Inquiry',
'variables' => [ 'inquiry' => $inquiry ]
]);
}
return json_encode(['status' => 'ok']);
}
/**
@@ -77,25 +90,31 @@ class AjaxFormController extends ActionController
*/
public function processConfiguratorFormAction(Inquiry $inquiry)
{
$response['status'] = 'ok';
$this->emailService->send([
'toEmail' => $this->settings['inquiryToEmail'],
'toName' => $this->settings['inquiryToName'],
'subject' => 'Anfrage E&P Events',
'templateName' => 'Configurator',
'variables' => [
'inquiry' => $inquiry,
'config' => $this->configurationService->getConfiguratorConfig(),
]
]);
return json_encode($response);
if (false === $this->spamProtectService->isSpamEmail($inquiry->getEmail())) {
$this->emailService->send([
'toEmail' => $this->settings['inquiryToEmail'],
'toName' => $this->settings['inquiryToName'],
'subject' => 'Anfrage E&P Events',
'templateName' => 'Configurator',
'variables' => [
'inquiry' => $inquiry,
'config' => $this->configurationService->getConfiguratorConfig(),
]
]);
}
return json_encode(['status' => 'ok']);
}
/**
* @return string
*/
protected function errorAction() {
$formErrors = [];
$response = [
'status' => 'validation',
'errors' => [],
];
if ($this->arguments->validate()->hasErrors()) {
foreach ($this->arguments->validate()->getFlattenedErrors() as $key => $errors)
{
@@ -106,13 +125,10 @@ class AjaxFormController extends ActionController
$translationKey = sprintf('tx_epevents.message.%s.%s', $key, $error->getCode());
$errorsRaw[] = LocalizationUtility::translate($translationKey, 'ep_events');
}
$formErrors[$key] = implode(', ', $errorsRaw);
$response['errors'][$key] = implode(', ', $errorsRaw);
}
}
$response['status'] = 'validation';
$response['errors'] = $formErrors;
return json_encode($response);
}
@@ -0,0 +1,37 @@
<?php
namespace EP\EpEvents\Service;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use TYPO3\CMS\Core\SingletonInterface;
class SpamProtectService implements SingletonInterface
{
public function isSpamEmail(string $email): bool
{
$client = HttpClient::create();
try {
$response = $client->request('GET', 'https://api.stopforumspam.org/api', [
'query' => [
'emailhash' => md5($email),
]
]);
$result = simplexml_load_string($response->getContent());
$success = (bool)$result->attributes()['success'];
if (false === $success) {
return false;
}
$appears = (string)$result->appears === 'yes';
if (true === $appears) {
return true;
}
} catch (TransportExceptionInterface $e) {
}
return false;
}
}
@@ -0,0 +1,34 @@
<?php
namespace EP\EpEvents\Tests\Unit\Service;
use EP\EpEvents\Service\SpamProtectService;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
class SpamProtectServiceTest extends UnitTestCase
{
/**
* @test
* @dataProvider spamEmailDataProvider
*/
public function canConnectToApi(string $email, bool $isSpam)
{
$service = new SpamProtectService();
$result = $service->isSpamEmail($email);
$this->assertEquals($isSpam, $result);
}
public function spamEmailDataProvider()
{
return [
'valid' => [
'email' => '[email protected]',
'isSpam' => false,
],
'invalid' => [
'email' => '[email protected]',
'isSpam' => true,
],
];
}
}