Filters
6 months ago
Scanning
5 years ago
AbstractFileObject.php
1 year ago
AbstractFilesystemScanner.php
2 months ago
DebugLogReader.php
2 years ago
DirectoryListing.php
5 months ago
DiskWriteCheck.php
5 months ago
FileObject.php
1 year ago
Filesystem.php
6 months ago
FilesystemExceptions.php
5 years ago
FilesystemScanner.php
1 week ago
FilesystemScannerDto.php
1 week ago
FilterableDirectoryIterator.php
1 year ago
LegacyFileRulesTrait.php
1 week ago
LogCleanup.php
5 months ago
LogFiles.php
1 year ago
MissingFileException.php
3 years ago
OPcache.php
5 months ago
PartIdentifier.php
8 months ago
PathChecker.php
2 years ago
PathIdentifier.php
6 months ago
Permissions.php
2 days ago
WpUploadsFolderSymlinker.php
1 week ago
PathChecker.php
88 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Filesystem; |
| 4 | |
| 5 | use UnexpectedValueException; |
| 6 | use WPStaging\Framework\Adapter\Directory; |
| 7 | |
| 8 | /** |
| 9 | * Class PathChecker |
| 10 | * This class is created to avoid circular dependency between Filesystem, PathIdentifier and Directory classes |
| 11 | * @package WPStaging\Framework\Filesystem |
| 12 | */ |
| 13 | class PathChecker |
| 14 | { |
| 15 | /** @var Filesystem */ |
| 16 | private $filesystem; |
| 17 | |
| 18 | /** @var Directory */ |
| 19 | private $directory; |
| 20 | |
| 21 | /** @var PathIdentifier */ |
| 22 | private $pathIdentifier; |
| 23 | |
| 24 | /** |
| 25 | * PathChecker constructor. |
| 26 | * |
| 27 | * @param Filesystem $filesystem |
| 28 | * @param Directory $directory |
| 29 | * @param PathIdentifier $pathIdentifier |
| 30 | */ |
| 31 | public function __construct(Filesystem $filesystem, Directory $directory, PathIdentifier $pathIdentifier) |
| 32 | { |
| 33 | $this->filesystem = $filesystem; |
| 34 | $this->directory = $directory; |
| 35 | $this->pathIdentifier = $pathIdentifier; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Check whether the path exists in the list. |
| 40 | * If the isRelative flag is checked it will ignore ABSPATH (root path of WP Installation), |
| 41 | * from both the paths during checking |
| 42 | * |
| 43 | * @param string $path The path to check |
| 44 | * @param array $list List of path to check against |
| 45 | * @param bool $isRelative Should the ABSPATH be ignored when checking. Default false |
| 46 | * @param ?string $basePath Use ABSPATH if null or empty otherwise use the provided path as base path |
| 47 | * |
| 48 | * @return bool |
| 49 | */ |
| 50 | public function isPathInPathsList(string $path, array $list, bool $isRelative = false, $basePath = null): bool |
| 51 | { |
| 52 | if (empty($basePath)) { |
| 53 | $basePath = $this->directory->getAbsPath(); |
| 54 | } |
| 55 | |
| 56 | $basePath = $this->filesystem->normalizePath($basePath); |
| 57 | $path = $this->filesystem->normalizePath($path); |
| 58 | // remove ABSPATH and add leading slash if not present |
| 59 | if ($isRelative) { |
| 60 | $path = '/' . ltrim(str_replace($basePath, '', $path), '/'); |
| 61 | } |
| 62 | |
| 63 | foreach ($list as $pathItem) { |
| 64 | $pathItem = $this->filesystem->normalizePath($pathItem); |
| 65 | try { |
| 66 | $pathItem = $this->pathIdentifier->transformIdentifiableToPath($pathItem); |
| 67 | } catch (UnexpectedValueException $ex) { |
| 68 | } |
| 69 | |
| 70 | // remove ABSPATH and add leading slash if not present |
| 71 | if ($isRelative) { |
| 72 | $pathItem = '/' . ltrim(str_replace($basePath, '', $pathItem), '/'); |
| 73 | } |
| 74 | |
| 75 | if ($path === $pathItem) { |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | // Check whether directory a child of any excluded directories |
| 80 | if (strpos($path, $pathItem . '/') === 0) { |
| 81 | return true; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 |