[CLEANUP] Replace count with empty in EXT:backend
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / FileController.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\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19
20 /**
21 * Gateway for TCE (TYPO3 Core Engine) file-handling through POST forms.
22 * This script serves as the file administration part of the TYPO3 Core Engine.
23 * Basically it includes two libraries which are used to manipulate files on the server.
24 * Before TYPO3 4.3, it was located in typo3/tce_file.php and redirected back to a
25 * $redirectURL. Since 4.3 this class is also used for accessing via AJAX
26 *
27 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
28 */
29 class FileController {
30
31 /**
32 * Array of file-operations.
33 *
34 * @var array
35 */
36 protected $file;
37
38 /**
39 * Clipboard operations array
40 *
41 * @var array
42 */
43 protected $CB;
44
45 /**
46 * If existing files should be overridden.
47 *
48 * @var bool
49 */
50 protected $overwriteExistingFiles;
51
52 /**
53 * VeriCode - a hash of server specific value and other things which
54 * identifies if a submission is OK. (see $GLOBALS['BE_USER']->veriCode())
55 *
56 * @var string
57 */
58 protected $vC;
59
60 /**
61 * The page where the user should be redirected after everything is done
62 *
63 * @var string
64 */
65 protected $redirect;
66
67 /**
68 * Internal, dynamic:
69 * File processor object
70 *
71 * @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility
72 */
73 protected $fileProcessor;
74
75 /**
76 * The result array from the file processor
77 *
78 * @var array
79 */
80 protected $fileData;
81
82 /**
83 * Constructor
84 */
85 public function __construct() {
86 $GLOBALS['SOBE'] = $this;
87 $this->init();
88 }
89
90 /**
91 * Registering incoming data
92 *
93 * @return void
94 */
95 protected function init() {
96 // Set the GPvars from outside
97 $this->file = GeneralUtility::_GP('file');
98 $this->CB = GeneralUtility::_GP('CB');
99 $this->overwriteExistingFiles = GeneralUtility::_GP('overwriteExistingFiles');
100 $this->vC = GeneralUtility::_GP('vC');
101 $this->redirect = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('redirect'));
102 $this->initClipboard();
103 $this->fileProcessor = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class);
104 }
105
106 /**
107 * Initialize the Clipboard. This will fetch the data about files to paste/delete if such an action has been sent.
108 *
109 * @return void
110 */
111 public function initClipboard() {
112 if (is_array($this->CB)) {
113 $clipObj = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Clipboard\Clipboard::class);
114 $clipObj->initializeClipboard();
115 if ($this->CB['paste']) {
116 $clipObj->setCurrentPad($this->CB['pad']);
117 $this->file = $clipObj->makePasteCmdArray_file($this->CB['paste'], $this->file);
118 }
119 if ($this->CB['delete']) {
120 $clipObj->setCurrentPad($this->CB['pad']);
121 $this->file = $clipObj->makeDeleteCmdArray_file($this->file);
122 }
123 }
124 }
125
126 /**
127 * Performing the file admin action:
128 * Initializes the objects, setting permissions, sending data to object.
129 *
130 * @return void
131 */
132 public function main() {
133 // Initializing:
134 $this->fileProcessor->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
135 $this->fileProcessor->setActionPermissions();
136 $this->fileProcessor->dontCheckForUnique = $this->overwriteExistingFiles ? 1 : 0;
137 // Checking referrer / executing:
138 $refInfo = parse_url(GeneralUtility::getIndpEnv('HTTP_REFERER'));
139 $httpHost = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
140 if ($httpHost != $refInfo['host'] && $this->vC != $this->getBackendUser()->veriCode() && !$GLOBALS['TYPO3_CONF_VARS']['SYS']['doNotCheckReferer'] && $GLOBALS['CLIENT']['BROWSER'] != 'flash') {
141 $this->fileProcessor->writeLog(0, 2, 1, 'Referrer host "%s" and server host "%s" did not match!', array($refInfo['host'], $httpHost));
142 } else {
143 $this->fileProcessor->start($this->file);
144 $this->fileData = $this->fileProcessor->processData();
145 }
146 }
147
148 /**
149 * Redirecting the user after the processing has been done.
150 * Might also display error messages directly, if any.
151 *
152 * @return void
153 */
154 public function finish() {
155 // Push errors to flash message queue, if there are any
156 $this->fileProcessor->pushErrorMessagesToFlashMessageQueue();
157 BackendUtility::setUpdateSignal('updateFolderTree');
158 if ($this->redirect) {
159 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($this->redirect);
160 }
161 }
162
163 /**
164 * Handles the actual process from within the ajaxExec function
165 * therefore, it does exactly the same as the real typo3/tce_file.php
166 * but without calling the "finish" method, thus makes it simpler to deal with the
167 * actual return value
168 *
169 * @param array $params Always empty.
170 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj The AjaxRequestHandler object used to return content and set content types
171 * @return void
172 */
173 public function processAjaxRequest(array $params, \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj) {
174 $this->init();
175 $this->main();
176 $errors = $this->fileProcessor->getErrorMessages();
177 if (!empty($errors)) {
178 $ajaxObj->setError(implode(',', $errors));
179 } else {
180 $flatResult = array();
181 foreach ($this->fileData as $action => $results) {
182 foreach ($results as $result) {
183 if (is_array($result)) {
184 foreach ($result as $subResult) {
185 $flatResult[$action][] = $this->flattenResultDataValue($subResult);
186 }
187 } else {
188 $flatResult[$action][] = $this->flattenResultDataValue($result);
189 }
190 }
191 }
192 $ajaxObj->addContent('result', $flatResult);
193 if ($this->redirect) {
194 $ajaxObj->addContent('redirect', $this->redirect);
195 }
196 $ajaxObj->setContentFormat('json');
197 }
198 }
199
200 /**
201 * Flatten result value from FileProcessor
202 *
203 * The value can be a File, Folder or boolean
204 *
205 * @param bool|\TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder $result
206 * @return bool|string|array
207 */
208 protected function flattenResultDataValue($result) {
209 if ($result instanceof \TYPO3\CMS\Core\Resource\File) {
210 $result = array_merge(
211 $result->toArray(),
212 array (
213 'date' => BackendUtility::date($result->getModificationTime()),
214 'iconClasses' => \TYPO3\CMS\Backend\Utility\IconUtility::mapFileExtensionToSpriteIconClass($result->getExtension()),
215 )
216 );
217 } elseif ($result instanceof \TYPO3\CMS\Core\Resource\Folder) {
218 $result = $result->getIdentifier();
219 }
220
221 return $result;
222 }
223
224 /**
225 * Returns the current BE user.
226 *
227 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
228 */
229 protected function getBackendUser() {
230 return $GLOBALS['BE_USER'];
231 }
232
233 }