Filters
1 day ago
Scanning
1 day ago
AbstractFileObject.php
1 day ago
AbstractFilesystemScanner.php
1 day ago
DebugLogReader.php
1 day ago
DirectoryListing.php
1 day ago
DirectorySize.php
1 day ago
DiskWriteCheck.php
1 day ago
FileObject.php
1 day ago
Filesystem.php
1 day ago
FilesystemExceptions.php
5 years ago
FilesystemScanner.php
1 day ago
FilesystemScannerDto.php
1 day ago
FilterableDirectoryIterator.php
1 day ago
LegacyFileRulesTrait.php
1 day ago
LogCleanup.php
1 day ago
LogFiles.php
1 day ago
MissingFileException.php
3 years ago
OPcache.php
1 day ago
PartIdentifier.php
1 day ago
PathChecker.php
1 day ago
PathIdentifier.php
1 day ago
Permissions.php
1 day ago
WpUploadsFolderSymlinker.php
1 day ago
LegacyFileRulesTrait.php
215 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Filesystem; |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | trait LegacyFileRulesTrait |
| 10 | { |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | protected function isExcludedFileByRules(string $path, array $excludedExtensions, array $fileNameRules, int $maxFileSizeBytes): bool |
| 21 | { |
| 22 | $name = basename($path); |
| 23 | |
| 24 | foreach ($fileNameRules as $rule) { |
| 25 | if ($this->ruleMatch($rule, $name)) { |
| 26 | return true; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | if (in_array(strtolower(pathinfo($name, PATHINFO_EXTENSION)), $excludedExtensions, true)) { |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | if ($maxFileSizeBytes > 0) { |
| 35 | $size = filesize($path); |
| 36 | if ($size !== false && $size > $maxFileSizeBytes) { |
| 37 | return true; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | protected function isExcludedFolderByRules(string $path, array $folderNameRules): bool |
| 52 | { |
| 53 | $name = basename($path); |
| 54 | foreach ($folderNameRules as $rule) { |
| 55 | if ($this->ruleMatch($rule, $name)) { |
| 56 | return true; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | protected function extractLegacyFileNameRules(array $fileRules): array |
| 68 | { |
| 69 | $normalizedRules = []; |
| 70 | |
| 71 | foreach ($fileRules as $fileRule) { |
| 72 | if (!is_string($fileRule)) { |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | $fileRule = trim($fileRule); |
| 77 | if ($fileRule === '') { |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | $fileRule = $this->reduceLegacyPathRuleToFileName($fileRule); |
| 82 | |
| 83 | if ($fileRule !== '') { |
| 84 | $normalizedRules[] = $fileRule; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return array_values(array_unique($normalizedRules)); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | protected function extractFileExtensions(array $fileRules): array |
| 96 | { |
| 97 | $extensions = []; |
| 98 | |
| 99 | foreach ($fileRules as $fileRule) { |
| 100 | if (!is_string($fileRule)) { |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | $fileRule = trim($fileRule); |
| 105 | if ($this->getLegacyVcsDirectoryName($fileRule) !== '') { |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | if (strpos($fileRule, '*.') !== 0) { |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | $fileRule = ltrim($fileRule, '*'); |
| 116 | $position = strrpos($fileRule, '.'); |
| 117 | if ($position === false || $position === strlen($fileRule) - 1) { |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | $extension = strtolower(substr($fileRule, $position + 1)); |
| 122 | if ($extension !== '') { |
| 123 | $extensions[] = $extension; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return array_values(array_unique($extensions)); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | protected function extractLegacyFolderNameRules(array $fileRules): array |
| 135 | { |
| 136 | $folderRules = []; |
| 137 | |
| 138 | foreach ($fileRules as $fileRule) { |
| 139 | if (!is_string($fileRule)) { |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | $vcsDirectoryName = $this->getLegacyVcsDirectoryName($fileRule); |
| 144 | if ($vcsDirectoryName === '') { |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | $folderRules[] = 'name_exact_matches ' . $vcsDirectoryName; |
| 149 | } |
| 150 | |
| 151 | return array_values(array_unique($folderRules)); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | protected function ruleMatch(string $rule, string $name): bool |
| 162 | { |
| 163 | $rule = trim($rule); |
| 164 | if (strpos($rule, ' ') === false) { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | list($ruleType, $ruleValue) = explode(' ', $rule, 2); |
| 169 | switch ($ruleType) { |
| 170 | case 'name_contains': |
| 171 | return strpos($name, $ruleValue) !== false; |
| 172 | case 'name_begins_with': |
| 173 | return strpos($name, $ruleValue) === 0; |
| 174 | case 'name_ends_with': |
| 175 | return substr($name, -strlen($ruleValue)) === $ruleValue; |
| 176 | case 'name_exact_matches': |
| 177 | return $name === $ruleValue; |
| 178 | default: |
| 179 | return false; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | private function getLegacyVcsDirectoryName(string $fileRule): string |
| 184 | { |
| 185 | $fileRule = trim($fileRule); |
| 186 | if ($fileRule === '') { |
| 187 | return ''; |
| 188 | } |
| 189 | |
| 190 | $fileRule = strtolower($this->reduceLegacyPathRuleToFileName($fileRule)); |
| 191 | if (strpos($fileRule, '*.') === 0) { |
| 192 | $directoryName = substr($fileRule, 2); |
| 193 | } elseif (strpos($fileRule, '.') === 0) { |
| 194 | $directoryName = substr($fileRule, 1); |
| 195 | } else { |
| 196 | return ''; |
| 197 | } |
| 198 | |
| 199 | if (!in_array($directoryName, ['git', 'svn', 'hg'], true)) { |
| 200 | return ''; |
| 201 | } |
| 202 | |
| 203 | return '.' . $directoryName; |
| 204 | } |
| 205 | |
| 206 | private function reduceLegacyPathRuleToFileName(string $fileRule): string |
| 207 | { |
| 208 | if (strpos($fileRule, '/') !== false || strpos($fileRule, '\\') !== false) { |
| 209 | return basename(str_replace('\\', '/', $fileRule)); |
| 210 | } |
| 211 | |
| 212 | return $fileRule; |
| 213 | } |
| 214 | } |
| 215 |