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