‪TYPO3CMS  10.4
PathUtility.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 
24 {
32  public static function ‪getRelativePathTo($targetPath)
33  {
34  return ‪self::getRelativePath(self::dirname(‪Environment::getCurrentScript()), $targetPath);
35  }
36 
43  public static function ‪getAbsoluteWebPath($targetPath)
44  {
45  if (self::isAbsolutePath($targetPath)) {
46  if (strpos($targetPath, ‪Environment::getPublicPath()) === 0) {
47  $targetPath = ‪self::stripPathSitePrefix($targetPath);
48  if (!‪Environment::isCli()) {
49  $targetPath = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH') . $targetPath;
50  }
51  }
52  } elseif (strpos($targetPath, '://') !== false) {
53  return $targetPath;
54  } else {
55  // Make an absolute path out of it
56  $targetPath = GeneralUtility::resolveBackPath(self::dirname(‪Environment::getCurrentScript()) . '/' . $targetPath);
57  $targetPath = ‪self::stripPathSitePrefix($targetPath);
58  if (!‪Environment::isCli()) {
59  $targetPath = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH') . $targetPath;
60  }
61  }
62  return $targetPath;
63  }
64 
73  public static function ‪getRelativePath($sourcePath, $targetPath)
74  {
75  $relativePath = null;
76  $sourcePath = rtrim(GeneralUtility::fixWindowsFilePath($sourcePath), '/');
77  $targetPath = rtrim(GeneralUtility::fixWindowsFilePath($targetPath), '/');
78  if ($sourcePath !== $targetPath) {
79  $commonPrefix = ‪self::getCommonPrefix([$sourcePath, $targetPath]);
80  if ($commonPrefix !== null && GeneralUtility::isAllowedAbsPath($commonPrefix)) {
81  $commonPrefixLength = strlen($commonPrefix);
82  $resolvedSourcePath = '';
83  $resolvedTargetPath = '';
84  $sourcePathSteps = 0;
85  if (strlen($sourcePath) > $commonPrefixLength) {
86  $resolvedSourcePath = (string)substr($sourcePath, $commonPrefixLength);
87  }
88  if (strlen($targetPath) > $commonPrefixLength) {
89  $resolvedTargetPath = (string)substr($targetPath, $commonPrefixLength);
90  }
91  if ($resolvedSourcePath !== '') {
92  $sourcePathSteps = count(explode('/', $resolvedSourcePath));
93  }
94  $relativePath = ‪self::sanitizeTrailingSeparator(str_repeat('../', $sourcePathSteps) . $resolvedTargetPath);
95  }
96  }
97  return $relativePath;
98  }
99 
110  public static function ‪getCommonPrefix(array $paths)
111  {
112  $paths = array_map([GeneralUtility::class, 'fixWindowsFilePath'], $paths);
113  $commonPath = null;
114  if (count($paths) === 1) {
115  $commonPath = array_shift($paths);
116  } elseif (count($paths) > 1) {
117  $parts = explode('/', (string)array_shift($paths));
118  $comparePath = '';
119  $break = false;
120  foreach ($parts as $part) {
121  $comparePath .= $part . '/';
122  foreach ($paths as $path) {
123  if (strpos($path . '/', $comparePath) !== 0) {
124  $break = true;
125  break;
126  }
127  }
128  if ($break) {
129  break;
130  }
131  $commonPath = $comparePath;
132  }
133  }
134  if ($commonPath !== null) {
135  $commonPath = ‪self::sanitizeTrailingSeparator($commonPath, '/');
136  }
137  return $commonPath;
138  }
139 
148  public static function ‪sanitizeTrailingSeparator($path, $separator = '/')
149  {
150  return rtrim($path, $separator) . $separator;
151  }
152 
165  public static function ‪basename($path)
166  {
167  $currentLocale = (string)setlocale(LC_CTYPE, '0');
168  setlocale(LC_CTYPE, ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLocale']);
169  $basename = ‪basename($path);
170  setlocale(LC_CTYPE, $currentLocale);
171  return $basename;
172  }
173 
186  public static function ‪dirname($path)
187  {
188  $currentLocale = (string)setlocale(LC_CTYPE, '0');
189  setlocale(LC_CTYPE, ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLocale']);
190  $dirname = ‪dirname($path);
191  setlocale(LC_CTYPE, $currentLocale);
192  return $dirname;
193  }
194 
208  public static function ‪pathinfo($path, $options = null)
209  {
210  $currentLocale = (string)setlocale(LC_CTYPE, '0');
211  setlocale(LC_CTYPE, ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLocale']);
212  $pathinfo = $options == null ? ‪pathinfo($path) : ‪pathinfo($path, $options);
213  setlocale(LC_CTYPE, $currentLocale);
214  return $pathinfo;
215  }
216 
223  public static function ‪isAbsolutePath($path)
224  {
225  // On Windows also a path starting with a drive letter is absolute: X:/
226  if (‪Environment::isWindows() && (substr($path, 1, 2) === ':/' || substr($path, 1, 2) === ':\\')) {
227  return true;
228  }
229  // Path starting with a / is always absolute, on every system
230  return $path[0] === '/';
231  }
232 
256  public static function ‪getAbsolutePathOfRelativeReferencedFileOrPath($baseFilenameOrPath, $includeFileName)
257  {
258  $fileName = static::basename($includeFileName);
259  $basePath = substr($baseFilenameOrPath, -1) === '/' ? $baseFilenameOrPath : static::dirname($baseFilenameOrPath);
260  $newDir = static::getCanonicalPath($basePath . '/' . static::dirname($includeFileName));
261  // Avoid double slash on empty path
262  $result = (($newDir !== '/') ? $newDir : '') . '/' . $fileName;
263  return $result;
264  }
265 
276  public static function ‪dirnameDuringBootstrap($path): string
277  {
278  return preg_replace('#(.*)(/|\\\\)([^\\\\/]+)$#', '$1', $path);
279  }
280 
291  public static function ‪basenameDuringBootstrap($path): string
292  {
293  return preg_replace('#.*[/\\\\]([^\\\\/]+)$#', '$1', $path);
294  }
295 
296  /*********************
297  *
298  * Cleaning methods
299  *
300  *********************/
307  public static function ‪getCanonicalPath($path)
308  {
309  // Replace backslashes with slashes to work with Windows paths if given
310  $path = trim(str_replace('\\', '/', $path));
311 
312  // @todo do we really need this? Probably only in testing context for vfs?
313  $protocol = '';
314  if (strpos($path, '://') !== false) {
315  [$protocol, $path] = explode('://', $path);
316  $protocol .= '://';
317  }
318 
319  $absolutePathPrefix = '';
320  if (static::isAbsolutePath($path)) {
321  if (‪Environment::isWindows() && substr($path, 1, 2) === ':/') {
322  $absolutePathPrefix = substr($path, 0, 3);
323  $path = substr($path, 3);
324  } else {
325  $path = ltrim($path, '/');
326  $absolutePathPrefix = '/';
327  }
328  }
329 
330  $theDirParts = explode('/', $path);
331  $theDirPartsCount = count($theDirParts);
332  for ($partCount = 0; $partCount < $theDirPartsCount; $partCount++) {
333  // double-slashes in path: remove element
334  if ($theDirParts[$partCount] === '') {
335  array_splice($theDirParts, $partCount, 1);
336  $partCount--;
337  $theDirPartsCount--;
338  }
339  // "." in path: remove element
340  if (($theDirParts[$partCount] ?? '') === '.') {
341  array_splice($theDirParts, $partCount, 1);
342  $partCount--;
343  $theDirPartsCount--;
344  }
345  // ".." in path:
346  if (($theDirParts[$partCount] ?? '') === '..') {
347  if ($partCount >= 1) {
348  // Remove this and previous element
349  array_splice($theDirParts, $partCount - 1, 2);
350  $partCount -= 2;
351  $theDirPartsCount -= 2;
352  } elseif ($absolutePathPrefix) {
353  // can't go higher than root dir
354  // simply remove this part and continue
355  array_splice($theDirParts, $partCount, 1);
356  $partCount--;
357  $theDirPartsCount--;
358  }
359  }
360  }
361 
362  return $protocol . $absolutePathPrefix . implode('/', $theDirParts);
363  }
364 
372  public static function ‪stripPathSitePrefix($path)
373  {
374  return substr($path, strlen(‪Environment::getPublicPath() . '/'));
375  }
376 }
‪TYPO3\CMS\Core\Utility\PathUtility\basenameDuringBootstrap
‪static string basenameDuringBootstrap($path)
Definition: PathUtility.php:291
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\Core\Utility\PathUtility\dirname
‪static string dirname($path)
Definition: PathUtility.php:186
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static string stripPathSitePrefix($path)
Definition: PathUtility.php:372
‪TYPO3\CMS\Core\Utility\PathUtility\getRelativePath
‪static string null getRelativePath($sourcePath, $targetPath)
Definition: PathUtility.php:73
‪TYPO3\CMS\Core\Utility\PathUtility\dirnameDuringBootstrap
‪static string dirnameDuringBootstrap($path)
Definition: PathUtility.php:276
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static bool isWindows()
Definition: Environment.php:292
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:16
‪TYPO3\CMS\Core\Core\Environment\getCurrentScript
‪static string getCurrentScript()
Definition: Environment.php:220
‪TYPO3\CMS\Core\Utility\PathUtility\getCanonicalPath
‪static string getCanonicalPath($path)
Definition: PathUtility.php:307
‪TYPO3\CMS\Core\Utility\PathUtility\basename
‪static string basename($path)
Definition: PathUtility.php:165
‪TYPO3\CMS\Core\Utility\PathUtility\getRelativePathTo
‪static string null getRelativePathTo($targetPath)
Definition: PathUtility.php:32
‪TYPO3\CMS\Core\Utility\PathUtility\getCommonPrefix
‪static string null getCommonPrefix(array $paths)
Definition: PathUtility.php:110
‪TYPO3\CMS\Core\Utility\PathUtility\pathinfo
‪static string string[] pathinfo($path, $options=null)
Definition: PathUtility.php:208
‪TYPO3\CMS\Core\Utility\PathUtility\isAbsolutePath
‪static bool isAbsolutePath($path)
Definition: PathUtility.php:223
‪TYPO3\CMS\Core\Utility\PathUtility\sanitizeTrailingSeparator
‪static string sanitizeTrailingSeparator($path, $separator='/')
Definition: PathUtility.php:148
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath)
Definition: PathUtility.php:43
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static bool isCli()
Definition: Environment.php:154
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsolutePathOfRelativeReferencedFileOrPath
‪static string getAbsolutePathOfRelativeReferencedFileOrPath($baseFilenameOrPath, $includeFileName)
Definition: PathUtility.php:256