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