ReplaceFileController.php 8.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
namespace TYPO3\CMS\Backend\Controller\File;

/**
 * This file is part of the TYPO3 CMS project.
 *
 * 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.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Backend\Template\DocumentTemplate;
use TYPO3\CMS\Backend\Utility\BackendUtility;
19
20
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
21
22
23
24
25
use TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException;
use TYPO3\CMS\Core\Resource\Folder;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Lang\LanguageService;
26
27
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
28
29
30
31

/**
 * Script Class for the rename-file form
 */
32
class ReplaceFileController {
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

	/**
	 * Document template object
	 *
	 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
	 */
	public $doc;

	/**
	 * Name of the filemount
	 *
	 * @var string
	 */
	public $title;

	/**
	 * sys_file uid
	 *
	 * @var int
	 */
	public $uid;

	/**
	 * The file or folder object that should be renamed
	 *
	 * @var \TYPO3\CMS\Core\Resource\ResourceInterface $fileOrFolderObject
	 */
	protected $fileOrFolderObject;

	/**
	 * Return URL of list module.
	 *
	 * @var string
	 */
	public $returnUrl;

	/**
	 * Accumulating content
	 *
	 * @var string
	 */
	public $content;

	/**
	 * Constructor
	 */
	public function __construct() {
		$GLOBALS['SOBE'] = $this;
		$this->init();
	}

	/**
	 * Init
	 *
	 * @return void
	 * @throws \RuntimeException
	 * @throws InsufficientFileAccessPermissionsException
	 */
	protected function init() {
		// Initialize GPvars:
		$this->uid = (int) GeneralUtility::_GP('uid');

		$this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
		// Cleaning and checking uid
		if ($this->uid > 0) {
			$this->fileOrFolderObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject('file:' . $this->uid);
		}
		if (!$this->fileOrFolderObject) {
			$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);
			throw new \RuntimeException($title . ': ' . $message, 1436895930);
		}
		if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
			throw new InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1436895931);
		}

		// If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
110
		// so the redirect will NOT end in an error message
111
112
113
114
115
116
117
118
119
120
		// 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 Folder) {
			$parsedUrl = parse_url($this->returnUrl);
			$queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
			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
121
122
		/** @var IconFactory $iconFactory */
		$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
123
		$icon = $iconFactory->getIcon('apps-filetree-root', Icon::SIZE_SMALL)->render();
124
125
126
127
128
		$this->title = $icon . htmlspecialchars($this->fileOrFolderObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->fileOrFolderObject->getIdentifier());
		// Setting template object
		$this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
		$this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/file_replace.html');
		$this->doc->JScode = $this->doc->wrapScriptTags('
129
130
			function backToList() {
				top.goToModule("file_FilelistList");
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
			}
		');
	}

	/**
	 * Main function, rendering the content of the rename form
	 *
	 * @return void
	 */
	public function main() {
		// Make page header:
		$this->content = $this->doc->startPage($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.pagetitle'));
		$pageContent = $this->doc->header($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.pagetitle'));
		$pageContent .= $this->doc->spacer(5);
		$pageContent .= $this->doc->divider(5);

147
		$code = '<form action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" role="form" method="post" name="editform" enctype="multipart/form-data">';
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

		// Making the formfields for renaming:
		$code .= '
			<div class="form-group">
				<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>
			</div>

			<div class="form-group">
				<label for="file_replace">' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.selectfile') . '</label>
				<div class="input-group col-xs-6">
					<input type="text" name="fakefile" id="fakefile" class="form-control input-xlarge" readonly>
					<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>
				</div>
				<input class="form-control" type="file" id="file_replace" multiple="false" name="replace_1" style="visibility: hidden;" />
			</div>

			<script>
			TYPO3.jQuery(\'#file_replace\').change(function(){
				TYPO3.jQuery(\'#fakefile\').val(TYPO3.jQuery(this).val());
			});
			</script>

170
			<input type="hidden" name="overwriteExistingFiles" value="replace" />
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
			<input type="hidden" name="file[replace][1][data]" value="1" />
			<input type="hidden" name="file[replace][1][uid]" value="' . $this->uid . '" />
		';
		// Making submit button:
		$code .= '
				<div class="form-group">
					<input class="btn btn-primary" type="submit" value="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.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;" />
					<input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
				</div>
		';
		$code .= '</form>';
		// Add the HTML as a section:
		$pageContent .= $code;
		$docHeaderButtons = array(
				'back' => ''
		);
188
		$docHeaderButtons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_rename');
189
190
		// Back
		if ($this->returnUrl) {
191
			$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
192
193
			$docHeaderButtons['back'] = '<a href="' . htmlspecialchars(GeneralUtility::linkThisUrl($this->returnUrl))
				. '" class="typo3-goBack" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack', TRUE) . '">'
194
				. $iconFactory->getIcon('actions-view-go-back', Icon::SIZE_SMALL)->render()
195
196
197
198
199
200
201
202
203
204
205
206
207
208
				. '</a>';
		}
		// Add the HTML as a section:
		$markerArray = array(
				'CSH' => $docHeaderButtons['csh'],
				'FUNC_MENU' => BackendUtility::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
				'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);
	}

209
210
211
	/**
	 * Processes the request, currently everything is handled and put together via "main()"
	 *
212
213
214
	 * @param ServerRequestInterface $request the current request
	 * @param ResponseInterface $response
	 * @return ResponseInterface the response with the content
215
	 */
216
	public function mainAction(ServerRequestInterface $request, ResponseInterface $response) {
217
		$this->main();
218

219
220
221
222
		$response->getBody()->write($this->content);
		return $response;
	}

223
224
225
226
	/**
	 * Outputting the accumulated content to screen
	 *
	 * @return void
227
	 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use the mainAction() method instead
228
229
	 */
	public function printContent() {
230
		GeneralUtility::logDeprecatedFunction();
231
232
233
234
235
236
237
238
239
		echo $this->content;
	}

	/**
	 * @return LanguageService
	 */
	protected function getLanguageService() {
		return $GLOBALS['LANG'];
	}
240
}