2a7b987603343212dc58ba3e42eae0cbcdcb072f
[Packages/TYPO3.CMS.git] / typo3 / sysext / impexp / Classes / Controller / ImportExportController.php
1 <?php
2 namespace TYPO3\CMS\Impexp\Controller;
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Module\BaseScriptClass;
20 use TYPO3\CMS\Backend\Template\DocumentTemplate;
21 use TYPO3\CMS\Backend\Template\ModuleTemplate;
22 use TYPO3\CMS\Backend\Tree\View\PageTreeView;
23 use TYPO3\CMS\Backend\Utility\BackendUtility;
24 use TYPO3\CMS\Core\Database\ConnectionPool;
25 use TYPO3\CMS\Core\Database\Query\QueryHelper;
26 use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
27 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
28 use TYPO3\CMS\Core\Imaging\Icon;
29 use TYPO3\CMS\Core\Imaging\IconFactory;
30 use TYPO3\CMS\Core\Localization\LanguageService;
31 use TYPO3\CMS\Core\Messaging\FlashMessage;
32 use TYPO3\CMS\Core\Messaging\FlashMessageService;
33 use TYPO3\CMS\Core\Resource\DuplicationBehavior;
34 use TYPO3\CMS\Core\Resource\Exception;
35 use TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter;
36 use TYPO3\CMS\Core\Resource\ResourceFactory;
37 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
38 use TYPO3\CMS\Core\Utility\File\ExtendedFileUtility;
39 use TYPO3\CMS\Core\Utility\GeneralUtility;
40 use TYPO3\CMS\Core\Utility\MathUtility;
41 use TYPO3\CMS\Fluid\View\StandaloneView;
42 use TYPO3\CMS\Impexp\Domain\Repository\PresetRepository;
43 use TYPO3\CMS\Impexp\Export;
44 use TYPO3\CMS\Impexp\Import;
45 use TYPO3\CMS\Impexp\View\ExportPageTreeView;
46
47 /**
48 * Main script class for the Import / Export facility
49 */
50 class ImportExportController extends BaseScriptClass
51 {
52 /**
53 * @var array|\TYPO3\CMS\Core\Resource\File[]
54 */
55 protected $uploadedFiles = [];
56
57 /**
58 * Array containing the current page.
59 *
60 * @var array
61 */
62 public $pageinfo;
63
64 /**
65 * @var Export
66 */
67 protected $export;
68
69 /**
70 * @var Import
71 */
72 protected $import;
73
74 /**
75 * @var ExtendedFileUtility
76 */
77 protected $fileProcessor;
78
79 /**
80 * @var LanguageService
81 */
82 protected $lang = null;
83
84 /**
85 * @var string
86 */
87 protected $treeHTML = '';
88
89 /**
90 * @var IconFactory
91 */
92 protected $iconFactory;
93
94 /**
95 * The name of the module
96 *
97 * @var string
98 */
99 protected $moduleName = 'xMOD_tximpexp';
100
101 /**
102 * ModuleTemplate Container
103 *
104 * @var ModuleTemplate
105 */
106 protected $moduleTemplate;
107
108 /**
109 * The name of the shortcut for this page
110 *
111 * @var string
112 */
113 protected $shortcutName;
114
115 /**
116 * preset repository
117 *
118 * @var PresetRepository
119 */
120 protected $presetRepository;
121
122 /**
123 * @var StandaloneView
124 */
125 protected $standaloneView = null;
126
127 /**
128 * @var bool
129 */
130 protected $excludeDisabledRecords = false;
131
132 /**
133 * Return URL
134 *
135 * @var string
136 */
137 protected $returnUrl;
138
139 /**
140 * Constructor
141 */
142 public function __construct()
143 {
144 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
145 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
146 $this->presetRepository = GeneralUtility::makeInstance(PresetRepository::class);
147
148 $templatePath = ExtensionManagementUtility::extPath('impexp') . 'Resources/Private/';
149
150 /* @var $view StandaloneView */
151 $this->standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
152 $this->standaloneView->setTemplateRootPaths([$templatePath . 'Templates/ImportExport/']);
153 $this->standaloneView->setLayoutRootPaths([$templatePath . 'Layouts/']);
154 $this->standaloneView->setPartialRootPaths([$templatePath . 'Partials/']);
155 $this->standaloneView->getRequest()->setControllerExtensionName('impexp');
156 }
157
158 /**
159 * Initializes the module and defining necessary variables for this module to run.
160 */
161 public function init()
162 {
163 $this->MCONF['name'] = $this->moduleName;
164 parent::init();
165 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
166 $this->lang = $this->getLanguageService();
167 }
168
169 /**
170 * Main module function
171 *
172 * @throws \BadFunctionCallException
173 * @throws \InvalidArgumentException
174 * @throws \RuntimeException
175 */
176 public function main()
177 {
178 $this->lang->includeLLFile('EXT:impexp/Resources/Private/Language/locallang.xlf');
179
180 // Start document template object:
181 // We keep this here, in case somebody relies on the old doc being here
182 $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
183 $this->doc->bodyTagId = 'imp-exp-mod';
184 $this->pageinfo = BackendUtility::readPageAccess($this->id, $this->perms_clause);
185 if (is_array($this->pageinfo)) {
186 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($this->pageinfo);
187 }
188 // Setting up the context sensitive menu:
189 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
190 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Impexp/ImportExport');
191 $this->moduleTemplate->addJavaScriptCode(
192 'ImpexpInLineJS',
193 'if (top.fsMod) top.fsMod.recentIds["web"] = ' . (int)$this->id . ';'
194 );
195
196 // Input data grabbed:
197 $inData = GeneralUtility::_GP('tx_impexp');
198 if ($inData === null) {
199 // This happens if the post request was larger than allowed on the server
200 // We set the import action as default and output a user information
201 $inData = [
202 'action' => 'import',
203 ];
204 $flashMessage = GeneralUtility::makeInstance(
205 FlashMessage::class,
206 $this->getLanguageService()->getLL('importdata_upload_nodata'),
207 $this->getLanguageService()->getLL('importdata_upload_error'),
208 FlashMessage::ERROR
209 );
210 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
211 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
212 $defaultFlashMessageQueue->enqueue($flashMessage);
213 }
214 if (!array_key_exists('excludeDisabled', $inData)) {
215 // flag doesn't exist initially; state is on by default
216 $inData['excludeDisabled'] = 1;
217 }
218 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
219 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
220 $this->standaloneView->assign('moduleUrl', (string)$uriBuilder->buildUriFromRoute('xMOD_tximpexp'));
221 $this->standaloneView->assign('id', $this->id);
222 $this->standaloneView->assign('inData', $inData);
223
224 switch ((string)$inData['action']) {
225 case 'export':
226 $this->shortcutName = $this->lang->getLL('title_export');
227 // Call export interface
228 $this->exportData($inData);
229 $this->standaloneView->setTemplate('Export.html');
230 break;
231 case 'import':
232 $backendUser = $this->getBackendUser();
233 $isEnabledForNonAdmin = $backendUser->getTSConfig('options.impexp.enableImportForNonAdminUser');
234 if (!$backendUser->isAdmin() && empty($isEnabledForNonAdmin['value'])) {
235 throw new \RuntimeException(
236 'Import module is disabled for non admin users and '
237 . 'userTsConfig options.impexp.enableImportForNonAdminUser is not enabled.',
238 1464435459
239 );
240 }
241 $this->shortcutName = $this->lang->getLL('title_import');
242 if (GeneralUtility::_POST('_upload')) {
243 $this->checkUpload();
244 }
245 // Finally: If upload went well, set the new file as the import file:
246 if (!empty($this->uploadedFiles[0])) {
247 // Only allowed extensions....
248 $extension = $this->uploadedFiles[0]->getExtension();
249 if ($extension === 't3d' || $extension === 'xml') {
250 $inData['file'] = $this->uploadedFiles[0]->getCombinedIdentifier();
251 }
252 }
253 // Call import interface:
254 $this->importData($inData);
255 $this->standaloneView->setTemplate('Import.html');
256 break;
257 }
258
259 // Setting up the buttons and markers for docheader
260 $this->getButtons();
261 }
262
263 /**
264 * Injects the request object for the current request and gathers all data
265 *
266 * IMPORTING DATA:
267 *
268 * Incoming array has syntax:
269 * GETvar 'id' = import page id (must be readable)
270 *
271 * file = (pointing to filename relative to PATH_site)
272 *
273 * [all relation fields are clear, but not files]
274 * - page-tree is written first
275 * - then remaining pages (to the root of import)
276 * - then all other records are written either to related included pages or if not found to import-root (should be a sysFolder in most cases)
277 * - then all internal relations are set and non-existing relations removed, relations to static tables preserved.
278 *
279 * EXPORTING DATA:
280 *
281 * Incoming array has syntax:
282 *
283 * file[] = file
284 * dir[] = dir
285 * list[] = table:pid
286 * record[] = table:uid
287 *
288 * pagetree[id] = (single id)
289 * pagetree[levels]=1,2,3, -1 = currently unpacked tree, -2 = only tables on page
290 * pagetree[tables][]=table/_ALL
291 *
292 * external_ref[tables][]=table/_ALL
293 *
294 * @param ServerRequestInterface $request the current request
295 * @param ResponseInterface $response
296 * @return ResponseInterface the response with the content
297 */
298 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
299 {
300 $GLOBALS['SOBE'] = $this;
301 $this->init();
302 $this->main();
303 $this->moduleTemplate->setContent($this->standaloneView->render());
304 $response->getBody()->write($this->moduleTemplate->renderContent());
305
306 return $response;
307 }
308
309 /**
310 * Create the panel of buttons for submitting the form or otherwise perform operations.
311 *
312 * @return array all available buttons as an associated array
313 */
314 protected function getButtons()
315 {
316 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
317 if ($this->getBackendUser()->mayMakeShortcut()) {
318 $shortcutButton = $buttonBar->makeShortcutButton()
319 ->setGetVariables(['tx_impexp'])
320 ->setDisplayName($this->shortcutName)
321 ->setModuleName($this->moduleName);
322 $buttonBar->addButton($shortcutButton);
323 }
324 // back button
325 if ($this->returnUrl) {
326 $backButton = $buttonBar->makeLinkButton()
327 ->setHref($this->returnUrl)
328 ->setTitle($this->lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
329 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
330 $buttonBar->addButton($backButton);
331 }
332 // Input data grabbed:
333 $inData = GeneralUtility::_GP('tx_impexp');
334 if ((string)$inData['action'] === 'import') {
335 if ($this->id && is_array($this->pageinfo) || $this->getBackendUser()->isAdmin() && !$this->id) {
336 if (is_array($this->pageinfo) && $this->pageinfo['uid']) {
337 // View
338 $onClick = BackendUtility::viewOnClick(
339 $this->pageinfo['uid'],
340 '',
341 BackendUtility::BEgetRootLine($this->pageinfo['uid'])
342 );
343 $viewButton = $buttonBar->makeLinkButton()
344 ->setTitle($this->lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
345 ->setHref('#')
346 ->setIcon($this->iconFactory->getIcon('actions-view-page', Icon::SIZE_SMALL))
347 ->setOnClick($onClick);
348 $buttonBar->addButton($viewButton);
349 }
350 }
351 }
352 }
353
354 /**************************
355 * EXPORT FUNCTIONS
356 **************************/
357
358 /**
359 * Export part of module
360 * Setting content in $this->content
361 *
362 * @param array $inData Content of POST VAR tx_impexp[]..
363 * @throws \InvalidArgumentException
364 * @throws \RuntimeException
365 * @throws \TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException
366 */
367 public function exportData($inData)
368 {
369 // BUILDING EXPORT DATA:
370 // Processing of InData array values:
371 $inData['filename'] = trim(preg_replace('/[^[:alnum:]._-]*/', '', preg_replace('/\\.(t3d|xml)$/', '', $inData['filename'])));
372 if (strlen($inData['filename'])) {
373 $inData['filename'] .= $inData['filetype'] === 'xml' ? '.xml' : '.t3d';
374 }
375 // Set exclude fields in export object:
376 if (!is_array($inData['exclude'])) {
377 $inData['exclude'] = [];
378 }
379 // Saving/Loading/Deleting presets:
380 $this->presetRepository->processPresets($inData);
381 // Create export object and configure it:
382 $this->export = GeneralUtility::makeInstance(Export::class);
383 $this->export->init(0);
384 $this->export->excludeMap = (array)$inData['exclude'];
385 $this->export->softrefCfg = (array)$inData['softrefCfg'];
386 $this->export->extensionDependencies = ($inData['extension_dep'] === '') ? [] : (array)$inData['extension_dep'];
387 $this->export->showStaticRelations = $inData['showStaticRelations'];
388 $this->export->includeExtFileResources = !$inData['excludeHTMLfileResources'];
389 $this->excludeDisabledRecords = (bool)$inData['excludeDisabled'];
390 $this->export->setExcludeDisabledRecords($this->excludeDisabledRecords);
391
392 // Static tables:
393 if (is_array($inData['external_static']['tables'])) {
394 $this->export->relStaticTables = $inData['external_static']['tables'];
395 }
396 // Configure which tables external relations are included for:
397 if (is_array($inData['external_ref']['tables'])) {
398 $this->export->relOnlyTables = $inData['external_ref']['tables'];
399 }
400 $saveFilesOutsideExportFile = false;
401 if (isset($inData['save_export']) && isset($inData['saveFilesOutsideExportFile']) && $inData['saveFilesOutsideExportFile'] === '1') {
402 $this->export->setSaveFilesOutsideExportFile(true);
403 $saveFilesOutsideExportFile = true;
404 }
405 $this->export->setHeaderBasics();
406 // Meta data setting:
407
408 $beUser = $this->getBackendUser();
409 $this->export->setMetaData(
410 $inData['meta']['title'],
411 $inData['meta']['description'],
412 $inData['meta']['notes'],
413 $beUser->user['username'],
414 $beUser->user['realName'],
415 $beUser->user['email']
416 );
417 // Configure which records to export
418 if (is_array($inData['record'])) {
419 foreach ($inData['record'] as $ref) {
420 $rParts = explode(':', $ref);
421 $this->export->export_addRecord($rParts[0], BackendUtility::getRecord($rParts[0], $rParts[1]));
422 }
423 }
424 // Configure which tables to export
425 if (is_array($inData['list'])) {
426 foreach ($inData['list'] as $ref) {
427 $rParts = explode(':', $ref);
428 if ($beUser->check('tables_select', $rParts[0])) {
429 $statement = $this->exec_listQueryPid($rParts[0], $rParts[1]);
430 while ($subTrow = $statement->fetch()) {
431 $this->export->export_addRecord($rParts[0], $subTrow);
432 }
433 }
434 }
435 }
436 // Pagetree
437 if (isset($inData['pagetree']['id'])) {
438 // Based on click-expandable tree
439 $idH = null;
440 if ($inData['pagetree']['levels'] == -1) {
441 $pagetree = GeneralUtility::makeInstance(ExportPageTreeView::class);
442 if ($this->excludeDisabledRecords) {
443 $pagetree->init(BackendUtility::BEenableFields('pages'));
444 }
445 $tree = $pagetree->ext_tree($inData['pagetree']['id'], $this->filterPageIds($this->export->excludeMap));
446 $this->treeHTML = $pagetree->printTree($tree);
447 $idH = $pagetree->buffer_idH;
448 } elseif ($inData['pagetree']['levels'] == -2) {
449 $this->addRecordsForPid($inData['pagetree']['id'], $inData['pagetree']['tables']);
450 } else {
451 // Based on depth
452 // Drawing tree:
453 // If the ID is zero, export root
454 if (!$inData['pagetree']['id'] && $beUser->isAdmin()) {
455 $sPage = [
456 'uid' => 0,
457 'title' => 'ROOT'
458 ];
459 } else {
460 $sPage = BackendUtility::getRecordWSOL('pages', $inData['pagetree']['id'], '*', ' AND ' . $this->perms_clause);
461 }
462 if (is_array($sPage)) {
463 $pid = $inData['pagetree']['id'];
464 $tree = GeneralUtility::makeInstance(PageTreeView::class);
465 $initClause = 'AND ' . $this->perms_clause . $this->filterPageIds($this->export->excludeMap);
466 if ($this->excludeDisabledRecords) {
467 $initClause .= BackendUtility::BEenableFields('pages');
468 }
469 $tree->init($initClause);
470 $HTML = $this->iconFactory->getIconForRecord('pages', $sPage, Icon::SIZE_SMALL)->render();
471 $tree->tree[] = ['row' => $sPage, 'HTML' => $HTML];
472 $tree->buffer_idH = [];
473 if ($inData['pagetree']['levels'] > 0) {
474 $tree->getTree($pid, $inData['pagetree']['levels'], '');
475 }
476 $idH = [];
477 $idH[$pid]['uid'] = $pid;
478 if (!empty($tree->buffer_idH)) {
479 $idH[$pid]['subrow'] = $tree->buffer_idH;
480 }
481 $pagetree = GeneralUtility::makeInstance(ExportPageTreeView::class);
482 $this->treeHTML = $pagetree->printTree($tree->tree);
483 $this->shortcutName .= ' (' . $sPage['title'] . ')';
484 }
485 }
486 // In any case we should have a multi-level array, $idH, with the page structure
487 // here (and the HTML-code loaded into memory for nice display...)
488 if (is_array($idH)) {
489 // Sets the pagetree and gets a 1-dim array in return with the pages (in correct submission order BTW...)
490 $flatList = $this->export->setPageTree($idH);
491 foreach ($flatList as $k => $value) {
492 $this->export->export_addRecord('pages', BackendUtility::getRecord('pages', $k));
493 $this->addRecordsForPid($k, $inData['pagetree']['tables']);
494 }
495 }
496 }
497 // After adding ALL records we set relations:
498 for ($a = 0; $a < 10; $a++) {
499 $addR = $this->export->export_addDBRelations($a);
500 if (empty($addR)) {
501 break;
502 }
503 }
504 // Finally files are added:
505 // MUST be after the DBrelations are set so that files from ALL added records are included!
506 $this->export->export_addFilesFromRelations();
507
508 $this->export->export_addFilesFromSysFilesRecords();
509
510 // If the download button is clicked, return file
511 if ($inData['download_export'] || $inData['save_export']) {
512 switch ((string)$inData['filetype']) {
513 case 'xml':
514 $out = $this->export->compileMemoryToFileContent('xml');
515 $fExt = '.xml';
516 break;
517 case 't3d':
518 $this->export->dontCompress = 1;
519 // intentional fall-through
520 // no break
521 default:
522 $out = $this->export->compileMemoryToFileContent();
523 $fExt = ($this->export->doOutputCompress() ? '-z' : '') . '.t3d';
524 }
525 // Filename:
526 $dlFile = $inData['filename'];
527 if (!$dlFile) {
528 $exportName = substr(preg_replace('/[^[:alnum:]_]/', '-', $inData['download_export_name']), 0, 20);
529 $dlFile = 'T3D_' . $exportName . '_' . date('Y-m-d_H-i') . $fExt;
530 }
531
532 // Export for download:
533 if ($inData['download_export']) {
534 $mimeType = 'application/octet-stream';
535 header('Content-Type: ' . $mimeType);
536 header('Content-Length: ' . strlen($out));
537 header('Content-Disposition: attachment; filename=' . basename($dlFile));
538 echo $out;
539 die;
540 }
541 // Export by saving:
542 if ($inData['save_export']) {
543 $saveFolder = $this->getDefaultImportExportFolder();
544 $lang = $this->getLanguageService();
545 if ($saveFolder !== false && $saveFolder->checkActionPermission('write')) {
546 $temporaryFileName = GeneralUtility::tempnam('export');
547 file_put_contents($temporaryFileName, $out);
548 $file = $saveFolder->addFile($temporaryFileName, $dlFile, 'replace');
549 if ($saveFilesOutsideExportFile) {
550 $filesFolderName = $dlFile . '.files';
551 $filesFolder = $saveFolder->createFolder($filesFolderName);
552 $temporaryFolderForExport = ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->export->getTemporaryFilesPathForExport());
553 $temporaryFilesForExport = $temporaryFolderForExport->getFiles();
554 foreach ($temporaryFilesForExport as $temporaryFileForExport) {
555 $filesFolder->getStorage()->moveFile($temporaryFileForExport, $filesFolder);
556 }
557 $temporaryFolderForExport->delete();
558 }
559
560 /** @var FlashMessage $flashMessage */
561 $flashMessage = GeneralUtility::makeInstance(
562 FlashMessage::class,
563 sprintf($lang->getLL('exportdata_savedInSBytes'), $file->getPublicUrl(), GeneralUtility::formatSize(strlen($out))),
564 $lang->getLL('exportdata_savedFile'),
565 FlashMessage::OK
566 );
567 } else {
568 /** @var FlashMessage $flashMessage */
569 $flashMessage = GeneralUtility::makeInstance(
570 FlashMessage::class,
571 sprintf($lang->getLL('exportdata_badPathS'), $saveFolder->getPublicUrl()),
572 $lang->getLL('exportdata_problemsSavingFile'),
573 FlashMessage::ERROR
574 );
575 }
576 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
577 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
578 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
579 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
580 $defaultFlashMessageQueue->enqueue($flashMessage);
581 }
582 }
583
584 $this->makeConfigurationForm($inData);
585
586 $this->makeSaveForm($inData);
587
588 $this->makeAdvancedOptionsForm($inData);
589
590 $this->standaloneView->assign('errors', $this->export->errorLog);
591
592 // Generate overview:
593 $this->standaloneView->assign(
594 'contentOverview',
595 $this->export->displayContentOverview()
596 );
597 }
598
599 /**
600 * Adds records to the export object for a specific page id.
601 *
602 * @param int $k Page id for which to select records to add
603 * @param array $tables Array of table names to select from
604 * @param int $maxNumber @deprecated since TYPO3 v9, will be removed in TYPO3 v10
605 */
606 public function addRecordsForPid($k, $tables, $maxNumber = null)
607 {
608 if (!is_array($tables)) {
609 return;
610 }
611 foreach ($GLOBALS['TCA'] as $table => $value) {
612 if ($table !== 'pages' && (in_array($table, $tables) || in_array('_ALL', $tables))) {
613 if ($this->getBackendUser()->check('tables_select', $table) && !$GLOBALS['TCA'][$table]['ctrl']['is_static']) {
614 if ($maxNumber !== null) {
615 // @deprecated since TYPO3 v9, will be removed in TYPO3 v10. Remove this if in v10
616 // and the 3rd method argument. trigger_error() is called by method exec_listQueryPid() below
617 $statement = $this->exec_listQueryPid($table, $k, MathUtility::forceIntegerInRange($maxNumber, 1));
618 } else {
619 $statement = $this->exec_listQueryPid($table, $k);
620 }
621 while ($subTrow = $statement->fetch()) {
622 $this->export->export_addRecord($table, $subTrow);
623 }
624 }
625 }
626 }
627 }
628
629 /**
630 * Selects records from table / pid
631 *
632 * @param string $table Table to select from
633 * @param int $pid Page ID to select from
634 * @param int $limit @deprecated since TYPO3 v9, will be removed in TYPO3 v10
635 * @return \Doctrine\DBAL\Driver\Statement Query statement
636 */
637 public function exec_listQueryPid($table, $pid, $limit = null)
638 {
639 // @deprecated In v10, remove this if and the method argument
640 if ($limit !== null) {
641 trigger_error(
642 'The third argument of addRecordsForPid() and exec_listQueryPid() has been'
643 . ' deprecated, do not limit exports anymore. The parameter will be removed in TYPO3 v10.',
644 E_USER_DEPRECATED
645 );
646 }
647
648 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
649
650 $orderBy = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ?: $GLOBALS['TCA'][$table]['ctrl']['default_sortby'];
651 $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
652
653 if ($this->excludeDisabledRecords === false) {
654 $queryBuilder->getRestrictions()
655 ->removeAll()
656 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
657 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
658 }
659
660 $queryBuilder->select('*')
661 ->from($table)
662 ->where(
663 $queryBuilder->expr()->eq(
664 'pid',
665 $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)
666 )
667 );
668
669 // @deprecated In v10, remove this if
670 if ($limit !== null) {
671 $queryBuilder->setMaxResults($limit);
672 }
673
674 foreach (QueryHelper::parseOrderBy((string)$orderBy) as $orderPair) {
675 list($fieldName, $order) = $orderPair;
676 $queryBuilder->addOrderBy($fieldName, $order);
677 }
678
679 $statement = $queryBuilder->execute();
680
681 // @deprecated In v10, remove this if, and the two getLL locallang target keys
682 if ($limit !== null && $statement->rowCount() == $limit) {
683 $limitWarning = sprintf($this->lang->getLL('makeconfig_anSqlQueryReturned'), $limit);
684 /** @var FlashMessage $flashMessage */
685 $flashMessage = GeneralUtility::makeInstance(
686 FlashMessage::class,
687 $this->lang->getLL('execlistqu_maxNumberLimit'),
688 $limitWarning,
689 FlashMessage::WARNING
690 );
691 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
692 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
693 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
694 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
695 $defaultFlashMessageQueue->enqueue($flashMessage);
696 }
697
698 return $statement;
699 }
700
701 /**
702 * Create configuration form
703 *
704 * @param array $inData Form configuration data
705 */
706 public function makeConfigurationForm($inData)
707 {
708 $nameSuggestion = '';
709 // Page tree export options:
710 if (isset($inData['pagetree']['id'])) {
711 $this->standaloneView->assign('treeHTML', $this->treeHTML);
712
713 $opt = [
714 '-2' => $this->lang->getLL('makeconfig_tablesOnThisPage'),
715 '-1' => $this->lang->getLL('makeconfig_expandedTree'),
716 '0' => $this->lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_0'),
717 '1' => $this->lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_1'),
718 '2' => $this->lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_2'),
719 '3' => $this->lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_3'),
720 '4' => $this->lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_4'),
721 '999' => $this->lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_infi'),
722 ];
723 $this->standaloneView->assign('levelSelectOptions', $opt);
724 $this->standaloneView->assign('tableSelectOptions', $this->getTableSelectOptions('pages'));
725 $nameSuggestion .= 'tree_PID' . $inData['pagetree']['id'] . '_L' . $inData['pagetree']['levels'];
726 }
727 // Single record export:
728 if (is_array($inData['record'])) {
729 $records = [];
730 foreach ($inData['record'] as $ref) {
731 $rParts = explode(':', $ref);
732 $tName = $rParts[0];
733 $rUid = $rParts[1];
734 $nameSuggestion .= $tName . '_' . $rUid;
735 $rec = BackendUtility::getRecordWSOL($tName, $rUid);
736 if (!empty($rec)) {
737 $records[] = [
738 'icon' => $this->iconFactory->getIconForRecord($tName, $rec, Icon::SIZE_SMALL)->render(),
739 'title' => BackendUtility::getRecordTitle($tName, $rec, true),
740 'tableName' => $tName,
741 'recordUid' => $rUid
742 ];
743 }
744 }
745 $this->standaloneView->assign('records', $records);
746 }
747 // Single tables/pids:
748 if (is_array($inData['list'])) {
749
750 // Display information about pages from which the export takes place
751 $tableList = [];
752 foreach ($inData['list'] as $reference) {
753 $referenceParts = explode(':', $reference);
754 $tableName = $referenceParts[0];
755 if ($this->getBackendUser()->check('tables_select', $tableName)) {
756 // If the page is actually the root, handle it differently
757 // NOTE: we don't compare integers, because the number actually comes from the split string above
758 if ($referenceParts[1] === '0') {
759 $iconAndTitle = $this->iconFactory->getIcon('apps-pagetree-root', Icon::SIZE_SMALL)->render() . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
760 } else {
761 $record = BackendUtility::getRecordWSOL('pages', $referenceParts[1]);
762 $iconAndTitle = $this->iconFactory->getIconForRecord('pages', $record, Icon::SIZE_SMALL)->render()
763 . BackendUtility::getRecordTitle('pages', $record, true);
764 }
765
766 $tableList[] = [
767 'iconAndTitle' => sprintf($this->lang->getLL('makeconfig_tableListEntry'), $tableName, $iconAndTitle),
768 'reference' => $reference
769 ];
770 }
771 }
772 $this->standaloneView->assign('tableList', $tableList);
773 }
774
775 $this->standaloneView->assign('externalReferenceTableSelectOptions', $this->getTableSelectOptions());
776 $this->standaloneView->assign('externalStaticTableSelectOptions', $this->getTableSelectOptions());
777 $this->standaloneView->assign('nameSuggestion', $nameSuggestion);
778 }
779
780 /**
781 * Create advanced options form
782 * Sets content in $this->content
783 *
784 * @param array $inData Form configurat data
785 */
786 public function makeAdvancedOptionsForm($inData)
787 {
788 $loadedExtensions = ExtensionManagementUtility::getLoadedExtensionListArray();
789 $loadedExtensions = array_combine($loadedExtensions, $loadedExtensions);
790 $this->standaloneView->assign('extensions', $loadedExtensions);
791 $this->standaloneView->assign('inData', $inData);
792 }
793
794 /**
795 * Create configuration form
796 *
797 * @param array $inData Form configuration data
798 */
799 public function makeSaveForm($inData)
800 {
801 $opt = $this->presetRepository->getPresets((int)$inData['pagetree']['id']);
802
803 $this->standaloneView->assign('presetSelectOptions', $opt);
804
805 $saveFolder = $this->getDefaultImportExportFolder();
806 if ($saveFolder) {
807 $this->standaloneView->assign('saveFolder', $saveFolder->getCombinedIdentifier());
808 }
809
810 // Add file options:
811 $opt = [];
812 $opt['xml'] = $this->lang->getLL('makesavefo_xml');
813 if ($this->export->compress) {
814 $opt['t3d_compressed'] = $this->lang->getLL('makesavefo_t3dFileCompressed');
815 }
816 $opt['t3d'] = $this->lang->getLL('makesavefo_t3dFile');
817
818 $this->standaloneView->assign('filetypeSelectOptions', $opt);
819
820 $fileName = '';
821 if ($saveFolder) {
822 $this->standaloneView->assign('saveFolder', $saveFolder->getPublicUrl());
823 $this->standaloneView->assign('hasSaveFolder', true);
824 }
825 $this->standaloneView->assign('fileName', $fileName);
826 }
827
828 /**************************
829 * IMPORT FUNCTIONS
830 **************************/
831
832 /**
833 * Import part of module
834 *
835 * @param array $inData Content of POST VAR tx_impexp[]..
836 * @throws \BadFunctionCallException
837 * @throws \InvalidArgumentException
838 * @throws \RuntimeException
839 */
840 public function importData($inData)
841 {
842 $access = is_array($this->pageinfo);
843 $beUser = $this->getBackendUser();
844 if ($this->id && $access || $beUser->isAdmin() && !$this->id) {
845 if ($beUser->isAdmin() && !$this->id) {
846 $this->pageinfo = ['title' => '[root-level]', 'uid' => 0, 'pid' => 0];
847 }
848 if ($inData['new_import']) {
849 unset($inData['import_mode']);
850 }
851 /** @var $import Import */
852 $import = GeneralUtility::makeInstance(Import::class);
853 $import->init();
854 $import->update = $inData['do_update'];
855 $import->import_mode = $inData['import_mode'];
856 $import->enableLogging = $inData['enableLogging'];
857 $import->global_ignore_pid = $inData['global_ignore_pid'];
858 $import->force_all_UIDS = $inData['force_all_UIDS'];
859 $import->showDiff = !$inData['notShowDiff'];
860 $import->allowPHPScripts = $inData['allowPHPScripts'];
861 $import->softrefInputValues = $inData['softrefInputValues'];
862
863 // OUTPUT creation:
864
865 // Make input selector:
866 // must have trailing slash.
867 $path = $this->getDefaultImportExportFolder();
868 $exportFiles = $this->getExportFiles();
869
870 $this->shortcutName .= ' (' . $this->pageinfo['title'] . ')';
871
872 // Configuration
873 $selectOptions = [''];
874 foreach ($exportFiles as $file) {
875 $selectOptions[$file->getCombinedIdentifier()] = $file->getPublicUrl();
876 }
877
878 $this->standaloneView->assign('import', $import);
879 $this->standaloneView->assign('inData', $inData);
880 $this->standaloneView->assign('fileSelectOptions', $selectOptions);
881
882 if ($path) {
883 $this->standaloneView->assign('importPath', sprintf($this->lang->getLL('importdata_fromPathS'), $path->getCombinedIdentifier()));
884 } else {
885 $this->standaloneView->assign('importPath', $this->lang->getLL('importdata_no_default_upload_folder'));
886 }
887 $this->standaloneView->assign('isAdmin', $beUser->isAdmin());
888
889 // Upload file:
890 $tempFolder = $this->getDefaultImportExportFolder();
891 if ($tempFolder) {
892 $this->standaloneView->assign('tempFolder', $tempFolder->getCombinedIdentifier());
893 $this->standaloneView->assign('hasTempUploadFolder', true);
894 if (GeneralUtility::_POST('_upload')) {
895 $this->standaloneView->assign('submitted', GeneralUtility::_POST('_upload'));
896 $this->standaloneView->assign('noFileUploaded', $this->fileProcessor->internalUploadMap[1]);
897 if ($this->uploadedFiles[0]) {
898 $this->standaloneView->assign('uploadedFile', $this->uploadedFiles[0]->getName());
899 }
900 }
901 }
902
903 // Perform import or preview depending:
904 $inFile = $this->getFile($inData['file']);
905 if ($inFile !== null && $inFile->exists()) {
906 $this->standaloneView->assign('metaDataInFileExists', true);
907 $importInhibitedMessages = [];
908 if ($import->loadFile($inFile->getForLocalProcessing(false), 1)) {
909 $importInhibitedMessages = $import->checkImportPrerequisites();
910 if ($inData['import_file']) {
911 if (empty($importInhibitedMessages)) {
912 $import->importData($this->id);
913 BackendUtility::setUpdateSignal('updatePageTree');
914 }
915 }
916 $import->display_import_pid_record = $this->pageinfo;
917 $this->standaloneView->assign('contentOverview', $import->displayContentOverview());
918 }
919 // Compile messages which are inhibiting a proper import and add them to output.
920 if (!empty($importInhibitedMessages)) {
921 $flashMessageQueue = GeneralUtility::makeInstance(FlashMessageService::class)->getMessageQueueByIdentifier('impexp.errors');
922 foreach ($importInhibitedMessages as $message) {
923 $flashMessageQueue->addMessage(GeneralUtility::makeInstance(
924 FlashMessage::class,
925 $message,
926 '',
927 FlashMessage::ERROR
928 ));
929 }
930 }
931 }
932
933 $this->standaloneView->assign('errors', $import->errorLog);
934 }
935 }
936
937 /****************************
938 * Helper functions
939 ****************************/
940
941 /**
942 * Returns a \TYPO3\CMS\Core\Resource\Folder object for saving export files
943 * to the server and is also used for uploading import files.
944 *
945 * @throws \InvalidArgumentException
946 * @return \TYPO3\CMS\Core\Resource\Folder|null
947 */
948 protected function getDefaultImportExportFolder()
949 {
950 $defaultImportExportFolder = null;
951
952 $defaultTemporaryFolder = $this->getBackendUser()->getDefaultUploadTemporaryFolder();
953 if ($defaultTemporaryFolder !== null) {
954 $importExportFolderName = 'importexport';
955 $createFolder = !$defaultTemporaryFolder->hasFolder($importExportFolderName);
956 if ($createFolder === true) {
957 try {
958 $defaultImportExportFolder = $defaultTemporaryFolder->createFolder($importExportFolderName);
959 } catch (Exception $folderAccessException) {
960 }
961 } else {
962 $defaultImportExportFolder = $defaultTemporaryFolder->getSubfolder($importExportFolderName);
963 }
964 }
965
966 return $defaultImportExportFolder;
967 }
968
969 /**
970 * Check if a file has been uploaded
971 *
972 * @throws \InvalidArgumentException
973 * @throws \UnexpectedValueException
974 */
975 public function checkUpload()
976 {
977 $file = GeneralUtility::_GP('file');
978 // Initializing:
979 $this->fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
980 $this->fileProcessor->setActionPermissions();
981 $conflictMode = empty(GeneralUtility::_GP('overwriteExistingFiles')) ? DuplicationBehavior::__default : DuplicationBehavior::REPLACE;
982 $this->fileProcessor->setExistingFilesConflictMode(DuplicationBehavior::cast($conflictMode));
983 $this->fileProcessor->start($file);
984 $result = $this->fileProcessor->processData();
985 if (!empty($result['upload'])) {
986 foreach ($result['upload'] as $uploadedFiles) {
987 $this->uploadedFiles += $uploadedFiles;
988 }
989 }
990 }
991
992 /**
993 * Returns option array to be used in Fluid
994 *
995 * @param string $excludeList Table names (and the string "_ALL") to exclude. Comma list
996 * @return array
997 */
998 public function getTableSelectOptions($excludeList = '')
999 {
1000 $optValues = [];
1001 if (!GeneralUtility::inList($excludeList, '_ALL')) {
1002 $optValues['_ALL'] = '[' . $this->lang->getLL('ALL_tables') . ']';
1003 }
1004 foreach ($GLOBALS['TCA'] as $table => $_) {
1005 if ($this->getBackendUser()->check('tables_select', $table) && !GeneralUtility::inList($excludeList, $table)) {
1006 $optValues[$table] = $table;
1007 }
1008 }
1009 return $optValues;
1010 }
1011
1012 /**
1013 * Filter page IDs by traversing exclude array, finding all
1014 * excluded pages (if any) and making an AND NOT IN statement for the select clause.
1015 *
1016 * @param array $exclude Exclude array from import/export object.
1017 * @return string AND where clause part to filter out page uids.
1018 */
1019 public function filterPageIds($exclude)
1020 {
1021 // Get keys:
1022 $exclude = array_keys($exclude);
1023 // Traverse
1024 $pageIds = [];
1025 foreach ($exclude as $element) {
1026 list($table, $uid) = explode(':', $element);
1027 if ($table === 'pages') {
1028 $pageIds[] = (int)$uid;
1029 }
1030 }
1031 // Add to clause:
1032 if (!empty($pageIds)) {
1033 return ' AND uid NOT IN (' . implode(',', $pageIds) . ')';
1034 }
1035 return '';
1036 }
1037
1038 /**
1039 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
1040 */
1041 protected function getBackendUser()
1042 {
1043 return $GLOBALS['BE_USER'];
1044 }
1045
1046 /**
1047 * @return LanguageService
1048 */
1049 protected function getLanguageService()
1050 {
1051 return $GLOBALS['LANG'];
1052 }
1053
1054 /**
1055 * Gets all export files.
1056 *
1057 * @throws \InvalidArgumentException
1058 * @return array|\TYPO3\CMS\Core\Resource\File[]
1059 */
1060 protected function getExportFiles()
1061 {
1062 $exportFiles = [];
1063
1064 $folder = $this->getDefaultImportExportFolder();
1065 if ($folder !== null) {
1066
1067 /** @var $filter FileExtensionFilter */
1068 $filter = GeneralUtility::makeInstance(FileExtensionFilter::class);
1069 $filter->setAllowedFileExtensions(['t3d', 'xml']);
1070 $folder->getStorage()->addFileAndFolderNameFilter([$filter, 'filterFileList']);
1071
1072 $exportFiles = $folder->getFiles();
1073 }
1074
1075 return $exportFiles;
1076 }
1077
1078 /**
1079 * Gets a file by combined identifier.
1080 *
1081 * @param string $combinedIdentifier
1082 * @return \TYPO3\CMS\Core\Resource\File|null
1083 */
1084 protected function getFile($combinedIdentifier)
1085 {
1086 try {
1087 $file = ResourceFactory::getInstance()->getFileObjectFromCombinedIdentifier($combinedIdentifier);
1088 } catch (\Exception $exception) {
1089 $file = null;
1090 }
1091
1092 return $file;
1093 }
1094 }