[TASK] Filelist: Create new file and opening it immediately for editing
[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 use TYPO3\CMS\Core\Utility\HttpUtility;
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 * VeriCode - a hash of server specific value and other things which
61 * identifies if a submission is OK. (see $GLOBALS['BE_USER']->veriCode())
62 *
63 * @var string
64 */
65 protected $vC;
66
67 /**
68 * The page where the user should be redirected after everything is done
69 *
70 * @var string
71 */
72 protected $redirect;
73
74 /**
75 * Internal, dynamic:
76 * File processor object
77 *
78 * @var ExtendedFileUtility
79 */
80 protected $fileProcessor;
81
82 /**
83 * The result array from the file processor
84 *
85 * @var array
86 */
87 protected $fileData;
88
89 /**
90 * Constructor
91 */
92 public function __construct()
93 {
94 $GLOBALS['SOBE'] = $this;
95 $this->init();
96 }
97
98 /**
99 * Registering incoming data
100 *
101 * @return void
102 */
103 protected function init()
104 {
105 // Set the GPvars from outside
106 $this->file = GeneralUtility::_GP('file');
107 $this->CB = GeneralUtility::_GP('CB');
108 $this->overwriteExistingFiles = DuplicationBehavior::cast(GeneralUtility::_GP('overwriteExistingFiles'));
109 $this->vC = GeneralUtility::_GP('vC');
110 $this->redirect = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('redirect'));
111 $this->initClipboard();
112 $this->fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
113 }
114
115 /**
116 * Initialize the Clipboard. This will fetch the data about files to paste/delete if such an action has been sent.
117 *
118 * @return void
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 * @return void
141 */
142 public function main()
143 {
144 // Initializing:
145 $this->fileProcessor->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
146 $this->fileProcessor->setActionPermissions();
147 $this->fileProcessor->setExistingFilesConflictMode($this->overwriteExistingFiles);
148 // Checking referrer / executing:
149 $refInfo = parse_url(GeneralUtility::getIndpEnv('HTTP_REFERER'));
150 $httpHost = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
151 if ($httpHost !== $refInfo['host'] && $this->vC !== $this->getBackendUser()->veriCode() && !$GLOBALS['TYPO3_CONF_VARS']['SYS']['doNotCheckReferer']) {
152 $this->fileProcessor->writeLog(0, 2, 1, 'Referrer host "%s" and server host "%s" did not match!', array($refInfo['host'], $httpHost));
153 } else {
154 $this->fileProcessor->start($this->file);
155 $this->fileData = $this->fileProcessor->processData();
156 }
157 }
158
159 /**
160 * Redirecting the user after the processing has been done.
161 * Might also display error messages directly, if any.
162 *
163 * @return void
164 */
165 public function finish()
166 {
167 BackendUtility::setUpdateSignal('updateFolderTree');
168 if ($this->redirect) {
169 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($this->redirect);
170 }
171 }
172
173 /**
174 * Injects the request object for the current request or subrequest
175 * As this controller goes only through the main() method, it just redirects to the given URL afterwards.
176 *
177 * @param ServerRequestInterface $request the current request
178 * @param ResponseInterface $response
179 * @return ResponseInterface the response with the content
180 */
181 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
182 {
183 $this->main();
184
185 BackendUtility::setUpdateSignal('updateFolderTree');
186
187 // go and edit the new created file
188 if ($request->getParsedBody()['edit']) {
189 $urlParameters = [
190 'target' => $this->file['newfile'][0]['target'] . $this->file['newfile'][0]['data']
191 ];
192 if ($this->redirect) {
193 $urlParameters['returnUrl'] = $this->redirect;
194 }
195 $this->redirect = BackendUtility::getModuleUrl('file_edit', $urlParameters);
196 }
197 if ($this->redirect) {
198 return $response
199 ->withHeader('Location', GeneralUtility::locationHeaderUrl($this->redirect))
200 ->withStatus(303);
201 } else {
202 // empty response
203 return $response;
204 }
205 }
206
207 /**
208 * Handles the actual process from within the ajaxExec function
209 * therefore, it does exactly the same as the real typo3/tce_file.php
210 * but without calling the "finish" method, thus makes it simpler to deal with the
211 * actual return value
212 *
213 * @param ServerRequestInterface $request
214 * @param ResponseInterface $response
215 * @return ResponseInterface
216 */
217 public function processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
218 {
219 $this->main();
220 $errors = $this->fileProcessor->getErrorMessages();
221 if (!empty($errors)) {
222 $response->getBody()->write(implode(',', $errors));
223 $response = $response->withHeader('Content-Type', 'text/html; charset=utf-8');
224 } else {
225 $flatResult = array();
226 foreach ($this->fileData as $action => $results) {
227 foreach ($results as $result) {
228 if (is_array($result)) {
229 foreach ($result as $subResult) {
230 $flatResult[$action][] = $this->flattenResultDataValue($subResult);
231 }
232 } else {
233 $flatResult[$action][] = $this->flattenResultDataValue($result);
234 }
235 }
236 }
237 $response->getBody()->write(json_encode($flatResult));
238 }
239 return $response;
240 }
241
242 /**
243 * Ajax entry point to check if a file exists in a folder
244 *
245 * @param ServerRequestInterface $request
246 * @param ResponseInterface $response
247 * @return ResponseInterface
248 */
249 public function fileExistsInFolderAction(ServerRequestInterface $request, ResponseInterface $response)
250 {
251 $fileName = isset($request->getParsedBody()['fileName']) ? $request->getParsedBody()['fileName'] : $request->getQueryParams()['fileName'];
252 $fileTarget = isset($request->getParsedBody()['fileTarget']) ? $request->getParsedBody()['fileTarget'] : $request->getQueryParams()['fileTarget'];
253
254 /** @var \TYPO3\CMS\Core\Resource\ResourceFactory $fileFactory */
255 $fileFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
256 /** @var Folder $fileTargetObject */
257 $fileTargetObject = $fileFactory->retrieveFileOrFolderObject($fileTarget);
258 $processedFileName = $fileTargetObject->getStorage()->sanitizeFileName($fileName, $fileTargetObject);
259
260 $result = false;
261 if ($fileTargetObject->hasFile($processedFileName)) {
262 $result = $this->flattenResultDataValue($fileTargetObject->getStorage()->getFileInFolder($processedFileName, $fileTargetObject));
263 }
264 $response->getBody()->write(json_encode($result));
265 return $response;
266 }
267
268 /**
269 * Flatten result value from FileProcessor
270 *
271 * The value can be a File, Folder or boolean
272 *
273 * @param bool|\TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder $result
274 * @return bool|string|array
275 */
276 protected function flattenResultDataValue($result)
277 {
278 if ($result instanceof \TYPO3\CMS\Core\Resource\File) {
279 $thumbUrl = '';
280 if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $result->getExtension())) {
281 $processedFile = $result->process(\TYPO3\CMS\Core\Resource\ProcessedFile::CONTEXT_IMAGEPREVIEW, array());
282 if ($processedFile) {
283 $thumbUrl = $processedFile->getPublicUrl(true);
284 }
285 }
286 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
287 $result = array_merge(
288 $result->toArray(),
289 array(
290 'date' => BackendUtility::date($result->getModificationTime()),
291 'icon' => $iconFactory->getIconForFileExtension($result->getExtension(), Icon::SIZE_SMALL)->render(),
292 'thumbUrl' => $thumbUrl
293 )
294 );
295 } elseif ($result instanceof \TYPO3\CMS\Core\Resource\Folder) {
296 $result = $result->getIdentifier();
297 }
298
299 return $result;
300 }
301
302 /**
303 * Returns the current BE user.
304 *
305 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
306 */
307 protected function getBackendUser()
308 {
309 return $GLOBALS['BE_USER'];
310 }
311 }