Skip to content

Commit 2b0c256

Browse files
committed
MessageDataLoaderTest
1 parent cec0eb1 commit 2b0c256

File tree

11 files changed

+205
-28
lines changed

11 files changed

+205
-28
lines changed

config/parameters.yml.dist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ parameters:
100100
messaging.use_manual_text_part: '%%env(USE_MANUAL_TEXT_PART)%%'
101101
env(USE_MANUAL_TEXT_PART): 0
102102
messaging.blacklist_grace_time: '%%env(MESSAGING_BLACKLIST_GRACE_TIME)%%'
103-
env(MESSAGING_BLACKLIST_GRACE_TIME):
103+
env(MESSAGING_BLACKLIST_GRACE_TIME): '600'
104104
messaging.google_sender_id: '%%env(GOOGLE_SENDERID)%%'
105105
env(GOOGLE_SENDERID): ''
106106
messaging.use_amazon_ses: '%%env(USE_AMAZONSES)%%'
@@ -114,11 +114,11 @@ parameters:
114114
messaging.external_image_timeout: '%%env(EXTERNALIMAGE_TIMEOUT)%%'
115115
env(EXTERNALIMAGE_TIMEOUT): 30
116116
messaging.external_image_max_size: '%%env(EXTERNALIMAGE_MAXSIZE)%%'
117-
env(EXTERNALIMAGE_MAXSIZE): 2048
117+
env(EXTERNALIMAGE_MAXSIZE): 204800
118118

119119
phplist.upload_images_dir: '%%env(PHPLIST_UPLOADIMAGES_DIR)%%'
120120
env(PHPLIST_UPLOADIMAGES_DIR): 'images'
121121
phplist.editor_images_dir: '%%env(FCKIMAGES_DIR)%%'
122122
env(FCKIMAGES_DIR): 'uploadimages'
123123
phplist.public_schema: '%%env(PUBLIC_SCHEMA)%%'
124-
env(PUBLIC_SCHEMA): 'http'
124+
env(PUBLIC_SCHEMA): 'https'

src/Domain/Common/RemotePageFetcher.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ public function __invoke(string $url, array $userData): string
3535

3636
foreach ($userData as $key => $val) {
3737
if ($key !== 'password') {
38-
$url = utf8_encode(str_ireplace("[$key]", urlencode($val), utf8_decode($url)));
38+
$url = mb_convert_encoding(
39+
str_ireplace(
40+
"[$key]",
41+
urlencode($val),
42+
mb_convert_encoding($url, 'ISO-8859-1', 'UTF-8')
43+
),
44+
'ISO-8859-1',
45+
'UTF-8'
46+
);
3947
}
4048
}
4149

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Domain\Messaging\Exception;
6+
7+
use RuntimeException;
8+
9+
class MessageCacheMissingException extends RuntimeException
10+
{
11+
public function __construct()
12+
{
13+
parent::__construct('Message cache is missing or expired');
14+
}
15+
}

src/Domain/Messaging/MessageHandler/CampaignProcessorMessageHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
1010
use Doctrine\ORM\EntityManagerInterface;
1111
use PhpList\Core\Domain\Configuration\Service\UserPersonalizer;
12+
use PhpList\Core\Domain\Messaging\Exception\MessageCacheMissingException;
1213
use PhpList\Core\Domain\Messaging\Exception\MessageSizeLimitExceededException;
1314
use PhpList\Core\Domain\Messaging\Message\CampaignProcessorMessage;
1415
use PhpList\Core\Domain\Messaging\Message\SyncCampaignProcessorMessage;
@@ -200,6 +201,9 @@ public function __invoke(CampaignProcessorMessage|SyncCampaignProcessorMessage $
200201
}
201202

202203
$messagePrecacheDto = $this->cache->get($cacheKey);
204+
if ($messagePrecacheDto === null) {
205+
throw new MessageCacheMissingException();
206+
}
203207
$this->handleEmailSending($campaign, $subscriber, $userMessage, $messagePrecacheDto);
204208
}
205209

