| @@ -1,0 +1,261 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +use AcyMailing\Core\AcymPlugin; | |
| 4 | +use AcyMailing\Helpers\MailerHelper; | |
| 5 | + | |
| 6 | +class plgAcymGoogle extends AcymPlugin | |
| 7 | +{ | |
| 8 | + const SENDING_METHOD_ID = 'google'; | |
| 9 | + const SENDING_METHOD_NAME = 'Google'; | |
| 10 | + | |
| 11 | + public function __construct() | |
| 12 | + { | |
| 13 | + parent::__construct(); | |
| 14 | + $this->pluginDescription->name = acym_translation('ACYM_GOOGLE'); | |
| 15 | + } | |
| 16 | + | |
| 17 | + public function onAcymGetSendingMethods(&$data, $isMailer = false) | |
| 18 | + { | |
| 19 | + if ($isMailer) { | |
| 20 | + return; | |
| 21 | + } | |
| 22 | + | |
| 23 | + $data['sendingMethods'][self::SENDING_METHOD_ID] = [ | |
| 24 | + 'name' => $this->pluginDescription->name, | |
| 25 | + 'image' => ACYM_IMAGES.'mailers/google.svg', | |
| 26 | + ]; | |
| 27 | + } | |
| 28 | + | |
| 29 | + public function onAcymGetSendingMethodsHtmlSetting(&$data) | |
| 30 | + { | |
| 31 | + $username = $this->config->get('google_username', $this->config->get('from_email')); | |
| 32 | + $setupDocumentation = ACYM_DOCUMENTATION.'setup/configuration/mail-configuration/set-up-oauth-2.0'; | |
| 33 | + $redirectUrl = trim($this->config->get('google_redirect_url', acym_baseURI())); | |
| 34 | + | |
| 35 | + $refreshToken = $this->config->get('google_refresh_token'); | |
| 36 | + $refreshTokenExpiration = $this->config->get('google_refresh_token_expiration'); | |
| 37 | + $mustAuthenticate = empty($refreshToken) || (!empty($refreshTokenExpiration) && $refreshTokenExpiration < time()); | |
| 38 | + ?> | |
| 39 | + <div class="send_settings grid-x cell large-6 xlarge-5 xxlarge-4 margin-auto" id="<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_settings"> | |
| 40 | + <div class="acym_port_465_closed is-hidden"> | |
| 41 | + <?php acym_display(acym_translation('ACYM_PORT_NEEDED'), 'error', false); ?> | |
| 42 | + </div> | |
| 43 | + <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> | |
| 44 | + <label for="google_username" class="cell"> | |
| 45 | + <?php echo esc_html(acym_translation('ACYM_SMTP_USERNAME')); | |
| 46 | + acym_info(['textShownInTooltip' => 'ACYM_SMTP_USERNAME_DESC']); ?> | |
| 47 | + </label> | |
| 48 | + <input id="google_username" | |
| 49 | + class="cell" | |
| 50 | + type="text" | |
| 51 | + name="config[google_username]" | |
| 52 | + value="<?php echo esc_attr($username); ?>"> | |
| 53 | + </div> | |
| 54 | + <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> | |
| 55 | + <label for="google_client_id" class="cell"><?php acym_externalLink('ACYM_SMTP_CLIENT_ID', $setupDocumentation, false); ?></label> | |
| 56 | + <input id="google_client_id" | |
| 57 | + class="cell" | |
| 58 | + type="text" | |
| 59 | + name="config[google_client_id]" | |
| 60 | + value="<?php echo esc_attr($this->config->get('google_client_id')); ?>"> | |
| 61 | + </div> | |
| 62 | + <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> | |
| 63 | + <label for="google_client_secret" class="cell"><?php acym_externalLink('ACYM_SMTP_CLIENT_SECRET', $setupDocumentation, false); ?></label> | |
| 64 | + <input id="google_client_secret" | |
| 65 | + class="cell" | |
| 66 | + type="text" | |
| 67 | + name="config[google_client_secret]" | |
| 68 | + value="<?php echo esc_attr($this->config->get('google_client_secret')); ?>"> | |
| 69 | + </div> | |
| 70 | + <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> | |
| 71 | + <label for="google_redirect_url" class="cell"><?php echo esc_html(acym_translation('ACYM_SMTP_REDIRECT_URL')); ?></label> | |
| 72 | + <input id="google_redirect_url" | |
| 73 | + class="cell" | |
| 74 | + type="text" | |
| 75 | + name="config[google_redirect_url]" | |
| 76 | + value="<?php echo esc_attr($redirectUrl); ?>"> | |
| 77 | + </div> | |
| 78 | + <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> | |
| 79 | + <?php if ($mustAuthenticate) { ?> | |
| 80 | + <button data-before-action="configSave" | |
| 81 | + data-task="loginForOAuth2Smtp" | |
| 82 | + class="button acy_button_submit margin-next-1"> | |
| 83 | + <?php echo esc_html(acym_translation('ACYM_AUTHENTICATE')); ?> | |
| 84 | + </button> | |
| 85 | + <?php $this->getCopySettingsButton($data, self::SENDING_METHOD_ID, 'wp_mail_smtp', false); ?> | |
| 86 | + <?php } else { ?> | |
| 87 | + <button data-before-action="configSave" | |
| 88 | + data-task="logoutForOAuth2Smtp" | |
| 89 | + class="button acy_button_submit button-secondary margin-bottom-1"> | |
| 90 | + <?php echo esc_html(acym_translation('ACYM_REVOKE_PERMISSIONS')); ?> | |
| 91 | + </button> | |
| 92 | + <?php | |
| 93 | + if (!empty($refreshTokenExpiration)) { | |
| 94 | + echo esc_html(acym_translationSprintf('ACYM_AUTHENTICATION_WILL_EXPIRE', acym_date($refreshTokenExpiration, 'ACYM_DATE_FORMAT_LC2'))); | |
| 95 | + } | |
| 96 | + ?> | |
| 97 | + <?php } ?> | |
| 98 | + </div> | |
| 99 | + </div> | |
| 100 | + <?php | |
| 101 | + } | |
| 102 | + | |
| 103 | + public function onAcymGetSettingsSendingMethodFromPlugin(&$data, $plugin, $method) | |
| 104 | + { | |
| 105 | + if ($method !== self::SENDING_METHOD_ID) { | |
| 106 | + return; | |
| 107 | + } | |
| 108 | + | |
| 109 | + if (ACYM_CMS === 'wordpress' && $plugin === 'wp_mail_smtp') { | |
| 110 | + $wpMailSmtpSetting = get_option('wp_mail_smtp', ''); | |
| 111 | + | |
| 112 | + if (empty($wpMailSmtpSetting['gmail'])) { | |
| 113 | + return; | |
| 114 | + } | |
| 115 | + | |
| 116 | + $data['google_username'] = $wpMailSmtpSetting['gmail']['user_details']['email'] ?? ''; | |
| 117 | + $data['google_client_id'] = $wpMailSmtpSetting['gmail']['client_id'] ?? ''; | |
| 118 | + $data['google_client_secret'] = $wpMailSmtpSetting['gmail']['client_secret'] ?? ''; | |
| 119 | + } | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * Doc: https://developers.google.com/identity/protocols/oauth2/web-server#httprest_2 | |
| 124 | + */ | |
| 125 | + public function onAcymOauthAuthenticate(string &$consentUrl, bool $isSmtp): void | |
| 126 | + { | |
| 127 | + if ($isSmtp) { | |
| 128 | + if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) { | |
| 129 | + return; | |
| 130 | + } | |
| 131 | + | |
| 132 | + $clientId = $this->config->get('google_client_id'); | |
| 133 | + $clientSecret = $this->config->get('google_client_secret'); | |
| 134 | + $redirectUrl = $this->config->get('google_redirect_url'); | |
| 135 | + } else { | |
| 136 | + if ($this->config->get('bounce_server') !== 'imap.gmail.com') { | |
| 137 | + return; | |
| 138 | + } | |
| 139 | + | |
| 140 | + $clientId = $this->config->get('bounce_client_id'); | |
| 141 | + $clientSecret = $this->config->get('bounce_client_secret'); | |
| 142 | + $redirectUrl = acym_baseURI(); | |
| 143 | + } | |
| 144 | + | |
| 145 | + if (empty($clientId) || empty($clientSecret) || empty($redirectUrl)) { | |
| 146 | + acym_enqueueMessage(acym_translation('ACYM_PLEASE_FILL_CLIENT_ID_SECRET'), 'error', false); | |
| 147 | + | |
| 148 | + return; | |
| 149 | + } | |
| 150 | + | |
| 151 | + $consentUrl = 'https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&prompt=consent&'; | |
| 152 | + $consentUrl .= 'client_id='.urlencode($clientId); | |
| 153 | + $consentUrl .= '&response_type=code'; | |
| 154 | + $consentUrl .= '&redirect_uri='.urlencode($redirectUrl); | |
| 155 | + $consentUrl .= '&scope='.urlencode('https://mail.google.com/'); | |
| 156 | + $consentUrl .= '&state=acymailing'.($isSmtp ? 'smtp' : 'bounce'); | |
| 157 | + } | |
| 158 | + | |
| 159 | + /** | |
| 160 | + * Doc: https://developers.google.com/identity/protocols/oauth2/web-server#httprest_3 | |
| 161 | + */ | |
| 162 | + public function onAcymOauthCredentialsCreation(bool $isSmtp, string $code): void | |
| 163 | + { | |
| 164 | + if ($isSmtp) { | |
| 165 | + if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) { | |
| 166 | + return; | |
| 167 | + } | |
| 168 | + | |
| 169 | + $clientId = trim($this->config->get('google_client_id')); | |
| 170 | + $clientSecret = trim($this->config->get('google_client_secret')); | |
| 171 | + $redirectUrl = trim($this->config->get('google_redirect_url', acym_baseURI())); | |
| 172 | + } else { | |
| 173 | + if ($this->config->get('bounce_server') !== 'imap.gmail.com') { | |
| 174 | + return; | |
| 175 | + } | |
| 176 | + | |
| 177 | + $clientId = trim($this->config->get('bounce_client_id')); | |
| 178 | + $clientSecret = trim($this->config->get('bounce_client_secret')); | |
| 179 | + $redirectUrl = acym_baseURI(); | |
| 180 | + } | |
| 181 | + | |
| 182 | + if (empty($clientId) || empty($clientSecret)) { | |
| 183 | + acym_enqueueMessage(acym_translation('ACYM_PLEASE_FILL_CLIENT_ID_SECRET'), 'error', false); | |
| 184 | + | |
| 185 | + return; | |
| 186 | + } | |
| 187 | + | |
| 188 | + $response = acym_makeCurlCall( | |
| 189 | + 'https://oauth2.googleapis.com/token', | |
| 190 | + [ | |
| 191 | + 'method' => 'POST', | |
| 192 | + 'data' => [ | |
| 193 | + 'grant_type' => 'authorization_code', | |
| 194 | + 'code' => $code, | |
| 195 | + 'client_id' => $clientId, | |
| 196 | + 'client_secret' => $clientSecret, | |
| 197 | + 'redirect_uri' => $redirectUrl, | |
| 198 | + ], | |
| 199 | + ] | |
| 200 | + ); | |
| 201 | + | |
| 202 | + acym_logError('Response from OAuth call: '.json_encode($response), self::SENDING_METHOD_ID); | |
| 203 | + | |
| 204 | + if (!empty($response['error'])) { | |
| 205 | + acym_enqueueMessage(acym_translationSprintf('ACYM_SMTP_OAUTH_ERROR', $response['error']), 'error', false); | |
| 206 | + | |
| 207 | + return; | |
| 208 | + } | |
| 209 | + | |
| 210 | + $expiringTime = time() + (int)$response['expires_in']; | |
| 211 | + | |
| 212 | + if (!empty($response['refresh_token_expires_in'])) { | |
| 213 | + $refreshExpiringTime = time() + (int)$response['refresh_token_expires_in']; | |
| 214 | + | |
| 215 | + acym_enqueueMessage( | |
| 216 | + acym_translationSprintf( | |
| 217 | + 'ACYM_OAUTH_TEMPORARY', | |
| 218 | + acym_date($refreshExpiringTime, 'ACYM_DATE_FORMAT_LC2') | |
| 219 | + ), | |
| 220 | + 'info', | |
| 221 | + false | |
| 222 | + ); | |
| 223 | + } else { | |
| 224 | + $refreshExpiringTime = 0; | |
| 225 | + acym_enqueueMessage(acym_translation('ACYM_SMTP_OAUTH_OK'), 'info'); | |
| 226 | + } | |
| 227 | + | |
| 228 | + if ($isSmtp) { | |
| 229 | + $newConfig = [ | |
| 230 | + 'google_access_token' => $response['access_token'], | |
| 231 | + 'google_access_token_expiration' => $expiringTime, | |
| 232 | + 'google_refresh_token' => $response['refresh_token'], | |
| 233 | + 'google_refresh_token_expiration' => $refreshExpiringTime, | |
| 234 | + ]; | |
| 235 | + } else { | |
| 236 | + $newConfig = [ | |
| 237 | + 'bounce_access_token' => $response['access_token'], | |
| 238 | + 'bounce_access_token_expiration' => $expiringTime, | |
| 239 | + 'bounce_refresh_token' => $response['refresh_token'], | |
| 240 | + 'bounce_refresh_token_expiration' => $refreshExpiringTime, | |
| 241 | + ]; | |
| 242 | + } | |
| 243 | + | |
| 244 | + $this->config->saveConfig($newConfig); | |
| 245 | + acym_config(true); | |
| 246 | + } | |
| 247 | + | |
| 248 | + public function onAcymOauthRevoke(): void | |
| 249 | + { | |
| 250 | + if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) { | |
| 251 | + return; | |
| 252 | + } | |
| 253 | + | |
| 254 | + $this->config->saveConfig( | |
| 255 | + [ | |
| 256 | + 'google_refresh_token' => '', | |
| 257 | + 'google_refresh_token_expiration' => '', | |
| 258 | + ] | |
| 259 | + ); | |
| 260 | + } | |
| 261 | +} | |