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