[!!!][TASK] Split service class of Import/Export (impexp)
[Packages/TYPO3.CMS.git] / typo3 / sysext / impexp / Classes / ImportExport.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\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Database\DatabaseConnection;
20 use TYPO3\CMS\Core\Imaging\Icon;
21 use TYPO3\CMS\Core\Imaging\IconFactory;
22 use TYPO3\CMS\Core\Utility\DebugUtility;
23 use TYPO3\CMS\Core\Utility\DiffUtility;
24 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
25 use TYPO3\CMS\Core\Utility\File\ExtendedFileUtility;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Core\Utility\PathUtility;
28 use TYPO3\CMS\Lang\LanguageService;
29
30 /**
31 * EXAMPLE for using the impexp-class for exporting stuff:
32 *
33 * Create and initialize:
34 * $this->export = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Impexp\ImportExport::class);
35 * $this->export->init();
36 * Set which tables relations we will allow:
37 * $this->export->relOnlyTables[]="tt_news"; // exclusively includes. See comment in the class
38 *
39 * Adding records:
40 * $this->export->export_addRecord("pages", $this->pageinfo);
41 * $this->export->export_addRecord("pages", \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord("pages", 38));
42 * $this->export->export_addRecord("pages", \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord("pages", 39));
43 * $this->export->export_addRecord("tt_content", \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord("tt_content", 12));
44 * $this->export->export_addRecord("tt_content", \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord("tt_content", 74));
45 * $this->export->export_addRecord("sys_template", \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord("sys_template", 20));
46 *
47 * Adding all the relations (recursively in 5 levels so relations has THEIR relations registered as well)
48 * for($a=0;$a<5;$a++) {
49 * $addR = $this->export->export_addDBRelations($a);
50 * if (empty($addR)) break;
51 * }
52 *
53 * Finally load all the files.
54 * $this->export->export_addFilesFromRelations(); // MUST be after the DBrelations are set so that file from ALL added records are included!
55 *
56 * Write export
57 * $out = $this->export->compileMemoryToFileContent();
58 */
59
60 /**
61 * T3D file Import/Export library (TYPO3 Record Document)
62 */
63 abstract class ImportExport
64 {
65 /**
66 * If set, static relations (not exported) will be shown in overview as well
67 *
68 * @var bool
69 */
70 public $showStaticRelations = false;
71
72 /**
73 * Name of the "fileadmin" folder where files for export/import should be located
74 *
75 * @var string
76 */
77 public $fileadminFolderName = '';
78
79 /**
80 * Whether "import" or "export" mode of object. Set through init() function
81 *
82 * @var string
83 */
84 public $mode = '';
85
86 /**
87 * Updates all records that has same UID instead of creating new!
88 *
89 * @var bool
90 */
91 public $update = false;
92
93 /**
94 * Is set by importData() when an import has been done.
95 *
96 * @var bool
97 */
98 public $doesImport = false;
99
100 /**
101 * If set to a page-record, then the preview display of the content will expect this page-record to be the target
102 * for the import and accordingly display validation information. This triggers the visual view of the
103 * import/export memory to validate if import is possible
104 *
105 * @var array
106 */
107 public $display_import_pid_record = array();
108
109 /**
110 * Setting import modes during update state: as_new, exclude, force_uid
111 *
112 * @var array
113 */
114 public $import_mode = array();
115
116 /**
117 * If set, PID correct is ignored globally
118 *
119 * @var bool
120 */
121 public $global_ignore_pid = false;
122
123 /**
124 * If set, all UID values are forced! (update or import)
125 *
126 * @var bool
127 */
128 public $force_all_UIDS = false;
129
130 /**
131 * If set, a diff-view column is added to the overview.
132 *
133 * @var bool
134 */
135 public $showDiff = false;
136
137 /**
138 * If set, and if the user is admin, allow the writing of PHP scripts to fileadmin/ area.
139 *
140 * @var bool
141 */
142 public $allowPHPScripts = false;
143
144 /**
145 * Array of values to substitute in editable softreferences.
146 *
147 * @var array
148 */
149 public $softrefInputValues = array();
150
151 /**
152 * Mapping between the fileID from import memory and the final filenames they are written to.
153 *
154 * @var array
155 */
156 public $fileIDMap = array();
157
158 /**
159 * Add table names here which are THE ONLY ones which will be included
160 * into export if found as relations. '_ALL' will allow all tables.
161 *
162 * @var array
163 */
164 public $relOnlyTables = array();
165
166 /**
167 * Add tables names here which should not be exported with the file.
168 * (Where relations should be mapped to same UIDs in target system).
169 *
170 * @var array
171 */
172 public $relStaticTables = array();
173
174 /**
175 * Exclude map. Keys are table:uid pairs and if set, records are not added to the export.
176 *
177 * @var array
178 */
179 public $excludeMap = array();
180
181 /**
182 * Soft Reference Token ID modes.
183 *
184 * @var array
185 */
186 public $softrefCfg = array();
187
188 /**
189 * Listing extension dependencies.
190 *
191 * @var array
192 */
193 public $extensionDependencies = array();
194
195 /**
196 * After records are written this array is filled with [table][original_uid] = [new_uid]
197 *
198 * @var array
199 */
200 public $import_mapId = array();
201
202 /**
203 * Error log.
204 *
205 * @var array
206 */
207 public $errorLog = array();
208
209 /**
210 * Cache for record paths
211 *
212 * @var array
213 */
214 public $cache_getRecordPath = array();
215
216 /**
217 * Cache of checkPID values.
218 *
219 * @var array
220 */
221 public $checkPID_cache = array();
222
223 /**
224 * Set internally if the gzcompress function exists
225 * Used by ImportExportController
226 *
227 * @var bool
228 */
229 public $compress = false;
230
231 /**
232 * Internal import/export memory
233 *
234 * @var array
235 */
236 public $dat = array();
237
238 /**
239 * File processing object
240 *
241 * @var ExtendedFileUtility
242 */
243 protected $fileProcObj = null;
244
245 /**
246 * @var array
247 */
248 protected $remainHeader = array();
249
250 /**
251 * @var IconFactory
252 */
253 protected $iconFactory;
254
255 /**
256 * The constructor
257 */
258 public function __construct()
259 {
260 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
261 }
262
263 /**************************
264 * Initialize
265 *************************/
266
267 /**
268 * Init the object, both import and export
269 *
270 * @return void
271 */
272 public function init()
273 {
274 $this->compress = function_exists('gzcompress');
275 $this->fileadminFolderName = !empty($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir']) ? rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') : 'fileadmin';
276 }
277
278 /********************************************************
279 * Visual rendering of import/export memory, $this->dat
280 ********************************************************/
281
282 /**
283 * Displays an overview of the header-content.
284 *
285 * @return string HTML content
286 */
287 public function displayContentOverview()
288 {
289 // Check extension dependencies:
290 if (is_array($this->dat['header']['extensionDependencies'])) {
291 foreach ($this->dat['header']['extensionDependencies'] as $extKey) {
292 if (!ExtensionManagementUtility::isLoaded($extKey)) {
293 $this->error('DEPENDENCY: The extension with key "' . $extKey . '" must be installed!');
294 }
295 }
296 }
297 // Probably this is done to save memory space?
298 unset($this->dat['files']);
299 $out = '';
300 // Traverse header:
301 if (is_array($this->dat['header'])) {
302 $this->remainHeader = $this->dat['header'];
303 // If there is a page tree set, show that:
304 $lang = $this->getLanguageService();
305 if (is_array($this->dat['header']['pagetree'])) {
306 reset($this->dat['header']['pagetree']);
307 $lines = array();
308 $this->traversePageTree($this->dat['header']['pagetree'], $lines);
309 $rows = array();
310 $rows[] = '
311 <tr class="bgColor5 tableheader">
312 <td>' . $lang->getLL('impexpcore_displaycon_controls', true) . '</td>
313 <td>' . $lang->getLL('impexpcore_displaycon_title', true) . '</td>
314 <td>' . $lang->getLL('impexpcore_displaycon_size', true) . '</td>
315 <td>' . $lang->getLL('impexpcore_displaycon_message', true) . '</td>
316 ' . ($this->update ? '<td>' . $lang->getLL('impexpcore_displaycon_updateMode', true) . '</td>' : '') . '
317 ' . ($this->update ? '<td>' . $lang->getLL('impexpcore_displaycon_currentPath', true) . '</td>' : '') . '
318 ' . ($this->showDiff ? '<td>' . $lang->getLL('impexpcore_displaycon_result', true) . '</td>' : '') . '
319 </tr>';
320 foreach ($lines as $r) {
321 $rows[] = '
322 <tr class="' . $r['class'] . '">
323 <td>' . $this->renderControls($r) . '</td>
324 <td nowrap="nowrap">' . $r['preCode'] . $r['title'] . '</td>
325 <td nowrap="nowrap">' . GeneralUtility::formatSize($r['size']) . '</td>
326 <td nowrap="nowrap">' . ($r['msg'] && !$this->doesImport ? '<span class="text-danger">' . htmlspecialchars($r['msg']) . '</span>' : '') . '</td>
327 ' . ($this->update ? '<td nowrap="nowrap">' . $r['updateMode'] . '</td>' : '') . '
328 ' . ($this->update ? '<td nowrap="nowrap">' . $r['updatePath'] . '</td>' : '') . '
329 ' . ($this->showDiff ? '<td>' . $r['showDiffContent'] . '</td>' : '') . '
330 </tr>';
331 }
332 $out = '
333 <strong>' . $lang->getLL('impexpcore_displaycon_insidePagetree', true) . '</strong>
334 <br /><br />
335 <table border="0" cellpadding="0" cellspacing="1">' . implode('', $rows) . '</table>
336 <br /><br />';
337 }
338 // Print remaining records that were not contained inside the page tree:
339 $lines = array();
340 if (is_array($this->remainHeader['records'])) {
341 if (is_array($this->remainHeader['records']['pages'])) {
342 $this->traversePageRecords($this->remainHeader['records']['pages'], $lines);
343 }
344 $this->traverseAllRecords($this->remainHeader['records'], $lines);
345 if (!empty($lines)) {
346 $rows = array();
347 $rows[] = '
348 <tr class="bgColor5 tableheader">
349 <td>' . $lang->getLL('impexpcore_displaycon_controls', true) . '</td>
350 <td>' . $lang->getLL('impexpcore_displaycon_title', true) . '</td>
351 <td>' . $lang->getLL('impexpcore_displaycon_size', true) . '</td>
352 <td>' . $lang->getLL('impexpcore_displaycon_message', true) . '</td>
353 ' . ($this->update ? '<td>' . $lang->getLL('impexpcore_displaycon_updateMode', true) . '</td>' : '') . '
354 ' . ($this->update ? '<td>' . $lang->getLL('impexpcore_displaycon_currentPath', true) . '</td>' : '') . '
355 ' . ($this->showDiff ? '<td>' . $lang->getLL('impexpcore_displaycon_result', true) . '</td>' : '') . '
356 </tr>';
357 foreach ($lines as $r) {
358 $rows[] = '<tr class="' . $r['class'] . '">
359 <td>' . $this->renderControls($r) . '</td>
360 <td nowrap="nowrap">' . $r['preCode'] . $r['title'] . '</td>
361 <td nowrap="nowrap">' . GeneralUtility::formatSize($r['size']) . '</td>
362 <td nowrap="nowrap">' . ($r['msg'] && !$this->doesImport ? '<span class="text-danger">' . htmlspecialchars($r['msg']) . '</span>' : '') . '</td>
363 ' . ($this->update ? '<td nowrap="nowrap">' . $r['updateMode'] . '</td>' : '') . '
364 ' . ($this->update ? '<td nowrap="nowrap">' . $r['updatePath'] . '</td>' : '') . '
365 ' . ($this->showDiff ? '<td>' . $r['showDiffContent'] . '</td>' : '') . '
366 </tr>';
367 }
368 $out .= '
369 <strong>' . $lang->getLL('impexpcore_singlereco_outsidePagetree', true) . '</strong>
370 <br /><br />
371 <table border="0" cellpadding="0" cellspacing="1">' . implode('', $rows) . '</table>';
372 }
373 }
374 }
375 return $out;
376 }
377
378 /**
379 * Go through page tree for display
380 *
381 * @param array $pT Page tree array with uid/subrow (from ->dat[header][pagetree]
382 * @param array $lines Output lines array (is passed by reference and modified)
383 * @param string $preCode Pre-HTML code
384 * @return void
385 */
386 public function traversePageTree($pT, &$lines, $preCode = '')
387 {
388 foreach ($pT as $k => $v) {
389 // Add this page:
390 $this->singleRecordLines('pages', $k, $lines, $preCode);
391 // Subrecords:
392 if (is_array($this->dat['header']['pid_lookup'][$k])) {
393 foreach ($this->dat['header']['pid_lookup'][$k] as $t => $recUidArr) {
394 if ($t != 'pages') {
395 foreach ($recUidArr as $ruid => $value) {
396 $this->singleRecordLines($t, $ruid, $lines, $preCode . '&nbsp;&nbsp;&nbsp;&nbsp;');
397 }
398 }
399 }
400 unset($this->remainHeader['pid_lookup'][$k]);
401 }
402 // Subpages, called recursively:
403 if (is_array($v['subrow'])) {
404 $this->traversePageTree($v['subrow'], $lines, $preCode . '&nbsp;&nbsp;&nbsp;&nbsp;');
405 }
406 }
407 }
408
409 /**
410 * Go through remaining pages (not in tree)
411 *
412 * @param array $pT Page tree array with uid/subrow (from ->dat[header][pagetree]
413 * @param array $lines Output lines array (is passed by reference and modified)
414 * @return void
415 */
416 public function traversePageRecords($pT, &$lines)
417 {
418 foreach ($pT as $k => $rHeader) {
419 $this->singleRecordLines('pages', $k, $lines, '', 1);
420 // Subrecords:
421 if (is_array($this->dat['header']['pid_lookup'][$k])) {
422 foreach ($this->dat['header']['pid_lookup'][$k] as $t => $recUidArr) {
423 if ($t != 'pages') {
424 foreach ($recUidArr as $ruid => $value) {
425 $this->singleRecordLines($t, $ruid, $lines, '&nbsp;&nbsp;&nbsp;&nbsp;');
426 }
427 }
428 }
429 unset($this->remainHeader['pid_lookup'][$k]);
430 }
431 }
432 }
433
434 /**
435 * Go through ALL records (if the pages are displayed first, those will not be amoung these!)
436 *
437 * @param array $pT Page tree array with uid/subrow (from ->dat[header][pagetree]
438 * @param array $lines Output lines array (is passed by reference and modified)
439 * @return void
440 */
441 public function traverseAllRecords($pT, &$lines)
442 {
443 foreach ($pT as $t => $recUidArr) {
444 $this->addGeneralErrorsByTable($t);
445 if ($t != 'pages') {
446 $preCode = '';
447 foreach ($recUidArr as $ruid => $value) {
448 $this->singleRecordLines($t, $ruid, $lines, $preCode, 1);
449 }
450 }
451 }
452 }
453
454 /**
455 * Log general error message for a given table
456 *
457 * @param string $table database table name
458 * @return void
459 */
460 protected function addGeneralErrorsByTable($table)
461 {
462 if ($this->update && $table === 'sys_file') {
463 $this->error('Updating sys_file records is not supported! They will be imported as new records!');
464 }
465 if ($this->force_all_UIDS && $table === 'sys_file') {
466 $this->error('Forcing uids of sys_file records is not supported! They will be imported as new records!');
467 }
468 }
469
470 /**
471 * Add entries for a single record
472 *
473 * @param string $table Table name
474 * @param int $uid Record uid
475 * @param array $lines Output lines array (is passed by reference and modified)
476 * @param string $preCode Pre-HTML code
477 * @param bool $checkImportInPidRecord If you want import validation, you can set this so it checks if the import can take place on the specified page.
478 * @return void
479 */
480 public function singleRecordLines($table, $uid, &$lines, $preCode, $checkImportInPidRecord = false)
481 {
482 // Get record:
483 $record = $this->dat['header']['records'][$table][$uid];
484 unset($this->remainHeader['records'][$table][$uid]);
485 if (!is_array($record) && !($table === 'pages' && !$uid)) {
486 $this->error('MISSING RECORD: ' . $table . ':' . $uid);
487 }
488 // Begin to create the line arrays information record, pInfo:
489 $pInfo = array();
490 $pInfo['ref'] = $table . ':' . $uid;
491 // Unknown table name:
492 $lang = $this->getLanguageService();
493 if ($table === '_SOFTREF_') {
494 $pInfo['preCode'] = $preCode;
495 $pInfo['title'] = '<em>' . $lang->getLL('impexpcore_singlereco_softReferencesFiles', true) . '</em>';
496 } elseif (!isset($GLOBALS['TCA'][$table])) {
497 // Unknown table name:
498 $pInfo['preCode'] = $preCode;
499 $pInfo['msg'] = 'UNKNOWN TABLE \'' . $pInfo['ref'] . '\'';
500 $pInfo['title'] = '<em>' . htmlspecialchars($record['title']) . '</em>';
501 } else {
502 // Otherwise, set table icon and title.
503 // Import Validation (triggered by $this->display_import_pid_record) will show messages if import is not possible of various items.
504 if (is_array($this->display_import_pid_record) && !empty($this->display_import_pid_record)) {
505 if ($checkImportInPidRecord) {
506 if (!$this->getBackendUser()->doesUserHaveAccess($this->display_import_pid_record, ($table === 'pages' ? 8 : 16))) {
507 $pInfo['msg'] .= '\'' . $pInfo['ref'] . '\' cannot be INSERTED on this page! ';
508 }
509 if (!$this->checkDokType($table, $this->display_import_pid_record['doktype']) && !$GLOBALS['TCA'][$table]['ctrl']['rootLevel']) {
510 $pInfo['msg'] .= '\'' . $table . '\' cannot be INSERTED on this page type (change page type to \'Folder\'.) ';
511 }
512 }
513 if (!$this->getBackendUser()->check('tables_modify', $table)) {
514 $pInfo['msg'] .= 'You are not allowed to CREATE \'' . $table . '\' tables! ';
515 }
516 if ($GLOBALS['TCA'][$table]['ctrl']['readOnly']) {
517 $pInfo['msg'] .= 'TABLE \'' . $table . '\' is READ ONLY! ';
518 }
519 if ($GLOBALS['TCA'][$table]['ctrl']['adminOnly'] && !$this->getBackendUser()->isAdmin()) {
520 $pInfo['msg'] .= 'TABLE \'' . $table . '\' is ADMIN ONLY! ';
521 }
522 if ($GLOBALS['TCA'][$table]['ctrl']['is_static']) {
523 $pInfo['msg'] .= 'TABLE \'' . $table . '\' is a STATIC TABLE! ';
524 }
525 if ((int)$GLOBALS['TCA'][$table]['ctrl']['rootLevel'] === 1) {
526 $pInfo['msg'] .= 'TABLE \'' . $table . '\' will be inserted on ROOT LEVEL! ';
527 }
528 $diffInverse = false;
529 $recInf = null;
530 if ($this->update) {
531 // In case of update-PREVIEW we swap the diff-sources.
532 $diffInverse = true;
533 $recInf = $this->doesRecordExist($table, $uid, $this->showDiff ? '*' : '');
534 $pInfo['updatePath'] = $recInf ? htmlspecialchars($this->getRecordPath($recInf['pid'])) : '<strong>NEW!</strong>';
535 // Mode selector:
536 $optValues = array();
537 $optValues[] = $recInf ? $lang->getLL('impexpcore_singlereco_update') : $lang->getLL('impexpcore_singlereco_insert');
538 if ($recInf) {
539 $optValues['as_new'] = $lang->getLL('impexpcore_singlereco_importAsNew');
540 }
541 if ($recInf) {
542 if (!$this->global_ignore_pid) {
543 $optValues['ignore_pid'] = $lang->getLL('impexpcore_singlereco_ignorePid');
544 } else {
545 $optValues['respect_pid'] = $lang->getLL('impexpcore_singlereco_respectPid');
546 }
547 }
548 if (!$recInf && $this->getBackendUser()->isAdmin()) {
549 $optValues['force_uid'] = sprintf($lang->getLL('impexpcore_singlereco_forceUidSAdmin'), $uid);
550 }
551 $optValues['exclude'] = $lang->getLL('impexpcore_singlereco_exclude');
552 if ($table === 'sys_file') {
553 $pInfo['updateMode'] = '';
554 } else {
555 $pInfo['updateMode'] = $this->renderSelectBox('tx_impexp[import_mode][' . $table . ':' . $uid . ']', $this->import_mode[$table . ':' . $uid], $optValues);
556 }
557 }
558 // Diff view:
559 if ($this->showDiff) {
560 // For IMPORTS, get new id:
561 if ($newUid = $this->import_mapId[$table][$uid]) {
562 $diffInverse = false;
563 $recInf = $this->doesRecordExist($table, $newUid, '*');
564 BackendUtility::workspaceOL($table, $recInf);
565 }
566 if (is_array($recInf)) {
567 $pInfo['showDiffContent'] = $this->compareRecords($recInf, $this->dat['records'][$table . ':' . $uid]['data'], $table, $diffInverse);
568 }
569 }
570 }
571 $pInfo['preCode'] = $preCode . '<span title="' . htmlspecialchars($table . ':' . $uid) . '">'
572 . $this->iconFactory->getIconForRecord($table, (array)$this->dat['records'][$table . ':' . $uid]['data'], Icon::SIZE_SMALL)->render()
573 . '</span>';
574 $pInfo['title'] = htmlspecialchars($record['title']);
575 // View page:
576 if ($table === 'pages') {
577 $viewID = $this->mode === 'export' ? $uid : ($this->doesImport ? $this->import_mapId['pages'][$uid] : 0);
578 if ($viewID) {
579 $pInfo['title'] = '<a href="#" onclick="' . htmlspecialchars(BackendUtility::viewOnClick($viewID)) . 'return false;">' . $pInfo['title'] . '</a>';
580 }
581 }
582 }
583 $pInfo['class'] = $table == 'pages' ? 'bgColor4-20' : 'bgColor4';
584 $pInfo['type'] = 'record';
585 $pInfo['size'] = $record['size'];
586 $lines[] = $pInfo;
587 // File relations:
588 if (is_array($record['filerefs'])) {
589 $this->addFiles($record['filerefs'], $lines, $preCode);
590 }
591 // DB relations
592 if (is_array($record['rels'])) {
593 $this->addRelations($record['rels'], $lines, $preCode);
594 }
595 // Soft ref
596 if (!empty($record['softrefs'])) {
597 $preCode_A = $preCode . '&nbsp;&nbsp;&nbsp;&nbsp;';
598 $preCode_B = $preCode . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
599 foreach ($record['softrefs'] as $info) {
600 $pInfo = array();
601 $pInfo['preCode'] = $preCode_A . $this->iconFactory->getIcon('status-status-reference-soft', Icon::SIZE_SMALL)->render();
602 $pInfo['title'] = '<em>' . $info['field'] . ', "' . $info['spKey'] . '" </em>: <span title="' . htmlspecialchars($info['matchString']) . '">' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($info['matchString'], 60)) . '</span>';
603 if ($info['subst']['type']) {
604 if (strlen($info['subst']['title'])) {
605 $pInfo['title'] .= '<br/>' . $preCode_B . '<strong>' . $lang->getLL('impexpcore_singlereco_title', true) . '</strong> ' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($info['subst']['title'], 60));
606 }
607 if (strlen($info['subst']['description'])) {
608 $pInfo['title'] .= '<br/>' . $preCode_B . '<strong>' . $lang->getLL('impexpcore_singlereco_descr', true) . '</strong> ' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($info['subst']['description'], 60));
609 }
610 $pInfo['title'] .= '<br/>' . $preCode_B . ($info['subst']['type'] == 'file' ? $lang->getLL('impexpcore_singlereco_filename', true) . ' <strong>' . $info['subst']['relFileName'] . '</strong>' : '') . ($info['subst']['type'] == 'string' ? $lang->getLL('impexpcore_singlereco_value', true) . ' <strong>' . $info['subst']['tokenValue'] . '</strong>' : '') . ($info['subst']['type'] == 'db' ? $lang->getLL('impexpcore_softrefsel_record', true) . ' <strong>' . $info['subst']['recordRef'] . '</strong>' : '');
611 }
612 $pInfo['ref'] = 'SOFTREF';
613 $pInfo['size'] = '';
614 $pInfo['class'] = 'bgColor3';
615 $pInfo['type'] = 'softref';
616 $pInfo['_softRefInfo'] = $info;
617 $pInfo['type'] = 'softref';
618 $mode = $this->softrefCfg[$info['subst']['tokenID']]['mode'];
619 if ($info['error'] && $mode !== 'editable' && $mode !== 'exclude') {
620 $pInfo['msg'] .= $info['error'];
621 }
622 $lines[] = $pInfo;
623 // Add relations:
624 if ($info['subst']['type'] == 'db') {
625 list($tempTable, $tempUid) = explode(':', $info['subst']['recordRef']);
626 $this->addRelations(array(array('table' => $tempTable, 'id' => $tempUid, 'tokenID' => $info['subst']['tokenID'])), $lines, $preCode_B, array(), '');
627 }
628 // Add files:
629 if ($info['subst']['type'] == 'file') {
630 $this->addFiles(array($info['file_ID']), $lines, $preCode_B, '', $info['subst']['tokenID']);
631 }
632 }
633 }
634 }
635
636 /**
637 * Add DB relations entries for a record's rels-array
638 *
639 * @param array $rels Array of relations
640 * @param array $lines Output lines array (is passed by reference and modified)
641 * @param string $preCode Pre-HTML code
642 * @param array $recurCheck Recursivity check stack
643 * @param string $htmlColorClass Alternative HTML color class to use.
644 * @return void
645 * @access private
646 * @see singleRecordLines()
647 */
648 public function addRelations($rels, &$lines, $preCode, $recurCheck = array(), $htmlColorClass = '')
649 {
650 foreach ($rels as $dat) {
651 $table = $dat['table'];
652 $uid = $dat['id'];
653 $pInfo = array();
654 $pInfo['ref'] = $table . ':' . $uid;
655 if (in_array($pInfo['ref'], $recurCheck)) {
656 continue;
657 }
658 $iconName = 'status-status-checked';
659 $iconClass = '';
660 $staticFixed = false;
661 $record = null;
662 if ($uid > 0) {
663 $record = $this->dat['header']['records'][$table][$uid];
664 if (!is_array($record)) {
665 if ($this->isTableStatic($table) || $this->isExcluded($table, $uid) || $dat['tokenID'] && !$this->includeSoftref($dat['tokenID'])) {
666 $pInfo['title'] = htmlspecialchars('STATIC: ' . $pInfo['ref']);
667 $iconClass = 'text-info';
668 $staticFixed = true;
669 } else {
670 $doesRE = $this->doesRecordExist($table, $uid);
671 $lostPath = $this->getRecordPath($table === 'pages' ? $doesRE['uid'] : $doesRE['pid']);
672 $pInfo['title'] = htmlspecialchars($pInfo['ref']);
673 $pInfo['title'] = '<span title="' . htmlspecialchars($lostPath) . '">' . $pInfo['title'] . '</span>';
674 $pInfo['msg'] = 'LOST RELATION' . (!$doesRE ? ' (Record not found!)' : ' (Path: ' . $lostPath . ')');
675 $iconClass = 'text-danger';
676 $iconName = 'status-dialog-warning';
677 }
678 } else {
679 $pInfo['title'] = htmlspecialchars($record['title']);
680 $pInfo['title'] = '<span title="' . htmlspecialchars($this->getRecordPath(($table === 'pages' ? $record['uid'] : $record['pid']))) . '">' . $pInfo['title'] . '</span>';
681 }
682 } else {
683 // Negative values in relation fields. This is typically sys_language fields, fe_users fields etc. They are static values. They CAN theoretically be negative pointers to uids in other tables but this is so rarely used that it is not supported
684 $pInfo['title'] = htmlspecialchars('FIXED: ' . $pInfo['ref']);
685 $staticFixed = true;
686 }
687
688 $icon = '<span class="' . $iconClass . '" title="' . htmlspecialchars($pInfo['ref']) . '">' . $this->iconFactory->getIcon($iconName, Icon::SIZE_SMALL)->render() . '</span>';
689
690 $pInfo['preCode'] = $preCode . '&nbsp;&nbsp;&nbsp;&nbsp;' . $icon;
691 $pInfo['class'] = $htmlColorClass ?: 'bgColor3';
692 $pInfo['type'] = 'rel';
693 if (!$staticFixed || $this->showStaticRelations) {
694 $lines[] = $pInfo;
695 if (is_array($record) && is_array($record['rels'])) {
696 $this->addRelations($record['rels'], $lines, $preCode . '&nbsp;&nbsp;', array_merge($recurCheck, array($pInfo['ref'])), $htmlColorClass);
697 }
698 }
699 }
700 }
701
702 /**
703 * Add file relation entries for a record's rels-array
704 *
705 * @param array $rels Array of file IDs
706 * @param array $lines Output lines array (is passed by reference and modified)
707 * @param string $preCode Pre-HTML code
708 * @param string $htmlColorClass Alternative HTML color class to use.
709 * @param string $tokenID Token ID if this is a softreference (in which case it only makes sense with a single element in the $rels array!)
710 * @return void
711 * @access private
712 * @see singleRecordLines()
713 */
714 public function addFiles($rels, &$lines, $preCode, $htmlColorClass = '', $tokenID = '')
715 {
716 foreach ($rels as $ID) {
717 // Process file:
718 $pInfo = array();
719 $fI = $this->dat['header']['files'][$ID];
720 if (!is_array($fI)) {
721 if (!$tokenID || $this->includeSoftref($tokenID)) {
722 $pInfo['msg'] = 'MISSING FILE: ' . $ID;
723 $this->error('MISSING FILE: ' . $ID);
724 } else {
725 return;
726 }
727 }
728 $pInfo['preCode'] = $preCode . '&nbsp;&nbsp;&nbsp;&nbsp;' . $this->iconFactory->getIcon('status-status-reference-hard', Icon::SIZE_SMALL)->render();
729 $pInfo['title'] = htmlspecialchars($fI['filename']);
730 $pInfo['ref'] = 'FILE';
731 $pInfo['size'] = $fI['filesize'];
732 $pInfo['class'] = $htmlColorClass ?: 'bgColor3';
733 $pInfo['type'] = 'file';
734 // If import mode and there is a non-RTE softreference, check the destination directory:
735 if ($this->mode === 'import' && $tokenID && !$fI['RTE_ORIG_ID']) {
736 if (isset($fI['parentRelFileName'])) {
737 $pInfo['msg'] = 'Seems like this file is already referenced from within an HTML/CSS file. That takes precedence. ';
738 } else {
739 $testDirPrefix = PathUtility::dirname($fI['relFileName']) . '/';
740 $testDirPrefix2 = $this->verifyFolderAccess($testDirPrefix);
741 if (!$testDirPrefix2) {
742 $pInfo['msg'] = 'ERROR: There are no available filemounts to write file in! ';
743 } elseif ($testDirPrefix !== $testDirPrefix2) {
744 $pInfo['msg'] = 'File will be attempted written to "' . $testDirPrefix2 . '". ';
745 }
746 }
747 // Check if file exists:
748 if (file_exists(PATH_site . $fI['relFileName'])) {
749 if ($this->update) {
750 $pInfo['updatePath'] .= 'File exists.';
751 } else {
752 $pInfo['msg'] .= 'File already exists! ';
753 }
754 }
755 // Check extension:
756 $fileProcObj = $this->getFileProcObj();
757 if ($fileProcObj->actionPerms['addFile']) {
758 $testFI = GeneralUtility::split_fileref(PATH_site . $fI['relFileName']);
759 if (!$this->allowPHPScripts && !$fileProcObj->checkIfAllowed($testFI['fileext'], $testFI['path'], $testFI['file'])) {
760 $pInfo['msg'] .= 'File extension was not allowed!';
761 }
762 } else {
763 $pInfo['msg'] = 'You user profile does not allow you to create files on the server!';
764 }
765 }
766 $pInfo['showDiffContent'] = PathUtility::stripPathSitePrefix($this->fileIDMap[$ID]);
767 $lines[] = $pInfo;
768 unset($this->remainHeader['files'][$ID]);
769 // RTE originals:
770 if ($fI['RTE_ORIG_ID']) {
771 $ID = $fI['RTE_ORIG_ID'];
772 $pInfo = array();
773 $fI = $this->dat['header']['files'][$ID];
774 if (!is_array($fI)) {
775 $pInfo['msg'] = 'MISSING RTE original FILE: ' . $ID;
776 $this->error('MISSING RTE original FILE: ' . $ID);
777 }
778 $pInfo['showDiffContent'] = PathUtility::stripPathSitePrefix($this->fileIDMap[$ID]);
779 $pInfo['preCode'] = $preCode . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $this->iconFactory->getIcon('status-status-reference-hard', Icon::SIZE_SMALL)->render();
780 $pInfo['title'] = htmlspecialchars($fI['filename']) . ' <em>(Original)</em>';
781 $pInfo['ref'] = 'FILE';
782 $pInfo['size'] = $fI['filesize'];
783 $pInfo['class'] = $htmlColorClass ?: 'bgColor3';
784 $pInfo['type'] = 'file';
785 $lines[] = $pInfo;
786 unset($this->remainHeader['files'][$ID]);
787 }
788 // External resources:
789 if (is_array($fI['EXT_RES_ID'])) {
790 foreach ($fI['EXT_RES_ID'] as $extID) {
791 $pInfo = array();
792 $fI = $this->dat['header']['files'][$extID];
793 if (!is_array($fI)) {
794 $pInfo['msg'] = 'MISSING External Resource FILE: ' . $extID;
795 $this->error('MISSING External Resource FILE: ' . $extID);
796 } else {
797 $pInfo['updatePath'] = $fI['parentRelFileName'];
798 }
799 $pInfo['showDiffContent'] = PathUtility::stripPathSitePrefix($this->fileIDMap[$extID]);
800 $pInfo['preCode'] = $preCode . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $this->iconFactory->getIcon('actions-insert-reference', Icon::SIZE_SMALL)->render();
801 $pInfo['title'] = htmlspecialchars($fI['filename']) . ' <em>(Resource)</em>';
802 $pInfo['ref'] = 'FILE';
803 $pInfo['size'] = $fI['filesize'];
804 $pInfo['class'] = $htmlColorClass ?: 'bgColor3';
805 $pInfo['type'] = 'file';
806 $lines[] = $pInfo;
807 unset($this->remainHeader['files'][$extID]);
808 }
809 }
810 }
811 }
812
813 /**
814 * Verifies that a table is allowed on a certain doktype of a page
815 *
816 * @param string $checkTable Table name to check
817 * @param int $doktype doktype value.
818 * @return bool TRUE if OK
819 */
820 public function checkDokType($checkTable, $doktype)
821 {
822 $allowedTableList = isset($GLOBALS['PAGES_TYPES'][$doktype]['allowedTables']) ? $GLOBALS['PAGES_TYPES'][$doktype]['allowedTables'] : $GLOBALS['PAGES_TYPES']['default']['allowedTables'];
823 $allowedArray = GeneralUtility::trimExplode(',', $allowedTableList, true);
824 // If all tables or the table is listed as an allowed type, return TRUE
825 if (strstr($allowedTableList, '*') || in_array($checkTable, $allowedArray)) {
826 return true;
827 }
828 return false;
829 }
830
831 /**
832 * Render input controls for import or export
833 *
834 * @param array $r Configuration for element
835 * @return string HTML
836 */
837 public function renderControls($r)
838 {
839 if ($this->mode === 'export') {
840 if ($r['type'] === 'record') {
841 return '<input type="checkbox" name="tx_impexp[exclude][' . $r['ref'] . ']" id="checkExclude' . $r['ref'] . '" value="1" /> <label for="checkExclude' . $r['ref'] . '">' . $this->getLanguageService()->getLL('impexpcore_singlereco_exclude', true) . '</label>';
842 } else {
843 return $r['type'] == 'softref' ? $this->softrefSelector($r['_softRefInfo']) : '';
844 }
845 } else {
846 // During import
847 // For softreferences with editable fields:
848 if ($r['type'] == 'softref' && is_array($r['_softRefInfo']['subst']) && $r['_softRefInfo']['subst']['tokenID']) {
849 $tokenID = $r['_softRefInfo']['subst']['tokenID'];
850 $cfg = $this->softrefCfg[$tokenID];
851 if ($cfg['mode'] === 'editable') {
852 return (strlen($cfg['title']) ? '<strong>' . htmlspecialchars($cfg['title']) . '</strong><br/>' : '') . htmlspecialchars($cfg['description']) . '<br/>
853 <input type="text" name="tx_impexp[softrefInputValues][' . $tokenID . ']" value="' . htmlspecialchars((isset($this->softrefInputValues[$tokenID]) ? $this->softrefInputValues[$tokenID] : $cfg['defValue'])) . '" />';
854 }
855 }
856 }
857 return '';
858 }
859
860 /**
861 * Selectorbox with export options for soft references
862 *
863 * @param array $cfg Softref configuration array. An export box is shown only if a substitution scheme is found for the soft reference.
864 * @return string Selector box HTML
865 */
866 public function softrefSelector($cfg)
867 {
868 // Looking for file ID if any:
869 $fI = $cfg['file_ID'] ? $this->dat['header']['files'][$cfg['file_ID']] : array();
870 // Substitution scheme has to be around and RTE images MUST be exported.
871 if (is_array($cfg['subst']) && $cfg['subst']['tokenID'] && !$fI['RTE_ORIG_ID']) {
872 // Create options:
873 $optValues = array();
874 $optValues[''] = '';
875 $optValues['editable'] = $this->getLanguageService()->getLL('impexpcore_softrefsel_editable');
876 $optValues['exclude'] = $this->getLanguageService()->getLL('impexpcore_softrefsel_exclude');
877 // Get current value:
878 $value = $this->softrefCfg[$cfg['subst']['tokenID']]['mode'];
879 // Render options selector:
880 $selectorbox = $this->renderSelectBox(('tx_impexp[softrefCfg][' . $cfg['subst']['tokenID'] . '][mode]'), $value, $optValues) . '<br/>';
881 if ($value === 'editable') {
882 $descriptionField = '';
883 // Title:
884 if (strlen($cfg['subst']['title'])) {
885 $descriptionField .= '
886 <input type="hidden" name="tx_impexp[softrefCfg][' . $cfg['subst']['tokenID'] . '][title]" value="' . htmlspecialchars($cfg['subst']['title']) . '" />
887 <strong>' . htmlspecialchars($cfg['subst']['title']) . '</strong><br/>';
888 }
889 // Description:
890 if (!strlen($cfg['subst']['description'])) {
891 $descriptionField .= '
892 ' . $this->getLanguageService()->getLL('impexpcore_printerror_description', true) . '<br/>
893 <input type="text" name="tx_impexp[softrefCfg][' . $cfg['subst']['tokenID'] . '][description]" value="' . htmlspecialchars($this->softrefCfg[$cfg['subst']['tokenID']]['description']) . '" />';
894 } else {
895 $descriptionField .= '
896
897 <input type="hidden" name="tx_impexp[softrefCfg][' . $cfg['subst']['tokenID'] . '][description]" value="' . htmlspecialchars($cfg['subst']['description']) . '" />' . htmlspecialchars($cfg['subst']['description']);
898 }
899 // Default Value:
900 $descriptionField .= '<input type="hidden" name="tx_impexp[softrefCfg][' . $cfg['subst']['tokenID'] . '][defValue]" value="' . htmlspecialchars($cfg['subst']['tokenValue']) . '" />';
901 } else {
902 $descriptionField = '';
903 }
904 return $selectorbox . $descriptionField;
905 }
906 return '';
907 }
908
909 /**
910 * Verifies that the input path (relative to PATH_site) is found in the backend users filemounts.
911 * If it doesn't it will try to find another relative filemount for the user and return an alternative path prefix for the file.
912 *
913 * @param string $dirPrefix Path relative to PATH_site
914 * @param bool $noAlternative If set, Do not look for alternative path! Just return FALSE
915 * @return string|bool If a path is available that will be returned, otherwise FALSE.
916 */
917 public function verifyFolderAccess($dirPrefix, $noAlternative = false)
918 {
919 $fileProcObj = $this->getFileProcObj();
920 // Check, if dirPrefix is inside a valid Filemount for user:
921 $result = $fileProcObj->checkPathAgainstMounts(PATH_site . $dirPrefix);
922 // If not, try to find another relative filemount and use that instead:
923 if (!$result) {
924 if ($noAlternative) {
925 return false;
926 }
927 // Find first web folder:
928 $result = $fileProcObj->findFirstWebFolder();
929 // If that succeeded, return the path to it:
930 if ($result) {
931 // Remove the "fileadmin/" prefix of input path - and append the rest to the return value:
932 if (GeneralUtility::isFirstPartOfStr($dirPrefix, $this->fileadminFolderName . '/')) {
933 $dirPrefix = substr($dirPrefix, strlen($this->fileadminFolderName . '/'));
934 }
935 return PathUtility::stripPathSitePrefix($fileProcObj->mounts[$result]['path'] . $dirPrefix);
936 }
937 } else {
938 return $dirPrefix;
939 }
940 return false;
941 }
942
943 /*****************************
944 * Helper functions of kinds
945 *****************************/
946
947 /**
948 *
949 * @return string
950 */
951 protected function getTemporaryFolderName()
952 {
953 $temporaryPath = PATH_site . 'typo3temp/';
954 do {
955 $temporaryFolderName = $temporaryPath . 'export_temp_files_' . mt_rand(1, PHP_INT_MAX);
956 } while (is_dir($temporaryFolderName));
957 GeneralUtility::mkdir($temporaryFolderName);
958 return $temporaryFolderName;
959 }
960
961 /**
962 * Recursively flattening the idH array
963 *
964 * @param array $idH Page uid hierarchy
965 * @param array $a Accumulation array of pages (internal, don't set from outside)
966 * @return array Array with uid-uid pairs for all pages in the page tree.
967 * @see Import::flatInversePageTree_pid()
968 */
969 public function flatInversePageTree($idH, $a = array())
970 {
971 if (is_array($idH)) {
972 $idH = array_reverse($idH);
973 foreach ($idH as $k => $v) {
974 $a[$v['uid']] = $v['uid'];
975 if (is_array($v['subrow'])) {
976 $a = $this->flatInversePageTree($v['subrow'], $a);
977 }
978 }
979 }
980 return $a;
981 }
982
983 /**
984 * Returns TRUE if the input table name is to be regarded as a static relation (that is, not exported etc).
985 *
986 * @param string $table Table name
987 * @return bool TRUE, if table is marked static
988 */
989 public function isTableStatic($table)
990 {
991 if (is_array($GLOBALS['TCA'][$table])) {
992 return $GLOBALS['TCA'][$table]['ctrl']['is_static'] || in_array($table, $this->relStaticTables) || in_array('_ALL', $this->relStaticTables);
993 }
994 return false;
995 }
996
997 /**
998 * Returns TRUE if the input table name is to be included as relation
999 *
1000 * @param string $table Table name
1001 * @return bool TRUE, if table is marked static
1002 */
1003 public function inclRelation($table)
1004 {
1005 return is_array($GLOBALS['TCA'][$table])
1006 && (in_array($table, $this->relOnlyTables) || in_array('_ALL', $this->relOnlyTables))
1007 && $this->getBackendUser()->check('tables_select', $table);
1008 }
1009
1010 /**
1011 * Returns TRUE if the element should be excluded as static record.
1012 *
1013 * @param string $table Table name
1014 * @param int $uid UID value
1015 * @return bool TRUE, if table is marked static
1016 */
1017 public function isExcluded($table, $uid)
1018 {
1019 return (bool)$this->excludeMap[$table . ':' . $uid];
1020 }
1021
1022 /**
1023 * Returns TRUE if soft reference should be included in exported file.
1024 *
1025 * @param string $tokenID Token ID for soft reference
1026 * @return bool TRUE if softreference media should be included
1027 */
1028 public function includeSoftref($tokenID)
1029 {
1030 $mode = $this->softrefCfg[$tokenID]['mode'];
1031 return $tokenID && $mode !== 'exclude' && $mode !== 'editable';
1032 }
1033
1034 /**
1035 * Checking if a PID is in the webmounts of the user
1036 *
1037 * @param int $pid Page ID to check
1038 * @return bool TRUE if OK
1039 */
1040 public function checkPID($pid)
1041 {
1042 if (!isset($this->checkPID_cache[$pid])) {
1043 $this->checkPID_cache[$pid] = (bool)$this->getBackendUser()->isInWebMount($pid);
1044 }
1045 return $this->checkPID_cache[$pid];
1046 }
1047
1048 /**
1049 * Checks if the position of an updated record is configured to be corrected. This can be disabled globally and changed for elements individually.
1050 *
1051 * @param string $table Table name
1052 * @param int $uid Uid or record
1053 * @return bool TRUE if the position of the record should be updated to match the one in the import structure
1054 */
1055 public function dontIgnorePid($table, $uid)
1056 {
1057 return $this->import_mode[$table . ':' . $uid] !== 'ignore_pid' && (!$this->global_ignore_pid || $this->import_mode[$table . ':' . $uid] === 'respect_pid');
1058 }
1059
1060 /**
1061 * Checks if the record exists
1062 *
1063 * @param string $table Table name
1064 * @param int $uid UID of record
1065 * @param string $fields Field list to select. Default is "uid,pid
1066 * @return array Result of \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord() which means the record if found, otherwise FALSE
1067 */
1068 public function doesRecordExist($table, $uid, $fields = '')
1069 {
1070 return BackendUtility::getRecord($table, $uid, $fields ? $fields : 'uid,pid');
1071 }
1072
1073 /**
1074 * Returns the page title path of a PID value. Results are cached internally
1075 *
1076 * @param int $pid Record PID to check
1077 * @return string The path for the input PID
1078 */
1079 public function getRecordPath($pid)
1080 {
1081 if (!isset($this->cache_getRecordPath[$pid])) {
1082 $clause = $this->getBackendUser()->getPagePermsClause(1);
1083 $this->cache_getRecordPath[$pid] = (string)BackendUtility::getRecordPath($pid, $clause, 20);
1084 }
1085 return $this->cache_getRecordPath[$pid];
1086 }
1087
1088 /**
1089 * Makes a selector-box from optValues
1090 *
1091 * @param string $prefix Form element name
1092 * @param string $value Current value
1093 * @param array $optValues Options to display (key/value pairs)
1094 * @return string HTML select element
1095 */
1096 public function renderSelectBox($prefix, $value, $optValues)
1097 {
1098 $opt = array();
1099 $isSelFlag = 0;
1100 foreach ($optValues as $k => $v) {
1101 $sel = (string)$k === (string)$value ? ' selected="selected"' : '';
1102 if ($sel) {
1103 $isSelFlag++;
1104 }
1105 $opt[] = '<option value="' . htmlspecialchars($k) . '"' . $sel . '>' . htmlspecialchars($v) . '</option>';
1106 }
1107 if (!$isSelFlag && (string)$value !== '') {
1108 $opt[] = '<option value="' . htmlspecialchars($value) . '" selected="selected">' . htmlspecialchars(('[\'' . $value . '\']')) . '</option>';
1109 }
1110 return '<select name="' . $prefix . '">' . implode('', $opt) . '</select>';
1111 }
1112
1113 /**
1114 * Compares two records, the current database record and the one from the import memory.
1115 * Will return HTML code to show any differences between them!
1116 *
1117 * @param array $databaseRecord Database record, all fields (new values)
1118 * @param array $importRecord Import memorys record for the same table/uid, all fields (old values)
1119 * @param string $table The table name of the record
1120 * @param bool $inverseDiff Inverse the diff view (switch red/green, needed for pre-update difference view)
1121 * @return string HTML
1122 */
1123 public function compareRecords($databaseRecord, $importRecord, $table, $inverseDiff = false)
1124 {
1125 // Initialize:
1126 $output = array();
1127 $diffUtility = GeneralUtility::makeInstance(DiffUtility::class);
1128 // Check if both inputs are records:
1129 if (is_array($databaseRecord) && is_array($importRecord)) {
1130 // Traverse based on database record
1131 foreach ($databaseRecord as $fN => $value) {
1132 if (is_array($GLOBALS['TCA'][$table]['columns'][$fN]) && $GLOBALS['TCA'][$table]['columns'][$fN]['config']['type'] != 'passthrough') {
1133 if (isset($importRecord[$fN])) {
1134 if (trim($databaseRecord[$fN]) !== trim($importRecord[$fN])) {
1135 // Create diff-result:
1136 $output[$fN] = $diffUtility->makeDiffDisplay(BackendUtility::getProcessedValue($table, $fN, !$inverseDiff ? $importRecord[$fN] : $databaseRecord[$fN], 0, 1, 1), BackendUtility::getProcessedValue($table, $fN, !$inverseDiff ? $databaseRecord[$fN] : $importRecord[$fN], 0, 1, 1));
1137 }
1138 unset($importRecord[$fN]);
1139 }
1140 }
1141 }
1142 // Traverse remaining in import record:
1143 foreach ($importRecord as $fN => $value) {
1144 if (is_array($GLOBALS['TCA'][$table]['columns'][$fN]) && $GLOBALS['TCA'][$table]['columns'][$fN]['config']['type'] !== 'passthrough') {
1145 $output[$fN] = '<strong>Field missing</strong> in database';
1146 }
1147 }
1148 // Create output:
1149 if (!empty($output)) {
1150 $tRows = array();
1151 foreach ($output as $fN => $state) {
1152 $tRows[] = '
1153 <tr>
1154 <td class="bgColor5">' . $this->getLanguageService()->sL($GLOBALS['TCA'][$table]['columns'][$fN]['label'], true) . ' (' . htmlspecialchars($fN) . ')</td>
1155 <td class="bgColor4">' . $state . '</td>
1156 </tr>
1157 ';
1158 }
1159 $output = '<table border="0" cellpadding="0" cellspacing="1">' . implode('', $tRows) . '</table>';
1160 } else {
1161 $output = 'Match';
1162 }
1163 return '<strong class="text-nowrap">[' . htmlspecialchars(($table . ':' . $importRecord['uid'] . ' => ' . $databaseRecord['uid'])) . ']:</strong> ' . $output;
1164 }
1165 return 'ERROR: One of the inputs were not an array!';
1166 }
1167
1168 /**
1169 * Creates the original file name for a copy-RTE image (magic type)
1170 *
1171 * @param string $string RTE copy filename, eg. "RTEmagicC_user_pm_icon_01.gif.gif
1172 * @return string|NULL RTE original filename, eg. "RTEmagicP_user_pm_icon_01.gif". If the input filename was NOT prefixed RTEmagicC_ as RTE images would be, NULL is returned!
1173 */
1174 public function getRTEoriginalFilename($string)
1175 {
1176 // If "magic image":
1177 if (GeneralUtility::isFirstPartOfStr($string, 'RTEmagicC_')) {
1178 // Find original file:
1179 $pI = pathinfo(substr($string, strlen('RTEmagicC_')));
1180 $filename = substr($pI['basename'], 0, -strlen(('.' . $pI['extension'])));
1181 $origFilePath = 'RTEmagicP_' . $filename;
1182 return $origFilePath;
1183 }
1184 return null;
1185 }
1186
1187 /**
1188 * Returns file processing object, initialized only once.
1189 *
1190 * @return ExtendedFileUtility File processor object
1191 */
1192 public function getFileProcObj()
1193 {
1194 if ($this->fileProcObj === null) {
1195 $this->fileProcObj = GeneralUtility::makeInstance(ExtendedFileUtility::class);
1196 $this->fileProcObj->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
1197 $this->fileProcObj->setActionPermissions();
1198 }
1199 return $this->fileProcObj;
1200 }
1201
1202 /**
1203 * Call Hook
1204 *
1205 * @param string $name Name of the hook
1206 * @param array $params Array with params
1207 * @return void
1208 */
1209 public function callHook($name, $params)
1210 {
1211 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/impexp/class.tx_impexp.php'][$name])) {
1212 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/impexp/class.tx_impexp.php'][$name] as $hook) {
1213 GeneralUtility::callUserFunction($hook, $params, $this);
1214 }
1215 }
1216 }
1217
1218 /*****************************
1219 * Error handling
1220 *****************************/
1221
1222 /**
1223 * Sets error message in the internal error log
1224 *
1225 * @param string $msg Error message
1226 * @return void
1227 */
1228 public function error($msg)
1229 {
1230 $this->errorLog[] = $msg;
1231 }
1232
1233 /**
1234 * Returns a table with the error-messages.
1235 *
1236 * @return string HTML print of error log
1237 */
1238 public function printErrorLog()
1239 {
1240 return !empty($this->errorLog) ? DebugUtility::viewArray($this->errorLog) : '';
1241 }
1242
1243 /**
1244 * @return BackendUserAuthentication
1245 */
1246 protected function getBackendUser()
1247 {
1248 return $GLOBALS['BE_USER'];
1249 }
1250
1251 /**
1252 * @return DatabaseConnection
1253 */
1254 protected function getDatabaseConnection()
1255 {
1256 return $GLOBALS['TYPO3_DB'];
1257 }
1258
1259 /**
1260 * @return LanguageService
1261 */
1262 protected function getLanguageService()
1263 {
1264 return $GLOBALS['LANG'];
1265 }
1266 }