[TASK] Create own response instance in controller actions
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / FileUploadController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller\File;
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\Backend\Template\ModuleTemplate;
20 use TYPO3\CMS\Core\Http\HtmlResponse;
21 use TYPO3\CMS\Core\Imaging\Icon;
22 use TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException;
23 use TYPO3\CMS\Core\Resource\ResourceFactory;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * Script Class for display up to 10 upload fields
28 */
29 class FileUploadController
30 {
31 /**
32 * Name of the filemount
33 *
34 * @var string
35 */
36 public $title;
37
38 /**
39 * Set with the target path inputted in &target
40 *
41 * @var string
42 */
43 public $target;
44
45 /**
46 * Return URL of list module.
47 *
48 * @var string
49 */
50 public $returnUrl;
51
52 /**
53 * Accumulating content
54 *
55 * @var string
56 */
57 public $content;
58
59 /**
60 * The folder object which is the target directory for the upload
61 *
62 * @var \TYPO3\CMS\Core\Resource\Folder $folderObject
63 */
64 protected $folderObject;
65
66 /**
67 * ModuleTemplate object
68 *
69 * @var ModuleTemplate
70 */
71 protected $moduleTemplate;
72
73 /**
74 * Constructor
75 */
76 public function __construct()
77 {
78 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
79 $GLOBALS['SOBE'] = $this;
80 $this->getLanguageService()->includeLLFile('EXT:lang/Resources/Private/Language/locallang_misc.xlf');
81 $this->init();
82 }
83
84 /**
85 * Initialize
86 *
87 * @throws InsufficientFolderAccessPermissionsException
88 */
89 protected function init()
90 {
91 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
92 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
93 // Initialize GPvars:
94 $this->target = GeneralUtility::_GP('target');
95 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
96 if (!$this->returnUrl) {
97 $this->returnUrl = (string)$uriBuilder->buildUriFromRoute('file_list', [
98 'id' => rawurlencode($this->target)
99 ]);
100 }
101 // Create the folder object
102 if ($this->target) {
103 $this->folderObject = ResourceFactory::getInstance()
104 ->retrieveFileOrFolderObject($this->target);
105 }
106 if ($this->folderObject->getStorage()->getUid() === 0) {
107 throw new InsufficientFolderAccessPermissionsException(
108 'You are not allowed to access folders outside your storages',
109 1375889834
110 );
111 }
112
113 // Cleaning and checking target directory
114 if (!$this->folderObject) {
115 $title = $this->getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
116 $message = $this->getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
117 throw new \RuntimeException($title . ': ' . $message, 1294586843);
118 }
119
120 // Setting up the context sensitive menu
121 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
122
123 // building pathInfo for metaInformation
124 $pathInfo = [
125 'combined_identifier' => $this->folderObject->getCombinedIdentifier(),
126 ];
127 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
128 }
129
130 /**
131 * Main function, rendering the upload file form fields
132 */
133 public function main()
134 {
135 $lang = $this->getLanguageService();
136 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
137 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
138
139 // set page title
140 $this->moduleTemplate->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:file_upload.php.pagetitle'));
141
142 $pageContent = '<form action="'
143 . htmlspecialchars((string)$uriBuilder->buildUriFromRoute('tce_file'))
144 . '" method="post" id="FileUploadController" name="editform" enctype="multipart/form-data">';
145 // Make page header:
146 $pageContent .= '<h1>' . $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:file_upload.php.pagetitle') . '</h1>';
147 $pageContent .= $this->renderUploadForm();
148
149 // Header Buttons
150 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
151
152 // csh button
153 $cshButton = $buttonBar->makeHelpButton()
154 ->setModuleName('xMOD_csh_corebe')
155 ->setFieldName('file_upload');
156 $buttonBar->addButton($cshButton);
157
158 // back button
159 if ($this->returnUrl) {
160 $backButton = $buttonBar->makeLinkButton()
161 ->setHref($this->returnUrl)
162 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
163 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
164 $buttonBar->addButton($backButton);
165 }
166
167 $pageContent .= '</form>';
168 $this->content .= '<div>' . $pageContent . '</div>';
169 $this->moduleTemplate->setContent($this->content);
170 }
171
172 /**
173 * This function renders the upload form
174 *
175 * @return string The HTML form as a string, ready for outputting
176 */
177 public function renderUploadForm()
178 {
179 // Make checkbox for "overwrite"
180 $content = '
181 <div id="c-override">
182 <p><label for="overwriteExistingFiles"><input type="checkbox" class="checkbox" name="overwriteExistingFiles" id="overwriteExistingFiles" value="replace" /> ' . htmlspecialchars($this->getLanguageService()->getLL('overwriteExistingFiles')) . '</label></p>
183 <p>&nbsp;</p>
184 <p>' . htmlspecialchars($this->getLanguageService()->getLL('uploadMultipleFilesInfo')) . '</p>
185 </div>
186 ';
187 // Produce the number of upload-fields needed:
188 $content .= '
189 <div id="c-upload">
190 ';
191 // Adding 'size="50" ' for the sake of Mozilla!
192 $content .= '
193 <input type="file" multiple="multiple" name="upload_1[]" />
194 <input type="hidden" name="data[upload][1][target]" value="' . htmlspecialchars($this->folderObject->getCombinedIdentifier()) . '" />
195 <input type="hidden" name="data[upload][1][data]" value="1" /><br />
196 ';
197 $content .= '
198 </div>
199 ';
200 // Submit button:
201 $content .= '
202 <div id="c-submit">
203 <input type="hidden" name="data[upload][1][redirect]" value="' . $this->returnUrl . '" /><br />
204 <input class="btn btn-default" type="submit" value="' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:file_upload.php.submit')) . '" />
205 </div>
206 ';
207 return $content;
208 }
209
210 /**
211 * Processes the request, currently everything is handled and put together via "main()"
212 *
213 * @param ServerRequestInterface $request the current request
214 * @return ResponseInterface the response with the content
215 */
216 public function mainAction(ServerRequestInterface $request): ResponseInterface
217 {
218 $this->main();
219 return new HtmlResponse($this->moduleTemplate->renderContent());
220 }
221
222 /**
223 * Returns LanguageService
224 *
225 * @return \TYPO3\CMS\Core\Localization\LanguageService
226 */
227 protected function getLanguageService()
228 {
229 return $GLOBALS['LANG'];
230 }
231 }