[!!!][TASK] Remove deprecated code in EXT:core
[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\Resource\Exception\InvalidPathException;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\PathUtility;
20
21 /**
22 * Class AbstractHierarchicalFilesystemDriver
23 */
24 abstract class AbstractHierarchicalFilesystemDriver extends AbstractDriver
25 {
26 /**
27 * Wrapper for \TYPO3\CMS\Core\Utility\GeneralUtility::validPathStr()
28 *
29 * @param string $theFile Filepath to evaluate
30 * @return bool TRUE if no '/', '..' or '\' is in the $theFile
31 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::validPathStr()
32 */
33 protected function isPathValid($theFile)
34 {
35 return GeneralUtility::validPathStr($theFile);
36 }
37
38 /**
39 * Makes sure the Path given as parameter is valid
40 *
41 * @param string $filePath The file path (including the file name!)
42 * @return string
43 * @throws InvalidPathException
44 */
45 protected function canonicalizeAndCheckFilePath($filePath)
46 {
47 $filePath = PathUtility::getCanonicalPath($filePath);
48
49 // filePath must be valid
50 // Special case is required by vfsStream in Unit Test context
51 if (!$this->isPathValid($filePath) && strpos($filePath, 'vfs://') !== 0) {
52 throw new InvalidPathException('File ' . $filePath . ' is not valid (".." and "//" is not allowed in path).', 1320286857);
53 }
54 return $filePath;
55 }
56
57 /**
58 * Makes sure the Path given as parameter is valid
59 *
60 * @param string $fileIdentifier The file path (including the file name!)
61 * @return string
62 * @throws InvalidPathException
63 */
64 protected function canonicalizeAndCheckFileIdentifier($fileIdentifier)
65 {
66 if ($fileIdentifier !== '') {
67 $fileIdentifier = $this->canonicalizeAndCheckFilePath($fileIdentifier);
68 $fileIdentifier = '/' . ltrim($fileIdentifier, '/');
69 if (!$this->isCaseSensitiveFileSystem()) {
70 $fileIdentifier = mb_strtolower($fileIdentifier, 'utf-8');
71 }
72 }
73 return $fileIdentifier;
74 }
75
76 /**
77 * Makes sure the Path given as parameter is valid
78 *
79 * @param string $folderPath The file path (including the file name!)
80 * @return string
81 */
82 protected function canonicalizeAndCheckFolderIdentifier($folderPath)
83 {
84 if ($folderPath === '/') {
85 $canonicalizedIdentifier = $folderPath;
86 } else {
87 $canonicalizedIdentifier = rtrim($this->canonicalizeAndCheckFileIdentifier($folderPath), '/') . '/';
88 }
89 return $canonicalizedIdentifier;
90 }
91
92 /**
93 * Returns the identifier of the folder the file resides in
94 *
95 * @param string $fileIdentifier
96 * @return mixed
97 */
98 public function getParentFolderIdentifierOfIdentifier($fileIdentifier)
99 {
100 $fileIdentifier = $this->canonicalizeAndCheckFileIdentifier($fileIdentifier);
101 return rtrim(GeneralUtility::fixWindowsFilePath(PathUtility::dirname($fileIdentifier)), '/') . '/';
102 }
103 }