src/Domain/Messaging/Service/Builder/EmailBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository;
1313
use PhpList\Core\Domain\Subscription\Repository\UserBlacklistRepository;
1414
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberHistoryManager;
15+
use Psr\Log\LoggerInterface;
1516
use Symfony\Component\Mime\Address;
1617
use Symfony\Component\Mime\Email;
1718

@@ -25,6 +26,7 @@ public function __construct(
2526
private readonly SubscriberRepository $subscriberRepository,
2627
private readonly SystemMailConstructor $systemMailConstructor,
2728
private readonly TemplateImageEmbedder $templateImageEmbedder,
29+
private readonly LoggerInterface $logger,
2830
private readonly string $googleSenderId,
2931
private readonly bool $useAmazonSes,
3032
private readonly bool $usePrecedenceHeader,
@@ -64,6 +66,11 @@ public function buildPhplistEmail(
6466
if (!$skipBlacklistCheck && $this->blacklistRepository->isEmailBlacklisted($to)) {
6567
$this->eventLogManager->log('', sprintf('Error, %s is blacklisted, not sending', $to));
6668
$subscriber = $this->subscriberRepository->findOneByEmail($to);
69+
if (!$subscriber) {
70+
$this->logger->error('Error: subscriber not found', ['email' => $to]);
71+
72+
return null;
73+
}
6774
$subscriber->setBlacklisted(true);
6875

6976
$this->subscriberHistoryManager->addHistory(

src/Domain/Messaging/Service/MessageDataLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function __invoke(Message $message): array
7676
$storedMessageData = $this->messageDataRepository->getForMessage($message->getId());
7777
foreach ($storedMessageData as $storedMessageDatum) {
7878
if (str_starts_with($storedMessageDatum->getData(), 'SER:')) {
79-
$unserialized = unserialize(substr($storedMessageDatum->getData(), 4));
79+
$unserialized = unserialize(substr($storedMessageDatum->getData(), 4), ['allowed_classes' => false]);
8080
array_walk_recursive($unserialized, function (&$val) {
8181
$val = stripslashes($val);
8282
});
@@ -123,7 +123,7 @@ public function __invoke(Message $message): array
123123
$messageData['fromemail'] = str_replace('>', '', $messageData['fromemail']);
124124
// make sure there are no quotes around the name
125125
$messageData['fromname'] = str_replace('"', '', ltrim(rtrim($messageData['fromname'])));
126-
} elseif (strpos($messageData['fromfield'], ' ')) {
126+
} elseif (str_contains($messageData['fromfield'], ' ')) {
127127
// if there is a space, we need to add the email
128128
$messageData['fromname'] = $messageData['fromfield'];
129129
$messageData['fromemail'] = $defaultFrom;

src/Domain/Messaging/Service/MessageProcessingPreparator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public function processMessageLinks(
7777
$cachedMessageDto->content = $this->replaceLinks($savedLinks, $cachedMessageDto->content);
7878
}
7979

80-
if ($cachedMessageDto->footer) {
81-
$cachedMessageDto->footer = $this->replaceLinks($savedLinks, $cachedMessageDto->footer);
80+
if ($cachedMessageDto->htmlFooter) {
81+
$cachedMessageDto->htmlFooter = $this->replaceLinks($savedLinks, $cachedMessageDto->htmlFooter);
8282
}
8383

8484
return $cachedMessageDto;

src/Domain/Messaging/Service/RateLimitedCampaignMailer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function composeEmail(
4040
->subject($processedContent->subject)
4141
->text($processedContent->textContent)
4242
// todo: check htmlFooterit should be html of textContent
43-
->html($processedContent->htmlFooter);
43+
->html($processedContent->content);
4444
}
4545

4646
/**

src/Domain/Messaging/Service/SystemMailConstructor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function __invoke($message, $subject = ''): array
6868
} else {
6969
$phpListPowered = $this->configProvider->getValue(ConfigOption::PoweredByText);
7070
}
71-
if (strpos($htmlContent, '[SIGNATURE]')) {
71+
if (str_contains($htmlContent, '[SIGNATURE]')) {
7272
$htmlContent = str_replace('[SIGNATURE]', $phpListPowered, $htmlContent);
7373
} elseif (strpos($htmlContent, '</body>')) {
7474
$htmlContent = str_replace('</body>', $phpListPowered.'</body>', $htmlContent);
@@ -80,7 +80,7 @@ public function __invoke($message, $subject = ''): array
8080
$textContent = str_replace('[SUBJECT]', $subject, $textContent);
8181
$textContent = str_replace('[FOOTER]', '', $textContent);
8282
$phpListPowered = trim(($this->html2Text)($this->configProvider->getValue(ConfigOption::PoweredByText)));
83-
if (strpos($textContent, '[SIGNATURE]')) {
83+
if (str_contains($textContent, '[SIGNATURE]')) {
8484
$textContent = str_replace('[SIGNATURE]', $phpListPowered, $textContent);
8585
} else {
8686
$textContent .= "\n\n" . $phpListPowered;

src/Domain/Messaging/Service/TemplateImageEmbedder.php

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ public function __invoke(string $html, int $messageId): string
112112
sort($htmlImages);
113113
for ($i = 0; $i < count($htmlImages); ++$i) {
114114
if ($image = $this->getTemplateImage($templateId, $htmlImages[$i])) {
115-
$content_type = $this->mimeMap[strtolower(substr($htmlImages[$i], strrpos($htmlImages[$i], '.') + 1))];
115+
$content_type = $this->mimeMap[strtolower(
116+
substr($htmlImages[$i], strrpos($htmlImages[$i], '.') + 1)
117+
)];
116118
$cid = $this->addHtmlImage($image->getData(), basename($htmlImages[$i]), $content_type);
117119
if (!empty($cid)) {
118120
$html = str_replace(basename($htmlImages[$i]), "cid:$cid", $html);
@@ -146,6 +148,7 @@ private function getFilesystemImage(string $filename): string
146148
{
147149
//# get the image contents
148150
$localFile = basename(urldecode($filename));
151+
$pageRoot = $this->configProvider->getValue(ConfigOption::PageRoot);
149152
if ($this->uploadImagesDir) {
150153
$imageRoot = $this->configProvider->getValue(ConfigOption::UploadImageRoot);
151154
if (is_file($imageRoot.$localFile)) {
@@ -161,15 +164,17 @@ private function getFilesystemImage(string $filename): string
161164
);
162165

163166
return base64_encode(file_get_contents($this->documentRoot.$localFile));
164-
} elseif (is_file($this->documentRoot.'/'.$this->uploadImagesDir.'/image/'.$localFile)) {
167+
} elseif (is_file($this->documentRoot . '/' . $this->uploadImagesDir . '/image/' . $localFile)) {
165168
$this->configManager->create(
166169
ConfigOption::UploadImageRoot->value,
167-
$this->documentRoot.'/'.$this->uploadImagesDir.'/image/',
170+
$this->documentRoot . '/' . $this->uploadImagesDir . '/image/',
168171
false,
169172
'string',
170173
);
171174

172-
return base64_encode(file_get_contents($this->documentRoot.'/'.$this->uploadImagesDir.'/image/'.$localFile));
175+
return base64_encode(
176+
file_get_contents($this->documentRoot . '/' . $this->uploadImagesDir . '/image/' . $localFile)
177+
);
173178
} elseif (is_file($this->documentRoot.'/'.$this->uploadImagesDir.'/'.$localFile)) {
174179
$this->configManager->create(
175180
ConfigOption::UploadImageRoot->value,
@@ -178,20 +183,26 @@ private function getFilesystemImage(string $filename): string
178183
'string',
179184
);
180185

181-
return base64_encode(file_get_contents($this->documentRoot.'/'.$this->uploadImagesDir.'/'.$localFile));
186+
return base64_encode(
187+
file_get_contents($this->documentRoot . '/' . $this->uploadImagesDir . '/' . $localFile)
188+
);
182189
}
183190
}
184-
} elseif (is_file($this->documentRoot.$this->configProvider->getValue(ConfigOption::PageRoot).'/'.$this->editorImagesDir.'/'.$localFile)) {
191+
} elseif (is_file($this->documentRoot . $pageRoot . '/' . $this->editorImagesDir . '/' . $localFile)) {
185192
$elements = parse_url($filename);
186193
$localFile = basename($elements['path']);
187194

188-
return base64_encode(file_get_contents($this->documentRoot.$this->configProvider->getValue(ConfigOption::PageRoot).'/'.$this->editorImagesDir.'/'.$localFile));
189-
} elseif (is_file($this->documentRoot.$this->configProvider->getValue(ConfigOption::PageRoot).'/'.$this->editorImagesDir.'/image/'.$localFile)) {
190-
return base64_encode(file_get_contents($this->documentRoot.$this->configProvider->getValue(ConfigOption::PageRoot).'/'.$this->editorImagesDir.'/image/'.$localFile));
191-
} elseif (is_file('../'.$this->editorImagesDir.'/'.$localFile)) {
192-
return base64_encode(file_get_contents('../'.$this->editorImagesDir.'/'.$localFile));
193-
} elseif (is_file('../'.$this->editorImagesDir.'/image/'.$localFile)) {
194-
return base64_encode(file_get_contents('../'.$this->editorImagesDir.'/image/'.$localFile));
195+
return base64_encode(
196+
file_get_contents($this->documentRoot . $pageRoot . '/' . $this->editorImagesDir . '/' . $localFile)
197+
);
198+
} elseif (is_file($this->documentRoot . $pageRoot . '/' . $this->editorImagesDir . '/image/' . $localFile)) {
199+
return base64_encode(
200+
file_get_contents($this->documentRoot . $pageRoot . '/' . $this->editorImagesDir . '/image/' . $localFile)
201+
);
202+
} elseif (is_file('../' . $this->editorImagesDir . '/' . $localFile)) {
203+
return base64_encode(file_get_contents('../' . $this->editorImagesDir. '/' . $localFile));
204+
} elseif (is_file('../' . $this->editorImagesDir . '/image/' . $localFile)) {
205+
return base64_encode(file_get_contents('../' . $this->editorImagesDir . '/image/' . $localFile));
195206
}
196207

197208
return '';
@@ -241,6 +252,7 @@ private function filesystemImageExists($filename): bool
241252
{
242253
//# find the image referenced and see if it's on the server
243254
$imageRoot = $this->configProvider->getValue(ConfigOption::UploadImageRoot);
255+
$pageRoot = $this->configProvider->getValue(ConfigOption::UploadImageRoot);
244256

245257
$elements = parse_url($filename);
246258
$localFile = basename($elements['path']);
@@ -254,11 +266,11 @@ private function filesystemImageExists($filename): bool
254266
|| is_file($imageRoot.'/'.$localFile);
255267
} else {
256268
return
257-
is_file($this->documentRoot.$this->configProvider->getValue(ConfigOption::PageRoot).'/'.$this->editorImagesDir.'/image/'.$localFile)
258-
|| is_file($this->documentRoot.$this->configProvider->getValue(ConfigOption::PageRoot).'/'.$this->editorImagesDir.'/'.$localFile)
269+
is_file($this->documentRoot . $pageRoot . '/' . $this->editorImagesDir . '/image/' . $localFile)
270+
|| is_file($pageRoot . '/' . $this->editorImagesDir . '/' . $localFile)
259271
//# commandline
260-
|| is_file('../'.$this->editorImagesDir.'/image/'.$localFile)
261-
|| is_file('../'.$this->editorImagesDir.'/'.$localFile);
272+
|| is_file('../' . $this->editorImagesDir . '/image/' . $localFile)
273+
|| is_file('../' . $this->editorImagesDir . '/' . $localFile);
262274
}
263275
}
264276

0 commit comments

Comments
 (0)