5baa38874dc8e91a5b1fcf4a565da1a89de842d1
[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 TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Imaging\Icon;
19 use TYPO3\CMS\Core\Imaging\IconFactory;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23
24 /**
25 * Script Class for display up to 10 upload fields
26 */
27 class FileUploadController
28 {
29 /**
30 * Document template object
31 *
32 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
33 */
34 public $doc;
35
36 /**
37 * Name of the filemount
38 *
39 * @var string
40 */
41 public $title;
42
43 /**
44 * Set with the target path inputted in &target
45 *
46 * @var string
47 */
48 public $target;
49
50 /**
51 * Return URL of list module.
52 *
53 * @var string
54 */
55 public $returnUrl;
56
57 /**
58 * Accumulating content
59 *
60 * @var string
61 */
62 public $content;
63
64 /**
65 * The folder object which is the target directory for the upload
66 *
67 * @var \TYPO3\CMS\Core\Resource\Folder $folderObject
68 */
69 protected $folderObject;
70
71 /**
72 * @var IconFactory
73 */
74 protected $iconFactory;
75
76 /**
77 * Constructor
78 */
79 public function __construct()
80 {
81 $GLOBALS['SOBE'] = $this;
82 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
83 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_misc.xlf');
84 $this->init();
85 }
86
87 /**
88 * Initialize
89 *
90 * @return void
91 */
92 protected function init()
93 {
94 // Initialize GPvars:
95 $this->target = GeneralUtility::_GP('target');
96 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
97 if (!$this->returnUrl) {
98 $this->returnUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir . BackendUtility::getModuleUrl('file_list') . '&id=' . rawurlencode($this->target);
99 }
100 // Create the folder object
101 if ($this->target) {
102 $this->folderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->target);
103 }
104 if ($this->folderObject->getStorage()->getUid() === 0) {
105 throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException('You are not allowed to access folders outside your storages', 1375889834);
106 }
107
108 // Cleaning and checking target directory
109 if (!$this->folderObject) {
110 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', true);
111 $message = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', true);
112 throw new \RuntimeException($title . ': ' . $message, 1294586843);
113 }
114 // Setting the title and the icon
115 $icon = $this->iconFactory->getIcon('apps-filetree-root', Icon::SIZE_SMALL)->render();
116 $this->title = $icon . htmlspecialchars($this->folderObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->folderObject->getIdentifier());
117 // Setting template object
118 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
119 $this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/file_upload.html');
120 $this->doc->form = '<form action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" method="post" name="editform" enctype="multipart/form-data">';
121 }
122
123 /**
124 * Main function, rendering the upload file form fields
125 *
126 * @return void
127 */
128 public function main()
129 {
130 // Make page header:
131 $this->content = $this->doc->startPage($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_upload.php.pagetitle'));
132 $form = $this->renderUploadForm();
133 $pageContent = $this->doc->header($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_upload.php.pagetitle')) . $this->doc->section('', $form);
134 // Header Buttons
135 $docHeaderButtons = array(
136 'csh' => BackendUtility::cshItem('xMOD_csh_corebe', 'file_upload'),
137 'back' => ''
138 );
139 $markerArray = array(
140 'CSH' => $docHeaderButtons['csh'],
141 'FUNC_MENU' => '',
142 'CONTENT' => $pageContent,
143 'PATH' => $this->title
144 );
145 // Back
146 if ($this->returnUrl) {
147 $docHeaderButtons['back'] = '<a href="' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::linkThisUrl($this->returnUrl)) . '" class="typo3-goBack" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack', true) . '">' . $this->iconFactory->getIcon('actions-view-go-back', Icon::SIZE_SMALL)->render() . '</a>';
148 }
149 $this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
150 $this->content .= $this->doc->endPage();
151 $this->content = $this->doc->insertStylesAndJS($this->content);
152 }
153
154 /**
155 * This function renders the upload form
156 *
157 * @return string The HTML form as a string, ready for outputting
158 */
159 public function renderUploadForm()
160 {
161 // Make checkbox for "overwrite"
162 $content = '
163 <div id="c-override">
164 <p><label for="overwriteExistingFiles"><input type="checkbox" class="checkbox" name="overwriteExistingFiles" id="overwriteExistingFiles" value="replace" /> ' . $this->getLanguageService()->getLL('overwriteExistingFiles', 1) . '</label></p>
165 <p>&nbsp;</p>
166 <p>' . $this->getLanguageService()->getLL('uploadMultipleFilesInfo', true) . '</p>
167 </div>
168 ';
169 // Produce the number of upload-fields needed:
170 $content .= '
171 <div id="c-upload">
172 ';
173 // Adding 'size="50" ' for the sake of Mozilla!
174 $content .= '
175 <input type="file" multiple="true" name="upload_1[]" />
176 <input type="hidden" name="file[upload][1][target]" value="' . htmlspecialchars($this->folderObject->getCombinedIdentifier()) . '" />
177 <input type="hidden" name="file[upload][1][data]" value="1" /><br />
178 ';
179 $content .= '
180 </div>
181 ';
182 // Submit button:
183 $content .= '
184 <div id="c-submit">
185 <input type="hidden" name="redirect" value="' . $this->returnUrl . '" /><br />
186 <input class="btn btn-default" type="submit" value="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_upload.php.submit', true) . '" />
187 </div>
188 ';
189 return $content;
190 }
191
192 /**
193 * Processes the request, currently everything is handled and put together via "main()"
194 *
195 * @param ServerRequestInterface $request the current request
196 * @param ResponseInterface $response
197 * @return ResponseInterface the response with the content
198 */
199 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
200 {
201 $this->main();
202
203 $response->getBody()->write($this->content);
204 return $response;
205 }
206
207 /**
208 * Outputting the accumulated content to screen
209 *
210 * @return void
211 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use the mainAction() method instead
212 */
213 public function printContent()
214 {
215 GeneralUtility::logDeprecatedFunction();
216 echo $this->content;
217 }
218
219 /**
220 * Returns LanguageService
221 *
222 * @return \TYPO3\CMS\Lang\LanguageService
223 */
224 protected function getLanguageService()
225 {
226 return $GLOBALS['LANG'];
227 }
228 }