| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Controllers; |
| 4 |
|
| 5 |
use AcyMailing\Controllers\Configuration\Language; |
| 6 |
use AcyMailing\Core\AcymController; |
| 7 |
use AcyMailing\Controllers\Configuration\Listing; |
| 8 |
use AcyMailing\Controllers\Configuration\Security; |
| 9 |
use AcyMailing\Controllers\Configuration\Mail; |
| 10 |
use AcyMailing\Controllers\Configuration\Queue; |
| 11 |
use AcyMailing\Controllers\Configuration\Subscription; |
| 12 |
use AcyMailing\Controllers\Configuration\License; |
| 13 |
|
| 14 |
class ConfigurationController extends AcymController |
| 15 |
{ |
| 16 |
use Listing; |
| 17 |
use Security; |
| 18 |
use Mail; |
| 19 |
use Queue; |
| 20 |
use Subscription; |
| 21 |
use License; |
| 22 |
use Language; |
| 23 |
|
| 24 |
public function __construct() |
| 25 |
{ |
| 26 |
parent::__construct(); |
| 27 |
$this->breadcrumb[acym_translation('ACYM_CONFIGURATION')] = acym_completeLink('configuration'); |
| 28 |
|
| 29 |
$this->loadScripts = [ |
| 30 |
'listing' => [ |
| 31 |
'colorpicker', |
| 32 |
], |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
public function getAjax(): void |
| 37 |
{ |
| 38 |
acym_checkToken(); |
| 39 |
|
| 40 |
$field = acym_getVar('string', 'field', ''); |
| 41 |
|
| 42 |
$whitelistedFields = [ |
| 43 |
'level', |
| 44 |
'unsplash_key', |
| 45 |
'tenor_key', |
| 46 |
]; |
| 47 |
|
| 48 |
if (acym_isAdmin()) { |
| 49 |
$whitelistedFields[] = 'save_thumbnail'; |
| 50 |
} |
| 51 |
|
| 52 |
if (!in_array($field, $whitelistedFields)) { |
| 53 |
acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_LOAD_INFORMATION'), [], false); |
| 54 |
} |
| 55 |
|
| 56 |
$res = $this->config->get($field, ''); |
| 57 |
|
| 58 |
if (intval($res) !== 0 && empty($res)) { |
| 59 |
acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_LOAD_INFORMATION'), [], false); |
| 60 |
} else { |
| 61 |
acym_sendAjaxResponse('', ['value' => $res]); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function displayMessage(string $message, bool $ajax = false): array |
| 66 |
{ |
| 67 |
$correspondences = [ |
| 68 |
'WEBSITE_NOT_FOUND' => ['message' => 'ACYM_WEBSITE_NOT_FOUND', 'type' => 'error'], |
| 69 |
'LICENSE_NOT_FOUND' => ['message' => 'ACYM_LICENSE_NOT_FOUND', 'type' => 'error'], |
| 70 |
'WELL_ATTACH' => ['message' => 'ACYM_LICENSE_WELL_ATTACH', 'type' => 'info'], |
| 71 |
'ISSUE_WHILE_ATTACH' => ['message' => 'ACYM_ISSUE_WHILE_ATTACHING_LICENSE', 'type' => 'error'], |
| 72 |
'ALREADY_ATTACH' => ['message' => 'ACYM_WEBSITE_ALREADY_ATTACHED', 'type' => 'info'], |
| 73 |
'LICENSES_DONT_MATCH' => ['message' => 'ACYM_CANT_UNLINK_WEBSITE_LICENSE_DONT_MATCH', 'type' => 'error'], |
| 74 |
'MAX_SITES_ATTACH' => ['message' => 'ACYM_REACHED_MAX_SITES_ATTACHED', 'type' => 'error'], |
| 75 |
'SITE_NOT_FOUND' => ['message' => 'ACYM_ISSUE_WHILE_ATTACHING_LICENSE', 'type' => 'error'], |
| 76 |
'UNLINK_SUCCESSFUL' => ['message' => 'ACYM_LICENSE_UNLINK_SUCCESSFUL', 'type' => 'info'], |
| 77 |
'UNLINK_FAILED' => ['message' => 'ACYM_ERROR_WHILE_UNLINK_LICENSE', 'type' => 'error'], |
| 78 |
'CRON_WELL_ACTIVATED' => ['message' => 'ACYM_AUTOMATIC_SEND_PROCESS_WELL_ACTIVATED', 'type' => 'info'], |
| 79 |
'CRON_WELL_DEACTIVATED' => ['message' => 'ACYM_AUTOMATIC_SEND_PROCESS_WELL_DEACTIVATED', 'type' => 'info'], |
| 80 |
'CRON_NOT_SAVED' => ['message' => 'ACYM_AUTOMATIC_SEND_PROCESS_NOT_ENABLED', 'type' => 'error'], |
| 81 |
]; |
| 82 |
|
| 83 |
if (!$ajax) { |
| 84 |
if (empty($message) || empty($correspondences[$message])) { |
| 85 |
acym_enqueueMessage(acym_translation('ACYM_ERROR_ON_CALL_ACYBA_WEBSITE'), 'error'); |
| 86 |
|
| 87 |
if (!empty($message)) acym_enqueueMessage(acym_translationSprintf('ACYM_CURL_ERROR_MESSAGE', $message), 'error'); |
| 88 |
|
| 89 |
return []; |
| 90 |
} |
| 91 |
|
| 92 |
acym_enqueueMessage(acym_translation($correspondences[$message]['message']), $correspondences[$message]['type']); |
| 93 |
|
| 94 |
return $correspondences[$message]['type'] === 'info' ? $correspondences[$message] : []; |
| 95 |
} else { |
| 96 |
if (empty($message) || empty($correspondences[$message])) { |
| 97 |
$response = ['message' => acym_translation('ACYM_ERROR_ON_CALL_ACYBA_WEBSITE'), 'type' => 'error']; |
| 98 |
|
| 99 |
if (!empty($message)) $response['message'] = acym_translationSprintf('ACYM_CURL_ERROR_MESSAGE', $message); |
| 100 |
|
| 101 |
return $response; |
| 102 |
} |
| 103 |
|
| 104 |
$response = $correspondences[$message]; |
| 105 |
$response['message'] = acym_translation($response['message']); |
| 106 |
|
| 107 |
return $response; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
function seeLogs(): void |
| 112 |
{ |
| 113 |
$filename = acym_getVar('string', 'filename'); |
| 114 |
|
| 115 |
if (empty($filename) || !acym_fileNameValid($filename)) { |
| 116 |
echo acym_translation('ACYM_FILENAME_EMPTY_OR_NOT_VALID'); |
| 117 |
exit; |
| 118 |
} |
| 119 |
|
| 120 |
$reportPath = acym_getLogPath($filename); |
| 121 |
|
| 122 |
if (!file_exists($reportPath)) { |
| 123 |
echo acym_translation('ACYM_EXIST_LOG'); |
| 124 |
exit; |
| 125 |
} |
| 126 |
|
| 127 |
if (ACYM_CMS === 'wordpress') @ob_get_clean(); |
| 128 |
|
| 129 |
$final = acym_fileGetContent($reportPath); |
| 130 |
echo nl2br($final); |
| 131 |
|
| 132 |
exit; |
| 133 |
} |
| 134 |
|
| 135 |
private function loginForOAuth2(bool $isSmtp = true): void |
| 136 |
{ |
| 137 |
$auth2Smtp = [ |
| 138 |
'smtp.gmail.com' => [ |
| 139 |
'baseUrl' => 'https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&prompt=consent&', |
| 140 |
'scope' => 'https%3A%2F%2Fmail.google.com%2F', |
| 141 |
], |
| 142 |
'smtp-mail.outlook.com' => [ |
| 143 |
'baseUrl' => 'https://login.microsoftonline.com/%s/oauth2/v2.0/authorize?response_mode=query&', |
| 144 |
'scope' => 'openid%20offline_access%20https%3A%2F%2Fgraph.microsoft.com%2Fmail.read%20https%3A%2F%2Foutlook.office.com%2FSMTP.Send', |
| 145 |
], |
| 146 |
'smtp.office365.com' => [ |
| 147 |
'baseUrl' => 'https://login.microsoftonline.com/%s/oauth2/v2.0/authorize?response_mode=query&', |
| 148 |
'scope' => 'openid%20offline_access%20https%3A%2F%2Fgraph.microsoft.com%2Fmail.read%20https%3A%2F%2Foutlook.office.com%2FSMTP.Send', |
| 149 |
], |
| 150 |
'imap.gmail.com' => [ |
| 151 |
'baseUrl' => 'https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&prompt=consent&', |
| 152 |
'scope' => 'https%3A%2F%2Fmail.google.com%2F', |
| 153 |
], |
| 154 |
'outlook.office365.com' => [ |
| 155 |
'baseUrl' => 'https://login.microsoftonline.com/%s/oauth2/v2.0/authorize?', |
| 156 |
'scope' => 'offline_access+https%3A%2F%2Foutlook.office.com%2FIMAP.AccessAsUser.All', |
| 157 |
], |
| 158 |
]; |
| 159 |
|
| 160 |
$this->store(); |
| 161 |
|
| 162 |
$this->config->save(['oauth_auth_type' => $isSmtp ? 'smtp' : 'bounce']); |
| 163 |
|
| 164 |
$host = strtolower($this->config->get($isSmtp ? 'smtp_host' : 'bounce_server')); |
| 165 |
$clientId = $this->config->get($isSmtp ? 'smtp_clientId' : 'bounce_client_id'); |
| 166 |
$clientSecret = $this->config->get($isSmtp ? 'smtp_secret' : 'bounce_client_secret'); |
| 167 |
if ($isSmtp) { |
| 168 |
$redirect_url = $this->config->get('smtp_redirectUrl'); |
| 169 |
} else { |
| 170 |
$redirect_url = acym_baseURI(); |
| 171 |
} |
| 172 |
|
| 173 |
if (empty($clientId) || empty($clientSecret) || empty($host) || !array_key_exists($host, $auth2Smtp)) { |
| 174 |
$this->listing(); |
| 175 |
|
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
if (in_array($host, ['smtp.office365.com', 'smtp-mail.outlook.com', 'outlook.office365.com'])) { |
| 180 |
$tenant = $this->config->get($isSmtp ? 'smtp_tenant' : 'bounce_tenant', 'consumers'); |
| 181 |
if (empty($tenant)) { |
| 182 |
acym_enqueueMessage(acym_translation('ACYM_TENANT_FIELD_IS_MISSING'), 'error'); |
| 183 |
$this->listing(); |
| 184 |
|
| 185 |
return; |
| 186 |
} |
| 187 |
$auth2Smtp[$host]['baseUrl'] = sprintf($auth2Smtp[$host]['baseUrl'], $tenant); |
| 188 |
} |
| 189 |
|
| 190 |
$redirectLink = $auth2Smtp[$host]['baseUrl']; |
| 191 |
$redirectLink .= 'client_id='.urlencode($clientId); |
| 192 |
$redirectLink .= '&response_type=code'; |
| 193 |
$redirectLink .= '&redirect_uri='.urlencode($redirect_url); |
| 194 |
$redirectLink .= '&scope='.$auth2Smtp[$host]['scope']; |
| 195 |
$redirectLink .= '&state=acymailing'; |
| 196 |
|
| 197 |
acym_redirect($redirectLink); |
| 198 |
} |
| 199 |
|
| 200 |
public function getAccessToken(): void |
| 201 |
{ |
| 202 |
$isBounce = $this->config->get('oauth_auth_type', 'bounce') == 'bounce'; |
| 203 |
$code = acym_getVar('string', 'code'); |
| 204 |
$clientId = trim($this->config->get($isBounce ? 'bounce_client_id' : 'smtp_clientId')); |
| 205 |
$secret = trim($this->config->get($isBounce ? 'bounce_client_secret' : 'smtp_secret')); |
| 206 |
$host = strtolower(trim($this->config->get($isBounce ? 'bounce_server' : 'smtp_host'))); |
| 207 |
if ($isBounce) { |
| 208 |
$redirectUrl = acym_baseURI(); |
| 209 |
} else { |
| 210 |
$smtpRedirectUrl = trim($this->config->get('smtp_redirectUrl')); |
| 211 |
$redirectUrl = empty($smtpRedirectUrl) ? acym_baseURI() : $smtpRedirectUrl; |
| 212 |
} |
| 213 |
$scope = ''; |
| 214 |
|
| 215 |
if (empty($clientId) || empty($secret) || empty($code)) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
if (in_array($host, ['imap.gmail.com', 'smtp.gmail.com'])) { |
| 220 |
$url = 'https://oauth2.googleapis.com/token'; |
| 221 |
} else { |
| 222 |
$tenant = trim($this->config->get($isBounce ? 'bounce_tenant' : 'smtp_tenant', 'consumers')); |
| 223 |
if (empty($tenant)) { |
| 224 |
acym_enqueueMessage(acym_translation('ACYM_TENANT_FIELD_IS_MISSING'), 'error'); |
| 225 |
} |
| 226 |
$url = 'https://login.microsoftonline.com/'.$tenant.'/oauth2/v2.0/token'; |
| 227 |
$scope = 'https://outlook.office.com/IMAP.AccessAsUser.All'; |
| 228 |
} |
| 229 |
|
| 230 |
$params = ['client_id' => $clientId, 'grant_type' => 'authorization_code', 'client_secret' => $secret, 'code' => $code, 'redirect_uri' => $redirectUrl]; |
| 231 |
|
| 232 |
if (!empty($scope)) { |
| 233 |
$params['scope'] = $scope; |
| 234 |
} |
| 235 |
|
| 236 |
$requestOption = [ |
| 237 |
'method' => 'POST', |
| 238 |
'data' => $params, |
| 239 |
]; |
| 240 |
|
| 241 |
$response = acym_makeCurlCall($url, $requestOption); |
| 242 |
|
| 243 |
acym_logError('Response from OAuth call: '.json_encode($response), 'imap_oauth'); |
| 244 |
|
| 245 |
if (empty($response['error'])) { |
| 246 |
$token = $response['token_type'].' '.$response['access_token']; |
| 247 |
$expireIn = time() + (int)$response['expires_in']; |
| 248 |
|
| 249 |
if ($isBounce) { |
| 250 |
$config = ['bounce_token' => $token, 'bounce_token_expireIn' => $expireIn]; |
| 251 |
} else { |
| 252 |
$config = ['smtp_token' => $token, 'smtp_token_expireIn' => $expireIn]; |
| 253 |
} |
| 254 |
|
| 255 |
if (!empty($response['refresh_token'])) { |
| 256 |
if ($isBounce) { |
| 257 |
$config['bounce_refresh_token'] = $response['refresh_token']; |
| 258 |
} else { |
| 259 |
$config['smtp_refresh_token'] = $response['refresh_token']; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
$config['oauth_auth_type'] = ''; |
| 264 |
$this->config->save($config); |
| 265 |
|
| 266 |
acym_enqueueMessage(acym_translation('ACYM_SMTP_OAUTH_OK'), 'info'); |
| 267 |
} else { |
| 268 |
acym_enqueueMessage(acym_translationSprintf('ACYM_SMTP_OAUTH_ERROR', $response['error']), 'error', false); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|