| 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 |
$actionParameters = ''; |
| 67 |
|
| 68 |
foreach ($actions as $key => $oneAction) { |
| 69 |
$actionOptions[$key] = $oneAction->name; |
| 70 |
$actionParameters .= '<div class="acym__mailbox__edition__action__one__parameters '.$key.' margin-top-1">'.$oneAction->option.'</div>'; |
| 71 |
} |
| 72 |
|
| 73 |
$initialAction = acym_select( |
| 74 |
$actionOptions, |
| 75 |
'acym_action[__num__][action]', |
| 76 |
'', |
| 77 |
[ |
| 78 |
'class' => 'acym__select acym__mailbox__edition__action__one__choice', |
| 79 |
'acym-data-infinite' => '', |
| 80 |
] |
| 81 |
); |
| 82 |
$initialAction .= $actionParameters; |
| 83 |
|
| 84 |
$data = [ |
| 85 |
'mailboxId' => $mailboxId, |
| 86 |
'mailboxActions' => $mailboxAction, |
| 87 |
'delayType' => new DelayType(), |
| 88 |
'groups' => acym_getGroups(), |
| 89 |
'lists' => $listsClass->getAllWithIdName(), |
| 90 |
'initialAction' => $initialAction, |
| 91 |
]; |
| 92 |
|
| 93 |
parent::display($data); |
| 94 |
} |
| 95 |
|
| 96 |
public function applyMailboxAction(): void |
| 97 |
{ |
| 98 |
$this->storeMailboxAction(); |
| 99 |
$this->mailboxAction(); |
| 100 |
} |
| 101 |
|
| 102 |
public function saveMailboxAction(): void |
| 103 |
{ |
| 104 |
$this->storeMailboxAction(); |
| 105 |
$this->listing(); |
| 106 |
} |
| 107 |
|
| 108 |
public function storeMailboxAction(): void |
| 109 |
{ |
| 110 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 111 |
|
| 112 |
$mailbox = acym_getVar('array', 'mailbox', []); |
| 113 |
$mailboxClass = new MailboxClass(); |
| 114 |
$mailboxObject = new \stdClass(); |
| 115 |
|
| 116 |
foreach ($mailbox as $column => $value) { |
| 117 |
acym_secureDBColumn($column); |
| 118 |
if (is_array($value) || is_object($value)) { |
| 119 |
$mailboxObject->$column = json_encode($value); |
| 120 |
} else { |
| 121 |
$mailboxObject->$column = $value; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
$actions = acym_getVar('array', 'acym_action', []); |
| 126 |
$mailboxObject->actions = []; |
| 127 |
foreach ($actions as $oneAction) { |
| 128 |
if (empty($oneAction['action'])) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
$mailboxObject->actions[] = [$oneAction['action'] => $oneAction[$oneAction['action']]]; |
| 133 |
} |
| 134 |
$mailboxObject->actions = json_encode($mailboxObject->actions); |
| 135 |
|
| 136 |
if (!empty($mailboxObject->password) && trim($mailboxObject->password, '*') === '') { |
| 137 |
unset($mailboxObject->password); |
| 138 |
} |
| 139 |
|
| 140 |
$mailboxId = $mailboxClass->save($mailboxObject); |
| 141 |
|
| 142 |
if (empty($mailboxId)) { |
| 143 |
acym_enqueueMessage(acym_translation('ACYM_ERROR_SAVING'), 'error'); |
| 144 |
} else { |
| 145 |
acym_enqueueMessage(acym_translation('ACYM_SUCCESSFULLY_SAVED')); |
| 146 |
acym_setVar('mailboxId', $mailboxId); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public function testMailboxAction(): void |
| 151 |
{ |
| 152 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 153 |
|
| 154 |
$mailbox = acym_getVar('array', 'mailbox', []); |
| 155 |
|
| 156 |
if (empty($mailbox)) { |
| 157 |
acym_sendAjaxResponse(acym_translation('ACYM_NO_CONFIGURATION'), [], false); |
| 158 |
} |
| 159 |
|
| 160 |
$mailbox = (object)$mailbox; |
| 161 |
|
| 162 |
// 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 |
| 163 |
if (!empty($mailbox->id)) { |
| 164 |
if ($mailbox->id !== 'configuration') { |
| 165 |
// We check if the password has not changed |
| 166 |
if (empty(trim($mailbox->password, '*'))) { |
| 167 |
$mailboxClass = new MailboxClass(); |
| 168 |
$mailboxFromDatabase = $mailboxClass->getOneById($mailbox->id); |
| 169 |
if (!empty($mailboxFromDatabase) && $this->isSameMailboxHost($mailbox->server ?? '', $mailboxFromDatabase->server)) { |
| 170 |
$mailbox->password = $mailboxFromDatabase->password; |
| 171 |
} |
| 172 |
} |
| 173 |
} elseif (empty(trim($mailbox->password, '*'))) { |
| 174 |
// If it comes from the configuration menu, we need to get the password from the config table (not the mailbox_action table) |
| 175 |
if ($this->isSameMailboxHost($mailbox->server ?? '', $this->config->get('bounce_server'))) { |
| 176 |
$mailbox->password = $this->config->get('bounce_password'); |
| 177 |
$mailbox->bounce_access_token = str_replace('Bearer ', '', $this->config->get('bounce_access_token', '')); |
| 178 |
if (empty($mailbox->connection_method)) { |
| 179 |
$mailbox->connection_method = $this->config->get('connection_method', 'imap'); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
$mailboxHelper = new MailboxHelper(); |
| 186 |
try { |
| 187 |
$isConnectionValid = $mailboxHelper->isConnectionValid($mailbox); |
| 188 |
} catch (\Exception $e) { |
| 189 |
$this->mailboxReport[] = $e->getMessage(); |
| 190 |
} |
| 191 |
|
| 192 |
if (empty($isConnectionValid)) { |
| 193 |
acym_sendAjaxResponse(acym_translation('ACYM_CONNECTION_FAILED'), ['report' => $this->mailboxReport], false); |
| 194 |
} else { |
| 195 |
acym_sendAjaxResponse(acym_translation('ACYM_CONNECTION_SUCCESSFUL')); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
private function isSameMailboxHost($submittedServer, $storedServer): bool |
| 200 |
{ |
| 201 |
$storedServer = trim((string)$storedServer); |
| 202 |
if ($storedServer === '') { |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
return strcasecmp(trim((string)$submittedServer), $storedServer) === 0; |
| 207 |
} |
| 208 |
} |
| 209 |
|