PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.9.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.9.2
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 / Utils / BackupPathResolver.php
wp-staging / Backup / Utils Last commit date
BackupPathResolver.php 4 months ago
BackupPathResolver.php
55 lines
1 <?php
2
3 namespace WPStaging\Backup\Utils;
4
5 use WPStaging\Backup\Service\BackupsFinder;
6 use WPStaging\Backup\WithBackupIdentifier;
7
8 /**
9 * Utility for resolving backup file paths securely within the backups directory.
10 */
11 class BackupPathResolver
12 {
13 use WithBackupIdentifier;
14
15 /**
16 * @var BackupsFinder
17 */
18 private $backupsFinder;
19
20 /**
21 * @param BackupsFinder $backupsFinder
22 */
23 public function __construct(BackupsFinder $backupsFinder)
24 {
25 $this->backupsFinder = $backupsFinder;
26 }
27
28 /**
29 * Resolve a backup file path securely within the backups directory.
30 *
31 * @param string $filePath
32 * @return string Empty string if invalid, or the resolved path
33 */
34 public function resolveBackupPath(string $filePath): string
35 {
36 $backupDir = wp_normalize_path($this->backupsFinder->getBackupsDirectory());
37 $filePath = wp_normalize_path(untrailingslashit($filePath));
38
39 $resolvedPath = $backupDir . str_replace($backupDir, '', $filePath);
40 $resolvedPath = wp_normalize_path($resolvedPath);
41
42 if (strpos($resolvedPath, $backupDir) !== 0) {
43 return '';
44 }
45
46 $basename = wp_basename($resolvedPath);
47 $extension = strtolower(pathinfo($basename, PATHINFO_EXTENSION));
48 if (!in_array($extension, ['wpstg', 'sql'], true) && !$this->isBackupPart($basename)) {
49 return '';
50 }
51
52 return $resolvedPath;
53 }
54 }
55