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