‪TYPO3CMS  10.4
OnlineMediaController.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
29 
35 {
42  public function ‪createAction(ServerRequestInterface $request): ResponseInterface
43  {
44  $url = $request->getParsedBody()['url'];
45  $targetFolderIdentifier = $request->getParsedBody()['targetFolder'];
46  $allowedExtensions = ‪GeneralUtility::trimExplode(',', $request->getParsedBody()['allowed'] ?: '');
47 
48  if (!empty($url)) {
49  $data = [];
50  $file = $this->‪addMediaFromUrl($url, $targetFolderIdentifier, $allowedExtensions);
51  if ($file !== null) {
52  $data['file'] = $file->getUid();
53  } else {
54  $data['error'] = $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.invalid_url');
55  }
56  return new ‪JsonResponse($data);
57  }
58  return new ‪JsonResponse();
59  }
60 
68  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
69  {
70  $files = $request->getParsedBody()['data'];
71  $redirect = $request->getParsedBody()['redirect'];
72  $newMedia = [];
73  if (isset($files['newMedia'])) {
74  $newMedia = (array)$files['newMedia'];
75  }
76 
77  foreach ($newMedia as $media) {
78  if (!empty($media['url']) && !empty($media['target'])) {
79  $allowed = !empty($media['allowed']) ? ‪GeneralUtility::trimExplode(',', $media['allowed']) : [];
80  $file = $this->‪addMediaFromUrl($media['url'], $media['target'], $allowed);
81  if ($file !== null) {
82  $flashMessage = GeneralUtility::makeInstance(
83  FlashMessage::class,
84  $file->getName(),
85  $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.added'),
87  true
88  );
89  } else {
90  $flashMessage = GeneralUtility::makeInstance(
91  FlashMessage::class,
92  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.invalid_url'),
93  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.new_media.failed'),
95  true
96  );
97  }
98  $this->‪addFlashMessage($flashMessage);
99  if (empty($redirect) && $media['redirect']) {
100  $redirect = $media['redirect'];
101  }
102  }
103  }
104 
105  $redirect = GeneralUtility::sanitizeLocalUrl($redirect);
106  if ($redirect) {
107  return new ‪RedirectResponse($redirect, 303);
108  }
109 
110  throw new \RuntimeException('No redirect after uploading a media found, probably a mis-use of the template not sending the proper Return URL.', 1511945040);
111  }
112 
119  protected function ‪addMediaFromUrl($url, $targetFolderIdentifier, array $allowedExtensions = [])
120  {
121  $targetFolder = null;
122  if ($targetFolderIdentifier) {
123  try {
124  $targetFolder = GeneralUtility::makeInstance(ResourceFactory::class)->getFolderObjectFromCombinedIdentifier($targetFolderIdentifier);
125  } catch (\‪Exception $e) {
126  $targetFolder = null;
127  }
128  }
129  if ($targetFolder === null) {
130  $targetFolder = $this->‪getBackendUser()->‪getDefaultUploadFolder();
131  }
132  return ‪OnlineMediaHelperRegistry::getInstance()->‪transformUrlToFile($url, $targetFolder, $allowedExtensions);
133  }
134 
140  protected function ‪addFlashMessage(‪FlashMessage $flashMessage)
141  {
143  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
144 
146  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
147  $defaultFlashMessageQueue->enqueue($flashMessage);
148  }
149 
153  protected function ‪getBackendUser()
154  {
155  return ‪$GLOBALS['BE_USER'];
156  }
157 
161  protected function ‪getLanguageService()
162  {
163  return ‪$GLOBALS['LANG'];
164  }
165 }
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry
Definition: OnlineMediaHelperRegistry.php:27
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\addMediaFromUrl
‪File null addMediaFromUrl($url, $targetFolderIdentifier, array $allowedExtensions=[])
Definition: OnlineMediaController.php:119
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry\transformUrlToFile
‪File null transformUrlToFile($url, Folder $targetFolder, $allowedExtensions=[])
Definition: OnlineMediaHelperRegistry.php:72
‪TYPO3\CMS\Backend\Exception
Definition: Exception.php:24
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: OnlineMediaController.php:68
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:194
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\addFlashMessage
‪addFlashMessage(FlashMessage $flashMessage)
Definition: OnlineMediaController.php:140
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\createAction
‪ResponseInterface createAction(ServerRequestInterface $request)
Definition: OnlineMediaController.php:42
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\getDefaultUploadFolder
‪TYPO3 CMS Core Resource Folder bool getDefaultUploadFolder($pid=null, $table=null, $field=null)
Definition: BackendUserAuthentication.php:1933
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\getBackendUser
‪TYPO3 CMS Core Authentication BackendUserAuthentication getBackendUser()
Definition: OnlineMediaController.php:153
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\Messaging\AbstractMessage\OK
‪const OK
Definition: AbstractMessage.php:29
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry\getInstance
‪static OnlineMediaHelperRegistry getInstance()
Definition: OnlineMediaHelperRegistry.php:33
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Controller
Definition: AbstractFormEngineAjaxController.php:18
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27
‪TYPO3\CMS\Backend\Controller\OnlineMediaController
Definition: OnlineMediaController.php:35
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:31
‪TYPO3\CMS\Backend\Controller\OnlineMediaController\getLanguageService
‪LanguageService getLanguageService()
Definition: OnlineMediaController.php:161