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
SGState.php
6 years ago
SGUploadHandler.php
6 years ago
SGUploadState.php
6 years ago
SGAuthClient.php
269 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 | foreach ($products as $product) { |
| 113 | if (!$product['licenses']) { |
| 114 | return true; |
| 115 | } |
| 116 | $availableLicenses = $product['licenses']-$product['used_licenses']; |
| 117 | if ($availableLicenses > 0) { |
| 118 | return true; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | public function linkUrlToProduct($url, $userProductId, &$error) |
| 126 | { |
| 127 | if (!$this->prepareAuthorizedRequest()) { |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | try { |
| 132 | $result = $this->client->linkUrlToProduct($url, $userProductId); |
| 133 | } |
| 134 | catch (BackupGuard\Exception $ex) { |
| 135 | $result = $this->handleUnauthorizedException($ex); |
| 136 | if ($result === true) { //we can try again |
| 137 | $result = $this->linkUrlToProduct($url, $userProductId); |
| 138 | } |
| 139 | |
| 140 | $error = $ex->getMessage(); |
| 141 | } |
| 142 | |
| 143 | return $result; |
| 144 | } |
| 145 | |
| 146 | public function filterUpdateChecks($options) |
| 147 | { |
| 148 | //we need to be sure that access token is fresh before checking for updates |
| 149 | $this->prepareAuthorizedRequest(); |
| 150 | |
| 151 | $options['headers']['access_token'] = $this->getAccessToken(); |
| 152 | |
| 153 | return $options; |
| 154 | } |
| 155 | |
| 156 | private function handleUnauthorizedException($ex) |
| 157 | { |
| 158 | if ($ex instanceof BackupGuard\UnauthorizedException) { |
| 159 | //access token has expired or is invalid, refresh it |
| 160 | if ($this->refreshAccessToken()) { |
| 161 | return true; |
| 162 | } |
| 163 | else { |
| 164 | return -1; //could not refresh token, login is required |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | private function prepareAuthorizedRequest() |
| 172 | { |
| 173 | //no access token found, login is required |
| 174 | if (!$this->accessToken) { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | //access token is expired, try to refresh it |
| 179 | if (time() > $this->accessTokenExpires) { |
| 180 | if (!$this->refreshAccessToken()) { |
| 181 | return false; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | private function setTokens($accessToken = '', $accessTokenExpires = 0, $refreshToken = '') |
| 189 | { |
| 190 | $this->accessToken = $accessToken; |
| 191 | $this->accessTokenExpires = $accessTokenExpires; |
| 192 | $this->client->setAccessToken($accessToken); |
| 193 | |
| 194 | SGConfig::set('SG_BACKUPGUARD_ACCESS_TOKEN', $accessToken, true); |
| 195 | SGConfig::set('SG_BACKUPGUARD_ACCESS_TOKEN_EXPIRES', $accessTokenExpires, true); |
| 196 | |
| 197 | SGConfig::set('SG_BACKUPGUARD_REFRESH_TOKEN', $refreshToken, true); |
| 198 | } |
| 199 | |
| 200 | private function createAccessToken($email, $password) |
| 201 | { |
| 202 | $tokens = $this->client->createAccessToken( |
| 203 | SG_BACKUPGUARD_CLIENT_ID, |
| 204 | SG_BACKUPGUARD_CLIENT_SECRET, |
| 205 | $email, |
| 206 | $password |
| 207 | ); |
| 208 | |
| 209 | $this->setTokens( |
| 210 | $tokens['access_token'], |
| 211 | time()+BackupGuard\Config::TOKEN_EXPIRES, |
| 212 | $tokens['refresh_token'] |
| 213 | ); |
| 214 | |
| 215 | return $tokens['access_token']; |
| 216 | } |
| 217 | |
| 218 | private function refreshAccessToken() |
| 219 | { |
| 220 | $refreshToken = SGConfig::get('SG_BACKUPGUARD_REFRESH_TOKEN', true); |
| 221 | if (!$refreshToken) { |
| 222 | $this->logout(); |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | try { |
| 227 | $tokens = $this->client->refreshAccessToken( |
| 228 | SG_BACKUPGUARD_CLIENT_ID, |
| 229 | SG_BACKUPGUARD_CLIENT_SECRET, |
| 230 | $refreshToken |
| 231 | ); |
| 232 | } |
| 233 | catch (BackupGuard\Exception $ex) { //for some reason the refresh token doesn't work |
| 234 | $this->logout(); |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | $this->setTokens( |
| 239 | $tokens['access_token'], |
| 240 | time()+BackupGuard\Config::TOKEN_EXPIRES, |
| 241 | $tokens['refresh_token'] |
| 242 | ); |
| 243 | |
| 244 | return $tokens['access_token']; |
| 245 | } |
| 246 | |
| 247 | // Added by Nerses |
| 248 | public function getMerchantOrderId() |
| 249 | { |
| 250 | if (!$this->prepareAuthorizedRequest()) { |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | try { |
| 255 | $result = $this->client->getMerchantOrderId(SG_PRODUCT_IDENTIFIER); |
| 256 | } |
| 257 | catch (BackupGuard\Exception $ex) { |
| 258 | $result = $this->handleUnauthorizedException($ex); |
| 259 | if ($result === true) { //we can try again |
| 260 | $result = $this->getMerchantOrderId(); |
| 261 | } |
| 262 | |
| 263 | $error = $ex->getMessage(); |
| 264 | } |
| 265 | |
| 266 | return $result; |
| 267 | } |
| 268 | } |
| 269 |