[BUGFIX] Allow recursive folder deletion
[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 * @throws \TYPO3\CMS\Core\Resource\Exception\InvalidPathException
59 */
60 protected function canonicalizeAndCheckFileIdentifier($fileIdentifier) {
61 if ($fileIdentifier !== '') {
62 $fileIdentifier = $this->canonicalizeAndCheckFilePath($fileIdentifier);
63 $fileIdentifier = '/' . ltrim($fileIdentifier, '/');
64 if (!$this->isCaseSensitiveFileSystem()) {
65 $fileIdentifier = strtolower($fileIdentifier);
66 }
67 }
68 return $fileIdentifier;
69 }
70
71 /**
72 * Makes sure the Path given as parameter is valid
73 *
74 * @param string $folderPath The file path (including the file name!)
75 * @return string
76 */
77 protected function canonicalizeAndCheckFolderIdentifier($folderPath) {
78 if ($folderPath === '/') {
79 $canonicalizedIdentifier = $folderPath;
80 } else {
81 $canonicalizedIdentifier = $this->canonicalizeAndCheckFileIdentifier($folderPath) . '/';
82 }
83 return $canonicalizedIdentifier;
84 }
85
86 /**
87 * Returns the identifier of the folder the file resides in
88 *
89 * @param string $fileIdentifier
90 * @return mixed
91 */
92 public function getParentFolderIdentifierOfIdentifier($fileIdentifier) {
93 $fileIdentifier = $this->canonicalizeAndCheckFileIdentifier($fileIdentifier);
94 return \TYPO3\CMS\Core\Utility\PathUtility::dirname($fileIdentifier) . '/';
95 }
96
97
98 }