PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.3.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.3.0
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 / Service / BackupMetadataReader.php
wp-staging / Backup / Service Last commit date
Compression 1 year ago Database 1 year ago AbstractBackupsFinder.php 1 year ago AbstractExtractor.php 10 months ago AbstractServiceProvider.php 2 years ago Archiver.php 11 months ago BackupAssets.php 2 years ago BackupContent.php 1 year ago BackupMetadataEditor.php 1 year ago BackupMetadataReader.php 1 year ago BackupSigner.php 1 year ago BackupsFinder.php 1 year ago Extractor.php 10 months ago FileBackupService.php 11 months ago FileBackupServiceProvider.php 2 years ago ServiceInterface.php 2 years ago ZlibCompressor.php 11 months ago
BackupMetadataReader.php
111 lines
1 <?php
2
3 namespace WPStaging\Backup\Service;
4
5 use WPStaging\Framework\Filesystem\FileObject;
6
7 class BackupMetadataReader
8 {
9 /** @var int */
10 private $existingMetadataPosition;
11
12 /** @var FileObject */
13 private $fileObject;
14
15 public function __construct(FileObject $fileObject)
16 {
17 $this->fileObject = $fileObject;
18 }
19
20 /**
21 * @return array The backup metadata array
22 * @throws \RuntimeException
23 */
24 public function readBackupMetadata(): array
25 {
26 // Default max size 128KB for backup metadata
27 $maxBackupMetadataSize = $this->getExpectedMaxBackupMetadataSize();
28 // Make sure the max size is never above 1MB
29 $negativeOffset = min($maxBackupMetadataSize, 1 * MB_IN_BYTES);
30 // Make sure the max size is never below 32KB
31 $negativeOffset = max($negativeOffset, 32 * KB_IN_BYTES);
32
33 // Set the pointer to the end of the file, minus the negative offset for which to start looking for the backup metadata.
34 $this->fileObject->fseek(max($this->fileObject->getSize() - $negativeOffset, 0), SEEK_SET);
35
36 $backupMetadata = null;
37
38 do {
39 $this->existingMetadataPosition = $this->fileObject->ftell();
40 $line = trim($this->fileObject->readAndMoveNext());
41 if ($this->isValidMetadata($line)) {
42 $backupMetadata = $this->extractMetadata($line);
43 }
44 } while ($this->fileObject->valid() && !is_array($backupMetadata));
45
46 if (!is_array($backupMetadata)) {
47 $error = sprintf('Could not find metadata in the backup file %s - This file could be corrupt.', $this->fileObject->getFilename());
48 throw new \RuntimeException($error);
49 }
50
51 return $backupMetadata;
52 }
53
54 public function extractMetadata(string $line): array
55 {
56 $json = [];
57 if (!$this->fileObject->isSqlFile()) {
58 $json = json_decode($line, true);
59 } else {
60 $json = json_decode(substr($line, 3), true);
61 }
62
63 return empty($json) ? [] : $json;
64 }
65
66 /**
67 * @param string $line
68 * @return bool
69 */
70 public function isValidMetadata(string $line): bool
71 {
72 if ($this->fileObject->isSqlFile() && substr($line, 3, 1) !== '{') {
73 return false;
74 } elseif (!$this->fileObject->isSqlFile() && substr($line, 0, 1) !== '{') {
75 return false;
76 }
77
78 $maybeMetadata = $this->extractMetadata($line);
79
80 if (!is_array($maybeMetadata) || !array_key_exists('networks', $maybeMetadata) || !is_array($maybeMetadata['networks'])) {
81 return false;
82 }
83
84 $network = $maybeMetadata['networks']['1'];
85 if (!is_array($network) || !array_key_exists('blogs', $network) || !is_array($network['blogs'])) {
86 return false;
87 }
88
89 return true;
90 }
91
92 public function getExistingMetadataPosition(): int
93 {
94 if ($this->existingMetadataPosition === null) {
95 $this->readBackupMetadata();
96 }
97
98 return $this->existingMetadataPosition;
99 }
100
101 private function getExpectedMaxBackupMetadataSize(): int
102 {
103 $maxBackupMetadataSize = 128 * KB_IN_BYTES;
104 if (!function_exists('apply_filters')) {
105 return $maxBackupMetadataSize;
106 }
107
108 return apply_filters('wpstg_max_backup_metadata_size', $maxBackupMetadataSize);
109 }
110 }
111