| @@ -1,293 +1,0 @@ | ||
| 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), self::MESSAGE_TYPE_INFO); | |
| 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), self::MESSAGE_TYPE_INFO); | |
| 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'])), self::MESSAGE_TYPE_INFO); | |
| 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), self::MESSAGE_TYPE_INFO); | |
| 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), self::MESSAGE_TYPE_INFO); | |
| 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($subject, $conditions['subject_text']) !== strlen($subject) - strlen($conditions['subjectvalue'])) { | |
| 126 | - $passSubject = false; | |
| 127 | - } elseif ($conditions['subject'] == 'contains' && strpos($subject, $conditions['subject_text']) === false) { | |
| 128 | - $passSubject = false; | |
| 129 | - } elseif ($conditions['subject'] == 'regex' && !preg_match($conditions['subject_regex'], $subject)) { | |
| 130 | - $passSubject = false; | |
| 131 | - } | |
| 132 | - | |
| 133 | - if (!$passSubject) { | |
| 134 | - $this->display(acym_translation('ACYM_SUBJECT_DOESNT_MATCH'), self::MESSAGE_TYPE_INFO); | |
| 135 | - | |
| 136 | - return false; | |
| 137 | - } | |
| 138 | - | |
| 139 | - return true; | |
| 140 | - } | |
| 141 | - | |
| 142 | - public function conditionsPass(): bool | |
| 143 | - { | |
| 144 | - if (!empty($this->action->conditions['sender']) && !$this->senderConditionPass()) { | |
| 145 | - return false; | |
| 146 | - } | |
| 147 | - | |
| 148 | - if (!empty($this->action->conditions['subject']) && !$this->subjectConditionPass()) { | |
| 149 | - return false; | |
| 150 | - } | |
| 151 | - | |
| 152 | - return true; | |
| 153 | - } | |
| 154 | - | |
| 155 | - public function handleAction(): void | |
| 156 | - { | |
| 157 | - if (empty($this->action)) { | |
| 158 | - return; | |
| 159 | - } | |
| 160 | - | |
| 161 | - $maxMessages = min($this->nbMessages, 100); | |
| 162 | - | |
| 163 | - if ($this->report) { | |
| 164 | - if (function_exists('apache_get_modules')) { | |
| 165 | - $modules = apache_get_modules(); | |
| 166 | - $this->mod_security2 = in_array('mod_security2', $modules); | |
| 167 | - } | |
| 168 | - | |
| 169 | - @ini_set('output_buffering', 'off'); | |
| 170 | - @ini_set('zlib.output_compression', 0); | |
| 171 | - | |
| 172 | - if (!headers_sent()) { | |
| 173 | - while (ob_get_level() > 0 && $this->obend++ < 3) { | |
| 174 | - @ob_end_flush(); | |
| 175 | - } | |
| 176 | - } | |
| 177 | - | |
| 178 | - $disp = "<div style='position:fixed; top:3px;left:3px;background-color : white;border : 1px solid grey; padding : 3px;font-size:14px'>"; | |
| 179 | - $disp .= acym_translation('ACYM_MAILBOX_ACTIONS'); | |
| 180 | - $disp .= ': <span id="counter">0</span> / '.$maxMessages; | |
| 181 | - $disp .= '</div>'; | |
| 182 | - $disp .= '<script type="text/javascript" language="javascript">'; | |
| 183 | - $disp .= 'var mycounter = document.getElementById("counter");'; | |
| 184 | - $disp .= 'function setCounter(val){ mycounter.innerHTML=val;}'; | |
| 185 | - $disp .= '</script>'; | |
| 186 | - echo $disp; | |
| 187 | - if (function_exists('ob_flush')) { | |
| 188 | - @ob_flush(); | |
| 189 | - } | |
| 190 | - if (!$this->mod_security2) { | |
| 191 | - @flush(); | |
| 192 | - } | |
| 193 | - } | |
| 194 | - | |
| 195 | - $msgNB = $maxMessages; | |
| 196 | - | |
| 197 | - while (($msgNB > 0) && ($this->_message = $this->getMessage($msgNB))) { | |
| 198 | - if ($this->report) { | |
| 199 | - echo '<script type="text/javascript" language="javascript">setCounter('.($maxMessages - $msgNB + 1).')</script>'; | |
| 200 | - if (function_exists('ob_flush')) { | |
| 201 | - @ob_flush(); | |
| 202 | - } | |
| 203 | - if (!$this->mod_security2) { | |
| 204 | - @flush(); | |
| 205 | - } | |
| 206 | - } | |
| 207 | - $this->_message->messageNB = $msgNB; | |
| 208 | - $msgNB--; | |
| 209 | - | |
| 210 | - if (empty($this->_message->subject)) { | |
| 211 | - $this->_message->subject = ''; | |
| 212 | - } | |
| 213 | - | |
| 214 | - if (!$this->decodeMessage()) { | |
| 215 | - $this->display(acym_translation('ACYM_ERROR_RETRIEVING_MESSAGE'), self::MESSAGE_TYPE_ERROR, $maxMessages - $this->_message->messageNB + 1); | |
| 216 | - continue; | |
| 217 | - } | |
| 218 | - | |
| 219 | - if (empty($this->_message->html)) { | |
| 220 | - $this->_message->html = nl2br($this->_message->text); | |
| 221 | - } | |
| 222 | - $stripedHtml = strip_tags($this->_message->html); | |
| 223 | - | |
| 224 | - if (strlen($stripedHtml) < 3) { | |
| 225 | - $this->display(acym_translation('ACYM_EMPTY_EMAIL_X', acym_escape($this->_message->subject)), self::MESSAGE_TYPE_ERROR); | |
| 226 | - if ($this->action->delete_wrong_emails) { | |
| 227 | - $this->deleteMessage($this->_message->messageNB); | |
| 228 | - } | |
| 229 | - continue; | |
| 230 | - } | |
| 231 | - | |
| 232 | - if (!$this->conditionsPass()) { | |
| 233 | - $this->display(acym_translation('ACYM_INVALID_EMAIL', $this->_message->subject), self::MESSAGE_TYPE_ERROR); | |
| 234 | - if ($this->action->delete_wrong_emails) { | |
| 235 | - $this->deleteMessage($this->_message->messageNB); | |
| 236 | - } | |
| 237 | - continue; | |
| 238 | - } | |
| 239 | - | |
| 240 | - if ($this->executeActions()) { | |
| 241 | - $this->display(acym_translation('ACYM_MESSAGE_DELETED')); | |
| 242 | - $this->deleteMessage($this->_message->messageNB); | |
| 243 | - } | |
| 244 | - | |
| 245 | - if (!empty($this->stoptime) && time() > $this->stoptime) { | |
| 246 | - break; | |
| 247 | - } | |
| 248 | - } | |
| 249 | - } | |
| 250 | - | |
| 251 | - public function executeActions(): bool | |
| 252 | - { | |
| 253 | - $executedActions = []; | |
| 254 | - | |
| 255 | - foreach ($this->action->actions as $actionKey => $oneAction) { | |
| 256 | - $this->display('<strong>'.acym_translationSprintf('ACYM_ACTION_X', intval($actionKey) + 1).'</strong>'); | |
| 257 | - | |
| 258 | - $actionId = array_keys($oneAction)[0]; | |
| 259 | - | |
| 260 | - if (empty($actionId)) { | |
| 261 | - continue; | |
| 262 | - } | |
| 263 | - $executedActions[$actionKey] = true; | |
| 264 | - | |
| 265 | - $reportMessages = []; | |
| 266 | - acym_trigger('onAcymMailboxAction_'.$actionId, [&$oneAction[$actionId], &$reportMessages, &$executedActions[$actionKey], $this]); | |
| 267 | - | |
| 268 | - foreach ($reportMessages as $reportMessage) { | |
| 269 | - $this->display($reportMessage['message'], $reportMessage['success'] ? self::MESSAGE_TYPE_SUCCESS : self::MESSAGE_TYPE_ERROR); | |
| 270 | - } | |
| 271 | - | |
| 272 | - $this->attachments = []; | |
| 273 | - } | |
| 274 | - | |
| 275 | - $allActionsExecuted = true; | |
| 276 | - foreach ($executedActions as $oneActionExecuted) { | |
| 277 | - $allActionsExecuted = $allActionsExecuted && $oneActionExecuted; | |
| 278 | - } | |
| 279 | - | |
| 280 | - return $allActionsExecuted; | |
| 281 | - } | |
| 282 | - | |
| 283 | - public function getAttachments(): string | |
| 284 | - { | |
| 285 | - $newAttachments = []; | |
| 286 | - foreach ($this->attachments as $attachment) { | |
| 287 | - unset($attachment->size); | |
| 288 | - $newAttachments[] = $attachment; | |
| 289 | - } | |
| 290 | - | |
| 291 | - return json_encode($newAttachments); | |
| 292 | - } | |
| 293 | -} | |