PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 1.3.0
JetBackup – Backup, Restore & Migrate v1.3.0
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 6 years ago Dropbox 6 years ago Request 6 years ago SGArchive.php 6 years ago SGAuthClient.php 6 years ago SGCallback.php 6 years ago SGCdrEntry.php 6 years ago SGCharsetHandler.php 6 years ago SGDBState.php 6 years ago SGEntry.php 6 years ago SGFileEntry.php 6 years ago SGFileState.php 6 years ago SGMigrateState.php 6 years ago SGMysqldump.php 6 years ago SGReloadHandler.php 6 years ago SGReloader.php 6 years ago SGReloaderState.php 6 years ago SGReviewManager.php 6 years ago SGState.php 6 years ago SGUploadHandler.php 6 years ago SGUploadState.php 6 years ago
SGAuthClient.php
272 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
12 private function __construct()
13 {
14 $this->accessToken = SGConfig::get('SG_BACKUPGUARD_ACCESS_TOKEN', true);
15 $this->accessTokenExpires = SGConfig::get('SG_BACKUPGUARD_ACCESS_TOKEN_EXPIRES', true);
16
17 $this->client = new BackupGuard\Client($this->accessToken);
18 }
19
20 private function __clone()
21 {
22
23 }
24
25 public static function getInstance()
26 {
27 if (!self::$instance) {
28 self::$instance = new self();
29 }
30
31 return self::$instance;
32 }
33
34 public function getAccessToken()
35 {
36 return $this->accessToken;
37 }
38
39 public function login($email, $password)
40 {
41 try {
42 $accessToken = $this->createAccessToken($email, $password);
43 }
44 catch (BackupGuard\Exception $ex) {
45 return false;
46 }
47
48 $this->client->setAccessToken($accessToken);
49
50 return true;
51 }
52
53 public function logout()
54 {
55 $this->setTokens(); //reset all tokens
56 $this->client->setAccessToken(null);
57 return true;
58 }
59
60 public function getCurrentUser()
61 {
62 try {
63 $user = $this->client->getCurrentUser();
64 }
65 catch (BackupGuard\Exception $ex) {
66 return false;
67 }
68
69 return $user;
70 }
71
72 public function validateUrl($url)
73 {
74 if (!$this->prepareAuthorizedRequest()) {
75 return -1;
76 }
77
78 try {
79 $result = $this->client->validateUrl($url, SG_PRODUCT_IDENTIFIER);
80 }
81 catch (BackupGuard\Exception $ex) {
82 $result = $this->handleUnauthorizedException($ex);
83 if ($result === true) { //we can try again
84 $result = $this->validateUrl($url);
85 }
86 }
87
88 return $result;
89 }
90
91 public function getAllUserProducts()
92 {
93 if (!$this->prepareAuthorizedRequest()) {
94 return -1;
95 }
96
97 try {
98 $result = $this->client->getAllUserProducts(SG_PRODUCT_IDENTIFIER);
99 }
100 catch (BackupGuard\Exception $ex) {
101 $result = $this->handleUnauthorizedException($ex);
102 if ($result === true) { //we can try again
103 $result = $this->getAllUserProducts();
104 }
105 }
106
107 return $result;
108 }
109
110 public function isAnyLicenseAvailable($products)
111 {
112 if (empty($products) || $products == -1) {
113 return false;
114 }
115 foreach ($products as $product) {
116 if (!$product['licenses']) {
117 return true;
118 }
119 $availableLicenses = $product['licenses']-$product['used_licenses'];
120 if ($availableLicenses > 0) {
121 return true;
122 }
123 }
124
125 return false;
126 }
127
128 public function linkUrlToProduct($url, $userProductId, &$error)
129 {
130 if (!$this->prepareAuthorizedRequest()) {
131 return -1;
132 }
133
134 try {
135 $result = $this->client->linkUrlToProduct($url, $userProductId);
136 }
137 catch (BackupGuard\Exception $ex) {
138 $result = $this->handleUnauthorizedException($ex);
139 if ($result === true) { //we can try again
140 $result = $this->linkUrlToProduct($url, $userProductId);
141 }
142
143 $error = $ex->getMessage();
144 }
145
146 return $result;
147 }
148
149 public function filterUpdateChecks($options)
150 {
151 //we need to be sure that access token is fresh before checking for updates
152 $this->prepareAuthorizedRequest();
153
154 $options['headers']['access_token'] = $this->getAccessToken();
155
156 return $options;
157 }
158
159 private function handleUnauthorizedException($ex)
160 {
161 if ($ex instanceof BackupGuard\UnauthorizedException) {
162 //access token has expired or is invalid, refresh it
163 if ($this->refreshAccessToken()) {
164 return true;
165 }
166 else {
167 return -1; //could not refresh token, login is required
168 }
169 }
170
171 return false;
172 }
173
174 private function prepareAuthorizedRequest()
175 {
176 //no access token found, login is required
177 if (!$this->accessToken) {
178 return false;
179 }
180
181 //access token is expired, try to refresh it
182 if (time() > $this->accessTokenExpires) {
183 if (!$this->refreshAccessToken()) {
184 return false;
185 }
186 }
187
188 return true;
189 }
190
191 private function setTokens($accessToken = '', $accessTokenExpires = 0, $refreshToken = '')
192 {
193 $this->accessToken = $accessToken;
194 $this->accessTokenExpires = $accessTokenExpires;
195 $this->client->setAccessToken($accessToken);
196
197 SGConfig::set('SG_BACKUPGUARD_ACCESS_TOKEN', $accessToken, true);
198 SGConfig::set('SG_BACKUPGUARD_ACCESS_TOKEN_EXPIRES', $accessTokenExpires, true);
199
200 SGConfig::set('SG_BACKUPGUARD_REFRESH_TOKEN', $refreshToken, true);
201 }
202
203 private function createAccessToken($email, $password)
204 {
205 $tokens = $this->client->createAccessToken(
206 SG_BACKUPGUARD_CLIENT_ID,
207 SG_BACKUPGUARD_CLIENT_SECRET,
208 $email,
209 $password
210 );
211
212 $this->setTokens(
213 $tokens['access_token'],
214 time()+BackupGuard\Config::TOKEN_EXPIRES,
215 $tokens['refresh_token']
216 );
217
218 return $tokens['access_token'];
219 }
220
221 private function refreshAccessToken()
222 {
223 $refreshToken = SGConfig::get('SG_BACKUPGUARD_REFRESH_TOKEN', true);
224 if (!$refreshToken) {
225 $this->logout();
226 return false;
227 }
228
229 try {
230 $tokens = $this->client->refreshAccessToken(
231 SG_BACKUPGUARD_CLIENT_ID,
232 SG_BACKUPGUARD_CLIENT_SECRET,
233 $refreshToken
234 );
235 }
236 catch (BackupGuard\Exception $ex) { //for some reason the refresh token doesn't work
237 $this->logout();
238 return false;
239 }
240
241 $this->setTokens(
242 $tokens['access_token'],
243 time()+BackupGuard\Config::TOKEN_EXPIRES,
244 $tokens['refresh_token']
245 );
246
247 return $tokens['access_token'];
248 }
249
250 // Added by Nerses
251 public function getMerchantOrderId()
252 {
253 if (!$this->prepareAuthorizedRequest()) {
254 return -1;
255 }
256
257 try {
258 $result = $this->client->getMerchantOrderId(SG_PRODUCT_IDENTIFIER);
259 }
260 catch (BackupGuard\Exception $ex) {
261 $result = $this->handleUnauthorizedException($ex);
262 if ($result === true) { //we can try again
263 $result = $this->getMerchantOrderId();
264 }
265
266 $error = $ex->getMessage();
267 }
268
269 return $result;
270 }
271 }
272