‪TYPO3CMS  10.4
ImportController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
38 
45 {
51  protected ‪$moduleName = 'tx_impexp_import';
52 
56  protected ‪$uploadedFiles = [];
57 
61  protected ‪$import;
62 
66  protected ‪$fileProcessor;
67 
75  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
76  {
77  $this->lang->includeLLFile('EXT:impexp/Resources/Private/Language/locallang.xlf');
78 
79  $this->pageinfo = ‪BackendUtility::readPageAccess($this->id, $this->perms_clause);
80  if (is_array($this->pageinfo)) {
81  $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($this->pageinfo);
82  }
83  // Setting up the context sensitive menu:
84  $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
85  $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Impexp/ImportExport');
86  $this->moduleTemplate->addJavaScriptCode(
87  'ImpexpInLineJS',
88  'if (top.fsMod) top.fsMod.recentIds["web"] = ' . (int)$this->id . ';'
89  );
90 
91  // Input data grabbed:
92  $inData = $request->getParsedBody()['tx_impexp'] ?? $request->getQueryParams()['tx_impexp'] ?? [];
93  if ($request->getMethod() === 'POST' && empty($request->getParsedBody())) {
94  // This happens if the post request was larger than allowed on the server
95  // We set the import action as default and output a user information
96  $flashMessage = GeneralUtility::makeInstance(
97  FlashMessage::class,
98  $this->lang->getLL('importdata_upload_nodata'),
99  $this->lang->getLL('importdata_upload_error'),
101  );
102  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
103  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
104  $defaultFlashMessageQueue->enqueue($flashMessage);
105  }
106  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
107  $this->standaloneView->assign('moduleUrl', (string)$uriBuilder->buildUriFromRoute($this->moduleName));
108  $this->standaloneView->assign('id', $this->id);
109  $this->standaloneView->assign('inData', $inData);
110 
111  if ($this->‪getBackendUser()->isImportEnabled() === false) {
112  throw new \RuntimeException(
113  'Import module is disabled for non admin users and '
114  . 'userTsConfig options.impexp.enableImportForNonAdminUser is not enabled.',
115  1464435459
116  );
117  }
118  $this->shortcutName = $this->lang->getLL('title_import');
119  if (GeneralUtility::_POST('_upload')) {
120  $this->‪checkUpload();
121  }
122  // Finally: If upload went well, set the new file as the import file:
123  if (!empty($this->uploadedFiles[0])) {
124  // Only allowed extensions....
125  $extension = $this->uploadedFiles[0]->getExtension();
126  if ($extension === 't3d' || $extension === 'xml') {
127  $inData['file'] = $this->uploadedFiles[0]->getCombinedIdentifier();
128  }
129  }
130  // Call import interface:
131  $this->‪importData($inData);
132  $this->standaloneView->setTemplate('Import.html');
133 
134  // Setting up the buttons and markers for docheader
135  $this->‪getButtons();
136 
137  $this->moduleTemplate->setContent($this->standaloneView->render());
138  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
139  }
140 
149  protected function ‪importData(array $inData): void
150  {
151  $access = is_array($this->pageinfo);
152  $beUser = $this->‪getBackendUser();
153  if ($this->id && $access || $beUser->isAdmin() && !$this->id) {
154  if ($beUser->isAdmin() && !$this->id) {
155  $this->pageinfo = ['title' => '[root-level]', 'uid' => 0, 'pid' => 0];
156  }
157  if ($inData['new_import']) {
158  unset($inData['import_mode']);
159  }
161  ‪$import = GeneralUtility::makeInstance(Import::class);
162  ‪$import->‪init();
163  ‪$import->update = $inData['do_update'];
164  ‪$import->import_mode = $inData['import_mode'];
165  ‪$import->enableLogging = $inData['enableLogging'];
166  ‪$import->global_ignore_pid = $inData['global_ignore_pid'];
167  ‪$import->force_all_UIDS = $inData['force_all_UIDS'];
168  ‪$import->showDiff = !$inData['notShowDiff'];
169  ‪$import->softrefInputValues = $inData['softrefInputValues'];
170 
171  // OUTPUT creation:
172 
173  // Make input selector:
174  // must have trailing slash.
175  $path = $this->‪getDefaultImportExportFolder();
176  $exportFiles = $this->‪getExportFiles();
177 
178  $this->shortcutName .= ' (' . htmlspecialchars($this->pageinfo['title']) . ')';
179 
180  // Configuration
181  $selectOptions = [''];
182  foreach ($exportFiles as $file) {
183  $selectOptions[$file->getCombinedIdentifier()] = $file->getPublicUrl();
184  }
185 
186  $this->standaloneView->assign('import', ‪$import);
187  $this->standaloneView->assign('inData', $inData);
188  $this->standaloneView->assign('fileSelectOptions', $selectOptions);
189 
190  if ($path) {
191  $this->standaloneView->assign('importPath', sprintf($this->lang->getLL('importdata_fromPathS'), $path->getCombinedIdentifier()));
192  } else {
193  $this->standaloneView->assign('importPath', $this->lang->getLL('importdata_no_default_upload_folder'));
194  }
195  $this->standaloneView->assign('isAdmin', $beUser->isAdmin());
196 
197  // Upload file:
198  $tempFolder = $this->‪getDefaultImportExportFolder();
199  if ($tempFolder) {
200  $this->standaloneView->assign('tempFolder', $tempFolder->getCombinedIdentifier());
201  $this->standaloneView->assign('hasTempUploadFolder', true);
202  if (GeneralUtility::_POST('_upload')) {
203  $this->standaloneView->assign('submitted', GeneralUtility::_POST('_upload'));
204  $this->standaloneView->assign('noFileUploaded', $this->fileProcessor->internalUploadMap[1]);
205  if ($this->uploadedFiles[0]) {
206  $this->standaloneView->assign('uploadedFile', $this->uploadedFiles[0]->getName());
207  }
208  }
209  }
210 
211  // Perform import or preview depending:
212  if (isset($inData['file'])) {
213  $inFile = $this->‪getFile($inData['file']);
214  if ($inFile !== null && $inFile->exists()) {
215  $this->standaloneView->assign('metaDataInFileExists', true);
216  $importInhibitedMessages = [];
217  if (‪$import->‪loadFile($inFile->getForLocalProcessing(false), 1)) {
218  $importInhibitedMessages = ‪$import->‪checkImportPrerequisites();
219  if ($inData['import_file']) {
220  if (empty($importInhibitedMessages)) {
221  ‪$import->‪importData($this->id);
222  ‪BackendUtility::setUpdateSignal('updatePageTree');
223  }
224  }
225  ‪$import->display_import_pid_record = ‪$this->pageinfo;
226  $this->standaloneView->assign('contentOverview', ‪$import->‪displayContentOverview());
227  }
228  // Compile messages which are inhibiting a proper import and add them to output.
229  if (!empty($importInhibitedMessages)) {
230  $flashMessageQueue = GeneralUtility::makeInstance(FlashMessageService::class)->getMessageQueueByIdentifier('impexp.errors');
231  foreach ($importInhibitedMessages as $message) {
232  $flashMessageQueue->addMessage(GeneralUtility::makeInstance(
233  FlashMessage::class,
234  $message,
235  '',
237  ));
238  }
239  }
240  }
241  }
242 
243  $this->standaloneView->assign('errors', ‪$import->errorLog);
244  }
245  }
246 
247  protected function ‪getButtons(): void
248  {
249  parent::getButtons();
250 
251  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
252  if ($this->id && is_array($this->pageinfo) || $this->‪getBackendUser()->isAdmin() && !$this->id) {
253  if (is_array($this->pageinfo) && $this->pageinfo['uid']) {
254  // View
256  $this->pageinfo['uid'],
257  '',
258  ‪BackendUtility::BEgetRootLine($this->pageinfo['uid'])
259  );
260  $viewButton = $buttonBar->makeLinkButton()
261  ->setTitle($this->lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
262  ->setHref('#')
263  ->setIcon($this->iconFactory->getIcon('actions-view-page', ‪Icon::SIZE_SMALL))
264  ->setOnClick($onClick);
265  $buttonBar->addButton($viewButton);
266  }
267  }
268  }
269 
275  protected function ‪checkUpload(): void
276  {
277  $file = GeneralUtility::_GP('file');
278  // Initializing:
279  $this->fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
280  $this->fileProcessor->setActionPermissions();
281  $conflictMode = empty(GeneralUtility::_GP('overwriteExistingFiles')) ? ‪DuplicationBehavior::__default : ‪DuplicationBehavior::REPLACE;
282  $this->fileProcessor->setExistingFilesConflictMode(‪DuplicationBehavior::cast($conflictMode));
283  $this->fileProcessor->start($file);
284  $result = $this->fileProcessor->processData();
285  if (!empty($result['upload'])) {
286  foreach ($result['upload'] as ‪$uploadedFiles) {
287  $this->uploadedFiles += ‪$uploadedFiles;
288  }
289  }
290  }
291 
298  protected function ‪getExportFiles(): array
299  {
300  $exportFiles = [];
301 
302  $folder = $this->‪getDefaultImportExportFolder();
303  if ($folder !== null) {
304 
306  $filter = GeneralUtility::makeInstance(FileExtensionFilter::class);
307  $filter->setAllowedFileExtensions(['t3d', 'xml']);
308  $folder->getStorage()->addFileAndFolderNameFilter([$filter, 'filterFileList']);
309 
310  $exportFiles = $folder->getFiles();
311  }
312 
313  return $exportFiles;
314  }
315 
322  protected function ‪getFile(string $combinedIdentifier): ?File
323  {
324  try {
325  $file = GeneralUtility::makeInstance(ResourceFactory::class)->getFileObjectFromCombinedIdentifier($combinedIdentifier);
326  } catch (\Exception $exception) {
327  $file = null;
328  }
329 
330  return $file;
331  }
332 
336  protected function ‪getBackendUser(): ‪BackendUserAuthentication
337  {
338  return ‪$GLOBALS['BE_USER'];
339  }
340 }
‪TYPO3\CMS\Impexp\Controller\ImportController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: ImportController.php:71
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Impexp\Controller\ImportController\$uploadedFiles
‪array File[] $uploadedFiles
Definition: ImportController.php:54
‪TYPO3\CMS\Core\Resource\DuplicationBehavior
Definition: DuplicationBehavior.php:24
‪TYPO3\CMS\Impexp\Controller\ImportController\getExportFiles
‪File[] getExportFiles()
Definition: ImportController.php:294
‪TYPO3\CMS\Impexp\Controller
Definition: ExportController.php:18
‪TYPO3\CMS\Impexp\Controller\ImportController\getButtons
‪getButtons()
Definition: ImportController.php:243
‪TYPO3\CMS\Impexp\Import\checkImportPrerequisites
‪array checkImportPrerequisites()
Definition: Import.php:257
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Utility\BackendUtility\setUpdateSignal
‪static setUpdateSignal($set='', $params='')
Definition: BackendUtility.php:2798
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Impexp\Controller\ImportController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: ImportController.php:332
‪TYPO3\CMS\Impexp\Controller\ImportController\getFile
‪File null getFile(string $combinedIdentifier)
Definition: ImportController.php:318
‪TYPO3\CMS\Impexp\Controller\ImportController
Definition: ImportController.php:45
‪TYPO3\CMS\Backend\Utility\BackendUtility\BEgetRootLine
‪static array BEgetRootLine($uid, $clause='', $workspaceOL=false, array $additionalFields=[])
Definition: BackendUtility.php:343
‪TYPO3\CMS\Impexp\Controller\ImportController\$moduleName
‪string $moduleName
Definition: ImportController.php:50
‪TYPO3\CMS\Impexp\Import\init
‪init()
Definition: Import.php:101
‪TYPO3\CMS\Impexp\Controller\ImportExportController\$pageinfo
‪array $pageinfo
Definition: ImportExportController.php:53
‪TYPO3\CMS\Impexp\ImportExport\displayContentOverview
‪array displayContentOverview()
Definition: ImportExport.php:265
‪TYPO3\CMS\Impexp\Import\loadFile
‪bool loadFile($filename, $all=false)
Definition: Import.php:1600
‪TYPO3\CMS\Impexp\Controller\ImportController\checkUpload
‪checkUpload()
Definition: ImportController.php:271
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:186
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility
Definition: ExtendedFileUtility.php:70
‪TYPO3\CMS\Impexp\Controller\ImportExportController
Definition: ImportExportController.php:42
‪TYPO3\CMS\Impexp\Controller\ImportController\$fileProcessor
‪ExtendedFileUtility $fileProcessor
Definition: ImportController.php:62
‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
Definition: RouteNotFoundException.php:22
‪TYPO3\CMS\Impexp\Import
Definition: Import.php:42
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\__default
‪const __default
Definition: DuplicationBehavior.php:25
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter
Definition: FileExtensionFilter.php:28
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Backend\Utility\BackendUtility\readPageAccess
‪static array false readPageAccess($id, $perms_clause)
Definition: BackendUtility.php:597
‪TYPO3\CMS\Backend\Utility\BackendUtility\viewOnClick
‪static string viewOnClick( $pageUid, $backPath='', $rootLine=null, $anchorSection='', $alternativeUrl='', $additionalGetVars='', $switchFocus=true)
Definition: BackendUtility.php:2359
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪TYPO3\CMS\Impexp\Import\importData
‪importData($pid)
Definition: Import.php:144
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Impexp\Controller\ImportExportController\getDefaultImportExportFolder
‪Folder null getDefaultImportExportFolder()
Definition: ImportExportController.php:200
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\REPLACE
‪const REPLACE
Definition: DuplicationBehavior.php:39
‪TYPO3\CMS\Impexp\Controller\ImportController\importData
‪importData(array $inData)
Definition: ImportController.php:145
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:31
‪TYPO3\CMS\Impexp\Controller\ImportController\$import
‪Import $import
Definition: ImportController.php:58
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26