e864e66bc46b4da4ea1fdb9a59dabcea9207070f
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Folder.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
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\Utility\PathUtility;
18
19 /**
20 * A folder that groups files in a storage. This may be a folder on the local
21 * disk, a bucket in Amazon S3 or a user or a tag in Flickr.
22 *
23 * This object is not persisted in TYPO3 locally, but created on the fly by
24 * storage drivers for the folders they "offer".
25 *
26 * Some folders serve as a physical container for files (e.g. folders on the
27 * local disk, S3 buckets or Flickr users). Other folders just group files by a
28 * certain criterion, e.g. a tag.
29 * The way this is implemented depends on the storage driver.
30 *
31 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
32 * @author Ingmar Schlecht <ingmar@typo3.org>
33 */
34 class Folder implements FolderInterface {
35
36 /**
37 * The storage this folder belongs to.
38 *
39 * @var ResourceStorage
40 */
41 protected $storage;
42
43 /**
44 * The identifier of this folder to identify it on the storage.
45 * On some drivers, this is the path to the folder, but drivers could also just
46 * provide any other unique identifier for this folder on the specific storage.
47 *
48 * @var string
49 */
50 protected $identifier;
51
52 /**
53 * The name of this folder
54 *
55 * @var string
56 */
57 protected $name;
58
59 /**
60 * The filters this folder should use for a file list.
61 *
62 * @var callable[]
63 */
64 protected $fileAndFolderNameFilters = array();
65
66 /**
67 * Modes for filter usage in getFiles()/getFolders()
68 */
69 const FILTER_MODE_NO_FILTERS = 0;
70 // Merge local filters into storage's filters
71 const FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS = 1;
72 // Only use the filters provided by the storage
73 const FILTER_MODE_USE_STORAGE_FILTERS = 2;
74 // Only use the filters provided by the current class
75 const FILTER_MODE_USE_OWN_FILTERS = 3;
76
77 /**
78 * Initialization of the folder
79 *
80 * @param ResourceStorage $storage
81 * @param $identifier
82 * @param $name
83 */
84 public function __construct(ResourceStorage $storage, $identifier, $name) {
85 $this->storage = $storage;
86 $this->identifier = rtrim($identifier, '/') . '/';
87 $this->name = $name;
88 }
89
90 /**
91 * Returns the name of this folder.
92 *
93 * @return string
94 */
95 public function getName() {
96 return $this->name;
97 }
98
99 /**
100 * Returns the full path of this folder, from the root.
101 *
102 * @param string $rootId ID of the root folder, NULL to auto-detect
103 *
104 * @return string
105 */
106 public function getReadablePath($rootId = NULL) {
107 $oldPermissionFlag = $this->getStorage()->getEvaluatePermissions();
108 $this->getStorage()->setEvaluatePermissions(FALSE);
109 if ($rootId === NULL) {
110 $rootId = $this->storage->getRootLevelFolder(FALSE)->getIdentifier();
111 }
112 $readablePath = '';
113 if ($this->identifier !== $rootId) {
114 $readablePath = $this->getParentFolder()->getReadablePath($rootId);
115 }
116 $this->getStorage()->setEvaluatePermissions($oldPermissionFlag);
117 return $readablePath . $this->name . '/';
118 }
119
120 /**
121 * Sets a new name of the folder
122 * currently this does not trigger the "renaming process"
123 * as the name is more seen as a label
124 *
125 * @param string $name The new name
126 * @return void
127 */
128 public function setName($name) {
129 $this->name = $name;
130 }
131
132 /**
133 * Returns the storage this folder belongs to.
134 *
135 * @return ResourceStorage
136 */
137 public function getStorage() {
138 return $this->storage;
139 }
140
141 /**
142 * Returns the path of this folder inside the storage. It depends on the
143 * type of storage whether this is a real path or just some unique identifier.
144 *
145 * @return string
146 */
147 public function getIdentifier() {
148 return $this->identifier;
149 }
150
151 /**
152 * Get hashed identifier
153 *
154 * @return string
155 */
156 public function getHashedIdentifier() {
157 return $this->storage->hashFileIdentifier($this->identifier);
158 }
159
160 /**
161 * Returns a combined identifier of this folder, i.e. the storage UID and
162 * the folder identifier separated by a colon ":".
163 *
164 * @return string Combined storage and folder identifier, e.g. StorageUID:folder/path/
165 */
166 public function getCombinedIdentifier() {
167 return $this->getStorage()->getUid() . ':' . $this->getIdentifier();
168 }
169
170 /**
171 * Returns a publicly accessible URL for this folder
172 *
173 * WARNING: Access to the folder may be restricted by further means, e.g. some
174 * web-based authentication. You have to take care of this yourself.
175 *
176 * @param bool $relativeToCurrentScript Determines whether the URL returned should be relative to the current script, in case it is relative at all (only for the LocalDriver)
177 * @return string
178 */
179 public function getPublicUrl($relativeToCurrentScript = FALSE) {
180 return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
181 }
182
183 /**
184 * Returns a list of files in this folder, optionally filtered. There are several filter modes available, see the
185 * FILTER_MODE_* constants for more information.
186 *
187 * For performance reasons the returned items can also be limited to a given range
188 *
189 * @param int $start The item to start at
190 * @param int $numberOfItems The number of items to return
191 * @param int $filterMode The filter mode to use for the file list.
192 * @param bool $recursive
193 * @param string $sort Property name used to sort the items.
194 * Among them may be: '' (empty, no sorting), name,
195 * fileext, size, tstamp and rw.
196 * If a driver does not support the given property, it
197 * should fall back to "name".
198 * @param bool $sortRev TRUE to indicate reverse sorting (last to first)
199 * @return \TYPO3\CMS\Core\Resource\File[]
200 */
201 public function getFiles($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = FALSE, $sort = '', $sortRev = FALSE) {
202 // Fallback for compatibility with the old method signature variable $useFilters that was used instead of $filterMode
203 if ($filterMode === FALSE) {
204 $useFilters = FALSE;
205 $backedUpFilters = array();
206 } else {
207 list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
208 }
209
210 $fileObjects = $this->storage->getFilesInFolder($this, $start, $numberOfItems, $useFilters, $recursive, $sort, $sortRev);
211
212 $this->restoreBackedUpFiltersInStorage($backedUpFilters);
213
214 return $fileObjects;
215 }
216
217 /**
218 * Returns amount of all files within this folder, optionally filtered by
219 * the given pattern
220 *
221 * @param array $filterMethods
222 * @param bool $recursive
223 * @return int
224 * @throws Exception\InsufficientFolderAccessPermissionsException
225 */
226 public function getFileCount(array $filterMethods = array(), $recursive = FALSE) {
227 return $this->storage->countFilesInFolder($this, TRUE, $recursive);
228 }
229
230 /**
231 * Returns the object for a subfolder of the current folder, if it exists.
232 *
233 * @param string $name Name of the subfolder
234 * @return Folder
235 * @throws \InvalidArgumentException
236 */
237 public function getSubfolder($name) {
238 if (!$this->storage->hasFolderInFolder($name, $this)) {
239 throw new \InvalidArgumentException('Folder "' . $name . '" does not exist in "' . $this->identifier . '"', 1329836110);
240 }
241 /** @var $factory ResourceFactory */
242 $factory = ResourceFactory::getInstance();
243 $folderObject = $factory->createFolderObject($this->storage, $this->identifier . $name . '/', $name);
244 return $folderObject;
245 }
246
247 /**
248 * Returns a list of subfolders
249 *
250 * @param int $start The item to start at
251 * @param int $numberOfItems The number of items to return
252 * @param int $filterMode The filter mode to use for the file list.
253 * @return Folder[]
254 */
255 public function getSubfolders($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS) {
256 list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
257 $folderObjects = $this->storage->getFoldersInFolder($this, $start, $numberOfItems, $useFilters);
258 $this->restoreBackedUpFiltersInStorage($backedUpFilters);
259 return $folderObjects;
260 }
261
262 /**
263 * Adds a file from the local server disk. If the file already exists and
264 * overwriting is disabled,
265 *
266 * @param string $localFilePath
267 * @param string $fileName
268 * @param string $conflictMode possible value are 'cancel', 'replace', 'changeName'
269 * @return File The file object
270 */
271 public function addFile($localFilePath, $fileName = NULL, $conflictMode = 'cancel') {
272 $fileName = $fileName ? $fileName : PathUtility::basename($localFilePath);
273 return $this->storage->addFile($localFilePath, $this, $fileName, $conflictMode);
274 }
275
276 /**
277 * Adds an uploaded file into the Storage.
278 *
279 * @param array $uploadedFileData contains information about the uploaded file given by $_FILES['file1']
280 * @param string $conflictMode possible value are 'cancel', 'replace'
281 * @return File The file object
282 */
283 public function addUploadedFile(array $uploadedFileData, $conflictMode = 'cancel') {
284 return $this->storage->addUploadedFile($uploadedFileData, $this, $uploadedFileData['name'], $conflictMode);
285 }
286
287 /**
288 * Renames this folder.
289 *
290 * @param string $newName
291 * @return Folder
292 */
293 public function rename($newName) {
294 return $this->storage->renameFolder($this, $newName);
295 }
296
297 /**
298 * Deletes this folder from its storage. This also means that this object becomes useless.
299 *
300 * @param bool $deleteRecursively
301 * @return bool TRUE if deletion succeeded
302 */
303 public function delete($deleteRecursively = TRUE) {
304 return $this->storage->deleteFolder($this, $deleteRecursively);
305 }
306
307 /**
308 * Creates a new blank file
309 *
310 * @param string $fileName
311 * @return File The new file object
312 */
313 public function createFile($fileName) {
314 return $this->storage->createFile($fileName, $this);
315 }
316
317 /**
318 * Creates a new folder
319 *
320 * @param string $folderName
321 * @return Folder The new folder object
322 */
323 public function createFolder($folderName) {
324 return $this->storage->createFolder($folderName, $this);
325 }
326
327 /**
328 * Copies folder to a target folder
329 *
330 * @param Folder $targetFolder Target folder to copy to.
331 * @param string $targetFolderName an optional destination fileName
332 * @param string $conflictMode "overrideExistingFile", "renameNewFile" or "cancel
333 * @return Folder New (copied) folder object.
334 */
335 public function copyTo(Folder $targetFolder, $targetFolderName = NULL, $conflictMode = 'renameNewFile') {
336 return $targetFolder->getStorage()->copyFolder($this, $targetFolder, $targetFolderName, $conflictMode);
337 }
338
339 /**
340 * Moves folder to a target folder
341 *
342 * @param Folder $targetFolder Target folder to move to.
343 * @param string $targetFolderName an optional destination fileName
344 * @param string $conflictMode "overrideExistingFile", "renameNewFile" or "cancel
345 * @return Folder New (copied) folder object.
346 */
347 public function moveTo(Folder $targetFolder, $targetFolderName = NULL, $conflictMode = 'renameNewFile') {
348 return $targetFolder->getStorage()->moveFolder($this, $targetFolder, $targetFolderName, $conflictMode);
349 }
350
351 /**
352 * Checks if a file exists in this folder
353 *
354 * @param string $name
355 * @return bool
356 */
357 public function hasFile($name) {
358 return $this->storage->hasFileInFolder($name, $this);
359 }
360
361 /**
362 * Checks if a folder exists in this folder.
363 *
364 * @param string $name
365 * @return bool
366 */
367 public function hasFolder($name) {
368 return $this->storage->hasFolderInFolder($name, $this);
369 }
370
371 /**
372 * Check if a file operation (= action) is allowed on this folder
373 *
374 * @param string $action Action that can be read, write or delete
375 * @return bool
376 */
377 public function checkActionPermission($action) {
378 return $this->getStorage()->checkFolderActionPermission($action, $this);
379 }
380
381 /**
382 * Updates the properties of this folder, e.g. after re-indexing or moving it.
383 *
384 * NOTE: This method should not be called from outside the File Abstraction Layer (FAL)!
385 *
386 * @param array $properties
387 * @return void
388 * @internal
389 */
390 public function updateProperties(array $properties) {
391 // Setting identifier and name to update values
392 if (isset($properties['identifier'])) {
393 $this->identifier = $properties['identifier'];
394 }
395 if (isset($properties['name'])) {
396 $this->name = $properties['name'];
397 }
398 }
399
400 /**
401 * Prepares the filters in this folder's storage according to a set filter mode.
402 *
403 * @param int $filterMode The filter mode to use; one of the FILTER_MODE_* constants
404 * @return array The backed up filters as an array (NULL if filters were not backed up) and whether to use filters or not (bool)
405 */
406 protected function prepareFiltersInStorage($filterMode) {
407 $backedUpFilters = NULL;
408 $useFilters = TRUE;
409
410 switch ($filterMode) {
411 case self::FILTER_MODE_USE_OWN_FILTERS:
412 $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
413 $this->storage->setFileAndFolderNameFilters($this->fileAndFolderNameFilters);
414
415 break;
416
417 case self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS:
418 if (count($this->fileAndFolderNameFilters) > 0) {
419 $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
420 foreach ($this->fileAndFolderNameFilters as $filter) {
421 $this->storage->addFileAndFolderNameFilter($filter);
422 }
423 }
424
425 break;
426
427 case self::FILTER_MODE_USE_STORAGE_FILTERS:
428 // nothing to do here
429
430 break;
431
432 case self::FILTER_MODE_NO_FILTERS:
433 $useFilters = FALSE;
434
435 break;
436 }
437 return array($backedUpFilters, $useFilters);
438 }
439
440 /**
441 * Restores the filters of a storage.
442 *
443 * @param array $backedUpFilters The filters to restore; might be NULL if no filters have been backed up, in
444 * which case this method does nothing.
445 * @see prepareFiltersInStorage()
446 */
447 protected function restoreBackedUpFiltersInStorage($backedUpFilters) {
448 if ($backedUpFilters !== NULL) {
449 $this->storage->setFileAndFolderNameFilters($backedUpFilters);
450 }
451 }
452
453 /**
454 * Sets the filters to use when listing files. These are only used if the filter mode is one of
455 * FILTER_MODE_USE_OWN_FILTERS and FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
456 *
457 * @param array $filters
458 */
459 public function setFileAndFolderNameFilters(array $filters) {
460 $this->fileAndFolderNameFilters = $filters;
461 }
462
463 /**
464 * Returns the role of this folder (if any). See FolderInterface::ROLE_* constants for possible values.
465 *
466 * @return int
467 */
468 public function getRole() {
469 return $this->storage->getRole($this);
470 }
471
472 /**
473 * Returns the parent folder.
474 *
475 * In non-hierarchical storages, that always is the root folder.
476 *
477 * The parent folder of the root folder is the root folder.
478 *
479 * @return Folder
480 */
481 public function getParentFolder() {
482 return $this->getStorage()->getFolder($this->getStorage()->getFolderIdentifierFromFileIdentifier($this->getIdentifier()));
483 }
484
485 }