123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace TYPO3\PharStreamWrapper;
- /*
- * This file is part of the TYPO3 project.
- *
- * It is free software; you can redistribute it and/or modify it under the terms
- * of the MIT License (MIT). For the full copyright and license information,
- * please read the LICENSE file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
- class Helper
- {
- /*
- * Resets PHP's OPcache if enabled as work-around for issues in `include()`
- * or `require()` calls and OPcache delivering wrong results.
- *
- * @see https://bugs.php.net/bug.php?id=66569
- */
- public static function resetOpCache()
- {
- if (function_exists('opcache_reset')
- && function_exists('opcache_get_status')
- ) {
- $status = opcache_get_status();
- if (!empty($status['opcache_enabled'])) {
- opcache_reset();
- }
- }
- }
- /**
- * Determines base file that can be accessed using the regular file system.
- * For e.g. "phar:///home/user/bundle.phar/content.txt" that would result
- * into "/home/user/bundle.phar".
- *
- * @param string $path
- * @return string|null
- */
- public static function determineBaseFile($path)
- {
- $parts = explode('/', static::normalizePath($path));
- while (count($parts)) {
- $currentPath = implode('/', $parts);
- if (@is_file($currentPath)) {
- return $currentPath;
- }
- array_pop($parts);
- }
- return null;
- }
- /**
- * @param string $path
- * @return string
- */
- public static function removePharPrefix($path)
- {
- $path = trim($path);
- if (stripos($path, 'phar://') !== 0) {
- return $path;
- }
- return substr($path, 7);
- }
- /**
- * Normalizes a path, removes phar:// prefix, fixes Windows directory
- * separators. Result is without trailing slash.
- *
- * @param string $path
- * @return string
- */
- public static function normalizePath($path)
- {
- return rtrim(
- static::getCanonicalPath(
- static::removePharPrefix($path)
- ),
- '/'
- );
- }
- /**
- * Fixes a path for windows-backslashes and reduces double-slashes to single slashes
- *
- * @param string $path File path to process
- * @return string
- */
- private static function normalizeWindowsPath($path)
- {
- return str_replace('\\', '/', $path);
- }
- /**
- * Resolves all dots, slashes and removes spaces after or before a path...
- *
- * @param string $path Input string
- * @return string Canonical path, always without trailing slash
- */
- private static function getCanonicalPath($path)
- {
- $path = static::normalizeWindowsPath($path);
- $absolutePathPrefix = '';
- if (static::isAbsolutePath($path)) {
- if (static::isWindows() && strpos($path, ':/') === 1) {
- $absolutePathPrefix = substr($path, 0, 3);
- $path = substr($path, 3);
- } else {
- $path = ltrim($path, '/');
- $absolutePathPrefix = '/';
- }
- }
- $pathParts = explode('/', $path);
- $pathPartsLength = count($pathParts);
- for ($partCount = 0; $partCount < $pathPartsLength; $partCount++) {
- // double-slashes in path: remove element
- if ($pathParts[$partCount] === '') {
- array_splice($pathParts, $partCount, 1);
- $partCount--;
- $pathPartsLength--;
- }
- // "." in path: remove element
- if ((isset($pathParts[$partCount]) ? $pathParts[$partCount] : '') === '.') {
- array_splice($pathParts, $partCount, 1);
- $partCount--;
- $pathPartsLength--;
- }
- // ".." in path:
- if ((isset($pathParts[$partCount]) ? $pathParts[$partCount] : '') === '..') {
- if ($partCount === 0) {
- array_splice($pathParts, $partCount, 1);
- $partCount--;
- $pathPartsLength--;
- } elseif ($partCount >= 1) {
- // Rremove this and previous element
- array_splice($pathParts, $partCount - 1, 2);
- $partCount -= 2;
- $pathPartsLength -= 2;
- } elseif ($absolutePathPrefix) {
- // can't go higher than root dir
- // simply remove this part and continue
- array_splice($pathParts, $partCount, 1);
- $partCount--;
- $pathPartsLength--;
- }
- }
- }
- return $absolutePathPrefix . implode('/', $pathParts);
- }
- /**
- * Checks if the $path is absolute or relative (detecting either '/' or
- * 'x:/' as first part of string) and returns TRUE if so.
- *
- * @param string $path File path to evaluate
- * @return bool
- */
- private static function isAbsolutePath($path)
- {
- // Path starting with a / is always absolute, on every system
- // On Windows also a path starting with a drive letter is absolute: X:/
- return (isset($path[0]) ? $path[0] : null) === '/'
- || static::isWindows() && (
- strpos($path, ':/') === 1
- || strpos($path, ':\\') === 1
- );
- }
- /**
- * @return bool
- */
- private static function isWindows()
- {
- return stripos(PHP_OS, 'WIN') === 0;
- }
- }
|