Ajax
1 month ago
BackgroundProcessing
1 year ago
Dto
1 week ago
Entity
1 week ago
Exceptions
1 year ago
FileHeader
1 month ago
Interfaces
6 months ago
Job
1 month ago
Request
1 year ago
Service
1 week ago
Storage
1 month ago
Task
1 week ago
Traits
10 months ago
Utils
3 months ago
AfterRestore.php
5 months ago
BackupDeleter.php
2 months ago
BackupDownload.php
8 months ago
BackupFileIndex.php
1 year ago
BackupGlitchReason.php
1 year ago
BackupHeader.php
1 month ago
BackupRepairer.php
6 months ago
BackupRetentionHandler.php
1 month ago
BackupScheduler.php
1 week ago
BackupServiceProvider.php
1 month ago
BackupValidator.php
6 months ago
FileHeader.php
1 week ago
FileHeaderAttribute.php
2 years ago
WithBackupIdentifier.php
1 year ago
WithBackupIdentifier.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup; |
| 4 | |
| 5 | use WPStaging\Backup\Service\Database\DatabaseImporter; |
| 6 | use WPStaging\Framework\Filesystem\PartIdentifier; |
| 7 | |
| 8 | trait WithBackupIdentifier |
| 9 | { |
| 10 | /** |
| 11 | * List of ids of multipart backups |
| 12 | * @var string[] |
| 13 | */ |
| 14 | protected $listedMultipartBackups = []; |
| 15 | |
| 16 | /** |
| 17 | * @param string $identifier |
| 18 | * @param string $input |
| 19 | * @return bool |
| 20 | */ |
| 21 | public function checkPartByIdentifier(string $identifier, string $input) |
| 22 | { |
| 23 | return preg_match("#{$identifier}(.[0-9]+)?.wpstg$#", $input); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @param string $name |
| 28 | * @return bool |
| 29 | */ |
| 30 | public function isBackupPart(string $name) |
| 31 | { |
| 32 | $dbExtension = DatabaseImporter::FILE_FORMAT; |
| 33 | $dbIdentifier = PartIdentifier::DATABASE_PART_IDENTIFIER; |
| 34 | if (preg_match("#{$dbIdentifier}(.[0-9]+)?.{$dbExtension}$#", $name)) { |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | $pluginIdentifier = PartIdentifier::PLUGIN_PART_IDENTIFIER; |
| 39 | $mupluginIdentifier = PartIdentifier::MU_PLUGIN_PART_IDENTIFIER; |
| 40 | $themeIdentifier = PartIdentifier::THEME_PART_IDENTIFIER; |
| 41 | $uploadIdentifier = PartIdentifier::UPLOAD_PART_IDENTIFIER; |
| 42 | $otherIdentifier = PartIdentifier::OTHER_WP_CONTENT_PART_IDENTIFIER; |
| 43 | $otherWpRootIdentifier = PartIdentifier::OTHER_WP_ROOT_PART_IDENTIFIER; |
| 44 | |
| 45 | $identifiers = "({$dbIdentifier}|{$pluginIdentifier}|{$mupluginIdentifier}|{$themeIdentifier}|{$uploadIdentifier}|{$otherIdentifier}|{$otherWpRootIdentifier})"; |
| 46 | |
| 47 | if ($this->checkPartByIdentifier($identifiers, $name)) { |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @return void |
| 56 | */ |
| 57 | public function clearListedMultipartBackups() |
| 58 | { |
| 59 | $this->listedMultipartBackups = []; |
| 60 | } |
| 61 | |
| 62 | public function isListedMultipartBackup(string $filename, bool $shouldAddBackup = true) |
| 63 | { |
| 64 | $id = $this->extractBackupIdFromFilename($filename); |
| 65 | if (in_array($id, $this->listedMultipartBackups)) { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | if ($shouldAddBackup) { |
| 70 | $this->listedMultipartBackups[] = $id; |
| 71 | } |
| 72 | |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param string $filename |
| 78 | * @return string |
| 79 | */ |
| 80 | public function extractBackupIdFromFilename(string $filename) |
| 81 | { |
| 82 | if (strpos($filename, '.' . PartIdentifier::DATABASE_PART_IDENTIFIER . '.' . DatabaseImporter::FILE_FORMAT) !== false) { |
| 83 | return $this->extractBackupIdFromDatabaseBackupFilename($filename); |
| 84 | } |
| 85 | |
| 86 | $fileInfos = explode('_', $filename); |
| 87 | $fileInfos = $fileInfos[count($fileInfos) - 1]; |
| 88 | return explode('.', $fileInfos)[0]; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param string $filename |
| 93 | * @return string |
| 94 | */ |
| 95 | protected function extractBackupIdFromDatabaseBackupFilename(string $filename) |
| 96 | { |
| 97 | // This is required if the table prefix contains underscore like wp_some |
| 98 | $filename = str_replace('.' . PartIdentifier::DATABASE_PART_IDENTIFIER . '.' . DatabaseImporter::FILE_FORMAT, '', $filename); |
| 99 | // Get position of last dot . in filename |
| 100 | $lastDotPosition = strrpos($filename, '.'); |
| 101 | // Get filename until last dot to remove the table prefix |
| 102 | $filename = substr($filename, 0, $lastDotPosition); |
| 103 | |
| 104 | $fileInfos = explode('_', $filename); |
| 105 | return $fileInfos[count($fileInfos) - 1]; |
| 106 | } |
| 107 | } |
| 108 |