PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.1.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.1.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
← All changes | back/Libraries/Mailer/OAuth.php +0 -178 10.0.1 → 11.1.1 View file →
@@ -1,178 +1,0 @@
1 -<?php
2 -
3 -/**
4 - * PHPMailer - PHP email creation and transport class.
5 - * PHP Version 5.5.
6 - *
7 - * @see https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
8 - *
9 - * @author Marcus Bointon (Synchro/coolbru) <[email protected]>
10 - * @author Jim Jagielski (jimjag) <[email protected]>
11 - * @author Andy Prevost (codeworxtech) <[email protected]>
12 - * @author Brent R. Matzelle (original founder)
13 - * @copyright 2012 - 2020 Marcus Bointon
14 - * @copyright 2010 - 2012 Jim Jagielski
15 - * @copyright 2004 - 2009 Andy Prevost
16 - * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
17 - * @note This program is distributed in the hope that it will be useful - WITHOUT
18 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 - * FITNESS FOR A PARTICULAR PURPOSE.
20 - */
21 -
22 -namespace AcyMailerPhp;
23 -
24 -
25 -/**
26 - * OAuth - OAuth2 authentication wrapper class.
27 - * Uses the oauth2-client package from the League of Extraordinary Packages.
28 - *
29 - * @see http://oauth2-client.thephpleague.com
30 - *
31 - * @author Marcus Bointon (Synchro/coolbru) <[email protected]>
32 - */
33 -class OAuth implements OAuthTokenProvider
34 -{
35 - const HOST_AUTH_2 = ['smtp.gmail.com', 'smtp-mail.outlook.com', 'smtp.office365.com'];
36 -
37 - /**
38 - * An instance of the League OAuth Client Provider.
39 - *
40 - * @var AbstractProvider
41 - */
42 - protected $provider;
43 -
44 - /**
45 - * The current OAuth access token.
46 - *
47 - * @var AccessToken
48 - */
49 - protected $oauthToken;
50 -
51 - /**
52 - * The user's email address, usually used as the login ID
53 - * and also the from address when sending email.
54 - *
55 - * @var string
56 - */
57 - protected $oauthUserEmail = '';
58 -
59 - /**
60 - * The client secret, generated in the app definition of the service you're connecting to.
61 - *
62 - * @var string
63 - */
64 - protected $oauthClientSecret = '';
65 -
66 - /**
67 - * The client ID, generated in the app definition of the service you're connecting to.
68 - *
69 - * @var string
70 - */
71 - protected $oauthClientId = '';
72 -
73 - /**
74 - * The refresh token, used to obtain new AccessTokens.
75 - *
76 - * @var string
77 - */
78 - protected $oauthRefreshToken = '';
79 -
80 - protected $expiredIn = '';
81 -
82 - protected $host = '';
83 -
84 - /**
85 - * OAuth constructor.
86 - *
87 - * @param array $options Associative array containing
88 - * `provider`, `userName`, `clientSecret`, `clientId` and `refreshToken` elements
89 - */
90 - public function __construct($options)
91 - {
92 - $this->oauthUserEmail = $options['userName'];
93 - $this->oauthClientSecret = $options['clientSecret'];
94 - $this->oauthClientId = $options['clientId'];
95 - $this->oauthToken = $options['oauthToken'];
96 - $this->oauthRefreshToken = $options['refreshToken'];
97 - $this->expiredIn = $options['expiredIn'];
98 - $this->host = strtolower($options['host']);
99 - }
100 -
101 - /**
102 - * Get a new AccessToken.
103 - *
104 - * @return AccessToken
105 - */
106 - protected function getToken()
107 - {
108 - $config = acym_config();
109 - if ($this->host === 'smtp.gmail.com') {
110 - $url = 'https://oauth2.googleapis.com/token';
111 - } else {
112 - $tenant = $config->get('smtp_tenant');
113 - if (empty($tenant)) {
114 - acym_enqueueMessage(acym_translation('ACYM_TENANT_FIELD_IS_MISSING'), 'error');
115 - }
116 - $url = 'https://login.microsoftonline.com/'.$tenant.'/oauth2/v2.0/token';
117 - }
118 -
119 - $data = ['client_id' => $this->oauthClientId, 'grant_type' => 'refresh_token', 'refresh_token' => $this->oauthRefreshToken, 'client_secret' => $this->oauthClientSecret];
120 -
121 - $response = acym_makeCurlCall(
122 - $url,
123 - ['data' => $data, 'method' => 'POST']
124 - );
125 -
126 - if (empty($response['error'])) {
127 - $token = $response['token_type'].' '.$response['access_token'];
128 - $expireIn = time() + (int)$response['expires_in'];
129 - $config->save(['smtp_token' => $token, 'smtp_token_expireIn' => $expireIn]);
130 -
131 - return $token;
132 - } else {
133 - acym_enqueueMessage(acym_translationSprintf('ACYM_OAUTH_REFRESH_TOKEN_ERROR', $response['error']), 'error');
134 - }
135 - }
136 -
137 - /**
138 - * Generate a base64-encoded OAuth token.
139 - *
140 - * @return string
141 - */
142 - public function getOauth64()
143 - {
144 - //Get a new token if it's not available or has expired
145 - if ($this->tokenHasExpired() || empty($this->oauthToken)) {
146 - $this->oauthToken = $this->getToken();
147 - }
148 -
149 - return base64_encode(
150 - 'user='.
151 - $this->oauthUserEmail.
152 - "\001auth=".
153 - $this->oauthToken.
154 - "\001\001"
155 - );
156 - }
157 -
158 - private function tokenHasExpired()
159 - {
160 - $expired = false;
161 - if ($this->expiredIn < time()) {
162 - $expired = true;
163 - }
164 -
165 - return $expired;
166 - }
167 -
168 - static function hostRequireOauth($host, $connectionType)
169 - {
170 - $host = strtolower($host);
171 -
172 - if (!in_array($host, self::HOST_AUTH_2)) {
173 - return false;
174 - }
175 -
176 - return $connectionType !== 'password';
177 - }
178 -}