PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.2.2
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.2.2
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / Helpers / MailboxHelper.php

MailboxHelper.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.2.2, at back/Helpers/MailboxHelper.php

298 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Helpers;
4
5 use AcyMailing\Classes\UserClass;
6
7 class MailboxHelper extends BounceHelper
8 {
9 private array $mailboxConfig = [];
10 public $conditions;
11
12 public function buildConfigFromMailbox(): bool
13 {
14 $this->mailboxConfig = [
15 'server' => $this->action->server,
16 'username' => $this->action->username,
17 'password' => $this->action->password,
18 'port' => $this->action->port,
19 'connect_method' => $this->action->connection_method,
20 'secure_method' => $this->action->secure_method,
21 'self_signed' => $this->action->self_signed,
22 'timeout' => 10,
23 'bounce_access_token' => empty($this->action->bounce_access_token) ? '' : $this->action->bounce_access_token,
24 ];
25
26 return $this->isConfigurationValid();
27 }
28
29 private function isConfigurationValid(): bool
30 {
31 $keyShouldNotBeEmpty = in_array($this->mailboxConfig['server'], self::HOSTS_NEEDING_OAUTH) ? 'bounce_access_token' : 'password';
32
33 $error = false;
34 foreach ($this->mailboxConfig as $key => $oneConfig) {
35 if (empty($oneConfig) && $key === $keyShouldNotBeEmpty) {
36 $error = true;
37 break;
38 }
39 }
40
41 return !$error;
42 }
43
44 public function isConnectionValid(object $mailbox, bool $close = true): bool
45 {
46 $this->action = $mailbox;
47 if (!$this->buildConfigFromMailbox()) {
48 return false;
49 }
50
51 if (!$this->init($this->mailboxConfig) || !$this->connect()) {
52 return false;
53 }
54
55 if ($close) {
56 $this->close();
57 }
58
59 return true;
60 }
61
62 private function senderConditionPass(): bool
63 {
64 $conditions = $this->action->conditions;
65 $fromEmail = $this->_message->header->from_email;
66
67 if ($conditions['sender'] == 'specific') {
68 $allowedSenders = explode(',', $conditions['specific']);
69 if (empty($allowedSenders) || !in_array($fromEmail, $allowedSenders)) {
70 $this->display(acym_translationSprintf('ACYM_SENDER_NOT_ALLOWED_X', $fromEmail));
71
72 return false;
73 }
74 }
75
76 if ($conditions['sender'] == 'group') {
77 $cmsUserId = acym_getCmsUserIdByEmail($fromEmail);
78 if (empty($cmsUserId)) {
79 $this->display(acym_translationSprintf('ACYM_SENDER_NOT_ALLOWED_X_CMS', $fromEmail, ACYM_CMS_TITLE));
80
81 return false;
82 }
83
84 $groups = acym_getGroupsByUser($cmsUserId, false);
85
86 if (!in_array($conditions['groups'], $groups)) {
87 $this->display(acym_translationSprintf('ACYM_SENDER_NOT_ALLOWED_X_GROUP', $fromEmail, implode(', ', $conditions['groups'])));
88
89 return false;
90 }
91 }
92
93 if ($conditions['sender'] == 'lists' && !empty($conditions['lists'])) {
94 $userClass = new UserClass();
95 $user = $userClass->getOneByEmail($fromEmail);
96
97 if (empty($user)) {
98 $this->display(acym_translationSprintf('ACYM_SENDER_NOT_ALLOWED_X_NOT_EXISTS', $fromEmail));
99
100 return false;
101 }
102
103 $subscriptions = $userClass->getSubscriptionStatus($user->id, $conditions['lists'], 1);
104
105 if (empty($subscriptions)) {
106 $this->display(acym_translationSprintf('ACYM_SENDER_NOT_ALLOWED_X_LIST', $fromEmail));
107
108 return false;
109 }
110 }
111
112 return true;
113 }
114
115 private function subjectConditionPass(): bool
116 {
117 $conditions = $this->action->conditions;
118 $subject = $this->_message->subject;
119
120 $passSubject = true;
121 if (empty($subject)) {
122 $passSubject = false;
123 } elseif ($conditions['subject'] == 'begins' && strpos($subject, $conditions['subject_text']) !== 0) {
124 $passSubject = false;
125 } elseif ($conditions['subject'] == 'ends' && strpos(
126 $subject,
127 $conditions['subject_text']
128 ) !== strlen($subject) - strlen($conditions['subjectvalue'])) {
129 $passSubject = false;
130 } elseif ($conditions['subject'] == 'contains' && strpos($subject, $conditions['subject_text']) === false) {
131 $passSubject = false;
132 } elseif ($conditions['subject'] == 'regex' && !preg_match($conditions['subject_regex'], $subject)) {
133 $passSubject = false;
134 }
135
136 if (!$passSubject) {
137 $this->display(acym_translation('ACYM_SUBJECT_DOESNT_MATCH'));
138
139 return false;
140 }
141
142 return true;
143 }
144
145 public function conditionsPass(): bool
146 {
147 if (!empty($this->action->conditions['sender']) && !$this->senderConditionPass()) {
148 return false;
149 }
150
151 if (!empty($this->action->conditions['subject']) && !$this->subjectConditionPass()) {
152 return false;
153 }
154
155 return true;
156 }
157
158 public function handleAction(): void
159 {
160 if (empty($this->action)) {
161 return;
162 }
163
164 $maxMessages = min($this->nbMessages, 100);
165
166 if ($this->report) {
167 if (function_exists('apache_get_modules')) {
168 $modules = apache_get_modules();
169 $this->mod_security2 = in_array('mod_security2', $modules);
170 }
171
172 @ini_set('output_buffering', 'off');
173 @ini_set('zlib.output_compression', 0);
174
175 if (!headers_sent()) {
176 while (ob_get_level() > 0 && $this->obend++ < 3) {
177 @ob_end_flush();
178 }
179 }
180
181 $disp = "<div style='position:fixed; top:3px;left:3px;background-color : white;border : 1px solid grey; padding : 3px;font-size:14px'>";
182 $disp .= acym_translation('ACYM_MAILBOX_ACTIONS');
183 $disp .= ': <span id="counter">0</span> / '.$maxMessages;
184 $disp .= '</div>';
185 $disp .= '<script type="text/javascript" language="javascript">';
186 $disp .= 'var mycounter = document.getElementById("counter");';
187 $disp .= 'function setCounter(val){ mycounter.innerHTML=val;}';
188 $disp .= '</script>';
189 echo $disp;
190 if (function_exists('ob_flush')) {
191 @ob_flush();
192 }
193 if (!$this->mod_security2) {
194 @flush();
195 }
196 }
197
198 $msgNB = $maxMessages;
199
200 while (($msgNB > 0) && ($this->_message = $this->getMessage($msgNB))) {
201 if ($this->report) {
202 echo '<script type="text/javascript" language="javascript">setCounter('.($maxMessages - $msgNB + 1).')</script>';
203 if (function_exists('ob_flush')) {
204 @ob_flush();
205 }
206 if (!$this->mod_security2) {
207 @flush();
208 }
209 }
210 $this->_message->messageNB = $msgNB;
211 $msgNB--;
212
213 if (empty($this->_message->subject)) {
214 $this->_message->subject = '';
215 }
216
217 if (!$this->decodeMessage()) {
218 $this->display(acym_translation('ACYM_ERROR_RETRIEVING_MESSAGE'), false, $maxMessages - $this->_message->messageNB + 1);
219 continue;
220 }
221
222 if (empty($this->_message->html)) {
223 $this->_message->html = nl2br($this->_message->text);
224 }
225 $stripedHtml = strip_tags($this->_message->html);
226
227 if (strlen($stripedHtml) < 3) {
228 $this->display(acym_translation('ACYM_EMPTY_EMAIL_X', acym_escape($this->_message->subject)));
229 if ($this->action->delete_wrong_emails) {
230 $this->deleteMessage($this->_message->messageNB);
231 }
232 continue;
233 }
234
235 if (!$this->conditionsPass()) {
236 $this->display(acym_translation('ACYM_INVALID_EMAIL', $this->_message->subject));
237 if ($this->action->delete_wrong_emails) {
238 $this->deleteMessage($this->_message->messageNB);
239 }
240 continue;
241 }
242
243 if ($this->executeActions()) {
244 $this->display(acym_translation('ACYM_MESSAGE_DELETED'));
245 $this->deleteMessage($this->_message->messageNB);
246 }
247
248 if (!empty($this->stoptime) && time() > $this->stoptime) {
249 break;
250 }
251 }
252 }
253
254 public function executeActions(): bool
255 {
256 $executedActions = [];
257
258 foreach ($this->action->actions as $actionKey => $oneAction) {
259
260 $this->display('<strong>'.acym_translationSprintf('ACYM_ACTION_X', intval($actionKey) + 1).'</strong>');
261
262 $actionId = array_keys($oneAction)[0];
263
264 if (empty($actionId)) {
265 continue;
266 }
267 $executedActions[$actionKey] = true;
268
269 $reportMessages = [];
270 acym_trigger('onAcymMailboxAction_'.$actionId, [&$oneAction[$actionId], &$reportMessages, &$executedActions[$actionKey], $this]);
271
272 foreach ($reportMessages as $reportMessage) {
273 $this->display($reportMessage['message'], $reportMessage['success']);
274 }
275
276 $this->attachments = [];
277 }
278
279 $allActionsExecuted = true;
280 foreach ($executedActions as $oneActionExecuted) {
281 $allActionsExecuted = $allActionsExecuted && $oneActionExecuted;
282 }
283
284 return $allActionsExecuted;
285 }
286
287 public function getAttachments(): string
288 {
289 $newAttachments = [];
290 foreach ($this->attachments as $attachment) {
291 unset($attachment->size);
292 $newAttachments[] = $attachment;
293 }
294
295 return json_encode($newAttachments);
296 }
297 }
298