[BUGFIX] Use CharsetConverter for strtolower of FAL identifier
[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\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
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 */
30 protected $charsetConversion;
31
32 /**
33 * Gets the charset conversion object.
34 *
35 * @return CharsetConverter
36 */
37 protected function getCharsetConversion()
38 {
39 if (!isset($this->charsetConversion)) {
40 $this->charsetConversion = GeneralUtility::makeInstance(CharsetConverter::class);
41 }
42 return $this->charsetConversion;
43 }
44
45 /**
46 * Wrapper for \TYPO3\CMS\Core\Utility\GeneralUtility::validPathStr()
47 *
48 * @param string $theFile Filepath to evaluate
49 * @return bool TRUE if no '/', '..' or '\' is in the $theFile
50 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::validPathStr()
51 */
52 protected function isPathValid($theFile)
53 {
54 return GeneralUtility::validPathStr($theFile);
55 }
56
57 /**
58 * Makes sure the Path given as parameter is valid
59 *
60 * @param string $filePath The file path (including the file name!)
61 * @return string
62 * @throws InvalidPathException
63 */
64 protected function canonicalizeAndCheckFilePath($filePath)
65 {
66 $filePath = PathUtility::getCanonicalPath($filePath);
67
68 // filePath must be valid
69 // Special case is required by vfsStream in Unit Test context
70 if (!$this->isPathValid($filePath) && substr($filePath, 0, 6) !== 'vfs://') {
71 throw new InvalidPathException('File ' . $filePath . ' is not valid (".." and "//" is not allowed in path).', 1320286857);
72 }
73 return $filePath;
74 }
75
76 /**
77 * Makes sure the Path given as parameter is valid
78 *
79 * @param string $fileIdentifier The file path (including the file name!)
80 * @return string
81 * @throws InvalidPathException
82 */
83 protected function canonicalizeAndCheckFileIdentifier($fileIdentifier)
84 {
85 if ($fileIdentifier !== '') {
86 $fileIdentifier = $this->canonicalizeAndCheckFilePath($fileIdentifier);
87 $fileIdentifier = '/' . ltrim($fileIdentifier, '/');
88 if (!$this->isCaseSensitiveFileSystem()) {
89 $fileIdentifier = $this->getCharsetConversion()->conv_case('utf-8', $fileIdentifier, 'toLower');
90 }
91 }
92 return $fileIdentifier;
93 }
94
95 /**
96 * Makes sure the Path given as parameter is valid
97 *
98 * @param string $folderPath The file path (including the file name!)
99 * @return string
100 */
101 protected function canonicalizeAndCheckFolderIdentifier($folderPath)
102 {
103 if ($folderPath === '/') {
104 $canonicalizedIdentifier = $folderPath;
105 } else {
106 $canonicalizedIdentifier = rtrim($this->canonicalizeAndCheckFileIdentifier($folderPath), '/') . '/';
107 }
108 return $canonicalizedIdentifier;
109 }
110
111 /**
112 * Returns the identifier of the folder the file resides in
113 *
114 * @param string $fileIdentifier
115 * @return mixed
116 */
117 public function getParentFolderIdentifierOfIdentifier($fileIdentifier)
118 {
119 $fileIdentifier = $this->canonicalizeAndCheckFileIdentifier($fileIdentifier);
120 return PathUtility::dirname($fileIdentifier) . '/';
121 }
122 }