-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathMessagePrecacheService.php
More file actions
240 lines (204 loc) · 11.1 KB
/
MessagePrecacheService.php
File metadata and controls
240 lines (204 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
declare(strict_types=1);
namespace PhpList\Core\Domain\Messaging\Service;
use PhpList\Core\Domain\Common\Html2Text;
use PhpList\Core\Domain\Common\RemotePageFetcher;
use PhpList\Core\Domain\Common\TextParser;
use PhpList\Core\Domain\Configuration\Model\ConfigOption;
use PhpList\Core\Domain\Configuration\Service\Manager\EventLogManager;
use PhpList\Core\Domain\Configuration\Service\Provider\ConfigProvider;
use PhpList\Core\Domain\Identity\Repository\AdminAttributeDefinitionRepository;
use PhpList\Core\Domain\Identity\Repository\AdministratorRepository;
use PhpList\Core\Domain\Messaging\Model\Dto\MessagePrecacheDto;
use PhpList\Core\Domain\Messaging\Model\Message;
use PhpList\Core\Domain\Messaging\Repository\TemplateRepository;
use PhpList\Core\Domain\Messaging\Service\Manager\TemplateImageManager;
use Psr\SimpleCache\CacheInterface;
/** @SuppressWarnings("ExcessiveParameterList") */
class MessagePrecacheService
{
private const REPLACE_KEYS = ['subject', 'id', 'fromname', 'fromemail'];
public function __construct(
private readonly CacheInterface $cache,
private readonly ConfigProvider $configProvider,
private readonly Html2Text $html2Text,
private readonly TextParser $textParser,
private readonly TemplateRepository $templateRepository,
private readonly RemotePageFetcher $remotePageFetcher,
private readonly EventLogManager $eventLogManager,
private readonly AdminAttributeDefinitionRepository $adminAttreDefRepository,
private readonly AdministratorRepository $adminRepository,
private readonly TemplateImageManager $templateImageManager,
private readonly bool $useManualTextPart,
private readonly string $uploadImageDir,
private readonly string $publicSchema,
) {
}
/**
* Retrieve the base (unpersonalized) message content for a campaign from cache,
* or cache it on first access. Handle [URL:] token fetch and basic placeholder replacements.
*/
public function precacheMessage(Message $campaign, array $loadedMessageData, ?bool $forwardContent = false): bool
{
$cacheKey = sprintf('messaging.message.base.%d.%d', $campaign->getId(), (int) $forwardContent);
$cached = $this->cache->get($cacheKey);
if ($cached !== null) {
return true;
}
$domain = $this->configProvider->getValue(ConfigOption::Domain);
$messagePrecacheDto = new MessagePrecacheDto();
$this->populateReplyTo($messagePrecacheDto, $loadedMessageData, $domain);
$this->populateBasicFields($messagePrecacheDto, $loadedMessageData, (bool) $forwardContent);
$messagePrecacheDto->htmlFormatted = $this->isHtml($messagePrecacheDto->content);
$messagePrecacheDto->sendFormat = $loadedMessageData['sendformat'];
$this->applyTemplate($messagePrecacheDto, $loadedMessageData);
//# if we are sending a URL that contains user attributes, we cannot pre-parse the message here
//# but that has quite some impact on speed. So check if that's the case and apply
$messagePrecacheDto->userSpecificUrl = (bool) preg_match('/\[.+\]/', $loadedMessageData['sendurl']);
if (!$this->applyRemoteContentIfPresent($messagePrecacheDto, $loadedMessageData)) {
return false;
}
$messagePrecacheDto->googleTrack = (bool) $loadedMessageData['google_track'];
$this->applyBasicReplacements($messagePrecacheDto, $loadedMessageData);
$this->populateAdminAttributes($messagePrecacheDto, $campaign);
$baseurl = $this->configProvider->getValue(ConfigOption::Website);
if ($this->uploadImageDir) {
//# escape subdirectories, otherwise this renders empty
$dir = str_replace('/', '\/', $this->uploadImageDir);
$messagePrecacheDto->content = preg_replace(
'/<img(.*)src="\/' . $dir . '(.*)>/iU',
'<img\\1src="' . $this->publicSchema . '://' . $baseurl . '/' . $this->uploadImageDir . '\\2>',
$messagePrecacheDto->content
);
}
$messagePrecacheDto->content = $this->templateImageManager->parseLogoPlaceholders($messagePrecacheDto->content);
$messagePrecacheDto->template = $this->templateImageManager
->parseLogoPlaceholders($messagePrecacheDto->template);
$messagePrecacheDto->htmlFooter = $this->templateImageManager
->parseLogoPlaceholders($messagePrecacheDto->htmlFooter);
$this->cache->set($cacheKey, $messagePrecacheDto);
return true;
}
private function isHtml(string $content): bool
{
return strip_tags($content) !== $content;
}
private function populateReplyTo(MessagePrecacheDto $messagePrecacheDto, $loadedMessageData, ?string $domain): void
{
// parse the reply-to field into its components - email and name
if (preg_match('/([^ ]+@[^ ]+)/', $loadedMessageData['replyto'], $regs)) {
// if there is an email in the from, rewrite it as "name <email>"
$loadedMessageData['replyto'] = str_replace($regs[0], '', $loadedMessageData['replyto']);
$replyToEmail = $regs[0];
// if the email has < and > take them out here
$replyToEmail = str_replace('<', '', $replyToEmail);
$replyToEmail = str_replace('>', '', $replyToEmail);
$messagePrecacheDto->replyToEmail = $replyToEmail;
// make sure there are no quotes around the name
$messagePrecacheDto->replyToName = str_replace('"', '', ltrim(rtrim($loadedMessageData['replyto'])));
} elseif (str_contains($loadedMessageData['replyto'], ' ')) {
// if there is a space, we need to add the email
$messagePrecacheDto->replyToName = $loadedMessageData['replyto'];
$messagePrecacheDto->replyToEmail = 'listmaster@' . $domain;
} elseif (!empty($loadedMessageData['replyto'])) {
$messagePrecacheDto->replyToEmail = $loadedMessageData['replyto'] . '@' . $domain;
//# makes more sense not to add the domain to the word, but the help says it does
//# so let's keep it for now
$messagePrecacheDto->replyToName = $loadedMessageData['replyto'] . '@' . $domain;
}
}
private function populateBasicFields(
MessagePrecacheDto $messagePrecacheDto,
array $loadedMessageData,
bool $forwardContent,
): void {
$messagePrecacheDto->fromName = $loadedMessageData['fromname'];
$messagePrecacheDto->fromEmail = $loadedMessageData['fromemail'];
//0013076: different content when forwarding 'to a friend'
$messagePrecacheDto->subject = $forwardContent
? stripslashes($loadedMessageData['forwardsubject'])
: $loadedMessageData['subject'];
//0013076: different content when forwarding 'to a friend'
$messagePrecacheDto->content = $forwardContent
? stripslashes($loadedMessageData['forwardmessage'])
: $loadedMessageData['message'];
if ($this->useManualTextPart && !$forwardContent) {
$messagePrecacheDto->textContent = $loadedMessageData['textmessage'];
}
//0013076: different content when forwarding 'to a friend'
$messagePrecacheDto->footer = $forwardContent
? stripslashes($loadedMessageData['forwardfooter'])
: $loadedMessageData['footer'];
if ($this->isHtml($messagePrecacheDto->footer)) {
$messagePrecacheDto->textFooter = ($this->html2Text)($messagePrecacheDto->footer);
$messagePrecacheDto->htmlFooter = $messagePrecacheDto->footer;
} else {
$messagePrecacheDto->textFooter = $messagePrecacheDto->footer;
$messagePrecacheDto->htmlFooter = ($this->textParser)($messagePrecacheDto->footer);
}
}
private function applyTemplate(MessagePrecacheDto $messagePrecacheDto, $loadedMessageData): void
{
if ($loadedMessageData['template']) {
$template = $this->templateRepository->findOneById($loadedMessageData['template']);
if ($template) {
$messagePrecacheDto->template = stripslashes($template->getContent());
$messagePrecacheDto->templateText = stripslashes($template->getText());
$messagePrecacheDto->templateId = $template->getId();
}
}
}
private function applyRemoteContentIfPresent(MessagePrecacheDto $messagePrecacheDto, $loadedMessageData): bool
{
if ($messagePrecacheDto->userSpecificUrl
|| !preg_match('/\[URL:([^\s]+)\]/i', $messagePrecacheDto->content, $regs)
) {
return true;
}
$remoteContent = ($this->remotePageFetcher)($regs[1], []);
if (!$remoteContent) {
$this->eventLogManager->log(
page: 'unknown page',
entry: 'Error fetching URL: ' . $loadedMessageData['sendurl'] . ' cannot proceed',
);
return false;
}
$messagePrecacheDto->content = str_replace($regs[0], $remoteContent, $messagePrecacheDto->content);
$messagePrecacheDto->htmlFormatted = $this->isHtml($remoteContent);
//# 17086 - disregard any template settings when we have a valid remote URL
$messagePrecacheDto->template = null;
$messagePrecacheDto->templateText = null;
$messagePrecacheDto->templateId = null;
return true;
}
private function applyBasicReplacements(MessagePrecacheDto $messagePrecacheDto, $loadedMessageData): void
{
foreach (self::REPLACE_KEYS as $key) {
$replace = (string) $loadedMessageData[$key];
$searchKey = '['. $key . ']';
// Replace in content except for user-specific URL
if (!$messagePrecacheDto->userSpecificUrl) {
$messagePrecacheDto->content = str_ireplace($searchKey, $replace, $messagePrecacheDto->content);
}
$messagePrecacheDto->textContent = str_ireplace($searchKey, $replace, $messagePrecacheDto->textContent);
$messagePrecacheDto->textFooter = str_ireplace($searchKey, $replace, $messagePrecacheDto->textFooter);
$messagePrecacheDto->htmlFooter = str_ireplace($searchKey, $replace, $messagePrecacheDto->htmlFooter);
}
}
private function populateAdminAttributes(MessagePrecacheDto $messagePrecacheDto, Message $campaign): void
{
$ownerAttrValues = $this->adminAttreDefRepository->getForAdmin($campaign->getOwner());
foreach ($ownerAttrValues as $attr) {
$messagePrecacheDto->adminAttributes['OWNER.' . $attr['name']] = $attr['value'];
}
$relatedAdmins = $this->adminRepository->getMessageRelatedAdmins($campaign->getId());
if (count($relatedAdmins) === 1) {
$listOwnerAttrValues = $this->adminAttreDefRepository->getForAdmin($relatedAdmins[0]);
} else {
$listOwnerAttrValues = $this->adminAttreDefRepository->getAllWithEmptyValues();
}
foreach ($listOwnerAttrValues as $attr) {
$messagePrecacheDto->adminAttributes['LISTOWNER.' . $attr['name']] = $attr['value'];
}
}
}