| @@ -20,8 +20,10 @@ | ||
| 20 | 20 | use Subscription; |
| 21 | 21 | use License; |
| 22 | 22 | use Language; |
| 23 | 23 | |
| 24 | + const CRON_TASK_NAME = 'acymailing_automated_tasks_cron_task'; | |
| 25 | + | |
| 24 | 26 | public function __construct() |
| 25 | 27 | { |
| 26 | 28 | parent::__construct(); |
| 27 | 29 | $this->breadcrumb[acym_translation('ACYM_CONFIGURATION')] = acym_completeLink('configuration'); |
| @@ -32,39 +34,10 @@ | ||
| 32 | 34 | ], |
| 33 | 35 | ]; |
| 34 | 36 | } |
| 35 | 37 | |
| 36 | - public function getAjax() | |
| 38 | + public function displayMessage(string $message, bool $ajax = false): array | |
| 37 | 39 | { |
| 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($message, $ajax = false) | |
| 66 | - { | |
| 67 | 40 | $correspondences = [ |
| 68 | 41 | 'WEBSITE_NOT_FOUND' => ['message' => 'ACYM_WEBSITE_NOT_FOUND', 'type' => 'error'], |
| 69 | 42 | 'LICENSE_NOT_FOUND' => ['message' => 'ACYM_LICENSE_NOT_FOUND', 'type' => 'error'], |
| 70 | 43 | 'WELL_ATTACH' => ['message' => 'ACYM_LICENSE_WELL_ATTACH', 'type' => 'info'], |
| @@ -83,16 +56,22 @@ | ||
| 83 | 56 | if (!$ajax) { |
| 84 | 57 | if (empty($message) || empty($correspondences[$message])) { |
| 85 | 58 | acym_enqueueMessage(acym_translation('ACYM_ERROR_ON_CALL_ACYBA_WEBSITE'), 'error'); |
| 86 | 59 | |
| 87 | - if (!empty($message)) acym_enqueueMessage(acym_translationSprintf('ACYM_CURL_ERROR_MESSAGE', $message), 'error'); | |
| 60 | + if (!empty($message)) { | |
| 61 | + $notification = [ | |
| 62 | + 'name' => 'curl_error', | |
| 63 | + 'removable' => 1, | |
| 64 | + ]; | |
| 65 | + acym_enqueueMessage(acym_translationSprintf('ACYM_CURL_ERROR_MESSAGE', $message), 'error', true, [$notification]); | |
| 66 | + } | |
| 88 | 67 | |
| 89 | - return false; | |
| 68 | + return []; | |
| 90 | 69 | } |
| 91 | 70 | |
| 92 | 71 | acym_enqueueMessage(acym_translation($correspondences[$message]['message']), $correspondences[$message]['type']); |
| 93 | 72 | |
| 94 | - return $correspondences[$message]['type'] == 'info'; | |
| 73 | + return $correspondences[$message]['type'] === 'info' ? $correspondences[$message] : []; | |
| 95 | 74 | } else { |
| 96 | 75 | if (empty($message) || empty($correspondences[$message])) { |
| 97 | 76 | $response = ['message' => acym_translation('ACYM_ERROR_ON_CALL_ACYBA_WEBSITE'), 'type' => 'error']; |
| 98 | 77 | |
| @@ -107,14 +86,14 @@ | ||
| 107 | 86 | return $response; |
| 108 | 87 | } |
| 109 | 88 | } |
| 110 | 89 | |
| 111 | - function seeLogs() | |
| 90 | + public function seeLogs(): void | |
| 112 | 91 | { |
| 113 | - $filename = acym_getVar('string', 'filename'); | |
| 92 | + $filename = acym_getVar('string', 'filename', ''); | |
| 114 | 93 | |
| 115 | 94 | if (empty($filename) || !acym_fileNameValid($filename)) { |
| 116 | - echo acym_translation('ACYM_FILENAME_EMPTY_OR_NOT_VALID'); | |
| 95 | + echo esc_html(acym_translation('ACYM_FILENAME_EMPTY_OR_NOT_VALID')); | |
| 117 | 96 | exit; |
| 118 | 97 | } |
| 119 | 98 | |
| 120 | 99 | $reportPath = acym_getLogPath($filename); |
| @@ -119,9 +98,9 @@ | ||
| 119 | 98 | |
| 120 | 99 | $reportPath = acym_getLogPath($filename); |
| 121 | 100 | |
| 122 | 101 | if (!file_exists($reportPath)) { |
| 123 | - echo acym_translation('ACYM_EXIST_LOG'); | |
| 102 | + echo esc_html(acym_translation('ACYM_EXIST_LOG')); | |
| 124 | 103 | exit; |
| 125 | 104 | } |
| 126 | 105 | |
| 127 | 106 | if (ACYM_CMS === 'wordpress') @ob_get_clean(); |
| @@ -126,146 +105,39 @@ | ||
| 126 | 105 | |
| 127 | 106 | if (ACYM_CMS === 'wordpress') @ob_get_clean(); |
| 128 | 107 | |
| 129 | 108 | $final = acym_fileGetContent($reportPath); |
| 130 | - echo nl2br($final); | |
| 109 | + echo nl2br(esc_html($final)); | |
| 131 | 110 | |
| 132 | 111 | exit; |
| 133 | 112 | } |
| 134 | 113 | |
| 135 | - private function loginForOAuth2(bool $isSmtp = true) | |
| 114 | + private function loginForOAuth2(bool $isSmtp = true): void | |
| 136 | 115 | { |
| 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 | - | |
| 116 | + // Step 1: prompt consent window to the user | |
| 160 | 117 | $this->store(); |
| 161 | 118 | |
| 162 | - $this->config->save(['oauth_auth_type' => $isSmtp ? 'smtp' : 'bounce']); | |
| 119 | + $redirectUrl = ''; | |
| 120 | + acym_trigger('onAcymOauthAuthenticate', [&$redirectUrl, $isSmtp]); | |
| 163 | 121 | |
| 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)) { | |
| 122 | + if (empty($redirectUrl)) { | |
| 174 | 123 | $this->listing(); |
| 175 | 124 | |
| 176 | 125 | return; |
| 177 | 126 | } |
| 178 | 127 | |
| 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); | |
| 128 | + acym_redirect($redirectUrl, '', 'message', false); | |
| 198 | 129 | } |
| 199 | 130 | |
| 200 | - public function getAccessToken() | |
| 131 | + public function handleOauthAuthentication(): void | |
| 201 | 132 | { |
| 202 | - $isBounce = $this->config->get('oauth_auth_type', 'bounce') == 'bounce'; | |
| 133 | + // Step 2: get the access and refresh tokens after the user is redirected to the config with a code in the URL | |
| 203 | 134 | $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 | 135 | |
| 215 | - if (empty($clientId) || empty($secret) || empty($code)) { | |
| 136 | + if (empty($code)) { | |
| 216 | 137 | return; |
| 217 | 138 | } |
| 218 | 139 | |
| 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 | - } | |
| 140 | + $isSmtp = acym_getVar('string', 'auth_type', 'smtp') === 'smtp'; | |
| 141 | + acym_trigger('onAcymOauthCredentialsCreation', [$isSmtp, $code]); | |
| 270 | 142 | } |
| 271 | 143 | } |