Skip to content
Snippets Groups Projects
Commit 3b56267f authored by Stefan Galinski's avatar Stefan Galinski :video_game:
Browse files

Merge branch 'cleanups' into 'master'

Cleanups

See merge request !14
parents c6f468bd 459cd1e4
No related branches found
No related tags found
1 merge request!14Cleanups
...@@ -184,12 +184,12 @@ class MailTemplateService { ...@@ -184,12 +184,12 @@ class MailTemplateService {
$this->resourceFactory = $this->objectManager->get(ResourceFactory::class); $this->resourceFactory = $this->objectManager->get(ResourceFactory::class);
// use defaultMailFromAddress if it is provided in LocalConfiguration.php; use the sg_mail TS setting as fallback // use defaultMailFromAddress if it is provided in LocalConfiguration.php; use the sg_mail TS setting as fallback
if (filter_var($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'], FILTER_VALIDATE_EMAIL)) { if (\filter_var($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'], FILTER_VALIDATE_EMAIL)) {
$this->fromAddress = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress']; $this->fromAddress = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
} else { } else {
$this->fromAddress = $tsSettings['mail']['default']['from']; $this->fromAddress = $tsSettings['mail']['default']['from'];
if (!filter_var($tsSettings['mail']['default']['from'], FILTER_VALIDATE_EMAIL)) { if (!\filter_var($tsSettings['mail']['default']['from'], FILTER_VALIDATE_EMAIL)) {
$this->fromAddress = 'noreply@example.org'; $this->fromAddress = 'noreply@example.org';
} else { } else {
$this->fromAddress = $tsSettings['mail']['default']['from']; $this->fromAddress = $tsSettings['mail']['default']['from'];
...@@ -201,12 +201,11 @@ class MailTemplateService { ...@@ -201,12 +201,11 @@ class MailTemplateService {
} }
$this->mailMessage->setFrom($this->fromAddress, $this->fromName); $this->mailMessage->setFrom($this->fromAddress, $this->fromName);
$this->bccAddresses = GeneralUtility::trimExplode(',', $tsSettings['mail']['default']['bcc']); $this->bccAddresses = GeneralUtility::trimExplode(',', $tsSettings['mail']['default']['bcc']);
$this->ccAddresses = GeneralUtility::trimExplode(',', $tsSettings['mail']['default']['cc']); $this->ccAddresses = GeneralUtility::trimExplode(',', $tsSettings['mail']['default']['cc']);
foreach ($this->bccAddresses as $index => $email) { foreach ($this->bccAddresses as $index => $email) {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { if (!\filter_var($email, FILTER_VALIDATE_EMAIL)) {
unset($this->bccAddresses[$index]); unset($this->bccAddresses[$index]);
} }
} }
...@@ -355,7 +354,7 @@ class MailTemplateService { ...@@ -355,7 +354,7 @@ class MailTemplateService {
*/ */
public function setMarkers(array $markers): MailTemplateService { public function setMarkers(array $markers): MailTemplateService {
$this->markers = $markers; $this->markers = $markers;
foreach($markers as $key => $currentMarker) { foreach ($markers as $key => $currentMarker) {
if (!\is_array($currentMarker) || !isset($currentMarker['markerLabel'])) { if (!\is_array($currentMarker) || !isset($currentMarker['markerLabel'])) {
continue; continue;
} }
...@@ -497,9 +496,8 @@ class MailTemplateService { ...@@ -497,9 +496,8 @@ class MailTemplateService {
*/ */
public static function getDefaultTemplateMarker($translationKey, array $marker, $extensionKey = 'sg_mail'): array { public static function getDefaultTemplateMarker($translationKey, array $marker, $extensionKey = 'sg_mail'): array {
$languagePath = 'LLL:EXT:' . $extensionKey . '/Resources/Private/Language/locallang.xlf:' . $translationKey; $languagePath = 'LLL:EXT:' . $extensionKey . '/Resources/Private/Language/locallang.xlf:' . $translationKey;
// Need the key for translations // Need the key for translations
if (trim($extensionKey) === '') { if (\trim($extensionKey) === '') {
return []; return [];
} }
...@@ -568,9 +566,8 @@ class MailTemplateService { ...@@ -568,9 +566,8 @@ class MailTemplateService {
$isTemplateBlacklisted = self::isTemplateBlacklisted($this->extensionKey, $this->templateName, $siteRootId); $isTemplateBlacklisted = self::isTemplateBlacklisted($this->extensionKey, $this->templateName, $siteRootId);
if ($isTemplateBlacklisted) { if ($isTemplateBlacklisted) {
// @TODO: This needs to be changed, because if the template is blacklisted, the email can not be sent // @TODO throw error or log ?
$success = TRUE; return FALSE;
return $success;
} }
/** @var Template $template */ /** @var Template $template */
...@@ -602,8 +599,8 @@ class MailTemplateService { ...@@ -602,8 +599,8 @@ class MailTemplateService {
// only standard template file is considered since version 4.1 // only standard template file is considered since version 4.1
$defaultTemplateFile = $templatePath . 'template.html'; $defaultTemplateFile = $templatePath . 'template.html';
if (file_exists($defaultTemplateFile)) { if (\file_exists($defaultTemplateFile)) {
$defaultTemplateContent = file_get_contents($defaultTemplateFile); $defaultTemplateContent = \file_get_contents($defaultTemplateFile);
} else { } else {
// use configured default html template // use configured default html template
/** @var TypoScriptSettingsService $typoScriptSettingsService */ /** @var TypoScriptSettingsService $typoScriptSettingsService */
...@@ -613,16 +610,15 @@ class MailTemplateService { ...@@ -613,16 +610,15 @@ class MailTemplateService {
$tsSettings['mail']['defaultHtmlTemplate'] $tsSettings['mail']['defaultHtmlTemplate']
); );
if (file_exists($defaultTemplateFile)) { if (\file_exists($defaultTemplateFile)) {
$defaultTemplateContent = file_get_contents($defaultTemplateFile); $defaultTemplateContent = \file_get_contents($defaultTemplateFile);
} else { } else {
return $success; return FALSE;
} }
} }
} }
} elseif (\filter_var($template->getToAddress(), FILTER_VALIDATE_EMAIL)) {
} elseif (filter_var($template->getToAddress(), FILTER_VALIDATE_EMAIL)) { $this->setToAddresses(\trim($template->getToAddress()));
$this->setToAddresses(trim($template->getToAddress()));
} }
if ($isPreview) { if ($isPreview) {
...@@ -633,7 +629,7 @@ class MailTemplateService { ...@@ -633,7 +629,7 @@ class MailTemplateService {
foreach ($markerArray as $marker) { foreach ($markerArray as $marker) {
$markerPath = GeneralUtility::trimExplode('.', $marker['marker']); $markerPath = GeneralUtility::trimExplode('.', $marker['marker']);
$temporaryMarkerArray = []; $temporaryMarkerArray = [];
foreach (array_reverse($markerPath) as $index => $markerPathSegment) { foreach (\array_reverse($markerPath) as $index => $markerPathSegment) {
if ($index === 0) { if ($index === 0) {
if ($marker['markerLabel']) { if ($marker['markerLabel']) {
$markerPathSegment = $marker['markerLabel']; $markerPathSegment = $marker['markerLabel'];
...@@ -651,7 +647,7 @@ class MailTemplateService { ...@@ -651,7 +647,7 @@ class MailTemplateService {
} }
} }
/** @noinspection SlowArrayOperationsInLoopInspection */ /** @noinspection SlowArrayOperationsInLoopInspection */
$previewMarker = array_merge_recursive($previewMarker, $temporaryMarkerArray); $previewMarker = \array_merge_recursive($previewMarker, $temporaryMarkerArray);
} }
$this->setIgnoreMailQueue(TRUE); $this->setIgnoreMailQueue(TRUE);
$this->setMarkers($previewMarker); $this->setMarkers($previewMarker);
...@@ -693,8 +689,8 @@ class MailTemplateService { ...@@ -693,8 +689,8 @@ class MailTemplateService {
// insert <br> tags, but replace every instance of three or more successive breaks with just two. // insert <br> tags, but replace every instance of three or more successive breaks with just two.
$emailBody = $emailView->render(); $emailBody = $emailView->render();
$emailBody = nl2br($emailBody); $emailBody = \nl2br($emailBody);
$emailBody = preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br><br>', $emailBody); $emailBody = \preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br><br>', $emailBody);
$mail = $this->addMailToMailQueue( $mail = $this->addMailToMailQueue(
$this->extensionKey, $this->templateName, $subject, $emailBody, $this->priority, $this->extensionKey, $this->templateName, $subject, $emailBody, $this->priority,
...@@ -779,59 +775,58 @@ class MailTemplateService { ...@@ -779,59 +775,58 @@ class MailTemplateService {
$mailRepository = $this->objectManager->get(MailRepository::class); $mailRepository = $this->objectManager->get(MailRepository::class);
/** @var Mail $mailToSend */ /** @var Mail $mailToSend */
$mailToSend = $mailRepository->findOneByUid($uid); $mailToSend = $mailRepository->findOneByUid($uid);
if (!$mailToSend || $mailToSend->getBlacklisted()) {
return FALSE;
}
if ($mailToSend && !$mailToSend->getBlacklisted()) { $this->mailMessage->setBody($mailToSend->getMailBody(), 'text/html');
$this->mailMessage->setBody($mailToSend->getMailBody(), 'text/html'); $plaintextService = GeneralUtility::makeInstance(PlaintextService::class);
$plaintextService = GeneralUtility::makeInstance(PlaintextService::class); $plaintextBody = $plaintextService->makePlain($mailToSend->getMailBody());
$plaintextBody = $plaintextService->makePlain($mailToSend->getMailBody()); $this->mailMessage->addPart($plaintextBody, 'text/plain');
$this->mailMessage->addPart($plaintextBody, 'text/plain'); $toAddresses = \trim($mailToSend->getToAddress());
$toAddresses = trim($mailToSend->getToAddress()); $addressesArray = GeneralUtility::trimExplode(',', $toAddresses, TRUE);
$addressesArray = GeneralUtility::trimExplode(',', $toAddresses, TRUE); if (\count($addressesArray) > 1) {
if (\count($addressesArray) > 1) { $toAddresses = $addressesArray;
$toAddresses = $addressesArray; }
} $this->mailMessage->setTo($toAddresses);
$this->mailMessage->setTo($toAddresses); $this->mailMessage->setFrom($mailToSend->getFromAddress(), $mailToSend->getFromName());
$this->mailMessage->setFrom($mailToSend->getFromAddress(), $mailToSend->getFromName()); $this->mailMessage->setSubject($mailToSend->getMailSubject());
$this->mailMessage->setSubject($mailToSend->getMailSubject());
if ($mailToSend->getBccAddresses()) { if ($mailToSend->getBccAddresses()) {
$this->mailMessage->setBcc(GeneralUtility::trimExplode(',', $mailToSend->getBccAddresses())); $this->mailMessage->setBcc(GeneralUtility::trimExplode(',', $mailToSend->getBccAddresses()));
} }
if ($mailToSend->getCcAddresses()) { if ($mailToSend->getCcAddresses()) {
$this->mailMessage->setCc(GeneralUtility::trimExplode(',', $mailToSend->getCcAddresses())); $this->mailMessage->setCc(GeneralUtility::trimExplode(',', $mailToSend->getCcAddresses()));
} }
if ($mailToSend->getReplyTo()) { if ($mailToSend->getReplyTo()) {
$this->mailMessage->setReplyTo($mailToSend->getReplyTo()); $this->mailMessage->setReplyTo($mailToSend->getReplyTo());
} }
$attachments = $mailToSend->getAttachments(); $attachments = $mailToSend->getAttachments();
if ($attachments->count() > 0) { if ($attachments->count() > 0) {
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
/** /**
* @var FileReference $attachment * @var FileReference $attachment
*/ */
$file = $attachment->getOriginalResource()->getOriginalFile(); $file = $attachment->getOriginalResource()->getOriginalFile();
$this->mailMessage->attach( $this->mailMessage->attach(
\Swift_Attachment::newInstance($file->getContents(), $file->getName(), $file->getMimeType()) \Swift_Attachment::newInstance($file->getContents(), $file->getName(), $file->getMimeType())
); );
}
}
$dateTime = new DateTime();
if ((int) $mailToSend->getSendingTime() === 0) {
$mailToSend->setSendingTime($dateTime->getTimestamp());
}
$mailToSend->setLastSendingTime($dateTime->getTimestamp());
$success = $this->mailMessage->send();
if ($success) {
$mailRepository->update($mailToSend);
} else {
$this->mailMessage->getFailedRecipients();
} }
return $success;
} }
$dateTime = new DateTime();
return NULL; if ((int) $mailToSend->getSendingTime() === 0) {
$mailToSend->setSendingTime($dateTime->getTimestamp());
}
$mailToSend->setLastSendingTime($dateTime->getTimestamp());
$success = $this->mailMessage->send();
if ($success) {
$mailRepository->update($mailToSend);
} else {
$this->mailMessage->getFailedRecipients();
}
return $success;
} }
/** /**
...@@ -848,23 +843,34 @@ class MailTemplateService { ...@@ -848,23 +843,34 @@ class MailTemplateService {
$fromName = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']; $fromName = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
} }
$fromMail = \trim($template->getFromMail()); $fromMail = $this->getValidFromMail(\trim($template->getFromMail()));
if (!filter_var($fromMail, FILTER_VALIDATE_EMAIL)) { $this->setFromAddress($fromMail, $fromName);
$this->setCcAddresses($template->getCc());
$this->setBccAddresses($template->getBcc());
$this->setReplyToAddress($template->getReplyTo());
$this->setFromName($fromName);
}
/**
* Sets the fromMail property of the mailTemplateService.
* Checks validity and uses all available fallbacks
*
* @param string $fromMail
* @return string
*/
private function getValidFromMail($fromMail): string {
$fromMail = \trim($fromMail);
if (!\filter_var($fromMail, FILTER_VALIDATE_EMAIL)) {
$fromMail = $this->fromAddress; $fromMail = $this->fromAddress;
} }
if (!filter_var($fromMail, FILTER_VALIDATE_EMAIL)) { if (!\filter_var($fromMail, FILTER_VALIDATE_EMAIL)) {
$fromMail = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress']; $fromMail = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
if (!filter_var($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'], FILTER_VALIDATE_EMAIL)) { if (!\filter_var($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'], FILTER_VALIDATE_EMAIL)) {
$fromMail = 'noreply@example.com'; $fromMail = 'noreply@example.com';
} }
} }
$this->setFromAddress($fromMail, $fromName); return $fromMail;
$this->setCcAddresses($template->getCc());
$this->setBccAddresses($template->getBcc());
$this->setReplyToAddress($template->getReplyTo());
$this->setFromName($fromName);
$this->setReplyToAddress($template->getReplyTo());
} }
/** /**
...@@ -891,7 +897,7 @@ class MailTemplateService { ...@@ -891,7 +897,7 @@ class MailTemplateService {
$valueAsString = $value ? 'true' : 'false'; $valueAsString = $value ? 'true' : 'false';
$allMarker .= $key . ': ' . $valueAsString . PHP_EOL; $allMarker .= $key . ': ' . $valueAsString . PHP_EOL;
} elseif (\is_object($value)) { } elseif (\is_object($value)) {
if (method_exists($value, '__toString')) { if (\method_exists($value, '__toString')) {
$allMarker .= $key . ': ' . $value->__toString() . PHP_EOL; $allMarker .= $key . ': ' . $value->__toString() . PHP_EOL;
} }
} }
......
...@@ -99,7 +99,7 @@ class PlaintextService { ...@@ -99,7 +99,7 @@ class PlaintextService {
* @param string $content * @param string $content
* @return array * @return array
*/ */
protected function addLineBreaks($content) { protected function addLineBreaks($content): array {
$tags2LineBreaks = [ $tags2LineBreaks = [
'</p>', '</p>',
'</tr>', '</tr>',
......
...@@ -130,9 +130,9 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { ...@@ -130,9 +130,9 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
$pathAsArray = GeneralUtility::trimExplode('/', $pathToRegistrationFile); $pathAsArray = GeneralUtility::trimExplode('/', $pathToRegistrationFile);
$filename = $pathAsArray[\count($pathAsArray) - 1]; $filename = $pathAsArray[\count($pathAsArray) - 1];
$filenameWithoutHash = GeneralUtility::trimExplode('_', $filename, FALSE, 2)[1]; $filenameWithoutHash = GeneralUtility::trimExplode('_', $filename, FALSE, 2)[1];
$hash = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $filenameWithoutHash); $hash = \md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $filenameWithoutHash);
// if the filename doesn't start with the hash value, ignore it // if the filename doesn't start with the hash value, ignore it
if (strpos($filename, $hash) !== 0) { if (\strpos($filename, $hash) !== 0) {
continue; continue;
} }
...@@ -240,7 +240,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { ...@@ -240,7 +240,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
$registerFolder = GeneralUtility::getFileAbsFileName($configurationLocation); $registerFolder = GeneralUtility::getFileAbsFileName($configurationLocation);
GeneralUtility::mkdir_deep($registerFolder); GeneralUtility::mkdir_deep($registerFolder);
$hashPrefix = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateKey . '.php'); $hashPrefix = \md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateKey . '.php');
$registerFile = GeneralUtility::getFileAbsFileName( $registerFile = GeneralUtility::getFileAbsFileName(
$registerFolder . '/' . $hashPrefix . '_' . $templateKey . '.php' $registerFolder . '/' . $hashPrefix . '_' . $templateKey . '.php'
); );
...@@ -268,7 +268,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { ...@@ -268,7 +268,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
]; ];
} }
file_put_contents($registerFile, '<?php return ' . var_export($newRegisterArray, TRUE) . ';'); \file_put_contents($registerFile, '<?php return ' . \var_export($newRegisterArray, TRUE) . ';');
return $registerFile; return $registerFile;
} }
...@@ -283,11 +283,11 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { ...@@ -283,11 +283,11 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
$registerFolder = GeneralUtility::getFileAbsFileName($configurationLocation); $registerFolder = GeneralUtility::getFileAbsFileName($configurationLocation);
GeneralUtility::mkdir_deep($registerFolder); GeneralUtility::mkdir_deep($registerFolder);
$hashPrefix = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateKey . '.php'); $hashPrefix = \md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateKey . '.php');
$registerFile = GeneralUtility::getFileAbsFileName( $registerFile = GeneralUtility::getFileAbsFileName(
$registerFolder . '/' . $hashPrefix . '_' . $templateKey . '.php' $registerFolder . '/' . $hashPrefix . '_' . $templateKey . '.php'
); );
if (file_exists($registerFile)) { if (\file_exists($registerFile)) {
\unlink($registerFile); \unlink($registerFile);
} }
} }
...@@ -340,13 +340,13 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { ...@@ -340,13 +340,13 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
GeneralUtility::mkdir_deep($registerFolder); GeneralUtility::mkdir_deep($registerFolder);
if ($filePath === '') { if ($filePath === '') {
$hashPrefix = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateName . '.php'); $hashPrefix = \md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateName . '.php');
$filePath = GeneralUtility::getFileAbsFileName( $filePath = GeneralUtility::getFileAbsFileName(
$registerFolder . '/' . $hashPrefix . '_' . $templateName . '.php' $registerFolder . '/' . $hashPrefix . '_' . $templateName . '.php'
); );
} }
if (file_exists($filePath)) { if (\file_exists($filePath)) {
$success = \unlink($filePath); $success = \unlink($filePath);
} else { } else {
return FALSE; return FALSE;
...@@ -383,7 +383,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { ...@@ -383,7 +383,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
$registerFolder = GeneralUtility::getFileAbsFileName($configurationLocation); $registerFolder = GeneralUtility::getFileAbsFileName($configurationLocation);
GeneralUtility::mkdir_deep($registerFolder); GeneralUtility::mkdir_deep($registerFolder);
$hashPrefix = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateName . '.php'); $hashPrefix = \md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateName . '.php');
$filePath = GeneralUtility::getFileAbsFileName( $filePath = GeneralUtility::getFileAbsFileName(
$registerFolder . '/' . $hashPrefix . '_' . $templateName . '.php' $registerFolder . '/' . $hashPrefix . '_' . $templateName . '.php'
); );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment