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