TYPO3 CMS  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([], $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
145  $this->fileProcessor->setActionPermissions();
146  $this->fileProcessor->setExistingFilesConflictMode($this->overwriteExistingFiles);
147  $this->fileProcessor->start($this->file);
148  $this->fileData = $this->fileProcessor->processData();
149  }
150 
157  public function finish()
158  {
159  BackendUtility::setUpdateSignal('updateFolderTree');
160  if ($this->redirect) {
162  }
163  }
164 
173  public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
174  {
175  $this->main();
176 
177  BackendUtility::setUpdateSignal('updateFolderTree');
178 
179  if ($this->redirect) {
180  return $response
181  ->withHeader('Location', GeneralUtility::locationHeaderUrl($this->redirect))
182  ->withStatus(303);
183  } else {
184  // empty response
185  return $response;
186  }
187  }
188 
199  public function processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
200  {
201  $this->main();
202  $errors = $this->fileProcessor->getErrorMessages();
203  if (!empty($errors)) {
204  $response->getBody()->write('<t3err>' . implode(',', $errors) . '</t3err>');
205  $response = $response
206  ->withHeader('Content-Type', 'text/html; charset=utf-8')
207  ->withStatus(500, '(AJAX)');
208  } else {
209  $flatResult = [];
210  foreach ($this->fileData as $action => $results) {
211  foreach ($results as $result) {
212  if (is_array($result)) {
213  foreach ($result as $subResult) {
214  $flatResult[$action][] = $this->flattenResultDataValue($subResult);
215  }
216  } else {
217  $flatResult[$action][] = $this->flattenResultDataValue($result);
218  }
219  }
220  }
221  $response->getBody()->write(json_encode($flatResult));
222  }
223  return $response;
224  }
225 
233  public function fileExistsInFolderAction(ServerRequestInterface $request, ResponseInterface $response)
234  {
235  $fileName = isset($request->getParsedBody()['fileName']) ? $request->getParsedBody()['fileName'] : $request->getQueryParams()['fileName'];
236  $fileTarget = isset($request->getParsedBody()['fileTarget']) ? $request->getParsedBody()['fileTarget'] : $request->getQueryParams()['fileTarget'];
237 
239  $fileFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
241  $fileTargetObject = $fileFactory->retrieveFileOrFolderObject($fileTarget);
242  $processedFileName = $fileTargetObject->getStorage()->sanitizeFileName($fileName, $fileTargetObject);
243 
244  $result = false;
245  if ($fileTargetObject->hasFile($processedFileName)) {
246  $result = $this->flattenResultDataValue($fileTargetObject->getStorage()->getFileInFolder($processedFileName, $fileTargetObject));
247  }
248  $response->getBody()->write(json_encode($result));
249  return $response;
250  }
251 
260  protected function flattenResultDataValue($result)
261  {
262  if ($result instanceof \TYPO3\CMS\Core\Resource\File) {
263  $thumbUrl = '';
264  if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $result->getExtension())) {
265  $processedFile = $result->process(\TYPO3\CMS\Core\Resource\ProcessedFile::CONTEXT_IMAGEPREVIEW, []);
266  if ($processedFile) {
267  $thumbUrl = $processedFile->getPublicUrl(true);
268  }
269  }
270  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
271  $result = array_merge(
272  $result->toArray(),
273  [
274  'date' => BackendUtility::date($result->getModificationTime()),
275  'icon' => $iconFactory->getIconForFileExtension($result->getExtension(), Icon::SIZE_SMALL)->render(),
276  'thumbUrl' => $thumbUrl
277  ]
278  );
279  } elseif ($result instanceof \TYPO3\CMS\Core\Resource\Folder) {
280  $result = $result->getIdentifier();
281  }
282 
283  return $result;
284  }
285 
291  protected function getBackendUser()
292  {
293  return $GLOBALS['BE_USER'];
294  }
295 }
mainAction(ServerRequestInterface $request, ResponseInterface $response)
static setUpdateSignal($set='', $params='')
processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
static redirect($url, $httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:76
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']