318d211ade730e59abb6768d5bf29852c03b3033
[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 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
24 */
25 class RenameFileController {
26
27 // Internal, static:
28 /**
29 * Document template object
30 *
31 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
32 */
33 public $doc;
34
35 // Name of the filemount
36 /**
37 * @var string
38 */
39 public $title;
40
41 // Internal, static (GPVar):
42 // Set with the target path inputted in &target
43 /**
44 * @var string
45 */
46 public $target;
47
48 /**
49 * The file or folder object that should be renamed
50 *
51 * @var \TYPO3\CMS\Core\Resource\ResourceInterface $fileOrFolderObject
52 */
53 protected $fileOrFolderObject;
54
55 // Return URL of list module.
56 /**
57 * @var string
58 */
59 public $returnUrl;
60
61 // Internal, dynamic:
62 // Accumulating content
63 /**
64 * @var string
65 */
66 public $content;
67
68 /**
69 * Constructor
70 */
71 public function __construct() {
72 $GLOBALS['SOBE'] = $this;
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 // Cleaning and checking target
88 if ($this->target) {
89 $this->fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->target);
90 }
91 if (!$this->fileOrFolderObject) {
92 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', TRUE);
93 $message = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', TRUE);
94 throw new \RuntimeException($title . ': ' . $message, 1294586844);
95 }
96 if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
97 throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889840);
98 }
99
100 // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
101 // so the redirect will NOT end in a error message
102 // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
103 // rename the folder
104 if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
105 $parsedUrl = parse_url($this->returnUrl);
106 $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
107 if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
108 $this->returnUrl = str_replace(urlencode($queryParts['id']), urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()), $this->returnUrl);
109 }
110 }
111 // Setting icon and title
112 $icon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('apps-filetree-root');
113 $this->title = $icon . htmlspecialchars($this->fileOrFolderObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->fileOrFolderObject->getIdentifier());
114 // Setting template object
115 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
116 $this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/file_rename.html');
117 $this->doc->backPath = $GLOBALS['BACK_PATH'];
118 $this->doc->JScode = $this->doc->wrapScriptTags('
119 function backToList() { //
120 top.goToModule("file_list");
121 }
122 ');
123 }
124
125 /**
126 * Main function, rendering the content of the rename form
127 *
128 * @return void
129 */
130 public function main() {
131 // Make page header:
132 $this->content = $this->doc->startPage($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.pagetitle'));
133 $pageContent = $this->doc->header($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.pagetitle'));
134 $pageContent .= $this->doc->spacer(5);
135 $pageContent .= $this->doc->divider(5);
136 if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
137 $fileIdentifier = $this->fileOrFolderObject->getCombinedIdentifier();
138 } else {
139 $fileIdentifier = $this->fileOrFolderObject->getUid();
140 }
141 $code = '<form action="tce_file.php" method="post" name="editform">';
142 // Making the formfields for renaming:
143 $code .= '
144
145 <div id="c-rename">
146 <input type="text" name="file[rename][0][target]" value="' . htmlspecialchars($this->fileOrFolderObject->getName()) . '"' . $GLOBALS['TBE_TEMPLATE']->formWidth(40) . ' />
147 <input type="hidden" name="file[rename][0][data]" value="' . htmlspecialchars($fileIdentifier) . '" />
148 </div>
149 ';
150 // Making submit button:
151 $code .= '
152 <div id="c-submit">
153 <input type="submit" value="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.submit', TRUE) . '" />
154 <input type="submit" value="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.cancel', TRUE) . '" onclick="backToList(); return false;" />
155 <input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
156 ' . \TYPO3\CMS\Backend\Form\FormEngine::getHiddenTokenField('tceAction') . '
157 </div>
158 ';
159 $code .= '</form>';
160 // Add the HTML as a section:
161 $pageContent .= $code;
162 $docHeaderButtons = array(
163 'back' => ''
164 );
165 $docHeaderButtons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_rename');
166 // Back
167 if ($this->returnUrl) {
168 $docHeaderButtons['back'] = '<a href="' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::linkThisUrl($this->returnUrl)) . '" class="typo3-goBack" title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack', TRUE) . '">' . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-view-go-back') . '</a>';
169 }
170 // Add the HTML as a section:
171 $markerArray = array(
172 'CSH' => $docHeaderButtons['csh'],
173 'FUNC_MENU' => BackendUtility::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
174 'CONTENT' => $pageContent,
175 'PATH' => $this->title
176 );
177 $this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
178 $this->content .= $this->doc->endPage();
179 $this->content = $this->doc->insertStylesAndJS($this->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 }