Implement 'stop forum spam' api to prevent form spam
This commit is contained in:
+2
-1
@@ -79,7 +79,8 @@
|
|||||||
"require-dev": {
|
"require-dev": {
|
||||||
"deployer/deployer": "^6.8",
|
"deployer/deployer": "^6.8",
|
||||||
"deployer/recipes": "^6.2",
|
"deployer/recipes": "^6.2",
|
||||||
"ichhabrecht/filefill": "^3.3"
|
"ichhabrecht/filefill": "^3.3",
|
||||||
|
"typo3/testing-framework": "^6.16"
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"sort-packages": true,
|
"sort-packages": true,
|
||||||
|
|||||||
Generated
+1799
-1
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\Domain\Model\Inquiry;
|
||||||
use EP\EpEvents\Service\ConfigurationService;
|
use EP\EpEvents\Service\ConfigurationService;
|
||||||
use EP\EpEvents\Service\EmailService;
|
use EP\EpEvents\Service\EmailService;
|
||||||
|
use EP\EpEvents\Service\SpamProtectService;
|
||||||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
|
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
|
||||||
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
|
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
|
||||||
|
|
||||||
@@ -46,10 +47,20 @@ class AjaxFormController extends ActionController
|
|||||||
*/
|
*/
|
||||||
protected $configurationService;
|
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->emailService = $emailService;
|
||||||
$this->configurationService = $configurationService;
|
$this->configurationService = $configurationService;
|
||||||
|
$this->spamProtectService = $spamProtectService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -59,15 +70,17 @@ class AjaxFormController extends ActionController
|
|||||||
*/
|
*/
|
||||||
public function processInquiryFormAction(Inquiry $inquiry)
|
public function processInquiryFormAction(Inquiry $inquiry)
|
||||||
{
|
{
|
||||||
$response['status'] = 'ok';
|
if (false === $this->spamProtectService->isSpamEmail($inquiry->getEmail())) {
|
||||||
$this->emailService->send([
|
$this->emailService->send([
|
||||||
'toEmail' => $this->settings['inquiryToEmail'],
|
'toEmail' => $this->settings['inquiryToEmail'],
|
||||||
'toName' => $this->settings['inquiryToName'],
|
'toName' => $this->settings['inquiryToName'],
|
||||||
'subject' => 'Anfrage E&P Events',
|
'subject' => 'Anfrage E&P Events',
|
||||||
'templateName' => 'Inquiry',
|
'templateName' => 'Inquiry',
|
||||||
'variables' => [ 'inquiry' => $inquiry ]
|
'variables' => [ 'inquiry' => $inquiry ]
|
||||||
]);
|
]);
|
||||||
return json_encode($response);
|
}
|
||||||
|
|
||||||
|
return json_encode(['status' => 'ok']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -77,25 +90,31 @@ class AjaxFormController extends ActionController
|
|||||||
*/
|
*/
|
||||||
public function processConfiguratorFormAction(Inquiry $inquiry)
|
public function processConfiguratorFormAction(Inquiry $inquiry)
|
||||||
{
|
{
|
||||||
$response['status'] = 'ok';
|
if (false === $this->spamProtectService->isSpamEmail($inquiry->getEmail())) {
|
||||||
$this->emailService->send([
|
$this->emailService->send([
|
||||||
'toEmail' => $this->settings['inquiryToEmail'],
|
'toEmail' => $this->settings['inquiryToEmail'],
|
||||||
'toName' => $this->settings['inquiryToName'],
|
'toName' => $this->settings['inquiryToName'],
|
||||||
'subject' => 'Anfrage E&P Events',
|
'subject' => 'Anfrage E&P Events',
|
||||||
'templateName' => 'Configurator',
|
'templateName' => 'Configurator',
|
||||||
'variables' => [
|
'variables' => [
|
||||||
'inquiry' => $inquiry,
|
'inquiry' => $inquiry,
|
||||||
'config' => $this->configurationService->getConfiguratorConfig(),
|
'config' => $this->configurationService->getConfiguratorConfig(),
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
return json_encode($response);
|
}
|
||||||
|
|
||||||
|
return json_encode(['status' => 'ok']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function errorAction() {
|
protected function errorAction() {
|
||||||
$formErrors = [];
|
$response = [
|
||||||
|
'status' => 'validation',
|
||||||
|
'errors' => [],
|
||||||
|
];
|
||||||
|
|
||||||
if ($this->arguments->validate()->hasErrors()) {
|
if ($this->arguments->validate()->hasErrors()) {
|
||||||
foreach ($this->arguments->validate()->getFlattenedErrors() as $key => $errors)
|
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());
|
$translationKey = sprintf('tx_epevents.message.%s.%s', $key, $error->getCode());
|
||||||
$errorsRaw[] = LocalizationUtility::translate($translationKey, 'ep_events');
|
$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);
|
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,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user