ApplyFiltersTrait.php
2 months ago
ArrayableTrait.php
7 months ago
BatchSizeCalculateTrait.php
7 months ago
BearerTokenTrait.php
5 months ago
BenchmarkTrait.php
3 years ago
BooleanTransientTrait.php
5 years ago
DatabaseSearchReplaceTrait.php
1 month ago
DbRowsGeneratorTrait.php
4 years ago
DebugLogTrait.php
1 year ago
DeveloperTimerTrait.php
9 months ago
EndOfLinePlaceholderTrait.php
1 year ago
EventLoggerTrait.php
2 months ago
FileScanToCacheTrait.php
2 days ago
FormatTrait.php
1 year ago
HttpRequestTrait.php
9 months ago
HydrateTrait.php
1 year ago
I18nTrait.php
1 year ago
IpResolverTrait.php
11 months ago
MaintenanceTrait.php
6 months ago
MemoryExhaustTrait.php
2 years ago
MySQLRowsGeneratorTrait.php
7 months ago
NoticesTrait.php
2 days ago
PropertyConstructor.php
5 years ago
RenameTmpDirectoryTrait.php
6 months ago
ResourceTrait.php
5 months ago
RestRequestTrait.php
4 months ago
RestoreFileExclusionTrait.php
1 year ago
SafeFileInfoTrait.php
2 days ago
SerializeTrait.php
1 year ago
SetTimeLimitTrait.php
2 months ago
SlashTrait.php
1 year ago
SqlCommentTrait.php
4 months ago
TablePrefixValidator.php
4 months ago
UrlTrait.php
1 year ago
ValueGetterTrait.php
2 years ago
WindowsOsTrait.php
1 year ago
SafeFileInfoTrait.php
82 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Traits; |
| 4 | |
| 5 | use RuntimeException; |
| 6 | use SplFileInfo; |
| 7 | |
| 8 | use function WPStaging\functions\debug_log; |
| 9 | |
| 10 | /** |
| 11 | * Wraps SplFileInfo stat calls that can throw a RuntimeException when |
| 12 | * open_basedir restrictions block access to the path being inspected. |
| 13 | */ |
| 14 | trait SafeFileInfoTrait |
| 15 | { |
| 16 | /** |
| 17 | * @param SplFileInfo $item |
| 18 | * @return bool|null Null when open_basedir blocks the check. |
| 19 | */ |
| 20 | protected function isLinkSafely(SplFileInfo $item) |
| 21 | { |
| 22 | try { |
| 23 | return $item->isLink(); |
| 24 | } catch (RuntimeException $openBaseDirException) { |
| 25 | $this->logInaccessiblePath($item, $openBaseDirException); |
| 26 | return null; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param SplFileInfo $item |
| 32 | * @return bool|null Null when open_basedir blocks the check. |
| 33 | */ |
| 34 | protected function isFileSafely(SplFileInfo $item) |
| 35 | { |
| 36 | try { |
| 37 | return $item->isFile(); |
| 38 | } catch (RuntimeException $openBaseDirException) { |
| 39 | $this->logInaccessiblePath($item, $openBaseDirException); |
| 40 | return null; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param SplFileInfo $item |
| 46 | * @return bool|null Null when open_basedir blocks the check. |
| 47 | */ |
| 48 | protected function isDirSafely(SplFileInfo $item) |
| 49 | { |
| 50 | try { |
| 51 | return $item->isDir(); |
| 52 | } catch (RuntimeException $openBaseDirException) { |
| 53 | $this->logInaccessiblePath($item, $openBaseDirException); |
| 54 | return null; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param SplFileInfo $item |
| 60 | * @return string|false False when open_basedir blocks the check or the target is unresolvable. |
| 61 | */ |
| 62 | protected function getRealPathSafely(SplFileInfo $item) |
| 63 | { |
| 64 | try { |
| 65 | return $item->getRealPath(); |
| 66 | } catch (RuntimeException $openBaseDirException) { |
| 67 | $this->logInaccessiblePath($item, $openBaseDirException); |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param SplFileInfo $item |
| 74 | * @param RuntimeException $exception |
| 75 | * @return void |
| 76 | */ |
| 77 | protected function logInaccessiblePath(SplFileInfo $item, RuntimeException $exception) |
| 78 | { |
| 79 | debug_log('WP STAGING: Skipping inaccessible path during scan: ' . $item->getPathname() . ' - ' . $exception->getMessage()); |
| 80 | } |
| 81 | } |
| 82 |