PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.1.3
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.1.3
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Backup / BackupRepairer.php
wp-staging / Backup Last commit date
Ajax 2 years ago BackgroundProcessing 2 years ago Dto 2 years ago Entity 2 years ago Exceptions 3 years ago Job 2 years ago Request 2 years ago Service 2 years ago Storage 2 years ago Task 2 years ago AfterRestore.php 3 years ago BackupDeleter.php 3 years ago BackupDownload.php 3 years ago BackupProcessLock.php 3 years ago BackupRepairer.php 3 years ago BackupScheduler.php 2 years ago BackupServiceProvider.php 2 years ago BackupValidator.php 2 years ago WithBackupIdentifier.php 2 years ago wpstgBackupHeader.txt 3 years ago
BackupRepairer.php
96 lines
1 <?php
2
3 namespace WPStaging\Backup;
4
5 use Exception;
6 use RuntimeException;
7 use UnexpectedValueException;
8 use WPStaging\Framework\Filesystem\FileObject;
9 use WPStaging\Backup\Entity\BackupMetadata;
10 use WPStaging\Backup\Exceptions\DiskNotWritableException;
11 use WPStaging\Backup\Service\BackupMetadataEditor;
12
13 class BackupRepairer
14 {
15 /** @var BackupMetadataEditor */
16 private $metadataEditor;
17
18 /** @var string|false */
19 private $error = false;
20
21 public function __construct(BackupMetadataEditor $metadataEditor)
22 {
23 $this->metadataEditor = $metadataEditor;
24 }
25
26 /** @return string|false */
27 public function getError()
28 {
29 return $this->error;
30 }
31
32 /**
33 * @param string $filePath
34 * @return bool true on success. False on error
35 */
36 public function repairMetadataSize($filePath)
37 {
38 $this->error = false;
39
40 $file = null;
41 try {
42 $file = new FileObject($filePath, FileObject::MODE_APPEND_AND_READ);
43 } catch (DiskNotWritableException $ex) {
44 $this->error = $ex->getMessage();
45 return false;
46 } catch (Exception $ex) {
47 $this->error = $ex->getMessage();
48 return false;
49 }
50
51 // Early bail if file is not wpstg
52 if ($file->getExtension() !== 'wpstg') {
53 return true;
54 }
55
56 $metadata = null;
57 try {
58 $metadata = $file->readBackupMetadata();
59 } catch (RuntimeException $ex) {
60 $this->error = $ex->getMessage();
61 return false;
62 }
63
64 $backupMetadata = new BackupMetadata();
65 $backupMetadata->hydrate($metadata);
66
67 // Early bail if size is not zero
68 if ($backupMetadata->getBackupSize() !== 0) {
69 return true;
70 }
71
72 /*
73 * The length of the backup file size in bytes is added to the total file size
74 *
75 * Before: "backupSize": "" // 2 bytes are already consumed by the string ""
76 * After: "backupSize": 123456 // 4 additional bytes are added = 6 (4+2)
77 */
78 $backupSize = $file->getSize() - 2 + strlen($file->getSize());
79 if ($backupSize < 1) {
80 $this->error = __('Backup size cannot be zero or less', 'wp-staging');
81 return false;
82 }
83
84 $backupMetadata->setBackupSize($backupSize);
85
86 try {
87 $this->metadataEditor->setBackupMetadata($file, $backupMetadata);
88 } catch (UnexpectedValueException $ex) {
89 $this->error = $ex->getMessage();
90 return false;
91 }
92
93 return true;
94 }
95 }
96