PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 1.6.9
JetBackup – Backup, Restore & Migrate v1.6.9
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / com / lib / SGAuthClient.php
backup / com / lib Last commit date
BackupGuard 5 years ago Dropbox 3 years ago Request 5 years ago SGArchive.php 4 years ago SGAuthClient.php 4 years ago SGCallback.php 5 years ago SGCdrEntry.php 9 years ago SGCharsetHandler.php 7 years ago SGDBState.php 8 years ago SGEntry.php 9 years ago SGFileEntry.php 5 years ago SGFileState.php 6 years ago SGMigrateState.php 8 years ago SGMysqldump.php 5 years ago SGReloadHandler.php 4 years ago SGReloader.php 5 years ago SGReloaderState.php 9 years ago SGReviewManager.php 6 years ago SGState.php 5 years ago SGStatsRequests.php 5 years ago SGUploadHandler.php 5 years ago SGUploadState.php 5 years ago
SGAuthClient.php
302 lines
1 <?php
2
3 require_once(dirname(__FILE__) . '/BackupGuard/Client.php');
4
5 class SGAuthClient
6 {
7 private static $_instance = null;
8 private $_client = null;
9 private $_accessToken = '';
10 private $_accessTokenExpires = 0;
11 private $_uploadAccessToken = '';
12 private $_uploadAccessTokenExpires = 0;
13
14 private function __construct()
15 {
16 $this->_accessToken = SGConfig::get('SG_BACKUPGUARD_ACCESS_TOKEN', true);
17 $this->_accessTokenExpires = SGConfig::get('SG_BACKUPGUARD_ACCESS_TOKEN_EXPIRES', true);
18 $this->_uploadAccessToken = SGConfig::get('SG_BACKUPGUARD_UPLOAD_ACCESS_TOKEN', true);
19 $this->_uploadAccessTokenExpires = SGConfig::get('SG_BACKUPGUARD_UPLOAD_ACCESS_TOKEN_EXPIRES', true);
20
21 $this->_client = new BackupGuard\Client($this->_accessToken);
22 }
23
24 private function __clone()
25 {
26 }
27
28 public static function getInstance()
29 {
30 if (!self::$_instance) {
31 self::$_instance = new self();
32 }
33
34 return self::$_instance;
35 }
36
37 public function getAccessToken()
38 {
39 return $this->_accessToken;
40 }
41
42 public function getUploadAccessToken()
43 {
44 return $this->_uploadAccessToken;
45 }
46
47 public function login($email, $password)
48 {
49 try {
50 $accessToken = $this->createAccessToken($email, $password);
51 } catch (BackupGuard\Exception $ex) {
52 return false;
53 }
54
55 $this->_client->setAccessToken($accessToken);
56
57 return true;
58 }
59
60 public function logout()
61 {
62 $this->setTokens(); //reset all tokens
63 $this->_client->setAccessToken(null);
64
65 return true;
66 }
67
68 public function getCurrentUser()
69 {
70 try {
71 $user = $this->_client->getCurrentUser();
72 } catch (BackupGuard\Exception $ex) {
73 return false;
74 }
75
76 return $user;
77 }
78
79 public function validateUrl($url)
80 {
81 if (!$this->prepareAuthorizedRequest()) {
82 return -1;
83 }
84
85 try {
86 $result = $this->_client->validateUrl($url, SG_PRODUCT_IDENTIFIER);
87 } catch (BackupGuard\Exception $ex) {
88 $result = $this->handleUnauthorizedException($ex);
89 if ($result === true) { //we can try again
90 $result = $this->validateUrl($url);
91 }
92 }
93
94 return $result;
95 }
96
97 public function getAllUserProducts()
98 {
99 if (!$this->prepareAuthorizedRequest()) {
100 return -1;
101 }
102
103 try {
104 $result = $this->_client->getAllUserProducts(SG_PRODUCT_IDENTIFIER);
105 } catch (BackupGuard\Exception $ex) {
106 $result = $this->handleUnauthorizedException($ex);
107 if ($result === true) { //we can try again
108 $result = $this->getAllUserProducts();
109 }
110 }
111
112 return $result;
113 }
114
115 public function isAnyLicenseAvailable($products)
116 {
117 if (empty($products) || $products == -1) {
118 return false;
119 }
120
121 foreach ($products as $product) {
122 if (!$product['licenses']) {
123 return true;
124 }
125
126 $availableLicenses = $product['licenses'] - $product['used_licenses'];
127 if ($availableLicenses > 0) {
128 return true;
129 }
130 }
131
132 return false;
133 }
134
135 public function linkUrlToProduct($url, $userProductId, &$error)
136 {
137 if (!$this->prepareAuthorizedRequest()) {
138 return -1;
139 }
140
141 try {
142 $result = $this->_client->linkUrlToProduct($url, $userProductId);
143 } catch (BackupGuard\Exception $ex) {
144 $result = $this->handleUnauthorizedException($ex);
145 if ($result === true) { //we can try again
146 $result = $this->linkUrlToProduct($url, $userProductId);
147 }
148
149 $error = $ex->getMessage();
150 }
151
152 return $result;
153 }
154
155 public function filterUpdateChecks($options)
156 {
157 //we need to be sure that access token is fresh before checking for updates
158 $this->prepareAuthorizedRequest();
159
160 $options['headers']['access_token'] = $this->getAccessToken();
161
162 return $options;
163 }
164
165 private function handleUnauthorizedException($ex)
166 {
167 if ($ex instanceof BackupGuard\UnauthorizedException) {
168 //access token has expired or is invalid, refresh it
169 if ($this->refreshAccessToken()) {
170 return true;
171 } else {
172 return -1; //could not refresh token, login is required
173 }
174 }
175
176 return false;
177 }
178
179 private function prepareAuthorizedRequest()
180 {
181 //no access token found, login is required
182 if (!$this->_accessToken) {
183 return false;
184 }
185
186 //access token is expired, try to refresh it
187 if (time() > $this->_accessTokenExpires) {
188 if (!$this->refreshAccessToken()) {
189 return false;
190 }
191 }
192
193 return true;
194 }
195
196 private function setTokens($accessToken = '', $accessTokenExpires = 0, $refreshToken = '')
197 {
198 $this->_accessToken = $accessToken;
199 $this->_accessTokenExpires = $accessTokenExpires;
200 $this->_client->setAccessToken($accessToken);
201
202 SGConfig::set('SG_BACKUPGUARD_ACCESS_TOKEN', $accessToken, true);
203 SGConfig::set('SG_BACKUPGUARD_ACCESS_TOKEN_EXPIRES', $accessTokenExpires, true);
204
205 SGConfig::set('SG_BACKUPGUARD_REFRESH_TOKEN', $refreshToken, true);
206 }
207
208 private function createAccessToken($email, $password)
209 {
210 $tokens = $this->_client->createAccessToken(
211 SG_BACKUPGUARD_CLIENT_ID,
212 SG_BACKUPGUARD_CLIENT_SECRET,
213 $email,
214 $password
215 );
216
217 $this->setTokens(
218 $tokens['access_token'],
219 time() + BackupGuard\Config::TOKEN_EXPIRES,
220 $tokens['refresh_token']
221 );
222
223 return $tokens['access_token'];
224 }
225
226 private function refreshAccessToken()
227 {
228 $refreshToken = SGConfig::get('SG_BACKUPGUARD_REFRESH_TOKEN', true);
229 if (!$refreshToken) {
230 $this->logout();
231
232 return false;
233 }
234
235 try {
236 $tokens = $this->_client->refreshAccessToken(
237 SG_BACKUPGUARD_CLIENT_ID,
238 SG_BACKUPGUARD_CLIENT_SECRET,
239 $refreshToken
240 );
241 } catch (BackupGuard\Exception $ex) { //for some reason the refresh token doesn't work
242 $this->logout();
243
244 return false;
245 }
246
247 $this->setTokens(
248 $tokens['access_token'],
249 time() + BackupGuard\Config::TOKEN_EXPIRES,
250 $tokens['refresh_token']
251 );
252
253 return $tokens['access_token'];
254 }
255
256 // Added by Nerses
257 public function getMerchantOrderId()
258 {
259 if (!$this->prepareAuthorizedRequest()) {
260 return -1;
261 }
262
263 try {
264 $result = $this->_client->getMerchantOrderId(SG_PRODUCT_IDENTIFIER);
265 } catch (BackupGuard\Exception $ex) {
266 $result = $this->handleUnauthorizedException($ex);
267 if ($result === true) { //we can try again
268 $result = $this->getMerchantOrderId();
269 }
270
271 // phpcs:disable
272 $error = $ex->getMessage();
273 // phpcs:enable
274 }
275
276 return $result;
277 }
278
279 public function createUploadAccessToken($email, $password)
280 {
281 $tokens = $this->_client->createUploadAccessToken(
282 SG_BACKUPGUARD_UPLOAD_CLIENT_ID,
283 SG_BACKUPGUARD_UPLOAD_CLIENT_SECRET,
284 $email,
285 $password,
286 SG_BACKUPGUARD_UPLOAD_SCOPE
287 );
288
289 $refreshToken = $tokens['refresh_token'];
290 $this->_uploadAccessToken = $tokens['access_token'];
291 $this->_uploadAccessTokenExpires = time() + BackupGuard\Config::TOKEN_EXPIRES;
292 $this->_client->setUploadAccessToken($this->_uploadAccessToken);
293
294 SGConfig::set('SG_BACKUPGUARD_UPLOAD_ACCESS_TOKEN', $this->_uploadAccessToken, true);
295 SGConfig::set('SG_BACKUPGUARD_UPLOAD_ACCESS_TOKEN_EXPIRES', $this->_uploadAccessTokenExpires, true);
296
297 SGConfig::set('SG_BACKUPGUARD_UPLOAD_REFRESH_TOKEN', $refreshToken, true);
298
299 return $tokens['access_token'];
300 }
301 }
302