TYPO3 CMS  TYPO3_8-7
OnlineMediaController.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 
31 {
39  public function createAction(ServerRequestInterface $request, ResponseInterface $response)
40  {
41  $url = $request->getParsedBody()['url'];
42  $targetFolderIdentifier = $request->getParsedBody()['targetFolder'];
43  $allowedExtensions = GeneralUtility::trimExplode(',', $request->getParsedBody()['allowed'] ?: '');
44 
45  if (!empty($url)) {
46  $data = [];
47  $file = $this->addMediaFromUrl($url, $targetFolderIdentifier, $allowedExtensions);
48  if ($file !== null) {
49  $data['file'] = $file->getUid();
50  } else {
51  $data['error'] = $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:online_media.error.invalid_url');
52  }
53  $response->getBody()->write(json_encode($data));
54  }
55  return $response;
56  }
57 
65  public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
66  {
67  $files = $request->getParsedBody()['file'];
68  $newMedia = [];
69  if (isset($files['newMedia'])) {
70  $newMedia = (array)$files['newMedia'];
71  }
72 
73  foreach ($newMedia as $media) {
74  if (!empty($media['url']) && !empty($media['target'])) {
75  $allowed = !empty($media['allowed']) ? GeneralUtility::trimExplode(',', $media['allowed']) : [];
76  $file = $this->addMediaFromUrl($media['url'], $media['target'], $allowed);
77  if ($file !== null) {
78  $flashMessage = GeneralUtility::makeInstance(
79  FlashMessage::class,
80  $file->getName(),
81  $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:online_media.new_media.added'),
83  true
84  );
85  } else {
86  $flashMessage = GeneralUtility::makeInstance(
87  FlashMessage::class,
88  $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:online_media.error.invalid_url'),
89  $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:online_media.error.new_media.failed'),
91  true
92  );
93  }
94  $this->addFlashMessage($flashMessage);
95  }
96  }
97 
98  $redirect = isset($request->getParsedBody()['redirect']) ? $request->getParsedBody()['redirect'] : $request->getQueryParams()['redirect'];
99  $redirect = GeneralUtility::sanitizeLocalUrl($redirect);
100  if ($redirect) {
101  $response = $response
102  ->withHeader('Location', GeneralUtility::locationHeaderUrl($redirect))
103  ->withStatus(303);
104  }
105 
106  return $response;
107  }
108 
115  protected function addMediaFromUrl($url, $targetFolderIdentifier, array $allowedExtensions = [])
116  {
117  $targetFolder = null;
118  if ($targetFolderIdentifier) {
119  try {
120  $targetFolder = ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier($targetFolderIdentifier);
121  } catch (\Exception $e) {
122  $targetFolder = null;
123  }
124  }
125  if ($targetFolder === null) {
126  $targetFolder = $this->getBackendUser()->getDefaultUploadFolder();
127  }
128  return OnlineMediaHelperRegistry::getInstance()->transformUrlToFile($url, $targetFolder, $allowedExtensions);
129  }
130 
136  protected function addFlashMessage(FlashMessage $flashMessage)
137  {
139  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
140 
142  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
143  $defaultFlashMessageQueue->enqueue($flashMessage);
144  }
145 
149  protected function getBackendUser()
150  {
151  return $GLOBALS['BE_USER'];
152  }
153 
157  protected function getLanguageService()
158  {
159  return $GLOBALS['LANG'];
160  }
161 }
addMediaFromUrl($url, $targetFolderIdentifier, array $allowedExtensions=[])
mainAction(ServerRequestInterface $request, ResponseInterface $response)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static makeInstance($className,... $constructorArguments)
createAction(ServerRequestInterface $request, ResponseInterface $response)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']