| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | namespace TYPO3\PharStreamWrapper; |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the TYPO3 project. |
||
| 7 | * |
||
| 8 | * It is free software; you can redistribute it and/or modify it under the terms |
||
| 9 | * of the MIT License (MIT). For the full copyright and license information, |
||
| 10 | * please read the LICENSE file that was distributed with this source code. |
||
| 11 | * |
||
| 12 | * The TYPO3 project - inspiring people to share! |
||
| 13 | */ |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Helper provides low-level tools on file name resolving. However it does not |
||
| 17 | * (and should not) maintain any runtime state information. In order to resolve |
||
| 18 | * Phar archive paths according resolvers have to be used. |
||
| 19 | * |
||
| 20 | * @see \TYPO3\PharStreamWrapper\Resolvable::resolve() |
||
| 21 | */ |
||
| 22 | class Helper |
||
| 23 | { |
||
| 24 | /* |
||
| 25 | * Resets PHP's OPcache if enabled as work-around for issues in `include()` |
||
| 26 | * or `require()` calls and OPcache delivering wrong results. |
||
| 27 | * |
||
| 28 | * @see https://bugs.php.net/bug.php?id=66569 |
||
| 29 | */ |
||
| 30 | public static function resetOpCache() |
||
| 31 | { |
||
| 32 | if (function_exists('opcache_reset') |
||
| 33 | && function_exists('opcache_get_status') |
||
| 34 | && !empty(@opcache_get_status()['opcache_enabled']) |
||
| 35 | ) { |
||
| 36 | @opcache_reset(); |
||
|
0 ignored issues
–
show
|
|||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Determines base file that can be accessed using the regular file system. |
||
| 42 | * For e.g. "phar:///home/user/bundle.phar/content.txt" that would result |
||
| 43 | * into "/home/user/bundle.phar". |
||
| 44 | * |
||
| 45 | * @param string $path |
||
| 46 | * @return string|null |
||
| 47 | */ |
||
| 48 | public static function determineBaseFile(string $path) |
||
| 49 | { |
||
| 50 | $parts = explode('/', static::normalizePath($path)); |
||
| 51 | |||
| 52 | while (count($parts)) { |
||
| 53 | $currentPath = implode('/', $parts); |
||
| 54 | if (@is_file($currentPath) && realpath($currentPath) !== false) { |
||
| 55 | return $currentPath; |
||
| 56 | } |
||
| 57 | array_pop($parts); |
||
| 58 | } |
||
| 59 | |||
| 60 | return null; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param string $path |
||
| 65 | * @return bool |
||
| 66 | */ |
||
| 67 | public static function hasPharPrefix(string $path): bool |
||
| 68 | { |
||
| 69 | return stripos($path, 'phar://') === 0; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $path |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public static function removePharPrefix(string $path): string |
||
| 77 | { |
||
| 78 | $path = trim($path); |
||
| 79 | if (!static::hasPharPrefix($path)) { |
||
| 80 | return $path; |
||
| 81 | } |
||
| 82 | return substr($path, 7); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Normalizes a path, removes phar:// prefix, fixes Windows directory |
||
| 87 | * separators. Result is without trailing slash. |
||
| 88 | * |
||
| 89 | * @param string $path |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public static function normalizePath(string $path): string |
||
| 93 | { |
||
| 94 | return rtrim( |
||
| 95 | static::normalizeWindowsPath( |
||
| 96 | static::removePharPrefix($path) |
||
| 97 | ), |
||
| 98 | '/' |
||
| 99 | ); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Fixes a path for windows-backslashes and reduces double-slashes to single slashes |
||
| 104 | * |
||
| 105 | * @param string $path File path to process |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | public static function normalizeWindowsPath(string $path): string |
||
| 109 | { |
||
| 110 | return str_replace('\\', '/', $path); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Resolves all dots, slashes and removes spaces after or before a path... |
||
| 115 | * |
||
| 116 | * @param string $path Input string |
||
| 117 | * @return string Canonical path, always without trailing slash |
||
| 118 | */ |
||
| 119 | private static function getCanonicalPath($path): string |
||
|
0 ignored issues
–
show
|
|||
| 120 | { |
||
| 121 | $path = static::normalizeWindowsPath($path); |
||
| 122 | |||
| 123 | $absolutePathPrefix = ''; |
||
| 124 | if (static::isAbsolutePath($path)) { |
||
| 125 | if (static::isWindows() && strpos($path, ':/') === 1) { |
||
| 126 | $absolutePathPrefix = substr($path, 0, 3); |
||
| 127 | $path = substr($path, 3); |
||
| 128 | } else { |
||
| 129 | $path = ltrim($path, '/'); |
||
| 130 | $absolutePathPrefix = '/'; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | $pathParts = explode('/', $path); |
||
| 135 | $pathPartsLength = count($pathParts); |
||
| 136 | for ($partCount = 0; $partCount < $pathPartsLength; $partCount++) { |
||
| 137 | // double-slashes in path: remove element |
||
| 138 | if ($pathParts[$partCount] === '') { |
||
| 139 | array_splice($pathParts, $partCount, 1); |
||
| 140 | $partCount--; |
||
| 141 | $pathPartsLength--; |
||
| 142 | } |
||
| 143 | // "." in path: remove element |
||
| 144 | if (($pathParts[$partCount] ?? '') === '.') { |
||
| 145 | array_splice($pathParts, $partCount, 1); |
||
| 146 | $partCount--; |
||
| 147 | $pathPartsLength--; |
||
| 148 | } |
||
| 149 | // ".." in path: |
||
| 150 | if (($pathParts[$partCount] ?? '') === '..') { |
||
| 151 | if ($partCount === 0) { |
||
| 152 | array_splice($pathParts, $partCount, 1); |
||
| 153 | $partCount--; |
||
| 154 | $pathPartsLength--; |
||
| 155 | } elseif ($partCount >= 1) { |
||
| 156 | // Rremove this and previous element |
||
| 157 | array_splice($pathParts, $partCount - 1, 2); |
||
| 158 | $partCount -= 2; |
||
| 159 | $pathPartsLength -= 2; |
||
| 160 | } elseif ($absolutePathPrefix) { |
||
| 161 | // can't go higher than root dir |
||
| 162 | // simply remove this part and continue |
||
| 163 | array_splice($pathParts, $partCount, 1); |
||
| 164 | $partCount--; |
||
| 165 | $pathPartsLength--; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | return $absolutePathPrefix . implode('/', $pathParts); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Checks if the $path is absolute or relative (detecting either '/' or |
||
| 175 | * 'x:/' as first part of string) and returns TRUE if so. |
||
| 176 | * |
||
| 177 | * @param string $path File path to evaluate |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | private static function isAbsolutePath($path): bool |
||
| 181 | { |
||
| 182 | // Path starting with a / is always absolute, on every system |
||
| 183 | // On Windows also a path starting with a drive letter is absolute: X:/ |
||
| 184 | return ($path[0] ?? null) === '/' |
||
| 185 | || static::isWindows() && ( |
||
| 186 | strpos($path, ':/') === 1 |
||
| 187 | || strpos($path, ':\\') === 1 |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | private static function isWindows(): bool |
||
| 195 | { |
||
| 196 | return stripos(PHP_OS, 'WIN') === 0; |
||
| 197 | } |
||
| 198 | } |
||
| 199 |
If you suppress an error, we recommend checking for the error condition explicitly: