8d3f3deeffab65e05a41fea304e3c72ae9829b83
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / FileController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller\File;
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Core\Http\HtmlResponse;
21 use TYPO3\CMS\Core\Http\JsonResponse;
22 use TYPO3\CMS\Core\Http\RedirectResponse;
23 use TYPO3\CMS\Core\Imaging\Icon;
24 use TYPO3\CMS\Core\Imaging\IconFactory;
25 use TYPO3\CMS\Core\Resource\DuplicationBehavior;
26 use TYPO3\CMS\Core\Resource\Folder;
27 use TYPO3\CMS\Core\Resource\ResourceFactory;
28 use TYPO3\CMS\Core\Utility\File\ExtendedFileUtility;
29 use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31 /**
32 * Gateway for TCE (TYPO3 Core Engine) file-handling through POST forms.
33 * This script serves as the file administration part of the TYPO3 Core Engine.
34 * Basically it includes two libraries which are used to manipulate files on the server.
35 */
36 class FileController
37 {
38 /**
39 * Array of file-operations.
40 *
41 * @var array
42 */
43 protected $file;
44
45 /**
46 * Clipboard operations array
47 *
48 * @var array
49 */
50 protected $CB;
51
52 /**
53 * Defines behaviour when uploading files with names that already exist; possible values are
54 * the values of the \TYPO3\CMS\Core\Resource\DuplicationBehavior enumeration
55 *
56 * @var \TYPO3\CMS\Core\Resource\DuplicationBehavior
57 */
58 protected $overwriteExistingFiles;
59
60 /**
61 * The page where the user should be redirected after everything is done
62 *
63 * @var string
64 */
65 protected $redirect;
66
67 /**
68 * Internal, dynamic:
69 * File processor object
70 *
71 * @var ExtendedFileUtility
72 */
73 protected $fileProcessor;
74
75 /**
76 * The result array from the file processor
77 *
78 * @var array
79 */
80 protected $fileData;
81
82 /**
83 * Constructor
84 */
85 public function __construct()
86 {
87 $GLOBALS['SOBE'] = $this;
88 $this->init();
89 }
90
91 /**
92 * Registering incoming data
93 */
94 protected function init()
95 {
96 // Set the GPvars from outside
97 $this->file = GeneralUtility::_GP('data');
98 if ($this->file === null) {
99 // This happens in clipboard mode only
100 $this->redirect = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('redirect'));
101 } else {
102 $mode = key($this->file);
103 $elementKey = key($this->file[$mode]);
104 $this->redirect = GeneralUtility::sanitizeLocalUrl($this->file[$mode][$elementKey]['redirect']);
105 }
106 $this->CB = GeneralUtility::_GP('CB');
107
108 if (isset($this->file['rename'][0]['conflictMode'])) {
109 $conflictMode = $this->file['rename'][0]['conflictMode'];
110 unset($this->file['rename'][0]['conflictMode']);
111 $this->overwriteExistingFiles = DuplicationBehavior::cast($conflictMode);
112 } else {
113 $this->overwriteExistingFiles = DuplicationBehavior::cast(GeneralUtility::_GP('overwriteExistingFiles'));
114 }
115 $this->initClipboard();
116 $this->fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
117 }
118
119 /**
120 * Initialize the Clipboard. This will fetch the data about files to paste/delete if such an action has been sent.
121 */
122 public function initClipboard()
123 {
124 if (is_array($this->CB)) {
125 $clipObj = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Clipboard\Clipboard::class);
126 $clipObj->initializeClipboard();
127 if ($this->CB['paste']) {
128 $clipObj->setCurrentPad($this->CB['pad']);
129 $this->file = $clipObj->makePasteCmdArray_file($this->CB['paste'], $this->file);
130 }
131 if ($this->CB['delete']) {
132 $clipObj->setCurrentPad($this->CB['pad']);
133 $this->file = $clipObj->makeDeleteCmdArray_file($this->file);
134 }
135 }
136 }
137
138 /**
139 * Performing the file admin action:
140 * Initializes the objects, setting permissions, sending data to object.
141 */
142 public function main()
143 {
144 // Initializing:
145 $this->fileProcessor->setActionPermissions();
146 $this->fileProcessor->setExistingFilesConflictMode($this->overwriteExistingFiles);
147 $this->fileProcessor->start($this->file);
148 $this->fileData = $this->fileProcessor->processData();
149 }
150
151 /**
152 * Redirecting the user after the processing has been done.
153 * Might also display error messages directly, if any.
154 */
155 public function finish()
156 {
157 BackendUtility::setUpdateSignal('updateFolderTree');
158 if ($this->redirect) {
159 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($this->redirect);
160 }
161 }
162
163 /**
164 * Injects the request object for the current request or subrequest
165 * As this controller goes only through the main() method, it just redirects to the given URL afterwards.
166 *
167 * @param ServerRequestInterface $request the current request
168 * @return ResponseInterface the response with the content
169 */
170 public function mainAction(ServerRequestInterface $request): ResponseInterface
171 {
172 $this->main();
173
174 BackendUtility::setUpdateSignal('updateFolderTree');
175
176 // go and edit the new created file
177 if ($request->getParsedBody()['edit'] ?? '') {
178 /** @var \TYPO3\CMS\Core\Resource\File $file */
179 $file = $this->fileData['newfile'][0];
180 $properties = $file->getProperties();
181 $urlParameters = [
182 'target' => $properties['storage'] . ':' . $properties['identifier']
183 ];
184 if ($this->redirect) {
185 $urlParameters['returnUrl'] = $this->redirect;
186 }
187 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
188 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
189 $this->redirect = (string)$uriBuilder->buildUriFromRoute('file_edit', $urlParameters);
190 }
191 if ($this->redirect) {
192 return new RedirectResponse(
193 GeneralUtility::locationHeaderUrl($this->redirect),
194 303
195 );
196 }
197 // empty response
198 return new HtmlResponse('');
199 }
200
201 /**
202 * Handles the actual process from within the ajaxExec function
203 * therefore, it does exactly the same as the real typo3/tce_file.php
204 * but without calling the "finish" method, thus makes it simpler to deal with the
205 * actual return value
206 *
207 * @param ServerRequestInterface $request
208 * @return ResponseInterface
209 */
210 public function processAjaxRequest(ServerRequestInterface $request): ResponseInterface
211 {
212 $this->main();
213 $errors = $this->fileProcessor->getErrorMessages();
214 if (!empty($errors)) {
215 return (new HtmlResponse('<t3err>' . implode(',', $errors) . '</t3err>'))->withStatus(500, '(AJAX)');
216 }
217 $flatResult = [];
218 foreach ($this->fileData as $action => $results) {
219 foreach ($results as $result) {
220 if (is_array($result)) {
221 foreach ($result as $subResult) {
222 $flatResult[$action][] = $this->flattenResultDataValue($subResult);
223 }
224 } else {
225 $flatResult[$action][] = $this->flattenResultDataValue($result);
226 }
227 }
228 }
229 return (new JsonResponse())->setPayload($flatResult);
230 }
231
232 /**
233 * Ajax entry point to check if a file exists in a folder
234 *
235 * @param ServerRequestInterface $request
236 * @return ResponseInterface
237 */
238 public function fileExistsInFolderAction(ServerRequestInterface $request): ResponseInterface
239 {
240 $fileName = $request->getParsedBody()['fileName'] ?? $request->getQueryParams()['fileName'];
241 $fileTarget = $request->getParsedBody()['fileTarget'] ?? $request->getQueryParams()['fileTarget'];
242
243 $fileFactory = GeneralUtility::makeInstance(ResourceFactory::class);
244 /** @var Folder $fileTargetObject */
245 $fileTargetObject = $fileFactory->retrieveFileOrFolderObject($fileTarget);
246 $processedFileName = $fileTargetObject->getStorage()->sanitizeFileName($fileName, $fileTargetObject);
247
248 $result = [];
249 if ($fileTargetObject->hasFile($processedFileName)) {
250 $result = $this->flattenResultDataValue($fileTargetObject->getStorage()->getFileInFolder($processedFileName, $fileTargetObject));
251 }
252 return (new JsonResponse())->setPayload($result);
253 }
254
255 /**
256 * Flatten result value from FileProcessor
257 *
258 * The value can be a File, Folder or boolean
259 *
260 * @param bool|\TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder $result
261 * @return bool|string|array
262 */
263 protected function flattenResultDataValue($result)
264 {
265 if ($result instanceof \TYPO3\CMS\Core\Resource\File) {
266 $thumbUrl = '';
267 if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $result->getExtension())) {
268 $processedFile = $result->process(\TYPO3\CMS\Core\Resource\ProcessedFile::CONTEXT_IMAGEPREVIEW, []);
269 if ($processedFile) {
270 $thumbUrl = $processedFile->getPublicUrl(true);
271 }
272 }
273 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
274 $result = array_merge(
275 $result->toArray(),
276 [
277 'date' => BackendUtility::date($result->getModificationTime()),
278 'icon' => $iconFactory->getIconForFileExtension($result->getExtension(), Icon::SIZE_SMALL)->render(),
279 'thumbUrl' => $thumbUrl
280 ]
281 );
282 } elseif ($result instanceof \TYPO3\CMS\Core\Resource\Folder) {
283 $result = $result->getIdentifier();
284 }
285
286 return $result;
287 }
288
289 /**
290 * Returns the current BE user.
291 *
292 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
293 */
294 protected function getBackendUser()
295 {
296 return $GLOBALS['BE_USER'];
297 }
298 }