TYPO3  7.6
FileController.php
Go to the documentation of this file.
1 <?php
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 
26 
35 {
41  protected $file;
42 
48  protected $CB;
49 
57 
64  protected $vC;
65 
71  protected $redirect;
72 
79  protected $fileProcessor;
80 
86  protected $fileData;
87 
91  public function __construct()
92  {
93  $GLOBALS['SOBE'] = $this;
94  $this->init();
95  }
96 
102  protected function init()
103  {
104  // Set the GPvars from outside
105  $this->file = GeneralUtility::_GP('file');
106  $this->CB = GeneralUtility::_GP('CB');
107  $this->overwriteExistingFiles = DuplicationBehavior::cast(GeneralUtility::_GP('overwriteExistingFiles'));
108  $this->vC = GeneralUtility::_GP('vC');
109  $this->redirect = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('redirect'));
110  $this->initClipboard();
111  $this->fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
112  }
113 
119  public function initClipboard()
120  {
121  if (is_array($this->CB)) {
122  $clipObj = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Clipboard\Clipboard::class);
123  $clipObj->initializeClipboard();
124  if ($this->CB['paste']) {
125  $clipObj->setCurrentPad($this->CB['pad']);
126  $this->file = $clipObj->makePasteCmdArray_file($this->CB['paste'], $this->file);
127  }
128  if ($this->CB['delete']) {
129  $clipObj->setCurrentPad($this->CB['pad']);
130  $this->file = $clipObj->makeDeleteCmdArray_file($this->file);
131  }
132  }
133  }
134 
141  public function main()
142  {
143  // Initializing:
144  $this->fileProcessor->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
145  $this->fileProcessor->setActionPermissions();
146  $this->fileProcessor->setExistingFilesConflictMode($this->overwriteExistingFiles);
147  // Checking referrer / executing:
148  $refInfo = parse_url(GeneralUtility::getIndpEnv('HTTP_REFERER'));
149  $httpHost = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
150  if ($httpHost !== $refInfo['host'] && $this->vC !== $this->getBackendUser()->veriCode() && !$GLOBALS['TYPO3_CONF_VARS']['SYS']['doNotCheckReferer']) {
151  $this->fileProcessor->writeLog(0, 2, 1, 'Referrer host "%s" and server host "%s" did not match!', array($refInfo['host'], $httpHost));
152  } else {
153  $this->fileProcessor->start($this->file);
154  $this->fileData = $this->fileProcessor->processData();
155  }
156  }
157 
164  public function finish()
165  {
166  // Push errors to flash message queue, if there are any
167  $this->fileProcessor->pushErrorMessagesToFlashMessageQueue();
168  BackendUtility::setUpdateSignal('updateFolderTree');
169  if ($this->redirect) {
170  \TYPO3\CMS\Core\Utility\HttpUtility::redirect($this->redirect);
171  }
172  }
173 
183  {
184  $this->main();
185 
186  // Push errors to flash message queue, if there are any
187  $this->fileProcessor->pushErrorMessagesToFlashMessageQueue();
188  BackendUtility::setUpdateSignal('updateFolderTree');
189 
190  if ($this->redirect) {
191  return $response
192  ->withHeader('Location', GeneralUtility::locationHeaderUrl($this->redirect))
193  ->withStatus(303);
194  } else {
195  // empty response
196  return $response;
197  }
198  }
199 
211  {
212  $this->main();
213  $errors = $this->fileProcessor->getErrorMessages();
214  if (!empty($errors)) {
215  $response->getBody()->write(implode(',', $errors));
216  $response = $response->withHeader('Content-Type', 'text/html; charset=utf-8');
217  } else {
218  $flatResult = array();
219  foreach ($this->fileData as $action => $results) {
220  foreach ($results as $result) {
221  if (is_array($result)) {
222  foreach ($result as $subResult) {
223  $flatResult[$action][] = $this->flattenResultDataValue($subResult);
224  }
225  } else {
226  $flatResult[$action][] = $this->flattenResultDataValue($result);
227  }
228  }
229  }
230  $response->getBody()->write(json_encode($flatResult));
231  }
232  return $response;
233  }
234 
242  public function fileExistsInFolderAction(ServerRequestInterface $request, ResponseInterface $response)
243  {
244  $fileName = isset($request->getParsedBody()['fileName']) ? $request->getParsedBody()['fileName'] : $request->getQueryParams()['fileName'];
245  $fileTarget = isset($request->getParsedBody()['fileTarget']) ? $request->getParsedBody()['fileTarget'] : $request->getQueryParams()['fileTarget'];
246 
248  $fileFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
250  $fileTargetObject = $fileFactory->retrieveFileOrFolderObject($fileTarget);
251  $processedFileName = $fileTargetObject->getStorage()->sanitizeFileName($fileName, $fileTargetObject);
252 
253  $result = false;
254  if ($fileTargetObject->hasFile($processedFileName)) {
255  $result = $this->flattenResultDataValue($fileTargetObject->getStorage()->getFileInFolder($processedFileName, $fileTargetObject));
256  }
257  $response->getBody()->write(json_encode($result));
258  return $response;
259  }
260 
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, array());
275  if ($processedFile) {
276  $thumbUrl = $processedFile->getPublicUrl(true);
277  }
278  }
279  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
280  $result = array_merge(
281  $result->toArray(),
282  array(
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 
300  protected function getBackendUser()
301  {
302  return $GLOBALS['BE_USER'];
303  }
304 }