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