PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.1
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.1
4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 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 / BackupFileIndex.php
wp-staging / Backup Last commit date
Ajax 6 days ago BackgroundProcessing 1 week ago Dto 6 days ago Entity 1 week ago Exceptions 1 week ago FileHeader 1 week ago Interfaces 1 week ago Job 6 days ago Request 1 week ago Service 6 days ago Storage 1 week ago Task 6 days ago Traits 1 week ago Utils 1 week ago AfterRestore.php 1 week ago BackupDeleter.php 1 week ago BackupDownload.php 1 week ago BackupFileIndex.php 1 week ago BackupGlitchReason.php 1 week ago BackupHeader.php 6 days ago BackupNextOffer.php 1 week ago BackupRepairer.php 1 week ago BackupRetentionHandler.php 1 week ago BackupScheduler.php 6 days ago BackupServiceProvider.php 1 week ago BackupValidator.php 6 days ago BeforeUpdateRowStatus.php 1 week ago FileHeader.php 1 week ago FileHeaderAttribute.php 1 week ago UpdateProtectionPausedNotice.php 1 week ago WithBackupIdentifier.php 1 week ago
BackupFileIndex.php
207 lines
1 <?php
2
3 namespace WPStaging\Backup;
4
5 use WPStaging\Framework\Job\Exception\FileValidationException;
6 use WPStaging\Backup\Interfaces\IndexLineInterface;
7 use WPStaging\Framework\Traits\FormatTrait;
8
9 class BackupFileIndex implements IndexLineInterface
10 {
11 use FormatTrait;
12
13
14 public $bytesStart;
15
16
17 public $bytesEnd;
18
19
20 public $identifiablePath;
21
22
23 public $isCompressed;
24
25 public function __construct()
26 {
27 $this->bytesStart = 0;
28 $this->bytesEnd = 0;
29 $this->identifiablePath = '';
30 $this->isCompressed = 0;
31 }
32
33
34
35
36
37 public function readIndex(string $index): BackupFileIndex
38 {
39
40
41
42
43
44
45
46
47
48
49
50 list($identifiablePath, $entryMetadata) = explode('|', trim($index));
51
52 $entryMetadata = explode(':', trim($entryMetadata));
53
54
55 if (count($entryMetadata) < 2) {
56
57 throw new \UnexpectedValueException('Invalid backup file index.');
58 }
59
60 $offsetStart = (int)$entryMetadata[0];
61 $writtenPreviously = (int)$entryMetadata[1];
62
63 if (count($entryMetadata) >= 3) {
64 $isCompressed = (int)$entryMetadata[2];
65 } else {
66 $isCompressed = 0;
67 }
68
69 $backupFileIndex = new BackupFileIndex();
70
71
72 $backupFileIndex->identifiablePath = str_replace(['{WPSTG_PIPE}', '{WPSTG_COLON}'], ['|', ':'], $identifiablePath);
73 $backupFileIndex->bytesStart = $offsetStart;
74 $backupFileIndex->bytesEnd = $writtenPreviously;
75 $backupFileIndex->isCompressed = $isCompressed;
76
77 return $backupFileIndex;
78 }
79
80
81
82
83
84
85 public function readIndexLine(string $indexLine): IndexLineInterface
86 {
87 return $this->readIndex($indexLine);
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public function createIndex(string $identifiablePath, int $bytesStart, int $bytesEnd, int $isCompressed): BackupFileIndex
103 {
104 $backupFileIndex = new BackupFileIndex();
105
106
107 $backupFileIndex->identifiablePath = str_replace(['|', ':'], ['{WPSTG_PIPE}', '{WPSTG_COLON}'], $identifiablePath);
108 $backupFileIndex->bytesStart = $bytesStart;
109 $backupFileIndex->bytesEnd = $bytesEnd;
110 $backupFileIndex->isCompressed = $isCompressed;
111
112 return $backupFileIndex;
113 }
114
115 public function getIndex(): string
116 {
117 return "$this->identifiablePath|$this->bytesStart:$this->bytesEnd:$this->isCompressed";
118 }
119
120 public function isIndexLine(string $item): bool
121 {
122 return !empty($item) && strpos($item, ':') !== false && strpos($item, '|') !== false;
123 }
124
125
126
127
128
129
130
131 public function getContentStartOffset(): int
132 {
133 return $this->bytesStart;
134 }
135
136
137
138
139
140
141
142 public function getStartOffset(): int
143 {
144 return $this->bytesStart;
145 }
146
147 public function getIdentifiablePath(): string
148 {
149 return $this->identifiablePath;
150 }
151
152
153
154
155
156
157
158 public function getUncompressedSize(): int
159 {
160 return $this->bytesEnd;
161 }
162
163
164
165
166
167
168
169 public function getCompressedSize(): int
170 {
171 return $this->bytesEnd;
172 }
173
174 public function getIsCompressed(): bool
175 {
176 return $this->isCompressed === 1;
177 }
178
179
180
181
182
183
184
185
186 public function validateFile(string $filePath, string $pathForErrorLogging = '')
187 {
188 if (empty($pathForErrorLogging)) {
189 $pathForErrorLogging = $filePath;
190 }
191
192 if (!file_exists($filePath)) {
193 throw new FileValidationException(sprintf('File doesn\'t exist: %s.', $pathForErrorLogging));
194 }
195
196
197 if ($this->getIsCompressed()) {
198 return;
199 }
200
201 $fileSize = filesize($filePath);
202 if ($this->getUncompressedSize() !== $fileSize) {
203 throw new FileValidationException(sprintf('Filesize validation failed for file %s. Expected: %s. Actual: %s', $pathForErrorLogging, $this->formatSize($this->getUncompressedSize(), 2), $this->formatSize($fileSize, 2)));
204 }
205 }
206 }
207