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