| @@ -1,0 +1,195 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Controllers\MailboxActions; | |
| 4 | + | |
| 5 | +use AcyMailing\Classes\ListClass; | |
| 6 | +use AcyMailing\Classes\MailboxClass; | |
| 7 | +use AcyMailing\Helpers\MailboxHelper; | |
| 8 | +use AcyMailing\Types\DelayType; | |
| 9 | + | |
| 10 | +trait Edition | |
| 11 | +{ | |
| 12 | + public function mailboxAction(): void | |
| 13 | + { | |
| 14 | + $mailboxClass = new MailboxClass(); | |
| 15 | + acym_setVar('layout', 'mailbox_action'); | |
| 16 | + $mailboxId = acym_getVar('int', 'mailboxId', 0); | |
| 17 | + $listsClass = new ListClass(); | |
| 18 | + | |
| 19 | + if (!empty($mailboxId)) { | |
| 20 | + $mailboxAction = $mailboxClass->getOneById($mailboxId); | |
| 21 | + $this->breadcrumb[acym_translation($mailboxAction->name)] = acym_completeLink('bounces&task=mailboxAction&mailboxId='.$mailboxId); | |
| 22 | + } else { | |
| 23 | + $this->breadcrumb[acym_translation('ACYM_NEW')] = acym_completeLink('bounces&task=mailboxAction'); | |
| 24 | + $mailboxAction = new \stdClass(); | |
| 25 | + $mailboxAction->name = ''; | |
| 26 | + $mailboxAction->active = 0; | |
| 27 | + $mailboxAction->frequency = 900; | |
| 28 | + $mailboxAction->description = ''; | |
| 29 | + | |
| 30 | + $mailboxAction->server = ''; | |
| 31 | + $mailboxAction->username = ''; | |
| 32 | + $mailboxAction->password = ''; | |
| 33 | + $mailboxAction->connection_method = 'imap'; | |
| 34 | + $mailboxAction->secure_method = 'ssl'; | |
| 35 | + $mailboxAction->port = ''; | |
| 36 | + $mailboxAction->self_signed = 1; | |
| 37 | + | |
| 38 | + $mailboxAction->delete_wrong_emails = 0; | |
| 39 | + $mailboxAction->conditions = [ | |
| 40 | + 'sender' => '', | |
| 41 | + 'specific' => '', | |
| 42 | + 'groups' => '', | |
| 43 | + 'lists' => '', | |
| 44 | + 'subject' => '', | |
| 45 | + 'subject_text' => '', | |
| 46 | + 'subject_regex' => '', | |
| 47 | + 'subject_remove' => 1, | |
| 48 | + ]; | |
| 49 | + | |
| 50 | + $mailboxAction->actions = []; | |
| 51 | + $mailboxAction->senderfrom = 0; | |
| 52 | + $mailboxAction->senderto = 0; | |
| 53 | + } | |
| 54 | + | |
| 55 | + if (empty($mailboxAction->conditions['groups'])) { | |
| 56 | + $mailboxAction->conditions['groups'] = []; | |
| 57 | + } | |
| 58 | + | |
| 59 | + if (empty($mailboxAction->conditions['lists'])) { | |
| 60 | + $mailboxAction->conditions['lists'] = []; | |
| 61 | + } | |
| 62 | + | |
| 63 | + acym_trigger('onAcymMailboxActionDefine', [&$actions]); | |
| 64 | + | |
| 65 | + $actionOptions = ['' => acym_translation('ACYM_CHOOSE_ACTION')]; | |
| 66 | + foreach ($actions as $key => $oneAction) { | |
| 67 | + $actionOptions[$key] = $oneAction->name; | |
| 68 | + } | |
| 69 | + | |
| 70 | + $data = [ | |
| 71 | + 'mailboxId' => $mailboxId, | |
| 72 | + 'mailboxActions' => $mailboxAction, | |
| 73 | + 'delayType' => new DelayType(), | |
| 74 | + 'groups' => acym_getGroups(), | |
| 75 | + 'lists' => $listsClass->getAllWithIdName(), | |
| 76 | + 'actionDeclarations' => $actions, | |
| 77 | + 'actionOptions' => $actionOptions, | |
| 78 | + ]; | |
| 79 | + | |
| 80 | + parent::display($data); | |
| 81 | + } | |
| 82 | + | |
| 83 | + public function applyMailboxAction(): void | |
| 84 | + { | |
| 85 | + $this->storeMailboxAction(); | |
| 86 | + $this->mailboxAction(); | |
| 87 | + } | |
| 88 | + | |
| 89 | + public function saveMailboxAction(): void | |
| 90 | + { | |
| 91 | + $this->storeMailboxAction(); | |
| 92 | + $this->listing(); | |
| 93 | + } | |
| 94 | + | |
| 95 | + public function storeMailboxAction(): void | |
| 96 | + { | |
| 97 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 98 | + | |
| 99 | + $mailbox = acym_getVar('array', 'mailbox', []); | |
| 100 | + $mailboxClass = new MailboxClass(); | |
| 101 | + $mailboxObject = new \stdClass(); | |
| 102 | + | |
| 103 | + foreach ($mailbox as $column => $value) { | |
| 104 | + acym_secureDBColumn($column); | |
| 105 | + if (is_array($value) || is_object($value)) { | |
| 106 | + $mailboxObject->$column = json_encode($value); | |
| 107 | + } else { | |
| 108 | + $mailboxObject->$column = $value; | |
| 109 | + } | |
| 110 | + } | |
| 111 | + | |
| 112 | + $actions = acym_getVar('array', 'acym_action', []); | |
| 113 | + $mailboxObject->actions = []; | |
| 114 | + foreach ($actions as $oneAction) { | |
| 115 | + if (empty($oneAction['action'])) { | |
| 116 | + continue; | |
| 117 | + } | |
| 118 | + | |
| 119 | + $mailboxObject->actions[] = [$oneAction['action'] => $oneAction[$oneAction['action']]]; | |
| 120 | + } | |
| 121 | + $mailboxObject->actions = json_encode($mailboxObject->actions); | |
| 122 | + | |
| 123 | + if (!empty($mailboxObject->password) && trim($mailboxObject->password, '*') === '') { | |
| 124 | + unset($mailboxObject->password); | |
| 125 | + } | |
| 126 | + | |
| 127 | + $mailboxId = $mailboxClass->save($mailboxObject); | |
| 128 | + | |
| 129 | + if (empty($mailboxId)) { | |
| 130 | + acym_enqueueMessage(acym_translation('ACYM_ERROR_SAVING'), 'error'); | |
| 131 | + } else { | |
| 132 | + acym_enqueueMessage(acym_translation('ACYM_SUCCESSFULLY_SAVED')); | |
| 133 | + acym_setVar('mailboxId', $mailboxId); | |
| 134 | + } | |
| 135 | + } | |
| 136 | + | |
| 137 | + public function testMailboxAction(): void | |
| 138 | + { | |
| 139 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 140 | + | |
| 141 | + $mailbox = acym_getVar('array', 'mailbox', []); | |
| 142 | + | |
| 143 | + if (empty($mailbox)) { | |
| 144 | + acym_sendAjaxResponse(acym_translation('ACYM_NO_CONFIGURATION'), [], false); | |
| 145 | + } | |
| 146 | + | |
| 147 | + $mailbox = (object)$mailbox; | |
| 148 | + | |
| 149 | + // If this is not a new mailbox or if it is not from the configuration menu, we need to get the password from the database | |
| 150 | + if (!empty($mailbox->id)) { | |
| 151 | + if ($mailbox->id !== 'configuration') { | |
| 152 | + // We check if the password has not changed | |
| 153 | + if (empty(trim($mailbox->password, '*'))) { | |
| 154 | + $mailboxClass = new MailboxClass(); | |
| 155 | + $mailboxFromDatabase = $mailboxClass->getOneById($mailbox->id); | |
| 156 | + if (!empty($mailboxFromDatabase) && $this->isSameMailboxHost($mailbox->server ?? '', $mailboxFromDatabase->server)) { | |
| 157 | + $mailbox->password = $mailboxFromDatabase->password; | |
| 158 | + } | |
| 159 | + } | |
| 160 | + } elseif (empty(trim($mailbox->password, '*'))) { | |
| 161 | + // If it comes from the configuration menu, we need to get the password from the config table (not the mailbox_action table) | |
| 162 | + if ($this->isSameMailboxHost($mailbox->server ?? '', $this->config->get('bounce_server'))) { | |
| 163 | + $mailbox->password = $this->config->get('bounce_password'); | |
| 164 | + $mailbox->bounce_access_token = str_replace('Bearer ', '', $this->config->get('bounce_access_token', '')); | |
| 165 | + if (empty($mailbox->connection_method)) { | |
| 166 | + $mailbox->connection_method = $this->config->get('connection_method', 'imap'); | |
| 167 | + } | |
| 168 | + } | |
| 169 | + } | |
| 170 | + } | |
| 171 | + | |
| 172 | + $mailboxHelper = new MailboxHelper(); | |
| 173 | + try { | |
| 174 | + $isConnectionValid = $mailboxHelper->isConnectionValid($mailbox); | |
| 175 | + } catch (\Exception $e) { | |
| 176 | + $this->mailboxReport[] = $e->getMessage(); | |
| 177 | + } | |
| 178 | + | |
| 179 | + if (empty($isConnectionValid)) { | |
| 180 | + acym_sendAjaxResponse(acym_translation('ACYM_CONNECTION_FAILED'), ['report' => $this->mailboxReport], false); | |
| 181 | + } else { | |
| 182 | + acym_sendAjaxResponse(acym_translation('ACYM_CONNECTION_SUCCESSFUL')); | |
| 183 | + } | |
| 184 | + } | |
| 185 | + | |
| 186 | + private function isSameMailboxHost($submittedServer, $storedServer): bool | |
| 187 | + { | |
| 188 | + $storedServer = trim((string)$storedServer); | |
| 189 | + if ($storedServer === '') { | |
| 190 | + return false; | |
| 191 | + } | |
| 192 | + | |
| 193 | + return strcasecmp(trim((string)$submittedServer), $storedServer) === 0; | |
| 194 | + } | |
| 195 | +} | |