PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 1.5.4
JetBackup – Backup, Restore & Migrate v1.5.4
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
374 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 $chunkSizeBytes = (int)getCloudUploadChunkSize() * 1024 * 1024;
232 $fileSize = backupGuardRealFilesize($filePath);
233 $backupFileName = $this->state->getBackupFileName().'.sgbp';
234
235 $this->delegate->willStartUpload((int)ceil($fileSize/$chunkSizeBytes));
236
237 $handle = fopen($filePath, "rb");
238 $byteOffset = $this->state->getOffset();
239 fseek($handle, $byteOffset);
240
241 if ($this->state->getAction() == SG_STATE_ACTION_PREPARING_STATE_FILE) {
242
243 $profileId = $this->getProfileId();
244 if ($profileId) {
245 $result = $this->client->createBackup($profileId, $backupFileName);
246 if (count($result)) {
247 $profileId = $result['profile_id'];
248 $backupId = $result['backup_id'];
249 }
250 else {
251 throw new SGExceptionForbidden('Something went wrong. Unable to create backup.');
252 }
253 }
254 else {
255 throw new SGExceptionForbidden('Something went wrong. Unable to create profile.');
256 }
257
258 $data = fread($handle, $chunkSizeBytes);
259 $result = $this->client->createUploadSession($backupId, $data);
260 if (count($result)) {
261 $uploadId = $result['upload_id'];
262 $offset = $result['offset'];
263 }
264 else {
265 throw new SGExceptionForbidden('Something went wrong. Unable to start upload session.');
266 }
267
268 $byteOffset += strlen($data);
269 }
270 else {
271 $uploadId = $this->state->getUploadId();
272 $backupId = $this->state->getBackupId();
273 $profileId = $this->state->getProfileId();
274 }
275
276 \SGPing::update();
277
278 while ($byteOffset < $fileSize) {
279 $data = fread($handle, $chunkSizeBytes);
280 $result = $this->client->resumeUploadSession($backupId, $uploadId, $byteOffset, $data);
281 if (count($result)) {
282 $uploadId = $result['upload_id'];
283 $offset = $result['offset'];
284 }
285 else {
286 throw new SGExceptionForbidden('Something went wrong. Unable to start upload session.');
287 }
288
289 if (!$this->delegate->shouldUploadNextChunk()) {
290 fclose($handle);
291 return;
292 }
293
294 \SGPing::update();
295 $byteOffset += strlen($data);
296 $shouldReload = $this->shouldReload();
297 if ($shouldReload && backupGuardIsReloadEnabled()) {
298 $this->saveStateData($byteOffset, $uploadId, $backupId, $profileId);
299 @fclose($handle);
300 $this->reload();
301 }
302 }
303
304 $path = $backupFileName;
305
306 $this->client->finalizeUpload($backupId, $uploadId, $path);
307 $this->client->finalizeBackup($backupId);
308 fclose($handle);
309 }
310
311 public function deleteFile($fileName)
312 {
313
314 }
315
316 public function deleteFolder($folderName)
317 {
318
319 }
320
321 public function fileExists($path)
322 {
323
324 }
325
326 public function checkCloudAccount()
327 {
328 $this->client->setAccessToken($this->accessToken);
329 $account = $this->client->checkCloudAccount();
330 return $account;
331 }
332
333 public function getProfiles()
334 {
335 $profiles = array();
336
337 try {
338 $profiles = $this->client->getProfiles();
339 }
340 catch (UnauthorizedException $exp) {
341 $this->connected = false;
342 $this->connectOffline();
343 $profiles = $this->getProfiles();
344 }
345 catch (Exception $exp) {
346 $profileId = $this->createProfile();
347 $profiles[] = array(
348 'id' => $profileId,
349 'name' => $profileName
350 );
351 }
352
353 return $profiles;
354 }
355
356 public function createProfile($name = null)
357 {
358 $profileName = $name?$name:backupGuardGetSiteUrl();
359 try {
360 $result = $this->client->createProfile($profileName);
361 $profileId = $result['profile_id'];
362 }
363 catch (UnauthorizedException $exp) {
364 $this->connected = false;
365 $this->connectOffline();
366 $profileId = $this->createProfile();
367 }
368 catch (Exception $exp) {
369 $profileId = 0;
370 }
371
372 return $profileId;
373 }
374 }