[TASK] impexp: Remove size handling
[Packages/TYPO3.CMS.git] / typo3 / sysext / impexp / Classes / Export.php
1 <?php
2 namespace TYPO3\CMS\Impexp;
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\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Database\ReferenceIndex;
19 use TYPO3\CMS\Core\Exception;
20 use TYPO3\CMS\Core\Html\HtmlParser;
21 use TYPO3\CMS\Core\Resource\File;
22 use TYPO3\CMS\Core\Resource\ResourceFactory;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Core\Utility\PathUtility;
25
26 /**
27 * EXAMPLE for using the impexp-class for exporting stuff:
28 *
29 * Create and initialize:
30 * $this->export = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Impexp\ImportExport::class);
31 * $this->export->init();
32 * Set which tables relations we will allow:
33 * $this->export->relOnlyTables[]="tt_news"; // exclusively includes. See comment in the class
34 *
35 * Adding records:
36 * $this->export->export_addRecord("pages", $this->pageinfo);
37 * $this->export->export_addRecord("pages", \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord("pages", 38));
38 * $this->export->export_addRecord("pages", \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord("pages", 39));
39 * $this->export->export_addRecord("tt_content", \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord("tt_content", 12));
40 * $this->export->export_addRecord("tt_content", \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord("tt_content", 74));
41 * $this->export->export_addRecord("sys_template", \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord("sys_template", 20));
42 *
43 * Adding all the relations (recursively in 5 levels so relations has THEIR relations registered as well)
44 * for($a=0;$a<5;$a++) {
45 * $addR = $this->export->export_addDBRelations($a);
46 * if (empty($addR)) break;
47 * }
48 *
49 * Finally load all the files.
50 * $this->export->export_addFilesFromRelations(); // MUST be after the DBrelations are set so that file from ALL added records are included!
51 *
52 * Write export
53 * $out = $this->export->compileMemoryToFileContent();
54 */
55
56 /**
57 * T3D file Export library (TYPO3 Record Document)
58 */
59 class Export extends ImportExport
60 {
61 /**
62 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10. In v10, just remove property, it is not used any longer.
63 * @var int
64 */
65 public $maxFileSize = 1000000;
66
67 /**
68 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10. In v10, just remove property, it is not used any longer.
69 * @var int
70 */
71 public $maxRecordSize = 1000000;
72
73 /**
74 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10. In v10, just remove property, it is not used any longer.
75 * @var int
76 */
77 public $maxExportSize = 10000000;
78
79 /**
80 * Set by user: If set, compression in t3d files is disabled
81 *
82 * @var bool
83 */
84 public $dontCompress = false;
85
86 /**
87 * If set, HTML file resources are included.
88 *
89 * @var bool
90 */
91 public $includeExtFileResources = false;
92
93 /**
94 * Files with external media (HTML/css style references inside)
95 *
96 * @var string
97 */
98 public $extFileResourceExtensions = 'html,htm,css';
99
100 /**
101 * Keys are [recordname], values are an array of fields to be included
102 * in the export
103 *
104 * @var array
105 */
106 protected $recordTypesIncludeFields = [];
107
108 /**
109 * Default array of fields to be included in the export
110 *
111 * @var array
112 */
113 protected $defaultRecordIncludeFields = ['uid', 'pid'];
114
115 /**
116 * @var bool
117 */
118 protected $saveFilesOutsideExportFile = false;
119
120 /**
121 * @var string|null
122 */
123 protected $temporaryFilesPathForExport = null;
124
125 /**************************
126 * Initialize
127 *************************/
128
129 /**
130 * Init the object
131 *
132 * @param bool $dontCompress If set, compression of t3d files is disabled
133 */
134 public function init($dontCompress = false)
135 {
136 parent::init();
137 $this->dontCompress = $dontCompress;
138 $this->mode = 'export';
139 }
140
141 /**************************
142 * Export / Init + Meta Data
143 *************************/
144
145 /**
146 * Set header basics
147 */
148 public function setHeaderBasics()
149 {
150 // Initializing:
151 if (is_array($this->softrefCfg)) {
152 foreach ($this->softrefCfg as $key => $value) {
153 if (!strlen($value['mode'])) {
154 unset($this->softrefCfg[$key]);
155 }
156 }
157 }
158 // Setting in header memory:
159 // Version of file format
160 $this->dat['header']['XMLversion'] = '1.0';
161 // Initialize meta data array (to put it in top of file)
162 $this->dat['header']['meta'] = [];
163 // Add list of tables to consider static
164 $this->dat['header']['relStaticTables'] = $this->relStaticTables;
165 // The list of excluded records
166 $this->dat['header']['excludeMap'] = $this->excludeMap;
167 // Soft Reference mode for elements
168 $this->dat['header']['softrefCfg'] = $this->softrefCfg;
169 // List of extensions the import depends on.
170 $this->dat['header']['extensionDependencies'] = $this->extensionDependencies;
171 $this->dat['header']['charset'] = 'utf-8';
172 }
173
174 /**
175 * Set charset
176 *
177 * @param string $charset Charset for the content in the export. During import the character set will be converted if the target system uses another charset.
178 */
179 public function setCharset($charset)
180 {
181 $this->dat['header']['charset'] = $charset;
182 }
183
184 /**
185 * Sets meta data
186 *
187 * @param string $title Title of the export
188 * @param string $description Description of the export
189 * @param string $notes Notes about the contents
190 * @param string $packager_username Backend Username of the packager (the guy making the export)
191 * @param string $packager_name Real name of the packager
192 * @param string $packager_email Email of the packager
193 */
194 public function setMetaData($title, $description, $notes, $packager_username, $packager_name, $packager_email)
195 {
196 $this->dat['header']['meta'] = [
197 'title' => $title,
198 'description' => $description,
199 'notes' => $notes,
200 'packager_username' => $packager_username,
201 'packager_name' => $packager_name,
202 'packager_email' => $packager_email,
203 'TYPO3_version' => TYPO3_version,
204 'created' => strftime('%A %e. %B %Y', $GLOBALS['EXEC_TIME'])
205 ];
206 }
207
208 /**
209 * Option to enable having the files not included in the export file.
210 * The files are saved to a temporary folder instead.
211 *
212 * @param bool $saveFilesOutsideExportFile
213 * @see getTemporaryFilesPathForExport()
214 */
215 public function setSaveFilesOutsideExportFile($saveFilesOutsideExportFile)
216 {
217 $this->saveFilesOutsideExportFile = $saveFilesOutsideExportFile;
218 }
219
220 /**************************
221 * Export / Init Page tree
222 *************************/
223
224 /**
225 * Sets the page-tree array in the export header and returns the array in a flattened version
226 *
227 * @param array $idH Hierarchy of ids, the page tree: array([uid] => array("uid" => [uid], "subrow" => array(.....)), [uid] => ....)
228 * @return array The hierarchical page tree converted to a one-dimensional list of pages
229 */
230 public function setPageTree($idH)
231 {
232 $this->dat['header']['pagetree'] = $this->unsetExcludedSections($idH);
233 return $this->flatInversePageTree($this->dat['header']['pagetree']);
234 }
235
236 /**
237 * Removes entries in the page tree which are found in ->excludeMap[]
238 *
239 * @param array $idH Page uid hierarchy
240 * @return array Modified input array
241 * @access private
242 * @see setPageTree()
243 */
244 public function unsetExcludedSections($idH)
245 {
246 if (is_array($idH)) {
247 foreach ($idH as $k => $v) {
248 if ($this->excludeMap['pages:' . $idH[$k]['uid']]) {
249 unset($idH[$k]);
250 } elseif (is_array($idH[$k]['subrow'])) {
251 $idH[$k]['subrow'] = $this->unsetExcludedSections($idH[$k]['subrow']);
252 }
253 }
254 }
255 return $idH;
256 }
257
258 /**************************
259 * Export
260 *************************/
261
262 /**
263 * Sets the fields of record types to be included in the export
264 *
265 * @param array $recordTypesIncludeFields Keys are [recordname], values are an array of fields to be included in the export
266 * @throws Exception if an array value is not type of array
267 */
268 public function setRecordTypesIncludeFields(array $recordTypesIncludeFields)
269 {
270 foreach ($recordTypesIncludeFields as $table => $fields) {
271 if (!is_array($fields)) {
272 throw new Exception('The include fields for record type ' . htmlspecialchars($table) . ' are not defined by an array.', 1391440658);
273 }
274 $this->setRecordTypeIncludeFields($table, $fields);
275 }
276 }
277
278 /**
279 * Sets the fields of a record type to be included in the export
280 *
281 * @param string $table The record type
282 * @param array $fields The fields to be included
283 */
284 public function setRecordTypeIncludeFields($table, array $fields)
285 {
286 $this->recordTypesIncludeFields[$table] = $fields;
287 }
288
289 /**
290 * Adds the record $row from $table.
291 * No checking for relations done here. Pure data.
292 *
293 * @param string $table Table name
294 * @param array $row Record row.
295 * @param int $relationLevel (Internal) if the record is added as a relation, this is set to the "level" it was on.
296 */
297 public function export_addRecord($table, $row, $relationLevel = 0)
298 {
299 BackendUtility::workspaceOL($table, $row);
300 if ($this->excludeDisabledRecords && !$this->isActive($table, $row['uid'])) {
301 return;
302 }
303 if ((string)$table !== '' && is_array($row) && $row['uid'] > 0 && !$this->excludeMap[$table . ':' . $row['uid']]) {
304 if ($this->checkPID($table === 'pages' ? $row['uid'] : $row['pid'])) {
305 if (!isset($this->dat['records'][$table . ':' . $row['uid']])) {
306 // Prepare header info:
307 $row = $this->filterRecordFields($table, $row);
308 $headerInfo = [];
309 $headerInfo['uid'] = $row['uid'];
310 $headerInfo['pid'] = $row['pid'];
311 $headerInfo['title'] = GeneralUtility::fixed_lgd_cs(BackendUtility::getRecordTitle($table, $row), 40);
312 if ($relationLevel) {
313 $headerInfo['relationLevel'] = $relationLevel;
314 }
315 // Set the header summary:
316 $this->dat['header']['records'][$table][$row['uid']] = $headerInfo;
317 // Create entry in the PID lookup:
318 $this->dat['header']['pid_lookup'][$row['pid']][$table][$row['uid']] = 1;
319 // Initialize reference index object:
320 $refIndexObj = GeneralUtility::makeInstance(ReferenceIndex::class);
321 $refIndexObj->enableRuntimeCache();
322 // Yes to workspace overlays for exporting....
323 $refIndexObj->WSOL = true;
324 $relations = $refIndexObj->getRelations($table, $row);
325 $relations = $this->fixFileIDsInRelations($relations);
326 $relations = $this->removeSoftrefsHavingTheSameDatabaseRelation($relations);
327 // Data:
328 $this->dat['records'][$table . ':' . $row['uid']] = [];
329 $this->dat['records'][$table . ':' . $row['uid']]['data'] = $row;
330 $this->dat['records'][$table . ':' . $row['uid']]['rels'] = $relations;
331 // Add information about the relations in the record in the header:
332 $this->dat['header']['records'][$table][$row['uid']]['rels'] = $this->flatDBrels($this->dat['records'][$table . ':' . $row['uid']]['rels']);
333 // Add information about the softrefs to header:
334 $this->dat['header']['records'][$table][$row['uid']]['softrefs'] = $this->flatSoftRefs($this->dat['records'][$table . ':' . $row['uid']]['rels']);
335 } else {
336 $this->error('Record ' . $table . ':' . $row['uid'] . ' already added.');
337 }
338 } else {
339 $this->error('Record ' . $table . ':' . $row['uid'] . ' was outside your DB mounts!');
340 }
341 }
342 }
343
344 /**
345 * This changes the file reference ID from a hash based on the absolute file path
346 * (coming from ReferenceIndex) to a hash based on the relative file path.
347 *
348 * @param array $relations
349 * @return array
350 */
351 protected function fixFileIDsInRelations(array $relations)
352 {
353 foreach ($relations as $field => $relation) {
354 if (isset($relation['type']) && $relation['type'] === 'file') {
355 foreach ($relation['newValueFiles'] as $key => $fileRelationData) {
356 $absoluteFilePath = $fileRelationData['ID_absFile'];
357 if (GeneralUtility::isFirstPartOfStr($absoluteFilePath, PATH_site)) {
358 $relatedFilePath = PathUtility::stripPathSitePrefix($absoluteFilePath);
359 $relations[$field]['newValueFiles'][$key]['ID'] = md5($relatedFilePath);
360 }
361 }
362 }
363 if ($relation['type'] === 'flex') {
364 if (is_array($relation['flexFormRels']['file'])) {
365 foreach ($relation['flexFormRels']['file'] as $key => $subList) {
366 foreach ($subList as $subKey => $fileRelationData) {
367 $absoluteFilePath = $fileRelationData['ID_absFile'];
368 if (GeneralUtility::isFirstPartOfStr($absoluteFilePath, PATH_site)) {
369 $relatedFilePath = PathUtility::stripPathSitePrefix($absoluteFilePath);
370 $relations[$field]['flexFormRels']['file'][$key][$subKey]['ID'] = md5($relatedFilePath);
371 }
372 }
373 }
374 }
375 }
376 }
377 return $relations;
378 }
379
380 /**
381 * Relations could contain db relations to sys_file records. Some configuration combinations of TCA and
382 * SoftReferenceIndex create also softref relation entries for the identical file. This results
383 * in double included files, one in array "files" and one in array "file_fal".
384 * This function checks the relations for this double inclusions and removes the redundant softref relation.
385 *
386 * @param array $relations
387 * @return array
388 */
389 protected function removeSoftrefsHavingTheSameDatabaseRelation($relations)
390 {
391 $fixedRelations = [];
392 foreach ($relations as $field => $relation) {
393 $newRelation = $relation;
394 if (isset($newRelation['type']) && $newRelation['type'] === 'db') {
395 foreach ($newRelation['itemArray'] as $key => $dbRelationData) {
396 if ($dbRelationData['table'] === 'sys_file') {
397 if (isset($newRelation['softrefs']['keys']['typolink'])) {
398 foreach ($newRelation['softrefs']['keys']['typolink'] as $softrefKey => $softRefData) {
399 if ($softRefData['subst']['type'] === 'file') {
400 $file = ResourceFactory::getInstance()->retrieveFileOrFolderObject($softRefData['subst']['relFileName']);
401 if ($file instanceof File) {
402 if ($file->getUid() == $dbRelationData['id']) {
403 unset($newRelation['softrefs']['keys']['typolink'][$softrefKey]);
404 }
405 }
406 }
407 }
408 if (empty($newRelation['softrefs']['keys']['typolink'])) {
409 unset($newRelation['softrefs']);
410 }
411 }
412 }
413 }
414 }
415 $fixedRelations[$field] = $newRelation;
416 }
417 return $fixedRelations;
418 }
419
420 /**
421 * This analyses the existing added records, finds all database relations to records and adds these records to the export file.
422 * This function can be called repeatedly until it returns an empty array.
423 * In principle it should not allow to infinite recursivity, but you better set a limit...
424 * Call this BEFORE the ext_addFilesFromRelations (so files from added relations are also included of course)
425 *
426 * @param int $relationLevel Recursion level
427 * @return array overview of relations found and added: Keys [table]:[uid], values array with table and id
428 * @see export_addFilesFromRelations()
429 */
430 public function export_addDBRelations($relationLevel = 0)
431 {
432 // Traverse all "rels" registered for "records"
433 if (!is_array($this->dat['records'])) {
434 $this->error('There were no records available.');
435 return [];
436 }
437 $addR = [];
438 foreach ($this->dat['records'] as $k => $value) {
439 if (!is_array($this->dat['records'][$k])) {
440 continue;
441 }
442 foreach ($this->dat['records'][$k]['rels'] as $fieldname => $vR) {
443 // For all DB types of relations:
444 if ($vR['type'] === 'db') {
445 foreach ($vR['itemArray'] as $fI) {
446 $this->export_addDBRelations_registerRelation($fI, $addR);
447 }
448 }
449 // For all flex/db types of relations:
450 if ($vR['type'] === 'flex') {
451 // DB relations in flex form fields:
452 if (is_array($vR['flexFormRels']['db'])) {
453 foreach ($vR['flexFormRels']['db'] as $subList) {
454 foreach ($subList as $fI) {
455 $this->export_addDBRelations_registerRelation($fI, $addR);
456 }
457 }
458 }
459 // DB oriented soft references in flex form fields:
460 if (is_array($vR['flexFormRels']['softrefs'])) {
461 foreach ($vR['flexFormRels']['softrefs'] as $subList) {
462 foreach ($subList['keys'] as $spKey => $elements) {
463 foreach ($elements as $el) {
464 if ($el['subst']['type'] === 'db' && $this->includeSoftref($el['subst']['tokenID'])) {
465 list($tempTable, $tempUid) = explode(':', $el['subst']['recordRef']);
466 $fI = [
467 'table' => $tempTable,
468 'id' => $tempUid
469 ];
470 $this->export_addDBRelations_registerRelation($fI, $addR, $el['subst']['tokenID']);
471 }
472 }
473 }
474 }
475 }
476 }
477 // In any case, if there are soft refs:
478 if (is_array($vR['softrefs']['keys'])) {
479 foreach ($vR['softrefs']['keys'] as $spKey => $elements) {
480 foreach ($elements as $el) {
481 if ($el['subst']['type'] === 'db' && $this->includeSoftref($el['subst']['tokenID'])) {
482 list($tempTable, $tempUid) = explode(':', $el['subst']['recordRef']);
483 $fI = [
484 'table' => $tempTable,
485 'id' => $tempUid
486 ];
487 $this->export_addDBRelations_registerRelation($fI, $addR, $el['subst']['tokenID']);
488 }
489 }
490 }
491 }
492 }
493 }
494
495 // Now, if there were new records to add, do so:
496 if (!empty($addR)) {
497 foreach ($addR as $fI) {
498 // Get and set record:
499 $row = BackendUtility::getRecord($fI['table'], $fI['id']);
500 if (is_array($row)) {
501 $this->export_addRecord($fI['table'], $row, $relationLevel + 1);
502 }
503 // Set status message
504 // Relation pointers always larger than zero except certain "select" types with
505 // negative values pointing to uids - but that is not supported here.
506 if ($fI['id'] > 0) {
507 $rId = $fI['table'] . ':' . $fI['id'];
508 if (!isset($this->dat['records'][$rId])) {
509 $this->dat['records'][$rId] = 'NOT_FOUND';
510 $this->error('Relation record ' . $rId . ' was not found!');
511 }
512 }
513 }
514 }
515 // Return overview of relations found and added
516 return $addR;
517 }
518
519 /**
520 * Helper function for export_addDBRelations()
521 *
522 * @param array $fI Array with table/id keys to add
523 * @param array $addR Add array, passed by reference to be modified
524 * @param string $tokenID Softref Token ID, if applicable.
525 * @see export_addDBRelations()
526 */
527 public function export_addDBRelations_registerRelation($fI, &$addR, $tokenID = '')
528 {
529 $rId = $fI['table'] . ':' . $fI['id'];
530 if (
531 isset($GLOBALS['TCA'][$fI['table']]) && !$this->isTableStatic($fI['table']) && !$this->isExcluded($fI['table'], $fI['id'])
532 && (!$tokenID || $this->includeSoftref($tokenID)) && $this->inclRelation($fI['table'])
533 ) {
534 if (!isset($this->dat['records'][$rId])) {
535 // Set this record to be included since it is not already.
536 $addR[$rId] = $fI;
537 }
538 }
539 }
540
541 /**
542 * This adds all files in relations.
543 * Call this method AFTER adding all records including relations.
544 *
545 * @see export_addDBRelations()
546 */
547 public function export_addFilesFromRelations()
548 {
549 // Traverse all "rels" registered for "records"
550 if (!is_array($this->dat['records'])) {
551 $this->error('There were no records available.');
552 return;
553 }
554 foreach ($this->dat['records'] as $k => $value) {
555 if (!isset($this->dat['records'][$k]['rels']) || !is_array($this->dat['records'][$k]['rels'])) {
556 continue;
557 }
558 foreach ($this->dat['records'][$k]['rels'] as $fieldname => $vR) {
559 // For all file type relations:
560 if ($vR['type'] === 'file') {
561 foreach ($vR['newValueFiles'] as $key => $fI) {
562 $this->export_addFile($fI, $k, $fieldname);
563 // Remove the absolute reference to the file so it doesn't expose absolute paths from source server:
564 unset($this->dat['records'][$k]['rels'][$fieldname]['newValueFiles'][$key]['ID_absFile']);
565 }
566 }
567 // For all flex type relations:
568 if ($vR['type'] === 'flex') {
569 if (is_array($vR['flexFormRels']['file'])) {
570 foreach ($vR['flexFormRels']['file'] as $key => $subList) {
571 foreach ($subList as $subKey => $fI) {
572 $this->export_addFile($fI, $k, $fieldname);
573 // Remove the absolute reference to the file so it doesn't expose absolute paths from source server:
574 unset($this->dat['records'][$k]['rels'][$fieldname]['flexFormRels']['file'][$key][$subKey]['ID_absFile']);
575 }
576 }
577 }
578 // DB oriented soft references in flex form fields:
579 if (is_array($vR['flexFormRels']['softrefs'])) {
580 foreach ($vR['flexFormRels']['softrefs'] as $key => $subList) {
581 foreach ($subList['keys'] as $spKey => $elements) {
582 foreach ($elements as $subKey => $el) {
583 if ($el['subst']['type'] === 'file' && $this->includeSoftref($el['subst']['tokenID'])) {
584 // Create abs path and ID for file:
585 $ID_absFile = GeneralUtility::getFileAbsFileName(PATH_site . $el['subst']['relFileName']);
586 $ID = md5($el['subst']['relFileName']);
587 if ($ID_absFile) {
588 if (!$this->dat['files'][$ID]) {
589 $fI = [
590 'filename' => PathUtility::basename($ID_absFile),
591 'ID_absFile' => $ID_absFile,
592 'ID' => $ID,
593 'relFileName' => $el['subst']['relFileName']
594 ];
595 $this->export_addFile($fI, '_SOFTREF_');
596 }
597 $this->dat['records'][$k]['rels'][$fieldname]['flexFormRels']['softrefs'][$key]['keys'][$spKey][$subKey]['file_ID'] = $ID;
598 }
599 }
600 }
601 }
602 }
603 }
604 }
605 // In any case, if there are soft refs:
606 if (is_array($vR['softrefs']['keys'])) {
607 foreach ($vR['softrefs']['keys'] as $spKey => $elements) {
608 foreach ($elements as $subKey => $el) {
609 if ($el['subst']['type'] === 'file' && $this->includeSoftref($el['subst']['tokenID'])) {
610 // Create abs path and ID for file:
611 $ID_absFile = GeneralUtility::getFileAbsFileName(PATH_site . $el['subst']['relFileName']);
612 $ID = md5($el['subst']['relFileName']);
613 if ($ID_absFile) {
614 if (!$this->dat['files'][$ID]) {
615 $fI = [
616 'filename' => PathUtility::basename($ID_absFile),
617 'ID_absFile' => $ID_absFile,
618 'ID' => $ID,
619 'relFileName' => $el['subst']['relFileName']
620 ];
621 $this->export_addFile($fI, '_SOFTREF_');
622 }
623 $this->dat['records'][$k]['rels'][$fieldname]['softrefs']['keys'][$spKey][$subKey]['file_ID'] = $ID;
624 }
625 }
626 }
627 }
628 }
629 }
630 }
631 }
632
633 /**
634 * This adds all files from sys_file records
635 */
636 public function export_addFilesFromSysFilesRecords()
637 {
638 if (!isset($this->dat['header']['records']['sys_file']) || !is_array($this->dat['header']['records']['sys_file'])) {
639 return;
640 }
641 foreach ($this->dat['header']['records']['sys_file'] as $sysFileUid => $_) {
642 $recordData = $this->dat['records']['sys_file:' . $sysFileUid]['data'];
643 $file = ResourceFactory::getInstance()->createFileObject($recordData);
644 $this->export_addSysFile($file);
645 }
646 }
647
648 /**
649 * Adds a files content from a sys file record to the export memory
650 *
651 * @param File $file
652 */
653 public function export_addSysFile(File $file)
654 {
655 $fileContent = '';
656 try {
657 if (!$this->saveFilesOutsideExportFile) {
658 $fileContent = $file->getContents();
659 } else {
660 $file->checkActionPermission('read');
661 }
662 } catch (\Exception $e) {
663 $this->error('Error when trying to add file ' . $file->getCombinedIdentifier() . ': ' . $e->getMessage());
664 return;
665 }
666 $fileUid = $file->getUid();
667 $fileSha1 = $file->getStorage()->hashFile($file, 'sha1');
668 if ($fileSha1 !== $file->getProperty('sha1')) {
669 $this->error('File sha1 hash of ' . $file->getCombinedIdentifier() . ' is not up-to-date in index! File added on current sha1.');
670 $this->dat['records']['sys_file:' . $fileUid]['data']['sha1'] = $fileSha1;
671 }
672
673 $fileRec = [];
674 $fileRec['filename'] = $file->getProperty('name');
675 $fileRec['filemtime'] = $file->getProperty('modification_date');
676
677 // build unique id based on the storage and the file identifier
678 $fileId = md5($file->getStorage()->getUid() . ':' . $file->getProperty('identifier_hash'));
679
680 // Setting this data in the header
681 $this->dat['header']['files_fal'][$fileId] = $fileRec;
682
683 if (!$this->saveFilesOutsideExportFile) {
684 // ... and finally add the heavy stuff:
685 $fileRec['content'] = $fileContent;
686 } else {
687 GeneralUtility::upload_copy_move($file->getForLocalProcessing(false), $this->getTemporaryFilesPathForExport() . $file->getProperty('sha1'));
688 }
689 $fileRec['content_sha1'] = $fileSha1;
690
691 $this->dat['files_fal'][$fileId] = $fileRec;
692 }
693
694 /**
695 * Adds a files content to the export memory
696 *
697 * @param array $fI File information with three keys: "filename" = filename without path, "ID_absFile" = absolute filepath to the file (including the filename), "ID" = md5 hash of "ID_absFile". "relFileName" is optional for files attached to records, but mandatory for soft referenced files (since the relFileName determines where such a file should be stored!)
698 * @param string $recordRef If the file is related to a record, this is the id on the form [table]:[id]. Information purposes only.
699 * @param string $fieldname If the file is related to a record, this is the field name it was related to. Information purposes only.
700 */
701 public function export_addFile($fI, $recordRef = '', $fieldname = '')
702 {
703 if (!@is_file($fI['ID_absFile'])) {
704 $this->error($fI['ID_absFile'] . ' was not a file! Skipping.');
705 return;
706 }
707 $fileInfo = stat($fI['ID_absFile']);
708 $fileRec = [];
709 $fileRec['filename'] = PathUtility::basename($fI['ID_absFile']);
710 $fileRec['filemtime'] = $fileInfo['mtime'];
711 //for internal type file_reference
712 $fileRec['relFileRef'] = PathUtility::stripPathSitePrefix($fI['ID_absFile']);
713 if ($recordRef) {
714 $fileRec['record_ref'] = $recordRef . '/' . $fieldname;
715 }
716 if ($fI['relFileName']) {
717 $fileRec['relFileName'] = $fI['relFileName'];
718 }
719 // Setting this data in the header
720 $this->dat['header']['files'][$fI['ID']] = $fileRec;
721 // ... and for the recordlisting, why not let us know WHICH relations there was...
722 if ($recordRef && $recordRef !== '_SOFTREF_') {
723 $refParts = explode(':', $recordRef, 2);
724 if (!is_array($this->dat['header']['records'][$refParts[0]][$refParts[1]]['filerefs'])) {
725 $this->dat['header']['records'][$refParts[0]][$refParts[1]]['filerefs'] = [];
726 }
727 $this->dat['header']['records'][$refParts[0]][$refParts[1]]['filerefs'][] = $fI['ID'];
728 }
729 $fileMd5 = md5_file($fI['ID_absFile']);
730 if (!$this->saveFilesOutsideExportFile) {
731 // ... and finally add the heavy stuff:
732 $fileRec['content'] = file_get_contents($fI['ID_absFile']);
733 } else {
734 GeneralUtility::upload_copy_move($fI['ID_absFile'], $this->getTemporaryFilesPathForExport() . $fileMd5);
735 }
736 $fileRec['content_md5'] = $fileMd5;
737 $this->dat['files'][$fI['ID']] = $fileRec;
738 // For soft references, do further processing:
739 if ($recordRef === '_SOFTREF_') {
740 // RTE files?
741 if ($RTEoriginal = $this->getRTEoriginalFilename(PathUtility::basename($fI['ID_absFile']))) {
742 $RTEoriginal_absPath = PathUtility::dirname($fI['ID_absFile']) . '/' . $RTEoriginal;
743 if (@is_file($RTEoriginal_absPath)) {
744 $RTEoriginal_ID = md5($RTEoriginal_absPath);
745 $fileInfo = stat($RTEoriginal_absPath);
746 $fileRec = [];
747 $fileRec['filename'] = PathUtility::basename($RTEoriginal_absPath);
748 $fileRec['filemtime'] = $fileInfo['mtime'];
749 $fileRec['record_ref'] = '_RTE_COPY_ID:' . $fI['ID'];
750 $this->dat['header']['files'][$fI['ID']]['RTE_ORIG_ID'] = $RTEoriginal_ID;
751 // Setting this data in the header
752 $this->dat['header']['files'][$RTEoriginal_ID] = $fileRec;
753 $fileMd5 = md5_file($RTEoriginal_absPath);
754 if (!$this->saveFilesOutsideExportFile) {
755 // ... and finally add the heavy stuff:
756 $fileRec['content'] = file_get_contents($RTEoriginal_absPath);
757 } else {
758 GeneralUtility::upload_copy_move($RTEoriginal_absPath, $this->getTemporaryFilesPathForExport() . $fileMd5);
759 }
760 $fileRec['content_md5'] = $fileMd5;
761 $this->dat['files'][$RTEoriginal_ID] = $fileRec;
762 } else {
763 $this->error('RTE original file "' . PathUtility::stripPathSitePrefix($RTEoriginal_absPath) . '" was not found!');
764 }
765 }
766 // Files with external media?
767 // This is only done with files grabbed by a softreference parser since it is deemed improbable that hard-referenced files should undergo this treatment.
768 $html_fI = pathinfo(PathUtility::basename($fI['ID_absFile']));
769 if ($this->includeExtFileResources && GeneralUtility::inList($this->extFileResourceExtensions, strtolower($html_fI['extension']))) {
770 $uniquePrefix = '###' . md5($GLOBALS['EXEC_TIME']) . '###';
771 if (strtolower($html_fI['extension']) === 'css') {
772 $prefixedMedias = explode($uniquePrefix, preg_replace('/(url[[:space:]]*\\([[:space:]]*["\']?)([^"\')]*)(["\']?[[:space:]]*\\))/i', '\\1' . $uniquePrefix . '\\2' . $uniquePrefix . '\\3', $fileRec['content']));
773 } else {
774 // html, htm:
775 $htmlParser = GeneralUtility::makeInstance(HtmlParser::class);
776 $prefixedMedias = explode($uniquePrefix, $htmlParser->prefixResourcePath($uniquePrefix, $fileRec['content'], [], $uniquePrefix));
777 }
778 $htmlResourceCaptured = false;
779 foreach ($prefixedMedias as $k => $v) {
780 if ($k % 2) {
781 $EXTres_absPath = GeneralUtility::resolveBackPath(PathUtility::dirname($fI['ID_absFile']) . '/' . $v);
782 $EXTres_absPath = GeneralUtility::getFileAbsFileName($EXTres_absPath);
783 if ($EXTres_absPath && GeneralUtility::isFirstPartOfStr($EXTres_absPath, PATH_site . $this->fileadminFolderName . '/') && @is_file($EXTres_absPath)) {
784 $htmlResourceCaptured = true;
785 $EXTres_ID = md5($EXTres_absPath);
786 $this->dat['header']['files'][$fI['ID']]['EXT_RES_ID'][] = $EXTres_ID;
787 $prefixedMedias[$k] = '{EXT_RES_ID:' . $EXTres_ID . '}';
788 // Add file to memory if it is not set already:
789 if (!isset($this->dat['header']['files'][$EXTres_ID])) {
790 $fileInfo = stat($EXTres_absPath);
791 $fileRec = [];
792 $fileRec['filename'] = PathUtility::basename($EXTres_absPath);
793 $fileRec['filemtime'] = $fileInfo['mtime'];
794 $fileRec['record_ref'] = '_EXT_PARENT_:' . $fI['ID'];
795 // Media relative to the HTML file.
796 $fileRec['parentRelFileName'] = $v;
797 // Setting this data in the header
798 $this->dat['header']['files'][$EXTres_ID] = $fileRec;
799 // ... and finally add the heavy stuff:
800 $fileRec['content'] = file_get_contents($EXTres_absPath);
801 $fileRec['content_md5'] = md5($fileRec['content']);
802 $this->dat['files'][$EXTres_ID] = $fileRec;
803 }
804 }
805 }
806 }
807 if ($htmlResourceCaptured) {
808 $this->dat['files'][$fI['ID']]['tokenizedContent'] = implode('', $prefixedMedias);
809 }
810 }
811 }
812 }
813
814 /**
815 * If saveFilesOutsideExportFile is enabled, this function returns the path
816 * where the files referenced in the export are copied to.
817 *
818 * @return string
819 * @throws \RuntimeException
820 * @see setSaveFilesOutsideExportFile()
821 */
822 public function getTemporaryFilesPathForExport()
823 {
824 if (!$this->saveFilesOutsideExportFile) {
825 throw new \RuntimeException('You need to set saveFilesOutsideExportFile to TRUE before you want to get the temporary files path for export.', 1401205213);
826 }
827 if ($this->temporaryFilesPathForExport === null) {
828 $temporaryFolderName = $this->getTemporaryFolderName();
829 $this->temporaryFilesPathForExport = $temporaryFolderName . '/';
830 }
831 return $this->temporaryFilesPathForExport;
832 }
833
834 /**
835 * DB relations flattend to 1-dim array.
836 * The list will be unique, no table/uid combination will appear twice.
837 *
838 * @param array $dbrels 2-dim Array of database relations organized by table key
839 * @return array 1-dim array where entries are table:uid and keys are array with table/id
840 */
841 public function flatDBrels($dbrels)
842 {
843 $list = [];
844 foreach ($dbrels as $dat) {
845 if ($dat['type'] === 'db') {
846 foreach ($dat['itemArray'] as $i) {
847 $list[$i['table'] . ':' . $i['id']] = $i;
848 }
849 }
850 if ($dat['type'] === 'flex' && is_array($dat['flexFormRels']['db'])) {
851 foreach ($dat['flexFormRels']['db'] as $subList) {
852 foreach ($subList as $i) {
853 $list[$i['table'] . ':' . $i['id']] = $i;
854 }
855 }
856 }
857 }
858 return $list;
859 }
860
861 /**
862 * Soft References flattend to 1-dim array.
863 *
864 * @param array $dbrels 2-dim Array of database relations organized by table key
865 * @return array 1-dim array where entries are arrays with properties of the soft link found and keys are a unique combination of field, spKey, structure path if applicable and token ID
866 */
867 public function flatSoftRefs($dbrels)
868 {
869 $list = [];
870 foreach ($dbrels as $field => $dat) {
871 if (is_array($dat['softrefs']['keys'])) {
872 foreach ($dat['softrefs']['keys'] as $spKey => $elements) {
873 if (is_array($elements)) {
874 foreach ($elements as $subKey => $el) {
875 $lKey = $field . ':' . $spKey . ':' . $subKey;
876 $list[$lKey] = array_merge(['field' => $field, 'spKey' => $spKey], $el);
877 // Add file_ID key to header - slightly "risky" way of doing this because if the calculation
878 // changes for the same value in $this->records[...] this will not work anymore!
879 if ($el['subst'] && $el['subst']['relFileName']) {
880 $list[$lKey]['file_ID'] = md5(PATH_site . $el['subst']['relFileName']);
881 }
882 }
883 }
884 }
885 }
886 if ($dat['type'] === 'flex' && is_array($dat['flexFormRels']['softrefs'])) {
887 foreach ($dat['flexFormRels']['softrefs'] as $structurePath => $subSoftrefs) {
888 if (is_array($subSoftrefs['keys'])) {
889 foreach ($subSoftrefs['keys'] as $spKey => $elements) {
890 foreach ($elements as $subKey => $el) {
891 $lKey = $field . ':' . $structurePath . ':' . $spKey . ':' . $subKey;
892 $list[$lKey] = array_merge(['field' => $field, 'spKey' => $spKey, 'structurePath' => $structurePath], $el);
893 // Add file_ID key to header - slightly "risky" way of doing this because if the calculation
894 // changes for the same value in $this->records[...] this will not work anymore!
895 if ($el['subst'] && $el['subst']['relFileName']) {
896 $list[$lKey]['file_ID'] = md5(PATH_site . $el['subst']['relFileName']);
897 }
898 }
899 }
900 }
901 }
902 }
903 }
904 return $list;
905 }
906
907 /**
908 * If include fields for a specific record type are set, the data
909 * are filtered out with fields are not included in the fields.
910 *
911 * @param string $table The record type to be filtered
912 * @param array $row The data to be filtered
913 * @return array The filtered record row
914 */
915 protected function filterRecordFields($table, array $row)
916 {
917 if (isset($this->recordTypesIncludeFields[$table])) {
918 $includeFields = array_unique(array_merge(
919 $this->recordTypesIncludeFields[$table],
920 $this->defaultRecordIncludeFields
921 ));
922 $newRow = [];
923 foreach ($row as $key => $value) {
924 if (in_array($key, $includeFields)) {
925 $newRow[$key] = $value;
926 }
927 }
928 } else {
929 $newRow = $row;
930 }
931 return $newRow;
932 }
933
934 /**************************
935 * File Output
936 *************************/
937
938 /**
939 * This compiles and returns the data content for an exported file
940 *
941 * @param string $type Type of output; "xml" gives xml, otherwise serialized array, possibly compressed.
942 * @return string The output file stream
943 */
944 public function compileMemoryToFileContent($type = '')
945 {
946 if ($type === 'xml') {
947 $out = $this->createXML();
948 } else {
949 $compress = $this->doOutputCompress();
950 $out = '';
951 // adding header:
952 $out .= $this->addFilePart(serialize($this->dat['header']), $compress);
953 // adding records:
954 $out .= $this->addFilePart(serialize($this->dat['records']), $compress);
955 // adding files:
956 $out .= $this->addFilePart(serialize($this->dat['files']), $compress);
957 // adding files_fal:
958 $out .= $this->addFilePart(serialize($this->dat['files_fal']), $compress);
959 }
960 return $out;
961 }
962
963 /**
964 * Creates XML string from input array
965 *
966 * @return string XML content
967 */
968 public function createXML()
969 {
970 // Options:
971 $options = [
972 'alt_options' => [
973 '/header' => [
974 'disableTypeAttrib' => true,
975 'clearStackPath' => true,
976 'parentTagMap' => [
977 'files' => 'file',
978 'files_fal' => 'file',
979 'records' => 'table',
980 'table' => 'rec',
981 'rec:rels' => 'relations',
982 'relations' => 'element',
983 'filerefs' => 'file',
984 'pid_lookup' => 'page_contents',
985 'header:relStaticTables' => 'static_tables',
986 'static_tables' => 'tablename',
987 'excludeMap' => 'item',
988 'softrefCfg' => 'softrefExportMode',
989 'extensionDependencies' => 'extkey',
990 'softrefs' => 'softref_element'
991 ],
992 'alt_options' => [
993 '/pagetree' => [
994 'disableTypeAttrib' => true,
995 'useIndexTagForNum' => 'node',
996 'parentTagMap' => [
997 'node:subrow' => 'node'
998 ]
999 ],
1000 '/pid_lookup/page_contents' => [
1001 'disableTypeAttrib' => true,
1002 'parentTagMap' => [
1003 'page_contents' => 'table'
1004 ],
1005 'grandParentTagMap' => [
1006 'page_contents/table' => 'item'
1007 ]
1008 ]
1009 ]
1010 ],
1011 '/records' => [
1012 'disableTypeAttrib' => true,
1013 'parentTagMap' => [
1014 'records' => 'tablerow',
1015 'tablerow:data' => 'fieldlist',
1016 'tablerow:rels' => 'related',
1017 'related' => 'field',
1018 'field:itemArray' => 'relations',
1019 'field:newValueFiles' => 'filerefs',
1020 'field:flexFormRels' => 'flexform',
1021 'relations' => 'element',
1022 'filerefs' => 'file',
1023 'flexform:db' => 'db_relations',
1024 'flexform:file' => 'file_relations',
1025 'flexform:softrefs' => 'softref_relations',
1026 'softref_relations' => 'structurePath',
1027 'db_relations' => 'path',
1028 'file_relations' => 'path',
1029 'path' => 'element',
1030 'keys' => 'softref_key',
1031 'softref_key' => 'softref_element'
1032 ],
1033 'alt_options' => [
1034 '/records/tablerow/fieldlist' => [
1035 'useIndexTagForAssoc' => 'field'
1036 ]
1037 ]
1038 ],
1039 '/files' => [
1040 'disableTypeAttrib' => true,
1041 'parentTagMap' => [
1042 'files' => 'file'
1043 ]
1044 ],
1045 '/files_fal' => [
1046 'disableTypeAttrib' => true,
1047 'parentTagMap' => [
1048 'files_fal' => 'file'
1049 ]
1050 ]
1051 ]
1052 ];
1053 // Creating XML file from $outputArray:
1054 $charset = $this->dat['header']['charset'] ?: 'utf-8';
1055 $XML = '<?xml version="1.0" encoding="' . $charset . '" standalone="yes" ?>' . LF;
1056 $XML .= GeneralUtility::array2xml($this->dat, '', 0, 'T3RecordDocument', 0, $options);
1057 return $XML;
1058 }
1059
1060 /**
1061 * Returns TRUE if the output should be compressed.
1062 *
1063 * @return bool TRUE if compression is possible AND requested.
1064 */
1065 public function doOutputCompress()
1066 {
1067 return $this->compress && !$this->dontCompress;
1068 }
1069
1070 /**
1071 * Returns a content part for a filename being build.
1072 *
1073 * @param array $data Data to store in part
1074 * @param bool $compress Compress file?
1075 * @return string Content stream.
1076 */
1077 public function addFilePart($data, $compress = false)
1078 {
1079 if ($compress) {
1080 $data = gzcompress($data);
1081 }
1082 return md5($data) . ':' . ($compress ? '1' : '0') . ':' . str_pad(strlen($data), 10, '0', STR_PAD_LEFT) . ':' . $data . ':';
1083 }
1084 }