Compression
4 months ago
Database
2 weeks ago
AbstractBackupsFinder.php
1 year ago
AbstractExtractor.php
1 day ago
AbstractServiceProvider.php
2 years ago
Archiver.php
2 weeks ago
BackupAssets.php
1 month ago
BackupContent.php
1 year ago
BackupMetadataEditor.php
1 year ago
BackupMetadataReader.php
5 months ago
BackupSigner.php
7 months ago
BackupsDirectoryResolver.php
2 weeks ago
BackupsFinder.php
2 weeks ago
Extractor.php
2 weeks ago
FileBackupService.php
1 day ago
FileBackupServiceProvider.php
2 years ago
ServiceInterface.php
2 years ago
TmpBackupCleaner.php
2 weeks ago
ZlibCompressor.php
11 months ago
TmpBackupCleaner.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup\Service; |
| 4 | |
| 5 | use DirectoryIterator; |
| 6 | use Exception; |
| 7 | use WPStaging\Framework\Network\RemoteDownloader; |
| 8 | |
| 9 | /** |
| 10 | * Cleans temporary backup files without loading backup contents into memory |
| 11 | */ |
| 12 | class TmpBackupCleaner |
| 13 | { |
| 14 | /** |
| 15 | * Delete temporary backup files from a directory. |
| 16 | * |
| 17 | * @param string $directory Absolute path to the backup directory. |
| 18 | * @param int $maxAge Optional max age in seconds. Zero deletes matching files regardless of age. |
| 19 | * @param int $now Current timestamp used for max age checks. |
| 20 | * @return int |
| 21 | */ |
| 22 | public function clean(string $directory, int $maxAge = 0, int $now = 0): int |
| 23 | { |
| 24 | $directory = rtrim($directory, '/\\'); |
| 25 | if (!is_dir($directory)) { |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | try { |
| 30 | $iterator = new DirectoryIterator($directory); |
| 31 | } catch (Exception $e) { |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | $deletedFiles = 0; |
| 36 | foreach ($iterator as $fileInfo) { |
| 37 | if ($fileInfo->isDot() || !$fileInfo->isFile()) { |
| 38 | continue; |
| 39 | } |
| 40 | |
| 41 | if (!$this->isTmpBackup($fileInfo->getFilename())) { |
| 42 | continue; |
| 43 | } |
| 44 | |
| 45 | $path = $fileInfo->getPathname(); |
| 46 | if ($maxAge > 0 && !$this->isOlderThan($path, $maxAge, $now)) { |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | if (@unlink($path)) { |
| 51 | $deletedFiles++; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return $deletedFiles; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Check whether a filename is a temporary backup or in-progress temporary backup upload. |
| 60 | * |
| 61 | * @param string $filename |
| 62 | * @return bool |
| 63 | */ |
| 64 | private function isTmpBackup(string $filename): bool |
| 65 | { |
| 66 | $extension = pathinfo($filename, PATHINFO_EXTENSION); |
| 67 | |
| 68 | if ($extension === Archiver::TMP_BACKUP_EXTENSION) { |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | if ($extension !== RemoteDownloader::UPLOADING_EXTENSION) { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | $filenameWithoutUploading = pathinfo($filename, PATHINFO_FILENAME); |
| 77 | $innerExtension = pathinfo($filenameWithoutUploading, PATHINFO_EXTENSION); |
| 78 | |
| 79 | return $innerExtension === Archiver::TMP_BACKUP_EXTENSION; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Check whether a file is older than the configured age. |
| 84 | * |
| 85 | * @param string $path |
| 86 | * @param int $maxAge |
| 87 | * @param int $now |
| 88 | * @return bool |
| 89 | */ |
| 90 | private function isOlderThan(string $path, int $maxAge, int $now): bool |
| 91 | { |
| 92 | if ($now <= 0) { |
| 93 | $now = time(); |
| 94 | } |
| 95 | |
| 96 | $mtime = @filemtime($path); |
| 97 | if ($mtime === false) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | return ($now - $mtime) >= $maxAge; |
| 102 | } |
| 103 | } |
| 104 |