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