eced801dfd512684de4175e4df5d750563e8d2e2
[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\Backend\Utility\BackendUtility;
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 // Initialize GPvars:
92 $this->target = GeneralUtility::_GP('target');
93 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
94 if (!$this->returnUrl) {
95 $this->returnUrl = BackendUtility::getModuleUrl('file_list', [
96 'id' => rawurlencode($this->target)
97 ]);
98 }
99 // Create the folder object
100 if ($this->target) {
101 $this->folderObject = ResourceFactory::getInstance()
102 ->retrieveFileOrFolderObject($this->target);
103 }
104 if ($this->folderObject->getStorage()->getUid() === 0) {
105 throw new InsufficientFolderAccessPermissionsException(
106 'You are not allowed to access folders outside your storages',
107 1375889834
108 );
109 }
110
111 // Cleaning and checking target directory
112 if (!$this->folderObject) {
113 $title = $this->getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
114 $message = $this->getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
115 throw new \RuntimeException($title . ': ' . $message, 1294586843);
116 }
117
118 // Setting up the context sensitive menu
119 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
120
121 // building pathInfo for metaInformation
122 $pathInfo = [
123 'combined_identifier' => $this->folderObject->getCombinedIdentifier(),
124 ];
125 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
126 }
127
128 /**
129 * Main function, rendering the upload file form fields
130 */
131 public function main()
132 {
133 $lang = $this->getLanguageService();
134
135 // set page title
136 $this->moduleTemplate->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:file_upload.php.pagetitle'));
137
138 $pageContent = '<form action="'
139 . htmlspecialchars(BackendUtility::getModuleUrl('tce_file'))
140 . '" method="post" id="FileUploadController" name="editform" enctype="multipart/form-data">';
141 // Make page header:
142 $pageContent .= '<h1>' . $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:file_upload.php.pagetitle') . '</h1>';
143 $pageContent .= $this->renderUploadForm();
144
145 // Header Buttons
146 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
147
148 // csh button
149 $cshButton = $buttonBar->makeHelpButton()
150 ->setModuleName('xMOD_csh_corebe')
151 ->setFieldName('file_upload');
152 $buttonBar->addButton($cshButton);
153
154 // back button
155 if ($this->returnUrl) {
156 $backButton = $buttonBar->makeLinkButton()
157 ->setHref($this->returnUrl)
158 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
159 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
160 $buttonBar->addButton($backButton);
161 }
162
163 $pageContent .= '</form>';
164 $this->content .= '<div>' . $pageContent . '</div>';
165 $this->moduleTemplate->setContent($this->content);
166 }
167
168 /**
169 * This function renders the upload form
170 *
171 * @return string The HTML form as a string, ready for outputting
172 */
173 public function renderUploadForm()
174 {
175 // Make checkbox for "overwrite"
176 $content = '
177 <div id="c-override">
178 <p><label for="overwriteExistingFiles"><input type="checkbox" class="checkbox" name="overwriteExistingFiles" id="overwriteExistingFiles" value="replace" /> ' . htmlspecialchars($this->getLanguageService()->getLL('overwriteExistingFiles')) . '</label></p>
179 <p>&nbsp;</p>
180 <p>' . htmlspecialchars($this->getLanguageService()->getLL('uploadMultipleFilesInfo')) . '</p>
181 </div>
182 ';
183 // Produce the number of upload-fields needed:
184 $content .= '
185 <div id="c-upload">
186 ';
187 // Adding 'size="50" ' for the sake of Mozilla!
188 $content .= '
189 <input type="file" multiple="multiple" name="upload_1[]" />
190 <input type="hidden" name="data[upload][1][target]" value="' . htmlspecialchars($this->folderObject->getCombinedIdentifier()) . '" />
191 <input type="hidden" name="data[upload][1][data]" value="1" /><br />
192 ';
193 $content .= '
194 </div>
195 ';
196 // Submit button:
197 $content .= '
198 <div id="c-submit">
199 <input type="hidden" name="data[upload][1][redirect]" value="' . $this->returnUrl . '" /><br />
200 <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')) . '" />
201 </div>
202 ';
203 return $content;
204 }
205
206 /**
207 * Processes the request, currently everything is handled and put together via "main()"
208 *
209 * @param ServerRequestInterface $request the current request
210 * @param ResponseInterface $response
211 * @return ResponseInterface the response with the content
212 */
213 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
214 {
215 $this->main();
216 $response->getBody()->write($this->moduleTemplate->renderContent());
217
218 return $response;
219 }
220
221 /**
222 * Returns LanguageService
223 *
224 * @return \TYPO3\CMS\Core\Localization\LanguageService
225 */
226 protected function getLanguageService()
227 {
228 return $GLOBALS['LANG'];
229 }
230 }