[TASK] BACK_PATH DocumentTemplate
[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->form = '<form action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" method="post" name="editform" enctype="multipart/form-data">';
111 }
112
113 /**
114 * Main function, rendering the upload file form fields
115 *
116 * @return void
117 */
118 public function main() {
119 // Make page header:
120 $this->content = $this->doc->startPage($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_upload.php.pagetitle'));
121 $form = $this->renderUploadForm();
122 $pageContent = $this->doc->header($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_upload.php.pagetitle')) . $this->doc->section('', $form);
123 // Header Buttons
124 $docHeaderButtons = array(
125 'csh' => BackendUtility::cshItem('xMOD_csh_corebe', 'file_upload'),
126 'back' => ''
127 );
128 $markerArray = array(
129 'CSH' => $docHeaderButtons['csh'],
130 'FUNC_MENU' => '',
131 'CONTENT' => $pageContent,
132 'PATH' => $this->title
133 );
134 // Back
135 if ($this->returnUrl) {
136 $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>';
137 }
138 $this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
139 $this->content .= $this->doc->endPage();
140 $this->content = $this->doc->insertStylesAndJS($this->content);
141 }
142
143 /**
144 * This function renders the upload form
145 *
146 * @return string The HTML form as a string, ready for outputting
147 */
148 public function renderUploadForm() {
149 // Make checkbox for "overwrite"
150 $content = '
151 <div id="c-override">
152 <p><label for="overwriteExistingFiles"><input type="checkbox" class="checkbox" name="overwriteExistingFiles" id="overwriteExistingFiles" value="replace" /> ' . $this->getLanguageService()->getLL('overwriteExistingFiles', 1) . '</label></p>
153 <p>&nbsp;</p>
154 <p>' . $this->getLanguageService()->getLL('uploadMultipleFilesInfo', TRUE) . '</p>
155 </div>
156 ';
157 // Produce the number of upload-fields needed:
158 $content .= '
159 <div id="c-upload">
160 ';
161 // Adding 'size="50" ' for the sake of Mozilla!
162 $content .= '
163 <input type="file" multiple="true" name="upload_1[]" />
164 <input type="hidden" name="file[upload][1][target]" value="' . htmlspecialchars($this->folderObject->getCombinedIdentifier()) . '" />
165 <input type="hidden" name="file[upload][1][data]" value="1" /><br />
166 ';
167 $content .= '
168 </div>
169 ';
170 // Submit button:
171 $content .= '
172 <div id="c-submit">
173 <input type="hidden" name="redirect" value="' . $this->returnUrl . '" /><br />
174 ' . \TYPO3\CMS\Backend\Form\FormEngine::getHiddenTokenField('tceAction') . '
175 <input class="btn btn-default" type="submit" value="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_upload.php.submit', TRUE) . '" />
176 </div>
177 ';
178 return $content;
179 }
180
181 /**
182 * Outputting the accumulated content to screen
183 *
184 * @return void
185 */
186 public function printContent() {
187 echo $this->content;
188 }
189
190 /**
191 * Returns LanguageService
192 *
193 * @return \TYPO3\CMS\Lang\LanguageService
194 */
195 protected function getLanguageService() {
196 return $GLOBALS['LANG'];
197 }
198
199 }