PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.11.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.11.1
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / dynamics / outlook / plugin.php

plugin.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.11.1, at back/dynamics/outlook/plugin.php

294 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use AcyMailing\Core\AcymPlugin;
4 use AcyMailing\Helpers\MailerHelper;
5
6 class plgAcymOutlook extends AcymPlugin
7 {
8 const SENDING_METHOD_ID = 'outlook';
9 const SENDING_METHOD_NAME = 'Outlook';
10 const SCOPE_SMTP = 'openid offline_access https://outlook.office.com/SMTP.Send';
11 const SCOPE_IMAP = 'openid offline_access https://outlook.office.com/IMAP.AccessAsUser.All';
12
13 public function __construct()
14 {
15 parent::__construct();
16 $this->pluginDescription->name = acym_translation('ACYM_OUTLOOK');
17 }
18
19 public function onAcymGetSendingMethods(&$data, $isMailer = false)
20 {
21 if ($isMailer) {
22 return;
23 }
24
25 $data['sendingMethods'][self::SENDING_METHOD_ID] = [
26 'name' => $this->pluginDescription->name,
27 'image' => ACYM_IMAGES.'mailers/outlook.svg',
28 ];
29 }
30
31 public function onAcymGetSendingMethodsHtmlSetting(&$data)
32 {
33 $username = $this->config->get('outlook_username', $this->config->get('from_email'));
34 $setupDocumentation = ACYM_DOCUMENTATION.'setup/configuration/mail-configuration/set-up-oauth-2.0';
35 $redirectUrl = trim($this->config->get('outlook_redirect_url', acym_baseURI()));
36
37 $refreshToken = $this->config->get('outlook_refresh_token');
38 $refreshTokenExpiration = $this->config->get('outlook_refresh_token_expiration');
39 $mustAuthenticate = empty($refreshToken) || (!empty($refreshTokenExpiration) && $refreshTokenExpiration < time());
40
41 $mailerHelper = new MailerHelper();
42
43 ob_start();
44 ?>
45 <div class="send_settings grid-x cell large-6 xlarge-5 xxlarge-4 margin-auto" id="<?php echo self::SENDING_METHOD_ID; ?>_settings">
46 <div class="acym_port_465_closed is-hidden">
47 <?php acym_display(acym_translation('ACYM_PORT_NEEDED'), 'error', false); ?>
48 </div>
49 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
50 <label for="outlook_username" class="cell">
51 <?php echo acym_translation('ACYM_SMTP_USERNAME').acym_info(['textShownInTooltip' => 'ACYM_SMTP_USERNAME_DESC']); ?>
52 </label>
53 <input id="outlook_username"
54 class="cell"
55 type="text"
56 name="config[outlook_username]"
57 value="<?php echo acym_escape($username); ?>">
58 </div>
59 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
60 <label for="outlook_tenant" class="cell"><?php echo acym_externalLink('ACYM_TENANT', $setupDocumentation, false); ?></label>
61 <?php
62 $value = $this->config->get('outlook_tenant');
63 echo acym_select(
64 [
65 'common' => 'ACYM_ANY_ACCOUNT_TYPE',
66 'consumers' => 'ACYM_MICROSOFT_ACCOUNTS',
67 'organizations' => 'ACYM_ORGANIZATIONS',
68 ],
69 'config[outlook_tenant]',
70 empty($value) ? 'consumers' : $value,
71 [
72 'class' => 'acym__select',
73 ],
74 '',
75 '',
76 '',
77 true
78 );
79 ?>
80 </div>
81 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
82 <label for="outlook_client_id" class="cell"><?php echo acym_externalLink('ACYM_SMTP_CLIENT_ID', $setupDocumentation, false); ?></label>
83 <input id="outlook_client_id"
84 class="cell"
85 type="text"
86 name="config[outlook_client_id]"
87 value="<?php echo acym_escape($this->config->get('outlook_client_id')); ?>">
88 </div>
89 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
90 <label for="outlook_client_secret" class="cell"><?php echo acym_externalLink('ACYM_SMTP_CLIENT_SECRET', $setupDocumentation, false); ?></label>
91 <input id="outlook_client_secret"
92 class="cell"
93 type="text"
94 name="config[outlook_client_secret]"
95 value="<?php echo acym_escape($this->config->get('outlook_client_secret')); ?>">
96 </div>
97 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
98 <label for="outlook_redirect_url" class="cell"><?php echo acym_translation('ACYM_SMTP_REDIRECT_URL'); ?></label>
99 <input id="outlook_redirect_url"
100 class="cell"
101 type="text"
102 name="config[outlook_redirect_url]"
103 value="<?php echo acym_escape($redirectUrl); ?>">
104 </div>
105 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
106 <?php if ($mustAuthenticate) { ?>
107 <button data-before-action="configSave"
108 data-task="loginForOAuth2Smtp"
109 class="button acy_button_submit margin-next-1">
110 <?php echo acym_translation('ACYM_AUTHENTICATE'); ?>
111 </button>
112 <?php echo $this->getCopySettingsButton($data, self::SENDING_METHOD_ID, 'wp_mail_smtp', false); ?>
113 <?php } else { ?>
114 <button data-before-action="configSave"
115 data-task="logoutForOAuth2Smtp"
116 class="button acy_button_submit button-secondary margin-bottom-1">
117 <?php echo acym_translation('ACYM_REVOKE_PERMISSIONS'); ?>
118 </button>
119 <?php
120 if (!empty($refreshTokenExpiration)) {
121 echo acym_translationSprintf('ACYM_AUTHENTICATION_WILL_EXPIRE', acym_date($refreshTokenExpiration, 'ACYM_DATE_FORMAT_LC2'));
122 }
123 ?>
124 <?php } ?>
125 </div>
126 </div>
127 <?php
128 $data['sendingMethodsHtmlSettings'][self::SENDING_METHOD_ID] = ob_get_clean();
129 }
130
131 public function onAcymGetSettingsSendingMethodFromPlugin(&$data, $plugin, $method)
132 {
133 if ($method !== self::SENDING_METHOD_ID) {
134 return;
135 }
136
137 if (ACYM_CMS === 'wordpress' && $plugin === 'wp_mail_smtp') {
138 $wpMailSmtpSetting = get_option('wp_mail_smtp', '');
139
140 if (empty($wpMailSmtpSetting['outlook'])) {
141 return;
142 }
143
144 $data['outlook_client_id'] = $wpMailSmtpSetting['outlook']['client_id'] ?? '';
145 $data['outlook_client_secret'] = $wpMailSmtpSetting['outlook']['client_secret'] ?? '';
146 }
147 }
148
149 public function onAcymOauthAuthenticate(string &$consentUrl, bool $isSmtp): void
150 {
151 if ($isSmtp) {
152 if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) {
153 return;
154 }
155
156 $clientId = $this->config->get('outlook_client_id');
157 $clientSecret = $this->config->get('outlook_client_secret');
158 $tenant = $this->config->get('outlook_tenant');
159 $redirectUrl = $this->config->get('outlook_redirect_url');
160 $scope = self::SCOPE_SMTP;
161 } else {
162 if ($this->config->get('bounce_server') !== 'outlook.office365.com') {
163 return;
164 }
165
166 $clientId = $this->config->get('bounce_client_id');
167 $clientSecret = $this->config->get('bounce_client_secret');
168 $tenant = $this->config->get('bounce_tenant');
169 $redirectUrl = acym_baseURI();
170 $scope = self::SCOPE_IMAP;
171 }
172
173 if (empty($clientId) || empty($clientSecret) || empty($redirectUrl)) {
174 acym_enqueueMessage(acym_translation('ACYM_PLEASE_FILL_CLIENT_ID_SECRET'), 'error', false);
175
176 return;
177 }
178
179 $consentUrl = 'https://login.microsoftonline.com/'.$tenant.'/oauth2/v2.0/authorize';
180 $consentUrl .= '?response_mode=query';
181 $consentUrl .= '&client_id='.urlencode($clientId);
182 $consentUrl .= '&prompt=consent';
183 $consentUrl .= '&response_type=code';
184 $consentUrl .= '&redirect_uri='.urlencode($redirectUrl);
185 $consentUrl .= '&scope='.urlencode($scope);
186 $consentUrl .= '&state=acymailing'.($isSmtp ? 'smtp' : 'bounce');
187 }
188
189 public function onAcymOauthCredentialsCreation(bool $isSmtp, string $code): void
190 {
191 if ($isSmtp) {
192 if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) {
193 return;
194 }
195
196 $clientId = trim($this->config->get('outlook_client_id'));
197 $clientSecret = trim($this->config->get('outlook_client_secret'));
198 $tenant = trim($this->config->get('outlook_tenant', 'consumers'));
199 $redirectUrl = trim($this->config->get('outlook_redirect_url', acym_baseURI()));
200 $scope = self::SCOPE_SMTP;
201 } else {
202 if ($this->config->get('bounce_server') !== 'outlook.office365.com') {
203 return;
204 }
205
206 $clientId = trim($this->config->get('bounce_client_id'));
207 $clientSecret = trim($this->config->get('bounce_client_secret'));
208 $tenant = trim($this->config->get('bounce_tenant', 'consumers'));
209 $redirectUrl = acym_baseURI();
210 $scope = self::SCOPE_IMAP;
211 }
212
213 if (empty($clientId) || empty($clientSecret)) {
214 acym_enqueueMessage(acym_translation('ACYM_PLEASE_FILL_CLIENT_ID_SECRET'), 'error', false);
215
216 return;
217 }
218
219 $response = acym_makeCurlCall(
220 'https://login.microsoftonline.com/'.$tenant.'/oauth2/v2.0/token',
221 [
222 'method' => 'POST',
223 'data' => [
224 'grant_type' => 'authorization_code',
225 'code' => $code,
226 'client_id' => $clientId,
227 'client_secret' => $clientSecret,
228 'redirect_uri' => $redirectUrl,
229 'scope' => $scope,
230 ],
231 ]
232 );
233
234 acym_logError('Response from OAuth call: '.json_encode($response), self::SENDING_METHOD_ID);
235
236 if (!empty($response['error'])) {
237 acym_enqueueMessage(acym_translationSprintf('ACYM_SMTP_OAUTH_ERROR', $response['error']), 'error', false);
238
239 return;
240 }
241
242 $expiringTime = time() + (int)$response['expires_in'];
243
244 if (!empty($response['refresh_token_expires_in'])) {
245 $refreshExpiringTime = time() + (int)$response['refresh_token_expires_in'];
246
247 acym_enqueueMessage(
248 acym_translationSprintf(
249 'ACYM_OAUTH_TEMPORARY',
250 acym_date($refreshExpiringTime, 'ACYM_DATE_FORMAT_LC2')
251 ),
252 'info',
253 false
254 );
255 } else {
256 $refreshExpiringTime = 0;
257 acym_enqueueMessage(acym_translation('ACYM_SMTP_OAUTH_OK'), 'info');
258 }
259
260 if ($isSmtp) {
261 $newConfig = [
262 'outlook_access_token' => $response['access_token'],
263 'outlook_access_token_expiration' => $expiringTime,
264 'outlook_refresh_token' => $response['refresh_token'],
265 'outlook_refresh_token_expiration' => $refreshExpiringTime,
266 ];
267 } else {
268 $newConfig = [
269 'bounce_access_token' => $response['access_token'],
270 'bounce_access_token_expiration' => $expiringTime,
271 'bounce_refresh_token' => $response['refresh_token'],
272 'bounce_refresh_token_expiration' => $refreshExpiringTime,
273 ];
274 }
275
276 $this->config->saveConfig($newConfig);
277 acym_config(true);
278 }
279
280 public function onAcymOauthRevoke(): void
281 {
282 if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) {
283 return;
284 }
285
286 $this->config->saveConfig(
287 [
288 'outlook_refresh_token' => '',
289 'outlook_refresh_token_expiration' => '',
290 ]
291 );
292 }
293 }
294