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