[FEATURE] Allow Storages outside the webroot
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Driver / AbstractDriver.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Driver;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Ingmar Schlecht <ingmar.schlecht@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Core\Resource\FileInterface;
31 use TYPO3\CMS\Core\Resource\Folder;
32 use TYPO3\CMS\Core\Utility\PathUtility;
33
34 /**
35 * An abstract implementation of a storage driver.
36 *
37 */
38 abstract class AbstractDriver implements DriverInterface {
39
40 /*******************
41 * CAPABILITIES
42 *******************/
43 /**
44 * The capabilities of this driver. See Storage::CAPABILITY_* constants for possible values. This value should be set
45 * in the constructor of derived classes.
46 *
47 * @var integer
48 */
49 protected $capabilities = 0;
50
51
52 /**
53 * The storage uid the driver was instantiated for
54 *
55 * @var integer
56 */
57 protected $storageUid;
58
59 /**
60 * A list of all supported hash algorithms, written all lower case and
61 * without any dashes etc. (e.g. sha1 instead of SHA-1)
62 * Be sure to set this in inherited classes!
63 *
64 * @var array
65 */
66 protected $supportedHashAlgorithms = array();
67
68 /**
69 * The configuration of this driver
70 *
71 * @var array
72 */
73 protected $configuration = array();
74
75 /**
76 * Creates this object.
77 *
78 * @param array $configuration
79 */
80 public function __construct(array $configuration = array()) {
81 $this->configuration = $configuration;
82 }
83
84 /**
85 * Checks a fileName for validity. This could be overidden in concrete
86 * drivers if they have different file naming rules.
87 *
88 * @param string $fileName
89 * @return boolean TRUE if file name is valid
90 */
91 protected function isValidFilename($fileName) {
92 if (strpos($fileName, '/') !== FALSE) {
93 return FALSE;
94 }
95 if (!preg_match('/^[\\pL\\d[:blank:]._-]*$/u', $fileName)) {
96 return FALSE;
97 }
98 return TRUE;
99 }
100
101 /**
102 * Sets the storage uid the driver belongs to
103 *
104 * @param integer $storageUid
105 * @return void
106 */
107 public function setStorageUid($storageUid) {
108 $this->storageUid = $storageUid;
109 }
110
111 /**
112 * Returns the capabilities of this driver.
113 *
114 * @return integer
115 * @see Storage::CAPABILITY_* constants
116 */
117 public function getCapabilities() {
118 return $this->capabilities;
119 }
120
121 /**
122 * Returns TRUE if this driver has the given capability.
123 *
124 * @param integer $capability A capability, as defined in a CAPABILITY_* constant
125 * @return boolean
126 */
127 public function hasCapability($capability) {
128 return $this->capabilities & $capability == $capability;
129 }
130
131 /*******************
132 * FILE FUNCTIONS
133 *******************/
134
135 /**
136 * Returns a temporary path for a given file, including the file extension.
137 *
138 * @param string $fileIdentifier
139 * @return string
140 */
141 protected function getTemporaryPathForFile($fileIdentifier) {
142 return \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('fal-tempfile-', '.' . PathUtility::pathinfo($fileIdentifier, PATHINFO_EXTENSION));
143 }
144
145 /**
146 * Hashes a file identifier, taking the case sensitivity of the file system
147 * into account. This helps mitigating problems with case-insensitive
148 * databases.
149 *
150 * @param string $identifier
151 * @return string
152 */
153 public function hashIdentifier($identifier) {
154 $identifier = $this->canonicalizeAndCheckFileIdentifier($identifier);
155 return sha1($identifier);
156 }
157
158 /**
159 * Basic implementation of the method that does directly return the
160 * file name as is.
161 *
162 * @param string $fileName Input string, typically the body of a fileName
163 * @param string $charset Charset of the a fileName (defaults to current charset; depending on context)
164 * @return string Output string with any characters not matching [.a-zA-Z0-9_-] is substituted by '_' and trailing dots removed
165 */
166 public function sanitizeFileName($fileName, $charset = '') {
167 return $fileName;
168 }
169
170 /**
171 * Returns TRUE if this driver uses case-sensitive identifiers. NOTE: This
172 * is a configurable setting, but the setting does not change the way the
173 * underlying file system treats the identifiers; the setting should
174 * therefore always reflect the file system and not try to change its
175 * behaviour
176 *
177 * @return boolean
178 */
179 public function isCaseSensitiveFileSystem() {
180 if (isset($this->configuration['caseSensitive'])) {
181 return (bool)$this->configuration['caseSensitive'];
182 }
183 return TRUE;
184 }
185
186 /**
187 * Makes sure the path given as parameter is valid
188 *
189 * @param string $filePath The file path (most times filePath)
190 * @return string
191 */
192 abstract protected function canonicalizeAndCheckFilePath($filePath);
193
194 /**
195 * Makes sure the identifier given as parameter is valid
196 *
197 * @param string $fileIdentifier The file Identifier
198 * @return string
199 * @throws \TYPO3\CMS\Core\Resource\Exception\InvalidPathException
200 */
201 abstract protected function canonicalizeAndCheckFileIdentifier($fileIdentifier);
202
203 /**
204 * Makes sure the identifier given as parameter is valid
205 *
206 * @param string $folderIdentifier The folder identifier
207 * @return string
208 */
209 abstract protected function canonicalizeAndCheckFolderIdentifier($folderIdentifier);
210
211 }