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