ee140f0088130a857bd084e9c89a2bdea1d2b0de
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / ResourceStorage.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\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Registry;
19 use TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException;
20 use TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException;
21 use TYPO3\CMS\Core\Resource\Index\FileIndexRepository;
22 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Core\Utility\PathUtility;
25 use TYPO3\CMS\Extbase\Object\ObjectManager;
26 use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
27
28 /**
29 * A "mount point" inside the TYPO3 file handling.
30 *
31 * A "storage" object handles
32 * - abstraction to the driver
33 * - permissions (from the driver, and from the user, + capabilities)
34 * - an entry point for files, folders, and for most other operations
35 *
36 * == Driver entry point
37 * The driver itself, that does the actual work on the file system,
38 * is inside the storage but completely shadowed by
39 * the storage, as the storage also handles the abstraction to the
40 * driver
41 *
42 * The storage can be on the local system, but can also be on a remote
43 * system. The combination of driver + configurable capabilities (storage
44 * is read-only e.g.) allows for flexible uses.
45 *
46 *
47 * == Permission system
48 * As all requests have to run through the storage, the storage knows about the
49 * permissions of a BE/FE user, the file permissions / limitations of the driver
50 * and has some configurable capabilities.
51 * Additionally, a BE user can use "filemounts" (known from previous installations)
52 * to limit his/her work-zone to only a subset (identifier and its subfolders/subfolders)
53 * of the user itself.
54 *
55 * Check 1: "User Permissions" [is the user allowed to write a file) [is the user allowed to write a file]
56 * Check 2: "File Mounts" of the User (act as subsets / filters to the identifiers) [is the user allowed to do something in this folder?]
57 * Check 3: "Capabilities" of Storage (then: of Driver) [is the storage/driver writable?]
58 * Check 4: "File permissions" of the Driver [is the folder writable?]
59 */
60 class ResourceStorage implements ResourceStorageInterface
61 {
62 /**
63 * The storage driver instance belonging to this storage.
64 *
65 * @var Driver\DriverInterface
66 */
67 protected $driver;
68
69 /**
70 * The database record for this storage
71 *
72 * @var array
73 */
74 protected $storageRecord;
75
76 /**
77 * The configuration belonging to this storage (decoded from the configuration field).
78 *
79 * @var array
80 */
81 protected $configuration;
82
83 /**
84 * @var Service\FileProcessingService
85 */
86 protected $fileProcessingService;
87
88 /**
89 * Whether to check if file or folder is in user mounts
90 * and the action is allowed for a user
91 * Default is FALSE so that resources are accessible for
92 * front end rendering or admins.
93 *
94 * @var bool
95 */
96 protected $evaluatePermissions = false;
97
98 /**
99 * User filemounts, added as an array, and used as filters
100 *
101 * @var array
102 */
103 protected $fileMounts = [];
104
105 /**
106 * The file permissions of the user (and their group) merged together and
107 * available as an array
108 *
109 * @var array
110 */
111 protected $userPermissions = [];
112
113 /**
114 * The capabilities of this storage as defined in the storage record.
115 * Also see the CAPABILITY_* constants below
116 *
117 * @var int
118 */
119 protected $capabilities;
120
121 /**
122 * @var Dispatcher
123 */
124 protected $signalSlotDispatcher;
125
126 /**
127 * @var Folder
128 */
129 protected $processingFolder;
130
131 /**
132 * All processing folders of this storage used in any storage
133 *
134 * @var Folder[]
135 */
136 protected $processingFolders;
137
138 /**
139 * whether this storage is online or offline in this request
140 *
141 * @var bool
142 */
143 protected $isOnline = null;
144
145 /**
146 * @var bool
147 */
148 protected $isDefault = false;
149
150 /**
151 * The filters used for the files and folder names.
152 *
153 * @var array
154 */
155 protected $fileAndFolderNameFilters = [];
156
157 /**
158 * Levels numbers used to generate hashed subfolders in the processing folder
159 */
160 const PROCESSING_FOLDER_LEVELS = 2;
161
162 /**
163 * Constructor for a storage object.
164 *
165 * @param Driver\DriverInterface $driver
166 * @param array $storageRecord The storage record row from the database
167 */
168 public function __construct(Driver\DriverInterface $driver, array $storageRecord)
169 {
170 $this->storageRecord = $storageRecord;
171 $this->configuration = $this->getResourceFactoryInstance()->convertFlexFormDataToConfigurationArray($storageRecord['configuration']);
172 $this->capabilities =
173 ($this->storageRecord['is_browsable'] ? self::CAPABILITY_BROWSABLE : 0) |
174 ($this->storageRecord['is_public'] ? self::CAPABILITY_PUBLIC : 0) |
175 ($this->storageRecord['is_writable'] ? self::CAPABILITY_WRITABLE : 0);
176
177 $this->driver = $driver;
178 $this->driver->setStorageUid($storageRecord['uid']);
179 $this->driver->mergeConfigurationCapabilities($this->capabilities);
180 try {
181 $this->driver->processConfiguration();
182 } catch (Exception\InvalidConfigurationException $e) {
183 // Configuration error
184 $this->isOnline = false;
185
186 $message = sprintf(
187 'Failed initializing storage [%d] "%s", error: %s',
188 $this->getUid(),
189 $this->getName(),
190 $e->getMessage()
191 );
192
193 $this->getLogger()->error($message);
194 }
195 $this->driver->initialize();
196 $this->capabilities = $this->driver->getCapabilities();
197
198 $this->isDefault = (isset($storageRecord['is_default']) && $storageRecord['is_default'] == 1);
199 $this->resetFileAndFolderNameFiltersToDefault();
200 }
201
202 /**
203 * Gets the configuration.
204 *
205 * @return array
206 */
207 public function getConfiguration()
208 {
209 return $this->configuration;
210 }
211
212 /**
213 * Sets the configuration.
214 *
215 * @param array $configuration
216 */
217 public function setConfiguration(array $configuration)
218 {
219 $this->configuration = $configuration;
220 }
221
222 /**
223 * Gets the storage record.
224 *
225 * @return array
226 */
227 public function getStorageRecord()
228 {
229 return $this->storageRecord;
230 }
231
232 /**
233 * Sets the storage that belongs to this storage.
234 *
235 * @param Driver\DriverInterface $driver
236 * @return ResourceStorage
237 */
238 public function setDriver(Driver\DriverInterface $driver)
239 {
240 $this->driver = $driver;
241 return $this;
242 }
243
244 /**
245 * Returns the driver object belonging to this storage.
246 *
247 * @return Driver\DriverInterface
248 */
249 protected function getDriver()
250 {
251 return $this->driver;
252 }
253
254 /**
255 * Returns the name of this storage.
256 *
257 * @return string
258 */
259 public function getName()
260 {
261 return $this->storageRecord['name'];
262 }
263
264 /**
265 * Returns the UID of this storage.
266 *
267 * @return int
268 */
269 public function getUid()
270 {
271 return (int)$this->storageRecord['uid'];
272 }
273
274 /**
275 * Tells whether there are children in this storage.
276 *
277 * @return bool
278 */
279 public function hasChildren()
280 {
281 return true;
282 }
283
284 /*********************************
285 * Capabilities
286 ********************************/
287 /**
288 * Returns the capabilities of this storage.
289 *
290 * @return int
291 * @see CAPABILITY_* constants
292 */
293 public function getCapabilities()
294 {
295 return (int)$this->capabilities;
296 }
297
298 /**
299 * Returns TRUE if this storage has the given capability.
300 *
301 * @param int $capability A capability, as defined in a CAPABILITY_* constant
302 * @return bool
303 */
304 protected function hasCapability($capability)
305 {
306 return ($this->capabilities & $capability) == $capability;
307 }
308
309 /**
310 * Returns TRUE if this storage is publicly available. This is just a
311 * configuration option and does not mean that it really *is* public. OTOH
312 * a storage that is marked as not publicly available will trigger the file
313 * publishing mechanisms of TYPO3.
314 *
315 * @return bool
316 */
317 public function isPublic()
318 {
319 return $this->hasCapability(self::CAPABILITY_PUBLIC);
320 }
321
322 /**
323 * Returns TRUE if this storage is writable. This is determined by the
324 * driver and the storage configuration; user permissions are not taken into account.
325 *
326 * @return bool
327 */
328 public function isWritable()
329 {
330 return $this->hasCapability(self::CAPABILITY_WRITABLE);
331 }
332
333 /**
334 * Returns TRUE if this storage is browsable by a (backend) user of TYPO3.
335 *
336 * @return bool
337 */
338 public function isBrowsable()
339 {
340 return $this->isOnline() && $this->hasCapability(self::CAPABILITY_BROWSABLE);
341 }
342
343 /**
344 * Returns TRUE if the identifiers used by this storage are case-sensitive.
345 *
346 * @return bool
347 */
348 public function usesCaseSensitiveIdentifiers()
349 {
350 return $this->driver->isCaseSensitiveFileSystem();
351 }
352
353 /**
354 * Returns TRUE if this storage is browsable by a (backend) user of TYPO3.
355 *
356 * @return bool
357 */
358 public function isOnline()
359 {
360 if ($this->isOnline === null) {
361 if ($this->getUid() === 0) {
362 $this->isOnline = true;
363 }
364 // the storage is not marked as online for a longer time
365 if ($this->storageRecord['is_online'] == 0) {
366 $this->isOnline = false;
367 }
368 if ($this->isOnline !== false) {
369 // all files are ALWAYS available in the frontend
370 if (TYPO3_MODE === 'FE') {
371 $this->isOnline = true;
372 } else {
373 // check if the storage is disabled temporary for now
374 $registryObject = GeneralUtility::makeInstance(Registry::class);
375 $offlineUntil = $registryObject->get('core', 'sys_file_storage-' . $this->getUid() . '-offline-until');
376 if ($offlineUntil && $offlineUntil > time()) {
377 $this->isOnline = false;
378 } else {
379 $this->isOnline = true;
380 }
381 }
382 }
383 }
384 return $this->isOnline;
385 }
386
387 /**
388 * Returns TRUE if auto extracting of metadata is enabled
389 *
390 * @return bool
391 */
392 public function autoExtractMetadataEnabled()
393 {
394 return !empty($this->storageRecord['auto_extract_metadata']);
395 }
396
397 /**
398 * Blows the "fuse" and marks the storage as offline.
399 *
400 * Can only be modified by an admin.
401 *
402 * Typically, this is only done if the configuration is wrong.
403 */
404 public function markAsPermanentlyOffline()
405 {
406 if ($this->getUid() > 0) {
407 // @todo: move this to the storage repository
408 GeneralUtility::makeInstance(ConnectionPool::class)
409 ->getConnectionForTable('sys_file_storage')
410 ->update(
411 'sys_file_storage',
412 ['is_online' => 0],
413 ['uid' => (int)$this->getUid()]
414 );
415 }
416 $this->storageRecord['is_online'] = 0;
417 $this->isOnline = false;
418 }
419
420 /**
421 * Marks this storage as offline for the next 5 minutes.
422 *
423 * Non-permanent: This typically happens for remote storages
424 * that are "flaky" and not available all the time.
425 */
426 public function markAsTemporaryOffline()
427 {
428 $registryObject = GeneralUtility::makeInstance(Registry::class);
429 $registryObject->set('core', 'sys_file_storage-' . $this->getUid() . '-offline-until', time() + 60 * 5);
430 $this->storageRecord['is_online'] = 0;
431 $this->isOnline = false;
432 }
433
434 /*********************************
435 * User Permissions / File Mounts
436 ********************************/
437 /**
438 * Adds a filemount as a "filter" for users to only work on a subset of a
439 * storage object
440 *
441 * @param string $folderIdentifier
442 * @param array $additionalData
443 *
444 * @throws Exception\FolderDoesNotExistException
445 */
446 public function addFileMount($folderIdentifier, $additionalData = [])
447 {
448 // check for the folder before we add it as a filemount
449 if ($this->driver->folderExists($folderIdentifier) === false) {
450 // if there is an error, this is important and should be handled
451 // as otherwise the user would see the whole storage without any restrictions for the filemounts
452 throw new Exception\FolderDoesNotExistException('Folder for file mount ' . $folderIdentifier . ' does not exist.', 1334427099);
453 }
454 $data = $this->driver->getFolderInfoByIdentifier($folderIdentifier);
455 $folderObject = $this->getResourceFactoryInstance()->createFolderObject($this, $data['identifier'], $data['name']);
456 // Use the canonical identifier instead of the user provided one!
457 $folderIdentifier = $folderObject->getIdentifier();
458 if (
459 !empty($this->fileMounts[$folderIdentifier])
460 && empty($this->fileMounts[$folderIdentifier]['read_only'])
461 && !empty($additionalData['read_only'])
462 ) {
463 // Do not overwrite a regular mount with a read only mount
464 return;
465 }
466 if (empty($additionalData)) {
467 $additionalData = [
468 'path' => $folderIdentifier,
469 'title' => $folderIdentifier,
470 'folder' => $folderObject
471 ];
472 } else {
473 $additionalData['folder'] = $folderObject;
474 if (!isset($additionalData['title'])) {
475 $additionalData['title'] = $folderIdentifier;
476 }
477 }
478 $this->fileMounts[$folderIdentifier] = $additionalData;
479 }
480
481 /**
482 * Returns all file mounts that are registered with this storage.
483 *
484 * @return array
485 */
486 public function getFileMounts()
487 {
488 return $this->fileMounts;
489 }
490
491 /**
492 * Checks if the given subject is within one of the registered user
493 * file mounts. If not, working with the file is not permitted for the user.
494 *
495 * @param ResourceInterface $subject file or folder
496 * @param bool $checkWriteAccess If true, it is not only checked if the subject is within the file mount but also whether it isn't a read only file mount
497 * @return bool
498 */
499 public function isWithinFileMountBoundaries($subject, $checkWriteAccess = false)
500 {
501 if (!$this->evaluatePermissions) {
502 return true;
503 }
504 $isWithinFileMount = false;
505 if (!$subject) {
506 $subject = $this->getRootLevelFolder();
507 }
508 $identifier = $subject->getIdentifier();
509
510 // Allow access to processing folder
511 if ($this->isWithinProcessingFolder($identifier)) {
512 $isWithinFileMount = true;
513 } else {
514 // Check if the identifier of the subject is within at
515 // least one of the file mounts
516 $writableFileMountAvailable = false;
517 foreach ($this->fileMounts as $fileMount) {
518 /** @var Folder $folder */
519 $folder = $fileMount['folder'];
520 if ($this->driver->isWithin($folder->getIdentifier(), $identifier)) {
521 $isWithinFileMount = true;
522 if (!$checkWriteAccess) {
523 break;
524 }
525 if (empty($fileMount['read_only'])) {
526 $writableFileMountAvailable = true;
527 break;
528 }
529 }
530 }
531 $isWithinFileMount = $checkWriteAccess ? $writableFileMountAvailable : $isWithinFileMount;
532 }
533 return $isWithinFileMount;
534 }
535
536 /**
537 * Sets whether the permissions to access or write
538 * into this storage should be checked or not.
539 *
540 * @param bool $evaluatePermissions
541 */
542 public function setEvaluatePermissions($evaluatePermissions)
543 {
544 $this->evaluatePermissions = (bool)$evaluatePermissions;
545 }
546
547 /**
548 * Gets whether the permissions to access or write
549 * into this storage should be checked or not.
550 *
551 * @return bool $evaluatePermissions
552 */
553 public function getEvaluatePermissions()
554 {
555 return $this->evaluatePermissions;
556 }
557
558 /**
559 * Sets the user permissions of the storage.
560 *
561 * @param array $userPermissions
562 */
563 public function setUserPermissions(array $userPermissions)
564 {
565 $this->userPermissions = $userPermissions;
566 }
567
568 /**
569 * Checks if the ACL settings allow for a certain action
570 * (is a user allowed to read a file or copy a folder).
571 *
572 * @param string $action
573 * @param string $type either File or Folder
574 * @return bool
575 */
576 public function checkUserActionPermission($action, $type)
577 {
578 if (!$this->evaluatePermissions) {
579 return true;
580 }
581
582 $allow = false;
583 if (!empty($this->userPermissions[strtolower($action) . ucfirst(strtolower($type))])) {
584 $allow = true;
585 }
586
587 return $allow;
588 }
589
590 /**
591 * Checks if a file operation (= action) is allowed on a
592 * File/Folder/Storage (= subject).
593 *
594 * This method, by design, does not throw exceptions or do logging.
595 * Besides the usage from other methods in this class, it is also used by
596 * the Filelist UI to check whether an action is allowed and whether action
597 * related UI elements should thus be shown (move icon, edit icon, etc.)
598 *
599 * @param string $action action, can be read, write, delete
600 * @param FileInterface $file
601 * @return bool
602 */
603 public function checkFileActionPermission($action, FileInterface $file)
604 {
605 $isProcessedFile = $file instanceof ProcessedFile;
606 // Check 1: Does the user have permission to perform the action? e.g. "readFile"
607 if (!$isProcessedFile && $this->checkUserActionPermission($action, 'File') === false) {
608 return false;
609 }
610 // Check 2: No action allowed on files for denied file extensions
611 if (!$this->checkFileExtensionPermission($file->getName())) {
612 return false;
613 }
614 $isReadCheck = false;
615 if (in_array($action, ['read', 'copy', 'move', 'replace'], true)) {
616 $isReadCheck = true;
617 }
618 $isWriteCheck = false;
619 if (in_array($action, ['add', 'write', 'move', 'rename', 'replace', 'delete'], true)) {
620 $isWriteCheck = true;
621 }
622 // Check 3: Does the user have the right to perform the action?
623 // (= is he within the file mount borders)
624 if (!$isProcessedFile && !$this->isWithinFileMountBoundaries($file, $isWriteCheck)) {
625 return false;
626 }
627
628 $isMissing = false;
629 if (!$isProcessedFile && $file instanceof File) {
630 $isMissing = $file->isMissing();
631 }
632
633 if ($this->driver->fileExists($file->getIdentifier()) === false) {
634 $file->setMissing(true);
635 $isMissing = true;
636 }
637
638 // Check 4: Check the capabilities of the storage (and the driver)
639 if ($isWriteCheck && ($isMissing || !$this->isWritable())) {
640 return false;
641 }
642
643 // Check 5: "File permissions" of the driver (only when file isn't marked as missing)
644 if (!$isMissing) {
645 $filePermissions = $this->driver->getPermissions($file->getIdentifier());
646 if ($isReadCheck && !$filePermissions['r']) {
647 return false;
648 }
649 if ($isWriteCheck && !$filePermissions['w']) {
650 return false;
651 }
652 }
653 return true;
654 }
655
656 /**
657 * Checks if a folder operation (= action) is allowed on a Folder.
658 *
659 * This method, by design, does not throw exceptions or do logging.
660 * See the checkFileActionPermission() method above for the reasons.
661 *
662 * @param string $action
663 * @param Folder $folder
664 * @return bool
665 */
666 public function checkFolderActionPermission($action, Folder $folder = null)
667 {
668 // Check 1: Does the user have permission to perform the action? e.g. "writeFolder"
669 if ($this->checkUserActionPermission($action, 'Folder') === false) {
670 return false;
671 }
672
673 // If we do not have a folder here, we cannot do further checks
674 if ($folder === null) {
675 return true;
676 }
677
678 $isReadCheck = false;
679 if (in_array($action, ['read', 'copy'], true)) {
680 $isReadCheck = true;
681 }
682 $isWriteCheck = false;
683 if (in_array($action, ['add', 'move', 'write', 'delete', 'rename'], true)) {
684 $isWriteCheck = true;
685 }
686 // Check 2: Does the user has the right to perform the action?
687 // (= is he within the file mount borders)
688 if (!$this->isWithinFileMountBoundaries($folder, $isWriteCheck)) {
689 return false;
690 }
691 // Check 3: Check the capabilities of the storage (and the driver)
692 if ($isReadCheck && !$this->isBrowsable()) {
693 return false;
694 }
695 if ($isWriteCheck && !$this->isWritable()) {
696 return false;
697 }
698
699 // Check 4: "Folder permissions" of the driver
700 $folderPermissions = $this->driver->getPermissions($folder->getIdentifier());
701 if ($isReadCheck && !$folderPermissions['r']) {
702 return false;
703 }
704 if ($isWriteCheck && !$folderPermissions['w']) {
705 return false;
706 }
707 return true;
708 }
709
710 /**
711 * If the fileName is given, checks it against the
712 * TYPO3_CONF_VARS[BE][fileDenyPattern] + and if the file extension is allowed.
713 *
714 * @param string $fileName full filename
715 * @return bool TRUE if extension/filename is allowed
716 */
717 protected function checkFileExtensionPermission($fileName)
718 {
719 $fileName = $this->driver->sanitizeFileName($fileName);
720 $isAllowed = GeneralUtility::verifyFilenameAgainstDenyPattern($fileName);
721 if ($isAllowed && $this->evaluatePermissions) {
722 $fileExtension = strtolower(PathUtility::pathinfo($fileName, PATHINFO_EXTENSION));
723 // Set up the permissions for the file extension
724 $fileExtensionPermissions = $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']['webspace'];
725 $fileExtensionPermissions['allow'] = GeneralUtility::uniqueList(strtolower($fileExtensionPermissions['allow']));
726 $fileExtensionPermissions['deny'] = GeneralUtility::uniqueList(strtolower($fileExtensionPermissions['deny']));
727 if ($fileExtension !== '') {
728 // If the extension is found amongst the allowed types, we return TRUE immediately
729 if ($fileExtensionPermissions['allow'] === '*' || GeneralUtility::inList($fileExtensionPermissions['allow'], $fileExtension)) {
730 return true;
731 }
732 // If the extension is found amongst the denied types, we return FALSE immediately
733 if ($fileExtensionPermissions['deny'] === '*' || GeneralUtility::inList($fileExtensionPermissions['deny'], $fileExtension)) {
734 return false;
735 }
736 // If no match we return TRUE
737 return true;
738 }
739 if ($fileExtensionPermissions['allow'] === '*') {
740 return true;
741 }
742 if ($fileExtensionPermissions['deny'] === '*') {
743 return false;
744 }
745 return true;
746 }
747 return $isAllowed;
748 }
749
750 /**
751 * Assures read permission for given folder.
752 *
753 * @param Folder $folder If a folder is given, mountpoints are checked. If not only user folder read permissions are checked.
754 * @throws Exception\InsufficientFolderAccessPermissionsException
755 */
756 protected function assureFolderReadPermission(Folder $folder = null)
757 {
758 if (!$this->checkFolderActionPermission('read', $folder)) {
759 if ($folder === null) {
760 throw new Exception\InsufficientFolderAccessPermissionsException(
761 'You are not allowed to read folders',
762 1430657869
763 );
764 }
765 throw new Exception\InsufficientFolderAccessPermissionsException(
766 'You are not allowed to access the given folder: "' . $folder->getName() . '"',
767 1375955684
768 );
769 }
770 }
771
772 /**
773 * Assures delete permission for given folder.
774 *
775 * @param Folder $folder If a folder is given, mountpoints are checked. If not only user folder delete permissions are checked.
776 * @param bool $checkDeleteRecursively
777 * @throws Exception\InsufficientFolderAccessPermissionsException
778 * @throws Exception\InsufficientFolderWritePermissionsException
779 * @throws Exception\InsufficientUserPermissionsException
780 */
781 protected function assureFolderDeletePermission(Folder $folder, $checkDeleteRecursively)
782 {
783 // Check user permissions for recursive deletion if it is requested
784 if ($checkDeleteRecursively && !$this->checkUserActionPermission('recursivedelete', 'Folder')) {
785 throw new Exception\InsufficientUserPermissionsException('You are not allowed to delete folders recursively', 1377779423);
786 }
787 // Check user action permission
788 if (!$this->checkFolderActionPermission('delete', $folder)) {
789 throw new Exception\InsufficientFolderAccessPermissionsException(
790 'You are not allowed to delete the given folder: "' . $folder->getName() . '"',
791 1377779039
792 );
793 }
794 // Check if the user has write permissions to folders
795 // Would be good if we could check for actual write permissions in the containig folder
796 // but we cannot since we have no access to the containing folder of this file.
797 if (!$this->checkUserActionPermission('write', 'Folder')) {
798 throw new Exception\InsufficientFolderWritePermissionsException('Writing to folders is not allowed.', 1377779111);
799 }
800 }
801
802 /**
803 * Assures read permission for given file.
804 *
805 * @param FileInterface $file
806 * @throws Exception\InsufficientFileAccessPermissionsException
807 * @throws Exception\IllegalFileExtensionException
808 */
809 protected function assureFileReadPermission(FileInterface $file)
810 {
811 if (!$this->checkFileActionPermission('read', $file)) {
812 throw new Exception\InsufficientFileAccessPermissionsException(
813 'You are not allowed to access that file: "' . $file->getName() . '"',
814 1375955429
815 );
816 }
817 if (!$this->checkFileExtensionPermission($file->getName())) {
818 throw new Exception\IllegalFileExtensionException(
819 'You are not allowed to use that file extension. File: "' . $file->getName() . '"',
820 1375955430
821 );
822 }
823 }
824
825 /**
826 * Assures write permission for given file.
827 *
828 * @param FileInterface $file
829 * @throws Exception\IllegalFileExtensionException
830 * @throws Exception\InsufficientFileWritePermissionsException
831 * @throws Exception\InsufficientUserPermissionsException
832 */
833 protected function assureFileWritePermissions(FileInterface $file)
834 {
835 // Check if user is allowed to write the file and $file is writable
836 if (!$this->checkFileActionPermission('write', $file)) {
837 throw new Exception\InsufficientFileWritePermissionsException('Writing to file "' . $file->getIdentifier() . '" is not allowed.', 1330121088);
838 }
839 if (!$this->checkFileExtensionPermission($file->getName())) {
840 throw new Exception\IllegalFileExtensionException('You are not allowed to edit a file with extension "' . $file->getExtension() . '"', 1366711933);
841 }
842 }
843
844 /**
845 * Assure replace permission for given file.
846 *
847 * @param FileInterface $file
848 * @throws Exception\InsufficientFileWritePermissionsException
849 * @throws Exception\InsufficientFolderWritePermissionsException
850 */
851 protected function assureFileReplacePermissions(FileInterface $file)
852 {
853 // Check if user is allowed to replace the file and $file is writable
854 if (!$this->checkFileActionPermission('replace', $file)) {
855 throw new Exception\InsufficientFileWritePermissionsException('Replacing file "' . $file->getIdentifier() . '" is not allowed.', 1436899571);
856 }
857 // Check if parentFolder is writable for the user
858 if (!$this->checkFolderActionPermission('write', $file->getParentFolder())) {
859 throw new Exception\InsufficientFolderWritePermissionsException('You are not allowed to write to the target folder "' . $file->getIdentifier() . '"', 1436899572);
860 }
861 }
862
863 /**
864 * Assures delete permission for given file.
865 *
866 * @param FileInterface $file
867 * @throws Exception\IllegalFileExtensionException
868 * @throws Exception\InsufficientFileWritePermissionsException
869 * @throws Exception\InsufficientFolderWritePermissionsException
870 */
871 protected function assureFileDeletePermissions(FileInterface $file)
872 {
873 // Check for disallowed file extensions
874 if (!$this->checkFileExtensionPermission($file->getName())) {
875 throw new Exception\IllegalFileExtensionException('You are not allowed to delete a file with extension "' . $file->getExtension() . '"', 1377778916);
876 }
877 // Check further permissions if file is not a processed file
878 if (!$file instanceof ProcessedFile) {
879 // Check if user is allowed to delete the file and $file is writable
880 if (!$this->checkFileActionPermission('delete', $file)) {
881 throw new Exception\InsufficientFileWritePermissionsException('You are not allowed to delete the file "' . $file->getIdentifier() . '"', 1319550425);
882 }
883 // Check if the user has write permissions to folders
884 // Would be good if we could check for actual write permissions in the containig folder
885 // but we cannot since we have no access to the containing folder of this file.
886 if (!$this->checkUserActionPermission('write', 'Folder')) {
887 throw new Exception\InsufficientFolderWritePermissionsException('Writing to folders is not allowed.', 1377778702);
888 }
889 }
890 }
891
892 /**
893 * Checks if a file/user has the permission to be written to a Folder/Storage.
894 * If not, throws an exception.
895 *
896 * @param Folder $targetFolder The target folder where the file should be written
897 * @param string $targetFileName The file name which should be written into the storage
898 *
899 * @throws Exception\InsufficientFolderWritePermissionsException
900 * @throws Exception\IllegalFileExtensionException
901 * @throws Exception\InsufficientUserPermissionsException
902 */
903 protected function assureFileAddPermissions($targetFolder, $targetFileName)
904 {
905 // Check for a valid file extension
906 if (!$this->checkFileExtensionPermission($targetFileName)) {
907 throw new Exception\IllegalFileExtensionException('Extension of file name is not allowed in "' . $targetFileName . '"!', 1322120271);
908 }
909 // Makes sure the user is allowed to upload
910 if (!$this->checkUserActionPermission('add', 'File')) {
911 throw new Exception\InsufficientUserPermissionsException('You are not allowed to add files to this storage "' . $this->getUid() . '"', 1376992145);
912 }
913 // Check if targetFolder is writable
914 if (!$this->checkFolderActionPermission('write', $targetFolder)) {
915 throw new Exception\InsufficientFolderWritePermissionsException('You are not allowed to write to the target folder "' . $targetFolder->getIdentifier() . '"', 1322120356);
916 }
917 }
918
919 /**
920 * Checks if a file has the permission to be uploaded to a Folder/Storage.
921 * If not, throws an exception.
922 *
923 * @param string $localFilePath the temporary file name from $_FILES['file1']['tmp_name']
924 * @param Folder $targetFolder The target folder where the file should be uploaded
925 * @param string $targetFileName the destination file name $_FILES['file1']['name']
926 * @param int $uploadedFileSize
927 *
928 * @throws Exception\InsufficientFolderWritePermissionsException
929 * @throws Exception\UploadException
930 * @throws Exception\IllegalFileExtensionException
931 * @throws Exception\UploadSizeException
932 * @throws Exception\InsufficientUserPermissionsException
933 */
934 protected function assureFileUploadPermissions($localFilePath, $targetFolder, $targetFileName, $uploadedFileSize)
935 {
936 // Makes sure this is an uploaded file
937 if (!is_uploaded_file($localFilePath)) {
938 throw new Exception\UploadException('The upload has failed, no uploaded file found!', 1322110455);
939 }
940 // Max upload size (kb) for files.
941 $maxUploadFileSize = GeneralUtility::getMaxUploadFileSize() * 1024;
942 if ($maxUploadFileSize > 0 && $uploadedFileSize >= $maxUploadFileSize) {
943 unlink($localFilePath);
944 throw new Exception\UploadSizeException('The uploaded file exceeds the size-limit of ' . $maxUploadFileSize . ' bytes', 1322110041);
945 }
946 $this->assureFileAddPermissions($targetFolder, $targetFileName);
947 }
948
949 /**
950 * Checks for permissions to move a file.
951 *
952 * @throws \RuntimeException
953 * @throws Exception\InsufficientFolderAccessPermissionsException
954 * @throws Exception\InsufficientUserPermissionsException
955 * @throws Exception\IllegalFileExtensionException
956 * @param FileInterface $file
957 * @param Folder $targetFolder
958 * @param string $targetFileName
959 */
960 protected function assureFileMovePermissions(FileInterface $file, Folder $targetFolder, $targetFileName)
961 {
962 // Check if targetFolder is within this storage
963 if ($this->getUid() !== $targetFolder->getStorage()->getUid()) {
964 throw new \RuntimeException('The target folder is not in the same storage. Target folder given: "' . $targetFolder->getIdentifier() . '"', 1422553107);
965 }
966 // Check for a valid file extension
967 if (!$this->checkFileExtensionPermission($targetFileName)) {
968 throw new Exception\IllegalFileExtensionException('Extension of file name is not allowed in "' . $targetFileName . '"!', 1378243279);
969 }
970 // Check if user is allowed to move and $file is readable and writable
971 if (!$file->getStorage()->checkFileActionPermission('move', $file)) {
972 throw new Exception\InsufficientUserPermissionsException('You are not allowed to move files to storage "' . $this->getUid() . '"', 1319219349);
973 }
974 // Check if target folder is writable
975 if (!$this->checkFolderActionPermission('write', $targetFolder)) {
976 throw new Exception\InsufficientFolderAccessPermissionsException('You are not allowed to write to the target folder "' . $targetFolder->getIdentifier() . '"', 1319219350);
977 }
978 }
979
980 /**
981 * Checks for permissions to rename a file.
982 *
983 * @param FileInterface $file
984 * @param string $targetFileName
985 * @throws Exception\InsufficientFileWritePermissionsException
986 * @throws Exception\IllegalFileExtensionException
987 * @throws Exception\InsufficientFileReadPermissionsException
988 * @throws Exception\InsufficientUserPermissionsException
989 */
990 protected function assureFileRenamePermissions(FileInterface $file, $targetFileName)
991 {
992 // Check if file extension is allowed
993 if (!$this->checkFileExtensionPermission($targetFileName) || !$this->checkFileExtensionPermission($file->getName())) {
994 throw new Exception\IllegalFileExtensionException('You are not allowed to rename a file with this extension. File given: "' . $file->getName() . '"', 1371466663);
995 }
996 // Check if user is allowed to rename
997 if (!$this->checkFileActionPermission('rename', $file)) {
998 throw new Exception\InsufficientUserPermissionsException('You are not allowed to rename files. File given: "' . $file->getName() . '"', 1319219351);
999 }
1000 // Check if the user is allowed to write to folders
1001 // Although it would be good to check, we cannot check here if the folder actually is writable
1002 // because we do not know in which folder the file resides.
1003 // So we rely on the driver to throw an exception in case the renaming failed.
1004 if (!$this->checkFolderActionPermission('write')) {
1005 throw new Exception\InsufficientFileWritePermissionsException('You are not allowed to write to folders', 1319219352);
1006 }
1007 }
1008
1009 /**
1010 * Check if a file has the permission to be copied on a File/Folder/Storage,
1011 * if not throw an exception
1012 *
1013 * @param FileInterface $file
1014 * @param Folder $targetFolder
1015 * @param string $targetFileName
1016 *
1017 * @throws Exception
1018 * @throws Exception\InsufficientFolderWritePermissionsException
1019 * @throws Exception\IllegalFileExtensionException
1020 * @throws Exception\InsufficientFileReadPermissionsException
1021 * @throws Exception\InsufficientUserPermissionsException
1022 */
1023 protected function assureFileCopyPermissions(FileInterface $file, Folder $targetFolder, $targetFileName)
1024 {
1025 // Check if targetFolder is within this storage, this should never happen
1026 if ($this->getUid() != $targetFolder->getStorage()->getUid()) {
1027 throw new Exception('The operation of the folder cannot be called by this storage "' . $this->getUid() . '"', 1319550405);
1028 }
1029 // Check if user is allowed to copy
1030 if (!$file->getStorage()->checkFileActionPermission('copy', $file)) {
1031 throw new Exception\InsufficientFileReadPermissionsException('You are not allowed to copy the file "' . $file->getIdentifier() . '"', 1319550426);
1032 }
1033 // Check if targetFolder is writable
1034 if (!$this->checkFolderActionPermission('write', $targetFolder)) {
1035 throw new Exception\InsufficientFolderWritePermissionsException('You are not allowed to write to the target folder "' . $targetFolder->getIdentifier() . '"', 1319550435);
1036 }
1037 // Check for a valid file extension
1038 if (!$this->checkFileExtensionPermission($targetFileName) || !$this->checkFileExtensionPermission($file->getName())) {
1039 throw new Exception\IllegalFileExtensionException('You are not allowed to copy a file of that type.', 1319553317);
1040 }
1041 }
1042
1043 /**
1044 * Check if a file has the permission to be copied on a File/Folder/Storage,
1045 * if not throw an exception
1046 *
1047 * @param FolderInterface $folderToCopy
1048 * @param FolderInterface $targetParentFolder
1049 *
1050 * @throws Exception
1051 * @throws Exception\InsufficientFolderWritePermissionsException
1052 * @throws Exception\IllegalFileExtensionException
1053 * @throws Exception\InsufficientFileReadPermissionsException
1054 * @throws Exception\InsufficientUserPermissionsException
1055 * @throws \RuntimeException
1056 */
1057 protected function assureFolderCopyPermissions(FolderInterface $folderToCopy, FolderInterface $targetParentFolder)
1058 {
1059 // Check if targetFolder is within this storage, this should never happen
1060 if ($this->getUid() !== $targetParentFolder->getStorage()->getUid()) {
1061 throw new Exception('The operation of the folder cannot be called by this storage "' . $this->getUid() . '"', 1377777624);
1062 }
1063 if (!$folderToCopy instanceof Folder) {
1064 throw new \RuntimeException('The folder "' . $folderToCopy->getIdentifier() . '" to copy is not of type folder.', 1384209020);
1065 }
1066 // Check if user is allowed to copy and the folder is readable
1067 if (!$folderToCopy->getStorage()->checkFolderActionPermission('copy', $folderToCopy)) {
1068 throw new Exception\InsufficientFileReadPermissionsException('You are not allowed to copy the folder "' . $folderToCopy->getIdentifier() . '"', 1377777629);
1069 }
1070 if (!$targetParentFolder instanceof Folder) {
1071 throw new \RuntimeException('The target folder "' . $targetParentFolder->getIdentifier() . '" is not of type folder.', 1384209021);
1072 }
1073 // Check if targetFolder is writable
1074 if (!$this->checkFolderActionPermission('write', $targetParentFolder)) {
1075 throw new Exception\InsufficientFolderWritePermissionsException('You are not allowed to write to the target folder "' . $targetParentFolder->getIdentifier() . '"', 1377777635);
1076 }
1077 }
1078
1079 /**
1080 * Check if a file has the permission to be copied on a File/Folder/Storage,
1081 * if not throw an exception
1082 *
1083 * @param FolderInterface $folderToMove
1084 * @param FolderInterface $targetParentFolder
1085 *
1086 * @throws \InvalidArgumentException
1087 * @throws Exception\InsufficientFolderWritePermissionsException
1088 * @throws Exception\IllegalFileExtensionException
1089 * @throws Exception\InsufficientFileReadPermissionsException
1090 * @throws Exception\InsufficientUserPermissionsException
1091 * @throws \RuntimeException
1092 */
1093 protected function assureFolderMovePermissions(FolderInterface $folderToMove, FolderInterface $targetParentFolder)
1094 {
1095 // Check if targetFolder is within this storage, this should never happen
1096 if ($this->getUid() !== $targetParentFolder->getStorage()->getUid()) {
1097 throw new \InvalidArgumentException('Cannot move a folder into a folder that does not belong to this storage.', 1325777289);
1098 }
1099 if (!$folderToMove instanceof Folder) {
1100 throw new \RuntimeException('The folder "' . $folderToMove->getIdentifier() . '" to move is not of type Folder.', 1384209022);
1101 }
1102 // Check if user is allowed to move and the folder is writable
1103 // In fact we would need to check if the parent folder of the folder to move is writable also
1104 // But as of now we cannot extract the parent folder from this folder
1105 if (!$folderToMove->getStorage()->checkFolderActionPermission('move', $folderToMove)) {
1106 throw new Exception\InsufficientFileReadPermissionsException('You are not allowed to copy the folder "' . $folderToMove->getIdentifier() . '"', 1377778045);
1107 }
1108 if (!$targetParentFolder instanceof Folder) {
1109 throw new \RuntimeException('The target folder "' . $targetParentFolder->getIdentifier() . '" is not of type Folder.', 1384209023);
1110 }
1111 // Check if targetFolder is writable
1112 if (!$this->checkFolderActionPermission('write', $targetParentFolder)) {
1113 throw new Exception\InsufficientFolderWritePermissionsException('You are not allowed to write to the target folder "' . $targetParentFolder->getIdentifier() . '"', 1377778049);
1114 }
1115 }
1116
1117 /**
1118 * Clean a fileName from not allowed characters
1119 *
1120 * @param string $fileName The name of the file to be add, If not set, the local file name is used
1121 * @param Folder $targetFolder The target folder where the file should be added
1122 *
1123 * @throws \InvalidArgumentException
1124 * @throws Exception\ExistingTargetFileNameException
1125 * @return FileInterface
1126 */
1127 public function sanitizeFileName($fileName, Folder $targetFolder = null)
1128 {
1129 $targetFolder = $targetFolder ?: $this->getDefaultFolder();
1130 $fileName = $this->driver->sanitizeFileName($fileName);
1131
1132 // The file name could be changed by an external slot
1133 $fileName = $this->emitSanitizeFileNameSignal($fileName, $targetFolder);
1134
1135 return $fileName;
1136 }
1137
1138 /********************
1139 * FILE ACTIONS
1140 ********************/
1141 /**
1142 * Moves a file from the local filesystem to this storage.
1143 *
1144 * @param string $localFilePath The file on the server's hard disk to add
1145 * @param Folder $targetFolder The target folder where the file should be added
1146 * @param string $targetFileName The name of the file to be add, If not set, the local file name is used
1147 * @param string $conflictMode a value of the DuplicationBehavior enumeration
1148 * @param bool $removeOriginal if set the original file will be removed after successful operation
1149 *
1150 * @throws \InvalidArgumentException
1151 * @throws Exception\ExistingTargetFileNameException
1152 * @return FileInterface
1153 */
1154 public function addFile($localFilePath, Folder $targetFolder, $targetFileName = '', $conflictMode = DuplicationBehavior::RENAME, $removeOriginal = true)
1155 {
1156 $localFilePath = PathUtility::getCanonicalPath($localFilePath);
1157 // File is not available locally NOR is it an uploaded file
1158 if (!is_uploaded_file($localFilePath) && !file_exists($localFilePath)) {
1159 throw new \InvalidArgumentException('File "' . $localFilePath . '" does not exist.', 1319552745);
1160 }
1161 $conflictMode = DuplicationBehavior::cast($conflictMode);
1162 $targetFolder = $targetFolder ?: $this->getDefaultFolder();
1163 $targetFileName = $this->sanitizeFileName($targetFileName ?: PathUtility::basename($localFilePath), $targetFolder);
1164
1165 $targetFileName = $this->emitPreFileAddSignal($targetFileName, $targetFolder, $localFilePath);
1166
1167 $this->assureFileAddPermissions($targetFolder, $targetFileName);
1168
1169 $replaceExisting = false;
1170 if ($conflictMode->equals(DuplicationBehavior::CANCEL) && $this->driver->fileExistsInFolder($targetFileName, $targetFolder->getIdentifier())) {
1171 throw new Exception\ExistingTargetFileNameException('File "' . $targetFileName . '" already exists in folder ' . $targetFolder->getIdentifier(), 1322121068);
1172 }
1173 if ($conflictMode->equals(DuplicationBehavior::RENAME)) {
1174 $targetFileName = $this->getUniqueName($targetFolder, $targetFileName);
1175 } elseif ($conflictMode->equals(DuplicationBehavior::REPLACE) && $this->driver->fileExistsInFolder($targetFileName, $targetFolder->getIdentifier())) {
1176 $replaceExisting = true;
1177 }
1178
1179 $fileIdentifier = $this->driver->addFile($localFilePath, $targetFolder->getIdentifier(), $targetFileName, $removeOriginal);
1180 $file = $this->getResourceFactoryInstance()->getFileObjectByStorageAndIdentifier($this->getUid(), $fileIdentifier);
1181
1182 if ($replaceExisting && $file instanceof File) {
1183 $this->getIndexer()->updateIndexEntry($file);
1184 }
1185 if ($this->autoExtractMetadataEnabled()) {
1186 $this->getIndexer()->extractMetaData($file);
1187 }
1188
1189 $this->emitPostFileAddSignal($file, $targetFolder);
1190
1191 return $file;
1192 }
1193
1194 /**
1195 * Updates a processed file with a new file from the local filesystem.
1196 *
1197 * @param string $localFilePath
1198 * @param ProcessedFile $processedFile
1199 * @param Folder $processingFolder
1200 * @return FileInterface
1201 * @throws \InvalidArgumentException
1202 * @internal use only
1203 */
1204 public function updateProcessedFile($localFilePath, ProcessedFile $processedFile, Folder $processingFolder = null)
1205 {
1206 if (!file_exists($localFilePath)) {
1207 throw new \InvalidArgumentException('File "' . $localFilePath . '" does not exist.', 1319552746);
1208 }
1209 if ($processingFolder === null) {
1210 $processingFolder = $this->getProcessingFolder($processedFile->getOriginalFile());
1211 }
1212 $fileIdentifier = $this->driver->addFile($localFilePath, $processingFolder->getIdentifier(), $processedFile->getName());
1213 // @todo check if we have to update the processed file other then the identifier
1214 $processedFile->setIdentifier($fileIdentifier);
1215 return $processedFile;
1216 }
1217
1218 /**
1219 * Creates a (cryptographic) hash for a file.
1220 *
1221 * @param FileInterface $fileObject
1222 * @param string $hash
1223 * @return string
1224 */
1225 public function hashFile(FileInterface $fileObject, $hash)
1226 {
1227 return $this->hashFileByIdentifier($fileObject->getIdentifier(), $hash);
1228 }
1229
1230 /**
1231 * Creates a (cryptographic) hash for a fileIdentifier.
1232
1233 * @param string $fileIdentifier
1234 * @param string $hash
1235 *
1236 * @return string
1237 */
1238 public function hashFileByIdentifier($fileIdentifier, $hash)
1239 {
1240 return $this->driver->hash($fileIdentifier, $hash);
1241 }
1242
1243 /**
1244 * Hashes a file identifier, taking the case sensitivity of the file system
1245 * into account. This helps mitigating problems with case-insensitive
1246 * databases.
1247 *
1248 * @param string|FileInterface $file
1249 * @return string
1250 */
1251 public function hashFileIdentifier($file)
1252 {
1253 if (is_object($file) && $file instanceof FileInterface) {
1254 /** @var FileInterface $file */
1255 $file = $file->getIdentifier();
1256 }
1257 return $this->driver->hashIdentifier($file);
1258 }
1259
1260 /**
1261 * Returns a publicly accessible URL for a file.
1262 *
1263 * WARNING: Access to the file may be restricted by further means, e.g.
1264 * some web-based authentication. You have to take care of this yourself.
1265 *
1266 * @param ResourceInterface $resourceObject The file or folder object
1267 * @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)
1268 * @return string|null NULL if file is missing or deleted, the generated url otherwise
1269 */
1270 public function getPublicUrl(ResourceInterface $resourceObject, $relativeToCurrentScript = false)
1271 {
1272 $publicUrl = null;
1273 if ($this->isOnline()) {
1274 // Pre-process the public URL by an accordant slot
1275 $this->emitPreGeneratePublicUrlSignal($resourceObject, $relativeToCurrentScript, ['publicUrl' => &$publicUrl]);
1276
1277 if (
1278 $publicUrl === null
1279 && $resourceObject instanceof File
1280 && ($helper = OnlineMediaHelperRegistry::getInstance()->getOnlineMediaHelper($resourceObject)) !== false
1281 ) {
1282 $publicUrl = $helper->getPublicUrl($resourceObject, $relativeToCurrentScript);
1283 }
1284
1285 // If slot did not handle the signal, use the default way to determine public URL
1286 if ($publicUrl === null) {
1287 if ($this->hasCapability(self::CAPABILITY_PUBLIC)) {
1288 $publicUrl = $this->driver->getPublicUrl($resourceObject->getIdentifier());
1289 }
1290
1291 if ($publicUrl === null && $resourceObject instanceof FileInterface) {
1292 $queryParameterArray = ['eID' => 'dumpFile', 't' => ''];
1293 if ($resourceObject instanceof File) {
1294 $queryParameterArray['f'] = $resourceObject->getUid();
1295 $queryParameterArray['t'] = 'f';
1296 } elseif ($resourceObject instanceof ProcessedFile) {
1297 $queryParameterArray['p'] = $resourceObject->getUid();
1298 $queryParameterArray['t'] = 'p';
1299 }
1300
1301 $queryParameterArray['token'] = GeneralUtility::hmac(implode('|', $queryParameterArray), 'resourceStorageDumpFile');
1302 $publicUrl = GeneralUtility::locationHeaderUrl(PathUtility::getAbsoluteWebPath(PATH_site . 'index.php'));
1303 $publicUrl .= '?' . http_build_query($queryParameterArray, '', '&', PHP_QUERY_RFC3986);
1304 }
1305
1306 // If requested, make the path relative to the current script in order to make it possible
1307 // to use the relative file
1308 if ($publicUrl !== null && $relativeToCurrentScript && !GeneralUtility::isValidUrl($publicUrl)) {
1309 $absolutePathToContainingFolder = PathUtility::dirname(PATH_site . $publicUrl);
1310 $pathPart = PathUtility::getRelativePathTo($absolutePathToContainingFolder);
1311 $filePart = substr(PATH_site . $publicUrl, strlen($absolutePathToContainingFolder) + 1);
1312 $publicUrl = $pathPart . $filePart;
1313 }
1314 }
1315 }
1316 return $publicUrl;
1317 }
1318
1319 /**
1320 * Passes a file to the File Processing Services and returns the resulting ProcessedFile object.
1321 *
1322 * @param FileInterface $fileObject The file object
1323 * @param string $context
1324 * @param array $configuration
1325 *
1326 * @return ProcessedFile
1327 * @throws \InvalidArgumentException
1328 */
1329 public function processFile(FileInterface $fileObject, $context, array $configuration)
1330 {
1331 if ($fileObject->getStorage() !== $this) {
1332 throw new \InvalidArgumentException('Cannot process files of foreign storage', 1353401835);
1333 }
1334 $processedFile = $this->getFileProcessingService()->processFile($fileObject, $this, $context, $configuration);
1335
1336 return $processedFile;
1337 }
1338
1339 /**
1340 * Copies a file from the storage for local processing.
1341 *
1342 * @param FileInterface $fileObject
1343 * @param bool $writable
1344 * @return string Path to local file (either original or copied to some temporary local location)
1345 */
1346 public function getFileForLocalProcessing(FileInterface $fileObject, $writable = true)
1347 {
1348 $filePath = $this->driver->getFileForLocalProcessing($fileObject->getIdentifier(), $writable);
1349 return $filePath;
1350 }
1351
1352 /**
1353 * Gets a file by identifier.
1354 *
1355 * @param string $identifier
1356 * @return FileInterface
1357 */
1358 public function getFile($identifier)
1359 {
1360 $file = $this->getFileFactory()->getFileObjectByStorageAndIdentifier($this->getUid(), $identifier);
1361 if (!$this->driver->fileExists($identifier)) {
1362 $file->setMissing(true);
1363 }
1364 return $file;
1365 }
1366
1367 /**
1368 * Gets information about a file.
1369 *
1370 * @param FileInterface $fileObject
1371 * @return array
1372 * @internal
1373 */
1374 public function getFileInfo(FileInterface $fileObject)
1375 {
1376 return $this->getFileInfoByIdentifier($fileObject->getIdentifier());
1377 }
1378
1379 /**
1380 * Gets information about a file by its identifier.
1381 *
1382 * @param string $identifier
1383 * @param array $propertiesToExtract
1384 * @return array
1385 * @internal
1386 */
1387 public function getFileInfoByIdentifier($identifier, array $propertiesToExtract = [])
1388 {
1389 return $this->driver->getFileInfoByIdentifier($identifier, $propertiesToExtract);
1390 }
1391
1392 /**
1393 * Unsets the file and folder name filters, thus making this storage return unfiltered filelists.
1394 */
1395 public function unsetFileAndFolderNameFilters()
1396 {
1397 $this->fileAndFolderNameFilters = [];
1398 }
1399
1400 /**
1401 * Resets the file and folder name filters to the default values defined in the TYPO3 configuration.
1402 */
1403 public function resetFileAndFolderNameFiltersToDefault()
1404 {
1405 $this->fileAndFolderNameFilters = $GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['defaultFilterCallbacks'];
1406 }
1407
1408 /**
1409 * Returns the file and folder name filters used by this storage.
1410 *
1411 * @return array
1412 */
1413 public function getFileAndFolderNameFilters()
1414 {
1415 return $this->fileAndFolderNameFilters;
1416 }
1417
1418 /**
1419 * @param array $filters
1420 * @return $this
1421 */
1422 public function setFileAndFolderNameFilters(array $filters)
1423 {
1424 $this->fileAndFolderNameFilters = $filters;
1425 return $this;
1426 }
1427
1428 /**
1429 * @param callable $filter
1430 */
1431 public function addFileAndFolderNameFilter($filter)
1432 {
1433 $this->fileAndFolderNameFilters[] = $filter;
1434 }
1435
1436 /**
1437 * @param string $fileIdentifier
1438 *
1439 * @return string
1440 */
1441 public function getFolderIdentifierFromFileIdentifier($fileIdentifier)
1442 {
1443 return $this->driver->getParentFolderIdentifierOfIdentifier($fileIdentifier);
1444 }
1445
1446 /**
1447 * Get file from folder
1448 *
1449 * @param string $fileName
1450 * @param Folder $folder
1451 * @return File|ProcessedFile|null
1452 */
1453 public function getFileInFolder($fileName, Folder $folder)
1454 {
1455 $identifier = $this->driver->getFileInFolder($fileName, $folder->getIdentifier());
1456 return $this->getFileFactory()->getFileObjectByStorageAndIdentifier($this->getUid(), $identifier);
1457 }
1458
1459 /**
1460 * @param Folder $folder
1461 * @param int $start
1462 * @param int $maxNumberOfItems
1463 * @param bool $useFilters
1464 * @param bool $recursive
1465 * @param string $sort Property name used to sort the items.
1466 * Among them may be: '' (empty, no sorting), name,
1467 * fileext, size, tstamp and rw.
1468 * If a driver does not support the given property, it
1469 * should fall back to "name".
1470 * @param bool $sortRev TRUE to indicate reverse sorting (last to first)
1471 * @return File[]
1472 * @throws Exception\InsufficientFolderAccessPermissionsException
1473 */
1474 public function getFilesInFolder(Folder $folder, $start = 0, $maxNumberOfItems = 0, $useFilters = true, $recursive = false, $sort = '', $sortRev = false)
1475 {
1476 $this->assureFolderReadPermission($folder);
1477
1478 $rows = $this->getFileIndexRepository()->findByFolder($folder);
1479
1480 $filters = $useFilters == true ? $this->fileAndFolderNameFilters : [];
1481 $fileIdentifiers = array_values($this->driver->getFilesInFolder($folder->getIdentifier(), $start, $maxNumberOfItems, $recursive, $filters, $sort, $sortRev));
1482
1483 $items = [];
1484 foreach ($fileIdentifiers as $identifier) {
1485 if (isset($rows[$identifier])) {
1486 $fileObject = $this->getFileFactory()->getFileObject($rows[$identifier]['uid'], $rows[$identifier]);
1487 } else {
1488 $fileObject = $this->getFileFactory()->getFileObjectByStorageAndIdentifier($this->getUid(), $identifier);
1489 }
1490 if ($fileObject instanceof FileInterface) {
1491 $key = $fileObject->getName();
1492 while (isset($items[$key])) {
1493 $key .= 'z';
1494 }
1495 $items[$key] = $fileObject;
1496 }
1497 }
1498
1499 return $items;
1500 }
1501
1502 /**
1503 * @param string $folderIdentifier
1504 * @param bool $useFilters
1505 * @param bool $recursive
1506 * @return array
1507 */
1508 public function getFileIdentifiersInFolder($folderIdentifier, $useFilters = true, $recursive = false)
1509 {
1510 $filters = $useFilters == true ? $this->fileAndFolderNameFilters : [];
1511 return $this->driver->getFilesInFolder($folderIdentifier, 0, 0, $recursive, $filters);
1512 }
1513
1514 /**
1515 * @param Folder $folder
1516 * @param bool $useFilters
1517 * @param bool $recursive
1518 * @return int Number of files in folder
1519 * @throws Exception\InsufficientFolderAccessPermissionsException
1520 */
1521 public function countFilesInFolder(Folder $folder, $useFilters = true, $recursive = false)
1522 {
1523 $this->assureFolderReadPermission($folder);
1524 $filters = $useFilters ? $this->fileAndFolderNameFilters : [];
1525 return $this->driver->countFilesInFolder($folder->getIdentifier(), $recursive, $filters);
1526 }
1527
1528 /**
1529 * @param string $folderIdentifier
1530 * @param bool $useFilters
1531 * @param bool $recursive
1532 * @return array
1533 */
1534 public function getFolderIdentifiersInFolder($folderIdentifier, $useFilters = true, $recursive = false)
1535 {
1536 $filters = $useFilters == true ? $this->fileAndFolderNameFilters : [];
1537 return $this->driver->getFoldersInFolder($folderIdentifier, 0, 0, $recursive, $filters);
1538 }
1539
1540 /**
1541 * Returns TRUE if the specified file exists
1542 *
1543 * @param string $identifier
1544 * @return bool
1545 */
1546 public function hasFile($identifier)
1547 {
1548 // Allow if identifier is in processing folder
1549 if (!$this->isWithinProcessingFolder($identifier)) {
1550 $this->assureFolderReadPermission();
1551 }
1552 return $this->driver->fileExists($identifier);
1553 }
1554
1555 /**
1556 * Get all processing folders that live in this storage
1557 *
1558 * @return Folder[]
1559 */
1560 public function getProcessingFolders()
1561 {
1562 if ($this->processingFolders === null) {
1563 $this->processingFolders = [];
1564 $this->processingFolders[] = $this->getProcessingFolder();
1565 /** @var $storageRepository StorageRepository */
1566 $storageRepository = GeneralUtility::makeInstance(StorageRepository::class);
1567 $allStorages = $storageRepository->findAll();
1568 foreach ($allStorages as $storage) {
1569 // To circumvent the permission check of the folder, we use the factory to create it "manually" instead of directly using $storage->getProcessingFolder()
1570 // See #66695 for details
1571 list($storageUid, $processingFolderIdentifier) = GeneralUtility::trimExplode(':', $storage->getStorageRecord()['processingfolder']);
1572 if (empty($processingFolderIdentifier) || (int)$storageUid !== $this->getUid()) {
1573 continue;
1574 }
1575 $potentialProcessingFolder = $this->getResourceFactoryInstance()->getInstance()->createFolderObject($this, $processingFolderIdentifier, $processingFolderIdentifier);
1576 if ($potentialProcessingFolder->getStorage() === $this && $potentialProcessingFolder->getIdentifier() !== $this->getProcessingFolder()->getIdentifier()) {
1577 $this->processingFolders[] = $potentialProcessingFolder;
1578 }
1579 }
1580 }
1581
1582 return $this->processingFolders;
1583 }
1584
1585 /**
1586 * Returns TRUE if folder that is in current storage is set as
1587 * processing folder for one of the existing storages
1588 *
1589 * @param Folder $folder
1590 * @return bool
1591 */
1592 public function isProcessingFolder(Folder $folder)
1593 {
1594 $isProcessingFolder = false;
1595 foreach ($this->getProcessingFolders() as $processingFolder) {
1596 if ($folder->getCombinedIdentifier() === $processingFolder->getCombinedIdentifier()) {
1597 $isProcessingFolder = true;
1598 break;
1599 }
1600 }
1601 return $isProcessingFolder;
1602 }
1603
1604 /**
1605 * Checks if the queried file in the given folder exists
1606 *
1607 * @param string $fileName
1608 * @param Folder $folder
1609 * @return bool
1610 */
1611 public function hasFileInFolder($fileName, Folder $folder)
1612 {
1613 $this->assureFolderReadPermission($folder);
1614 return $this->driver->fileExistsInFolder($fileName, $folder->getIdentifier());
1615 }
1616
1617 /**
1618 * Get contents of a file object
1619 *
1620 * @param FileInterface $file
1621 *
1622 * @throws Exception\InsufficientFileReadPermissionsException
1623 * @return string
1624 */
1625 public function getFileContents($file)
1626 {
1627 $this->assureFileReadPermission($file);
1628 return $this->driver->getFileContents($file->getIdentifier());
1629 }
1630
1631 /**
1632 * Outputs file Contents,
1633 * clears output buffer first and sends headers accordingly.
1634 *
1635 * @param FileInterface $file
1636 * @param bool $asDownload If set Content-Disposition attachment is sent, inline otherwise
1637 * @param string $alternativeFilename the filename for the download (if $asDownload is set)
1638 * @param string $overrideMimeType If set this will be used as Content-Type header instead of the automatically detected mime type.
1639 */
1640 public function dumpFileContents(FileInterface $file, $asDownload = false, $alternativeFilename = null, $overrideMimeType = null)
1641 {
1642 $downloadName = $alternativeFilename ?: $file->getName();
1643 $contentDisposition = $asDownload ? 'attachment' : 'inline';
1644 header('Content-Disposition: ' . $contentDisposition . '; filename="' . $downloadName . '"');
1645 header('Content-Type: ' . ($overrideMimeType ?: $file->getMimeType()));
1646 header('Content-Length: ' . $file->getSize());
1647
1648 // Cache-Control header is needed here to solve an issue with browser IE8 and lower
1649 // See for more information: http://support.microsoft.com/kb/323308
1650 header("Cache-Control: ''");
1651 header(
1652 'Last-Modified: ' .
1653 gmdate('D, d M Y H:i:s', array_pop($this->driver->getFileInfoByIdentifier($file->getIdentifier(), ['mtime']))) . ' GMT',
1654 true,
1655 200
1656 );
1657 ob_clean();
1658 flush();
1659 while (ob_get_level() > 0) {
1660 ob_end_clean();
1661 }
1662 $this->driver->dumpFileContents($file->getIdentifier());
1663 }
1664
1665 /**
1666 * Set contents of a file object.
1667 *
1668 * @param AbstractFile $file
1669 * @param string $contents
1670 *
1671 * @throws \Exception|\RuntimeException
1672 * @throws Exception\InsufficientFileWritePermissionsException
1673 * @throws Exception\InsufficientUserPermissionsException
1674 * @return int The number of bytes written to the file
1675 */
1676 public function setFileContents(AbstractFile $file, $contents)
1677 {
1678 // Check if user is allowed to edit
1679 $this->assureFileWritePermissions($file);
1680 // Call driver method to update the file and update file index entry afterwards
1681 $result = $this->driver->setFileContents($file->getIdentifier(), $contents);
1682 if ($file instanceof File) {
1683 $this->getIndexer()->updateIndexEntry($file);
1684 }
1685 $this->emitPostFileSetContentsSignal($file, $contents);
1686 return $result;
1687 }
1688
1689 /**
1690 * Creates a new file
1691 *
1692 * previously in \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::func_newfile()
1693 *
1694 * @param string $fileName The name of the file to be created
1695 * @param Folder $targetFolderObject The target folder where the file should be created
1696 *
1697 * @throws Exception\IllegalFileExtensionException
1698 * @throws Exception\InsufficientFolderWritePermissionsException
1699 * @return FileInterface The file object
1700 */
1701 public function createFile($fileName, Folder $targetFolderObject)
1702 {
1703 $this->assureFileAddPermissions($targetFolderObject, $fileName);
1704 $newFileIdentifier = $this->driver->createFile($fileName, $targetFolderObject->getIdentifier());
1705 $this->emitPostFileCreateSignal($newFileIdentifier, $targetFolderObject);
1706 return $this->getResourceFactoryInstance()->getFileObjectByStorageAndIdentifier($this->getUid(), $newFileIdentifier);
1707 }
1708
1709 /**
1710 * Previously in \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::deleteFile()
1711 *
1712 * @param FileInterface $fileObject
1713 * @throws Exception\InsufficientFileAccessPermissionsException
1714 * @throws Exception\FileOperationErrorException
1715 * @return bool TRUE if deletion succeeded
1716 */
1717 public function deleteFile($fileObject)
1718 {
1719 $this->assureFileDeletePermissions($fileObject);
1720
1721 $this->emitPreFileDeleteSignal($fileObject);
1722 $deleted = true;
1723
1724 if ($this->driver->fileExists($fileObject->getIdentifier())) {
1725 // Disable permission check to find nearest recycler and move file without errors
1726 $currentPermissions = $this->evaluatePermissions;
1727 $this->evaluatePermissions = false;
1728
1729 $recyclerFolder = $this->getNearestRecyclerFolder($fileObject);
1730 if ($recyclerFolder === null) {
1731 $result = $this->driver->deleteFile($fileObject->getIdentifier());
1732 } else {
1733 $result = $this->moveFile($fileObject, $recyclerFolder);
1734 $deleted = false;
1735 }
1736
1737 $this->evaluatePermissions = $currentPermissions;
1738
1739 if (!$result) {
1740 throw new Exception\FileOperationErrorException('Deleting the file "' . $fileObject->getIdentifier() . '\' failed.', 1329831691);
1741 }
1742 }
1743 // Mark the file object as deleted
1744 if ($deleted && $fileObject instanceof AbstractFile) {
1745 $fileObject->setDeleted();
1746 }
1747
1748 $this->emitPostFileDeleteSignal($fileObject);
1749
1750 return true;
1751 }
1752
1753 /**
1754 * Previously in \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::func_copy()
1755 * copies a source file (from any location) in to the target
1756 * folder, the latter has to be part of this storage
1757 *
1758 * @param FileInterface $file
1759 * @param Folder $targetFolder
1760 * @param string $targetFileName an optional destination fileName
1761 * @param string $conflictMode a value of the DuplicationBehavior enumeration
1762 *
1763 * @throws \Exception|Exception\AbstractFileOperationException
1764 * @throws Exception\ExistingTargetFileNameException
1765 * @return FileInterface
1766 */
1767 public function copyFile(FileInterface $file, Folder $targetFolder, $targetFileName = null, $conflictMode = DuplicationBehavior::RENAME)
1768 {
1769 $conflictMode = DuplicationBehavior::cast($conflictMode);
1770 if ($targetFileName === null) {
1771 $targetFileName = $file->getName();
1772 }
1773 $sanitizedTargetFileName = $this->driver->sanitizeFileName($targetFileName);
1774 $this->assureFileCopyPermissions($file, $targetFolder, $sanitizedTargetFileName);
1775 $this->emitPreFileCopySignal($file, $targetFolder);
1776 // File exists and we should abort, let's abort
1777 if ($conflictMode->equals(DuplicationBehavior::CANCEL) && $targetFolder->hasFile($sanitizedTargetFileName)) {
1778 throw new Exception\ExistingTargetFileNameException('The target file already exists.', 1320291064);
1779 }
1780 // File exists and we should find another name, let's find another one
1781 if ($conflictMode->equals(DuplicationBehavior::RENAME) && $targetFolder->hasFile($sanitizedTargetFileName)) {
1782 $sanitizedTargetFileName = $this->getUniqueName($targetFolder, $sanitizedTargetFileName);
1783 }
1784 $sourceStorage = $file->getStorage();
1785 // Call driver method to create a new file from an existing file object,
1786 // and return the new file object
1787 if ($sourceStorage === $this) {
1788 $newFileObjectIdentifier = $this->driver->copyFileWithinStorage($file->getIdentifier(), $targetFolder->getIdentifier(), $sanitizedTargetFileName);
1789 } else {
1790 $tempPath = $file->getForLocalProcessing();
1791 $newFileObjectIdentifier = $this->driver->addFile($tempPath, $targetFolder->getIdentifier(), $sanitizedTargetFileName);
1792 }
1793 $newFileObject = $this->getResourceFactoryInstance()->getFileObjectByStorageAndIdentifier($this->getUid(), $newFileObjectIdentifier);
1794 $this->emitPostFileCopySignal($file, $targetFolder);
1795 return $newFileObject;
1796 }
1797
1798 /**
1799 * Moves a $file into a $targetFolder
1800 * the target folder has to be part of this storage
1801 *
1802 * previously in \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::func_move()
1803 *
1804 * @param FileInterface $file
1805 * @param Folder $targetFolder
1806 * @param string $targetFileName an optional destination fileName
1807 * @param string $conflictMode a value of the DuplicationBehavior enumeration
1808 *
1809 * @throws Exception\ExistingTargetFileNameException
1810 * @throws \RuntimeException
1811 * @return FileInterface
1812 */
1813 public function moveFile($file, $targetFolder, $targetFileName = null, $conflictMode = DuplicationBehavior::RENAME)
1814 {
1815 $conflictMode = DuplicationBehavior::cast($conflictMode);
1816 if ($targetFileName === null) {
1817 $targetFileName = $file->getName();
1818 }
1819 $originalFolder = $file->getParentFolder();
1820 $sanitizedTargetFileName = $this->driver->sanitizeFileName($targetFileName);
1821 $this->assureFileMovePermissions($file, $targetFolder, $sanitizedTargetFileName);
1822 if ($targetFolder->hasFile($sanitizedTargetFileName)) {
1823 // File exists and we should abort, let's abort
1824 if ($conflictMode->equals(DuplicationBehavior::RENAME)) {
1825 $sanitizedTargetFileName = $this->getUniqueName($targetFolder, $sanitizedTargetFileName);
1826 } elseif ($conflictMode->equals(DuplicationBehavior::CANCEL)) {
1827 throw new Exception\ExistingTargetFileNameException('The target file already exists', 1329850997);
1828 }
1829 }
1830 $this->emitPreFileMoveSignal($file, $targetFolder);
1831 $sourceStorage = $file->getStorage();
1832 // Call driver method to move the file and update the index entry
1833 try {
1834 if ($sourceStorage === $this) {
1835 $newIdentifier = $this->driver->moveFileWithinStorage($file->getIdentifier(), $targetFolder->getIdentifier(), $sanitizedTargetFileName);
1836 if (!$file instanceof AbstractFile) {
1837 throw new \RuntimeException('The given file is not of type AbstractFile.', 1384209025);
1838 }
1839 $file->updateProperties(['identifier' => $newIdentifier]);
1840 } else {
1841 $tempPath = $file->getForLocalProcessing();
1842 $newIdentifier = $this->driver->addFile($tempPath, $targetFolder->getIdentifier(), $sanitizedTargetFileName);
1843
1844 // Disable permission check to find nearest recycler and move file without errors
1845 $currentPermissions = $sourceStorage->evaluatePermissions;
1846 $sourceStorage->evaluatePermissions = false;
1847
1848 $recyclerFolder = $sourceStorage->getNearestRecyclerFolder($file);
1849 if ($recyclerFolder === null) {
1850 $sourceStorage->driver->deleteFile($file->getIdentifier());
1851 } else {
1852 $sourceStorage->moveFile($file, $recyclerFolder);
1853 }
1854 $sourceStorage->evaluatePermissions = $currentPermissions;
1855 if ($file instanceof File) {
1856 $file->updateProperties(['storage' => $this->getUid(), 'identifier' => $newIdentifier]);
1857 }
1858 }
1859 $this->getIndexer()->updateIndexEntry($file);
1860 } catch (\TYPO3\CMS\Core\Exception $e) {
1861 echo $e->getMessage();
1862 }
1863 $this->emitPostFileMoveSignal($file, $targetFolder, $originalFolder);
1864 return $file;
1865 }
1866
1867 /**
1868 * Previously in \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::func_rename()
1869 *
1870 * @param FileInterface $file
1871 * @param string $targetFileName
1872 * @param string $conflictMode
1873 * @return FileInterface
1874 * @throws ExistingTargetFileNameException
1875 */
1876 public function renameFile($file, $targetFileName, $conflictMode = DuplicationBehavior::RENAME)
1877 {
1878 // The name should be different from the current.
1879 if ($file->getName() === $targetFileName) {
1880 return $file;
1881 }
1882 $sanitizedTargetFileName = $this->driver->sanitizeFileName($targetFileName);
1883 $this->assureFileRenamePermissions($file, $sanitizedTargetFileName);
1884 $this->emitPreFileRenameSignal($file, $sanitizedTargetFileName);
1885
1886 $conflictMode = DuplicationBehavior::cast($conflictMode);
1887
1888 // Call driver method to rename the file and update the index entry
1889 try {
1890 $newIdentifier = $this->driver->renameFile($file->getIdentifier(), $sanitizedTargetFileName);
1891 if ($file instanceof File) {
1892 $file->updateProperties(['identifier' => $newIdentifier]);
1893 }
1894 $this->getIndexer()->updateIndexEntry($file);
1895 } catch (ExistingTargetFileNameException $exception) {
1896 if ($conflictMode->equals(DuplicationBehavior::RENAME)) {
1897 $newName = $this->getUniqueName($file->getParentFolder(), $sanitizedTargetFileName);
1898 $file = $this->renameFile($file, $newName);
1899 } elseif ($conflictMode->equals(DuplicationBehavior::CANCEL)) {
1900 throw $exception;
1901 } elseif ($conflictMode->equals(DuplicationBehavior::REPLACE)) {
1902 $sourceFileIdentifier = substr($file->getCombinedIdentifier(), 0, strrpos($file->getCombinedIdentifier(), '/') + 1) . $targetFileName;
1903 $sourceFile = $this->getResourceFactoryInstance()->getFileObjectFromCombinedIdentifier($sourceFileIdentifier);
1904 $file = $this->replaceFile($sourceFile, PATH_site . $file->getPublicUrl());
1905 }
1906 } catch (\RuntimeException $e) {
1907 }
1908
1909 $this->emitPostFileRenameSignal($file, $sanitizedTargetFileName);
1910
1911 return $file;
1912 }
1913
1914 /**
1915 * Replaces a file with a local file (e.g. a freshly uploaded file)
1916 *
1917 * @param FileInterface $file
1918 * @param string $localFilePath
1919 *
1920 * @return FileInterface
1921 *
1922 * @throws Exception\IllegalFileExtensionException
1923 * @throws \InvalidArgumentException
1924 */
1925 public function replaceFile(FileInterface $file, $localFilePath)
1926 {
1927 $this->assureFileReplacePermissions($file);
1928 if (!file_exists($localFilePath)) {
1929 throw new \InvalidArgumentException('File "' . $localFilePath . '" does not exist.', 1325842622);
1930 }
1931 $this->emitPreFileReplaceSignal($file, $localFilePath);
1932 $this->driver->replaceFile($file->getIdentifier(), $localFilePath);
1933 if ($file instanceof File) {
1934 $this->getIndexer()->updateIndexEntry($file);
1935 }
1936 if ($this->autoExtractMetadataEnabled()) {
1937 $this->getIndexer()->extractMetaData($file);
1938 }
1939 $this->emitPostFileReplaceSignal($file, $localFilePath);
1940
1941 return $file;
1942 }
1943
1944 /**
1945 * Adds an uploaded file into the Storage. Previously in \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::file_upload()
1946 *
1947 * @param array $uploadedFileData contains information about the uploaded file given by $_FILES['file1']
1948 * @param Folder $targetFolder the target folder
1949 * @param string $targetFileName the file name to be written
1950 * @param string $conflictMode a value of the DuplicationBehavior enumeration
1951 * @return FileInterface The file object
1952 */
1953 public function addUploadedFile(array $uploadedFileData, Folder $targetFolder = null, $targetFileName = null, $conflictMode = DuplicationBehavior::CANCEL)
1954 {
1955 $conflictMode = DuplicationBehavior::cast($conflictMode);
1956 $localFilePath = $uploadedFileData['tmp_name'];
1957 if ($targetFolder === null) {
1958 $targetFolder = $this->getDefaultFolder();
1959 }
1960 if ($targetFileName === null) {
1961 $targetFileName = $uploadedFileData['name'];
1962 }
1963 $targetFileName = $this->driver->sanitizeFileName($targetFileName);
1964
1965 $this->assureFileUploadPermissions($localFilePath, $targetFolder, $targetFileName, $uploadedFileData['size']);
1966 if ($this->hasFileInFolder($targetFileName, $targetFolder) && $conflictMode->equals(DuplicationBehavior::REPLACE)) {
1967 $file = $this->getFileInFolder($targetFileName, $targetFolder);
1968 $resultObject = $this->replaceFile($file, $localFilePath);
1969 } else {
1970 $resultObject = $this->addFile($localFilePath, $targetFolder, $targetFileName, (string)$conflictMode);
1971 }
1972 return $resultObject;
1973 }
1974
1975 /********************
1976 * FOLDER ACTIONS
1977 ********************/
1978 /**
1979 * Returns an array with all file objects in a folder and its subfolders, with the file identifiers as keys.
1980 * @todo check if this is a duplicate
1981 * @param Folder $folder
1982 * @return File[]
1983 */
1984 protected function getAllFileObjectsInFolder(Folder $folder)
1985 {
1986 $files = [];
1987 $folderQueue = [$folder];
1988 while (!empty($folderQueue)) {
1989 $folder = array_shift($folderQueue);
1990 foreach ($folder->getSubfolders() as $subfolder) {
1991 $folderQueue[] = $subfolder;
1992 }
1993 foreach ($folder->getFiles() as $file) {
1994 /** @var FileInterface $file */
1995 $files[$file->getIdentifier()] = $file;
1996 }
1997 }
1998
1999 return $files;
2000 }
2001
2002 /**
2003 * Moves a folder. If you want to move a folder from this storage to another
2004 * one, call this method on the target storage, otherwise you will get an exception.
2005 *
2006 * @param Folder $folderToMove The folder to move.
2007 * @param Folder $targetParentFolder The target parent folder
2008 * @param string $newFolderName
2009 * @param string $conflictMode a value of the DuplicationBehavior enumeration
2010 *
2011 * @throws \Exception|\TYPO3\CMS\Core\Exception
2012 * @throws \InvalidArgumentException
2013 * @throws InvalidTargetFolderException
2014 * @return Folder
2015 */
2016 public function moveFolder(Folder $folderToMove, Folder $targetParentFolder, $newFolderName = null, $conflictMode = DuplicationBehavior::RENAME)
2017 {
2018 // @todo add tests
2019 $originalFolder = $folderToMove->getParentFolder();
2020 $this->assureFolderMovePermissions($folderToMove, $targetParentFolder);
2021 $sourceStorage = $folderToMove->getStorage();
2022 $returnObject = null;
2023 $sanitizedNewFolderName = $this->driver->sanitizeFileName($newFolderName ?: $folderToMove->getName());
2024 // @todo check if folder already exists in $targetParentFolder, handle this conflict then
2025 $this->emitPreFolderMoveSignal($folderToMove, $targetParentFolder, $sanitizedNewFolderName);
2026 // Get all file objects now so we are able to update them after moving the folder
2027 $fileObjects = $this->getAllFileObjectsInFolder($folderToMove);
2028 if ($sourceStorage === $this) {
2029 if ($this->isWithinFolder($folderToMove, $targetParentFolder)) {
2030 throw new InvalidTargetFolderException(
2031 sprintf(
2032 'Cannot move folder "%s" into target folder "%s", because the target folder is already within the folder to be moved!',
2033 $folderToMove->getName(),
2034 $targetParentFolder->getName()
2035 ),
2036 1422723050
2037 );
2038 }
2039 $fileMappings = $this->driver->moveFolderWithinStorage($folderToMove->getIdentifier(), $targetParentFolder->getIdentifier(), $sanitizedNewFolderName);
2040 } else {
2041 $fileMappings = $this->moveFolderBetweenStorages($folderToMove, $targetParentFolder, $sanitizedNewFolderName);
2042 }
2043 // Update the identifier and storage of all file objects
2044 foreach ($fileObjects as $oldIdentifier => $fileObject) {
2045 $newIdentifier = $fileMappings[$oldIdentifier];
2046 $fileObject->updateProperties(['storage' => $this->getUid(), 'identifier' => $newIdentifier]);
2047 $this->getIndexer()->updateIndexEntry($fileObject);
2048 }
2049 $returnObject = $this->getFolder($fileMappings[$folderToMove->getIdentifier()]);
2050 $this->emitPostFolderMoveSignal($folderToMove, $targetParentFolder, $returnObject->getName(), $originalFolder);
2051 return $returnObject;
2052 }
2053
2054 /**
2055 * Moves the given folder from a different storage to the target folder in this storage.
2056 *
2057 * @param Folder $folderToMove
2058 * @param Folder $targetParentFolder
2059 * @param string $newFolderName
2060 *
2061 * @return bool
2062 * @throws \RuntimeException
2063 */
2064 protected function moveFolderBetweenStorages(Folder $folderToMove, Folder $targetParentFolder, $newFolderName)
2065 {
2066 throw new \RuntimeException('Not yet implemented', 1476046361);
2067 }
2068
2069 /**
2070 * Copies a folder.
2071 *
2072 * @param FolderInterface $folderToCopy The folder to copy
2073 * @param FolderInterface $targetParentFolder The target folder
2074 * @param string $newFolderName
2075 * @param string $conflictMode a value of the DuplicationBehavior enumeration
2076 * @return Folder The new (copied) folder object
2077 * @throws InvalidTargetFolderException
2078 */
2079 public function copyFolder(FolderInterface $folderToCopy, FolderInterface $targetParentFolder, $newFolderName = null, $conflictMode = DuplicationBehavior::RENAME)
2080 {
2081 // @todo implement the $conflictMode handling
2082 $this->assureFolderCopyPermissions($folderToCopy, $targetParentFolder);
2083 $returnObject = null;
2084 $sanitizedNewFolderName = $this->driver->sanitizeFileName($newFolderName ?: $folderToCopy->getName());
2085 if ($folderToCopy instanceof Folder && $targetParentFolder instanceof Folder) {
2086 $this->emitPreFolderCopySignal($folderToCopy, $targetParentFolder, $sanitizedNewFolderName);
2087 }
2088 $sourceStorage = $folderToCopy->getStorage();
2089 // call driver method to move the file
2090 // that also updates the file object properties
2091 if ($sourceStorage === $this) {
2092 if ($this->isWithinFolder($folderToCopy, $targetParentFolder)) {
2093 throw new InvalidTargetFolderException(
2094 sprintf(
2095 'Cannot copy folder "%s" into target folder "%s", because the target folder is already within the folder to be copied!',
2096 $folderToCopy->getName(),
2097 $targetParentFolder->getName()
2098 ),
2099 1422723059
2100 );
2101 }
2102 $this->driver->copyFolderWithinStorage($folderToCopy->getIdentifier(), $targetParentFolder->getIdentifier(), $sanitizedNewFolderName);
2103 $returnObject = $this->getFolder($targetParentFolder->getSubfolder($sanitizedNewFolderName)->getIdentifier());
2104 } else {
2105 $this->copyFolderBetweenStorages($folderToCopy, $targetParentFolder, $sanitizedNewFolderName);
2106 }
2107 $this->emitPostFolderCopySignal($folderToCopy, $targetParentFolder, $returnObject->getName());
2108 return $returnObject;
2109 }
2110
2111 /**
2112 * Copies a folder between storages.
2113 *
2114 * @param Folder $folderToCopy
2115 * @param Folder $targetParentFolder
2116 * @param string $newFolderName
2117 *
2118 * @return bool
2119 * @throws \RuntimeException
2120 */
2121 protected function copyFolderBetweenStorages(Folder $folderToCopy, Folder $targetParentFolder, $newFolderName)
2122 {
2123 throw new \RuntimeException('Not yet implemented.', 1476046386);
2124 }
2125
2126 /**
2127 * Previously in \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::folder_move()
2128 *
2129 * @param Folder $folderObject
2130 * @param string $newName
2131 * @throws \Exception
2132 * @throws \InvalidArgumentException
2133 * @return Folder
2134 */
2135 public function renameFolder($folderObject, $newName)
2136 {
2137
2138 // Renaming the folder should check if the parent folder is writable
2139 // We cannot do this however because we cannot extract the parent folder from a folder currently
2140 if (!$this->checkFolderActionPermission('rename', $folderObject)) {
2141 throw new Exception\InsufficientUserPermissionsException('You are not allowed to rename the folder "' . $folderObject->getIdentifier() . '\'', 1357811441);
2142 }
2143
2144 $sanitizedNewName = $this->driver->sanitizeFileName($newName);
2145 $returnObject = null;
2146 if ($this->driver->folderExistsInFolder($sanitizedNewName, $folderObject->getIdentifier())) {
2147 throw new \InvalidArgumentException('The folder ' . $sanitizedNewName . ' already exists in folder ' . $folderObject->getIdentifier(), 1325418870);
2148 }
2149
2150 $this->emitPreFolderRenameSignal($folderObject, $sanitizedNewName);
2151
2152 $fileObjects = $this->getAllFileObjectsInFolder($folderObject);
2153 $fileMappings = $this->driver->renameFolder($folderObject->getIdentifier(), $sanitizedNewName);
2154 // Update the identifier of all file objects
2155 foreach ($fileObjects as $oldIdentifier => $fileObject) {
2156 $newIdentifier = $fileMappings[$oldIdentifier];
2157 $fileObject->updateProperties(['identifier' => $newIdentifier]);
2158 $this->getIndexer()->updateIndexEntry($fileObject);
2159 }
2160 $returnObject = $this->getFolder($fileMappings[$folderObject->getIdentifier()]);
2161
2162 $this->emitPostFolderRenameSignal($folderObject, $returnObject->getName());
2163
2164 return $returnObject;
2165 }
2166
2167 /**
2168 * Previously in \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::folder_delete()
2169 *
2170 * @param Folder $folderObject
2171 * @param bool $deleteRecursively
2172 * @throws \RuntimeException
2173 * @throws Exception\InsufficientFolderAccessPermissionsException
2174 * @throws Exception\InsufficientUserPermissionsException
2175 * @throws Exception\FileOperationErrorException
2176 * @throws Exception\InvalidPathException
2177 * @return bool
2178 */
2179 public function deleteFolder($folderObject, $deleteRecursively = false)
2180 {
2181 $isEmpty = $this->driver->isFolderEmpty($folderObject->getIdentifier());
2182 $this->assureFolderDeletePermission($folderObject, ($deleteRecursively && !$isEmpty));
2183 if (!$isEmpty && !$deleteRecursively) {
2184 throw new \RuntimeException('Could not delete folder "' . $folderObject->getIdentifier() . '" because it is not empty.', 1325952534);
2185 }
2186
2187 $this->emitPreFolderDeleteSignal($folderObject);
2188
2189 foreach ($this->getFilesInFolder($folderObject, 0, 0, false, $deleteRecursively) as $file) {
2190 $this->deleteFile($file);
2191 }
2192
2193 $result = $this->driver->deleteFolder($folderObject->getIdentifier(), $deleteRecursively);
2194
2195 $this->emitPostFolderDeleteSignal($folderObject);
2196
2197 return $result;
2198 }
2199
2200 /**
2201 * Returns the Identifier for a folder within a given folder.
2202 *
2203 * @param string $folderName The name of the target folder
2204 * @param Folder $parentFolder
2205 * @param bool $returnInaccessibleFolderObject
2206 * @return Folder|InaccessibleFolder
2207 * @throws \Exception
2208 * @throws Exception\InsufficientFolderAccessPermissionsException
2209 */
2210 public function getFolderInFolder($folderName, Folder $parentFolder, $returnInaccessibleFolderObject = false)
2211 {
2212 $folderIdentifier = $this->driver->getFolderInFolder($folderName, $parentFolder->getIdentifier());
2213 return $this->getFolder($folderIdentifier, $returnInaccessibleFolderObject);
2214 }
2215
2216 /**
2217 * @param Folder $folder
2218 * @param int $start
2219 * @param int $maxNumberOfItems
2220 * @param bool $useFilters
2221 * @param bool $recursive
2222 * @param string $sort Property name used to sort the items.
2223 * Among them may be: '' (empty, no sorting), name,
2224 * fileext, size, tstamp and rw.
2225 * If a driver does not support the given property, it
2226 * should fall back to "name".
2227 * @param bool $sortRev TRUE to indicate reverse sorting (last to first)
2228 * @return Folder[]
2229 */
2230 public function getFoldersInFolder(Folder $folder, $start = 0, $maxNumberOfItems = 0, $useFilters = true, $recursive = false, $sort = '', $sortRev = false)
2231 {
2232 $filters = $useFilters == true ? $this->fileAndFolderNameFilters : [];
2233
2234 $folderIdentifiers = $this->driver->getFoldersInFolder($folder->getIdentifier(), $start, $maxNumberOfItems, $recursive, $filters, $sort, $sortRev);
2235
2236 // Exclude processing folders
2237 foreach ($this->getProcessingFolders() as $processingFolder) {
2238 $processingIdentifier = $processingFolder->getIdentifier();
2239 if (isset($folderIdentifiers[$processingIdentifier])) {
2240 unset($folderIdentifiers[$processingIdentifier]);
2241 }
2242 }
2243 $folders = [];
2244 foreach ($folderIdentifiers as $folderIdentifier) {
2245 $folders[$folderIdentifier] = $this->getFolder($folderIdentifier, true);
2246 }
2247 return $folders;
2248 }
2249
2250 /**
2251 * @param Folder $folder
2252 * @param bool $useFilters
2253 * @param bool $recursive
2254 * @return int Number of subfolders
2255 * @throws Exception\InsufficientFolderAccessPermissionsException
2256 */
2257 public function countFoldersInFolder(Folder $folder, $useFilters = true, $recursive = false)
2258 {
2259 $this->assureFolderReadPermission($folder);
2260 $filters = $useFilters ? $this->fileAndFolderNameFilters : [];
2261 return $this->driver->countFoldersInFolder($folder->getIdentifier(), $recursive, $filters);
2262 }
2263
2264 /**
2265 * Returns TRUE if the specified folder exists.
2266 *
2267 * @param string $identifier
2268 * @return bool
2269 */
2270 public function hasFolder($identifier)
2271 {
2272 $this->assureFolderReadPermission();
2273 return $this->driver->folderExists($identifier);
2274 }
2275
2276 /**
2277 * Checks if the given file exists in the given folder
2278 *
2279 * @param string $folderName
2280 * @param Folder $folder
2281 * @return bool
2282 */
2283 public function hasFolderInFolder($folderName, Folder $folder)
2284 {
2285 $this->assureFolderReadPermission($folder);
2286 return $this->driver->folderExistsInFolder($folderName, $folder->getIdentifier());
2287 }
2288
2289 /**
2290 * Creates a new folder.
2291 *
2292 * previously in \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::func_newfolder()
2293 *
2294 * @param string $folderName The new folder name
2295 * @param Folder $parentFolder (optional) the parent folder to create the new folder inside of. If not given, the root folder is used
2296 * @return Folder
2297 * @throws Exception\ExistingTargetFolderException
2298 * @throws Exception\InsufficientFolderAccessPermissionsException
2299 * @throws Exception\InsufficientFolderWritePermissionsException
2300 * @throws \Exception
2301 */
2302 public function createFolder($folderName, Folder $parentFolder = null)
2303 {
2304 if ($parentFolder === null) {
2305 $parentFolder = $this->getRootLevelFolder();
2306 } elseif (!$this->driver->folderExists($parentFolder->getIdentifier())) {
2307 throw new \InvalidArgumentException('Parent folder "' . $parentFolder->getIdentifier() . '" does not exist.', 1325689164);
2308 }
2309 if (!$this->checkFolderActionPermission('add', $parentFolder)) {
2310 throw new Exception\InsufficientFolderWritePermissionsException('You are not allowed to create directories in the folder "' . $parentFolder->getIdentifier() . '"', 1323059807);
2311 }
2312 if ($this->driver->folderExistsInFolder($folderName, $parentFolder->getIdentifier())) {
2313 throw new Exception\ExistingTargetFolderException('Folder "' . $folderName . '" already exists.', 1423347324);
2314 }
2315
2316 $this->emitPreFolderAddSignal($parentFolder, $folderName);
2317
2318 $newFolder = $this->getDriver()->createFolder($folderName, $parentFolder->getIdentifier(), true);
2319 $newFolder = $this->getFolder($newFolder);
2320
2321 $this->emitPostFolderAddSignal($newFolder);
2322
2323 return $newFolder;
2324 }
2325
2326 /**
2327 * Retrieves information about a folder
2328 *
2329 * @param Folder $folder
2330 * @return array
2331 */
2332 public function getFolderInfo(Folder $folder)
2333 {
2334 return $this->driver->getFolderInfoByIdentifier($folder->getIdentifier());
2335 }
2336
2337 /**
2338 * Returns the default folder where new files are stored if no other folder is given.
2339 *
2340 * @return Folder
2341 */
2342 public function getDefaultFolder()
2343 {
2344 return $this->getFolder($this->driver->getDefaultFolder());
2345 }
2346
2347 /**
2348 * @param string $identifier
2349 * @param bool $returnInaccessibleFolderObject
2350 *
2351 * @return Folder|InaccessibleFolder
2352 * @throws \Exception
2353 * @throws Exception\InsufficientFolderAccessPermissionsException
2354 */
2355 public function getFolder($identifier, $returnInaccessibleFolderObject = false)
2356 {
2357 $data = $this->driver->getFolderInfoByIdentifier($identifier);
2358 $folder = $this->getResourceFactoryInstance()->createFolderObject($this, $data['identifier'], $data['name']);
2359
2360 try {
2361 $this->assureFolderReadPermission($folder);
2362 } catch (Exception\InsufficientFolderAccessPermissionsException $e) {
2363 $folder = null;
2364 if ($returnInaccessibleFolderObject) {
2365 // if parent folder is readable return inaccessible folder object
2366 $parentPermissions = $this->driver->getPermissions($this->driver->getParentFolderIdentifierOfIdentifier($identifier));
2367 if ($parentPermissions['r']) {
2368 $folder = GeneralUtility::makeInstance(
2369 InaccessibleFolder::class,
2370 $this,
2371 $data['identifier'],
2372 $data['name']
2373 );
2374 }
2375 }
2376
2377 if ($folder === null) {
2378 throw $e;
2379 }
2380 }
2381 return $folder;
2382 }
2383
2384 /**
2385 * Returns TRUE if the specified file is in a folder that is set a processing for a storage
2386 *
2387 * @param string $identifier
2388 * @return bool
2389 */
2390 public function isWithinProcessingFolder($identifier)
2391 {
2392 $inProcessingFolder = false;
2393 foreach ($this->getProcessingFolders() as $processingFolder) {
2394 if ($this->driver->isWithin($processingFolder->getIdentifier(), $identifier)) {
2395 $inProcessingFolder = true;
2396 break;
2397 }
2398 }
2399 return $inProcessingFolder;
2400 }
2401
2402 /**
2403 * Checks if a resource (file or folder) is within the given folder
2404 *
2405 * @param Folder $folder
2406 * @param ResourceInterface $resource
2407 * @return bool
2408 * @throws \InvalidArgumentException
2409 */
2410 public function isWithinFolder(Folder $folder, ResourceInterface $resource)
2411 {
2412 if ($folder->getStorage() !== $this) {
2413 throw new \InvalidArgumentException('Given folder "' . $folder->getIdentifier() . '" is not part of this storage!', 1422709241);
2414 }
2415 if ($folder->getStorage() !== $resource->getStorage()) {
2416 return false;
2417 }
2418 return $this->driver->isWithin($folder->getIdentifier(), $resource->getIdentifier());
2419 }
2420
2421 /**
2422 * Returns the folders on the root level of the storage
2423 * or the first mount point of this storage for this user
2424 * if $respectFileMounts is set.
2425 *
2426 * @param bool $respectFileMounts
2427 * @return Folder
2428 */
2429 public function getRootLevelFolder($respectFileMounts = true)
2430 {
2431 if ($respectFileMounts && !empty($this->fileMounts)) {
2432 $mount = reset($this->fileMounts);
2433 return $mount['folder'];
2434 }
2435 return $this->getResourceFactoryInstance()->createFolderObject($this, $this->driver->getRootLevelFolder(), '');
2436 }
2437
2438 /**
2439 * Emits sanitize fileName signal.
2440 *
2441 * @param string $fileName
2442 * @param Folder $targetFolder
2443 * @return string Modified target file name
2444 */
2445 protected function emitSanitizeFileNameSignal($fileName, Folder $targetFolder)
2446 {
2447 list($fileName) = $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_SanitizeFileName, [$fileName, $targetFolder, $this, $this->driver]);
2448 return $fileName;
2449 }
2450
2451 /**
2452 * Emits file pre-add signal.
2453 *
2454 * @param string $targetFileName
2455 * @param Folder $targetFolder
2456 * @param string $sourceFilePath
2457 * @return string Modified target file name
2458 */
2459 protected function emitPreFileAddSignal($targetFileName, Folder $targetFolder, $sourceFilePath)
2460 {
2461 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PreFileAdd, [&$targetFileName, $targetFolder, $sourceFilePath, $this, $this->driver]);
2462 return $targetFileName;
2463 }
2464
2465 /**
2466 * Emits the file post-add signal.
2467 *
2468 * @param FileInterface $file
2469 * @param Folder $targetFolder
2470 */
2471 protected function emitPostFileAddSignal(FileInterface $file, Folder $targetFolder)
2472 {
2473 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PostFileAdd, [$file, $targetFolder]);
2474 }
2475
2476 /**
2477 * Emits file pre-copy signal.
2478 *
2479 * @param FileInterface $file
2480 * @param Folder $targetFolder
2481 */
2482 protected function emitPreFileCopySignal(FileInterface $file, Folder $targetFolder)
2483 {
2484 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PreFileCopy, [$file, $targetFolder]);
2485 }
2486
2487 /**
2488 * Emits the file post-copy signal.
2489 *
2490 * @param FileInterface $file
2491 * @param Folder $targetFolder
2492 */
2493 protected function emitPostFileCopySignal(FileInterface $file, Folder $targetFolder)
2494 {
2495 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PostFileCopy, [$file, $targetFolder]);
2496 }
2497
2498 /**
2499 * Emits the file pre-move signal.
2500 *
2501 * @param FileInterface $file
2502 * @param Folder $targetFolder
2503 */
2504 protected function emitPreFileMoveSignal(FileInterface $file, Folder $targetFolder)
2505 {
2506 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PreFileMove, [$file, $targetFolder]);
2507 }
2508
2509 /**
2510 * Emits the file post-move signal.
2511 *
2512 * @param FileInterface $file
2513 * @param Folder $targetFolder
2514 * @param FolderInterface $originalFolder
2515 */
2516 protected function emitPostFileMoveSignal(FileInterface $file, Folder $targetFolder, FolderInterface $originalFolder)
2517 {
2518 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PostFileMove, [$file, $targetFolder, $originalFolder]);
2519 }
2520
2521 /**
2522 * Emits the file pre-rename signal
2523 *
2524 * @param FileInterface $file
2525 * @param $targetFolder
2526 */
2527 protected function emitPreFileRenameSignal(FileInterface $file, $targetFolder)
2528 {
2529 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PreFileRename, [$file, $targetFolder]);
2530 }
2531
2532 /**
2533 * Emits the file post-rename signal.
2534 *
2535 * @param FileInterface $file
2536 * @param string $sanitizedTargetFileName
2537 */
2538 protected function emitPostFileRenameSignal(FileInterface $file, $sanitizedTargetFileName)
2539 {
2540 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PostFileRename, [$file, $sanitizedTargetFileName]);
2541 }
2542
2543 /**
2544 * Emits the file pre-replace signal.
2545 *
2546 * @param FileInterface $file
2547 * @param $localFilePath
2548 */
2549 protected function emitPreFileReplaceSignal(FileInterface $file, $localFilePath)
2550 {
2551 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PreFileReplace, [$file, $localFilePath]);
2552 }
2553
2554 /**
2555 * Emits the file post-replace signal
2556 *
2557 * @param FileInterface $file
2558 * @param string $localFilePath
2559 */
2560 protected function emitPostFileReplaceSignal(FileInterface $file, $localFilePath)
2561 {
2562 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PostFileReplace, [$file, $localFilePath]);
2563 }
2564
2565 /**
2566 * Emits the file post-create signal
2567 *
2568 * @param string $newFileIdentifier
2569 * @param Folder $targetFolder
2570 */
2571 protected function emitPostFileCreateSignal($newFileIdentifier, Folder $targetFolder)
2572 {
2573 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PostFileCreate, [$newFileIdentifier, $targetFolder]);
2574 }
2575
2576 /**
2577 * Emits the file pre-deletion signal.
2578 *
2579 * @param FileInterface $file
2580 */
2581 protected function emitPreFileDeleteSignal(FileInterface $file)
2582 {
2583 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PreFileDelete, [$file]);
2584 }
2585
2586 /**
2587 * Emits the file post-deletion signal
2588 *
2589 * @param FileInterface $file
2590 */
2591 protected function emitPostFileDeleteSignal(FileInterface $file)
2592 {
2593 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PostFileDelete, [$file]);
2594 }
2595
2596 /**
2597 * Emits the file post-set-contents signal
2598 *
2599 * @param FileInterface $file
2600 * @param mixed $content
2601 */
2602 protected function emitPostFileSetContentsSignal(FileInterface $file, $content)
2603 {
2604 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PostFileSetContents, [$file, $content]);
2605 }
2606
2607 /**
2608 * Emits the folder pre-add signal.
2609 *
2610 * @param Folder $targetFolder
2611 * @param string $name
2612 */
2613 protected function emitPreFolderAddSignal(Folder $targetFolder, $name)
2614 {
2615 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PreFolderAdd, [$targetFolder, $name]);
2616 }
2617
2618 /**
2619 * Emits the folder post-add signal.
2620 *
2621 * @param Folder $folder
2622 */
2623 protected function emitPostFolderAddSignal(Folder $folder)
2624 {
2625 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PostFolderAdd, [$folder]);
2626 }
2627
2628 /**
2629 * Emits the folder pre-copy signal.
2630 *
2631 * @param Folder $folder
2632 * @param Folder $targetFolder
2633 * @param $newName
2634 */
2635 protected function emitPreFolderCopySignal(Folder $folder, Folder $targetFolder, $newName)
2636 {
2637 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PreFolderCopy, [$folder, $targetFolder, $newName]);
2638 }
2639
2640 /**
2641 * Emits the folder post-copy signal.
2642 *
2643 * @param Folder $folder
2644 * @param Folder $targetFolder
2645 * @param $newName
2646 */
2647 protected function emitPostFolderCopySignal(Folder $folder, Folder $targetFolder, $newName)
2648 {
2649 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PostFolderCopy, [$folder, $targetFolder, $newName]);
2650 }
2651
2652 /**
2653 * Emits the folder pre-move signal.
2654 *
2655 * @param Folder $folder
2656 * @param Folder $targetFolder
2657 * @param $newName
2658 */
2659 protected function emitPreFolderMoveSignal(Folder $folder, Folder $targetFolder, $newName)
2660 {
2661 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PreFolderMove, [$folder, $targetFolder, $newName]);
2662 }
2663
2664 /**
2665 * Emits the folder post-move signal.
2666 *
2667 * @param Folder $folder
2668 * @param Folder $targetFolder
2669 * @param string $newName
2670 * @param Folder $originalFolder
2671 */
2672 protected function emitPostFolderMoveSignal(Folder $folder, Folder $targetFolder, $newName, Folder $originalFolder)
2673 {
2674 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PostFolderMove, [$folder, $targetFolder, $newName, $originalFolder]);
2675 }
2676
2677 /**
2678 * Emits the folder pre-rename signal.
2679 *
2680 * @param Folder $folder
2681 * @param string $newName
2682 */
2683 protected function emitPreFolderRenameSignal(Folder $folder, $newName)
2684 {
2685 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PreFolderRename, [$folder, $newName]);
2686 }
2687
2688 /**
2689 * Emits the folder post-rename signal.
2690 *
2691 * @param Folder $folder
2692 * @param string $newName
2693 */
2694 protected function emitPostFolderRenameSignal(Folder $folder, $newName)
2695 {
2696 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PostFolderRename, [$folder, $newName]);
2697 }
2698
2699 /**
2700 * Emits the folder pre-deletion signal.
2701 *
2702 * @param Folder $folder
2703 */
2704 protected function emitPreFolderDeleteSignal(Folder $folder)
2705 {
2706 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PreFolderDelete, [$folder]);
2707 }
2708
2709 /**
2710 * Emits folder post-deletion signal..
2711 *
2712 * @param Folder $folder
2713 */
2714 protected function emitPostFolderDeleteSignal(Folder $folder)
2715 {
2716 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PostFolderDelete, [$folder]);
2717 }
2718
2719 /**
2720 * Emits file pre-processing signal when generating a public url for a file or folder.
2721 *
2722 * @param ResourceInterface $resourceObject
2723 * @param bool $relativeToCurrentScript
2724 * @param array $urlData
2725 */
2726 protected function emitPreGeneratePublicUrlSignal(ResourceInterface $resourceObject, $relativeToCurrentScript, array $urlData)
2727 {
2728 $this->getSignalSlotDispatcher()->dispatch(self::class, self::SIGNAL_PreGeneratePublicUrl, [$this, $this->driver, $resourceObject, $relativeToCurrentScript, $urlData]);
2729 }
2730
2731 /**
2732 * Returns the destination path/fileName of a unique fileName/foldername in that path.
2733 * If $theFile exists in $theDest (directory) the file have numbers appended up to $this->maxNumber. Hereafter a unique string will be appended.
2734 * This function is used by fx. DataHandler when files are attached to records and needs to be uniquely named in the uploads/* folders
2735 *
2736 * @param FolderInterface $folder
2737 * @param string $theFile The input fileName to check
2738 * @param bool $dontCheckForUnique If set the fileName is returned with the path prepended without checking whether it already existed!
2739 *
2740 * @throws \RuntimeException
2741 * @return string A unique fileName inside $folder, based on $theFile.
2742 * @see \TYPO3\CMS\Core\Utility\File\BasicFileUtility::getUniqueName()
2743 */
2744 protected function getUniqueName(FolderInterface $folder, $theFile, $dontCheckForUnique = false)
2745 {
2746 static $maxNumber = 99, $uniqueNamePrefix = '';
2747 // Fetches info about path, name, extension of $theFile
2748 $origFileInfo = PathUtility::pathinfo($theFile);
2749 // Adds prefix
2750 if ($uniqueNamePrefix) {
2751 $origFileInfo['basename'] = $uniqueNamePrefix . $origFileInfo['basename'];
2752 $origFileInfo['filename'] = $uniqueNamePrefix . $origFileInfo['filename'];
2753 }
2754 // Check if the file exists and if not - return the fileName...
2755 // The destinations file
2756 $theDestFile = $origFileInfo['basename'];
2757 // If the file does NOT exist we return this fileName
2758 if (!$this->driver->fileExistsInFolder($theDestFile, $folder->getIdentifier()) || $dontCheckForUnique) {
2759 return $theDestFile;
2760 }
2761 // Well the fileName in its pure form existed. Now we try to append
2762 // numbers / unique-strings and see if we can find an available fileName
2763 // This removes _xx if appended to the file
2764 $theTempFileBody = preg_replace('/_[0-9][0-9]$/', '', $origFileInfo['filename']);
2765 $theOrigExt = $origFileInfo['extension'] ? '.' . $origFileInfo['extension'] : '';
2766 for ($a = 1; $a <= $maxNumber + 1; $a++) {
2767 // First we try to append numbers
2768 if ($a <= $maxNumber) {
2769 $insert = '_' . sprintf('%02d', $a);
2770 } else {
2771 $insert = '_' . substr(md5(uniqid('', true)), 0, 6);
2772 }
2773 $theTestFile = $theTempFileBody . $insert . $theOrigExt;
2774 // The destinations file
2775 $theDestFile = $theTestFile;
2776 // If the file does NOT exist we return this fileName
2777 if (!$this->driver->fileExistsInFolder($theDestFile, $folder->getIdentifier())) {
2778 return $theDestFile;
2779 }
2780 }
2781 throw new \RuntimeException('Last possible name "' . $theDestFile . '" is already taken.', 1325194291);
2782 }
2783
2784 /**
2785 * Get the SignalSlot dispatcher.
2786 *
2787 * @return Dispatcher
2788 */
2789 protected function getSignalSlotDispatcher()
2790 {
2791 if (!isset($this->signalSlotDispatcher)) {
2792 $this->signalSlotDispatcher = $this->getObjectManager()->get(Dispatcher::class);
2793 }
2794 return $this->signalSlotDispatcher;
2795 }
2796
2797 /**
2798 * Gets the ObjectManager.
2799 *
2800 * @return ObjectManager
2801 */
2802 protected function getObjectManager()
2803 {
2804 return GeneralUtility::makeInstance(ObjectManager::class);
2805 }
2806
2807 /**
2808 * @return ResourceFactory
2809 */
2810 protected function getFileFactory()
2811 {
2812 return GeneralUtility::makeInstance(ResourceFactory::class);
2813 }
2814
2815 /**
2816 * @return Index\FileIndexRepository
2817 */
2818 protected function getFileIndexRepository()
2819 {
2820 return FileIndexRepository::getInstance();
2821 }
2822
2823 /**
2824 * @return Service\FileProcessingService
2825 */
2826 protected function getFileProcessingService()
2827 {
2828 if (!$this->fileProcessingService) {
2829 $this->fileProcessingService = GeneralUtility::makeInstance(Service\FileProcessingService::class, $this, $this->driver);
2830 }
2831 return $this->fileProcessingService;
2832 }
2833
2834 /**
2835 * Gets the role of a folder.
2836 *
2837 * @param FolderInterface $folder Folder object to get the role from
2838 * @return string The role the folder has
2839 */
2840 public function getRole(FolderInterface $folder)
2841 {
2842 $folderRole = FolderInterface::ROLE_DEFAULT;
2843 $identifier = $folder->getIdentifier();
2844 if (method_exists($this->driver, 'getRole')) {
2845 $folderRole = $this->driver->getRole($folder->getIdentifier());
2846 }
2847 if (isset($this->fileMounts[$identifier])) {
2848 $folderRole = FolderInterface::ROLE_MOUNT;
2849
2850 if (!empty($this->fileMounts[$identifier]['read_only'])) {
2851 $folderRole = FolderInterface::ROLE_READONLY_MOUNT;
2852 }
2853 if ($this->fileMounts[$identifier]['user_mount']) {
2854 $folderRole = FolderInterface::ROLE_USER_MOUNT;
2855 }
2856 }
2857 if ($folder instanceof Folder && $this->isProcessingFolder($folder)) {
2858 $folderRole = FolderInterface::ROLE_PROCESSING;
2859 }
2860
2861 return $folderRole;
2862 }
2863
2864 /**
2865 * Getter function to return the folder where the files can
2866 * be processed. Does not check for access rights here.
2867 *
2868 * @param File $file Specific file you want to have the processing folder for
2869 * @return Folder
2870 */
2871 public function getProcessingFolder(File $file = null)
2872 {
2873 if (!isset($this->processingFolder)) {
2874 $processingFolder = self::DEFAULT_ProcessingFolder;
2875 if (!empty($this->storageRecord['processingfolder'])) {
2876 $processingFolder = $this->storageRecord['processingfolder'];
2877 }
2878 try {
2879 if (strpos($processingFolder, ':') !== false) {
2880 list($storageUid, $processingFolderIdentifier) = explode(':', $processingFolder, 2);
2881 $storage = $this->getResourceFactoryInstance()->getStorageObject($storageUid);
2882 if ($storage->hasFolder($processingFolderIdentifier)) {
2883 $this->processingFolder = $storage->getFolder($processingFolderIdentifier);
2884 } else {
2885 $rootFolder = $storage->getRootLevelFolder(false);
2886 $currentEvaluatePermissions = $storage->getEvaluatePermissions();
2887 $storage->setEvaluatePermissions(false);
2888 $this->processingFolder = $storage->createFolder(
2889 ltrim($processingFolderIdentifier, '/'),
2890 $rootFolder
2891 );
2892 $storage->setEvaluatePermissions($currentEvaluatePermissions);
2893 }
2894 } else {
2895 if ($this->driver->folderExists($processingFolder) === false) {
2896 $rootFolder = $this->getRootLevelFolder(false);
2897 try {
2898 $currentEvaluatePermissions = $this->evaluatePermissions;
2899 $this->evaluatePermissions = false;
2900 $this->processingFolder = $this->createFolder(
2901 $processingFolder,
2902 $rootFolder
2903 );
2904 $this->evaluatePermissions = $currentEvaluatePermissions;
2905 } catch (\InvalidArgumentException $e) {
2906 $this->processingFolder = GeneralUtility::makeInstance(
2907 InaccessibleFolder::class,
2908 $this,
2909 $processingFolder,
2910 $processingFolder
2911 );
2912 }
2913 } else {
2914 $data = $this->driver->getFolderInfoByIdentifier($processingFolder);
2915 $this->processingFolder = $this->getResourceFactoryInstance()->createFolderObject($this, $data['identifier'], $data['name']);
2916 }
2917 }
2918 } catch (Exception\InsufficientFolderWritePermissionsException $e) {
2919 $this->processingFolder = GeneralUtility::makeInstance(
2920 InaccessibleFolder::class,
2921 $this,
2922 $processingFolder,
2923 $processingFolder
2924 );
2925 } catch (Exception\ResourcePermissionsUnavailableException $e) {
2926 $this->processingFolder = GeneralUtility::makeInstance(
2927 InaccessibleFolder::class,
2928 $this,
2929 $processingFolder,
2930 $processingFolder
2931 );
2932 }
2933 }
2934
2935 $processingFolder = $this->processingFolder;
2936 if (!empty($file)) {
2937 $processingFolder = $this->getNestedProcessingFolder($file, $processingFolder);
2938 }
2939 return $processingFolder;
2940 }
2941
2942 /**
2943 * Getter function to return the the file's corresponding hashed subfolder
2944 * of the processed folder
2945 *
2946 * @param File $file
2947 * @param Folder $rootProcessingFolder
2948 * @return Folder
2949 * @throws Exception\InsufficientFolderWritePermissionsException
2950 */
2951 protected function getNestedProcessingFolder(File $file, Folder $rootProcessingFolder)
2952 {
2953 $processingFolder = $rootProcessingFolder;
2954 $nestedFolderNames = $this->getNamesForNestedProcessingFolder(
2955 $file->getIdentifier(),
2956 self::PROCESSING_FOLDER_LEVELS
2957 );
2958
2959 try {
2960 foreach ($nestedFolderNames as $folderName) {
2961 if ($processingFolder->hasFolder($folderName)) {
2962 $processingFolder = $processingFolder->getSubfolder($folderName);
2963 } else {
2964 $currentEvaluatePermissions = $processingFolder->getStorage()->getEvaluatePermissions();
2965 $processingFolder->getStorage()->setEvaluatePermissions(false);
2966 $processingFolder = $processingFolder->createFolder($folderName);
2967 $processingFolder->getStorage()->setEvaluatePermissions($currentEvaluatePermissions);
2968 }
2969 }
2970 } catch (Exception\FolderDoesNotExistException $e) {
2971 }
2972
2973 return $processingFolder;
2974 }
2975
2976 /**
2977 * Generates appropriate hashed sub-folder path for a given file identifier
2978 *
2979 * @param string $fileIdentifier
2980 * @param int $levels
2981 * @return string[]
2982 */
2983 protected function getNamesForNestedProcessingFolder($fileIdentifier, $levels)
2984 {
2985 $names = [];
2986 if ($levels === 0) {
2987 return $names;
2988 }
2989 $hash = md5($fileIdentifier);
2990 for ($i = 1; $i <= $levels; $i++) {
2991 $names[] = substr($hash, $i, 1);
2992 }
2993 return $names;
2994 }
2995
2996 /**
2997 * Gets the driver Type configured for this storage.
2998 *
2999 * @return string
3000 */
3001 public function getDriverType()
3002 {
3003 return $this->storageRecord['driver'];
3004 }
3005
3006 /**
3007 * Gets the Indexer.
3008 *
3009 * @return Index\Indexer
3010 */
3011 protected function getIndexer()
3012 {
3013 return GeneralUtility::makeInstance(Index\Indexer::class, $this);
3014 }
3015
3016 /**
3017 * @param bool $isDefault
3018 */
3019 public function setDefault($isDefault)
3020 {
3021 $this->isDefault = (bool)$isDefault;
3022 }
3023
3024 /**
3025 * @return bool
3026 */
3027 public function isDefault()
3028 {
3029 return $this->isDefault;
3030 }
3031
3032 /**
3033 * @return ResourceFactory
3034 */
3035 public function getResourceFactoryInstance(): ResourceFactory
3036 {
3037 return ResourceFactory::getInstance();
3038 }
3039
3040 /**
3041 * Returns the current BE user.
3042 *
3043 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
3044 */
3045 protected function getBackendUser()
3046 {
3047 return $GLOBALS['BE_USER'];
3048 }
3049
3050 /**
3051 * Get the nearest Recycler folder for given file
3052 *
3053 * Return null if:
3054 * - There is no folder with ROLE_RECYCLER in the rootline of the given File
3055 * - File is a ProcessedFile (we don't know the concept of recycler folders for processedFiles)
3056 * - File is located in a folder with ROLE_RECYCLER
3057 *
3058 * @param FileInterface $file
3059 * @return Folder|null
3060 */
3061 protected function getNearestRecyclerFolder(FileInterface $file)
3062 {
3063 if ($file instanceof ProcessedFile) {
3064 return null;
3065 }
3066
3067 $recyclerFolder = null;
3068 $folder = $file->getParentFolder();
3069
3070 do {
3071 if ($folder->getRole() === FolderInterface::ROLE_RECYCLER) {
3072 break;
3073 }
3074
3075 foreach ($folder->getSubfolders() as $subFolder) {
3076 if ($subFolder->getRole() === FolderInterface::ROLE_RECYCLER) {
3077 $recyclerFolder = $subFolder;
3078 break;
3079 }
3080 }
3081
3082 $parentFolder = $folder->getParentFolder();
3083 $isFolderLoop = $folder->getIdentifier() === $parentFolder->getIdentifier();
3084 $folder = $parentFolder;
3085 } while ($recyclerFolder === null && !$isFolderLoop);
3086
3087 return $recyclerFolder;
3088 }
3089
3090 /**
3091 * @return \TYPO3\CMS\Core\Log\Logger
3092 */
3093 protected function getLogger()
3094 {
3095 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
3096 $logManager = GeneralUtility::makeInstance(
3097 \TYPO3\CMS\Core\Log\LogManager::class
3098 );
3099 return $logManager->getLogger(get_class($this));
3100 }
3101 }