[TASK] BACK_PATH BE controller
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / ReplaceFileController.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\Template\DocumentTemplate;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Backend\Utility\IconUtility;
20 use TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException;
21 use TYPO3\CMS\Core\Resource\Folder;
22 use TYPO3\CMS\Core\Resource\ResourceFactory;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Lang\LanguageService;
25
26 /**
27 * Script Class for the rename-file form
28 */
29 class ReplaceFileController {
30
31 /**
32 * Document template object
33 *
34 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
35 */
36 public $doc;
37
38 /**
39 * Name of the filemount
40 *
41 * @var string
42 */
43 public $title;
44
45 /**
46 * sys_file uid
47 *
48 * @var int
49 */
50 public $uid;
51
52 /**
53 * The file or folder object that should be renamed
54 *
55 * @var \TYPO3\CMS\Core\Resource\ResourceInterface $fileOrFolderObject
56 */
57 protected $fileOrFolderObject;
58
59 /**
60 * Return URL of list module.
61 *
62 * @var string
63 */
64 public $returnUrl;
65
66 /**
67 * Accumulating content
68 *
69 * @var string
70 */
71 public $content;
72
73 /**
74 * Constructor
75 */
76 public function __construct() {
77 $GLOBALS['SOBE'] = $this;
78 $this->init();
79 }
80
81 /**
82 * Init
83 *
84 * @return void
85 * @throws \RuntimeException
86 * @throws InsufficientFileAccessPermissionsException
87 */
88 protected function init() {
89 // Initialize GPvars:
90 $this->uid = (int) GeneralUtility::_GP('uid');
91
92 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
93 // Cleaning and checking uid
94 if ($this->uid > 0) {
95 $this->fileOrFolderObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject('file:' . $this->uid);
96 }
97 if (!$this->fileOrFolderObject) {
98 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', TRUE);
99 $message = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', TRUE);
100 throw new \RuntimeException($title . ': ' . $message, 1436895930);
101 }
102 if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
103 throw new InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1436895931);
104 }
105
106 // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
107 // so the redirect will NOT end in an error message
108 // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
109 // rename the folder
110 if ($this->fileOrFolderObject instanceof Folder) {
111 $parsedUrl = parse_url($this->returnUrl);
112 $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
113 if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
114 $this->returnUrl = str_replace(urlencode($queryParts['id']), urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()), $this->returnUrl);
115 }
116 }
117 // Setting icon and title
118 $icon = IconUtility::getSpriteIcon('apps-filetree-root');
119 $this->title = $icon . htmlspecialchars($this->fileOrFolderObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->fileOrFolderObject->getIdentifier());
120 // Setting template object
121 $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
122 $this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/file_replace.html');
123 $this->doc->JScode = $this->doc->wrapScriptTags('
124 function backToList() { //
125 top.goToModule("file_list");
126 }
127 ');
128 }
129
130 /**
131 * Main function, rendering the content of the rename form
132 *
133 * @return void
134 */
135 public function main() {
136 // Make page header:
137 $this->content = $this->doc->startPage($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.pagetitle'));
138 $pageContent = $this->doc->header($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.pagetitle'));
139 $pageContent .= $this->doc->spacer(5);
140 $pageContent .= $this->doc->divider(5);
141
142 $code = '<form action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" role="form" method="post" name="editform" enctype="multipart/form-data">';
143
144 // Making the formfields for renaming:
145 $code .= '
146 <div class="form-group">
147 <input type="checkbox" value="1" id="keepFilename" name="file[replace][1][keepFilename]"> <label for="keepFilename">' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.keepfiletitle') . '</label>
148 </div>
149
150 <div class="form-group">
151 <label for="file_replace">' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.selectfile') . '</label>
152 <div class="input-group col-xs-6">
153 <input type="text" name="fakefile" id="fakefile" class="form-control input-xlarge" readonly>
154 <a class="input-group-addon btn btn-primary" onclick="TYPO3.jQuery(\'#file_replace\').click();">' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.browse') . '</a>
155 </div>
156 <input class="form-control" type="file" id="file_replace" multiple="false" name="replace_1" style="visibility: hidden;" />
157 </div>
158
159 <script>
160 TYPO3.jQuery(\'#file_replace\').change(function(){
161 TYPO3.jQuery(\'#fakefile\').val(TYPO3.jQuery(this).val());
162 });
163 </script>
164
165 <input type="hidden" name="overwriteExistingFiles" value="replace" />
166 <input type="hidden" name="file[replace][1][data]" value="1" />
167 <input type="hidden" name="file[replace][1][uid]" value="' . $this->uid . '" />
168 ';
169 // Making submit button:
170 $code .= '
171 <div class="form-group">
172 <input class="btn btn-primary" type="submit" value="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.submit', TRUE) . '" />
173 <input class="btn btn-danger" type="submit" value="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.cancel', TRUE) . '" onclick="backToList(); return false;" />
174 <input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
175 ' . \TYPO3\CMS\Backend\Form\FormEngine::getHiddenTokenField('tceAction') . '
176 </div>
177 ';
178 $code .= '</form>';
179 // Add the HTML as a section:
180 $pageContent .= $code;
181 $docHeaderButtons = array(
182 'back' => ''
183 );
184 $docHeaderButtons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_rename');
185 // Back
186 if ($this->returnUrl) {
187 $docHeaderButtons['back'] = '<a href="' . htmlspecialchars(GeneralUtility::linkThisUrl($this->returnUrl))
188 . '" class="typo3-goBack" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack', TRUE) . '">'
189 . IconUtility::getSpriteIcon('actions-view-go-back')
190 . '</a>';
191 }
192 // Add the HTML as a section:
193 $markerArray = array(
194 'CSH' => $docHeaderButtons['csh'],
195 'FUNC_MENU' => BackendUtility::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
196 'CONTENT' => $pageContent,
197 'PATH' => $this->title
198 );
199 $this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
200 $this->content .= $this->doc->endPage();
201 $this->content = $this->doc->insertStylesAndJS($this->content);
202 }
203
204 /**
205 * Outputting the accumulated content to screen
206 *
207 * @return void
208 */
209 public function printContent() {
210 echo $this->content;
211 }
212
213 /**
214 * @return LanguageService
215 */
216 protected function getLanguageService() {
217 return $GLOBALS['LANG'];
218 }
219 }