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