24f025da4339f11acc280fbec9028d7fbcc0e4db
[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\Localization\LanguageService;
20 use TYPO3\CMS\Core\Messaging\FlashMessage;
21 use TYPO3\CMS\Core\Messaging\FlashMessageService;
22 use TYPO3\CMS\Core\Resource\File;
23 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
24 use TYPO3\CMS\Core\Resource\ResourceFactory;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27 /**
28 * Class OnlineMediaController handles uploading online media
29 */
30 class OnlineMediaController
31 {
32 /**
33 * AJAX endpoint for storing the URL as a sys_file record
34 *
35 * @param ServerRequestInterface $request
36 * @param ResponseInterface $response
37 * @return ResponseInterface
38 */
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
58 /**
59 * Process add media request
60 *
61 * @param ServerRequestInterface $request
62 * @param ResponseInterface $response
63 * @return ResponseInterface
64 */
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'),
82 FlashMessage::OK,
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'),
90 FlashMessage::ERROR,
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
109 /**
110 * @param string $url
111 * @param string $targetFolderIdentifier
112 * @param string[] $allowedExtensions
113 * @return File|NULL
114 */
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
131 /**
132 * Add flash message to message queue
133 *
134 * @param FlashMessage $flashMessage
135 */
136 protected function addFlashMessage(FlashMessage $flashMessage)
137 {
138 /** @var $flashMessageService FlashMessageService */
139 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
140
141 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
142 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
143 $defaultFlashMessageQueue->enqueue($flashMessage);
144 }
145
146 /**
147 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
148 */
149 protected function getBackendUser()
150 {
151 return $GLOBALS['BE_USER'];
152 }
153
154 /**
155 * @return LanguageService
156 */
157 protected function getLanguageService()
158 {
159 return $GLOBALS['LANG'];
160 }
161 }