756ccc8748bff2a101ba6a2412c2c0e34fba4bbe
[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 TYPO3\CMS\Core\Http\AjaxRequestHandler;
18 use TYPO3\CMS\Core\Resource\Folder;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Core\Utility\File\ExtendedFileUtility;
22
23 /**
24 * Gateway for TCE (TYPO3 Core Engine) file-handling through POST forms.
25 * This script serves as the file administration part of the TYPO3 Core Engine.
26 * Basically it includes two libraries which are used to manipulate files on the server.
27 * Before TYPO3 4.3, it was located in typo3/tce_file.php and redirected back to a
28 * $redirectURL. Since 4.3 this class is also used for accessing via AJAX
29 */
30 class FileController {
31
32 /**
33 * Array of file-operations.
34 *
35 * @var array
36 */
37 protected $file;
38
39 /**
40 * Clipboard operations array
41 *
42 * @var array
43 */
44 protected $CB;
45
46 /**
47 * Defines behaviour when uploading files with names that already exist; possible value are 'cancel', 'replace', 'changeName'
48 *
49 * @var string
50 */
51 protected $overwriteExistingFiles;
52
53 /**
54 * VeriCode - a hash of server specific value and other things which
55 * identifies if a submission is OK. (see $GLOBALS['BE_USER']->veriCode())
56 *
57 * @var string
58 */
59 protected $vC;
60
61 /**
62 * The page where the user should be redirected after everything is done
63 *
64 * @var string
65 */
66 protected $redirect;
67
68 /**
69 * Internal, dynamic:
70 * File processor object
71 *
72 * @var ExtendedFileUtility
73 */
74 protected $fileProcessor;
75
76 /**
77 * The result array from the file processor
78 *
79 * @var array
80 */
81 protected $fileData;
82
83 /**
84 * Constructor
85 */
86 public function __construct() {
87 $GLOBALS['SOBE'] = $this;
88 $this->init();
89 }
90
91 /**
92 * Registering incoming data
93 *
94 * @return void
95 */
96 protected function init() {
97 // Set the GPvars from outside
98 $this->file = GeneralUtility::_GP('file');
99 $this->CB = GeneralUtility::_GP('CB');
100 $this->overwriteExistingFiles = GeneralUtility::_GP('overwriteExistingFiles');
101
102 if ((string)$this->overwriteExistingFiles === '1') {
103 GeneralUtility::deprecationLog('overwriteExitingFiles = 1 is deprecated. Use overwriteExitingFiles = "replace". Support for old behavior will be removed in TYPO3 CMS 8.');
104 $this->overwriteExistingFiles = 'replace';
105 }
106
107 $this->vC = GeneralUtility::_GP('vC');
108 $this->redirect = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('redirect'));
109 $this->initClipboard();
110 $this->fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
111 }
112
113 /**
114 * Initialize the Clipboard. This will fetch the data about files to paste/delete if such an action has been sent.
115 *
116 * @return void
117 */
118 public function initClipboard() {
119 if (is_array($this->CB)) {
120 $clipObj = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Clipboard\Clipboard::class);
121 $clipObj->initializeClipboard();
122 if ($this->CB['paste']) {
123 $clipObj->setCurrentPad($this->CB['pad']);
124 $this->file = $clipObj->makePasteCmdArray_file($this->CB['paste'], $this->file);
125 }
126 if ($this->CB['delete']) {
127 $clipObj->setCurrentPad($this->CB['pad']);
128 $this->file = $clipObj->makeDeleteCmdArray_file($this->file);
129 }
130 }
131 }
132
133 /**
134 * Performing the file admin action:
135 * Initializes the objects, setting permissions, sending data to object.
136 *
137 * @return void
138 */
139 public function main() {
140 // Initializing:
141 $this->fileProcessor->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
142 $this->fileProcessor->setActionPermissions();
143 switch ($this->overwriteExistingFiles) {
144 case 'replace':
145 case 'changeName':
146 $conflictMode = $this->overwriteExistingFiles;
147 break;
148 default:
149 $conflictMode = 'cancel';
150 break;
151 }
152 $this->fileProcessor->setExistingFilesConflictMode($conflictMode);
153 // Checking referrer / executing:
154 $refInfo = parse_url(GeneralUtility::getIndpEnv('HTTP_REFERER'));
155 $httpHost = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
156 if ($httpHost !== $refInfo['host'] && $this->vC !== $this->getBackendUser()->veriCode() && !$GLOBALS['TYPO3_CONF_VARS']['SYS']['doNotCheckReferer']) {
157 $this->fileProcessor->writeLog(0, 2, 1, 'Referrer host "%s" and server host "%s" did not match!', array($refInfo['host'], $httpHost));
158 } else {
159 $this->fileProcessor->start($this->file);
160 $this->fileData = $this->fileProcessor->processData();
161 }
162 }
163
164 /**
165 * Redirecting the user after the processing has been done.
166 * Might also display error messages directly, if any.
167 *
168 * @return void
169 */
170 public function finish() {
171 // Push errors to flash message queue, if there are any
172 $this->fileProcessor->pushErrorMessagesToFlashMessageQueue();
173 BackendUtility::setUpdateSignal('updateFolderTree');
174 if ($this->redirect) {
175 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($this->redirect);
176 }
177 }
178
179 /**
180 * Handles the actual process from within the ajaxExec function
181 * therefore, it does exactly the same as the real typo3/tce_file.php
182 * but without calling the "finish" method, thus makes it simpler to deal with the
183 * actual return value
184 *
185 * @param array $params Always empty.
186 * @param AjaxRequestHandler $ajaxObj The AjaxRequestHandler object used to return content and set content types
187 * @return void
188 */
189 public function processAjaxRequest(array $params, AjaxRequestHandler $ajaxObj) {
190 $this->init();
191 $this->main();
192 $errors = $this->fileProcessor->getErrorMessages();
193 if (!empty($errors)) {
194 $ajaxObj->setError(implode(',', $errors));
195 } else {
196 $flatResult = array();
197 foreach ($this->fileData as $action => $results) {
198 foreach ($results as $result) {
199 if (is_array($result)) {
200 foreach ($result as $subResult) {
201 $flatResult[$action][] = $this->flattenResultDataValue($subResult);
202 }
203 } else {
204 $flatResult[$action][] = $this->flattenResultDataValue($result);
205 }
206 }
207 }
208 $ajaxObj->addContent('result', $flatResult);
209 if ($this->redirect) {
210 $ajaxObj->addContent('redirect', $this->redirect);
211 }
212 $ajaxObj->setContentFormat('json');
213 }
214 }
215
216 /**
217 * Ajax entry point to check if a file exists in a folder
218 *
219 * @param array $params Always empty.
220 * @param AjaxRequestHandler $ajaxObj The AjaxRequestHandler object used to return content and set content types
221 * @return void
222 */
223 public function fileExistsAjaxRequest(array $params, AjaxRequestHandler $ajaxObj) {
224 $fileName = GeneralUtility::_GP('fileName');
225 $fileTarget = GeneralUtility::_GP('fileTarget');
226
227 /** @var \TYPO3\CMS\Core\Resource\ResourceFactory $fileFactory */
228 $fileFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
229 /** @var Folder $fileTargetObject */
230 $fileTargetObject = $fileFactory->retrieveFileOrFolderObject($fileTarget);
231 $processedFileName = $fileTargetObject->getStorage()->sanitizeFileName($fileName, $fileTargetObject);
232
233 $result = FALSE;
234 if ($fileTargetObject->hasFile($processedFileName)) {
235 $result = $this->flattenResultDataValue($fileTargetObject->getStorage()->getFileInFolder($processedFileName, $fileTargetObject));
236 }
237 $ajaxObj->addContent('result', $result);
238 $ajaxObj->setContentFormat('json');
239 }
240
241 /**
242 * Flatten result value from FileProcessor
243 *
244 * The value can be a File, Folder or boolean
245 *
246 * @param bool|\TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder $result
247 * @return bool|string|array
248 */
249 protected function flattenResultDataValue($result) {
250 if ($result instanceof \TYPO3\CMS\Core\Resource\File) {
251 $thumbUrl = '';
252 if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $result->getExtension())) {
253 $processedFile = $result->process(\TYPO3\CMS\Core\Resource\ProcessedFile::CONTEXT_IMAGEPREVIEW, array());
254 if ($processedFile) {
255 $thumbUrl = $processedFile->getPublicUrl(TRUE);
256 }
257 }
258 $result = array_merge(
259 $result->toArray(),
260 array (
261 'date' => BackendUtility::date($result->getModificationTime()),
262 'iconClasses' => \TYPO3\CMS\Backend\Utility\IconUtility::mapFileExtensionToSpriteIconClass($result->getExtension()),
263 'thumbUrl' => $thumbUrl
264 )
265 );
266 } elseif ($result instanceof \TYPO3\CMS\Core\Resource\Folder) {
267 $result = $result->getIdentifier();
268 }
269
270 return $result;
271 }
272
273 /**
274 * Returns the current BE user.
275 *
276 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
277 */
278 protected function getBackendUser() {
279 return $GLOBALS['BE_USER'];
280 }
281
282 }