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