[BUGFIX] Correctly resolve parent folder in AbstractHierarchicalFilesystemDriver
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Driver / AbstractHierarchicalFilesystemDriver.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Driver;
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
17 use TYPO3\CMS\Core\Charset\CharsetConverter;
18 use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\PathUtility;
21
22 /**
23 * Class AbstractHierarchicalFilesystemDriver
24 */
25 abstract class AbstractHierarchicalFilesystemDriver extends AbstractDriver
26 {
27 /**
28 * @var CharsetConverter
29 * @deprecated instantiate CharsetConverter yourself in your driver implementation.
30 */
31 protected $charsetConversion;
32
33 /**
34 * Gets the charset conversion object.
35 *
36 * @return CharsetConverter
37 * @deprecated since TYPO3 v9.3, will be removed in TYPO3 v10.0.ß. Instantiate the CharsetConverter object yourself in your driver class.
38 */
39 protected function getCharsetConversion()
40 {
41 trigger_error('Shorthand method "getCharsetConversion()" within the FAL driver method will be removed in TYPO3 v10.0, instantiate CharsetConverter yourself.', E_USER_DEPRECATED);
42 if (!isset($this->charsetConversion)) {
43 $this->charsetConversion = GeneralUtility::makeInstance(CharsetConverter::class);
44 }
45 return $this->charsetConversion;
46 }
47
48 /**
49 * Wrapper for \TYPO3\CMS\Core\Utility\GeneralUtility::validPathStr()
50 *
51 * @param string $theFile Filepath to evaluate
52 * @return bool TRUE if no '/', '..' or '\' is in the $theFile
53 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::validPathStr()
54 */
55 protected function isPathValid($theFile)
56 {
57 return GeneralUtility::validPathStr($theFile);
58 }
59
60 /**
61 * Makes sure the Path given as parameter is valid
62 *
63 * @param string $filePath The file path (including the file name!)
64 * @return string
65 * @throws InvalidPathException
66 */
67 protected function canonicalizeAndCheckFilePath($filePath)
68 {
69 $filePath = PathUtility::getCanonicalPath($filePath);
70
71 // filePath must be valid
72 // Special case is required by vfsStream in Unit Test context
73 if (!$this->isPathValid($filePath) && strpos($filePath, 'vfs://') !== 0) {
74 throw new InvalidPathException('File ' . $filePath . ' is not valid (".." and "//" is not allowed in path).', 1320286857);
75 }
76 return $filePath;
77 }
78
79 /**
80 * Makes sure the Path given as parameter is valid
81 *
82 * @param string $fileIdentifier The file path (including the file name!)
83 * @return string
84 * @throws InvalidPathException
85 */
86 protected function canonicalizeAndCheckFileIdentifier($fileIdentifier)
87 {
88 if ($fileIdentifier !== '') {
89 $fileIdentifier = $this->canonicalizeAndCheckFilePath($fileIdentifier);
90 $fileIdentifier = '/' . ltrim($fileIdentifier, '/');
91 if (!$this->isCaseSensitiveFileSystem()) {
92 $fileIdentifier = mb_strtolower($fileIdentifier, 'utf-8');
93 }
94 }
95 return $fileIdentifier;
96 }
97
98 /**
99 * Makes sure the Path given as parameter is valid
100 *
101 * @param string $folderPath The file path (including the file name!)
102 * @return string
103 */
104 protected function canonicalizeAndCheckFolderIdentifier($folderPath)
105 {
106 if ($folderPath === '/') {
107 $canonicalizedIdentifier = $folderPath;
108 } else {
109 $canonicalizedIdentifier = rtrim($this->canonicalizeAndCheckFileIdentifier($folderPath), '/') . '/';
110 }
111 return $canonicalizedIdentifier;
112 }
113
114 /**
115 * Returns the identifier of the folder the file resides in
116 *
117 * @param string $fileIdentifier
118 * @return mixed
119 */
120 public function getParentFolderIdentifierOfIdentifier($fileIdentifier)
121 {
122 $fileIdentifier = $this->canonicalizeAndCheckFileIdentifier($fileIdentifier);
123 return rtrim(GeneralUtility::fixWindowsFilePath(PathUtility::dirname($fileIdentifier)), '/') . '/';
124 }
125 }