| 1 |
<?php |
| 2 |
|
| 3 |
namespace League\Flysystem; |
| 4 |
|
| 5 |
use League\Flysystem\Util\MimeType; |
| 6 |
use LogicException; |
| 7 |
|
| 8 |
use function strcmp; |
| 9 |
|
| 10 |
class Util |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Get normalized pathinfo. |
| 14 |
* |
| 15 |
* @param string $path |
| 16 |
* |
| 17 |
* @return array pathinfo |
| 18 |
*/ |
| 19 |
public static function pathinfo($path) |
| 20 |
{ |
| 21 |
$pathinfo = compact('path'); |
| 22 |
|
| 23 |
if ('' !== $dirname = dirname($path)) { |
| 24 |
$pathinfo['dirname'] = static::normalizeDirname($dirname); |
| 25 |
} |
| 26 |
|
| 27 |
$pathinfo['basename'] = static::basename($path); |
| 28 |
|
| 29 |
$pathinfo += pathinfo($pathinfo['basename']); |
| 30 |
|
| 31 |
return $pathinfo + ['dirname' => '']; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Normalize a dirname return value. |
| 36 |
* |
| 37 |
* @param string $dirname |
| 38 |
* |
| 39 |
* @return string normalized dirname |
| 40 |
*/ |
| 41 |
public static function normalizeDirname($dirname) |
| 42 |
{ |
| 43 |
return $dirname === '.' ? '' : $dirname; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get a normalized dirname from a path. |
| 48 |
* |
| 49 |
* @param string $path |
| 50 |
* |
| 51 |
* @return string dirname |
| 52 |
*/ |
| 53 |
public static function dirname($path) |
| 54 |
{ |
| 55 |
return static::normalizeDirname(dirname($path)); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Map result arrays. |
| 60 |
* |
| 61 |
* @param array $object |
| 62 |
* @param array $map |
| 63 |
* |
| 64 |
* @return array mapped result |
| 65 |
*/ |
| 66 |
public static function map(array $object, array $map) |
| 67 |
{ |
| 68 |
$result = []; |
| 69 |
|
| 70 |
foreach ($map as $from => $to) { |
| 71 |
if ( ! isset($object[$from])) { |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
$result[$to] = $object[$from]; |
| 76 |
} |
| 77 |
|
| 78 |
return $result; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Normalize path. |
| 83 |
* |
| 84 |
* @param string $path |
| 85 |
* |
| 86 |
* @throws LogicException |
| 87 |
* |
| 88 |
* @return string |
| 89 |
*/ |
| 90 |
public static function normalizePath($path) |
| 91 |
{ |
| 92 |
return static::normalizeRelativePath($path); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Normalize relative directories in a path. |
| 97 |
* |
| 98 |
* @param string $path |
| 99 |
* |
| 100 |
* @throws LogicException |
| 101 |
* |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
public static function normalizeRelativePath($path) |
| 105 |
{ |
| 106 |
$path = str_replace('\\', '/', $path); |
| 107 |
$path = static::removeFunkyWhiteSpace($path); |
| 108 |
$parts = []; |
| 109 |
|
| 110 |
foreach (explode('/', $path) as $part) { |
| 111 |
switch ($part) { |
| 112 |
case '': |
| 113 |
case '.': |
| 114 |
break; |
| 115 |
|
| 116 |
case '..': |
| 117 |
if (empty($parts)) { |
| 118 |
throw new LogicException( |
| 119 |
'Path is outside of the defined root, path: [' . $path . ']' |
| 120 |
); |
| 121 |
} |
| 122 |
array_pop($parts); |
| 123 |
break; |
| 124 |
|
| 125 |
default: |
| 126 |
$parts[] = $part; |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
$path = implode('/', $parts); |
| 132 |
|
| 133 |
return $path; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Rejects unprintable characters and invalid unicode characters. |
| 138 |
* |
| 139 |
* @param string $path |
| 140 |
* |
| 141 |
* @return string $path |
| 142 |
*/ |
| 143 |
protected static function removeFunkyWhiteSpace($path) |
| 144 |
{ |
| 145 |
if (preg_match('#\p{C}+#u', $path)) { |
| 146 |
throw CorruptedPathDetected::forPath($path); |
| 147 |
} |
| 148 |
|
| 149 |
return $path; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Normalize prefix. |
| 154 |
* |
| 155 |
* @param string $prefix |
| 156 |
* @param string $separator |
| 157 |
* |
| 158 |
* @return string normalized path |
| 159 |
*/ |
| 160 |
public static function normalizePrefix($prefix, $separator) |
| 161 |
{ |
| 162 |
return rtrim($prefix, $separator) . $separator; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Get content size. |
| 167 |
* |
| 168 |
* @param string $contents |
| 169 |
* |
| 170 |
* @return int content size |
| 171 |
*/ |
| 172 |
public static function contentSize($contents) |
| 173 |
{ |
| 174 |
return defined('MB_OVERLOAD_STRING') ? mb_strlen($contents, '8bit') : strlen($contents); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Guess MIME Type based on the path of the file and it's content. |
| 179 |
* |
| 180 |
* @param string $path |
| 181 |
* @param string|resource $content |
| 182 |
* |
| 183 |
* @return string|null MIME Type or NULL if no extension detected |
| 184 |
*/ |
| 185 |
public static function guessMimeType($path, $content) |
| 186 |
{ |
| 187 |
$mimeType = MimeType::detectByContent($content); |
| 188 |
|
| 189 |
if ( ! (empty($mimeType) || in_array($mimeType, ['application/x-empty', 'text/plain', 'text/x-asm']))) { |
| 190 |
return $mimeType; |
| 191 |
} |
| 192 |
|
| 193 |
return MimeType::detectByFilename($path); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Emulate directories. |
| 198 |
* |
| 199 |
* @param array $listing |
| 200 |
* |
| 201 |
* @return array listing with emulated directories |
| 202 |
*/ |
| 203 |
public static function emulateDirectories(array $listing) |
| 204 |
{ |
| 205 |
$directories = []; |
| 206 |
$listedDirectories = []; |
| 207 |
|
| 208 |
foreach ($listing as $object) { |
| 209 |
[$directories, $listedDirectories] = static::emulateObjectDirectories($object, $directories, $listedDirectories); |
| 210 |
} |
| 211 |
|
| 212 |
$directories = array_diff(array_unique($directories), array_unique($listedDirectories)); |
| 213 |
|
| 214 |
foreach ($directories as $directory) { |
| 215 |
$listing[] = static::pathinfo($directory) + ['type' => 'dir']; |
| 216 |
} |
| 217 |
|
| 218 |
return $listing; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Ensure a Config instance. |
| 223 |
* |
| 224 |
* @param null|array|Config $config |
| 225 |
* |
| 226 |
* @return Config config instance |
| 227 |
* |
| 228 |
* @throw LogicException |
| 229 |
*/ |
| 230 |
public static function ensureConfig($config) |
| 231 |
{ |
| 232 |
if ($config === null) { |
| 233 |
return new Config(); |
| 234 |
} |
| 235 |
|
| 236 |
if ($config instanceof Config) { |
| 237 |
return $config; |
| 238 |
} |
| 239 |
|
| 240 |
if (is_array($config)) { |
| 241 |
return new Config($config); |
| 242 |
} |
| 243 |
|
| 244 |
throw new LogicException('A config should either be an array or a Flysystem\Config object.'); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Rewind a stream. |
| 249 |
* |
| 250 |
* @param resource $resource |
| 251 |
*/ |
| 252 |
public static function rewindStream($resource) |
| 253 |
{ |
| 254 |
if (ftell($resource) !== 0 && static::isSeekableStream($resource)) { |
| 255 |
rewind($resource); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
public static function isSeekableStream($resource) |
| 260 |
{ |
| 261 |
$metadata = stream_get_meta_data($resource); |
| 262 |
|
| 263 |
return $metadata['seekable']; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Get the size of a stream. |
| 268 |
* |
| 269 |
* @param resource $resource |
| 270 |
* |
| 271 |
* @return int|null stream size |
| 272 |
*/ |
| 273 |
public static function getStreamSize($resource) |
| 274 |
{ |
| 275 |
$stat = fstat($resource); |
| 276 |
|
| 277 |
if ( ! is_array($stat) || ! isset($stat['size'])) { |
| 278 |
return null; |
| 279 |
} |
| 280 |
|
| 281 |
return $stat['size']; |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Emulate the directories of a single object. |
| 286 |
* |
| 287 |
* @param array $object |
| 288 |
* @param array $directories |
| 289 |
* @param array $listedDirectories |
| 290 |
* |
| 291 |
* @return array |
| 292 |
*/ |
| 293 |
protected static function emulateObjectDirectories(array $object, array $directories, array $listedDirectories) |
| 294 |
{ |
| 295 |
if ($object['type'] === 'dir') { |
| 296 |
$listedDirectories[] = $object['path']; |
| 297 |
} |
| 298 |
|
| 299 |
if ( ! isset($object['dirname']) || trim($object['dirname']) === '') { |
| 300 |
return [$directories, $listedDirectories]; |
| 301 |
} |
| 302 |
|
| 303 |
$parent = $object['dirname']; |
| 304 |
|
| 305 |
while (isset($parent) && trim($parent) !== '' && ! in_array($parent, $directories)) { |
| 306 |
$directories[] = $parent; |
| 307 |
$parent = static::dirname($parent); |
| 308 |
} |
| 309 |
|
| 310 |
if (isset($object['type']) && $object['type'] === 'dir') { |
| 311 |
$listedDirectories[] = $object['path']; |
| 312 |
|
| 313 |
return [$directories, $listedDirectories]; |
| 314 |
} |
| 315 |
|
| 316 |
return [$directories, $listedDirectories]; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Returns the trailing name component of the path. |
| 321 |
* |
| 322 |
* @param string $path |
| 323 |
* |
| 324 |
* @return string |
| 325 |
*/ |
| 326 |
private static function basename($path) |
| 327 |
{ |
| 328 |
$separators = DIRECTORY_SEPARATOR === '/' ? '/' : '\/'; |
| 329 |
|
| 330 |
$path = rtrim($path, $separators); |
| 331 |
|
| 332 |
$basename = preg_replace('#.*?([^' . preg_quote($separators, '#') . ']+$)#', '$1', $path); |
| 333 |
|
| 334 |
if (DIRECTORY_SEPARATOR === '/') { |
| 335 |
return $basename; |
| 336 |
} |
| 337 |
// @codeCoverageIgnoreStart |
| 338 |
// Extra Windows path munging. This is tested via AppVeyor, but code |
| 339 |
// coverage is not reported. |
| 340 |
|
| 341 |
// Handle relative paths with drive letters. c:file.txt. |
| 342 |
while (preg_match('#^[a-zA-Z]{1}:[^\\\/]#', $basename)) { |
| 343 |
$basename = substr($basename, 2); |
| 344 |
} |
| 345 |
|
| 346 |
// Remove colon for standalone drive letter names. |
| 347 |
if (preg_match('#^[a-zA-Z]{1}:$#', $basename)) { |
| 348 |
$basename = rtrim($basename, ':'); |
| 349 |
} |
| 350 |
|
| 351 |
return $basename; |
| 352 |
// @codeCoverageIgnoreEnd |
| 353 |
} |
| 354 |
} |
| 355 |
|