-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f936d75
commit decba35
Showing
9 changed files
with
206 additions
and
8 deletions.
There are no files selected for viewing
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\SentMails; | ||
|
||
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
class Configuration | ||
{ | ||
|
||
/** @var string[] */ | ||
public array $rejectBySearchTerms = []; | ||
|
||
public int $mailInformationMaxTime = 60; | ||
|
||
public string $mailAPIUsername = ''; | ||
public string $mailAPIPassword = ''; | ||
|
||
public function __construct() | ||
{ | ||
try { | ||
$settings = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('sent_mails'); | ||
|
||
$this->rejectBySearchTerms = GeneralUtility::trimExplode('|', $settings['rejectBySearchTerms'] ?? '', true); | ||
$this->mailAPIUsername = $settings['mailAPIUsername'] ?? ''; | ||
$this->mailAPIPassword = $settings['mailAPIPassword'] ?? ''; | ||
} catch (\Exception $e) { | ||
// do nothing | ||
} | ||
} | ||
|
||
} |
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,30 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace StudioMitte\SentMails\EventListener; | ||
|
||
|
||
use StudioMitte\SentMails\Configuration; | ||
use Symfony\Component\Mailer\Event\MessageEvent; | ||
|
||
class MailMessageListener | ||
{ | ||
|
||
public function __construct( | ||
protected Configuration $configuration) | ||
{ | ||
} | ||
|
||
public function __invoke(MessageEvent $event): void | ||
{ | ||
$message = $event->getMessage(); | ||
|
||
$messageAsString = $message->toString(); | ||
foreach ($this->configuration->rejectBySearchTerms as $searchTerm) { | ||
if (preg_match(sprintf('/%s/i', $searchTerm), $messageAsString)) { | ||
$event->reject(); | ||
return; | ||
} | ||
} | ||
} | ||
} |
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,77 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace StudioMitte\SentMails\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use StudioMitte\SentMails\Configuration; | ||
use StudioMitte\SentMails\Repository\MailRepository; | ||
use TYPO3\CMS\Core\Http\JsonResponse; | ||
|
||
class MailInformation implements MiddlewareInterface | ||
{ | ||
|
||
public function __construct( | ||
protected readonly MailRepository $mailRepository, | ||
protected readonly Configuration $configuration | ||
) | ||
{ | ||
|
||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
if (!$this->isValidRequest($request)) { | ||
return $handler->handle($request); | ||
} | ||
|
||
$response = new JsonResponse(); | ||
if (!$this->validateBasicAuth($request)) { | ||
$response = $response->withAddedHeader('WWW-Authenticate', 'Basic realm="Access check"'); | ||
} | ||
|
||
try { | ||
$searchParam = $this->getSearchParam($request); | ||
$response->setPayload($this->mailRepository->getMailsBySearch($searchParam)); | ||
} catch (\Exception $e) { | ||
$response = $response->withStatus(400); | ||
$response->setPayload(['error' => $e->getMessage()]); | ||
} | ||
return $response; | ||
} | ||
|
||
protected function getSearchParam(ServerRequestInterface $request): string | ||
{ | ||
$searchParams = trim($request->getQueryParams()['search'] ?? ''); | ||
if (mb_strlen($searchParams) < 10) { | ||
throw new \InvalidArgumentException('Search parameter must be at least 10 characters long', 1627980733); | ||
} | ||
return $searchParams; | ||
} | ||
|
||
protected function isValidRequest(ServerRequestInterface $request): bool | ||
{ | ||
if (!$request->getUri()) { | ||
return false; | ||
} | ||
$uri = $request->getUri(); | ||
|
||
return $uri->getPath() === '/api/mailinformation'; | ||
} | ||
|
||
protected function validateBasicAuth(ServerRequestInterface $request): bool | ||
{ | ||
$username = $request->getServerParams()['PHP_AUTH_USER'] ?? false; | ||
$password = $request->getServerParams()['PHP_AUTH_PW'] ?? false; | ||
|
||
return | ||
$this->configuration->mailAPIUsername && | ||
$this->configuration->mailAPIPassword && | ||
$username === $this->configuration->mailAPIUsername && | ||
$password === $this->configuration->mailAPIPassword; | ||
} | ||
|
||
} |
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,14 @@ | ||
<?php | ||
|
||
return [ | ||
'frontend' => [ | ||
'typo3/theme/logger' => [ | ||
'target' => \StudioMitte\SentMails\Middleware\MailInformation::class, | ||
'before' => [ | ||
'typo3/cms-frontend/timetracker', | ||
], | ||
'after' => [ | ||
], | ||
], | ||
], | ||
]; |
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
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,8 @@ | ||
# cat=Feature; type=string; label=Search terms leading to rejection:List of search terms (separated by ###) which lead to rejecting the mail sending process | ||
rejectBySearchTerms = | ||
|
||
# cat=Feature; type=string; label=Mail API Username:Provide the username for the mail API | ||
mailAPIUsername = | ||
|
||
# cat=Feature; type=string; label=Mail API Password:Provide the password for the mail API | ||
mailAPIPassword = |