PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 1.4.9
JetBackup – Backup, Restore & Migrate v1.4.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 / core / storage / BackupGuardStorage.php
backup / com / core / storage Last commit date
BackupGuardStorage.php 5 years ago SGDropbox.php 5 years ago SGDropboxStorage.php 5 years ago SGStorage.php 5 years ago
BackupGuardStorage.php
373 lines
1 <?php
2 namespace BackupGuard;
3
4 use \SGConfig;
5 use \SGExceptionForbidden;
6 require_once(SG_STORAGE_PATH.'SGStorage.php');
7 require_once(SG_LIB_PATH.'BackupGuard/Client.php');
8
9 class Storage extends \SGStorage
10 {
11 private $client = null;
12 private $accessToken = '';
13 private $refreshToken = '';
14
15 public function init()
16 {
17 $this->client = new Client();
18 $this->client->setUploadAccessToken(SGConfig::get('SG_BACKUPGUARD_UPLOAD_ACCESS_TOKEN'));
19 }
20
21 public function setAccessToken($accessToken)
22 {
23 $this->accessToken = $accessToken;
24 }
25
26 private function getRefreshToken()
27 {
28 return SGConfig::get('SG_BACKUPGUARD_UPLOAD_REFRESH_TOKEN', true);
29 }
30
31 private function getProfileId()
32 {
33 return SGConfig::get("BACKUP_GUARD_PROFILE_ID");
34 }
35
36 private function getAccessToken(&$expirationTs = 0)
37 {
38 $expirationTs = (int)SGConfig::get('SG_BACKUPGUARD_UPLOAD_ACCESS_TOKEN_EXPIRES');
39 return SGConfig::get('SG_BACKUPGUARD_UPLOAD_ACCESS_TOKEN');
40 }
41
42 public function connect()
43 {
44 if ($this->isConnected()) {
45 return;
46 }
47
48 $args = func_get_args();
49 if (!count($args)) {
50 throw new SGExceptionForbidden('Invalid credentials');
51 }
52
53 $email = $args[0];
54 $password = $args[1];
55 $tmp = isset($args[2])?$args[2]:false;
56
57 if (!$email || !$password) {
58 throw new SGExceptionForbidden('Invalid credentials');
59 }
60
61 if ($tmp) {
62 $tokens = $this->client->createAccessToken(
63 SG_BACKUPGUARD_UPLOAD_CLIENT_ID,
64 SG_BACKUPGUARD_UPLOAD_CLIENT_SECRET,
65 $email,
66 $password
67 );
68 }
69 else {
70 $tokens = $this->client->createUploadAccessToken(
71 SG_BACKUPGUARD_UPLOAD_CLIENT_ID,
72 SG_BACKUPGUARD_UPLOAD_CLIENT_SECRET,
73 $email,
74 $password,
75 SG_BACKUPGUARD_UPLOAD_SCOPE
76 );
77 }
78
79 if (empty($tokens)) {
80 throw new SGExceptionForbidden('Invalid credentials');
81 }
82
83 $this->setTokens(
84 $tokens['access_token'],
85 time()+Config::TOKEN_EXPIRES,
86 $tokens['refresh_token'],
87 $tmp
88 );
89
90 return $tokens['access_token'];
91 }
92
93 private function setTokens($accessToken = '', $accessTokenExpires = 0, $refreshToken = '', $tmp = false)
94 {
95 $this->accessToken = $accessToken;
96 $this->refreshToken = $refreshToken;
97
98 if (!$tmp) {
99 SGConfig::set('SG_BACKUPGUARD_UPLOAD_ACCESS_TOKEN', $accessToken);
100 SGConfig::set('SG_BACKUPGUARD_UPLOAD_ACCESS_TOKEN_EXPIRES', $accessTokenExpires);
101 SGConfig::set('SG_BACKUPGUARD_UPLOAD_REFRESH_TOKEN', $refreshToken);
102
103 $this->client->setUploadAccessToken($accessToken);
104 }
105 }
106
107 public function connectOffline()
108 {
109 if ($this->isConnected()) {
110 return;
111 }
112
113 $refreshToken = $this->getRefreshToken();
114
115 if (!$refreshToken) {
116 throw new \SGExceptionNotFound('Refresh token not found');
117 }
118
119 // refresh access token using the refresh token
120 $tokens = $this->client->refreshAccessToken(SG_BACKUPGUARD_UPLOAD_CLIENT_ID, SG_BACKUPGUARD_UPLOAD_CLIENT_SECRET, $refreshToken);
121
122 // set the new access token
123 $this->setTokens(
124 $tokens['access_token'],
125 time()+Config::TOKEN_EXPIRES,
126 $tokens['refresh_token']
127 );
128
129 $this->connected = true;
130 }
131
132 public function checkConnected()
133 {
134 $accessToken = $this->getAccessToken($expirationTs);
135 // to avoid case where token can expire in the middle of upload
136 $this->connected = ($accessToken&&$expirationTs>=(time()+60))?true:false;
137
138 if ($this->connected) {
139 $this->client->setUploadAccessToken(SGConfig::get('SG_BACKUPGUARD_UPLOAD_ACCESS_TOKEN'));
140 }
141 }
142
143 public function getListOfFiles()
144 {
145 if (!$this->isConnected()) {
146 $this->connectOffline();
147 }
148
149 $profileId = $this->getProfileId();
150
151 if ($profileId) {
152 $list = $this->client->getAllBackups($profileId);
153 return $list;
154 }
155
156 return array();
157 }
158
159 public function createFolder($folderName)
160 {
161
162 }
163
164 public function downloadFile($filePath, $size, $backupId = null)
165 {
166 $offset = 0;
167 $result = false;
168 $chunk = 2000000; // 2MB
169 $loaclFilePath = SG_BACKUP_DIRECTORY.basename($filePath);
170 $serverFilePath = $filePath;
171
172 $fp = @fopen($loaclFilePath, 'ab');
173
174 while ($size > $offset) {
175 if (!file_exists($loaclFilePath)) {
176 $result = false;
177 break;
178 }
179
180 $data = $this->client->downloadFile($backupId, $serverFilePath, $offset, $chunk);
181
182 if (strlen($data)) {
183 fwrite($fp, $data);
184 }
185 else {
186 break;
187 }
188
189 $offset += $chunk;
190 $result = true;
191 }
192
193 return $result;
194 }
195
196 private function saveStateData($fileOffset, $uploadId, $backupId, $profileId)
197 {
198 $token = $this->delegate->getToken();
199 $actionId = $this->delegate->getActionId();
200 $pendingStorageUploads = $this->delegate->getPendingStorageUploads();
201 $currentUploadChunksCount = $this->delegate->getCurrentUploadChunksCount();
202 $progress = $this->delegate->getProgress();
203
204 $this->state->setCurrentUploadChunksCount($currentUploadChunksCount);
205 $this->state->setStorageType(SG_STORAGE_BACKUP_GUARD);
206 $this->state->setPendingStorageUploads($pendingStorageUploads);
207 $this->state->setToken($token);
208 $this->state->setActionId($actionId);
209 $this->state->setAction(SG_STATE_ACTION_UPLOADING_BACKUP);
210 $this->state->setProgress($progress);
211
212 $this->state->setOffset($fileOffset);
213 $this->state->setUploadId($uploadId);
214 $this->state->setBackupId($backupId);
215 $this->state->setProfileId($profileId);
216
217 $this->state->save();
218 }
219
220 public function uploadFile($filePath)
221 {
222 if (!$this->isConnected()) {
223 throw new SGExceptionForbidden('Permission denied. Authentication required.');
224 }
225
226 if (!file_exists($filePath) || !is_readable($filePath)) {
227 throw new \SGExceptionNotFound('File does not exist or is not readable: '.$filePath);
228 }
229
230 $chunkSizeBytes = 2000000;
231 $fileSize = backupGuardRealFilesize($filePath);
232 $backupFileName = $this->state->getBackupFileName().'.sgbp';
233
234 $this->delegate->willStartUpload((int)ceil($fileSize/$chunkSizeBytes));
235
236 $handle = fopen($filePath, "rb");
237 $byteOffset = $this->state->getOffset();
238 fseek($handle, $byteOffset);
239
240 if ($this->state->getAction() == SG_STATE_ACTION_PREPARING_STATE_FILE) {
241
242 $profileId = $this->getProfileId();
243 if ($profileId) {
244 $result = $this->client->createBackup($profileId, $backupFileName);
245 if (count($result)) {
246 $profileId = $result['profile_id'];
247 $backupId = $result['backup_id'];
248 }
249 else {
250 throw new SGExceptionForbidden('Something went wrong. Unable to create backup.');
251 }
252 }
253 else {
254 throw new SGExceptionForbidden('Something went wrong. Unable to create profile.');
255 }
256
257 $data = fread($handle, $chunkSizeBytes);
258 $result = $this->client->createUploadSession($backupId, $data);
259 if (count($result)) {
260 $uploadId = $result['upload_id'];
261 $offset = $result['offset'];
262 }
263 else {
264 throw new SGExceptionForbidden('Something went wrong. Unable to start upload session.');
265 }
266
267 $byteOffset += strlen($data);
268 }
269 else {
270 $uploadId = $this->state->getUploadId();
271 $backupId = $this->state->getBackupId();
272 $profileId = $this->state->getProfileId();
273 }
274
275 \SGPing::update();
276
277 while ($byteOffset < $fileSize) {
278 $data = fread($handle, $chunkSizeBytes);
279 $result = $this->client->resumeUploadSession($backupId, $uploadId, $byteOffset, $data);
280 if (count($result)) {
281 $uploadId = $result['upload_id'];
282 $offset = $result['offset'];
283 }
284 else {
285 throw new SGExceptionForbidden('Something went wrong. Unable to start upload session.');
286 }
287
288 if (!$this->delegate->shouldUploadNextChunk()) {
289 fclose($handle);
290 return;
291 }
292
293 \SGPing::update();
294 $byteOffset += strlen($data);
295 $shouldReload = $this->shouldReload();
296 if ($shouldReload && backupGuardIsReloadEnabled()) {
297 $this->saveStateData($byteOffset, $uploadId, $backupId, $profileId);
298 @fclose($handle);
299 $this->reload();
300 }
301 }
302
303 $path = $backupFileName;
304
305 $this->client->finalizeUpload($backupId, $uploadId, $path);
306 $this->client->finalizeBackup($backupId);
307 fclose($handle);
308 }
309
310 public function deleteFile($fileName)
311 {
312
313 }
314
315 public function deleteFolder($folderName)
316 {
317
318 }
319
320 public function fileExists($path)
321 {
322
323 }
324
325 public function checkCloudAccount()
326 {
327 $this->client->setAccessToken($this->accessToken);
328 $account = $this->client->checkCloudAccount();
329 return $account;
330 }
331
332 public function getProfiles()
333 {
334 $profiles = array();
335
336 try {
337 $profiles = $this->client->getProfiles();
338 }
339 catch (UnauthorizedException $exp) {
340 $this->connected = false;
341 $this->connectOffline();
342 $profiles = $this->getProfiles();
343 }
344 catch (Exception $exp) {
345 $profileId = $this->createProfile();
346 $profiles[] = array(
347 'id' => $profileId,
348 'name' => $profileName
349 );
350 }
351
352 return $profiles;
353 }
354
355 public function createProfile($name = null)
356 {
357 $profileName = $name?$name:backupGuardGetSiteUrl();
358 try {
359 $result = $this->client->createProfile($profileName);
360 $profileId = $result['profile_id'];
361 }
362 catch (UnauthorizedException $exp) {
363 $this->connected = false;
364 $this->connectOffline();
365 $profileId = $this->createProfile();
366 }
367 catch (Exception $exp) {
368 $profileId = 0;
369 }
370
371 return $profileId;
372 }
373 }