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