RenameFileController.php 7.07 KB
Newer Older
1
2
3
<?php
namespace TYPO3\CMS\Backend\Controller\File;

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
16

Nicole Cordes's avatar
Nicole Cordes committed
17
18
19
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

20
21
22
23
24
25
26
27
28
/**
 * Script Class for the rename-file form.
 */
class RenameFileController {

	// Internal, static:
	/**
	 * Document template object
	 *
29
	 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
30
31
32
33
34
	 */
	public $doc;

	// Name of the filemount
	/**
35
	 * @var string
36
37
38
39
40
41
	 */
	public $title;

	// Internal, static (GPVar):
	// Set with the target path inputted in &target
	/**
42
	 * @var string
43
44
45
46
47
48
	 */
	public $target;

	/**
	 * The file or folder object that should be renamed
	 *
49
	 * @var \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder $fileOrFolderObject
50
51
52
53
54
	 */
	protected $fileOrFolderObject;

	// Return URL of list module.
	/**
55
	 * @var string
56
57
58
59
60
61
	 */
	public $returnUrl;

	// Internal, dynamic:
	// Accumulating content
	/**
62
	 * @var string
63
64
65
66
	 */
	public $content;

	/**
67
68
69
70
71
72
73
74
75
76
77
	 * Constructor
	 */
	public function __construct() {
		$GLOBALS['SOBE'] = $this;
		$GLOBALS['BACK_PATH'] = '';

		$this->init();
	}

	/**
	 * Initialize
78
	 *
79
	 * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
80
	 */
81
	protected function init() {
82
		// Initialize GPvars:
Nicole Cordes's avatar
Nicole Cordes committed
83
84
		$this->target = GeneralUtility::_GP('target');
		$this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
85
86
87
88
89
		// Cleaning and checking target
		if ($this->target) {
			$this->fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->target);
		}
		if (!$this->fileOrFolderObject) {
90
91
			$title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', TRUE);
			$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
101
102
103
		// this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
		// rename the folder
		if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
			$parsedUrl = parse_url($this->returnUrl);
Nicole Cordes's avatar
Nicole Cordes committed
104
			$queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
105
106
107
108
109
110
			if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
				$this->returnUrl = str_replace(urlencode($queryParts['id']), urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()), $this->returnUrl);
			}
		}
		// Setting icon and title
		$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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
		$this->doc->backPath = $GLOBALS['BACK_PATH'];
		$this->doc->JScode = $this->doc->wrapScriptTags('
			function backToList() {	//
				top.goToModule("file_list");
			}
		');
	}

	/**
	 * Main function, rendering the content of the rename form
	 *
	 * @return void
	 */
	public function main() {
		// Make page header:
130
131
		$this->content = $this->doc->startPage($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.pagetitle'));
		$pageContent = $this->doc->header($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.pagetitle'));
132
133
134
135
136
		if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
			$fileIdentifier = $this->fileOrFolderObject->getCombinedIdentifier();
		} else {
			$fileIdentifier = $this->fileOrFolderObject->getUid();
		}
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
145
146
			</div>
		';
		// Making submit button:
147
148
		$pageContent .= '
			<div class="form-group">
149
150
				<input class="btn btn-primary" type="submit" value="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.submit', TRUE) . '" />
				<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
154
			</div>
		';
155
		$pageContent .= '</form>';
156
157
158
		$docHeaderButtons = array(
			'back' => ''
		);
Benni Mack's avatar
Benni Mack committed
159
		$docHeaderButtons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_rename');
160
161
		// Back
		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
165
166
		// Add the HTML as a section:
		$markerArray = array(
			'CSH' => $docHeaderButtons['csh'],
167
			'FUNC_MENU' => '',
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
			'CONTENT' => $pageContent,
			'PATH' => $this->title
		);
		$this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
		$this->content .= $this->doc->endPage();
		$this->content = $this->doc->insertStylesAndJS($this->content);
	}

	/**
	 * Outputting the accumulated content to screen
	 *
	 * @return void
	 */
	public function printContent() {
		echo $this->content;
	}

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
	/**
	 * Returns LanguageService
	 *
	 * @return \TYPO3\CMS\Lang\LanguageService
	 */
	protected function getLanguageService() {
		return $GLOBALS['LANG'];
	}

	/**
	 * Returns an instance of DocumentTemplate
	 *
	 * @return \TYPO3\CMS\Backend\Template\DocumentTemplate
	 */
	protected function getDocumentTemplate() {
		return $GLOBALS['TBE_TEMPLATE'];
	}

203
}