2cb512c8d8e2e298bea8e2132cee13721b8b0d64
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / OnlineMediaController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller;
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;
19 use TYPO3\CMS\Core\Http\JsonResponse;
20 use TYPO3\CMS\Core\Http\RedirectResponse;
21 use TYPO3\CMS\Core\Localization\LanguageService;
22 use TYPO3\CMS\Core\Messaging\FlashMessage;
23 use TYPO3\CMS\Core\Messaging\FlashMessageService;
24 use TYPO3\CMS\Core\Resource\File;
25 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
26 use TYPO3\CMS\Core\Resource\ResourceFactory;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29 /**
30 * Class OnlineMediaController handles uploading online media
31 */
32 class OnlineMediaController
33 {
34 /**
35 * AJAX endpoint for storing the URL as a sys_file record
36 *
37 * @return ResponseInterface
38 */
39 public function createAction(ServerRequestInterface $request): ResponseInterface
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:core/Resources/Private/Language/locallang_core.xlf:online_media.error.invalid_url');
52 }
53 return new JsonResponse($data);
54 }
55 return new JsonResponse();
56 }
57
58 /**
59 * Process add media request, and redirects to the previous page
60 *
61 * @param ServerRequestInterface $request
62 * @return ResponseInterface
63 * @throws \RuntimeException
64 */
65 public function mainAction(ServerRequestInterface $request): ResponseInterface
66 {
67 $files = $request->getParsedBody()['data'];
68 $redirect = $request->getParsedBody()['redirect'];
69 $newMedia = [];
70 if (isset($files['newMedia'])) {
71 $newMedia = (array)$files['newMedia'];
72 }
73
74 foreach ($newMedia as $media) {
75 if (!empty($media['url']) && !empty($media['target'])) {
76 $allowed = !empty($media['allowed']) ? GeneralUtility::trimExplode(',', $media['allowed']) : [];
77 $file = $this->addMediaFromUrl($media['url'], $media['target'], $allowed);
78 if ($file !== null) {
79 $flashMessage = GeneralUtility::makeInstance(
80 FlashMessage::class,
81 $file->getName(),
82 $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.added'),
83 FlashMessage::OK,
84 true
85 );
86 } else {
87 $flashMessage = GeneralUtility::makeInstance(
88 FlashMessage::class,
89 $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.invalid_url'),
90 $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.error.new_media.failed'),
91 FlashMessage::ERROR,
92 true
93 );
94 }
95 $this->addFlashMessage($flashMessage);
96 if (empty($redirect) && $media['redirect']) {
97 $redirect = $media['redirect'];
98 }
99 }
100 }
101
102 $redirect = GeneralUtility::sanitizeLocalUrl($redirect);
103 if ($redirect) {
104 return new RedirectResponse($redirect, 303);
105 }
106
107 throw new \RuntimeException('No redirect after uploading a media found, probably a mis-use of the template not sending the proper Return URL.', 1511945040);
108 }
109
110 /**
111 * @param string $url
112 * @param string $targetFolderIdentifier
113 * @param string[] $allowedExtensions
114 * @return File|null
115 */
116 protected function addMediaFromUrl($url, $targetFolderIdentifier, array $allowedExtensions = [])
117 {
118 $targetFolder = null;
119 if ($targetFolderIdentifier) {
120 try {
121 $targetFolder = ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier($targetFolderIdentifier);
122 } catch (\Exception $e) {
123 $targetFolder = null;
124 }
125 }
126 if ($targetFolder === null) {
127 $targetFolder = $this->getBackendUser()->getDefaultUploadFolder();
128 }
129 return OnlineMediaHelperRegistry::getInstance()->transformUrlToFile($url, $targetFolder, $allowedExtensions);
130 }
131
132 /**
133 * Add flash message to message queue
134 *
135 * @param FlashMessage $flashMessage
136 */
137 protected function addFlashMessage(FlashMessage $flashMessage)
138 {
139 /** @var FlashMessageService $flashMessageService */
140 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
141
142 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue $defaultFlashMessageQueue */
143 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
144 $defaultFlashMessageQueue->enqueue($flashMessage);
145 }
146
147 /**
148 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
149 */
150 protected function getBackendUser()
151 {
152 return $GLOBALS['BE_USER'];
153 }
154
155 /**
156 * @return LanguageService
157 */
158 protected function getLanguageService()
159 {
160 return $GLOBALS['LANG'];
161 }
162 }