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