TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
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 
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
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->setActionPermissions();
145  $this->fileProcessor->setExistingFilesConflictMode($this->overwriteExistingFiles);
146  // Checking referrer / executing:
147  $refInfo = parse_url(GeneralUtility::getIndpEnv('HTTP_REFERER'));
148  $httpHost = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
149  if ($httpHost !== $refInfo['host'] && $this->vC !== $this->getBackendUser()->veriCode() && !$GLOBALS['TYPO3_CONF_VARS']['SYS']['doNotCheckReferer']) {
150  $this->fileProcessor->writeLog(0, 2, 1, 'Referrer host "%s" and server host "%s" did not match!', [$refInfo['host'], $httpHost]);
151  } else {
152  $this->fileProcessor->start($this->file);
153  $this->fileData = $this->fileProcessor->processData();
154  }
155  }
156 
163  public function finish()
164  {
165  BackendUtility::setUpdateSignal('updateFolderTree');
166  if ($this->redirect) {
168  }
169  }
170 
179  public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
180  {
181  $this->main();
182 
183  BackendUtility::setUpdateSignal('updateFolderTree');
184 
185  // go and edit the new created file
186  if ($request->getParsedBody()['edit']) {
187  $urlParameters = [
188  'target' => $this->file['newfile'][0]['target'] . $this->file['newfile'][0]['data']
189  ];
190  if ($this->redirect) {
191  $urlParameters['returnUrl'] = $this->redirect;
192  }
193  $this->redirect = BackendUtility::getModuleUrl('file_edit', $urlParameters);
194  }
195  if ($this->redirect) {
196  return $response
197  ->withHeader('Location', GeneralUtility::locationHeaderUrl($this->redirect))
198  ->withStatus(303);
199  } else {
200  // empty response
201  return $response;
202  }
203  }
204 
215  public function processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
216  {
217  $this->main();
218  $errors = $this->fileProcessor->getErrorMessages();
219  if (!empty($errors)) {
220  $response->getBody()->write('<t3err>' . implode(',', $errors) . '</t3err>');
221  $response = $response
222  ->withHeader('Content-Type', 'text/html; charset=utf-8')
223  ->withStatus(500, '(AJAX)');
224  } else {
225  $flatResult = [];
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 
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 
255  $fileFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
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 
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, []);
282  if ($processedFile) {
283  $thumbUrl = $processedFile->getPublicUrl(true);
284  }
285  }
286  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
287  $result = array_merge(
288  $result->toArray(),
289  [
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 
307  protected function getBackendUser()
308  {
309  return $GLOBALS['BE_USER'];
310  }
311 }
static redirect($url, $httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:76
mainAction(ServerRequestInterface $request, ResponseInterface $response)
processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)