Passed
Push — master ( b66d49...67ad9d )
by
unknown
12:25
created

DataHandler::copyPages()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 5
nop 2
dl 0
loc 29
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Core\DataHandling;
17
18
use Doctrine\DBAL\Driver\Statement;
19
use Doctrine\DBAL\Exception as DBALException;
20
use Doctrine\DBAL\Platforms\PostgreSQL94Platform as PostgreSqlPlatform;
21
use Doctrine\DBAL\Platforms\SQLServer2012Platform as SQLServerPlatform;
22
use Doctrine\DBAL\Types\IntegerType;
23
use Psr\Log\LoggerAwareInterface;
24
use Psr\Log\LoggerAwareTrait;
25
use TYPO3\CMS\Backend\Utility\BackendUtility;
26
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
27
use TYPO3\CMS\Core\Cache\CacheManager;
28
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
29
use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidIdentifierException;
30
use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidParentRowException;
31
use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidParentRowLoopException;
32
use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidParentRowRootException;
33
use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidPointerFieldValueException;
34
use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
35
use TYPO3\CMS\Core\Configuration\Richtext;
36
use TYPO3\CMS\Core\Crypto\PasswordHashing\InvalidPasswordHashException;
37
use TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory;
38
use TYPO3\CMS\Core\Database\Connection;
39
use TYPO3\CMS\Core\Database\ConnectionPool;
40
use TYPO3\CMS\Core\Database\Query\QueryHelper;
41
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
42
use TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface;
43
use TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction;
44
use TYPO3\CMS\Core\Database\RelationHandler;
45
use TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore;
46
use TYPO3\CMS\Core\DataHandling\Localization\DataMapProcessor;
47
use TYPO3\CMS\Core\DataHandling\Model\CorrelationId;
48
use TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory;
49
use TYPO3\CMS\Core\Html\RteHtmlParser;
50
use TYPO3\CMS\Core\Localization\LanguageService;
51
use TYPO3\CMS\Core\Messaging\FlashMessage;
52
use TYPO3\CMS\Core\Messaging\FlashMessageService;
53
use TYPO3\CMS\Core\Resource\ResourceFactory;
54
use TYPO3\CMS\Core\Service\OpcodeCacheService;
55
use TYPO3\CMS\Core\SysLog\Action as SystemLogGenericAction;
56
use TYPO3\CMS\Core\SysLog\Action\Cache as SystemLogCacheAction;
57
use TYPO3\CMS\Core\SysLog\Action\Database as SystemLogDatabaseAction;
58
use TYPO3\CMS\Core\SysLog\Error as SystemLogErrorClassification;
59
use TYPO3\CMS\Core\SysLog\Type as SystemLogType;
60
use TYPO3\CMS\Core\Type\Bitmask\Permission;
61
use TYPO3\CMS\Core\Utility\ArrayUtility;
62
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
63
use TYPO3\CMS\Core\Utility\GeneralUtility;
64
use TYPO3\CMS\Core\Utility\HttpUtility;
65
use TYPO3\CMS\Core\Utility\MathUtility;
66
use TYPO3\CMS\Core\Utility\StringUtility;
67
use TYPO3\CMS\Core\Versioning\VersionState;
68
69
/**
70
 * The main data handler class which takes care of correctly updating and inserting records.
71
 * This class was formerly known as TCEmain.
72
 *
73
 * This is the TYPO3 Core Engine class for manipulation of the database
74
 * This class is used by eg. the tce_db BE route (SimpleDataHandlerController) which provides an interface for POST forms to this class.
75
 *
76
 * Dependencies:
77
 * - $GLOBALS['TCA'] must exist
78
 * - $GLOBALS['LANG'] must exist
79
 *
80
 * Also see document 'TYPO3 Core API' for details.
81
 */
82
class DataHandler implements LoggerAwareInterface
83
{
84
    use LoggerAwareTrait;
85
86
    // *********************
87
    // Public variables you can configure before using the class:
88
    // *********************
89
    /**
90
     * If TRUE, the default log-messages will be stored. This should not be necessary if the locallang-file for the
91
     * log-display is properly configured. So disabling this will just save some database-space as the default messages are not saved.
92
     *
93
     * @var bool
94
     */
95
    public $storeLogMessages = true;
96
97
    /**
98
     * If TRUE, actions are logged to sys_log.
99
     *
100
     * @var bool
101
     */
102
    public $enableLogging = true;
103
104
    /**
105
     * If TRUE, the datamap array is reversed in the order, which is a nice thing if you're creating a whole new
106
     * bunch of records.
107
     *
108
     * @var bool
109
     */
110
    public $reverseOrder = false;
111
112
    /**
113
     * If TRUE, only fields which are different from the database values are saved! In fact, if a whole input array
114
     * is similar, it's not saved then.
115
     *
116
     * @var bool
117
     * @internal should only be used from within TYPO3 Core
118
     */
119
    public $checkSimilar = true;
120
121
    /**
122
     * This will read the record after having updated or inserted it. If anything is not properly submitted an error
123
     * is written to the log. This feature consumes extra time by selecting records
124
     *
125
     * @var bool
126
     */
127
    public $checkStoredRecords = true;
128
129
    /**
130
     * If set, values '' and 0 will equal each other when the stored records are checked.
131
     *
132
     * @var bool
133
     */
134
    public $checkStoredRecords_loose = true;
135
136
    /**
137
     * If set, then the 'hideAtCopy' flag for tables will be ignored.
138
     *
139
     * @var bool
140
     */
141
    public $neverHideAtCopy = false;
142
143
    /**
144
     * If set, then the TCE class has been instantiated during an import action of a T3D
145
     *
146
     * @var bool
147
     */
148
    public $isImporting = false;
149
150
    /**
151
     * If set, then transformations are NOT performed on the input.
152
     *
153
     * @var bool
154
     */
155
    public $dontProcessTransformations = false;
156
157
    /**
158
     * Will distinguish between translations (with parent) and localizations (without parent) while still using the same methods to copy the records
159
     * TRUE: translation of a record connected to the default language
160
     * FALSE: localization of a record without connection to the default language
161
     *
162
     * @var bool
163
     */
164
    protected $useTransOrigPointerField = true;
165
166
    /**
167
     * If TRUE, workspace restrictions are bypassed on edit and create actions (process_datamap()).
168
     * YOU MUST KNOW what you do if you use this feature!
169
     *
170
     * @var bool
171
     * @internal should only be used from within TYPO3 Core
172
     */
173
    public $bypassWorkspaceRestrictions = false;
174
175
    /**
176
     * If TRUE, access check, check for deleted etc. for records is bypassed.
177
     * YOU MUST KNOW what you are doing if you use this feature!
178
     *
179
     * @var bool
180
     */
181
    public $bypassAccessCheckForRecords = false;
182
183
    /**
184
     * Comma-separated list. This list of tables decides which tables will be copied. If empty then none will.
185
     * If '*' then all will (that the user has permission to of course)
186
     *
187
     * @var string
188
     * @internal should only be used from within TYPO3 Core
189
     */
190
    public $copyWhichTables = '*';
191
192
    /**
193
     * If 0 then branch is NOT copied.
194
     * If 1 then pages on the 1st level is copied.
195
     * If 2 then pages on the second level is copied ... and so on
196
     *
197
     * @var int
198
     */
199
    public $copyTree = 0;
200
201
    /**
202
     * [table][fields]=value: New records are created with default values and you can set this array on the
203
     * form $defaultValues[$table][$field] = $value to override the default values fetched from TCA.
204
     * If ->setDefaultsFromUserTS is called UserTSconfig default values will overrule existing values in this array
205
     * (thus UserTSconfig overrules externally set defaults which overrules TCA defaults)
206
     *
207
     * @var array
208
     * @internal should only be used from within TYPO3 Core
209
     */
210
    public $defaultValues = [];
211
212
    /**
213
     * [table][fields]=value: You can set this array on the form $overrideValues[$table][$field] = $value to
214
     * override the incoming data. You must set this externally. You must make sure the fields in this array are also
215
     * found in the table, because it's not checked. All columns can be set by this array!
216
     *
217
     * @var array
218
     * @internal should only be used from within TYPO3 Core
219
     */
220
    public $overrideValues = [];
221
222
    /**
223
     * If entries are set in this array corresponding to fields for update, they are ignored and thus NOT updated.
224
     * You could set this array from a series of checkboxes with value=0 and hidden fields before the checkbox with 1.
225
     * Then an empty checkbox will disable the field.
226
     *
227
     * @var array
228
     * @internal should only be used from within TYPO3 Core
229
     */
230
    public $data_disableFields = [];
231
232
    /**
233
     * Use this array to validate suggested uids for tables by setting [table]:[uid]. This is a dangerous option
234
     * since it will force the inserted record to have a certain UID. The value just have to be TRUE, but if you set
235
     * it to "DELETE" it will make sure any record with that UID will be deleted first (raw delete).
236
     * The option is used for import of T3D files when synchronizing between two mirrored servers.
237
     * As a security measure this feature is available only for Admin Users (for now)
238
     *
239
     * @var array
240
     */
241
    public $suggestedInsertUids = [];
242
243
    /**
244
     * Object. Call back object for FlexForm traversal. Useful when external classes wants to use the
245
     * iteration functions inside DataHandler for traversing a FlexForm structure.
246
     *
247
     * @var object
248
     * @internal should only be used from within TYPO3 Core
249
     */
250
    public $callBackObj;
251
252
    /**
253
     * A string which can be used as correlationId for RecordHistory entries.
254
     * The string can later be used to rollback multiple changes at once.
255
     *
256
     * @var CorrelationId|null
257
     */
258
    protected $correlationId;
259
260
    // *********************
261
    // Internal variables (mapping arrays) which can be used (read-only) from outside
262
    // *********************
263
    /**
264
     * Contains mapping of auto-versionized records.
265
     *
266
     * @var array
267
     * @internal should only be used from within TYPO3 Core
268
     */
269
    public $autoVersionIdMap = [];
270
271
    /**
272
     * When new elements are created, this array contains a map between their "NEW..." string IDs and the eventual UID they got when stored in database
273
     *
274
     * @var array
275
     */
276
    public $substNEWwithIDs = [];
277
278
    /**
279
     * Like $substNEWwithIDs, but where each old "NEW..." id is mapped to the table it was from.
280
     *
281
     * @var array
282
     * @internal should only be used from within TYPO3 Core
283
     */
284
    public $substNEWwithIDs_table = [];
285
286
    /**
287
     * Holds the tables and there the ids of newly created child records from IRRE
288
     *
289
     * @var array
290
     * @internal should only be used from within TYPO3 Core
291
     */
292
    public $newRelatedIDs = [];
293
294
    /**
295
     * This array is the sum of all copying operations in this class.
296
     *
297
     * @var array
298
     * @internal should only be used from within TYPO3 Core
299
     */
300
    public $copyMappingArray_merged = [];
301
302
    /**
303
     * Per-table array with UIDs that have been deleted.
304
     *
305
     * @var array
306
     */
307
    protected $deletedRecords = [];
308
309
    /**
310
     * Errors are collected in this variable.
311
     *
312
     * @var array
313
     * @internal should only be used from within TYPO3 Core
314
     */
315
    public $errorLog = [];
316
317
    /**
318
     * Fields from the pages-table for which changes will trigger a pagetree refresh
319
     *
320
     * @var array
321
     */
322
    public $pagetreeRefreshFieldsFromPages = ['pid', 'sorting', 'deleted', 'hidden', 'title', 'doktype', 'is_siteroot', 'fe_group', 'nav_hide', 'nav_title', 'module', 'starttime', 'endtime', 'content_from_pid', 'extendToSubpages'];
323
324
    /**
325
     * Indicates whether the pagetree needs a refresh because of important changes
326
     *
327
     * @var bool
328
     * @internal should only be used from within TYPO3 Core
329
     */
330
    public $pagetreeNeedsRefresh = false;
331
332
    // *********************
333
    // Internal Variables, do not touch.
334
    // *********************
335
336
    // Variables set in init() function:
337
338
    /**
339
     * The user-object the script uses. If not set from outside, this is set to the current global $BE_USER.
340
     *
341
     * @var BackendUserAuthentication
342
     */
343
    public $BE_USER;
344
345
    /**
346
     * Will be set to uid of be_user executing this script
347
     *
348
     * @var int
349
     * @internal should only be used from within TYPO3 Core
350
     */
351
    public $userid;
352
353
    /**
354
     * Will be set to username of be_user executing this script
355
     *
356
     * @var string
357
     * @internal should only be used from within TYPO3 Core
358
     */
359
    public $username;
360
361
    /**
362
     * Will be set if user is admin
363
     *
364
     * @var bool
365
     * @internal should only be used from within TYPO3 Core
366
     */
367
    public $admin;
368
369
    /**
370
     * @var PagePermissionAssembler
371
     */
372
    protected $pagePermissionAssembler;
373
374
    /**
375
     * The list of <table>-<fields> that cannot be edited by user. This is compiled from TCA/exclude-flag combined with non_exclude_fields for the user.
376
     *
377
     * @var array
378
     */
379
    protected $excludedTablesAndFields = [];
380
381
    /**
382
     * Data submitted from the form view, used to control behaviours,
383
     * e.g. this is used to activate/deactivate fields and thus store NULL values
384
     *
385
     * @var array
386
     */
387
    protected $control = [];
388
389
    /**
390
     * Set with incoming data array
391
     *
392
     * @var array
393
     */
394
    public $datamap = [];
395
396
    /**
397
     * Set with incoming cmd array
398
     *
399
     * @var array
400
     */
401
    public $cmdmap = [];
402
403
    /**
404
     * List of changed old record ids to new records ids
405
     *
406
     * @var array
407
     */
408
    protected $mmHistoryRecords = [];
409
410
    /**
411
     * List of changed old record ids to new records ids
412
     *
413
     * @var array
414
     */
415
    protected $historyRecords = [];
416
417
    // Internal static:
418
419
    /**
420
     * The interval between sorting numbers used with tables with a 'sorting' field defined.
421
     *
422
     * Min 1, should be power of 2
423
     *
424
     * @var int
425
     * @internal should only be used from within TYPO3 Core
426
     */
427
    public $sortIntervals = 256;
428
429
    // Internal caching arrays
430
    /**
431
     * User by function checkRecordInsertAccess() to store whether a record can be inserted on a page id
432
     *
433
     * @var array
434
     */
435
    protected $recInsertAccessCache = [];
436
437
    /**
438
     * Caching array for check of whether records are in a webmount
439
     *
440
     * @var array
441
     */
442
    protected $isRecordInWebMount_Cache = [];
443
444
    /**
445
     * Caching array for page ids in webmounts
446
     *
447
     * @var array
448
     */
449
    protected $isInWebMount_Cache = [];
450
451
    /**
452
     * Used for caching page records in pageInfo()
453
     *
454
     * @var array
455
     */
456
    protected $pageCache = [];
457
458
    // Other arrays:
459
    /**
460
     * For accumulation of MM relations that must be written after new records are created.
461
     *
462
     * @var array
463
     * @internal
464
     */
465
    public $dbAnalysisStore = [];
466
467
    /**
468
     * Used for tracking references that might need correction after operations
469
     *
470
     * @var array
471
     * @internal
472
     */
473
    public $registerDBList = [];
474
475
    /**
476
     * Used for tracking references that might need correction in pid field after operations (e.g. IRRE)
477
     *
478
     * @var array
479
     * @internal
480
     */
481
    public $registerDBPids = [];
482
483
    /**
484
     * Used by the copy action to track the ids of new pages so subpages are correctly inserted!
485
     * THIS is internally cleared for each executed copy operation! DO NOT USE THIS FROM OUTSIDE!
486
     * Read from copyMappingArray_merged instead which is accumulating this information.
487
     *
488
     * NOTE: This is used by some outside scripts (e.g. hooks), as the results in $copyMappingArray_merged
489
     * are only available after an action has been completed.
490
     *
491
     * @var array
492
     * @internal
493
     */
494
    public $copyMappingArray = [];
495
496
    /**
497
     * Array used for remapping uids and values at the end of process_datamap
498
     *
499
     * @var array
500
     * @internal
501
     */
502
    public $remapStack = [];
503
504
    /**
505
     * Array used for remapping uids and values at the end of process_datamap
506
     * (e.g. $remapStackRecords[<table>][<uid>] = <index in $remapStack>)
507
     *
508
     * @var array
509
     * @internal
510
     */
511
    public $remapStackRecords = [];
512
513
    /**
514
     * Array used for checking whether new children need to be remapped
515
     *
516
     * @var array
517
     */
518
    protected $remapStackChildIds = [];
519
520
    /**
521
     * Array used for executing addition actions after remapping happened (set processRemapStack())
522
     *
523
     * @var array
524
     */
525
    protected $remapStackActions = [];
526
527
    /**
528
     * Registry object to gather reference index update requests and perform updates after
529
     * main processing has been done. The first call to start() instantiates this object.
530
     * Recursive sub instances receive this instance via __construct().
531
     * The final update() call is done at the end of process_cmdmap() or process_datamap()
532
     * in the outer most instance.
533
     *
534
     * @var ReferenceIndexUpdater
535
     */
536
    protected $referenceIndexUpdater;
537
538
    /**
539
     * Tells, that this DataHandler instance was called from \TYPO3\CMS\Impext\ImportExport.
540
     * This variable is set by \TYPO3\CMS\Impext\ImportExport
541
     *
542
     * @var bool
543
     * @internal only used within TYPO3 Core
544
     */
545
    public $callFromImpExp = false;
546
547
    // Various
548
549
    /**
550
     * Set to "currentRecord" during checking of values.
551
     *
552
     * @var array
553
     * @internal
554
     */
555
    public $checkValue_currentRecord = [];
556
557
    /**
558
     * Disable delete clause
559
     *
560
     * @var bool
561
     */
562
    protected $disableDeleteClause = false;
563
564
    /**
565
     * @var array
566
     */
567
    protected $checkModifyAccessListHookObjects;
568
569
    /**
570
     * @var array
571
     */
572
    protected $version_remapMMForVersionSwap_reg;
573
574
    /**
575
     * The outer most instance of \TYPO3\CMS\Core\DataHandling\DataHandler:
576
     * This object instantiates itself on versioning and localization ...
577
     *
578
     * @var \TYPO3\CMS\Core\DataHandling\DataHandler
579
     */
580
    protected $outerMostInstance;
581
582
    /**
583
     * Internal cache for collecting records that should trigger cache clearing
584
     *
585
     * @var array
586
     */
587
    protected static $recordsToClearCacheFor = [];
588
589
    /**
590
     * Internal cache for pids of records which were deleted. It's not possible
591
     * to retrieve the parent folder/page at a later stage
592
     *
593
     * @var array
594
     */
595
    protected static $recordPidsForDeletedRecords = [];
596
597
    /**
598
     * Runtime Cache to store and retrieve data computed for a single request
599
     *
600
     * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
601
     */
602
    protected $runtimeCache;
603
604
    /**
605
     * Prefix for the cache entries of nested element calls since the runtimeCache has a global scope.
606
     *
607
     * @var string
608
     */
609
    protected $cachePrefixNestedElementCalls = 'core-datahandler-nestedElementCalls-';
610
611
    /**
612
     * Sets up the data handler cache and some additional options, the main logic is done in the start() method.
613
     *
614
     * @param ReferenceIndexUpdater|null $referenceIndexUpdater Hand over from outer most instance to sub instances
615
     */
616
    public function __construct(ReferenceIndexUpdater $referenceIndexUpdater = null)
617
    {
618
        $this->checkStoredRecords = (bool)$GLOBALS['TYPO3_CONF_VARS']['BE']['checkStoredRecords'];
619
        $this->checkStoredRecords_loose = (bool)$GLOBALS['TYPO3_CONF_VARS']['BE']['checkStoredRecordsLoose'];
620
        $this->runtimeCache = $this->getRuntimeCache();
621
        $this->pagePermissionAssembler = GeneralUtility::makeInstance(PagePermissionAssembler::class, $GLOBALS['TYPO3_CONF_VARS']['BE']['defaultPermissions']);
622
        if ($referenceIndexUpdater === null) {
623
            // Create ReferenceIndexUpdater object. This should only happen on outer most instance,
624
            // sub instances should receive the reference index updater from a parent.
625
            $referenceIndexUpdater = GeneralUtility::makeInstance(ReferenceIndexUpdater::class);
626
        }
627
        $this->referenceIndexUpdater = $referenceIndexUpdater;
628
    }
629
630
    /**
631
     * @param array $control
632
     * @internal
633
     */
634
    public function setControl(array $control)
635
    {
636
        $this->control = $control;
637
    }
638
639
    /**
640
     * Initializing.
641
     * For details, see 'TYPO3 Core API' document.
642
     * This function does not start the processing of data, but merely initializes the object
643
     *
644
     * @param array $data Data to be modified or inserted in the database
645
     * @param array $cmd Commands to copy, move, delete, localize, versionize records.
646
     * @param BackendUserAuthentication|null $altUserObject An alternative userobject you can set instead of the default, which is $GLOBALS['BE_USER']
647
     */
648
    public function start($data, $cmd, $altUserObject = null)
649
    {
650
        // Initializing BE_USER
651
        $this->BE_USER = is_object($altUserObject) ? $altUserObject : $GLOBALS['BE_USER'];
652
        $this->userid = $this->BE_USER->user['uid'] ?? 0;
653
        $this->username = $this->BE_USER->user['username'] ?? '';
654
        $this->admin = $this->BE_USER->user['admin'] ?? false;
655
656
        // set correlation id for each new set of data or commands
657
        $this->correlationId = CorrelationId::forScope(
658
            md5(StringUtility::getUniqueId(self::class))
659
        );
660
661
        // Get default values from user TSconfig
662
        $tcaDefaultOverride = $this->BE_USER->getTSConfig()['TCAdefaults.'] ?? null;
663
        if (is_array($tcaDefaultOverride)) {
664
            $this->setDefaultsFromUserTS($tcaDefaultOverride);
665
        }
666
667
        // generates the excludelist, based on TCA/exclude-flag and non_exclude_fields for the user:
668
        if (!$this->admin) {
669
            $this->excludedTablesAndFields = array_flip($this->getExcludeListArray());
670
        }
671
        // Setting the data and cmd arrays
672
        if (is_array($data)) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
673
            reset($data);
674
            $this->datamap = $data;
675
        }
676
        if (is_array($cmd)) {
0 ignored issues
show
introduced by
The condition is_array($cmd) is always true.
Loading history...
677
            reset($cmd);
678
            $this->cmdmap = $cmd;
679
        }
680
    }
681
682
    /**
683
     * Function that can mirror input values in datamap-array to other uid numbers.
684
     * Example: $mirror[table][11] = '22,33' will look for content in $this->datamap[table][11] and copy it to $this->datamap[table][22] and $this->datamap[table][33]
685
     *
686
     * @param array $mirror This array has the syntax $mirror[table_name][uid] = [list of uids to copy data-value TO!]
687
     * @internal
688
     */
689
    public function setMirror($mirror)
690
    {
691
        if (!is_array($mirror)) {
0 ignored issues
show
introduced by
The condition is_array($mirror) is always true.
Loading history...
692
            return;
693
        }
694
695
        foreach ($mirror as $table => $uid_array) {
696
            if (!isset($this->datamap[$table])) {
697
                continue;
698
            }
699
700
            foreach ($uid_array as $id => $uidList) {
701
                if (!isset($this->datamap[$table][$id])) {
702
                    continue;
703
                }
704
705
                $theIdsInArray = GeneralUtility::trimExplode(',', $uidList, true);
706
                foreach ($theIdsInArray as $copyToUid) {
707
                    $this->datamap[$table][$copyToUid] = $this->datamap[$table][$id];
708
                }
709
            }
710
        }
711
    }
712
713
    /**
714
     * Initializes default values coming from User TSconfig
715
     *
716
     * @param array $userTS User TSconfig array
717
     * @internal should only be used from within DataHandler
718
     */
719
    public function setDefaultsFromUserTS($userTS)
720
    {
721
        if (!is_array($userTS)) {
0 ignored issues
show
introduced by
The condition is_array($userTS) is always true.
Loading history...
722
            return;
723
        }
724
725
        foreach ($userTS as $k => $v) {
726
            $k = mb_substr($k, 0, -1);
727
            if (!$k || !is_array($v) || !isset($GLOBALS['TCA'][$k])) {
728
                continue;
729
            }
730
731
            if (is_array($this->defaultValues[$k])) {
732
                $this->defaultValues[$k] = array_merge($this->defaultValues[$k], $v);
733
            } else {
734
                $this->defaultValues[$k] = $v;
735
            }
736
        }
737
    }
738
739
    /**
740
     * When a new record is created, all values that haven't been set but are set via PageTSconfig / UserTSconfig
741
     * get applied here.
742
     *
743
     * This is only executed for new records. The most important part is that the pageTS of the actual resolved $pid
744
     * is taken, and a new field array with empty defaults is set again.
745
     *
746
     * @param string $table
747
     * @param int $pageId
748
     * @param array $prepopulatedFieldArray
749
     * @return array
750
     */
751
    protected function applyDefaultsForFieldArray(string $table, int $pageId, array $prepopulatedFieldArray): array
752
    {
753
        // First set TCAdefaults respecting the given PageID
754
        $tcaDefaults = BackendUtility::getPagesTSconfig($pageId)['TCAdefaults.'] ?? null;
755
        // Re-apply $this->defaultValues settings
756
        $this->setDefaultsFromUserTS($tcaDefaults);
757
        $cleanFieldArray = $this->newFieldArray($table);
758
        if (isset($prepopulatedFieldArray['pid'])) {
759
            $cleanFieldArray['pid'] = $prepopulatedFieldArray['pid'];
760
        }
761
        $sortColumn = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ?? null;
762
        if ($sortColumn !== null && isset($prepopulatedFieldArray[$sortColumn])) {
763
            $cleanFieldArray[$sortColumn] = $prepopulatedFieldArray[$sortColumn];
764
        }
765
        return $cleanFieldArray;
766
    }
767
768
    /*********************************************
769
     *
770
     * HOOKS
771
     *
772
     *********************************************/
773
    /**
774
     * Hook: processDatamap_afterDatabaseOperations
775
     * (calls $hookObj->processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $this);)
776
     *
777
     * Note: When using the hook after INSERT operations, you will only get the temporary NEW... id passed to your hook as $id,
778
     * but you can easily translate it to the real uid of the inserted record using the $this->substNEWwithIDs array.
779
     *
780
     * @param array $hookObjectsArr (reference) Array with hook objects
781
     * @param string $status (reference) Status of the current operation, 'new' or 'update
782
     * @param string $table (reference) The table currently processing data for
783
     * @param string $id (reference) The record uid currently processing data for, [integer] or [string] (like 'NEW...')
784
     * @param array $fieldArray (reference) The field array of a record
785
     * @internal should only be used from within DataHandler
786
     */
787
    public function hook_processDatamap_afterDatabaseOperations(&$hookObjectsArr, &$status, &$table, &$id, &$fieldArray)
788
    {
789
        // Process hook directly:
790
        if (!isset($this->remapStackRecords[$table][$id])) {
791
            foreach ($hookObjectsArr as $hookObj) {
792
                if (method_exists($hookObj, 'processDatamap_afterDatabaseOperations')) {
793
                    $hookObj->processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $this);
794
                }
795
            }
796
        } else {
797
            $this->remapStackRecords[$table][$id]['processDatamap_afterDatabaseOperations'] = [
798
                'status' => $status,
799
                'fieldArray' => $fieldArray,
800
                'hookObjectsArr' => $hookObjectsArr
801
            ];
802
        }
803
    }
804
805
    /**
806
     * Gets the 'checkModifyAccessList' hook objects.
807
     * The first call initializes the accordant objects.
808
     *
809
     * @return array The 'checkModifyAccessList' hook objects (if any)
810
     * @throws \UnexpectedValueException
811
     */
812
    protected function getCheckModifyAccessListHookObjects()
813
    {
814
        if (!isset($this->checkModifyAccessListHookObjects)) {
815
            $this->checkModifyAccessListHookObjects = [];
816
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList'] ?? [] as $className) {
817
                $hookObject = GeneralUtility::makeInstance($className);
818
                if (!$hookObject instanceof DataHandlerCheckModifyAccessListHookInterface) {
819
                    throw new \UnexpectedValueException($className . ' must implement interface ' . DataHandlerCheckModifyAccessListHookInterface::class, 1251892472);
820
                }
821
                $this->checkModifyAccessListHookObjects[] = $hookObject;
822
            }
823
        }
824
        return $this->checkModifyAccessListHookObjects;
825
    }
826
827
    /*********************************************
828
     *
829
     * PROCESSING DATA
830
     *
831
     *********************************************/
832
    /**
833
     * Processing the data-array
834
     * Call this function to process the data-array set by start()
835
     *
836
     * @return bool|void
837
     */
838
    public function process_datamap()
839
    {
840
        $this->controlActiveElements();
841
842
        // Keep versionized(!) relations here locally:
843
        $registerDBList = [];
844
        $this->registerElementsToBeDeleted();
845
        $this->datamap = $this->unsetElementsToBeDeleted($this->datamap);
846
        // Editing frozen:
847
        if ($this->BE_USER->workspace !== 0 && $this->BE_USER->workspaceRec['freeze']) {
848
            $this->newlog('All editing in this workspace has been frozen!', SystemLogErrorClassification::USER_ERROR);
849
            return false;
850
        }
851
        // First prepare user defined objects (if any) for hooks which extend this function:
852
        $hookObjectsArr = [];
853
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'] ?? [] as $className) {
854
            $hookObject = GeneralUtility::makeInstance($className);
855
            if (method_exists($hookObject, 'processDatamap_beforeStart')) {
856
                $hookObject->processDatamap_beforeStart($this);
857
            }
858
            $hookObjectsArr[] = $hookObject;
859
        }
860
        // Pre-process data-map and synchronize localization states
861
        $this->datamap = GeneralUtility::makeInstance(SlugEnricher::class)->enrichDataMap($this->datamap);
862
        $this->datamap = DataMapProcessor::instance($this->datamap, $this->BE_USER, $this->referenceIndexUpdater)->process();
863
        // Organize tables so that the pages-table is always processed first. This is required if you want to make sure that content pointing to a new page will be created.
864
        $orderOfTables = [];
865
        // Set pages first.
866
        if (isset($this->datamap['pages'])) {
867
            $orderOfTables[] = 'pages';
868
        }
869
        $orderOfTables = array_unique(array_merge($orderOfTables, array_keys($this->datamap)));
870
        // Process the tables...
871
        foreach ($orderOfTables as $table) {
872
            // Check if
873
            //	   - table is set in $GLOBALS['TCA'],
874
            //	   - table is NOT readOnly
875
            //	   - the table is set with content in the data-array (if not, there's nothing to process...)
876
            //	   - permissions for tableaccess OK
877
            $modifyAccessList = $this->checkModifyAccessList($table);
878
            if (!$modifyAccessList) {
879
                $this->log($table, 0, SystemLogDatabaseAction::UPDATE, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to modify table \'%s\' without permission', 1, [$table]);
880
            }
881
            if (!isset($GLOBALS['TCA'][$table]) || $this->tableReadOnly($table) || !is_array($this->datamap[$table]) || !$modifyAccessList) {
882
                continue;
883
            }
884
885
            if ($this->reverseOrder) {
886
                $this->datamap[$table] = array_reverse($this->datamap[$table], 1);
887
            }
888
            // For each record from the table, do:
889
            // $id is the record uid, may be a string if new records...
890
            // $incomingFieldArray is the array of fields
891
            foreach ($this->datamap[$table] as $id => $incomingFieldArray) {
892
                if (!is_array($incomingFieldArray)) {
893
                    continue;
894
                }
895
                $theRealPid = null;
896
897
                // Hook: processDatamap_preProcessFieldArray
898
                foreach ($hookObjectsArr as $hookObj) {
899
                    if (method_exists($hookObj, 'processDatamap_preProcessFieldArray')) {
900
                        $hookObj->processDatamap_preProcessFieldArray($incomingFieldArray, $table, $id, $this);
901
                    }
902
                }
903
                // ******************************
904
                // Checking access to the record
905
                // ******************************
906
                $createNewVersion = false;
907
                $recordAccess = false;
908
                $old_pid_value = '';
909
                // Is it a new record? (Then Id is a string)
910
                if (!MathUtility::canBeInterpretedAsInteger($id)) {
911
                    // Get a fieldArray with tca default values
912
                    $fieldArray = $this->newFieldArray($table);
913
                    // A pid must be set for new records.
914
                    if (isset($incomingFieldArray['pid'])) {
915
                        $pid_value = $incomingFieldArray['pid'];
916
                        // Checking and finding numerical pid, it may be a string-reference to another value
917
                        $canProceed = true;
918
                        // If a NEW... id
919
                        if (strpos($pid_value, 'NEW') !== false) {
920
                            if ($pid_value[0] === '-') {
921
                                $negFlag = -1;
922
                                $pid_value = substr($pid_value, 1);
923
                            } else {
924
                                $negFlag = 1;
925
                            }
926
                            // Trying to find the correct numerical value as it should be mapped by earlier processing of another new record.
927
                            if (isset($this->substNEWwithIDs[$pid_value])) {
928
                                if ($negFlag === 1) {
929
                                    $old_pid_value = $this->substNEWwithIDs[$pid_value];
930
                                }
931
                                $pid_value = (int)($negFlag * $this->substNEWwithIDs[$pid_value]);
932
                            } else {
933
                                $canProceed = false;
934
                            }
935
                        }
936
                        $pid_value = (int)$pid_value;
937
                        if ($canProceed) {
938
                            $fieldArray = $this->resolveSortingAndPidForNewRecord($table, $pid_value, $fieldArray);
939
                        }
940
                    }
941
                    $theRealPid = $fieldArray['pid'];
942
                    // Checks if records can be inserted on this $pid.
943
                    // If this is a page translation, the check needs to be done for the l10n_parent record
944
                    if ($table === 'pages' && $incomingFieldArray[$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0 && $incomingFieldArray[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] > 0) {
945
                        $recordAccess = $this->checkRecordInsertAccess($table, $incomingFieldArray[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']]);
946
                    } else {
947
                        $recordAccess = $this->checkRecordInsertAccess($table, $theRealPid);
948
                    }
949
                    if ($recordAccess) {
950
                        $this->addDefaultPermittedLanguageIfNotSet($table, $incomingFieldArray);
951
                        $recordAccess = $this->BE_USER->recordEditAccessInternals($table, $incomingFieldArray, true);
952
                        if (!$recordAccess) {
953
                            $this->newlog('recordEditAccessInternals() check failed. [' . $this->BE_USER->errorMsg . ']', SystemLogErrorClassification::USER_ERROR);
954
                        } elseif (!$this->bypassWorkspaceRestrictions && !$this->BE_USER->workspaceAllowsLiveEditingInTable($table)) {
955
                            // If LIVE records cannot be created due to workspace restrictions, prepare creation of placeholder-record
956
                            // So, if no live records were allowed in the current workspace, we have to create a new version of this record
957
                            if (BackendUtility::isTableWorkspaceEnabled($table)) {
958
                                $createNewVersion = true;
959
                            } else {
960
                                $recordAccess = false;
961
                                $this->newlog('Record could not be created in this workspace', SystemLogErrorClassification::USER_ERROR);
962
                            }
963
                        }
964
                    }
965
                    // Yes new record, change $record_status to 'insert'
966
                    $status = 'new';
967
                } else {
968
                    // Nope... $id is a number
969
                    $fieldArray = [];
970
                    $recordAccess = $this->checkRecordUpdateAccess($table, $id, $incomingFieldArray, $hookObjectsArr);
971
                    if (!$recordAccess) {
972
                        if ($this->enableLogging) {
973
                            $propArr = $this->getRecordProperties($table, $id);
974
                            $this->log($table, $id, SystemLogDatabaseAction::UPDATE, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to modify record \'%s\' (%s) without permission. Or non-existing page.', 2, [$propArr['header'], $table . ':' . $id], $propArr['event_pid']);
975
                        }
976
                        continue;
977
                    }
978
                    // Next check of the record permissions (internals)
979
                    $recordAccess = $this->BE_USER->recordEditAccessInternals($table, $id);
980
                    if (!$recordAccess) {
981
                        $this->newlog('recordEditAccessInternals() check failed. [' . $this->BE_USER->errorMsg . ']', SystemLogErrorClassification::USER_ERROR);
982
                    } else {
983
                        // Here we fetch the PID of the record that we point to...
984
                        $tempdata = $this->recordInfo($table, $id, 'pid' . (BackendUtility::isTableWorkspaceEnabled($table) ? ',t3ver_oid,t3ver_wsid,t3ver_stage' : ''));
985
                        $theRealPid = $tempdata['pid'] ?? null;
986
                        // Use the new id of the versionized record we're trying to write to:
987
                        // (This record is a child record of a parent and has already been versionized.)
988
                        if (!empty($this->autoVersionIdMap[$table][$id])) {
989
                            // For the reason that creating a new version of this record, automatically
990
                            // created related child records (e.g. "IRRE"), update the accordant field:
991
                            $this->getVersionizedIncomingFieldArray($table, $id, $incomingFieldArray, $registerDBList);
992
                            // Use the new id of the copied/versionized record:
993
                            $id = $this->autoVersionIdMap[$table][$id];
994
                            $recordAccess = true;
995
                        } elseif (!$this->bypassWorkspaceRestrictions && ($errorCode = $this->BE_USER->workspaceCannotEditRecord($table, $tempdata))) {
996
                            $recordAccess = false;
997
                            // Versioning is required and it must be offline version!
998
                            // Check if there already is a workspace version
999
                            $workspaceVersion = BackendUtility::getWorkspaceVersionOfRecord($this->BE_USER->workspace, $table, $id, 'uid,t3ver_oid');
1000
                            if ($workspaceVersion) {
1001
                                $id = $workspaceVersion['uid'];
1002
                                $recordAccess = true;
1003
                            } elseif ($this->BE_USER->workspaceAllowAutoCreation($table, $id, $theRealPid)) {
1004
                                // new version of a record created in a workspace - so always refresh pagetree to indicate there is a change in the workspace
1005
                                $this->pagetreeNeedsRefresh = true;
1006
1007
                                /** @var DataHandler $tce */
1008
                                $tce = GeneralUtility::makeInstance(__CLASS__, $this->referenceIndexUpdater);
1009
                                $tce->enableLogging = $this->enableLogging;
1010
                                // Setting up command for creating a new version of the record:
1011
                                $cmd = [];
1012
                                $cmd[$table][$id]['version'] = [
1013
                                    'action' => 'new',
1014
                                    // Default is to create a version of the individual records
1015
                                    'label' => 'Auto-created for WS #' . $this->BE_USER->workspace
1016
                                ];
1017
                                $tce->start([], $cmd, $this->BE_USER);
1018
                                $tce->process_cmdmap();
1019
                                $this->errorLog = array_merge($this->errorLog, $tce->errorLog);
1020
                                // If copying was successful, share the new uids (also of related children):
1021
                                if (!empty($tce->copyMappingArray[$table][$id])) {
1022
                                    foreach ($tce->copyMappingArray as $origTable => $origIdArray) {
1023
                                        foreach ($origIdArray as $origId => $newId) {
1024
                                            $this->autoVersionIdMap[$origTable][$origId] = $newId;
1025
                                        }
1026
                                    }
1027
                                    // Update registerDBList, that holds the copied relations to child records:
1028
                                    $registerDBList = array_merge($registerDBList, $tce->registerDBList);
1029
                                    // For the reason that creating a new version of this record, automatically
1030
                                    // created related child records (e.g. "IRRE"), update the accordant field:
1031
                                    $this->getVersionizedIncomingFieldArray($table, $id, $incomingFieldArray, $registerDBList);
1032
                                    // Use the new id of the copied/versionized record:
1033
                                    $id = $this->autoVersionIdMap[$table][$id];
1034
                                    $recordAccess = true;
1035
                                } else {
1036
                                    $this->newlog('Could not be edited in offline workspace in the branch where found (failure state: \'' . $errorCode . '\'). Auto-creation of version failed!', SystemLogErrorClassification::USER_ERROR);
1037
                                }
1038
                            } else {
1039
                                $this->newlog('Could not be edited in offline workspace in the branch where found (failure state: \'' . $errorCode . '\'). Auto-creation of version not allowed in workspace!', SystemLogErrorClassification::USER_ERROR);
1040
                            }
1041
                        }
1042
                    }
1043
                    // The default is 'update'
1044
                    $status = 'update';
1045
                }
1046
                // If access was granted above, proceed to create or update record:
1047
                if (!$recordAccess) {
1048
                    continue;
1049
                }
1050
1051
                // Here the "pid" is set IF NOT the old pid was a string pointing to a place in the subst-id array.
1052
                [$tscPID] = BackendUtility::getTSCpid($table, $id, $old_pid_value ?: $fieldArray['pid']);
1053
                if ($status === 'new') {
1054
                    // Apply TCAdefaults from pageTS
1055
                    $fieldArray = $this->applyDefaultsForFieldArray($table, (int)$tscPID, $fieldArray);
1056
                    // Apply page permissions as well
1057
                    if ($table === 'pages') {
1058
                        $fieldArray = $this->pagePermissionAssembler->applyDefaults(
1059
                            $fieldArray,
1060
                            (int)$tscPID,
1061
                            (int)$this->userid,
1062
                            (int)$this->BE_USER->firstMainGroup
1063
                        );
1064
                    }
1065
                }
1066
                // Processing of all fields in incomingFieldArray and setting them in $fieldArray
1067
                $fieldArray = $this->fillInFieldArray($table, $id, $fieldArray, $incomingFieldArray, $theRealPid, $status, $tscPID);
1068
                // NOTICE! All manipulation beyond this point bypasses both "excludeFields" AND possible "MM" relations to field!
1069
                // Forcing some values unto field array:
1070
                // NOTICE: This overriding is potentially dangerous; permissions per field is not checked!!!
1071
                $fieldArray = $this->overrideFieldArray($table, $fieldArray);
1072
                // Setting system fields
1073
                if ($status === 'new') {
1074
                    if ($GLOBALS['TCA'][$table]['ctrl']['crdate']) {
1075
                        $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['crdate']] = $GLOBALS['EXEC_TIME'];
1076
                    }
1077
                    if ($GLOBALS['TCA'][$table]['ctrl']['cruser_id']) {
1078
                        $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['cruser_id']] = $this->userid;
1079
                    }
1080
                } elseif ($this->checkSimilar) {
1081
                    // Removing fields which are equal to the current value:
1082
                    $fieldArray = $this->compareFieldArrayWithCurrentAndUnset($table, $id, $fieldArray);
1083
                }
1084
                if ($GLOBALS['TCA'][$table]['ctrl']['tstamp'] && !empty($fieldArray)) {
1085
                    $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['tstamp']] = $GLOBALS['EXEC_TIME'];
1086
                }
1087
                // Set stage to "Editing" to make sure we restart the workflow
1088
                if (BackendUtility::isTableWorkspaceEnabled($table)) {
1089
                    $fieldArray['t3ver_stage'] = 0;
1090
                }
1091
                // Hook: processDatamap_postProcessFieldArray
1092
                foreach ($hookObjectsArr as $hookObj) {
1093
                    if (method_exists($hookObj, 'processDatamap_postProcessFieldArray')) {
1094
                        $hookObj->processDatamap_postProcessFieldArray($status, $table, $id, $fieldArray, $this);
1095
                    }
1096
                }
1097
                // Performing insert/update. If fieldArray has been unset by some userfunction (see hook above), don't do anything
1098
                // Kasper: Unsetting the fieldArray is dangerous; MM relations might be saved already
1099
                if (is_array($fieldArray)) {
1100
                    if ($status === 'new') {
1101
                        if ($table === 'pages') {
1102
                            // for new pages always a refresh is needed
1103
                            $this->pagetreeNeedsRefresh = true;
1104
                        }
1105
1106
                        // This creates a version of the record, instead of adding it to the live workspace
1107
                        if ($createNewVersion) {
1108
                            // new record created in a workspace - so always refresh pagetree to indicate there is a change in the workspace
1109
                            $this->pagetreeNeedsRefresh = true;
1110
                            $fieldArray['pid'] = $theRealPid;
1111
                            $fieldArray['t3ver_oid'] = 0;
1112
                            // Setting state for version (so it can know it is currently a new version...)
1113
                            $fieldArray['t3ver_state'] = (string)new VersionState(VersionState::NEW_PLACEHOLDER);
1114
                            $fieldArray['t3ver_wsid'] = $this->BE_USER->workspace;
1115
                            $this->insertDB($table, $id, $fieldArray, true, (int)($incomingFieldArray['uid'] ?? 0));
1116
                            // Hold auto-versionized ids of placeholders
1117
                            $this->autoVersionIdMap[$table][$this->substNEWwithIDs[$id]] = $this->substNEWwithIDs[$id];
1118
                        } else {
1119
                            $this->insertDB($table, $id, $fieldArray, false, (int)($incomingFieldArray['uid'] ?? 0));
1120
                        }
1121
                    } else {
1122
                        if ($table === 'pages') {
1123
                            // only a certain number of fields needs to be checked for updates
1124
                            // if $this->checkSimilar is TRUE, fields with unchanged values are already removed here
1125
                            $fieldsToCheck = array_intersect($this->pagetreeRefreshFieldsFromPages, array_keys($fieldArray));
1126
                            if (!empty($fieldsToCheck)) {
1127
                                $this->pagetreeNeedsRefresh = true;
1128
                            }
1129
                        }
1130
                        $this->updateDB($table, $id, $fieldArray);
1131
                    }
1132
                }
1133
                // Hook: processDatamap_afterDatabaseOperations
1134
                // Note: When using the hook after INSERT operations, you will only get the temporary NEW... id passed to your hook as $id,
1135
                // but you can easily translate it to the real uid of the inserted record using the $this->substNEWwithIDs array.
1136
                $this->hook_processDatamap_afterDatabaseOperations($hookObjectsArr, $status, $table, $id, $fieldArray);
1137
            }
1138
        }
1139
        // Process the stack of relations to remap/correct
1140
        $this->processRemapStack();
1141
        $this->dbAnalysisStoreExec();
1142
        // Hook: processDatamap_afterAllOperations
1143
        // Note: When this hook gets called, all operations on the submitted data have been finished.
1144
        foreach ($hookObjectsArr as $hookObj) {
1145
            if (method_exists($hookObj, 'processDatamap_afterAllOperations')) {
1146
                $hookObj->processDatamap_afterAllOperations($this);
1147
            }
1148
        }
1149
1150
        if ($this->isOuterMostInstance()) {
1151
            $this->referenceIndexUpdater->update();
1152
            $this->processClearCacheQueue();
1153
            $this->resetElementsToBeDeleted();
1154
        }
1155
    }
1156
1157
    /**
1158
     * @param string $table
1159
     * @param string $value
1160
     * @param string $dbType
1161
     * @return string
1162
     */
1163
    protected function normalizeTimeFormat(string $table, string $value, string $dbType): string
1164
    {
1165
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
1166
        $platform = $connection->getDatabasePlatform();
1167
        if ($platform instanceof SQLServerPlatform) {
1168
            $defaultLength = QueryHelper::getDateTimeFormats()[$dbType]['empty'];
1169
            $value = substr(
1170
                $value,
1171
                0,
1172
                strlen($defaultLength)
1173
            );
1174
        }
1175
        return $value;
1176
    }
1177
1178
    /**
1179
     * Sets the "sorting" DB field and the "pid" field of an incoming record that should be added (NEW1234)
1180
     * depending on the record that should be added or where it should be added.
1181
     *
1182
     * This method is called from process_datamap()
1183
     *
1184
     * @param string $table the table name of the record to insert
1185
     * @param int $pid the real PID (numeric) where the record should be
1186
     * @param array $fieldArray field+value pairs to add
1187
     * @return array the modified field array
1188
     */
1189
    protected function resolveSortingAndPidForNewRecord(string $table, int $pid, array $fieldArray): array
1190
    {
1191
        $sortColumn = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ?? '';
1192
        // Points to a page on which to insert the element, possibly in the top of the page
1193
        if ($pid >= 0) {
1194
            // Ensure that the "pid" is not a translated page ID, but the default page ID
1195
            $pid = $this->getDefaultLanguagePageId($pid);
1196
            // The numerical pid is inserted in the data array
1197
            $fieldArray['pid'] = $pid;
1198
            // If this table is sorted we better find the top sorting number
1199
            if ($sortColumn) {
1200
                $fieldArray[$sortColumn] = $this->getSortNumber($table, 0, $pid);
1201
            }
1202
        } elseif ($sortColumn) {
1203
            // Points to another record before itself
1204
            // If this table is sorted we better find the top sorting number
1205
            // Because $pid is < 0, getSortNumber() returns an array
1206
            $sortingInfo = $this->getSortNumber($table, 0, $pid);
1207
            $fieldArray['pid'] = $sortingInfo['pid'];
1208
            $fieldArray[$sortColumn] = $sortingInfo['sortNumber'];
1209
        } else {
1210
            // Here we fetch the PID of the record that we point to
1211
            $record = $this->recordInfo($table, abs($pid), 'pid');
0 ignored issues
show
Bug introduced by
It seems like abs($pid) can also be of type double; however, parameter $id of TYPO3\CMS\Core\DataHandl...taHandler::recordInfo() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1211
            $record = $this->recordInfo($table, /** @scrutinizer ignore-type */ abs($pid), 'pid');
Loading history...
1212
            // Ensure that the "pid" is not a translated page ID, but the default page ID
1213
            $fieldArray['pid'] = $this->getDefaultLanguagePageId($record['pid']);
1214
        }
1215
        return $fieldArray;
1216
    }
1217
1218
    /**
1219
     * Filling in the field array
1220
     * $this->excludedTablesAndFields is used to filter fields if needed.
1221
     *
1222
     * @param string $table Table name
1223
     * @param int $id Record ID
1224
     * @param array $fieldArray Default values, Preset $fieldArray with 'pid' maybe (pid and uid will be not be overridden anyway)
1225
     * @param array $incomingFieldArray Is which fields/values you want to set. There are processed and put into $fieldArray if OK
1226
     * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted.
1227
     * @param string $status Is 'new' or 'update'
1228
     * @param int $tscPID TSconfig PID
1229
     * @return array Field Array
1230
     * @internal should only be used from within DataHandler
1231
     */
1232
    public function fillInFieldArray($table, $id, $fieldArray, $incomingFieldArray, $realPid, $status, $tscPID)
1233
    {
1234
        // Initialize:
1235
        $originalLanguageRecord = null;
1236
        $originalLanguage_diffStorage = null;
1237
        $diffStorageFlag = false;
1238
        // Setting 'currentRecord' and 'checkValueRecord':
1239
        if (strpos($id, 'NEW') !== false) {
1240
            // Must have the 'current' array - not the values after processing below...
1241
            $checkValueRecord = $fieldArray;
1242
            // IF $incomingFieldArray is an array, overlay it.
1243
            // The point is that when new records are created as copies with flex type fields there might be a field containing information about which DataStructure to use and without that information the flexforms cannot be correctly processed.... This should be OK since the $checkValueRecord is used by the flexform evaluation only anyways...
1244
            if (is_array($incomingFieldArray) && is_array($checkValueRecord)) {
0 ignored issues
show
introduced by
The condition is_array($checkValueRecord) is always true.
Loading history...
1245
                ArrayUtility::mergeRecursiveWithOverrule($checkValueRecord, $incomingFieldArray);
1246
            }
1247
            $currentRecord = $checkValueRecord;
1248
        } else {
1249
            // We must use the current values as basis for this!
1250
            $currentRecord = ($checkValueRecord = $this->recordInfo($table, $id, '*'));
1251
        }
1252
1253
        // Get original language record if available:
1254
        if (is_array($currentRecord)
1255
            && $GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField']
1256
            && $GLOBALS['TCA'][$table]['ctrl']['languageField']
1257
            && $currentRecord[$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0
1258
            && $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']
1259
            && (int)$currentRecord[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] > 0
1260
        ) {
1261
            $originalLanguageRecord = $this->recordInfo($table, $currentRecord[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']], '*');
1262
            BackendUtility::workspaceOL($table, $originalLanguageRecord);
1263
            $originalLanguage_diffStorage = json_decode(
1264
                (string)($currentRecord[$GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField']] ?? ''),
1265
                true
1266
            );
1267
        }
1268
1269
        $this->checkValue_currentRecord = $checkValueRecord;
1270
        // In the following all incoming value-fields are tested:
1271
        // - Are the user allowed to change the field?
1272
        // - Is the field uid/pid (which are already set)
1273
        // - perms-fields for pages-table, then do special things...
1274
        // - If the field is nothing of the above and the field is configured in TCA, the fieldvalues are evaluated by ->checkValue
1275
        // If everything is OK, the field is entered into $fieldArray[]
1276
        foreach ($incomingFieldArray as $field => $fieldValue) {
1277
            if (isset($this->excludedTablesAndFields[$table . '-' . $field]) || $this->data_disableFields[$table][$id][$field]) {
1278
                continue;
1279
            }
1280
1281
            // The field must be editable.
1282
            // Checking if a value for language can be changed:
1283
            $languageDeny = $GLOBALS['TCA'][$table]['ctrl']['languageField'] && (string)$GLOBALS['TCA'][$table]['ctrl']['languageField'] === (string)$field && !$this->BE_USER->checkLanguageAccess($fieldValue);
1284
            if ($languageDeny) {
1285
                continue;
1286
            }
1287
1288
            switch ($field) {
1289
                case 'uid':
1290
                case 'pid':
1291
                    // Nothing happens, already set
1292
                    break;
1293
                case 'perms_userid':
1294
                case 'perms_groupid':
1295
                case 'perms_user':
1296
                case 'perms_group':
1297
                case 'perms_everybody':
1298
                    // Permissions can be edited by the owner or the administrator
1299
                    if ($table === 'pages' && ($this->admin || $status === 'new' || $this->pageInfo($id, 'perms_userid') == $this->userid)) {
1300
                        $value = (int)$fieldValue;
1301
                        switch ($field) {
1302
                            case 'perms_userid':
1303
                            case 'perms_groupid':
1304
                                $fieldArray[$field] = $value;
1305
                                break;
1306
                            default:
1307
                                if ($value >= 0 && $value < (2 ** 5)) {
1308
                                    $fieldArray[$field] = $value;
1309
                                }
1310
                        }
1311
                    }
1312
                    break;
1313
                case 't3ver_oid':
1314
                case 't3ver_wsid':
1315
                case 't3ver_state':
1316
                case 't3ver_stage':
1317
                    break;
1318
                case 'l10n_state':
1319
                    $fieldArray[$field] = $fieldValue;
1320
                    break;
1321
                default:
1322
                    if (isset($GLOBALS['TCA'][$table]['columns'][$field])) {
1323
                        // Evaluating the value
1324
                        $res = $this->checkValue($table, $field, $fieldValue, $id, $status, $realPid, $tscPID, $incomingFieldArray);
1325
                        if (array_key_exists('value', $res)) {
1326
                            $fieldArray[$field] = $res['value'];
1327
                        }
1328
                        // Add the value of the original record to the diff-storage content:
1329
                        if ($GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField']) {
1330
                            $originalLanguage_diffStorage[$field] = (string)$originalLanguageRecord[$field];
1331
                            $diffStorageFlag = true;
1332
                        }
1333
                    } elseif ($GLOBALS['TCA'][$table]['ctrl']['origUid'] === $field) {
1334
                        // Allow value for original UID to pass by...
1335
                        $fieldArray[$field] = $fieldValue;
1336
                    }
1337
            }
1338
        }
1339
1340
        // Dealing with a page translation, setting "sorting", "pid", "perms_*" to the same values as the original record
1341
        if ($table === 'pages' && is_array($originalLanguageRecord)) {
1342
            $fieldArray['sorting'] = $originalLanguageRecord['sorting'];
1343
            $fieldArray['perms_userid'] = $originalLanguageRecord['perms_userid'];
1344
            $fieldArray['perms_groupid'] = $originalLanguageRecord['perms_groupid'];
1345
            $fieldArray['perms_user'] = $originalLanguageRecord['perms_user'];
1346
            $fieldArray['perms_group'] = $originalLanguageRecord['perms_group'];
1347
            $fieldArray['perms_everybody'] = $originalLanguageRecord['perms_everybody'];
1348
        }
1349
1350
        // Add diff-storage information:
1351
        if ($diffStorageFlag
1352
            && !array_key_exists($GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField'], $fieldArray)
1353
        ) {
1354
            // If the field is set it would probably be because of an undo-operation - in which case we should not update the field of course...
1355
            $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField']] = json_encode($originalLanguage_diffStorage);
1356
        }
1357
        // Return fieldArray
1358
        return $fieldArray;
1359
    }
1360
1361
    /*********************************************
1362
     *
1363
     * Evaluation of input values
1364
     *
1365
     ********************************************/
1366
    /**
1367
     * Evaluates a value according to $table/$field settings.
1368
     * This function is for real database fields - NOT FlexForm "pseudo" fields.
1369
     * NOTICE: Calling this function expects this: 1) That the data is saved!
1370
     *
1371
     * @param string $table Table name
1372
     * @param string $field Field name
1373
     * @param string $value Value to be evaluated. Notice, this is the INPUT value from the form. The original value (from any existing record) must be manually looked up inside the function if needed - or taken from $currentRecord array.
1374
     * @param string $id The record-uid, mainly - but not exclusively - used for logging
1375
     * @param string $status 'update' or 'new' flag
1376
     * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted.
1377
     * @param int $tscPID TSconfig PID
1378
     * @param array $incomingFieldArray the fields being explicitly set by the outside (unlike $fieldArray)
1379
     * @return array Returns the evaluated $value as key "value" in this array. Can be checked with isset($res['value']) ...
1380
     * @internal should only be used from within DataHandler
1381
     */
1382
    public function checkValue($table, $field, $value, $id, $status, $realPid, $tscPID, $incomingFieldArray = [])
1383
    {
1384
        $curValueRec = null;
1385
        // Result array
1386
        $res = [];
1387
1388
        // Processing special case of field pages.doktype
1389
        if ($table === 'pages' && $field === 'doktype') {
1390
            // If the user may not use this specific doktype, we issue a warning
1391
            if (!($this->admin || GeneralUtility::inList($this->BE_USER->groupData['pagetypes_select'], $value))) {
1392
                if ($this->enableLogging) {
1393
                    $propArr = $this->getRecordProperties($table, $id);
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $id of TYPO3\CMS\Core\DataHandl...::getRecordProperties(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1393
                    $propArr = $this->getRecordProperties($table, /** @scrutinizer ignore-type */ $id);
Loading history...
1394
                    $this->log($table, $id, SystemLogDatabaseAction::CHECK, 0, SystemLogErrorClassification::USER_ERROR, 'You cannot change the \'doktype\' of page \'%s\' to the desired value.', 1, [$propArr['header']], $propArr['event_pid']);
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $recuid of TYPO3\CMS\Core\DataHandling\DataHandler::log(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1394
                    $this->log($table, /** @scrutinizer ignore-type */ $id, SystemLogDatabaseAction::CHECK, 0, SystemLogErrorClassification::USER_ERROR, 'You cannot change the \'doktype\' of page \'%s\' to the desired value.', 1, [$propArr['header']], $propArr['event_pid']);
Loading history...
1395
                }
1396
                return $res;
1397
            }
1398
            if ($status === 'update') {
1399
                // This checks 1) if we should check for disallowed tables and 2) if there are records from disallowed tables on the current page
1400
                $onlyAllowedTables = $GLOBALS['PAGES_TYPES'][$value]['onlyAllowedTables'] ?? $GLOBALS['PAGES_TYPES']['default']['onlyAllowedTables'];
1401
                if ($onlyAllowedTables) {
1402
                    // use the real page id (default language)
1403
                    $recordId = $this->getDefaultLanguagePageId($id);
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $pageId of TYPO3\CMS\Core\DataHandl...DefaultLanguagePageId(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1403
                    $recordId = $this->getDefaultLanguagePageId(/** @scrutinizer ignore-type */ $id);
Loading history...
1404
                    $theWrongTables = $this->doesPageHaveUnallowedTables($recordId, $value);
0 ignored issues
show
Bug introduced by
$value of type string is incompatible with the type integer expected by parameter $doktype of TYPO3\CMS\Core\DataHandl...geHaveUnallowedTables(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1404
                    $theWrongTables = $this->doesPageHaveUnallowedTables($recordId, /** @scrutinizer ignore-type */ $value);
Loading history...
1405
                    if ($theWrongTables) {
1406
                        if ($this->enableLogging) {
1407
                            $propArr = $this->getRecordProperties($table, $id);
1408
                            $this->log($table, $id, SystemLogDatabaseAction::CHECK, 0, SystemLogErrorClassification::USER_ERROR, '\'doktype\' of page \'%s\' could not be changed because the page contains records from disallowed tables; %s', 2, [$propArr['header'], $theWrongTables], $propArr['event_pid']);
1409
                        }
1410
                        return $res;
1411
                    }
1412
                }
1413
            }
1414
        }
1415
1416
        $curValue = null;
1417
        if ((int)$id !== 0) {
1418
            // Get current value:
1419
            $curValueRec = $this->recordInfo($table, $id, $field);
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $id of TYPO3\CMS\Core\DataHandl...taHandler::recordInfo(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1419
            $curValueRec = $this->recordInfo($table, /** @scrutinizer ignore-type */ $id, $field);
Loading history...
1420
            // isset() won't work here, since values can be NULL
1421
            if ($curValueRec !== null && array_key_exists($field, $curValueRec)) {
1422
                $curValue = $curValueRec[$field];
1423
            }
1424
        }
1425
1426
        if ($table === 'be_users'
1427
            && ($field === 'admin' || $field === 'password')
1428
            && $status === 'update'
1429
        ) {
1430
            // Do not allow a non system maintainer admin to change admin flag and password of system maintainers
1431
            $systemMaintainers = array_map('intval', $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemMaintainers'] ?? []);
1432
            // False if current user is not in system maintainer list or if switch to user mode is active
1433
            $isCurrentUserSystemMaintainer = $this->BE_USER->isSystemMaintainer();
1434
            $isTargetUserInSystemMaintainerList = in_array((int)$id, $systemMaintainers, true);
1435
            if ($field === 'admin') {
1436
                $isFieldChanged = (int)$curValueRec[$field] !== (int)$value;
1437
            } else {
1438
                $isFieldChanged = $curValueRec[$field] !== $value;
1439
            }
1440
            if (!$isCurrentUserSystemMaintainer && $isTargetUserInSystemMaintainerList && $isFieldChanged) {
1441
                $value = $curValueRec[$field];
1442
                $message = GeneralUtility::makeInstance(
1443
                    FlashMessage::class,
1444
                    $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.adminCanNotChangeSystemMaintainer'),
1445
                    '',
1446
                    FlashMessage::ERROR,
1447
                    true
1448
                );
1449
                $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
1450
                $flashMessageService->getMessageQueueByIdentifier()->enqueue($message);
1451
            }
1452
        }
1453
1454
        // Getting config for the field
1455
        $tcaFieldConf = $this->resolveFieldConfigurationAndRespectColumnsOverrides($table, $field);
1456
1457
        // Create $recFID only for those types that need it
1458
        if ($tcaFieldConf['type'] === 'flex') {
1459
            $recFID = $table . ':' . $id . ':' . $field;
1460
        } else {
1461
            $recFID = null;
1462
        }
1463
1464
        // Perform processing:
1465
        $res = $this->checkValue_SW($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $realPid, $recFID, $field, [], $tscPID, ['incomingFieldArray' => $incomingFieldArray]);
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $id of TYPO3\CMS\Core\DataHandl...andler::checkValue_SW(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1465
        $res = $this->checkValue_SW($res, $value, $tcaFieldConf, $table, /** @scrutinizer ignore-type */ $id, $curValue, $status, $realPid, $recFID, $field, [], $tscPID, ['incomingFieldArray' => $incomingFieldArray]);
Loading history...
1466
        return $res;
1467
    }
1468
1469
    /**
1470
     * Use columns overrides for evaluation.
1471
     *
1472
     * Fetch the TCA ["config"] part for a specific field, including the columnsOverrides value.
1473
     * Used for checkValue purposes currently (as it takes the checkValue_currentRecord value).
1474
     *
1475
     * @param string $table
1476
     * @param string $field
1477
     * @return array
1478
     */
1479
    protected function resolveFieldConfigurationAndRespectColumnsOverrides(string $table, string $field): array
1480
    {
1481
        $tcaFieldConf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
1482
        $recordType = BackendUtility::getTCAtypeValue($table, $this->checkValue_currentRecord);
1483
        $columnsOverridesConfigOfField = $GLOBALS['TCA'][$table]['types'][$recordType]['columnsOverrides'][$field]['config'] ?? null;
1484
        if ($columnsOverridesConfigOfField) {
1485
            ArrayUtility::mergeRecursiveWithOverrule($tcaFieldConf, $columnsOverridesConfigOfField);
1486
        }
1487
        return $tcaFieldConf;
1488
    }
1489
1490
    /**
1491
     * Branches out evaluation of a field value based on its type as configured in $GLOBALS['TCA']
1492
     * Can be called for FlexForm pseudo fields as well, BUT must not have $field set if so.
1493
     *
1494
     * @param array $res The result array. The processed value (if any!) is set in the "value" key.
1495
     * @param string $value The value to set.
1496
     * @param array $tcaFieldConf Field configuration from $GLOBALS['TCA']
1497
     * @param string $table Table name
1498
     * @param int $id UID of record
1499
     * @param mixed $curValue Current value of the field
1500
     * @param string $status 'update' or 'new' flag
1501
     * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted.
1502
     * @param string $recFID Field identifier [table:uid:field] for flexforms
1503
     * @param string $field Field name. Must NOT be set if the call is for a flexform field (since flexforms are not allowed within flexforms).
1504
     * @param array $uploadedFiles
1505
     * @param int $tscPID TSconfig PID
1506
     * @param array $additionalData Additional data to be forwarded to sub-processors
1507
     * @return array Returns the evaluated $value as key "value" in this array.
1508
     * @internal should only be used from within DataHandler
1509
     */
1510
    public function checkValue_SW($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $realPid, $recFID, $field, $uploadedFiles, $tscPID, array $additionalData = null)
1511
    {
1512
        // Convert to NULL value if defined in TCA
1513
        if ($value === null && !empty($tcaFieldConf['eval']) && GeneralUtility::inList($tcaFieldConf['eval'], 'null')) {
0 ignored issues
show
introduced by
The condition $value === null is always false.
Loading history...
1514
            $res = ['value' => null];
1515
            return $res;
1516
        }
1517
1518
        switch ($tcaFieldConf['type']) {
1519
            case 'text':
1520
                $res = $this->checkValueForText($value, $tcaFieldConf, $table, $id, $realPid, $field);
1521
                break;
1522
            case 'passthrough':
1523
            case 'imageManipulation':
1524
            case 'user':
1525
                $res['value'] = $value;
1526
                break;
1527
            case 'input':
1528
                $res = $this->checkValueForInput($value, $tcaFieldConf, $table, $id, $realPid, $field);
1529
                break;
1530
            case 'slug':
1531
                $res = $this->checkValueForSlug((string)$value, $tcaFieldConf, $table, $id, (int)$realPid, $field, $additionalData['incomingFieldArray'] ?? []);
1532
                break;
1533
            case 'check':
1534
                $res = $this->checkValueForCheck($res, $value, $tcaFieldConf, $table, $id, $realPid, $field);
1535
                break;
1536
            case 'radio':
1537
                $res = $this->checkValueForRadio($res, $value, $tcaFieldConf, $table, $id, $realPid, $field);
1538
                break;
1539
            case 'group':
1540
            case 'select':
1541
                $res = $this->checkValueForGroupSelect($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $recFID, $uploadedFiles, $field);
1542
                break;
1543
            case 'inline':
1544
                $res = $this->checkValueForInline($res, $value, $tcaFieldConf, $table, $id, $status, $field, $additionalData);
1545
                break;
1546
            case 'flex':
1547
                // FlexForms are only allowed for real fields.
1548
                if ($field) {
1549
                    $res = $this->checkValueForFlex($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $realPid, $recFID, $tscPID, $uploadedFiles, $field);
1550
                }
1551
                break;
1552
            default:
1553
                // Do nothing
1554
        }
1555
        $res = $this->checkValueForInternalReferences($res, $value, $tcaFieldConf, $table, $id, $field);
1556
        return $res;
1557
    }
1558
1559
    /**
1560
     * Checks values that are used for internal references. If the provided $value
1561
     * is a NEW-identifier, the direct processing is stopped. Instead, the value is
1562
     * forwarded to the remap-stack to be post-processed and resolved into a proper
1563
     * UID after all data has been resolved.
1564
     *
1565
     * This method considers TCA types that cannot handle and resolve these internal
1566
     * values directly, like 'passthrough', 'none' or 'user'. Values are only modified
1567
     * here if the $field is used as 'transOrigPointerField' or 'translationSource'.
1568
     *
1569
     * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
1570
     * @param string $value The value to set.
1571
     * @param array $tcaFieldConf Field configuration from TCA
1572
     * @param string $table Table name
1573
     * @param int $id UID of record
1574
     * @param string $field The field name
1575
     * @return array The result array. The processed value (if any!) is set in the "value" key.
1576
     */
1577
    protected function checkValueForInternalReferences(array $res, $value, $tcaFieldConf, $table, $id, $field)
1578
    {
1579
        $relevantFieldNames = [
1580
            $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] ?? null,
1581
            $GLOBALS['TCA'][$table]['ctrl']['translationSource'] ?? null,
1582
        ];
1583
1584
        if (
1585
            // in case field is empty
1586
            empty($field)
1587
            // in case the field is not relevant
1588
            || !in_array($field, $relevantFieldNames)
1589
            // in case the 'value' index has been unset already
1590
            || !array_key_exists('value', $res)
1591
            // in case it's not a NEW-identifier
1592
            || strpos($value, 'NEW') === false
1593
        ) {
1594
            return $res;
1595
        }
1596
1597
        $valueArray = [$value];
1598
        $this->remapStackRecords[$table][$id] = ['remapStackIndex' => count($this->remapStack)];
1599
        $this->addNewValuesToRemapStackChildIds($valueArray);
1600
        $this->remapStack[] = [
1601
            'args' => [$valueArray, $tcaFieldConf, $id, $table, $field],
1602
            'pos' => ['valueArray' => 0, 'tcaFieldConf' => 1, 'id' => 2, 'table' => 3],
1603
            'field' => $field
1604
        ];
1605
        unset($res['value']);
1606
1607
        return $res;
1608
    }
1609
1610
    /**
1611
     * Evaluate "text" type values.
1612
     *
1613
     * @param string $value The value to set.
1614
     * @param array $tcaFieldConf Field configuration from TCA
1615
     * @param string $table Table name
1616
     * @param int $id UID of record
1617
     * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted.
1618
     * @param string $field Field name
1619
     * @return array $res The result array. The processed value (if any!) is set in the "value" key.
1620
     */
1621
    protected function checkValueForText($value, $tcaFieldConf, $table, $id, $realPid, $field)
1622
    {
1623
        if (isset($tcaFieldConf['eval']) && $tcaFieldConf['eval'] !== '') {
1624
            $cacheId = $this->getFieldEvalCacheIdentifier($tcaFieldConf['eval']);
1625
            $evalCodesArray = $this->runtimeCache->get($cacheId);
1626
            if (!is_array($evalCodesArray)) {
1627
                $evalCodesArray = GeneralUtility::trimExplode(',', $tcaFieldConf['eval'], true);
1628
                $this->runtimeCache->set($cacheId, $evalCodesArray);
1629
            }
1630
            $valueArray = $this->checkValue_text_Eval($value, $evalCodesArray, $tcaFieldConf['is_in']);
1631
        } else {
1632
            $valueArray = ['value' => $value];
1633
        }
1634
1635
        // Handle richtext transformations
1636
        if ($this->dontProcessTransformations) {
1637
            return $valueArray;
1638
        }
1639
        // Keep null as value
1640
        if ($value === null) {
0 ignored issues
show
introduced by
The condition $value === null is always false.
Loading history...
1641
            return $valueArray;
1642
        }
1643
        if (isset($tcaFieldConf['enableRichtext']) && (bool)$tcaFieldConf['enableRichtext'] === true) {
1644
            $recordType = BackendUtility::getTCAtypeValue($table, $this->checkValue_currentRecord);
1645
            $richtextConfigurationProvider = GeneralUtility::makeInstance(Richtext::class);
1646
            $richtextConfiguration = $richtextConfigurationProvider->getConfiguration($table, $field, $realPid, $recordType, $tcaFieldConf);
1647
            $rteParser = GeneralUtility::makeInstance(RteHtmlParser::class);
1648
            $valueArray['value'] = $rteParser->transformTextForPersistence((string)$value, $richtextConfiguration['proc.'] ?? []);
1649
        }
1650
1651
        return $valueArray;
1652
    }
1653
1654
    /**
1655
     * Evaluate "input" type values.
1656
     *
1657
     * @param string $value The value to set.
1658
     * @param array $tcaFieldConf Field configuration from TCA
1659
     * @param string $table Table name
1660
     * @param int $id UID of record
1661
     * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted.
1662
     * @param string $field Field name
1663
     * @return array $res The result array. The processed value (if any!) is set in the "value" key.
1664
     */
1665
    protected function checkValueForInput($value, $tcaFieldConf, $table, $id, $realPid, $field)
1666
    {
1667
        // Handle native date/time fields
1668
        $isDateOrDateTimeField = false;
1669
        $format = '';
1670
        $emptyValue = '';
1671
        $dateTimeTypes = QueryHelper::getDateTimeTypes();
1672
        // normal integer "date" fields (timestamps) are handled in checkValue_input_Eval
1673
        if (isset($tcaFieldConf['dbType']) && in_array($tcaFieldConf['dbType'], $dateTimeTypes, true)) {
1674
            if (empty($value)) {
1675
                $value = null;
1676
            } else {
1677
                $isDateOrDateTimeField = true;
1678
                $dateTimeFormats = QueryHelper::getDateTimeFormats();
1679
                $format = $dateTimeFormats[$tcaFieldConf['dbType']]['format'];
1680
1681
                // Convert the date/time into a timestamp for the sake of the checks
1682
                $emptyValue = $dateTimeFormats[$tcaFieldConf['dbType']]['empty'];
1683
                // We expect the ISO 8601 $value to contain a UTC timezone specifier.
1684
                // We explicitly fallback to UTC if no timezone specifier is given (e.g. for copy operations).
1685
                $dateTime = new \DateTime($value, new \DateTimeZone('UTC'));
1686
                // The timestamp (UTC) returned by getTimestamp() will be converted to
1687
                // a local time string by gmdate() later.
1688
                $value = $value === $emptyValue ? null : $dateTime->getTimestamp();
1689
            }
1690
        }
1691
        // Secures the string-length to be less than max.
1692
        if (isset($tcaFieldConf['max']) && (int)$tcaFieldConf['max'] > 0) {
1693
            $value = mb_substr((string)$value, 0, (int)$tcaFieldConf['max'], 'utf-8');
1694
        }
1695
1696
        if (empty($tcaFieldConf['eval'])) {
1697
            $res = ['value' => $value];
1698
        } else {
1699
            // Process evaluation settings:
1700
            $cacheId = $this->getFieldEvalCacheIdentifier($tcaFieldConf['eval']);
1701
            $evalCodesArray = $this->runtimeCache->get($cacheId);
1702
            if (!is_array($evalCodesArray)) {
1703
                $evalCodesArray = GeneralUtility::trimExplode(',', $tcaFieldConf['eval'], true);
1704
                $this->runtimeCache->set($cacheId, $evalCodesArray);
1705
            }
1706
1707
            $res = $this->checkValue_input_Eval($value, $evalCodesArray, $tcaFieldConf['is_in'] ?? '', $table);
1708
            if (isset($tcaFieldConf['dbType']) && isset($res['value']) && !$res['value']) {
1709
                // set the value to null if we have an empty value for a native field
1710
                $res['value'] = null;
1711
            }
1712
1713
            // Process UNIQUE settings:
1714
            // Field is NOT set for flexForms - which also means that uniqueInPid and unique is NOT available for flexForm fields! Also getUnique should not be done for versioning
1715
            if ($field && !empty($res['value'])) {
1716
                if (in_array('uniqueInPid', $evalCodesArray, true)) {
1717
                    $res['value'] = $this->getUnique($table, $field, $res['value'], $id, $realPid);
1718
                }
1719
                if ($res['value'] && in_array('unique', $evalCodesArray, true)) {
1720
                    $res['value'] = $this->getUnique($table, $field, $res['value'], $id);
1721
                }
1722
            }
1723
        }
1724
1725
        // Checking range of value:
1726
        // @todo: The "checkbox" option was removed for type=input, this check could be probably relaxed?
1727
        if (
1728
            isset($tcaFieldConf['range']) && $tcaFieldConf['range']
1729
            && (!isset($tcaFieldConf['checkbox']) || $res['value'] != $tcaFieldConf['checkbox'])
1730
            && (!isset($tcaFieldConf['default']) || (int)$res['value'] !== (int)$tcaFieldConf['default'])
1731
        ) {
1732
            if (isset($tcaFieldConf['range']['upper']) && (int)$res['value'] > (int)$tcaFieldConf['range']['upper']) {
1733
                $res['value'] = (int)$tcaFieldConf['range']['upper'];
1734
            }
1735
            if (isset($tcaFieldConf['range']['lower']) && (int)$res['value'] < (int)$tcaFieldConf['range']['lower']) {
1736
                $res['value'] = (int)$tcaFieldConf['range']['lower'];
1737
            }
1738
        }
1739
1740
        // Handle native date/time fields
1741
        if ($isDateOrDateTimeField) {
1742
            // Convert the timestamp back to a date/time
1743
            $res['value'] = $res['value'] ? gmdate($format, $res['value']) : $emptyValue;
1744
        }
1745
        return $res;
1746
    }
1747
1748
    /**
1749
     * Evaluate "slug" type values.
1750
     *
1751
     * @param string $value The value to set.
1752
     * @param array $tcaFieldConf Field configuration from TCA
1753
     * @param string $table Table name
1754
     * @param int $id UID of record
1755
     * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted.
1756
     * @param string $field Field name
1757
     * @param array $incomingFieldArray the fields being explicitly set by the outside (unlike $fieldArray) for the record
1758
     * @return array $res The result array. The processed value (if any!) is set in the "value" key.
1759
     * @see SlugEnricher
1760
     * @see SlugHelper
1761
     */
1762
    protected function checkValueForSlug(string $value, array $tcaFieldConf, string $table, $id, int $realPid, string $field, array $incomingFieldArray = []): array
1763
    {
1764
        $workspaceId = $this->BE_USER->workspace;
1765
        $helper = GeneralUtility::makeInstance(SlugHelper::class, $table, $field, $tcaFieldConf, $workspaceId);
1766
        $fullRecord = array_replace_recursive($this->checkValue_currentRecord, $incomingFieldArray ?? []);
1767
        // Generate a value if there is none, otherwise ensure that all characters are cleaned up
1768
        if ($value === '') {
1769
            $value = $helper->generate($fullRecord, $realPid);
1770
        } else {
1771
            $value = $helper->sanitize($value);
1772
        }
1773
1774
        // Return directly in case no evaluations are defined
1775
        if (empty($tcaFieldConf['eval'])) {
1776
            return ['value' => $value];
1777
        }
1778
1779
        $state = RecordStateFactory::forName($table)
1780
            ->fromArray($fullRecord, $realPid, $id);
1781
        $evalCodesArray = GeneralUtility::trimExplode(',', $tcaFieldConf['eval'], true);
1782
        if (in_array('unique', $evalCodesArray, true)) {
1783
            $value = $helper->buildSlugForUniqueInTable($value, $state);
1784
        }
1785
        if (in_array('uniqueInSite', $evalCodesArray, true)) {
1786
            $value = $helper->buildSlugForUniqueInSite($value, $state);
1787
        }
1788
        if (in_array('uniqueInPid', $evalCodesArray, true)) {
1789
            $value = $helper->buildSlugForUniqueInPid($value, $state);
1790
        }
1791
1792
        return ['value' => $value];
1793
    }
1794
1795
    /**
1796
     * Evaluates 'check' type values.
1797
     *
1798
     * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
1799
     * @param string $value The value to set.
1800
     * @param array $tcaFieldConf Field configuration from TCA
1801
     * @param string $table Table name
1802
     * @param int $id UID of record
1803
     * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted.
1804
     * @param string $field Field name
1805
     * @return array Modified $res array
1806
     */
1807
    protected function checkValueForCheck($res, $value, $tcaFieldConf, $table, $id, $realPid, $field)
1808
    {
1809
        $items = $tcaFieldConf['items'];
1810
        if (!empty($tcaFieldConf['itemsProcFunc'])) {
1811
            /** @var ItemProcessingService $processingService */
1812
            $processingService = GeneralUtility::makeInstance(ItemProcessingService::class);
1813
            $items = $processingService->getProcessingItems(
1814
                $table,
1815
                $realPid,
1816
                $field,
1817
                $this->checkValue_currentRecord,
1818
                $tcaFieldConf,
1819
                $tcaFieldConf['items']
1820
            );
1821
        }
1822
1823
        $itemC = 0;
1824
        if ($items !== null) {
1825
            $itemC = count($items);
1826
        }
1827
        if (!$itemC) {
1828
            $itemC = 1;
1829
        }
1830
        $maxV = (2 ** $itemC) - 1;
1831
        if ($value < 0) {
1832
            // @todo: throw LogicException here? Negative values for checkbox items do not make sense and indicate a coding error.
1833
            $value = 0;
1834
        }
1835
        if ($value > $maxV) {
1836
            // @todo: This case is pretty ugly: If there is an itemsProcFunc registered, and if it returns a dynamic,
1837
            // @todo: changing list of items, then it may happen that a value is transformed and vanished checkboxes
1838
            // @todo: are permanently removed from the value.
1839
            // @todo: Suggestion: Throw an exception instead? Maybe a specific, catchable exception that generates a
1840
            // @todo: error message to the user - dynamic item sets via itemProcFunc on check would be a bad idea anyway.
1841
            $value = $value & $maxV;
1842
        }
1843
        if ($field && $value > 0 && !empty($tcaFieldConf['eval'])) {
1844
            $evalCodesArray = GeneralUtility::trimExplode(',', $tcaFieldConf['eval'], true);
1845
            $otherRecordsWithSameValue = [];
1846
            $maxCheckedRecords = 0;
1847
            if (in_array('maximumRecordsCheckedInPid', $evalCodesArray, true)) {
1848
                $otherRecordsWithSameValue = $this->getRecordsWithSameValue($table, $id, $field, $value, $realPid);
1849
                $maxCheckedRecords = (int)$tcaFieldConf['validation']['maximumRecordsCheckedInPid'];
1850
            }
1851
            if (in_array('maximumRecordsChecked', $evalCodesArray, true)) {
1852
                $otherRecordsWithSameValue = $this->getRecordsWithSameValue($table, $id, $field, $value);
1853
                $maxCheckedRecords = (int)$tcaFieldConf['validation']['maximumRecordsChecked'];
1854
            }
1855
1856
            // there are more than enough records with value "1" in the DB
1857
            // if so, set this value to "0" again
1858
            if ($maxCheckedRecords && count($otherRecordsWithSameValue) >= $maxCheckedRecords) {
1859
                $value = 0;
1860
                $this->log($table, $id, SystemLogDatabaseAction::CHECK, 0, SystemLogErrorClassification::USER_ERROR, 'Could not activate checkbox for field "%s". A total of %s record(s) can have this checkbox activated. Uncheck other records first in order to activate the checkbox of this record.', -1, [$this->getLanguageService()->sL(BackendUtility::getItemLabel($table, $field)), $maxCheckedRecords]);
1861
            }
1862
        }
1863
        $res['value'] = $value;
1864
        return $res;
1865
    }
1866
1867
    /**
1868
     * Evaluates 'radio' type values.
1869
     *
1870
     * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
1871
     * @param string $value The value to set.
1872
     * @param array $tcaFieldConf Field configuration from TCA
1873
     * @param string $table The table of the record
1874
     * @param int $id The id of the record
1875
     * @param int $pid The pid of the record
1876
     * @param string $field The field to check
1877
     * @return array Modified $res array
1878
     */
1879
    protected function checkValueForRadio($res, $value, $tcaFieldConf, $table, $id, $pid, $field)
1880
    {
1881
        if (is_array($tcaFieldConf['items'])) {
1882
            foreach ($tcaFieldConf['items'] as $set) {
1883
                if ((string)$set[1] === (string)$value) {
1884
                    $res['value'] = $value;
1885
                    break;
1886
                }
1887
            }
1888
        }
1889
1890
        // if no value was found and an itemsProcFunc is defined, check that for the value
1891
        if ($tcaFieldConf['itemsProcFunc'] && empty($res['value'])) {
1892
            $processingService = GeneralUtility::makeInstance(ItemProcessingService::class);
1893
            $processedItems = $processingService->getProcessingItems(
1894
                $table,
1895
                $pid,
1896
                $field,
1897
                $this->checkValue_currentRecord,
1898
                $tcaFieldConf,
1899
                $tcaFieldConf['items']
1900
            );
1901
1902
            foreach ($processedItems as $set) {
1903
                if ((string)$set[1] === (string)$value) {
1904
                    $res['value'] = $value;
1905
                    break;
1906
                }
1907
            }
1908
        }
1909
1910
        return $res;
1911
    }
1912
1913
    /**
1914
     * Evaluates 'group' or 'select' type values.
1915
     *
1916
     * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
1917
     * @param string|array $value The value to set.
1918
     * @param array $tcaFieldConf Field configuration from TCA
1919
     * @param string $table Table name
1920
     * @param int $id UID of record
1921
     * @param mixed $curValue Current value of the field
1922
     * @param string $status 'update' or 'new' flag
1923
     * @param string $recFID Field identifier [table:uid:field] for flexforms
1924
     * @param array $uploadedFiles
1925
     * @param string $field Field name
1926
     * @return array Modified $res array
1927
     */
1928
    protected function checkValueForGroupSelect($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $recFID, $uploadedFiles, $field)
1929
    {
1930
        // Detecting if value sent is an array and if so, implode it around a comma:
1931
        if (is_array($value)) {
1932
            $value = implode(',', $value);
1933
        }
1934
        // This converts all occurrences of '&#123;' to the byte 123 in the string - this is needed in very rare cases where file names with special characters (e.g. ???, umlaut) gets sent to the server as HTML entities instead of bytes. The error is done only by MSIE, not Mozilla and Opera.
1935
        // Anyway, this should NOT disturb anything else:
1936
        $value = $this->convNumEntityToByteValue($value);
1937
        // When values are sent as group or select they come as comma-separated values which are exploded by this function:
1938
        $valueArray = $this->checkValue_group_select_explodeSelectGroupValue($value);
1939
        // If multiple is not set, remove duplicates:
1940
        if (!$tcaFieldConf['multiple']) {
1941
            $valueArray = array_unique($valueArray);
1942
        }
1943
        // If an exclusive key is found, discard all others:
1944
        if ($tcaFieldConf['type'] === 'select' && $tcaFieldConf['exclusiveKeys']) {
1945
            $exclusiveKeys = GeneralUtility::trimExplode(',', $tcaFieldConf['exclusiveKeys']);
1946
            foreach ($valueArray as $index => $key) {
1947
                if (in_array($key, $exclusiveKeys, true)) {
1948
                    $valueArray = [$index => $key];
1949
                    break;
1950
                }
1951
            }
1952
        }
1953
        // This could be a good spot for parsing the array through a validation-function which checks if the values are correct (except that database references are not in their final form - but that is the point, isn't it?)
1954
        // NOTE!!! Must check max-items of files before the later check because that check would just leave out file names if there are too many!!
1955
        $valueArray = $this->applyFiltersToValues($tcaFieldConf, $valueArray);
1956
        // Checking for select / authMode, removing elements from $valueArray if any of them is not allowed!
1957
        if ($tcaFieldConf['type'] === 'select' && $tcaFieldConf['authMode']) {
1958
            $preCount = count($valueArray);
1959
            foreach ($valueArray as $index => $key) {
1960
                if (!$this->BE_USER->checkAuthMode($table, $field, $key, $tcaFieldConf['authMode'])) {
1961
                    unset($valueArray[$index]);
1962
                }
1963
            }
1964
            // During the check it turns out that the value / all values were removed - we respond by simply returning an empty array so nothing is written to DB for this field.
1965
            if ($preCount && empty($valueArray)) {
1966
                return [];
1967
            }
1968
        }
1969
        // For select types which has a foreign table attached:
1970
        $unsetResult = false;
1971
        if (
1972
            $tcaFieldConf['type'] === 'group' && $tcaFieldConf['internal_type'] === 'db'
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($tcaFieldConf['type'] =...ecial'] === 'languages', Probably Intended Meaning: $tcaFieldConf['type'] ==...cial'] === 'languages')
Loading history...
1973
            || $tcaFieldConf['type'] === 'select' && ($tcaFieldConf['foreign_table'] || isset($tcaFieldConf['special']) && $tcaFieldConf['special'] === 'languages')
1974
        ) {
1975
            // check, if there is a NEW... id in the value, that should be substituted later
1976
            if (strpos($value, 'NEW') !== false) {
1977
                $this->remapStackRecords[$table][$id] = ['remapStackIndex' => count($this->remapStack)];
1978
                $this->addNewValuesToRemapStackChildIds($valueArray);
1979
                $this->remapStack[] = [
1980
                    'func' => 'checkValue_group_select_processDBdata',
1981
                    'args' => [$valueArray, $tcaFieldConf, $id, $status, $tcaFieldConf['type'], $table, $field],
1982
                    'pos' => ['valueArray' => 0, 'tcaFieldConf' => 1, 'id' => 2, 'table' => 5],
1983
                    'field' => $field
1984
                ];
1985
                $unsetResult = true;
1986
            } else {
1987
                $valueArray = $this->checkValue_group_select_processDBdata($valueArray, $tcaFieldConf, $id, $status, $tcaFieldConf['type'], $table, $field);
1988
            }
1989
        }
1990
        if (!$unsetResult) {
1991
            $newVal = $this->checkValue_checkMax($tcaFieldConf, $valueArray);
1992
            $res['value'] = $this->castReferenceValue(implode(',', $newVal), $tcaFieldConf);
1993
        } else {
1994
            unset($res['value']);
1995
        }
1996
        return $res;
1997
    }
1998
1999
    /**
2000
     * Applies the filter methods from a column's TCA configuration to a value array.
2001
     *
2002
     * @param array $tcaFieldConfiguration
2003
     * @param array $values
2004
     * @return array|mixed
2005
     * @throws \RuntimeException
2006
     */
2007
    protected function applyFiltersToValues(array $tcaFieldConfiguration, array $values)
2008
    {
2009
        if (empty($tcaFieldConfiguration['filter']) || !is_array($tcaFieldConfiguration['filter'])) {
2010
            return $values;
2011
        }
2012
        foreach ($tcaFieldConfiguration['filter'] as $filter) {
2013
            if (empty($filter['userFunc'])) {
2014
                continue;
2015
            }
2016
            $parameters = $filter['parameters'] ?: [];
2017
            $parameters['values'] = $values;
2018
            $parameters['tcaFieldConfig'] = $tcaFieldConfiguration;
2019
            $values = GeneralUtility::callUserFunction($filter['userFunc'], $parameters, $this);
2020
            if (!is_array($values)) {
2021
                throw new \RuntimeException('Failed calling filter userFunc.', 1336051942);
2022
            }
2023
        }
2024
        return $values;
2025
    }
2026
2027
    /**
2028
     * Evaluates 'flex' type values.
2029
     *
2030
     * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
2031
     * @param string|array $value The value to set.
2032
     * @param array $tcaFieldConf Field configuration from TCA
2033
     * @param string $table Table name
2034
     * @param int $id UID of record
2035
     * @param mixed $curValue Current value of the field
2036
     * @param string $status 'update' or 'new' flag
2037
     * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted.
2038
     * @param string $recFID Field identifier [table:uid:field] for flexforms
2039
     * @param int $tscPID TSconfig PID
2040
     * @param array $uploadedFiles Uploaded files for the field
2041
     * @param string $field Field name
2042
     * @return array Modified $res array
2043
     */
2044
    protected function checkValueForFlex($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $realPid, $recFID, $tscPID, $uploadedFiles, $field)
2045
    {
2046
        if (is_array($value)) {
2047
            // This value is necessary for flex form processing to happen on flexform fields in page records when they are copied.
2048
            // Problem: when copying a page, flexform XML comes along in the array for the new record - but since $this->checkValue_currentRecord
2049
            // does not have a uid or pid for that sake, the FlexFormTools->getDataStructureIdentifier() function returns no good DS. For new
2050
            // records we do know the expected PID so therefore we send that with this special parameter. Only active when larger than zero.
2051
            $row = $this->checkValue_currentRecord;
2052
            if ($status === 'new') {
2053
                $row['pid'] = $realPid;
2054
            }
2055
2056
            $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
2057
2058
            // Get data structure. The methods may throw various exceptions, with some of them being
2059
            // ok in certain scenarios, for instance on new record rows. Those are ok to "eat" here
2060
            // and substitute with a dummy DS.
2061
            $dataStructureArray = ['sheets' => ['sDEF' => []]];
2062
            try {
2063
                $dataStructureIdentifier = $flexFormTools->getDataStructureIdentifier(
2064
                    ['config' => $tcaFieldConf],
2065
                    $table,
2066
                    $field,
2067
                    $row
2068
                );
2069
2070
                $dataStructureArray = $flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
2071
            } catch (InvalidParentRowException|InvalidParentRowLoopException|InvalidParentRowRootException|InvalidPointerFieldValueException|InvalidIdentifierException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
2072
            }
2073
2074
            // Get current value array:
2075
            $currentValueArray = (string)$curValue !== '' ? GeneralUtility::xml2array($curValue) : [];
2076
            if (!is_array($currentValueArray)) {
2077
                $currentValueArray = [];
2078
            }
2079
            // Remove all old meta for languages...
2080
            // Evaluation of input values:
2081
            $value['data'] = $this->checkValue_flex_procInData($value['data'] ?? [], $currentValueArray['data'] ?? [], $uploadedFiles['data'] ?? [], $dataStructureArray, [$table, $id, $curValue, $status, $realPid, $recFID, $tscPID]);
2082
            // Create XML from input value:
2083
            $xmlValue = $this->checkValue_flexArray2Xml($value, true);
2084
2085
            // Here we convert the currently submitted values BACK to an array, then merge the two and then BACK to XML again. This is needed to ensure the charsets are the same
2086
            // (provided that the current value was already stored IN the charset that the new value is converted to).
2087
            $arrValue = GeneralUtility::xml2array($xmlValue);
2088
2089
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkFlexFormValue'] ?? [] as $className) {
2090
                $hookObject = GeneralUtility::makeInstance($className);
2091
                if (method_exists($hookObject, 'checkFlexFormValue_beforeMerge')) {
2092
                    $hookObject->checkFlexFormValue_beforeMerge($this, $currentValueArray, $arrValue);
2093
                }
2094
            }
2095
2096
            ArrayUtility::mergeRecursiveWithOverrule($currentValueArray, $arrValue);
2097
            $xmlValue = $this->checkValue_flexArray2Xml($currentValueArray, true);
2098
2099
            // Action commands (sorting order and removals of elements) for flexform sections,
2100
            // see FormEngine for the use of this GP parameter
2101
            $actionCMDs = GeneralUtility::_GP('_ACTION_FLEX_FORMdata');
2102
            if (is_array($actionCMDs[$table][$id][$field]['data'] ?? null)) {
2103
                $arrValue = GeneralUtility::xml2array($xmlValue);
2104
                $this->_ACTION_FLEX_FORMdata($arrValue['data'], $actionCMDs[$table][$id][$field]['data']);
2105
                $xmlValue = $this->checkValue_flexArray2Xml($arrValue, true);
2106
            }
2107
            // Create the value XML:
2108
            $res['value'] = '';
2109
            $res['value'] .= $xmlValue;
2110
        } else {
2111
            // Passthrough...:
2112
            $res['value'] = $value;
2113
        }
2114
2115
        return $res;
2116
    }
2117
2118
    /**
2119
     * Converts an array to FlexForm XML
2120
     *
2121
     * @param array $array Array with FlexForm data
2122
     * @param bool $addPrologue If set, the XML prologue is returned as well.
2123
     * @return string Input array converted to XML
2124
     * @internal should only be used from within DataHandler
2125
     */
2126
    public function checkValue_flexArray2Xml($array, $addPrologue = false)
2127
    {
2128
        /** @var FlexFormTools $flexObj */
2129
        $flexObj = GeneralUtility::makeInstance(FlexFormTools::class);
2130
        return $flexObj->flexArray2Xml($array, $addPrologue);
2131
    }
2132
2133
    /**
2134
     * Actions for flex form element (move, delete)
2135
     * allows to remove and move flexform sections
2136
     *
2137
     * @param array $valueArray by reference
2138
     * @param array $actionCMDs
2139
     */
2140
    protected function _ACTION_FLEX_FORMdata(&$valueArray, $actionCMDs)
2141
    {
2142
        if (!is_array($valueArray) || !is_array($actionCMDs)) {
0 ignored issues
show
introduced by
The condition is_array($valueArray) is always true.
Loading history...
introduced by
The condition is_array($actionCMDs) is always true.
Loading history...
2143
            return;
2144
        }
2145
2146
        foreach ($actionCMDs as $key => $value) {
2147
            if ($key === '_ACTION') {
2148
                // First, check if there are "commands":
2149
                if (empty(array_filter($actionCMDs[$key]))) {
2150
                    continue;
2151
                }
2152
2153
                asort($actionCMDs[$key]);
2154
                $newValueArray = [];
2155
                foreach ($actionCMDs[$key] as $idx => $order) {
2156
                    // Just one reflection here: It is clear that when removing elements from a flexform, then we will get lost
2157
                    // files unless we act on this delete operation by traversing and deleting files that were referred to.
2158
                    if ($order !== 'DELETE') {
2159
                        $newValueArray[$idx] = $valueArray[$idx];
2160
                    }
2161
                    unset($valueArray[$idx]);
2162
                }
2163
                $valueArray += $newValueArray;
2164
            } elseif (is_array($actionCMDs[$key]) && isset($valueArray[$key])) {
2165
                $this->_ACTION_FLEX_FORMdata($valueArray[$key], $actionCMDs[$key]);
2166
            }
2167
        }
2168
    }
2169
2170
    /**
2171
     * Evaluates 'inline' type values.
2172
     * (partly copied from the select_group function on this issue)
2173
     *
2174
     * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
2175
     * @param string $value The value to set.
2176
     * @param array $tcaFieldConf Field configuration from TCA
2177
     * @param array $PP Additional parameters in a numeric array: $table,$id,$curValue,$status,$realPid,$recFID
2178
     * @param string $field Field name
2179
     * @param array $additionalData Additional data to be forwarded to sub-processors
2180
     * @internal should only be used from within DataHandler
2181
     */
2182
    public function checkValue_inline($res, $value, $tcaFieldConf, $PP, $field, array $additionalData = null)
2183
    {
2184
        [$table, $id, , $status] = $PP;
2185
        $this->checkValueForInline($res, $value, $tcaFieldConf, $table, $id, $status, $field, $additionalData);
2186
    }
2187
2188
    /**
2189
     * Evaluates 'inline' type values.
2190
     * (partly copied from the select_group function on this issue)
2191
     *
2192
     * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
2193
     * @param string $value The value to set.
2194
     * @param array $tcaFieldConf Field configuration from TCA
2195
     * @param string $table Table name
2196
     * @param int $id UID of record
2197
     * @param string $status 'update' or 'new' flag
2198
     * @param string $field Field name
2199
     * @param array $additionalData Additional data to be forwarded to sub-processors
2200
     * @return array|bool Modified $res array
2201
     * @internal should only be used from within DataHandler
2202
     */
2203
    public function checkValueForInline($res, $value, $tcaFieldConf, $table, $id, $status, $field, array $additionalData = null)
2204
    {
2205
        if (!$tcaFieldConf['foreign_table']) {
2206
            // Fatal error, inline fields should always have a foreign_table defined
2207
            return false;
2208
        }
2209
        // When values are sent they come as comma-separated values which are exploded by this function:
2210
        $valueArray = GeneralUtility::trimExplode(',', $value);
2211
        // Remove duplicates: (should not be needed)
2212
        $valueArray = array_unique($valueArray);
2213
        // Example for received data:
2214
        // $value = 45,NEW4555fdf59d154,12,123
2215
        // We need to decide whether we use the stack or can save the relation directly.
2216
        if (!empty($value) && (strpos($value, 'NEW') !== false || !MathUtility::canBeInterpretedAsInteger($id))) {
2217
            $this->remapStackRecords[$table][$id] = ['remapStackIndex' => count($this->remapStack)];
2218
            $this->addNewValuesToRemapStackChildIds($valueArray);
2219
            $this->remapStack[] = [
2220
                'func' => 'checkValue_inline_processDBdata',
2221
                'args' => [$valueArray, $tcaFieldConf, $id, $status, $table, $field, $additionalData],
2222
                'pos' => ['valueArray' => 0, 'tcaFieldConf' => 1, 'id' => 2, 'table' => 4],
2223
                'additionalData' => $additionalData,
2224
                'field' => $field,
2225
            ];
2226
            unset($res['value']);
2227
        } elseif ($value || MathUtility::canBeInterpretedAsInteger($id)) {
2228
            $res['value'] = $this->checkValue_inline_processDBdata($valueArray, $tcaFieldConf, $id, $status, $table, $field, $additionalData);
2229
        }
2230
        return $res;
2231
    }
2232
2233
    /**
2234
     * Checks if a fields has more items than defined via TCA in maxitems.
2235
     * If there are more items than allowed, the item list is truncated to the defined number.
2236
     *
2237
     * @param array $tcaFieldConf Field configuration from TCA
2238
     * @param array $valueArray Current value array of items
2239
     * @return array The truncated value array of items
2240
     * @internal should only be used from within DataHandler
2241
     */
2242
    public function checkValue_checkMax($tcaFieldConf, $valueArray)
2243
    {
2244
        // BTW, checking for min and max items here does NOT make any sense when MM is used because the above function
2245
        // calls will just return an array with a single item (the count) if MM is used... Why didn't I perform the check
2246
        // before? Probably because we could not evaluate the validity of record uids etc... Hmm...
2247
        // NOTE to the comment: It's not really possible to check for too few items, because you must then determine first,
2248
        // if the field is actual used regarding the CType.
2249
        $maxitems = isset($tcaFieldConf['maxitems']) ? (int)$tcaFieldConf['maxitems'] : 99999;
2250
        return array_slice($valueArray, 0, $maxitems);
2251
    }
2252
2253
    /*********************************************
2254
     *
2255
     * Helper functions for evaluation functions.
2256
     *
2257
     ********************************************/
2258
    /**
2259
     * Gets a unique value for $table/$id/$field based on $value
2260
     *
2261
     * @param string $table Table name
2262
     * @param string $field Field name for which $value must be unique
2263
     * @param string $value Value string.
2264
     * @param int $id UID to filter out in the lookup (the record itself...)
2265
     * @param int $newPid If set, the value will be unique for this PID
2266
     * @return string Modified value (if not-unique). Will be the value appended with a number (until 100, then the function just breaks).
2267
     * @todo: consider workspaces, especially when publishing a unique value which has a unique value already in live
2268
     * @internal should only be used from within DataHandler
2269
     */
2270
    public function getUnique($table, $field, $value, $id, $newPid = 0)
2271
    {
2272
        if (!is_array($GLOBALS['TCA'][$table]) || !is_array($GLOBALS['TCA'][$table]['columns'][$field])) {
2273
            // Field is not configured in TCA
2274
            return $value;
2275
        }
2276
2277
        if ((string)$GLOBALS['TCA'][$table]['columns'][$field]['l10n_mode'] === 'exclude') {
2278
            $transOrigPointerField = $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'];
2279
            $l10nParent = (int)$this->checkValue_currentRecord[$transOrigPointerField];
2280
            if ($l10nParent > 0) {
2281
                // Current record is a translation and l10n_mode "exclude" just copies the value from source language
2282
                return $value;
2283
            }
2284
        }
2285
2286
        $newValue = $originalValue = $value;
2287
        $statement = $this->getUniqueCountStatement($newValue, $table, $field, (int)$id, (int)$newPid);
2288
        // For as long as records with the test-value existing, try again (with incremented numbers appended)
2289
        if ($statement->fetchColumn()) {
2290
            for ($counter = 0; $counter <= 100; $counter++) {
2291
                $newValue = $value . $counter;
2292
                $statement->bindValue(1, $newValue);
2293
                $statement->execute();
2294
                if (!$statement->fetchColumn()) {
2295
                    break;
2296
                }
2297
            }
2298
        }
2299
2300
        if ($originalValue !== $newValue) {
2301
            $this->log($table, $id, SystemLogDatabaseAction::CHECK, 0, SystemLogErrorClassification::WARNING, 'The value of the field "%s" has been changed from "%s" to "%s" as it is required to be unique.', 1, [$field, $originalValue, $newValue], $newPid);
2302
        }
2303
2304
        return $newValue;
2305
    }
2306
2307
    /**
2308
     * Gets the count of records for a unique field
2309
     *
2310
     * @param string $value The string value which should be unique
2311
     * @param string $table Table name
2312
     * @param string $field Field name for which $value must be unique
2313
     * @param int $uid UID to filter out in the lookup (the record itself...)
2314
     * @param int $pid If set, the value will be unique for this PID
2315
     * @return \Doctrine\DBAL\Driver\Statement Return the prepared statement to check uniqueness
2316
     */
2317
    protected function getUniqueCountStatement(
2318
        string $value,
2319
        string $table,
2320
        string $field,
2321
        int $uid,
2322
        int $pid
2323
    ): Statement {
2324
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
2325
        $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
2326
        $queryBuilder
2327
            ->count('uid')
2328
            ->from($table)
2329
            ->where(
2330
                $queryBuilder->expr()->eq($field, $queryBuilder->createPositionalParameter($value, \PDO::PARAM_STR)),
2331
                $queryBuilder->expr()->neq('uid', $queryBuilder->createPositionalParameter($uid, \PDO::PARAM_INT))
2332
            );
2333
        // ignore translations of current record if field is configured with l10n_mode = "exclude"
2334
        if (($GLOBALS['TCA'][$table]['columns'][$field]['l10n_mode'] ?? '') === 'exclude'
2335
            && ($GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] ?? '') !== ''
2336
            && ($GLOBALS['TCA'][$table]['columns'][$field]['languageField'] ?? '') !== '') {
2337
            $queryBuilder
2338
                ->andWhere(
2339
                    $queryBuilder->expr()->orX(
2340
                    // records without l10n_parent must be taken into account (in any language)
2341
                        $queryBuilder->expr()->eq(
2342
                            $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'],
2343
                            $queryBuilder->createPositionalParameter(0, \PDO::PARAM_INT)
2344
                        ),
2345
                        // translations of other records must be taken into account
2346
                        $queryBuilder->expr()->neq(
2347
                            $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'],
2348
                            $queryBuilder->createPositionalParameter($uid, \PDO::PARAM_INT)
2349
                        )
2350
                    )
2351
                );
2352
        }
2353
        if ($pid !== 0) {
2354
            $queryBuilder->andWhere(
2355
                $queryBuilder->expr()->eq('pid', $queryBuilder->createPositionalParameter($pid, \PDO::PARAM_INT))
2356
            );
2357
        } else {
2358
            // pid>=0 for versioning
2359
            $queryBuilder->andWhere(
2360
                $queryBuilder->expr()->gte('pid', $queryBuilder->createPositionalParameter(0, \PDO::PARAM_INT))
2361
            );
2362
        }
2363
        return $queryBuilder->execute();
2364
    }
2365
2366
    /**
2367
     * gets all records that have the same value in a field
2368
     * excluding the given uid
2369
     *
2370
     * @param string $tableName Table name
2371
     * @param int $uid UID to filter out in the lookup (the record itself...)
2372
     * @param string $fieldName Field name for which $value must be unique
2373
     * @param string $value Value string.
2374
     * @param int $pageId If set, the value will be unique for this PID
2375
     * @return array
2376
     * @internal should only be used from within DataHandler
2377
     */
2378
    public function getRecordsWithSameValue($tableName, $uid, $fieldName, $value, $pageId = 0)
2379
    {
2380
        $result = [];
2381
        if (empty($GLOBALS['TCA'][$tableName]['columns'][$fieldName])) {
2382
            return $result;
2383
        }
2384
2385
        $uid = (int)$uid;
2386
        $pageId = (int)$pageId;
2387
2388
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
2389
        $queryBuilder->getRestrictions()
2390
            ->removeAll()
2391
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
2392
            ->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, (int)$this->BE_USER->workspace));
2393
2394
        $queryBuilder->select('*')
2395
            ->from($tableName)
2396
            ->where(
2397
                $queryBuilder->expr()->eq(
2398
                    $fieldName,
2399
                    $queryBuilder->createNamedParameter($value, \PDO::PARAM_STR)
2400
                ),
2401
                $queryBuilder->expr()->neq(
2402
                    'uid',
2403
                    $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
2404
                )
2405
            );
2406
2407
        if ($pageId) {
2408
            $queryBuilder->andWhere(
2409
                $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT))
2410
            );
2411
        }
2412
2413
        $result = $queryBuilder->execute()->fetchAll();
2414
2415
        return $result;
2416
    }
2417
2418
    /**
2419
     * @param string $value The field value to be evaluated
2420
     * @param array $evalArray Array of evaluations to traverse.
2421
     * @param string $is_in The "is_in" value of the field configuration from TCA
2422
     * @return array
2423
     * @internal should only be used from within DataHandler
2424
     */
2425
    public function checkValue_text_Eval($value, $evalArray, $is_in)
2426
    {
2427
        $res = [];
2428
        $set = true;
2429
        foreach ($evalArray as $func) {
2430
            switch ($func) {
2431
                case 'trim':
2432
                    $value = trim($value);
2433
                    break;
2434
                case 'required':
2435
                    if (!$value) {
2436
                        $set = false;
2437
                    }
2438
                    break;
2439
                default:
2440
                    if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
2441
                        if (class_exists($func)) {
2442
                            $evalObj = GeneralUtility::makeInstance($func);
2443
                            if (method_exists($evalObj, 'evaluateFieldValue')) {
2444
                                $value = $evalObj->evaluateFieldValue($value, $is_in, $set);
2445
                            }
2446
                        }
2447
                    }
2448
            }
2449
        }
2450
        if ($set) {
2451
            $res['value'] = $value;
2452
        }
2453
        return $res;
2454
    }
2455
2456
    /**
2457
     * Evaluation of 'input'-type values based on 'eval' list
2458
     *
2459
     * @param string $value Value to evaluate
2460
     * @param array $evalArray Array of evaluations to traverse.
2461
     * @param string $is_in Is-in string for 'is_in' evaluation
2462
     * @param string $table Table name the eval is evaluated on
2463
     * @return array Modified $value in key 'value' or empty array
2464
     * @internal should only be used from within DataHandler
2465
     */
2466
    public function checkValue_input_Eval($value, $evalArray, $is_in, string $table = ''): array
2467
    {
2468
        $res = [];
2469
        $set = true;
2470
        foreach ($evalArray as $func) {
2471
            switch ($func) {
2472
                case 'int':
2473
                case 'year':
2474
                    $value = (int)$value;
2475
                    break;
2476
                case 'time':
2477
                case 'timesec':
2478
                    // If $value is a pure integer we have the number of seconds, we can store that directly
2479
                    if ($value !== '' && !MathUtility::canBeInterpretedAsInteger($value)) {
2480
                        // $value is an ISO 8601 date
2481
                        $value = (new \DateTime($value))->getTimestamp();
2482
                    }
2483
                    break;
2484
                case 'date':
2485
                case 'datetime':
2486
                    // If $value is a pure integer we have the number of seconds, we can store that directly
2487
                    if ($value !== null && $value !== '' && !MathUtility::canBeInterpretedAsInteger($value)) {
2488
                        // The value we receive from JS is an ISO 8601 date, which is always in UTC. (the JS code works like that, on purpose!)
2489
                        // For instance "1999-11-11T11:11:11Z"
2490
                        // Since the user actually specifies the time in the server's local time, we need to mangle this
2491
                        // to reflect the server TZ. So we make this 1999-11-11T11:11:11+0200 (assuming Europe/Vienna here)
2492
                        // In the database we store the date in UTC (1999-11-11T09:11:11Z), hence we take the timestamp of this converted value.
2493
                        // For achieving this we work with timestamps only (which are UTC) and simply adjust it for the
2494
                        // TZ difference.
2495
                        try {
2496
                            // Make the date from JS a timestamp
2497
                            $value = (new \DateTime($value))->getTimestamp();
2498
                        } catch (\Exception $e) {
2499
                            // set the default timezone value to achieve the value of 0 as a result
2500
                            $value = (int)date('Z', 0);
2501
                        }
2502
2503
                        // @todo this hacky part is problematic when it comes to times around DST switch! Add test to prove that this is broken.
2504
                        $value -= date('Z', $value);
2505
                    }
2506
                    break;
2507
                case 'double2':
2508
                    $value = preg_replace('/[^0-9,\\.-]/', '', $value);
2509
                    $negative = $value[0] === '-';
2510
                    $value = strtr($value, [',' => '.', '-' => '']);
2511
                    if (strpos($value, '.') === false) {
2512
                        $value .= '.0';
2513
                    }
2514
                    $valueArray = explode('.', $value);
2515
                    $dec = array_pop($valueArray);
2516
                    $value = implode('', $valueArray) . '.' . $dec;
2517
                    if ($negative) {
2518
                        $value *= -1;
2519
                    }
2520
                    $value = number_format($value, 2, '.', '');
0 ignored issues
show
Bug introduced by
$value of type string is incompatible with the type double expected by parameter $num of number_format(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

2520
                    $value = number_format(/** @scrutinizer ignore-type */ $value, 2, '.', '');
Loading history...
2521
                    break;
2522
                case 'md5':
2523
                    if (strlen($value) !== 32) {
2524
                        $set = false;
2525
                    }
2526
                    break;
2527
                case 'trim':
2528
                    $value = trim($value);
2529
                    break;
2530
                case 'upper':
2531
                    $value = mb_strtoupper($value, 'utf-8');
2532
                    break;
2533
                case 'lower':
2534
                    $value = mb_strtolower($value, 'utf-8');
2535
                    break;
2536
                case 'required':
2537
                    if (!isset($value) || $value === '') {
2538
                        $set = false;
2539
                    }
2540
                    break;
2541
                case 'is_in':
2542
                    $c = mb_strlen($value);
2543
                    if ($c) {
2544
                        $newVal = '';
2545
                        for ($a = 0; $a < $c; $a++) {
2546
                            $char = mb_substr($value, $a, 1);
2547
                            if (mb_strpos($is_in, $char) !== false) {
2548
                                $newVal .= $char;
2549
                            }
2550
                        }
2551
                        $value = $newVal;
2552
                    }
2553
                    break;
2554
                case 'nospace':
2555
                    $value = str_replace(' ', '', $value);
2556
                    break;
2557
                case 'alpha':
2558
                    $value = preg_replace('/[^a-zA-Z]/', '', $value);
2559
                    break;
2560
                case 'num':
2561
                    $value = preg_replace('/[^0-9]/', '', $value);
2562
                    break;
2563
                case 'alphanum':
2564
                    $value = preg_replace('/[^a-zA-Z0-9]/', '', $value);
2565
                    break;
2566
                case 'alphanum_x':
2567
                    $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value);
2568
                    break;
2569
                case 'domainname':
2570
                    if (!preg_match('/^[a-z0-9.\\-]*$/i', $value)) {
2571
                        $value = (string)HttpUtility::idn_to_ascii($value);
2572
                    }
2573
                    break;
2574
                case 'email':
2575
                    if ((string)$value !== '') {
2576
                        $this->checkValue_input_ValidateEmail($value, $set);
2577
                    }
2578
                    break;
2579
                case 'saltedPassword':
2580
                    // An incoming value is either the salted password if the user did not change existing password
2581
                    // when submitting the form, or a plaintext new password that needs to be turned into a salted password now.
2582
                    // The strategy is to see if a salt instance can be created from the incoming value. If so,
2583
                    // no new password was submitted and we keep the value. If no salting instance can be created,
2584
                    // incoming value must be a new plain text value that needs to be hashed.
2585
                    $hashFactory = GeneralUtility::makeInstance(PasswordHashFactory::class);
2586
                    $mode = $table === 'fe_users' ? 'FE' : 'BE';
2587
                    try {
2588
                        $hashFactory->get($value, $mode);
2589
                    } catch (InvalidPasswordHashException $e) {
2590
                        // We got no salted password instance, incoming value must be a new plaintext password
2591
                        // Get an instance of the current configured salted password strategy and hash the value
2592
                        $newHashInstance = $hashFactory->getDefaultHashInstance($mode);
2593
                        $value = $newHashInstance->getHashedPassword($value);
2594
                    }
2595
                    break;
2596
                default:
2597
                    if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
2598
                        if (class_exists($func)) {
2599
                            $evalObj = GeneralUtility::makeInstance($func);
2600
                            if (method_exists($evalObj, 'evaluateFieldValue')) {
2601
                                $value = $evalObj->evaluateFieldValue($value, $is_in, $set);
2602
                            }
2603
                        }
2604
                    }
2605
            }
2606
        }
2607
        if ($set) {
2608
            $res['value'] = $value;
2609
        }
2610
        return $res;
2611
    }
2612
2613
    /**
2614
     * If $value is not a valid e-mail address,
2615
     * $set will be set to false and a flash error
2616
     * message will be added
2617
     *
2618
     * @param string $value Value to evaluate
2619
     * @param bool $set TRUE if an update should be done
2620
     * @throws \InvalidArgumentException
2621
     * @throws \TYPO3\CMS\Core\Exception
2622
     */
2623
    protected function checkValue_input_ValidateEmail($value, &$set)
2624
    {
2625
        if (GeneralUtility::validEmail($value)) {
2626
            return;
2627
        }
2628
2629
        $set = false;
2630
        /** @var FlashMessage $message */
2631
        $message = GeneralUtility::makeInstance(
2632
            FlashMessage::class,
2633
            sprintf($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.invalidEmail'), $value),
2634
            '', // header is optional
2635
            FlashMessage::ERROR,
2636
            true // whether message should be stored in session
2637
        );
2638
        /** @var FlashMessageService $flashMessageService */
2639
        $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
2640
        $flashMessageService->getMessageQueueByIdentifier()->enqueue($message);
2641
    }
2642
2643
    /**
2644
     * Returns data for group/db and select fields
2645
     *
2646
     * @param array $valueArray Current value array
2647
     * @param array $tcaFieldConf TCA field config
2648
     * @param int $id Record id, used for look-up of MM relations (local_uid)
2649
     * @param string $status Status string ('update' or 'new')
2650
     * @param string $type The type, either 'select', 'group' or 'inline'
2651
     * @param string $currentTable Table name, needs to be passed to \TYPO3\CMS\Core\Database\RelationHandler
2652
     * @param string $currentField field name, needs to be set for writing to sys_history
2653
     * @return array Modified value array
2654
     * @internal should only be used from within DataHandler
2655
     */
2656
    public function checkValue_group_select_processDBdata($valueArray, $tcaFieldConf, $id, $status, $type, $currentTable, $currentField)
2657
    {
2658
        if ($type === 'group') {
2659
            $tables = $tcaFieldConf['allowed'];
2660
        } elseif (!empty($tcaFieldConf['special']) && $tcaFieldConf['special'] === 'languages') {
2661
            $tables = 'sys_language';
2662
        } else {
2663
            $tables = $tcaFieldConf['foreign_table'];
2664
        }
2665
        $prep = $type === 'group' ? $tcaFieldConf['prepend_tname'] : '';
2666
        $newRelations = implode(',', $valueArray);
2667
        /** @var RelationHandler $dbAnalysis */
2668
        $dbAnalysis = $this->createRelationHandlerInstance();
2669
        $dbAnalysis->registerNonTableValues = !empty($tcaFieldConf['allowNonIdValues']);
2670
        $dbAnalysis->start($newRelations, $tables, '', 0, $currentTable, $tcaFieldConf);
2671
        if ($tcaFieldConf['MM']) {
2672
            // convert submitted items to use version ids instead of live ids
2673
            // (only required for MM relations in a workspace context)
2674
            $dbAnalysis->convertItemArray();
2675
            if ($status === 'update') {
2676
                /** @var RelationHandler $oldRelations_dbAnalysis */
2677
                $oldRelations_dbAnalysis = $this->createRelationHandlerInstance();
2678
                $oldRelations_dbAnalysis->registerNonTableValues = !empty($tcaFieldConf['allowNonIdValues']);
2679
                // Db analysis with $id will initialize with the existing relations
2680
                $oldRelations_dbAnalysis->start('', $tables, $tcaFieldConf['MM'], $id, $currentTable, $tcaFieldConf);
2681
                $oldRelations = implode(',', $oldRelations_dbAnalysis->getValueArray());
2682
                $dbAnalysis->writeMM($tcaFieldConf['MM'], $id, $prep);
0 ignored issues
show
Bug introduced by
It seems like $prep can also be of type string; however, parameter $prependTableName of TYPO3\CMS\Core\Database\RelationHandler::writeMM() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

2682
                $dbAnalysis->writeMM($tcaFieldConf['MM'], $id, /** @scrutinizer ignore-type */ $prep);
Loading history...
2683
                if ($oldRelations != $newRelations) {
2684
                    $this->mmHistoryRecords[$currentTable . ':' . $id]['oldRecord'][$currentField] = $oldRelations;
2685
                    $this->mmHistoryRecords[$currentTable . ':' . $id]['newRecord'][$currentField] = $newRelations;
2686
                } else {
2687
                    $this->mmHistoryRecords[$currentTable . ':' . $id]['oldRecord'][$currentField] = '';
2688
                    $this->mmHistoryRecords[$currentTable . ':' . $id]['newRecord'][$currentField] = '';
2689
                }
2690
            } else {
2691
                $this->dbAnalysisStore[] = [$dbAnalysis, $tcaFieldConf['MM'], $id, $prep, $currentTable];
2692
            }
2693
            $valueArray = $dbAnalysis->countItems();
2694
        } else {
2695
            $valueArray = $dbAnalysis->getValueArray($prep);
0 ignored issues
show
Bug introduced by
It seems like $prep can also be of type string; however, parameter $prependTableName of TYPO3\CMS\Core\Database\...andler::getValueArray() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

2695
            $valueArray = $dbAnalysis->getValueArray(/** @scrutinizer ignore-type */ $prep);
Loading history...
2696
        }
2697
        // Here we should see if 1) the records exist anymore, 2) which are new and check if the BE_USER has read-access to the new ones.
2698
        return $valueArray;
2699
    }
2700
2701
    /**
2702
     * Explodes the $value, which is a list of files/uids (group select)
2703
     *
2704
     * @param string $value Input string, comma separated values. For each part it will also be detected if a '|' is found and the first part will then be used if that is the case. Further the value will be rawurldecoded.
2705
     * @return array The value array.
2706
     * @internal should only be used from within DataHandler
2707
     */
2708
    public function checkValue_group_select_explodeSelectGroupValue($value)
2709
    {
2710
        $valueArray = GeneralUtility::trimExplode(',', $value, true);
2711
        foreach ($valueArray as &$newVal) {
2712
            $temp = explode('|', $newVal, 2);
2713
            $newVal = str_replace(['|', ','], '', rawurldecode($temp[0]));
2714
        }
2715
        unset($newVal);
2716
        return $valueArray;
2717
    }
2718
2719
    /**
2720
     * Starts the processing the input data for flexforms. This will traverse all sheets / languages and for each it will traverse the sub-structure.
2721
     * See checkValue_flex_procInData_travDS() for more details.
2722
     * WARNING: Currently, it traverses based on the actual _data_ array and NOT the _structure_. This means that values for non-valid fields, lKey/vKey/sKeys will be accepted! For traversal of data with a call back function you should rather use \TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools
2723
     *
2724
     * @param array $dataPart The 'data' part of the INPUT flexform data
2725
     * @param array $dataPart_current The 'data' part of the CURRENT flexform data
2726
     * @param array $uploadedFiles The uploaded files for the 'data' part of the INPUT flexform data
2727
     * @param array $dataStructure Data structure for the form (might be sheets or not). Only values in the data array which has a configuration in the data structure will be processed.
2728
     * @param array $pParams A set of parameters to pass through for the calling of the evaluation functions
2729
     * @param string $callBackFunc Optional call back function, see checkValue_flex_procInData_travDS()  DEPRECATED, use \TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools instead for traversal!
2730
     * @param array $workspaceOptions
2731
     * @return array The modified 'data' part.
2732
     * @see checkValue_flex_procInData_travDS()
2733
     * @internal should only be used from within DataHandler
2734
     */
2735
    public function checkValue_flex_procInData($dataPart, $dataPart_current, $uploadedFiles, $dataStructure, $pParams, $callBackFunc = '', array $workspaceOptions = [])
2736
    {
2737
        if (is_array($dataPart)) {
0 ignored issues
show
introduced by
The condition is_array($dataPart) is always true.
Loading history...
2738
            foreach ($dataPart as $sKey => $sheetDef) {
2739
                if (isset($dataStructure['sheets'][$sKey]) && is_array($dataStructure['sheets'][$sKey]) && is_array($sheetDef)) {
2740
                    foreach ($sheetDef as $lKey => $lData) {
2741
                        $this->checkValue_flex_procInData_travDS(
2742
                            $dataPart[$sKey][$lKey],
2743
                            $dataPart_current[$sKey][$lKey],
2744
                            $uploadedFiles[$sKey][$lKey],
2745
                            $dataStructure['sheets'][$sKey]['ROOT']['el'],
2746
                            $pParams,
2747
                            $callBackFunc,
2748
                            $sKey . '/' . $lKey . '/',
2749
                            $workspaceOptions
2750
                        );
2751
                    }
2752
                }
2753
            }
2754
        }
2755
        return $dataPart;
2756
    }
2757
2758
    /**
2759
     * Processing of the sheet/language data array
2760
     * When it finds a field with a value the processing is done by ->checkValue_SW() by default but if a call back function name is given that method in this class will be called for the processing instead.
2761
     *
2762
     * @param array $dataValues New values (those being processed): Multidimensional Data array for sheet/language, passed by reference!
2763
     * @param array $dataValues_current Current values: Multidimensional Data array. May be empty array() if not needed (for callBackFunctions)
2764
     * @param array $uploadedFiles Uploaded files array for sheet/language. May be empty array() if not needed (for callBackFunctions)
2765
     * @param array $DSelements Data structure which fits the data array
2766
     * @param array $pParams A set of parameters to pass through for the calling of the evaluation functions / call back function
2767
     * @param string $callBackFunc Call back function, default is checkValue_SW(). If $this->callBackObj is set to an object, the callback function in that object is called instead.
2768
     * @param string $structurePath
2769
     * @param array $workspaceOptions
2770
     * @see checkValue_flex_procInData()
2771
     * @internal should only be used from within DataHandler
2772
     */
2773
    public function checkValue_flex_procInData_travDS(&$dataValues, $dataValues_current, $uploadedFiles, $DSelements, $pParams, $callBackFunc, $structurePath, array $workspaceOptions = [])
2774
    {
2775
        if (!is_array($DSelements)) {
0 ignored issues
show
introduced by
The condition is_array($DSelements) is always true.
Loading history...
2776
            return;
2777
        }
2778
2779
        // For each DS element:
2780
        foreach ($DSelements as $key => $dsConf) {
2781
            // Array/Section:
2782
            if ($DSelements[$key]['type'] === 'array') {
2783
                if (!is_array($dataValues[$key]['el'])) {
2784
                    continue;
2785
                }
2786
2787
                if ($DSelements[$key]['section']) {
2788
                    foreach ($dataValues[$key]['el'] as $ik => $el) {
2789
                        if (!is_array($el)) {
2790
                            continue;
2791
                        }
2792
2793
                        if (!is_array($dataValues_current[$key]['el'])) {
2794
                            $dataValues_current[$key]['el'] = [];
2795
                        }
2796
                        $theKey = key($el);
2797
                        if (!is_array($dataValues[$key]['el'][$ik][$theKey]['el'])) {
2798
                            continue;
2799
                        }
2800
2801
                        $this->checkValue_flex_procInData_travDS($dataValues[$key]['el'][$ik][$theKey]['el'], is_array($dataValues_current[$key]['el'][$ik]) ? $dataValues_current[$key]['el'][$ik][$theKey]['el'] : [], $uploadedFiles[$key]['el'][$ik][$theKey]['el'], $DSelements[$key]['el'][$theKey]['el'], $pParams, $callBackFunc, $structurePath . $key . '/el/' . $ik . '/' . $theKey . '/el/', $workspaceOptions);
2802
                    }
2803
                } else {
2804
                    if (!isset($dataValues[$key]['el'])) {
2805
                        $dataValues[$key]['el'] = [];
2806
                    }
2807
                    $this->checkValue_flex_procInData_travDS($dataValues[$key]['el'], $dataValues_current[$key]['el'], $uploadedFiles[$key]['el'], $DSelements[$key]['el'], $pParams, $callBackFunc, $structurePath . $key . '/el/', $workspaceOptions);
2808
                }
2809
            } else {
2810
                // When having no specific sheets, it's "TCEforms.config", when having a sheet, it's just "config"
2811
                $fieldConfiguration = $dsConf['TCEforms']['config'] ?? $dsConf['config'] ?? null;
2812
                // init with value from config for passthrough fields
2813
                if (!empty($fieldConfiguration['type']) && $fieldConfiguration['type'] === 'passthrough') {
2814
                    if (!empty($dataValues_current[$key]['vDEF'])) {
2815
                        // If there is existing value, keep it
2816
                        $dataValues[$key]['vDEF'] = $dataValues_current[$key]['vDEF'];
2817
                    } elseif (
2818
                        !empty($fieldConfiguration['default'])
2819
                        && isset($pParams[1])
2820
                        && !MathUtility::canBeInterpretedAsInteger($pParams[1])
2821
                    ) {
2822
                        // If is new record and a default is specified for field, use it.
2823
                        $dataValues[$key]['vDEF'] = $fieldConfiguration['default'];
2824
                    }
2825
                }
2826
                if (!is_array($fieldConfiguration) || !is_array($dataValues[$key])) {
2827
                    continue;
2828
                }
2829
2830
                foreach ($dataValues[$key] as $vKey => $data) {
2831
                    if ($callBackFunc) {
2832
                        if (is_object($this->callBackObj)) {
2833
                            $res = $this->callBackObj->{$callBackFunc}($pParams, $fieldConfiguration, $dataValues[$key][$vKey], $dataValues_current[$key][$vKey], $uploadedFiles[$key][$vKey], $structurePath . $key . '/' . $vKey . '/', $workspaceOptions);
2834
                        } else {
2835
                            $res = $this->{$callBackFunc}($pParams, $fieldConfiguration, $dataValues[$key][$vKey], $dataValues_current[$key][$vKey], $uploadedFiles[$key][$vKey], $structurePath . $key . '/' . $vKey . '/', $workspaceOptions);
2836
                        }
2837
                    } else {
2838
                        // Default
2839
                        [$CVtable, $CVid, $CVcurValue, $CVstatus, $CVrealPid, $CVrecFID, $CVtscPID] = $pParams;
2840
2841
                        $additionalData = [
2842
                            'flexFormId' => $CVrecFID,
2843
                            'flexFormPath' => trim(rtrim($structurePath, '/') . '/' . $key . '/' . $vKey, '/'),
2844
                        ];
2845
2846
                        $res = $this->checkValue_SW([], $dataValues[$key][$vKey], $fieldConfiguration, $CVtable, $CVid, $dataValues_current[$key][$vKey], $CVstatus, $CVrealPid, $CVrecFID, '', $uploadedFiles[$key][$vKey], $CVtscPID, $additionalData);
2847
                    }
2848
                    // Adding the value:
2849
                    if (isset($res['value'])) {
2850
                        $dataValues[$key][$vKey] = $res['value'];
2851
                    }
2852
                    // Finally, check if new and old values are different (or no .vDEFbase value is found) and if so, we record the vDEF value for diff'ing.
2853
                    // We do this after $dataValues has been updated since I expect that $dataValues_current holds evaluated values from database (so this must be the right value to compare with).
2854
                    if (mb_substr($vKey, -9) !== '.vDEFbase') {
2855
                        if ($GLOBALS['TYPO3_CONF_VARS']['BE']['flexFormXMLincludeDiffBase'] && $vKey !== 'vDEF' && ((string)$dataValues[$key][$vKey] !== (string)$dataValues_current[$key][$vKey] || !isset($dataValues_current[$key][$vKey . '.vDEFbase']))) {
2856
                            // Now, check if a vDEF value is submitted in the input data, if so we expect this has been processed prior to this operation (normally the case since those fields are higher in the form) and we can use that:
2857
                            if (isset($dataValues[$key]['vDEF'])) {
2858
                                $diffValue = $dataValues[$key]['vDEF'];
2859
                            } else {
2860
                                // If not found (for translators with no access to the default language) we use the one from the current-value data set:
2861
                                $diffValue = $dataValues_current[$key]['vDEF'];
2862
                            }
2863
                            // Setting the reference value for vDEF for this translation. This will be used for translation tools to make a diff between the vDEF and vDEFbase to see if an update would be fitting.
2864
                            $dataValues[$key][$vKey . '.vDEFbase'] = $diffValue;
2865
                        }
2866
                    }
2867
                }
2868
            }
2869
        }
2870
    }
2871
2872
    /**
2873
     * Returns data for inline fields.
2874
     *
2875
     * @param array $valueArray Current value array
2876
     * @param array $tcaFieldConf TCA field config
2877
     * @param int $id Record id
2878
     * @param string $status Status string ('update' or 'new')
2879
     * @param string $table Table name, needs to be passed to \TYPO3\CMS\Core\Database\RelationHandler
2880
     * @param string $field The current field the values are modified for
2881
     * @param array $additionalData Additional data to be forwarded to sub-processors
2882
     * @return string Modified values
2883
     */
2884
    protected function checkValue_inline_processDBdata($valueArray, $tcaFieldConf, $id, $status, $table, $field, array $additionalData = null)
2885
    {
2886
        $foreignTable = $tcaFieldConf['foreign_table'];
2887
        $valueArray = $this->applyFiltersToValues($tcaFieldConf, $valueArray);
2888
        // Fetch the related child records using \TYPO3\CMS\Core\Database\RelationHandler
2889
        /** @var RelationHandler $dbAnalysis */
2890
        $dbAnalysis = $this->createRelationHandlerInstance();
2891
        $dbAnalysis->start(implode(',', $valueArray), $foreignTable, '', 0, $table, $tcaFieldConf);
2892
        // IRRE with a pointer field (database normalization):
2893
        if ($tcaFieldConf['foreign_field']) {
2894
            // if the record was imported, sorting was also imported, so skip this
2895
            $skipSorting = (bool)$this->callFromImpExp;
2896
            // update record in intermediate table (sorting & pointer uid to parent record)
2897
            $dbAnalysis->writeForeignField($tcaFieldConf, $id, 0, $skipSorting);
2898
            $newValue = $dbAnalysis->countItems(false);
2899
        } elseif ($this->getInlineFieldType($tcaFieldConf) === 'mm') {
2900
            // In order to fully support all the MM stuff, directly call checkValue_group_select_processDBdata instead of repeating the needed code here
2901
            $valueArray = $this->checkValue_group_select_processDBdata($valueArray, $tcaFieldConf, $id, $status, 'select', $table, $field);
2902
            $newValue = $valueArray[0];
2903
        } else {
2904
            $valueArray = $dbAnalysis->getValueArray();
2905
            // Checking that the number of items is correct:
2906
            $valueArray = $this->checkValue_checkMax($tcaFieldConf, $valueArray);
2907
            $newValue = $this->castReferenceValue(implode(',', $valueArray), $tcaFieldConf);
2908
        }
2909
        return $newValue;
2910
    }
2911
2912
    /*********************************************
2913
     *
2914
     * PROCESSING COMMANDS
2915
     *
2916
     ********************************************/
2917
    /**
2918
     * Processing the cmd-array
2919
     * See "TYPO3 Core API" for a description of the options.
2920
     *
2921
     * @return void|bool
2922
     */
2923
    public function process_cmdmap()
2924
    {
2925
        // Editing frozen:
2926
        if ($this->BE_USER->workspace !== 0 && $this->BE_USER->workspaceRec['freeze']) {
2927
            $this->newlog('All editing in this workspace has been frozen!', SystemLogErrorClassification::USER_ERROR);
2928
            return false;
2929
        }
2930
        // Hook initialization:
2931
        $hookObjectsArr = [];
2932
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'] ?? [] as $className) {
2933
            $hookObj = GeneralUtility::makeInstance($className);
2934
            if (method_exists($hookObj, 'processCmdmap_beforeStart')) {
2935
                $hookObj->processCmdmap_beforeStart($this);
2936
            }
2937
            $hookObjectsArr[] = $hookObj;
2938
        }
2939
        $pasteDatamap = [];
2940
        // Traverse command map:
2941
        foreach ($this->cmdmap as $table => $_) {
2942
            // Check if the table may be modified!
2943
            $modifyAccessList = $this->checkModifyAccessList($table);
2944
            if (!$modifyAccessList) {
2945
                $this->log($table, 0, SystemLogDatabaseAction::UPDATE, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to modify table \'%s\' without permission', 1, [$table]);
2946
            }
2947
            // Check basic permissions and circumstances:
2948
            if (!isset($GLOBALS['TCA'][$table]) || $this->tableReadOnly($table) || !is_array($this->cmdmap[$table]) || !$modifyAccessList) {
2949
                continue;
2950
            }
2951
2952
            // Traverse the command map:
2953
            foreach ($this->cmdmap[$table] as $id => $incomingCmdArray) {
2954
                if (!is_array($incomingCmdArray)) {
2955
                    continue;
2956
                }
2957
2958
                if ($table === 'pages') {
2959
                    // for commands on pages do a pagetree-refresh
2960
                    $this->pagetreeNeedsRefresh = true;
2961
                }
2962
2963
                foreach ($incomingCmdArray as $command => $value) {
2964
                    $pasteUpdate = false;
2965
                    if (is_array($value) && isset($value['action']) && $value['action'] === 'paste') {
2966
                        // Extended paste command: $command is set to "move" or "copy"
2967
                        // $value['update'] holds field/value pairs which should be updated after copy/move operation
2968
                        // $value['target'] holds original $value (target of move/copy)
2969
                        $pasteUpdate = $value['update'];
2970
                        $value = $value['target'];
2971
                    }
2972
                    foreach ($hookObjectsArr as $hookObj) {
2973
                        if (method_exists($hookObj, 'processCmdmap_preProcess')) {
2974
                            $hookObj->processCmdmap_preProcess($command, $table, $id, $value, $this, $pasteUpdate);
2975
                        }
2976
                    }
2977
                    // Init copyMapping array:
2978
                    // Must clear this array before call from here to those functions:
2979
                    // Contains mapping information between new and old id numbers.
2980
                    $this->copyMappingArray = [];
2981
                    // process the command
2982
                    $commandIsProcessed = false;
2983
                    foreach ($hookObjectsArr as $hookObj) {
2984
                        if (method_exists($hookObj, 'processCmdmap')) {
2985
                            $hookObj->processCmdmap($command, $table, $id, $value, $commandIsProcessed, $this, $pasteUpdate);
2986
                        }
2987
                    }
2988
                    // Only execute default commands if a hook hasn't been processed the command already
2989
                    if (!$commandIsProcessed) {
2990
                        $procId = $id;
2991
                        $backupUseTransOrigPointerField = $this->useTransOrigPointerField;
2992
                        // Branch, based on command
2993
                        switch ($command) {
2994
                            case 'move':
2995
                                $this->moveRecord($table, $id, $value);
2996
                                break;
2997
                            case 'copy':
2998
                                $target = $value['target'] ?? $value;
2999
                                $ignoreLocalization = (bool)($value['ignoreLocalization'] ?? false);
3000
                                if ($table === 'pages') {
3001
                                    $this->copyPages($id, $target);
3002
                                } else {
3003
                                    $this->copyRecord($table, $id, $target, true, [], '', 0, $ignoreLocalization);
3004
                                }
3005
                                $procId = $this->copyMappingArray[$table][$id];
3006
                                break;
3007
                            case 'localize':
3008
                                $this->useTransOrigPointerField = true;
3009
                                $this->localize($table, $id, $value);
3010
                                break;
3011
                            case 'copyToLanguage':
3012
                                $this->useTransOrigPointerField = false;
3013
                                $this->localize($table, $id, $value);
3014
                                break;
3015
                            case 'inlineLocalizeSynchronize':
3016
                                $this->inlineLocalizeSynchronize($table, $id, $value);
3017
                                break;
3018
                            case 'delete':
3019
                                $this->deleteAction($table, $id);
3020
                                break;
3021
                            case 'undelete':
3022
                                $this->undeleteRecord((string)$table, (int)$id);
3023
                                break;
3024
                        }
3025
                        $this->useTransOrigPointerField = $backupUseTransOrigPointerField;
3026
                        if (is_array($pasteUpdate)) {
3027
                            $pasteDatamap[$table][$procId] = $pasteUpdate;
3028
                        }
3029
                    }
3030
                    foreach ($hookObjectsArr as $hookObj) {
3031
                        if (method_exists($hookObj, 'processCmdmap_postProcess')) {
3032
                            $hookObj->processCmdmap_postProcess($command, $table, $id, $value, $this, $pasteUpdate, $pasteDatamap);
3033
                        }
3034
                    }
3035
                    // Merging the copy-array info together for remapping purposes.
3036
                    ArrayUtility::mergeRecursiveWithOverrule($this->copyMappingArray_merged, $this->copyMappingArray);
3037
                }
3038
            }
3039
        }
3040
        /** @var DataHandler $copyTCE */
3041
        $copyTCE = $this->getLocalTCE();
3042
        $copyTCE->start($pasteDatamap, [], $this->BE_USER);
3043
        $copyTCE->process_datamap();
3044
        $this->errorLog = array_merge($this->errorLog, $copyTCE->errorLog);
3045
        unset($copyTCE);
3046
3047
        // Finally, before exit, check if there are ID references to remap.
3048
        // This might be the case if versioning or copying has taken place!
3049
        $this->remapListedDBRecords();
3050
        $this->processRemapStack();
3051
        foreach ($hookObjectsArr as $hookObj) {
3052
            if (method_exists($hookObj, 'processCmdmap_afterFinish')) {
3053
                $hookObj->processCmdmap_afterFinish($this);
3054
            }
3055
        }
3056
        if ($this->isOuterMostInstance()) {
3057
            $this->referenceIndexUpdater->update();
3058
            $this->processClearCacheQueue();
3059
            $this->resetNestedElementCalls();
3060
        }
3061
    }
3062
3063
    /*********************************************
3064
     *
3065
     * Cmd: Copying
3066
     *
3067
     ********************************************/
3068
    /**
3069
     * Copying a single record
3070
     *
3071
     * @param string $table Element table
3072
     * @param int $uid Element UID
3073
     * @param int $destPid >=0 then it points to a page-id on which to insert the record (as the first element). <0 then it points to a uid from its own table after which to insert it (works if
3074
     * @param bool $first Is a flag set, if the record copied is NOT a 'slave' to another record copied. That is, if this record was asked to be copied in the cmd-array
3075
     * @param array $overrideValues Associative array with field/value pairs to override directly. Notice; Fields must exist in the table record and NOT be among excluded fields!
3076
     * @param string $excludeFields Commalist of fields to exclude from the copy process (might get default values)
3077
     * @param int $language Language ID (from sys_language table)
3078
     * @param bool $ignoreLocalization If TRUE, any localization routine is skipped
3079
     * @return int|null ID of new record, if any
3080
     * @internal should only be used from within DataHandler
3081
     */
3082
    public function copyRecord($table, $uid, $destPid, $first = false, $overrideValues = [], $excludeFields = '', $language = 0, $ignoreLocalization = false)
3083
    {
3084
        $uid = ($origUid = (int)$uid);
3085
        // Only copy if the table is defined in $GLOBALS['TCA'], a uid is given and the record wasn't copied before:
3086
        if (empty($GLOBALS['TCA'][$table]) || $uid === 0) {
3087
            return null;
3088
        }
3089
        if ($this->isRecordCopied($table, $uid)) {
3090
            return null;
3091
        }
3092
3093
        // Fetch record with permission check
3094
        $row = $this->recordInfoWithPermissionCheck($table, $uid, Permission::PAGE_SHOW);
3095
3096
        // This checks if the record can be selected which is all that a copy action requires.
3097
        if ($row === false) {
3098
            $this->log($table, $uid, SystemLogDatabaseAction::INSERT, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to copy record "%s:%s" which does not exist or you do not have permission to read', -1, [$table, $uid]);
3099
            return null;
3100
        }
3101
3102
        // NOT using \TYPO3\CMS\Backend\Utility\BackendUtility::getTSCpid() because we need the real pid - not the ID of a page, if the input is a page...
3103
        $tscPID = BackendUtility::getTSconfig_pidValue($table, $uid, $destPid);
3104
3105
        // Check if table is allowed on destination page
3106
        if (!$this->isTableAllowedForThisPage($tscPID, $table)) {
3107
            $this->log($table, $uid, SystemLogDatabaseAction::INSERT, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to insert record "%s:%s" on a page (%s) that can\'t store record type.', -1, [$table, $uid, $tscPID]);
3108
            return null;
3109
        }
3110
3111
        $fullLanguageCheckNeeded = $table !== 'pages';
3112
        // Used to check language and general editing rights
3113
        if (!$ignoreLocalization && ($language <= 0 || !$this->BE_USER->checkLanguageAccess($language)) && !$this->BE_USER->recordEditAccessInternals($table, $uid, false, false, $fullLanguageCheckNeeded)) {
3114
            $this->log($table, $uid, SystemLogDatabaseAction::INSERT, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to copy record "%s:%s" without having permissions to do so. [' . $this->BE_USER->errorMsg . '].', -1, [$table, $uid]);
3115
            return null;
3116
        }
3117
3118
        $data = [];
3119
        $nonFields = array_unique(GeneralUtility::trimExplode(',', 'uid,perms_userid,perms_groupid,perms_user,perms_group,perms_everybody,t3ver_oid,t3ver_wsid,t3ver_state,t3ver_stage,' . $excludeFields, true));
3120
        BackendUtility::workspaceOL($table, $row, $this->BE_USER->workspace);
0 ignored issues
show
Bug introduced by
It seems like $row can also be of type true; however, parameter $row of TYPO3\CMS\Backend\Utilit...dUtility::workspaceOL() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3120
        BackendUtility::workspaceOL($table, /** @scrutinizer ignore-type */ $row, $this->BE_USER->workspace);
Loading history...
3121
        $row = BackendUtility::purgeComputedPropertiesFromRecord($row);
3122
3123
        // Initializing:
3124
        $theNewID = StringUtility::getUniqueId('NEW');
3125
        $enableField = isset($GLOBALS['TCA'][$table]['ctrl']['enablecolumns']) ? $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'] : '';
3126
        $headerField = $GLOBALS['TCA'][$table]['ctrl']['label'];
3127
        // Getting "copy-after" fields if applicable:
3128
        $copyAfterFields = $destPid < 0 ? $this->fixCopyAfterDuplFields($table, $uid, abs($destPid), 0) : [];
0 ignored issues
show
Bug introduced by
It seems like abs($destPid) can also be of type double; however, parameter $prevUid of TYPO3\CMS\Core\DataHandl...ixCopyAfterDuplFields() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3128
        $copyAfterFields = $destPid < 0 ? $this->fixCopyAfterDuplFields($table, $uid, /** @scrutinizer ignore-type */ abs($destPid), 0) : [];
Loading history...
3129
        // Page TSconfig related:
3130
        $TSConfig = BackendUtility::getPagesTSconfig($tscPID)['TCEMAIN.'] ?? [];
3131
        $tE = $this->getTableEntries($table, $TSConfig);
3132
        // Traverse ALL fields of the selected record:
3133
        foreach ($row as $field => $value) {
3134
            if (!in_array($field, $nonFields, true)) {
3135
                // Get TCA configuration for the field:
3136
                $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
3137
                // Preparation/Processing of the value:
3138
                // "pid" is hardcoded of course:
3139
                // isset() won't work here, since values can be NULL in each of the arrays
3140
                // except setDefaultOnCopyArray, since we exploded that from a string
3141
                if ($field === 'pid') {
3142
                    $value = $destPid;
3143
                } elseif (array_key_exists($field, $overrideValues)) {
3144
                    // Override value...
3145
                    $value = $overrideValues[$field];
3146
                } elseif (array_key_exists($field, $copyAfterFields)) {
3147
                    // Copy-after value if available:
3148
                    $value = $copyAfterFields[$field];
3149
                } else {
3150
                    // Hide at copy may override:
3151
                    if ($first && $field == $enableField && $GLOBALS['TCA'][$table]['ctrl']['hideAtCopy'] && !$this->neverHideAtCopy && !$tE['disableHideAtCopy']) {
3152
                        $value = 1;
3153
                    }
3154
                    // Prepend label on copy:
3155
                    if ($first && $field == $headerField && $GLOBALS['TCA'][$table]['ctrl']['prependAtCopy'] && !$tE['disablePrependAtCopy']) {
3156
                        $value = $this->getCopyHeader($table, $this->resolvePid($table, $destPid), $field, $this->clearPrefixFromValue($table, $value), 0);
3157
                    }
3158
                    // Processing based on the TCA config field type (files, references, flexforms...)
3159
                    $value = $this->copyRecord_procBasedOnFieldType($table, $uid, $field, $value, $row, $conf, $tscPID, $language);
3160
                }
3161
                // Add value to array.
3162
                $data[$table][$theNewID][$field] = $value;
3163
            }
3164
        }
3165
        // Overriding values:
3166
        if ($GLOBALS['TCA'][$table]['ctrl']['editlock']) {
3167
            $data[$table][$theNewID][$GLOBALS['TCA'][$table]['ctrl']['editlock']] = 0;
3168
        }
3169
        // Setting original UID:
3170
        if ($GLOBALS['TCA'][$table]['ctrl']['origUid']) {
3171
            $data[$table][$theNewID][$GLOBALS['TCA'][$table]['ctrl']['origUid']] = $uid;
3172
        }
3173
        // Do the copy by simply submitting the array through DataHandler:
3174
        /** @var DataHandler $copyTCE */
3175
        $copyTCE = $this->getLocalTCE();
3176
        $copyTCE->start($data, [], $this->BE_USER);
3177
        $copyTCE->process_datamap();
3178
        // Getting the new UID:
3179
        $theNewSQLID = $copyTCE->substNEWwithIDs[$theNewID];
3180
        if ($theNewSQLID) {
3181
            $this->copyMappingArray[$table][$origUid] = $theNewSQLID;
3182
            // Keep automatically versionized record information:
3183
            if (isset($copyTCE->autoVersionIdMap[$table][$theNewSQLID])) {
3184
                $this->autoVersionIdMap[$table][$theNewSQLID] = $copyTCE->autoVersionIdMap[$table][$theNewSQLID];
3185
            }
3186
        }
3187
        $this->errorLog = array_merge($this->errorLog, $copyTCE->errorLog);
3188
        unset($copyTCE);
3189
        if (!$ignoreLocalization && $language == 0) {
3190
            //repointing the new translation records to the parent record we just created
3191
            $overrideValues[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] = $theNewSQLID;
3192
            if (isset($GLOBALS['TCA'][$table]['ctrl']['translationSource'])) {
3193
                $overrideValues[$GLOBALS['TCA'][$table]['ctrl']['translationSource']] = 0;
3194
            }
3195
            $this->copyL10nOverlayRecords($table, $uid, $destPid, $first, $overrideValues, $excludeFields);
3196
        }
3197
3198
        return $theNewSQLID;
3199
    }
3200
3201
    /**
3202
     * Copying pages
3203
     * Main function for copying pages.
3204
     *
3205
     * @param int $uid Page UID to copy
3206
     * @param int $destPid Destination PID: >=0 then it points to a page-id on which to insert the record (as the first element). <0 then it points to a uid from its own table after which to insert it (works if
3207
     * @internal should only be used from within DataHandler
3208
     */
3209
    public function copyPages($uid, $destPid)
3210
    {
3211
        // Initialize:
3212
        $uid = (int)$uid;
3213
        $destPid = (int)$destPid;
3214
3215
        $copyTablesAlongWithPage = $this->getAllowedTablesToCopyWhenCopyingAPage();
3216
        // Begin to copy pages if we're allowed to:
3217
        if ($this->admin || in_array('pages', $copyTablesAlongWithPage, true)) {
3218
            // Copy this page we're on. And set first-flag (this will trigger that the record is hidden if that is configured)
3219
            // This method also copies the localizations of a page
3220
            $theNewRootID = $this->copySpecificPage($uid, $destPid, $copyTablesAlongWithPage, true);
3221
            // If we're going to copy recursively
3222
            if ($theNewRootID && $this->copyTree) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $theNewRootID of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
3223
                // Get ALL subpages to copy (read-permissions are respected!):
3224
                $CPtable = $this->int_pageTreeInfo([], $uid, (int)$this->copyTree, $theNewRootID);
3225
                // Now copying the subpages:
3226
                foreach ($CPtable as $thePageUid => $thePagePid) {
3227
                    $newPid = $this->copyMappingArray['pages'][$thePagePid];
3228
                    if (isset($newPid)) {
3229
                        $this->copySpecificPage($thePageUid, $newPid, $copyTablesAlongWithPage);
3230
                    } else {
3231
                        $this->log('pages', $uid, SystemLogDatabaseAction::CHECK, 0, SystemLogErrorClassification::USER_ERROR, 'Something went wrong during copying branch');
3232
                        break;
3233
                    }
3234
                }
3235
            }
3236
        } else {
3237
            $this->log('pages', $uid, SystemLogDatabaseAction::CHECK, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to copy page without permission to this table');
3238
        }
3239
    }
3240
3241
    /**
3242
     * Compile a list of tables that should be copied along when a page is about to be copied.
3243
     *
3244
     * First, get the list that the user is allowed to modify (all if admin),
3245
     * and then check against a possible limitation within "DataHandler->copyWhichTables" if not set to "*"
3246
     * to limit the list further down
3247
     *
3248
     * @return array
3249
     */
3250
    protected function getAllowedTablesToCopyWhenCopyingAPage(): array
3251
    {
3252
        // Finding list of tables to copy.
3253
        // These are the tables, the user may modify
3254
        $copyTablesArray = $this->admin ? $this->compileAdminTables() : explode(',', $this->BE_USER->groupData['tables_modify']);
3255
        // If not all tables are allowed then make a list of allowed tables.
3256
        // That is the tables that figure in both allowed tables AND the copyTable-list
3257
        if (strpos($this->copyWhichTables, '*') === false) {
3258
            $definedTablesToCopy = GeneralUtility::trimExplode(',', $this->copyWhichTables, true);
3259
            // Pages are always allowed
3260
            $definedTablesToCopy[] = 'pages';
3261
            $definedTablesToCopy = array_flip($definedTablesToCopy);
3262
            foreach ($copyTablesArray as $k => $table) {
3263
                if (!$table || !isset($definedTablesToCopy[$table])) {
3264
                    unset($copyTablesArray[$k]);
3265
                }
3266
            }
3267
        }
3268
        $copyTablesArray = array_unique($copyTablesArray);
3269
        return $copyTablesArray;
3270
    }
3271
    /**
3272
     * Copying a single page ($uid) to $destPid and all tables in the array copyTablesArray.
3273
     *
3274
     * @param int $uid Page uid
3275
     * @param int $destPid Destination PID: >=0 then it points to a page-id on which to insert the record (as the first element). <0 then it points to a uid from its own table after which to insert it (works if
3276
     * @param array $copyTablesArray Table on pages to copy along with the page.
3277
     * @param bool $first Is a flag set, if the record copied is NOT a 'slave' to another record copied. That is, if this record was asked to be copied in the cmd-array
3278
     * @return int|null The id of the new page, if applicable.
3279
     * @internal should only be used from within DataHandler
3280
     */
3281
    public function copySpecificPage($uid, $destPid, $copyTablesArray, $first = false)
3282
    {
3283
        // Copy the page itself:
3284
        $theNewRootID = $this->copyRecord('pages', $uid, $destPid, $first);
3285
        $currentWorkspaceId = (int)$this->BE_USER->workspace;
3286
        // If a new page was created upon the copy operation we will proceed with all the tables ON that page:
3287
        if ($theNewRootID) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $theNewRootID of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
3288
            foreach ($copyTablesArray as $table) {
3289
                // All records under the page is copied.
3290
                if ($table && is_array($GLOBALS['TCA'][$table]) && $table !== 'pages') {
3291
                    $fields = ['uid'];
3292
                    $languageField = null;
3293
                    $transOrigPointerField = null;
3294
                    $translationSourceField = null;
3295
                    if (BackendUtility::isTableLocalizable($table)) {
3296
                        $languageField = $GLOBALS['TCA'][$table]['ctrl']['languageField'];
3297
                        $transOrigPointerField = $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'];
3298
                        $fields[] = $languageField;
3299
                        $fields[] = $transOrigPointerField;
3300
                        if (isset($GLOBALS['TCA'][$table]['ctrl']['translationSource'])) {
3301
                            $translationSourceField = $GLOBALS['TCA'][$table]['ctrl']['translationSource'];
3302
                            $fields[] = $translationSourceField;
3303
                        }
3304
                    }
3305
                    $isTableWorkspaceEnabled = BackendUtility::isTableWorkspaceEnabled($table);
3306
                    if ($isTableWorkspaceEnabled) {
3307
                        $fields[] = 't3ver_oid';
3308
                        $fields[] = 't3ver_state';
3309
                        $fields[] = 't3ver_wsid';
3310
                    }
3311
                    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
3312
                    $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
3313
                    $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, $currentWorkspaceId));
3314
                    $queryBuilder
3315
                        ->select(...$fields)
3316
                        ->from($table)
3317
                        ->where(
3318
                            $queryBuilder->expr()->eq(
3319
                                'pid',
3320
                                $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
3321
                            )
3322
                        );
3323
                    if (!empty($GLOBALS['TCA'][$table]['ctrl']['sortby'])) {
3324
                        $queryBuilder->orderBy($GLOBALS['TCA'][$table]['ctrl']['sortby'], 'DESC');
3325
                    }
3326
                    $queryBuilder->addOrderBy('uid');
3327
                    try {
3328
                        $result = $queryBuilder->execute();
3329
                        $rows = [];
3330
                        $movedLiveIds = [];
3331
                        $movedLiveRecords = [];
3332
                        while ($row = $result->fetch()) {
3333
                            if ($isTableWorkspaceEnabled && (int)$row['t3ver_state'] === VersionState::MOVE_POINTER) {
3334
                                $movedLiveIds[(int)$row['t3ver_oid']] = (int)$row['uid'];
3335
                            }
3336
                            $rows[(int)$row['uid']] = $row;
3337
                        }
3338
                        // Resolve placeholders of workspace versions
3339
                        if (!empty($rows) && $currentWorkspaceId > 0 && $isTableWorkspaceEnabled) {
3340
                            // If a record was moved within the page, the PlainDataResolver needs the moved record
3341
                            // but not the original live version, otherwise the moved record is not considered at all.
3342
                            // For this reason, we find the live ids, where there was also a moved record in the SQL
3343
                            // query above in $movedLiveIds and now we removed them before handing them over to PlainDataResolver.
3344
                            // see changeContentSortingAndCopyDraftPage test
3345
                            foreach ($movedLiveIds as $liveId => $movePlaceHolderId) {
3346
                                if (isset($rows[$liveId])) {
3347
                                    $movedLiveRecords[$movePlaceHolderId] = $rows[$liveId];
3348
                                    unset($rows[$liveId]);
3349
                                }
3350
                            }
3351
                            $rows = array_reverse(
3352
                                $this->resolveVersionedRecords(
3353
                                    $table,
3354
                                    implode(',', $fields),
3355
                                    $GLOBALS['TCA'][$table]['ctrl']['sortby'],
3356
                                    array_keys($rows)
3357
                                ),
3358
                                true
3359
                            );
3360
                            foreach ($movedLiveRecords as $movePlaceHolderId => $liveRecord) {
3361
                                $rows[$movePlaceHolderId] = $liveRecord;
3362
                            }
3363
                        }
3364
                        if (is_array($rows)) {
3365
                            $languageSourceMap = [];
3366
                            $overrideValues = $translationSourceField ? [$translationSourceField => 0] : [];
3367
                            $doRemap = false;
3368
                            foreach ($rows as $row) {
3369
                                // Skip localized records that will be processed in
3370
                                // copyL10nOverlayRecords() on copying the default language record
3371
                                $transOrigPointer = $row[$transOrigPointerField];
3372
                                if ($row[$languageField] > 0 && $transOrigPointer > 0 && (isset($rows[$transOrigPointer]) || isset($movedLiveIds[$transOrigPointer]))) {
3373
                                    continue;
3374
                                }
3375
                                // Copying each of the underlying records...
3376
                                $newUid = $this->copyRecord($table, $row['uid'], $theNewRootID, false, $overrideValues);
3377
                                if ($translationSourceField) {
3378
                                    $languageSourceMap[$row['uid']] = $newUid;
3379
                                    if ($row[$languageField] > 0) {
3380
                                        $doRemap = true;
3381
                                    }
3382
                                }
3383
                            }
3384
                            if ($doRemap) {
3385
                                //remap is needed for records in non-default language records in the "free mode"
3386
                                $this->copy_remapTranslationSourceField($table, $rows, $languageSourceMap);
3387
                            }
3388
                        }
3389
                    } catch (DBALException $e) {
3390
                        $databaseErrorMessage = $e->getPrevious()->getMessage();
3391
                        $this->log($table, $uid, SystemLogDatabaseAction::CHECK, 0, SystemLogErrorClassification::USER_ERROR, 'An SQL error occurred: ' . $databaseErrorMessage);
3392
                    }
3393
                }
3394
            }
3395
            $this->processRemapStack();
3396
            return $theNewRootID;
3397
        }
3398
        return null;
3399
    }
3400
3401
    /**
3402
     * Copying records, but makes a "raw" copy of a record.
3403
     * Basically the only thing observed is field processing like the copying of files and correction of ids. All other fields are 1-1 copied.
3404
     * Technically the copy is made with THIS instance of the DataHandler class contrary to copyRecord() which creates a new instance and uses the processData() function.
3405
     * The copy is created by insertNewCopyVersion() which bypasses most of the regular input checking associated with processData() - maybe copyRecord() should even do this as well!?
3406
     * This function is used to create new versions of a record.
3407
     * NOTICE: DOES NOT CHECK PERMISSIONS to create! And since page permissions are just passed through and not changed to the user who executes the copy we cannot enforce permissions without getting an incomplete copy - unless we change permissions of course.
3408
     *
3409
     * @param string $table Element table
3410
     * @param int $uid Element UID
3411
     * @param int $pid Element PID (real PID, not checked)
3412
     * @param array $overrideArray Override array - must NOT contain any fields not in the table!
3413
     * @param array $workspaceOptions Options to be forwarded if actions happen on a workspace currently
3414
     * @return int Returns the new ID of the record (if applicable)
3415
     * @internal should only be used from within DataHandler
3416
     */
3417
    public function copyRecord_raw($table, $uid, $pid, $overrideArray = [], array $workspaceOptions = [])
3418
    {
3419
        $uid = (int)$uid;
3420
        // Stop any actions if the record is marked to be deleted:
3421
        // (this can occur if IRRE elements are versionized and child elements are removed)
3422
        if ($this->isElementToBeDeleted($table, $uid)) {
3423
            return null;
3424
        }
3425
        // Only copy if the table is defined in TCA, a uid is given and the record wasn't copied before:
3426
        if (!$GLOBALS['TCA'][$table] || !$uid || $this->isRecordCopied($table, $uid)) {
3427
            return null;
3428
        }
3429
3430
        // Fetch record with permission check
3431
        $row = $this->recordInfoWithPermissionCheck($table, $uid, Permission::PAGE_SHOW);
3432
3433
        // This checks if the record can be selected which is all that a copy action requires.
3434
        if ($row === false) {
3435
            $this->log(
3436
                $table,
3437
                $uid,
3438
                SystemLogDatabaseAction::DELETE,
3439
                0,
3440
                SystemLogErrorClassification::USER_ERROR,
3441
                'Attempt to rawcopy/versionize record which either does not exist or you don\'t have permission to read'
3442
            );
3443
            return null;
3444
        }
3445
3446
        // Set up fields which should not be processed. They are still written - just passed through no-questions-asked!
3447
        $nonFields = ['uid', 'pid', 't3ver_oid', 't3ver_wsid', 't3ver_state', 't3ver_stage', 'perms_userid', 'perms_groupid', 'perms_user', 'perms_group', 'perms_everybody'];
3448
3449
        // Merge in override array.
3450
        $row = array_merge($row, $overrideArray);
0 ignored issues
show
Bug introduced by
It seems like $row can also be of type true; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3450
        $row = array_merge(/** @scrutinizer ignore-type */ $row, $overrideArray);
Loading history...
3451
        // Traverse ALL fields of the selected record:
3452
        foreach ($row as $field => $value) {
3453
            if (!in_array($field, $nonFields, true)) {
3454
                // Get TCA configuration for the field:
3455
                $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
3456
                if (is_array($conf)) {
3457
                    // Processing based on the TCA config field type (files, references, flexforms...)
3458
                    $value = $this->copyRecord_procBasedOnFieldType($table, $uid, $field, $value, $row, $conf, $pid, 0, $workspaceOptions);
3459
                }
3460
                // Add value to array.
3461
                $row[$field] = $value;
3462
            }
3463
        }
3464
        $row['pid'] = $pid;
3465
        // Setting original UID:
3466
        if ($GLOBALS['TCA'][$table]['ctrl']['origUid']) {
3467
            $row[$GLOBALS['TCA'][$table]['ctrl']['origUid']] = $uid;
3468
        }
3469
        // Do the copy by internal function
3470
        $theNewSQLID = $this->insertNewCopyVersion($table, $row, $pid);
3471
3472
        // When a record is copied in workspace (eg. to create a delete placeholder record for a live record), records
3473
        // pointing to that record need a reference index update. This is for instance the case in FAL, if a sys_file_reference
3474
        // for a eg. tt_content record is marked as deleted. The tt_content record then needs a reference index update.
3475
        // This scenario seems to currently only show up if in workspaces, so the refindex update is restricted to this for now.
3476
        if (!empty($workspaceOptions)) {
3477
            $this->referenceIndexUpdater->registerUpdateForReferencesToItem($table, (int)$row['uid'], (int)$this->BE_USER->workspace);
3478
        }
3479
3480
        if ($theNewSQLID) {
3481
            $this->dbAnalysisStoreExec();
3482
            $this->dbAnalysisStore = [];
3483
            return $this->copyMappingArray[$table][$uid] = $theNewSQLID;
3484
        }
3485
        return null;
3486
    }
3487
3488
    /**
3489
     * Inserts a record in the database, passing TCA configuration values through checkValue() but otherwise does NOTHING and checks nothing regarding permissions.
3490
     * Passes the "version" parameter to insertDB() so the copy will look like a new version in the log - should probably be changed or modified a bit for more broad usage...
3491
     *
3492
     * @param string $table Table name
3493
     * @param array $fieldArray Field array to insert as a record
3494
     * @param int $realPid The value of PID field.
3495
     * @return int Returns the new ID of the record (if applicable)
3496
     * @internal should only be used from within DataHandler
3497
     */
3498
    public function insertNewCopyVersion($table, $fieldArray, $realPid)
3499
    {
3500
        $id = StringUtility::getUniqueId('NEW');
3501
        // $fieldArray is set as current record.
3502
        // The point is that when new records are created as copies with flex type fields there might be a field containing information about which DataStructure to use and without that information the flexforms cannot be correctly processed.... This should be OK since the $checkValueRecord is used by the flexform evaluation only anyways...
3503
        $this->checkValue_currentRecord = $fieldArray;
3504
        // Makes sure that transformations aren't processed on the copy.
3505
        $backupDontProcessTransformations = $this->dontProcessTransformations;
3506
        $this->dontProcessTransformations = true;
3507
        // Traverse record and input-process each value:
3508
        foreach ($fieldArray as $field => $fieldValue) {
3509
            if (isset($GLOBALS['TCA'][$table]['columns'][$field])) {
3510
                // Evaluating the value.
3511
                $res = $this->checkValue($table, $field, $fieldValue, $id, 'new', $realPid, 0, $fieldArray);
3512
                if (isset($res['value'])) {
3513
                    $fieldArray[$field] = $res['value'];
3514
                }
3515
            }
3516
        }
3517
        // System fields being set:
3518
        if ($GLOBALS['TCA'][$table]['ctrl']['crdate']) {
3519
            $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['crdate']] = $GLOBALS['EXEC_TIME'];
3520
        }
3521
        if ($GLOBALS['TCA'][$table]['ctrl']['cruser_id']) {
3522
            $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['cruser_id']] = $this->userid;
3523
        }
3524
        if ($GLOBALS['TCA'][$table]['ctrl']['tstamp']) {
3525
            $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['tstamp']] = $GLOBALS['EXEC_TIME'];
3526
        }
3527
        // Finally, insert record:
3528
        $this->insertDB($table, $id, $fieldArray, true);
3529
        // Resets dontProcessTransformations to the previous state.
3530
        $this->dontProcessTransformations = $backupDontProcessTransformations;
3531
        // Return new id:
3532
        return $this->substNEWwithIDs[$id];
3533
    }
3534
3535
    /**
3536
     * Processing/Preparing content for copyRecord() function
3537
     *
3538
     * @param string $table Table name
3539
     * @param int $uid Record uid
3540
     * @param string $field Field name being processed
3541
     * @param string $value Input value to be processed.
3542
     * @param array $row Record array
3543
     * @param array $conf TCA field configuration
3544
     * @param int $realDestPid Real page id (pid) the record is copied to
3545
     * @param int $language Language ID (from sys_language table) used in the duplicated record
3546
     * @param array $workspaceOptions Options to be forwarded if actions happen on a workspace currently
3547
     * @return array|string
3548
     * @internal
3549
     * @see copyRecord()
3550
     */
3551
    public function copyRecord_procBasedOnFieldType($table, $uid, $field, $value, $row, $conf, $realDestPid, $language = 0, array $workspaceOptions = [])
3552
    {
3553
        $inlineSubType = $this->getInlineFieldType($conf);
3554
        // Get the localization mode for the current (parent) record (keep|select):
3555
        // Register if there are references to take care of or MM is used on an inline field (no change to value):
3556
        if ($this->isReferenceField($conf) || $inlineSubType === 'mm') {
3557
            $value = $this->copyRecord_processManyToMany($table, $uid, $field, $value, $conf, $language);
3558
        } elseif ($inlineSubType !== false) {
3559
            $value = $this->copyRecord_processInline($table, $uid, $field, $value, $row, $conf, $realDestPid, $language, $workspaceOptions);
3560
        }
3561
        // For "flex" fieldtypes we need to traverse the structure for two reasons: If there are file references they have to be prepended with absolute paths and if there are database reference they MIGHT need to be remapped (still done in remapListedDBRecords())
3562
        if ($conf['type'] === 'flex') {
3563
            // Get current value array:
3564
            $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
3565
            $dataStructureIdentifier = $flexFormTools->getDataStructureIdentifier(
3566
                ['config' => $conf],
3567
                $table,
3568
                $field,
3569
                $row
3570
            );
3571
            $dataStructureArray = $flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
3572
            $currentValueArray = GeneralUtility::xml2array($value);
3573
            // Traversing the XML structure, processing files:
3574
            if (is_array($currentValueArray)) {
3575
                $currentValueArray['data'] = $this->checkValue_flex_procInData($currentValueArray['data'], [], [], $dataStructureArray, [$table, $uid, $field, $realDestPid], 'copyRecord_flexFormCallBack', $workspaceOptions);
3576
                // Setting value as an array! -> which means the input will be processed according to the 'flex' type when the new copy is created.
3577
                $value = $currentValueArray;
3578
            }
3579
        }
3580
        return $value;
3581
    }
3582
3583
    /**
3584
     * Processes the children of an MM relation field (select, group, inline) when the parent record is copied.
3585
     *
3586
     * @param string $table
3587
     * @param int $uid
3588
     * @param string $field
3589
     * @param mixed $value
3590
     * @param array $conf
3591
     * @param string $language
3592
     * @return mixed
3593
     */
3594
    protected function copyRecord_processManyToMany($table, $uid, $field, $value, $conf, $language)
3595
    {
3596
        $allowedTables = $conf['type'] === 'group' ? $conf['allowed'] : $conf['foreign_table'];
3597
        $prependName = $conf['type'] === 'group' ? $conf['prepend_tname'] : '';
3598
        $mmTable = isset($conf['MM']) && $conf['MM'] ? $conf['MM'] : '';
3599
        $localizeForeignTable = isset($conf['foreign_table']) && BackendUtility::isTableLocalizable($conf['foreign_table']);
3600
        // Localize referenced records of select fields:
3601
        $localizingNonManyToManyFieldReferences = empty($mmTable) && $localizeForeignTable && isset($conf['localizeReferencesAtParentLocalization']) && $conf['localizeReferencesAtParentLocalization'];
3602
        /** @var RelationHandler $dbAnalysis */
3603
        $dbAnalysis = $this->createRelationHandlerInstance();
3604
        $dbAnalysis->start($value, $allowedTables, $mmTable, $uid, $table, $conf);
3605
        $purgeItems = false;
3606
        if ($language > 0 && $localizingNonManyToManyFieldReferences) {
3607
            foreach ($dbAnalysis->itemArray as $index => $item) {
3608
                // Since select fields can reference many records, check whether there's already a localization:
3609
                $recordLocalization = BackendUtility::getRecordLocalization($item['table'], $item['id'], $language);
0 ignored issues
show
Bug introduced by
$language of type string is incompatible with the type integer expected by parameter $language of TYPO3\CMS\Backend\Utilit...getRecordLocalization(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3609
                $recordLocalization = BackendUtility::getRecordLocalization($item['table'], $item['id'], /** @scrutinizer ignore-type */ $language);
Loading history...
3610
                if ($recordLocalization) {
3611
                    $dbAnalysis->itemArray[$index]['id'] = $recordLocalization[0]['uid'];
3612
                } elseif ($this->isNestedElementCallRegistered($item['table'], $item['id'], 'localize-' . (string)$language) === false) {
3613
                    $dbAnalysis->itemArray[$index]['id'] = $this->localize($item['table'], $item['id'], $language);
0 ignored issues
show
Bug introduced by
$language of type string is incompatible with the type integer expected by parameter $language of TYPO3\CMS\Core\DataHandl...DataHandler::localize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3613
                    $dbAnalysis->itemArray[$index]['id'] = $this->localize($item['table'], $item['id'], /** @scrutinizer ignore-type */ $language);
Loading history...
3614
                }
3615
            }
3616
            $purgeItems = true;
3617
        }
3618
3619
        if ($purgeItems || $mmTable) {
3620
            $dbAnalysis->purgeItemArray();
3621
            $value = implode(',', $dbAnalysis->getValueArray($prependName));
0 ignored issues
show
Bug introduced by
It seems like $prependName can also be of type string; however, parameter $prependTableName of TYPO3\CMS\Core\Database\...andler::getValueArray() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3621
            $value = implode(',', $dbAnalysis->getValueArray(/** @scrutinizer ignore-type */ $prependName));
Loading history...
3622
        }
3623
        // Setting the value in this array will notify the remapListedDBRecords() function that this field MAY need references to be corrected
3624
        if ($value) {
3625
            $this->registerDBList[$table][$uid][$field] = $value;
3626
        }
3627
3628
        return $value;
3629
    }
3630
3631
    /**
3632
     * Processes child records in an inline (IRRE) element when the parent record is copied.
3633
     *
3634
     * @param string $table
3635
     * @param int $uid
3636
     * @param string $field
3637
     * @param mixed $value
3638
     * @param array $row
3639
     * @param array $conf
3640
     * @param int $realDestPid
3641
     * @param string $language
3642
     * @param array $workspaceOptions
3643
     * @return string
3644
     */
3645
    protected function copyRecord_processInline(
3646
        $table,
3647
        $uid,
3648
        $field,
3649
        $value,
3650
        $row,
3651
        $conf,
3652
        $realDestPid,
3653
        $language,
3654
        array $workspaceOptions
3655
    ) {
3656
        // Fetch the related child records using \TYPO3\CMS\Core\Database\RelationHandler
3657
        /** @var RelationHandler $dbAnalysis */
3658
        $dbAnalysis = $this->createRelationHandlerInstance();
3659
        $dbAnalysis->start($value, $conf['foreign_table'], '', $uid, $table, $conf);
3660
        // Walk through the items, copy them and remember the new id:
3661
        foreach ($dbAnalysis->itemArray as $k => $v) {
3662
            $newId = null;
3663
            // If language is set and differs from original record, this isn't a copy action but a localization of our parent/ancestor:
3664
            if ($language > 0 && BackendUtility::isTableLocalizable($table) && $language != $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']]) {
3665
                // Children should be localized when the parent gets localized the first time, just do it:
3666
                $newId = $this->localize($v['table'], $v['id'], $language);
0 ignored issues
show
Bug introduced by
$language of type string is incompatible with the type integer expected by parameter $language of TYPO3\CMS\Core\DataHandl...DataHandler::localize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3666
                $newId = $this->localize($v['table'], $v['id'], /** @scrutinizer ignore-type */ $language);
Loading history...
3667
            } else {
3668
                if (!MathUtility::canBeInterpretedAsInteger($realDestPid)) {
3669
                    $newId = $this->copyRecord($v['table'], $v['id'], -$v['id']);
3670
                // If the destination page id is a NEW string, keep it on the same page
3671
                } elseif ($this->BE_USER->workspace > 0 && BackendUtility::isTableWorkspaceEnabled($v['table'])) {
3672
                    // A filled $workspaceOptions indicated that this call
3673
                    // has it's origin in previous versionizeRecord() processing
3674
                    if (!empty($workspaceOptions)) {
3675
                        // Versions use live default id, thus the "new"
3676
                        // id is the original live default child record
3677
                        $newId = $v['id'];
3678
                        $this->versionizeRecord(
3679
                            $v['table'],
3680
                            $v['id'],
3681
                            $workspaceOptions['label'] ?? 'Auto-created for WS #' . $this->BE_USER->workspace,
3682
                            $workspaceOptions['delete'] ?? false
3683
                        );
3684
                    // Otherwise just use plain copyRecord() to create placeholders etc.
3685
                    } else {
3686
                        // If a record has been copied already during this request,
3687
                        // prevent superfluous duplication and use the existing copy
3688
                        if (isset($this->copyMappingArray[$v['table']][$v['id']])) {
3689
                            $newId = $this->copyMappingArray[$v['table']][$v['id']];
3690
                        } else {
3691
                            $newId = $this->copyRecord($v['table'], $v['id'], $realDestPid);
3692
                        }
3693
                    }
3694
                } elseif ($this->BE_USER->workspace > 0 && !BackendUtility::isTableWorkspaceEnabled($v['table'])) {
3695
                    // We are in workspace context creating a new parent version and have a child table
3696
                    // that is not workspace aware. We don't do anything with this child.
3697
                    continue;
3698
                } else {
3699
                    // If a record has been copied already during this request,
3700
                    // prevent superfluous duplication and use the existing copy
3701
                    if (isset($this->copyMappingArray[$v['table']][$v['id']])) {
3702
                        $newId = $this->copyMappingArray[$v['table']][$v['id']];
3703
                    } else {
3704
                        $newId = $this->copyRecord_raw($v['table'], $v['id'], $realDestPid, [], $workspaceOptions);
3705
                    }
3706
                }
3707
            }
3708
            // If the current field is set on a page record, update the pid of related child records:
3709
            if ($table === 'pages') {
3710
                $this->registerDBPids[$v['table']][$v['id']] = $uid;
3711
            } elseif (isset($this->registerDBPids[$table][$uid])) {
3712
                $this->registerDBPids[$v['table']][$v['id']] = $this->registerDBPids[$table][$uid];
3713
            }
3714
            $dbAnalysis->itemArray[$k]['id'] = $newId;
3715
        }
3716
        // Store the new values, we will set up the uids for the subtype later on (exception keep localization from original record):
3717
        $value = implode(',', $dbAnalysis->getValueArray());
3718
        $this->registerDBList[$table][$uid][$field] = $value;
3719
3720
        return $value;
3721
    }
3722
3723
    /**
3724
     * Callback function for traversing the FlexForm structure in relation to creating copied files of file relations inside of flex form structures.
3725
     *
3726
     * @param array $pParams Array of parameters in num-indexes: table, uid, field
3727
     * @param array $dsConf TCA field configuration (from Data Structure XML)
3728
     * @param string $dataValue The value of the flexForm field
3729
     * @param string $_1 Not used.
3730
     * @param string $_2 Not used.
3731
     * @param string $_3 Not used.
3732
     * @param array $workspaceOptions
3733
     * @return array Result array with key "value" containing the value of the processing.
3734
     * @see copyRecord()
3735
     * @see checkValue_flex_procInData_travDS()
3736
     * @internal should only be used from within DataHandler
3737
     */
3738
    public function copyRecord_flexFormCallBack($pParams, $dsConf, $dataValue, $_1, $_2, $_3, $workspaceOptions)
3739
    {
3740
        // Extract parameters:
3741
        [$table, $uid, $field, $realDestPid] = $pParams;
3742
        // If references are set for this field, set flag so they can be corrected later (in ->remapListedDBRecords())
3743
        if (($this->isReferenceField($dsConf) || $this->getInlineFieldType($dsConf) !== false) && (string)$dataValue !== '') {
3744
            $dataValue = $this->copyRecord_procBasedOnFieldType($table, $uid, $field, $dataValue, [], $dsConf, $realDestPid, 0, $workspaceOptions);
3745
            $this->registerDBList[$table][$uid][$field] = 'FlexForm_reference';
3746
        }
3747
        // Return
3748
        return ['value' => $dataValue];
3749
    }
3750
3751
    /**
3752
     * Find l10n-overlay records and perform the requested copy action for these records.
3753
     *
3754
     * @param string $table Record Table
3755
     * @param string $uid UID of the record in the default language
3756
     * @param string $destPid Position to copy to
3757
     * @param bool $first
3758
     * @param array $overrideValues
3759
     * @param string $excludeFields
3760
     * @internal should only be used from within DataHandler
3761
     */
3762
    public function copyL10nOverlayRecords($table, $uid, $destPid, $first = false, $overrideValues = [], $excludeFields = '')
3763
    {
3764
        // There's no need to perform this for tables that are not localizable
3765
        if (!BackendUtility::isTableLocalizable($table)) {
3766
            return;
3767
        }
3768
3769
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
3770
        $queryBuilder->getRestrictions()
3771
            ->removeAll()
3772
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
3773
            ->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, (int)$this->BE_USER->workspace));
3774
3775
        $queryBuilder->select('*')
3776
            ->from($table)
3777
            ->where(
3778
                $queryBuilder->expr()->eq(
3779
                    $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'],
3780
                    $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT, ':pointer')
3781
                )
3782
            );
3783
3784
        // Never copy the actual placeholders around, as the newly copied records are
3785
        // always created as new record / new placeholder pairs
3786
        if (BackendUtility::isTableWorkspaceEnabled($table)) {
3787
            $queryBuilder->andWhere(
3788
                $queryBuilder->expr()->neq(
3789
                    't3ver_state',
3790
                    VersionState::DELETE_PLACEHOLDER
3791
                )
3792
            );
3793
        }
3794
3795
        // If $destPid is < 0, get the pid of the record with uid equal to abs($destPid)
3796
        $tscPID = BackendUtility::getTSconfig_pidValue($table, $uid, $destPid);
0 ignored issues
show
Bug introduced by
$uid of type string is incompatible with the type integer expected by parameter $uid of TYPO3\CMS\Backend\Utilit...:getTSconfig_pidValue(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3796
        $tscPID = BackendUtility::getTSconfig_pidValue($table, /** @scrutinizer ignore-type */ $uid, $destPid);
Loading history...
3797
        // Get the localized records to be copied
3798
        $l10nRecords = $queryBuilder->execute()->fetchAll();
3799
        if (is_array($l10nRecords)) {
3800
            $localizedDestPids = [];
3801
            // If $destPid < 0, then it is the uid of the original language record we are inserting after
3802
            if ($destPid < 0) {
3803
                // Get the localized records of the record we are inserting after
3804
                $queryBuilder->setParameter('pointer', abs($destPid), \PDO::PARAM_INT);
0 ignored issues
show
Bug introduced by
$destPid of type string is incompatible with the type double|integer expected by parameter $num of abs(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3804
                $queryBuilder->setParameter('pointer', abs(/** @scrutinizer ignore-type */ $destPid), \PDO::PARAM_INT);
Loading history...
3805
                $destL10nRecords = $queryBuilder->execute()->fetchAll();
3806
                // Index the localized record uids by language
3807
                if (is_array($destL10nRecords)) {
3808
                    foreach ($destL10nRecords as $record) {
3809
                        $localizedDestPids[$record[$GLOBALS['TCA'][$table]['ctrl']['languageField']]] = -$record['uid'];
3810
                    }
3811
                }
3812
            }
3813
            $languageSourceMap = [
3814
                $uid => $overrideValues[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']]
3815
            ];
3816
            // Copy the localized records after the corresponding localizations of the destination record
3817
            foreach ($l10nRecords as $record) {
3818
                $localizedDestPid = (int)$localizedDestPids[$record[$GLOBALS['TCA'][$table]['ctrl']['languageField']]];
3819
                if ($localizedDestPid < 0) {
3820
                    $newUid = $this->copyRecord($table, $record['uid'], $localizedDestPid, $first, $overrideValues, $excludeFields, $record[$GLOBALS['TCA'][$table]['ctrl']['languageField']]);
3821
                } else {
3822
                    $newUid = $this->copyRecord($table, $record['uid'], $destPid < 0 ? $tscPID : $destPid, $first, $overrideValues, $excludeFields, $record[$GLOBALS['TCA'][$table]['ctrl']['languageField']]);
0 ignored issues
show
Bug introduced by
It seems like $destPid < 0 ? $tscPID : $destPid can also be of type string; however, parameter $destPid of TYPO3\CMS\Core\DataHandl...taHandler::copyRecord() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3822
                    $newUid = $this->copyRecord($table, $record['uid'], /** @scrutinizer ignore-type */ $destPid < 0 ? $tscPID : $destPid, $first, $overrideValues, $excludeFields, $record[$GLOBALS['TCA'][$table]['ctrl']['languageField']]);
Loading history...
3823
                }
3824
                $languageSourceMap[$record['uid']] = $newUid;
3825
            }
3826
            $this->copy_remapTranslationSourceField($table, $l10nRecords, $languageSourceMap);
3827
        }
3828
    }
3829
3830
    /**
3831
     * Remap languageSource field to uids of newly created records
3832
     *
3833
     * @param string $table Table name
3834
     * @param array $l10nRecords array of localized records from the page we're copying from (source records)
3835
     * @param array $languageSourceMap array mapping source records uids to newly copied uids
3836
     */
3837
    protected function copy_remapTranslationSourceField($table, $l10nRecords, $languageSourceMap)
3838
    {
3839
        if (empty($GLOBALS['TCA'][$table]['ctrl']['translationSource']) || empty($GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'])) {
3840
            return;
3841
        }
3842
        $translationSourceFieldName = $GLOBALS['TCA'][$table]['ctrl']['translationSource'];
3843
        $translationParentFieldName = $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'];
3844
3845
        //We can avoid running these update queries by sorting the $l10nRecords by languageSource dependency (in copyL10nOverlayRecords)
3846
        //and first copy records depending on default record (and map the field).
3847
        foreach ($l10nRecords as $record) {
3848
            $oldSourceUid = $record[$translationSourceFieldName];
3849
            if ($oldSourceUid <= 0 && $record[$translationParentFieldName] > 0) {
3850
                //BC fix - in connected mode 'translationSource' field should not be 0
3851
                $oldSourceUid = $record[$translationParentFieldName];
3852
            }
3853
            if ($oldSourceUid > 0) {
3854
                if (empty($languageSourceMap[$oldSourceUid])) {
3855
                    // we don't have mapping information available e.g when copyRecord returned null
3856
                    continue;
3857
                }
3858
                $newFieldValue = $languageSourceMap[$oldSourceUid];
3859
                $updateFields = [
3860
                    $translationSourceFieldName => $newFieldValue
3861
                ];
3862
                GeneralUtility::makeInstance(ConnectionPool::class)
3863
                    ->getConnectionForTable($table)
3864
                    ->update($table, $updateFields, ['uid' => (int)$languageSourceMap[$record['uid']]]);
3865
                if ($this->BE_USER->workspace > 0) {
3866
                    GeneralUtility::makeInstance(ConnectionPool::class)
3867
                        ->getConnectionForTable($table)
3868
                        ->update($table, $updateFields, ['t3ver_oid' => (int)$languageSourceMap[$record['uid']], 't3ver_wsid' => $this->BE_USER->workspace]);
3869
                }
3870
            }
3871
        }
3872
    }
3873
3874
    /*********************************************
3875
     *
3876
     * Cmd: Moving, Localizing
3877
     *
3878
     ********************************************/
3879
    /**
3880
     * Moving single records
3881
     *
3882
     * @param string $table Table name to move
3883
     * @param int $uid Record uid to move
3884
     * @param int $destPid Position to move to: $destPid: >=0 then it points to a page-id on which to insert the record (as the first element). <0 then it points to a uid from its own table after which to insert it (works if
3885
     * @internal should only be used from within DataHandler
3886
     */
3887
    public function moveRecord($table, $uid, $destPid)
3888
    {
3889
        if (!$GLOBALS['TCA'][$table]) {
3890
            return;
3891
        }
3892
3893
        // In case the record to be moved turns out to be an offline version,
3894
        // we have to find the live version and work on that one.
3895
        if ($lookForLiveVersion = BackendUtility::getLiveVersionOfRecord($table, $uid, 'uid')) {
3896
            $uid = $lookForLiveVersion['uid'];
3897
        }
3898
        // Initialize:
3899
        $destPid = (int)$destPid;
3900
        // Get this before we change the pid (for logging)
3901
        $propArr = $this->getRecordProperties($table, $uid);
3902
        $moveRec = $this->getRecordProperties($table, $uid, true);
3903
        // This is the actual pid of the moving to destination
3904
        $resolvedPid = $this->resolvePid($table, $destPid);
3905
        // Finding out, if the record may be moved from where it is. If the record is a non-page, then it depends on edit-permissions.
3906
        // If the record is a page, then there are two options: If the page is moved within itself,
3907
        // (same pid) it's edit-perms of the pid. If moved to another place then its both delete-perms of the pid and new-page perms on the destination.
3908
        if ($table !== 'pages' || $resolvedPid == $moveRec['pid']) {
3909
            // Edit rights for the record...
3910
            $mayMoveAccess = $this->checkRecordUpdateAccess($table, $uid);
3911
        } else {
3912
            $mayMoveAccess = $this->doesRecordExist($table, $uid, Permission::PAGE_DELETE);
3913
        }
3914
        // Finding out, if the record may be moved TO another place. Here we check insert-rights (non-pages = edit, pages = new),
3915
        // unless the pages are moved on the same pid, then edit-rights are checked
3916
        if ($table !== 'pages' || $resolvedPid != $moveRec['pid']) {
3917
            // Insert rights for the record...
3918
            $mayInsertAccess = $this->checkRecordInsertAccess($table, $resolvedPid, SystemLogDatabaseAction::MOVE);
3919
        } else {
3920
            $mayInsertAccess = $this->checkRecordUpdateAccess($table, $uid);
3921
        }
3922
        // Checking if there is anything else disallowing moving the record by checking if editing is allowed
3923
        $fullLanguageCheckNeeded = $table !== 'pages';
3924
        $mayEditAccess = $this->BE_USER->recordEditAccessInternals($table, $uid, false, false, $fullLanguageCheckNeeded);
3925
        // If moving is allowed, begin the processing:
3926
        if (!$mayEditAccess) {
3927
            $this->log($table, $uid, SystemLogDatabaseAction::MOVE, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to move record "%s" (%s) without having permissions to do so. [' . $this->BE_USER->errorMsg . ']', 14, [$propArr['header'], $table . ':' . $uid], $propArr['event_pid']);
3928
            return;
3929
        }
3930
3931
        if (!$mayMoveAccess) {
3932
            $this->log($table, $uid, SystemLogDatabaseAction::MOVE, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to move record \'%s\' (%s) without having permissions to do so.', 14, [$propArr['header'], $table . ':' . $uid], $propArr['event_pid']);
3933
            return;
3934
        }
3935
3936
        if (!$mayInsertAccess) {
3937
            $this->log($table, $uid, SystemLogDatabaseAction::MOVE, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to move record \'%s\' (%s) without having permissions to insert.', 14, [$propArr['header'], $table . ':' . $uid], $propArr['event_pid']);
3938
            return;
3939
        }
3940
3941
        $recordWasMoved = false;
3942
        // Move the record via a hook, used e.g. for versioning
3943
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['moveRecordClass'] ?? [] as $className) {
3944
            $hookObj = GeneralUtility::makeInstance($className);
3945
            if (method_exists($hookObj, 'moveRecord')) {
3946
                $hookObj->moveRecord($table, $uid, $destPid, $propArr, $moveRec, $resolvedPid, $recordWasMoved, $this);
3947
            }
3948
        }
3949
        // Move the record if a hook hasn't moved it yet
3950
        if (!$recordWasMoved) {
0 ignored issues
show
introduced by
The condition $recordWasMoved is always false.
Loading history...
3951
            $this->moveRecord_raw($table, $uid, $destPid);
3952
        }
3953
    }
3954
3955
    /**
3956
     * Moves a record without checking security of any sort.
3957
     * USE ONLY INTERNALLY
3958
     *
3959
     * @param string $table Table name to move
3960
     * @param int $uid Record uid to move
3961
     * @param int $destPid Position to move to: $destPid: >=0 then it points to a page-id on which to insert the record (as the first element). <0 then it points to a uid from its own table after which to insert it (works if
3962
     * @see moveRecord()
3963
     * @internal should only be used from within DataHandler
3964
     */
3965
    public function moveRecord_raw($table, $uid, $destPid)
3966
    {
3967
        $sortColumn = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ?? '';
3968
        $origDestPid = $destPid;
3969
        // This is the actual pid of the moving to destination
3970
        $resolvedPid = $this->resolvePid($table, $destPid);
3971
        // Checking if the pid is negative, but no sorting row is defined. In that case, find the correct pid.
3972
        // Basically this check make the error message 4-13 meaning less... But you can always remove this check if you
3973
        // prefer the error instead of a no-good action (which is to move the record to its own page...)
3974
        if (($destPid < 0 && !$sortColumn) || $destPid >= 0) {
3975
            $destPid = $resolvedPid;
3976
        }
3977
        // Get this before we change the pid (for logging)
3978
        $propArr = $this->getRecordProperties($table, $uid);
3979
        $moveRec = $this->getRecordProperties($table, $uid, true);
3980
        // Prepare user defined objects (if any) for hooks which extend this function:
3981
        $hookObjectsArr = [];
3982
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['moveRecordClass'] ?? [] as $className) {
3983
            $hookObjectsArr[] = GeneralUtility::makeInstance($className);
3984
        }
3985
        // Timestamp field:
3986
        $updateFields = [];
3987
        if ($GLOBALS['TCA'][$table]['ctrl']['tstamp']) {
3988
            $updateFields[$GLOBALS['TCA'][$table]['ctrl']['tstamp']] = $GLOBALS['EXEC_TIME'];
3989
        }
3990
3991
        // Check if this is a translation of a page, if so then it just needs to be kept "sorting" in sync
3992
        // Usually called from moveL10nOverlayRecords()
3993
        if ($table === 'pages') {
3994
            $defaultLanguagePageUid = $this->getDefaultLanguagePageId((int)$uid);
3995
            // In workspaces, the default language page may have been moved to a different pid than the
3996
            // default language page record of live workspace. In this case, localized pages need to be
3997
            // moved to the pid of the workspace move record.
3998
            $defaultLanguagePageWorkspaceOverlay = BackendUtility::getWorkspaceVersionOfRecord((int)$this->BE_USER->workspace, 'pages', $defaultLanguagePageUid, 'uid');
3999
            if (is_array($defaultLanguagePageWorkspaceOverlay)) {
4000
                $defaultLanguagePageUid = (int)$defaultLanguagePageWorkspaceOverlay['uid'];
4001
            }
4002
            if ($defaultLanguagePageUid !== (int)$uid) {
4003
                // If the default language page has been moved, localized pages need to be moved to
4004
                // that pid and sorting, too.
4005
                $originalTranslationRecord = $this->recordInfo($table, $defaultLanguagePageUid, 'pid,' . $sortColumn);
4006
                $updateFields[$sortColumn] = $originalTranslationRecord[$sortColumn];
4007
                $destPid = $originalTranslationRecord['pid'];
4008
            }
4009
        }
4010
4011
        // Insert as first element on page (where uid = $destPid)
4012
        if ($destPid >= 0) {
4013
            if ($table !== 'pages' || $this->destNotInsideSelf($destPid, $uid)) {
4014
                // Clear cache before moving
4015
                [$parentUid] = BackendUtility::getTSCpid($table, $uid, '');
4016
                $this->registerRecordIdForPageCacheClearing($table, $uid, $parentUid);
4017
                // Setting PID
4018
                $updateFields['pid'] = $destPid;
4019
                // Table is sorted by 'sortby'
4020
                if ($sortColumn && !isset($updateFields[$sortColumn])) {
4021
                    $sortNumber = $this->getSortNumber($table, $uid, $destPid);
4022
                    $updateFields[$sortColumn] = $sortNumber;
4023
                }
4024
                // Check for child records that have also to be moved
4025
                $this->moveRecord_procFields($table, $uid, $destPid);
4026
                // Create query for update:
4027
                GeneralUtility::makeInstance(ConnectionPool::class)
4028
                    ->getConnectionForTable($table)
4029
                    ->update($table, $updateFields, ['uid' => (int)$uid]);
4030
                // Check for the localizations of that element
4031
                $this->moveL10nOverlayRecords($table, $uid, $destPid, $destPid);
4032
                // Call post processing hooks:
4033
                foreach ($hookObjectsArr as $hookObj) {
4034
                    if (method_exists($hookObj, 'moveRecord_firstElementPostProcess')) {
4035
                        $hookObj->moveRecord_firstElementPostProcess($table, $uid, $destPid, $moveRec, $updateFields, $this);
4036
                    }
4037
                }
4038
4039
                $this->getRecordHistoryStore()->moveRecord($table, $uid, ['oldPageId' => $propArr['pid'], 'newPageId' => $destPid, 'oldData' => $propArr, 'newData' => $updateFields], $this->correlationId);
4040
                if ($this->enableLogging) {
4041
                    // Logging...
4042
                    $oldpagePropArr = $this->getRecordProperties('pages', $propArr['pid']);
4043
                    if ($destPid != $propArr['pid']) {
4044
                        // Logged to old page
4045
                        $newPropArr = $this->getRecordProperties($table, $uid);
4046
                        $newpagePropArr = $this->getRecordProperties('pages', $destPid);
4047
                        $this->log($table, $uid, SystemLogDatabaseAction::MOVE, $destPid, SystemLogErrorClassification::MESSAGE, 'Moved record \'%s\' (%s) to page \'%s\' (%s)', 2, [$propArr['header'], $table . ':' . $uid, $newpagePropArr['header'], $newPropArr['pid']], $propArr['pid']);
4048
                        // Logged to new page
4049
                        $this->log($table, $uid, SystemLogDatabaseAction::MOVE, $destPid, SystemLogErrorClassification::MESSAGE, 'Moved record \'%s\' (%s) from page \'%s\' (%s)', 3, [$propArr['header'], $table . ':' . $uid, $oldpagePropArr['header'], $propArr['pid']], $destPid);
4050
                    } else {
4051
                        // Logged to new page
4052
                        $this->log($table, $uid, SystemLogDatabaseAction::MOVE, $destPid, SystemLogErrorClassification::MESSAGE, 'Moved record \'%s\' (%s) on page \'%s\' (%s)', 4, [$propArr['header'], $table . ':' . $uid, $oldpagePropArr['header'], $propArr['pid']], $destPid);
4053
                    }
4054
                }
4055
                // Clear cache after moving
4056
                $this->registerRecordIdForPageCacheClearing($table, $uid);
4057
                $this->fixUniqueInPid($table, $uid);
4058
                $this->fixUniqueInSite($table, (int)$uid);
4059
                if ($table === 'pages') {
4060
                    $this->fixUniqueInSiteForSubpages((int)$uid);
4061
                }
4062
            } elseif ($this->enableLogging) {
4063
                $destPropArr = $this->getRecordProperties('pages', $destPid);
4064
                $this->log($table, $uid, SystemLogDatabaseAction::MOVE, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to move page \'%s\' (%s) to inside of its own rootline (at page \'%s\' (%s))', 10, [$propArr['header'], $uid, $destPropArr['header'], $destPid], $propArr['pid']);
4065
            }
4066
        } elseif ($sortColumn) {
4067
            // Put after another record
4068
            // Table is being sorted
4069
            // Save the position to which the original record is requested to be moved
4070
            $originalRecordDestinationPid = $destPid;
4071
            $sortInfo = $this->getSortNumber($table, $uid, $destPid);
4072
            // Setting the destPid to the new pid of the record.
4073
            $destPid = $sortInfo['pid'];
4074
            // If not an array, there was an error (which is already logged)
4075
            if (is_array($sortInfo)) {
4076
                if ($table !== 'pages' || $this->destNotInsideSelf($destPid, $uid)) {
4077
                    // clear cache before moving
4078
                    $this->registerRecordIdForPageCacheClearing($table, $uid);
4079
                    // We now update the pid and sortnumber (if not set for page translations)
4080
                    $updateFields['pid'] = $destPid;
4081
                    if (!isset($updateFields[$sortColumn])) {
4082
                        $updateFields[$sortColumn] = $sortInfo['sortNumber'];
4083
                    }
4084
                    // Check for child records that have also to be moved
4085
                    $this->moveRecord_procFields($table, $uid, $destPid);
4086
                    // Create query for update:
4087
                    GeneralUtility::makeInstance(ConnectionPool::class)
4088
                        ->getConnectionForTable($table)
4089
                        ->update($table, $updateFields, ['uid' => (int)$uid]);
4090
                    // Check for the localizations of that element
4091
                    $this->moveL10nOverlayRecords($table, $uid, $destPid, $originalRecordDestinationPid);
4092
                    // Call post processing hooks:
4093
                    foreach ($hookObjectsArr as $hookObj) {
4094
                        if (method_exists($hookObj, 'moveRecord_afterAnotherElementPostProcess')) {
4095
                            $hookObj->moveRecord_afterAnotherElementPostProcess($table, $uid, $destPid, $origDestPid, $moveRec, $updateFields, $this);
4096
                        }
4097
                    }
4098
                    $this->getRecordHistoryStore()->moveRecord($table, $uid, ['oldPageId' => $propArr['pid'], 'newPageId' => $destPid, 'oldData' => $propArr, 'newData' => $updateFields], $this->correlationId);
4099
                    if ($this->enableLogging) {
4100
                        // Logging...
4101
                        $oldpagePropArr = $this->getRecordProperties('pages', $propArr['pid']);
4102
                        if ($destPid != $propArr['pid']) {
4103
                            // Logged to old page
4104
                            $newPropArr = $this->getRecordProperties($table, $uid);
4105
                            $newpagePropArr = $this->getRecordProperties('pages', $destPid);
4106
                            $this->log($table, $uid, SystemLogDatabaseAction::MOVE, 0, SystemLogErrorClassification::MESSAGE, 'Moved record \'%s\' (%s) to page \'%s\' (%s)', 2, [$propArr['header'], $table . ':' . $uid, $newpagePropArr['header'], $newPropArr['pid']], $propArr['pid']);
4107
                            // Logged to old page
4108
                            $this->log($table, $uid, SystemLogDatabaseAction::MOVE, 0, SystemLogErrorClassification::MESSAGE, 'Moved record \'%s\' (%s) from page \'%s\' (%s)', 3, [$propArr['header'], $table . ':' . $uid, $oldpagePropArr['header'], $propArr['pid']], $destPid);
4109
                        } else {
4110
                            // Logged to old page
4111
                            $this->log($table, $uid, SystemLogDatabaseAction::MOVE, 0, SystemLogErrorClassification::MESSAGE, 'Moved record \'%s\' (%s) on page \'%s\' (%s)', 4, [$propArr['header'], $table . ':' . $uid, $oldpagePropArr['header'], $propArr['pid']], $destPid);
4112
                        }
4113
                    }
4114
                    // Clear cache after moving
4115
                    $this->registerRecordIdForPageCacheClearing($table, $uid);
4116
                    $this->fixUniqueInPid($table, $uid);
4117
                    $this->fixUniqueInSite($table, (int)$uid);
4118
                    if ($table === 'pages') {
4119
                        $this->fixUniqueInSiteForSubpages((int)$uid);
4120
                    }
4121
                } elseif ($this->enableLogging) {
4122
                    $destPropArr = $this->getRecordProperties('pages', $destPid);
4123
                    $this->log($table, $uid, SystemLogDatabaseAction::MOVE, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to move page \'%s\' (%s) to inside of its own rootline (at page \'%s\' (%s))', 10, [$propArr['header'], $uid, $destPropArr['header'], $destPid], $propArr['pid']);
4124
                }
4125
            } else {
4126
                $this->log($table, $uid, SystemLogDatabaseAction::MOVE, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to move record \'%s\' (%s) to after another record, although the table has no sorting row.', 13, [$propArr['header'], $table . ':' . $uid], $propArr['event_pid']);
4127
            }
4128
        }
4129
    }
4130
4131
    /**
4132
     * Walk through all fields of the moved record and look for children of e.g. the inline type.
4133
     * If child records are found, they are also move to the new $destPid.
4134
     *
4135
     * @param string $table Record Table
4136
     * @param int $uid Record UID
4137
     * @param int $destPid Position to move to
4138
     * @internal should only be used from within DataHandler
4139
     */
4140
    public function moveRecord_procFields($table, $uid, $destPid)
4141
    {
4142
        $row = BackendUtility::getRecordWSOL($table, $uid);
4143
        if (is_array($row) && (int)$destPid !== (int)$row['pid']) {
4144
            $conf = $GLOBALS['TCA'][$table]['columns'];
4145
            foreach ($row as $field => $value) {
4146
                $this->moveRecord_procBasedOnFieldType($table, $uid, $destPid, $field, $value, $conf[$field]['config']);
4147
            }
4148
        }
4149
    }
4150
4151
    /**
4152
     * Move child records depending on the field type of the parent record.
4153
     *
4154
     * @param string $table Record Table
4155
     * @param string $uid Record UID
4156
     * @param string $destPid Position to move to
4157
     * @param string $field Record field
4158
     * @param string $value Record field value
4159
     * @param array $conf TCA configuration of current field
4160
     * @internal should only be used from within DataHandler
4161
     */
4162
    public function moveRecord_procBasedOnFieldType($table, $uid, $destPid, $field, $value, $conf)
4163
    {
4164
        $dbAnalysis = null;
4165
        if ($conf['type'] === 'inline') {
4166
            $foreign_table = $conf['foreign_table'];
4167
            $moveChildrenWithParent = !isset($conf['behaviour']['disableMovingChildrenWithParent']) || !$conf['behaviour']['disableMovingChildrenWithParent'];
4168
            if ($foreign_table && $moveChildrenWithParent) {
4169
                $inlineType = $this->getInlineFieldType($conf);
4170
                if ($inlineType === 'list' || $inlineType === 'field') {
4171
                    if ($table === 'pages') {
4172
                        // If the inline elements are related to a page record,
4173
                        // make sure they reside at that page and not at its parent
4174
                        $destPid = $uid;
4175
                    }
4176
                    $dbAnalysis = $this->createRelationHandlerInstance();
4177
                    $dbAnalysis->start($value, $conf['foreign_table'], '', $uid, $table, $conf);
0 ignored issues
show
Bug introduced by
$uid of type string is incompatible with the type integer expected by parameter $MMuid of TYPO3\CMS\Core\Database\RelationHandler::start(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

4177
                    $dbAnalysis->start($value, $conf['foreign_table'], '', /** @scrutinizer ignore-type */ $uid, $table, $conf);
Loading history...
4178
                }
4179
            }
4180
        }
4181
        // Move the records
4182
        if (isset($dbAnalysis)) {
4183
            // Moving records to a positive destination will insert each
4184
            // record at the beginning, thus the order is reversed here:
4185
            foreach (array_reverse($dbAnalysis->itemArray) as $v) {
4186
                $this->moveRecord($v['table'], $v['id'], $destPid);
0 ignored issues
show
Bug introduced by
$destPid of type string is incompatible with the type integer expected by parameter $destPid of TYPO3\CMS\Core\DataHandl...taHandler::moveRecord(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

4186
                $this->moveRecord($v['table'], $v['id'], /** @scrutinizer ignore-type */ $destPid);
Loading history...
4187
            }
4188
        }
4189
    }
4190
4191
    /**
4192
     * Find l10n-overlay records and perform the requested move action for these records.
4193
     *
4194
     * @param string $table Record Table
4195
     * @param string $uid Record UID
4196
     * @param string $destPid Position to move to
4197
     * @param string $originalRecordDestinationPid Position to move the original record to
4198
     * @internal should only be used from within DataHandler
4199
     */
4200
    public function moveL10nOverlayRecords($table, $uid, $destPid, $originalRecordDestinationPid)
4201
    {
4202
        // There's no need to perform this for non-localizable tables
4203
        if (!BackendUtility::isTableLocalizable($table)) {
4204
            return;
4205
        }
4206
4207
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
4208
        $queryBuilder->getRestrictions()
4209
            ->removeAll()
4210
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
4211
            ->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, $this->BE_USER->workspace));
4212
4213
        $l10nRecords = $queryBuilder->select('*')
4214
            ->from($table)
4215
            ->where(
4216
                $queryBuilder->expr()->eq(
4217
                    $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'],
4218
                    $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT, ':pointer')
4219
                )
4220
            )
4221
            ->execute()
4222
            ->fetchAll();
4223
4224
        if (is_array($l10nRecords)) {
4225
            $localizedDestPids = [];
4226
            // If $$originalRecordDestinationPid < 0, then it is the uid of the original language record we are inserting after
4227
            if ($originalRecordDestinationPid < 0) {
4228
                // Get the localized records of the record we are inserting after
4229
                $queryBuilder->setParameter('pointer', abs($originalRecordDestinationPid), \PDO::PARAM_INT);
0 ignored issues
show
Bug introduced by
$originalRecordDestinationPid of type string is incompatible with the type double|integer expected by parameter $num of abs(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

4229
                $queryBuilder->setParameter('pointer', abs(/** @scrutinizer ignore-type */ $originalRecordDestinationPid), \PDO::PARAM_INT);
Loading history...
4230
                $destL10nRecords = $queryBuilder->execute()->fetchAll();
4231
                // Index the localized record uids by language
4232
                if (is_array($destL10nRecords)) {
4233
                    foreach ($destL10nRecords as $record) {
4234
                        $localizedDestPids[$record[$GLOBALS['TCA'][$table]['ctrl']['languageField']]] = -$record['uid'];
4235
                    }
4236
                }
4237
            }
4238
            // Move the localized records after the corresponding localizations of the destination record
4239
            foreach ($l10nRecords as $record) {
4240
                $localizedDestPid = (int)$localizedDestPids[$record[$GLOBALS['TCA'][$table]['ctrl']['languageField']]];
4241
                if ($localizedDestPid < 0) {
4242
                    $this->moveRecord($table, $record['uid'], $localizedDestPid);
4243
                } else {
4244
                    $this->moveRecord($table, $record['uid'], $destPid);
0 ignored issues
show
Bug introduced by
$destPid of type string is incompatible with the type integer expected by parameter $destPid of TYPO3\CMS\Core\DataHandl...taHandler::moveRecord(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

4244
                    $this->moveRecord($table, $record['uid'], /** @scrutinizer ignore-type */ $destPid);
Loading history...
4245
                }
4246
            }
4247
        }
4248
    }
4249
4250
    /**
4251
     * Localizes a record to another system language
4252
     *
4253
     * @param string $table Table name
4254
     * @param int $uid Record uid (to be localized)
4255
     * @param int $language Language ID (from sys_language table)
4256
     * @return int|bool The uid (int) of the new translated record or FALSE (bool) if something went wrong
4257
     * @internal should only be used from within DataHandler
4258
     */
4259
    public function localize($table, $uid, $language)
4260
    {
4261
        $newId = false;
4262
        $uid = (int)$uid;
4263
        if (!$GLOBALS['TCA'][$table] || !$uid || $this->isNestedElementCallRegistered($table, $uid, 'localize-' . (string)$language) !== false) {
4264
            return false;
4265
        }
4266
4267
        $this->registerNestedElementCall($table, $uid, 'localize-' . (string)$language);
4268
        if (!$GLOBALS['TCA'][$table]['ctrl']['languageField'] || !$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']) {
4269
            $this->newlog('Localization failed; "languageField" and "transOrigPointerField" must be defined for the table ' . $table, SystemLogErrorClassification::USER_ERROR);
4270
            return false;
4271
        }
4272
        $langRec = BackendUtility::getRecord('sys_language', (int)$language, 'uid,title');
4273
        if (!$langRec) {
4274
            $this->newlog('Sys language UID "' . $language . '" not found valid!', SystemLogErrorClassification::USER_ERROR);
4275
            return false;
4276
        }
4277
4278
        if (!$this->doesRecordExist($table, $uid, Permission::PAGE_SHOW)) {
4279
            $this->newlog('Attempt to localize record ' . $table . ':' . $uid . ' without permission.', SystemLogErrorClassification::USER_ERROR);
4280
            return false;
4281
        }
4282
4283
        // Getting workspace overlay if possible - this will localize versions in workspace if any
4284
        $row = BackendUtility::getRecordWSOL($table, $uid);
4285
        if (!is_array($row)) {
0 ignored issues
show
introduced by
The condition is_array($row) is always true.
Loading history...
4286
            $this->newlog('Attempt to localize record ' . $table . ':' . $uid . ' that did not exist!', SystemLogErrorClassification::USER_ERROR);
4287
            return false;
4288
        }
4289
4290
        // Make sure that records which are translated from another language than the default language have a correct
4291
        // localization source set themselves, before translating them to another language.
4292
        if ((int)$row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] !== 0
4293
            && $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0) {
4294
            $localizationParentRecord = BackendUtility::getRecord(
4295
                $table,
4296
                $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']]
4297
            );
4298
            if ((int)$localizationParentRecord[$GLOBALS['TCA'][$table]['ctrl']['languageField']] !== 0) {
4299
                $this->newlog('Localization failed; Source record ' . $table . ':' . $localizationParentRecord['uid'] . ' contained a reference to an original record that is not a default record (which is strange)!', SystemLogErrorClassification::USER_ERROR);
4300
                return false;
4301
            }
4302
        }
4303
4304
        // Default language records must never have a localization parent as they are the origin of any translation.
4305
        if ((int)$row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] !== 0
4306
            && (int)$row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] === 0) {
4307
            $this->newlog('Localization failed; Source record ' . $table . ':' . $row['uid'] . ' contained a reference to an original default record but is a default record itself (which is strange)!', SystemLogErrorClassification::USER_ERROR);
4308
            return false;
4309
        }
4310
4311
        $recordLocalizations = BackendUtility::getRecordLocalization($table, $uid, $language, 'AND pid=' . (int)$row['pid']);
4312
4313
        if (!empty($recordLocalizations)) {
4314
            $this->newlog(sprintf(
4315
                'Localization failed: there already are localizations (%s) for language %d of the "%s" record %d!',
4316
                implode(', ', array_column($recordLocalizations, 'uid')),
4317
                $language,
4318
                $table,
4319
                $uid
4320
            ), 1);
4321
            return false;
4322
        }
4323
4324
        // Initialize:
4325
        $overrideValues = [];
4326
        // Set override values:
4327
        $overrideValues[$GLOBALS['TCA'][$table]['ctrl']['languageField']] = $langRec['uid'];
4328
        // If the translated record is a default language record, set it's uid as localization parent of the new record.
4329
        // If translating from any other language, no override is needed; we just can copy the localization parent of
4330
        // the original record (which is pointing to the correspondent default language record) to the new record.
4331
        // In copy / free mode the TransOrigPointer field is always set to 0, as no connection to the localization parent is wanted in that case.
4332
        // For pages, there is no "copy/free mode".
4333
        if (($this->useTransOrigPointerField || $table === 'pages') && (int)$row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] === 0) {
4334
            $overrideValues[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] = $uid;
4335
        } elseif (!$this->useTransOrigPointerField) {
4336
            $overrideValues[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] = 0;
4337
        }
4338
        if (isset($GLOBALS['TCA'][$table]['ctrl']['translationSource'])) {
4339
            $overrideValues[$GLOBALS['TCA'][$table]['ctrl']['translationSource']] = $uid;
4340
        }
4341
        // Copy the type (if defined in both tables) from the original record so that translation has same type as original record
4342
        if (isset($GLOBALS['TCA'][$table]['ctrl']['type'])) {
4343
            $overrideValues[$GLOBALS['TCA'][$table]['ctrl']['type']] = $row[$GLOBALS['TCA'][$table]['ctrl']['type']];
4344
        }
4345
        // Set exclude Fields:
4346
        foreach ($GLOBALS['TCA'][$table]['columns'] as $fN => $fCfg) {
4347
            $translateToMsg = '';
4348
            // Check if we are just prefixing:
4349
            if ($fCfg['l10n_mode'] === 'prefixLangTitle') {
4350
                if (($fCfg['config']['type'] === 'text' || $fCfg['config']['type'] === 'input') && (string)$row[$fN] !== '') {
4351
                    [$tscPID] = BackendUtility::getTSCpid($table, $uid, '');
4352
                    $TSConfig = BackendUtility::getPagesTSconfig($tscPID)['TCEMAIN.'] ?? [];
4353
                    $tE = $this->getTableEntries($table, $TSConfig);
4354
                    if (!empty($TSConfig['translateToMessage']) && !$tE['disablePrependAtCopy']) {
4355
                        $translateToMsg = $this->getLanguageService()->sL($TSConfig['translateToMessage']);
4356
                        $translateToMsg = @sprintf($translateToMsg, $langRec['title']);
4357
                    }
4358
4359
                    foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processTranslateToClass'] ?? [] as $className) {
4360
                        $hookObj = GeneralUtility::makeInstance($className);
4361
                        if (method_exists($hookObj, 'processTranslateTo_copyAction')) {
4362
                            $hookObj->processTranslateTo_copyAction($row[$fN], $langRec, $this, $fN);
4363
                        }
4364
                    }
4365
                    if (!empty($translateToMsg)) {
4366
                        $overrideValues[$fN] = '[' . $translateToMsg . '] ' . $row[$fN];
4367
                    } else {
4368
                        $overrideValues[$fN] = $row[$fN];
4369
                    }
4370
                }
4371
            }
4372
        }
4373
4374
        if ($table !== 'pages') {
4375
            // Get the uid of record after which this localized record should be inserted
4376
            $previousUid = $this->getPreviousLocalizedRecordUid($table, $uid, $row['pid'], $language);
4377
            // Execute the copy:
4378
            $newId = $this->copyRecord($table, $uid, -$previousUid, true, $overrideValues, '', $language);
4379
        } else {
4380
            // Create new page which needs to contain the same pid as the original page
4381
            $overrideValues['pid'] = $row['pid'];
4382
            // Take over the hidden state of the original language state, this is done due to legacy reasons where-as
4383
            // pages_language_overlay was set to "hidden -> default=0" but pages hidden -> default 1"
4384
            if (!empty($GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'])) {
4385
                $hiddenFieldName = $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'];
4386
                $overrideValues[$hiddenFieldName] = $row[$hiddenFieldName] ?? $GLOBALS['TCA'][$table]['columns'][$hiddenFieldName]['config']['default'];
4387
            }
4388
            $temporaryId = StringUtility::getUniqueId('NEW');
4389
            $copyTCE = $this->getLocalTCE();
4390
            $copyTCE->start([$table => [$temporaryId => $overrideValues]], [], $this->BE_USER);
4391
            $copyTCE->process_datamap();
4392
            // Getting the new UID as if it had been copied:
4393
            $theNewSQLID = $copyTCE->substNEWwithIDs[$temporaryId];
4394
            if ($theNewSQLID) {
4395
                $this->copyMappingArray[$table][$uid] = $theNewSQLID;
4396
                $newId = $theNewSQLID;
4397
            }
4398
        }
4399
4400
        return $newId;
4401
    }
4402
4403
    /**
4404
     * Performs localization or synchronization of child records.
4405
     * The $command argument expects an array, but supports a string for backward-compatibility.
4406
     *
4407
     * $command = array(
4408
     *   'field' => 'tx_myfieldname',
4409
     *   'language' => 2,
4410
     *   // either the key 'action' or 'ids' must be set
4411
     *   'action' => 'synchronize', // or 'localize'
4412
     *   'ids' => array(1, 2, 3, 4) // child element ids
4413
     * );
4414
     *
4415
     * @param string $table The table of the localized parent record
4416
     * @param int $id The uid of the localized parent record
4417
     * @param array|string $command Defines the command to be performed (see example above)
4418
     */
4419
    protected function inlineLocalizeSynchronize($table, $id, $command)
4420
    {
4421
        $parentRecord = BackendUtility::getRecordWSOL($table, $id);
4422
4423
        // Backward-compatibility handling
4424
        if (!is_array($command)) {
4425
            // <field>, (localize | synchronize | <uid>):
4426
            $parts = GeneralUtility::trimExplode(',', $command);
4427
            $command = [
4428
                'field' => $parts[0],
4429
                // The previous process expected $id to point to the localized record already
4430
                'language' => (int)$parentRecord[$GLOBALS['TCA'][$table]['ctrl']['languageField']]
4431
            ];
4432
            if (!MathUtility::canBeInterpretedAsInteger($parts[1])) {
4433
                $command['action'] = $parts[1];
4434
            } else {
4435
                $command['ids'] = [$parts[1]];
4436
            }
4437
        }
4438
4439
        // In case the parent record is the default language record, fetch the localization
4440
        if (empty($parentRecord[$GLOBALS['TCA'][$table]['ctrl']['languageField']])) {
4441
            // Fetch the live record
4442
            // @todo: this needs to be revisited, as getRecordLocalization() does a BackendWorkspaceRestriction
4443
            // based on $GLOBALS[BE_USER], which could differ from the $this->BE_USER->workspace value
4444
            $parentRecordLocalization = BackendUtility::getRecordLocalization($table, $id, $command['language'], 'AND t3ver_oid=0');
4445
            if (empty($parentRecordLocalization)) {
4446
                if ($this->enableLogging) {
4447
                    $this->log($table, $id, SystemLogGenericAction::UNDEFINED, 0, SystemLogErrorClassification::MESSAGE, 'Localization for parent record ' . $table . ':' . $id . '" cannot be fetched', -1, [], $this->eventPid($table, $id, $parentRecord['pid']));
4448
                }
4449
                return;
4450
            }
4451
            $parentRecord = $parentRecordLocalization[0];
4452
            $id = $parentRecord['uid'];
4453
            // Process overlay for current selected workspace
4454
            BackendUtility::workspaceOL($table, $parentRecord);
4455
        }
4456
4457
        $field = $command['field'];
4458
        $language = $command['language'];
4459
        $action = $command['action'];
4460
        $ids = $command['ids'];
4461
4462
        if (!$field || !($action === 'localize' || $action === 'synchronize') && empty($ids) || !isset($GLOBALS['TCA'][$table]['columns'][$field]['config'])) {
4463
            return;
4464
        }
4465
4466
        $config = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
4467
        $foreignTable = $config['foreign_table'];
4468
4469
        $transOrigPointer = (int)$parentRecord[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']];
4470
        $childTransOrigPointerField = $GLOBALS['TCA'][$foreignTable]['ctrl']['transOrigPointerField'];
4471
4472
        if (!$parentRecord || !is_array($parentRecord) || $language <= 0 || !$transOrigPointer) {
4473
            return;
4474
        }
4475
4476
        $inlineSubType = $this->getInlineFieldType($config);
4477
        if ($inlineSubType === false) {
4478
            return;
4479
        }
4480
4481
        $transOrigRecord = BackendUtility::getRecordWSOL($table, $transOrigPointer);
4482
4483
        $removeArray = [];
4484
        $mmTable = $inlineSubType === 'mm' && isset($config['MM']) && $config['MM'] ? $config['MM'] : '';
4485
        // Fetch children from original language parent:
4486
        /** @var RelationHandler $dbAnalysisOriginal */
4487
        $dbAnalysisOriginal = $this->createRelationHandlerInstance();
4488
        $dbAnalysisOriginal->start($transOrigRecord[$field], $foreignTable, $mmTable, $transOrigRecord['uid'], $table, $config);
4489
        $elementsOriginal = [];
4490
        foreach ($dbAnalysisOriginal->itemArray as $item) {
4491
            $elementsOriginal[$item['id']] = $item;
4492
        }
4493
        unset($dbAnalysisOriginal);
4494
        // Fetch children from current localized parent:
4495
        /** @var RelationHandler $dbAnalysisCurrent */
4496
        $dbAnalysisCurrent = $this->createRelationHandlerInstance();
4497
        $dbAnalysisCurrent->start($parentRecord[$field], $foreignTable, $mmTable, $id, $table, $config);
4498
        // Perform synchronization: Possibly removal of already localized records:
4499
        if ($action === 'synchronize') {
4500
            foreach ($dbAnalysisCurrent->itemArray as $index => $item) {
4501
                $childRecord = BackendUtility::getRecordWSOL($item['table'], $item['id']);
4502
                if (isset($childRecord[$childTransOrigPointerField]) && $childRecord[$childTransOrigPointerField] > 0) {
4503
                    $childTransOrigPointer = $childRecord[$childTransOrigPointerField];
4504
                    // If synchronization is requested, child record was translated once, but original record does not exist anymore, remove it:
4505
                    if (!isset($elementsOriginal[$childTransOrigPointer])) {
4506
                        unset($dbAnalysisCurrent->itemArray[$index]);
4507
                        $removeArray[$item['table']][$item['id']]['delete'] = 1;
4508
                    }
4509
                }
4510
            }
4511
        }
4512
        // Perform synchronization/localization: Possibly add unlocalized records for original language:
4513
        if ($action === 'localize' || $action === 'synchronize') {
4514
            foreach ($elementsOriginal as $originalId => $item) {
4515
                $item['id'] = $this->localize($item['table'], $item['id'], $language);
4516
                $item['id'] = $this->overlayAutoVersionId($item['table'], $item['id']);
4517
                $dbAnalysisCurrent->itemArray[] = $item;
4518
            }
4519
        } elseif (!empty($ids)) {
4520
            foreach ($ids as $childId) {
4521
                if (!MathUtility::canBeInterpretedAsInteger($childId) || !isset($elementsOriginal[$childId])) {
4522
                    continue;
4523
                }
4524
                $item = $elementsOriginal[$childId];
4525
                $item['id'] = $this->localize($item['table'], $item['id'], $language);
4526
                $item['id'] = $this->overlayAutoVersionId($item['table'], $item['id']);
4527
                $dbAnalysisCurrent->itemArray[] = $item;
4528
            }
4529
        }
4530
        // Store the new values, we will set up the uids for the subtype later on (exception keep localization from original record):
4531
        $value = implode(',', $dbAnalysisCurrent->getValueArray());
4532
        $this->registerDBList[$table][$id][$field] = $value;
4533
        // Remove child records (if synchronization requested it):
4534
        if (is_array($removeArray) && !empty($removeArray)) {
4535
            /** @var DataHandler $tce */
4536
            $tce = GeneralUtility::makeInstance(__CLASS__, $this->referenceIndexUpdater);
4537
            $tce->enableLogging = $this->enableLogging;
4538
            $tce->start([], $removeArray, $this->BE_USER);
4539
            $tce->process_cmdmap();
4540
            unset($tce);
4541
        }
4542
        $updateFields = [];
4543
        // Handle, reorder and store relations:
4544
        if ($inlineSubType === 'list') {
4545
            $updateFields = [$field => $value];
4546
        } elseif ($inlineSubType === 'field') {
4547
            $dbAnalysisCurrent->writeForeignField($config, $id);
4548
            $updateFields = [$field => $dbAnalysisCurrent->countItems(false)];
4549
        } elseif ($inlineSubType === 'mm') {
4550
            $dbAnalysisCurrent->writeMM($config['MM'], $id);
4551
            $updateFields = [$field => $dbAnalysisCurrent->countItems(false)];
4552
        }
4553
        // Update field referencing to child records of localized parent record:
4554
        if (!empty($updateFields)) {
4555
            $this->updateDB($table, $id, $updateFields);
4556
        }
4557
    }
4558
4559
    /*********************************************
4560
     *
4561
     * Cmd: delete
4562
     *
4563
     ********************************************/
4564
    /**
4565
     * Delete a single record
4566
     *
4567
     * @param string $table Table name
4568
     * @param int $id Record UID
4569
     * @internal should only be used from within DataHandler
4570
     */
4571
    public function deleteAction($table, $id)
4572
    {
4573
        $recordToDelete = BackendUtility::getRecord($table, $id);
4574
4575
        if (is_array($recordToDelete) && isset($recordToDelete['t3ver_wsid']) && (int)$recordToDelete['t3ver_wsid'] !== 0) {
4576
            // When dealing with a workspace record, use discard.
4577
            $this->discard($table, null, $recordToDelete);
4578
            return;
4579
        }
4580
4581
        // Record asked to be deleted was found:
4582
        if (is_array($recordToDelete)) {
4583
            $recordWasDeleted = false;
4584
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'] ?? [] as $className) {
4585
                $hookObj = GeneralUtility::makeInstance($className);
4586
                if (method_exists($hookObj, 'processCmdmap_deleteAction')) {
4587
                    $hookObj->processCmdmap_deleteAction($table, $id, $recordToDelete, $recordWasDeleted, $this);
4588
                }
4589
            }
4590
            // Delete the record if a hook hasn't deleted it yet
4591
            if (!$recordWasDeleted) {
0 ignored issues
show
introduced by
The condition $recordWasDeleted is always false.
Loading history...
4592
                $this->deleteEl($table, $id);
4593
            }
4594
        }
4595
    }
4596
4597
    /**
4598
     * Delete element from any table
4599
     *
4600
     * @param string $table Table name
4601
     * @param int $uid Record UID
4602
     * @param bool $noRecordCheck Flag: If $noRecordCheck is set, then the function does not check permission to delete record
4603
     * @param bool $forceHardDelete If TRUE, the "deleted" flag is ignored if applicable for record and the record is deleted COMPLETELY!
4604
     * @param bool $deleteRecordsOnPage If false and if deleting pages, records on the page will not be deleted (edge case while swapping workspaces)
4605
     * @internal should only be used from within DataHandler
4606
     */
4607
    public function deleteEl($table, $uid, $noRecordCheck = false, $forceHardDelete = false, bool $deleteRecordsOnPage = true)
4608
    {
4609
        if ($table === 'pages') {
4610
            $this->deletePages($uid, $noRecordCheck, $forceHardDelete, $deleteRecordsOnPage);
4611
        } else {
4612
            $this->discardWorkspaceVersionsOfRecord($table, $uid);
4613
            $this->deleteRecord($table, $uid, $noRecordCheck, $forceHardDelete);
4614
        }
4615
    }
4616
4617
    /**
4618
     * Discard workspace overlays of a live record: When a live row
4619
     * is deleted, all existing workspace overlays are discarded.
4620
     *
4621
     * @param string $table Table name
4622
     * @param int $uid Record UID
4623
     * @internal should only be used from within DataHandler
4624
     */
4625
    protected function discardWorkspaceVersionsOfRecord($table, $uid): void
4626
    {
4627
        $versions = BackendUtility::selectVersionsOfRecord($table, $uid, '*', null);
4628
        if ($versions === null) {
4629
            // Null is returned by selectVersionsOfRecord() when table is not workspace aware.
4630
            return;
4631
        }
4632
        foreach ($versions as $record) {
4633
            if ($record['_CURRENT_VERSION'] ?? false) {
4634
                // The live record is included in the result from selectVersionsOfRecord()
4635
                // and marked as '_CURRENT_VERSION'. Skip this one.
4636
                continue;
4637
            }
4638
            // BE user must be put into this workspace temporarily so stuff like refindex updating
4639
            // is properly registered for this workspace when discarding records in there.
4640
            $currentUserWorkspace = $this->BE_USER->workspace;
4641
            $this->BE_USER->workspace = (int)$record['t3ver_wsid'];
4642
            $this->discard($table, null, $record);
4643
            // Switch user back to original workspace
4644
            $this->BE_USER->workspace = $currentUserWorkspace;
4645
        }
4646
    }
4647
4648
    /**
4649
     * Deleting a record
4650
     * This function may not be used to delete pages-records unless the underlying records are already deleted
4651
     * Deletes a record regardless of versioning state (live or offline, doesn't matter, the uid decides)
4652
     * If both $noRecordCheck and $forceHardDelete are set it could even delete a "deleted"-flagged record!
4653
     *
4654
     * @param string $table Table name
4655
     * @param int $uid Record UID
4656
     * @param bool $noRecordCheck Flag: If $noRecordCheck is set, then the function does not check permission to delete record
4657
     * @param bool $forceHardDelete If TRUE, the "deleted" flag is ignored if applicable for record and the record is deleted COMPLETELY!
4658
     * @internal should only be used from within DataHandler
4659
     */
4660
    public function deleteRecord($table, $uid, $noRecordCheck = false, $forceHardDelete = false)
4661
    {
4662
        $currentUserWorkspace = (int)$this->BE_USER->workspace;
4663
        $uid = (int)$uid;
4664
        if (!$GLOBALS['TCA'][$table] || !$uid) {
4665
            $this->log($table, $uid, SystemLogDatabaseAction::DELETE, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to delete record without delete-permissions. [' . $this->BE_USER->errorMsg . ']');
4666
            return;
4667
        }
4668
        // Skip processing already deleted records
4669
        if (!$forceHardDelete && $this->hasDeletedRecord($table, $uid)) {
4670
            return;
4671
        }
4672
4673
        // Checking if there is anything else disallowing deleting the record by checking if editing is allowed
4674
        $fullLanguageAccessCheck = true;
4675
        if ($table === 'pages') {
4676
            // If this is a page translation, the full language access check should not be done
4677
            $defaultLanguagePageId = $this->getDefaultLanguagePageId($uid);
4678
            if ($defaultLanguagePageId !== $uid) {
4679
                $fullLanguageAccessCheck = false;
4680
            }
4681
        }
4682
        $hasEditAccess = $this->BE_USER->recordEditAccessInternals($table, $uid, false, $forceHardDelete, $fullLanguageAccessCheck);
4683
        if (!$hasEditAccess) {
4684
            $this->log($table, $uid, SystemLogDatabaseAction::DELETE, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to delete record without delete-permissions');
4685
            return;
4686
        }
4687
        if ($table === 'pages') {
4688
            $perms = Permission::PAGE_DELETE;
4689
        } elseif ($table === 'sys_file_reference' && array_key_exists('pages', $this->datamap)) {
4690
            // @todo: find a more generic way to handle content relations of a page (without needing content editing access to that page)
4691
            $perms = Permission::PAGE_EDIT;
4692
        } else {
4693
            $perms = Permission::CONTENT_EDIT;
4694
        }
4695
        if (!$noRecordCheck && !$this->doesRecordExist($table, $uid, $perms)) {
4696
            return;
4697
        }
4698
4699
        $recordToDelete = [];
4700
        $recordWorkspaceId = 0;
4701
        if (BackendUtility::isTableWorkspaceEnabled($table)) {
4702
            $recordToDelete = BackendUtility::getRecord($table, $uid);
4703
            $recordWorkspaceId = (int)$recordToDelete['t3ver_wsid'];
4704
        }
4705
4706
        // Clear cache before deleting the record, else the correct page cannot be identified by clear_cache
4707
        [$parentUid] = BackendUtility::getTSCpid($table, $uid, '');
4708
        $this->registerRecordIdForPageCacheClearing($table, $uid, $parentUid);
4709
        $deleteField = $GLOBALS['TCA'][$table]['ctrl']['delete'];
4710
        $databaseErrorMessage = '';
4711
        if ($recordWorkspaceId > 0) {
4712
            // If this is a workspace record, use discard
4713
            $this->BE_USER->workspace = $recordWorkspaceId;
4714
            $this->discard($table, null, $recordToDelete);
4715
            // Switch user back to original workspace
4716
            $this->BE_USER->workspace = $currentUserWorkspace;
4717
        } elseif ($deleteField && !$forceHardDelete) {
4718
            $updateFields = [
4719
                $deleteField => 1
4720
            ];
4721
            if ($GLOBALS['TCA'][$table]['ctrl']['tstamp']) {
4722
                $updateFields[$GLOBALS['TCA'][$table]['ctrl']['tstamp']] = $GLOBALS['EXEC_TIME'];
4723
            }
4724
            // before deleting this record, check for child records or references
4725
            $this->deleteRecord_procFields($table, $uid);
4726
            try {
4727
                // Delete all l10n records as well
4728
                $this->deletedRecords[$table][] = (int)$uid;
4729
                $this->deleteL10nOverlayRecords($table, $uid);
4730
                GeneralUtility::makeInstance(ConnectionPool::class)
4731
                    ->getConnectionForTable($table)
4732
                    ->update($table, $updateFields, ['uid' => (int)$uid]);
4733
            } catch (DBALException $e) {
4734
                $databaseErrorMessage = $e->getPrevious()->getMessage();
4735
            }
4736
        } else {
4737
            // Delete the hard way...:
4738
            try {
4739
                $this->hardDeleteSingleRecord($table, (int)$uid);
4740
                $this->deletedRecords[$table][] = (int)$uid;
4741
                $this->deleteL10nOverlayRecords($table, $uid);
4742
            } catch (DBALException $e) {
4743
                $databaseErrorMessage = $e->getPrevious()->getMessage();
4744
            }
4745
        }
4746
        if ($this->enableLogging) {
4747
            $state = SystemLogDatabaseAction::DELETE;
4748
            if ($databaseErrorMessage === '') {
4749
                if ($forceHardDelete) {
4750
                    $message = 'Record \'%s\' (%s) was deleted unrecoverable from page \'%s\' (%s)';
4751
                } else {
4752
                    $message = 'Record \'%s\' (%s) was deleted from page \'%s\' (%s)';
4753
                }
4754
                $propArr = $this->getRecordProperties($table, $uid);
4755
                $pagePropArr = $this->getRecordProperties('pages', $propArr['pid']);
4756
4757
                $this->log($table, $uid, $state, 0, SystemLogErrorClassification::MESSAGE, $message, 0, [
4758
                    $propArr['header'],
4759
                    $table . ':' . $uid,
4760
                    $pagePropArr['header'],
4761
                    $propArr['pid']
4762
                ], $propArr['event_pid']);
4763
            } else {
4764
                $this->log($table, $uid, $state, 0, SystemLogErrorClassification::TODAYS_SPECIAL, $databaseErrorMessage);
4765
            }
4766
        }
4767
4768
        // Add history entry
4769
        $this->getRecordHistoryStore()->deleteRecord($table, $uid, $this->correlationId);
4770
4771
        // Update reference index with table/uid on left side (recuid)
4772
        $this->updateRefIndex($table, $uid);
4773
        // Update reference index with table/uid on right side (ref_uid). Important if children of a relation are deleted.
4774
        $this->referenceIndexUpdater->registerUpdateForReferencesToItem($table, $uid, $currentUserWorkspace);
4775
    }
4776
4777
    /**
4778
     * Used to delete page because it will check for branch below pages and disallowed tables on the page as well.
4779
     *
4780
     * @param int $uid Page id
4781
     * @param bool $force If TRUE, pages are not checked for permission.
4782
     * @param bool $forceHardDelete If TRUE, the "deleted" flag is ignored if applicable for record and the record is deleted COMPLETELY!
4783
     * @param bool $deleteRecordsOnPage If false, records on the page will not be deleted (edge case while swapping workspaces)
4784
     * @internal should only be used from within DataHandler
4785
     */
4786
    public function deletePages($uid, $force = false, $forceHardDelete = false, bool $deleteRecordsOnPage = true)
4787
    {
4788
        $uid = (int)$uid;
4789
        if ($uid === 0) {
4790
            if ($this->enableLogging) {
4791
                $this->log('pages', $uid, SystemLogGenericAction::UNDEFINED, 0, SystemLogErrorClassification::SYSTEM_ERROR, 'Deleting all pages starting from the root-page is disabled.', -1, [], 0);
4792
            }
4793
            return;
4794
        }
4795
        // Getting list of pages to delete:
4796
        if ($force) {
4797
            // Returns the branch WITHOUT permission checks (0 secures that), so it cannot return -1
4798
            $pageIdsInBranch = $this->doesBranchExist('', $uid, 0, true);
4799
            $res = GeneralUtility::intExplode(',', $pageIdsInBranch . $uid, true);
4800
        } else {
4801
            $res = $this->canDeletePage($uid);
4802
        }
4803
        // Perform deletion if not error:
4804
        if (is_array($res)) {
4805
            foreach ($res as $deleteId) {
4806
                $this->deleteSpecificPage($deleteId, $forceHardDelete, $deleteRecordsOnPage);
4807
            }
4808
        } else {
4809
            /** @var FlashMessage $flashMessage */
4810
            $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $res, '', FlashMessage::ERROR, true);
4811
            /** @var FlashMessageService $flashMessageService */
4812
            $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
4813
            $flashMessageService->getMessageQueueByIdentifier()->addMessage($flashMessage);
4814
            $this->newlog($res, SystemLogErrorClassification::USER_ERROR);
4815
        }
4816
    }
4817
4818
    /**
4819
     * Delete a page (or set deleted field to 1) and all records on it.
4820
     *
4821
     * @param int $uid Page id
4822
     * @param bool $forceHardDelete If TRUE, the "deleted" flag is ignored if applicable for record and the record is deleted COMPLETELY!
4823
     * @param bool $deleteRecordsOnPage If false, records on the page will not be deleted (edge case while swapping workspaces)
4824
     * @internal
4825
     * @see deletePages()
4826
     */
4827
    public function deleteSpecificPage($uid, $forceHardDelete = false, bool $deleteRecordsOnPage = true)
4828
    {
4829
        $uid = (int)$uid;
4830
        if (!$uid) {
4831
            // Early void return on invalid uid
4832
            return;
4833
        }
4834
        $forceHardDelete = (bool)$forceHardDelete;
4835
4836
        // Delete either a default language page or a translated page
4837
        $pageIdInDefaultLanguage = $this->getDefaultLanguagePageId($uid);
4838
        $isPageTranslation = false;
4839
        $pageLanguageId = 0;
4840
        if ($pageIdInDefaultLanguage !== $uid) {
4841
            // For translated pages, translated records in other tables (eg. tt_content) for the
4842
            // to-delete translated page have their pid field set to the uid of the default language record,
4843
            // NOT the uid of the translated page record.
4844
            // If a translated page is deleted, only translations of records in other tables of this language
4845
            // should be deleted. The code checks if the to-delete page is a translated page and
4846
            // adapts the query for other tables to use the uid of the default language page as pid together
4847
            // with the language id of the translated page.
4848
            $isPageTranslation = true;
4849
            $pageLanguageId = $this->pageInfo($uid, $GLOBALS['TCA']['pages']['ctrl']['languageField']);
4850
        }
4851
4852
        if ($deleteRecordsOnPage) {
4853
            $tableNames = $this->compileAdminTables();
4854
            foreach ($tableNames as $table) {
4855
                if ($table === 'pages' || ($isPageTranslation && !BackendUtility::isTableLocalizable($table))) {
4856
                    // Skip pages table. And skip table if not translatable, but a translated page is deleted
4857
                    continue;
4858
                }
4859
4860
                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
4861
                $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
4862
                $queryBuilder
4863
                    ->select('uid')
4864
                    ->from($table)
4865
                    // order by uid is needed here to process possible live records first - overlays always
4866
                    // have a higher uid. Otherwise dbms like postgres may return rows in arbitrary order,
4867
                    // leading to hard to debug issues. This is especially relevant for the
4868
                    // discardWorkspaceVersionsOfRecord() call below.
4869
                    ->addOrderBy('uid');
4870
4871
                if ($isPageTranslation) {
4872
                    // Only delete records in the specified language
4873
                    $queryBuilder->where(
4874
                        $queryBuilder->expr()->eq(
4875
                            'pid',
4876
                            $queryBuilder->createNamedParameter($pageIdInDefaultLanguage, \PDO::PARAM_INT)
4877
                        ),
4878
                        $queryBuilder->expr()->eq(
4879
                            $GLOBALS['TCA'][$table]['ctrl']['languageField'],
4880
                            $queryBuilder->createNamedParameter($pageLanguageId, \PDO::PARAM_INT)
4881
                        )
4882
                    );
4883
                } else {
4884
                    // Delete all records on this page
4885
                    $queryBuilder->where(
4886
                        $queryBuilder->expr()->eq(
4887
                            'pid',
4888
                            $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
4889
                        )
4890
                    );
4891
                }
4892
4893
                $currentUserWorkspace = (int)$this->BE_USER->workspace;
4894
                if ($currentUserWorkspace !== 0 && BackendUtility::isTableWorkspaceEnabled($table)) {
4895
                    // If we are in a workspace, make sure only records of this workspace are deleted.
4896
                    $queryBuilder->andWhere(
4897
                        $queryBuilder->expr()->eq(
4898
                            't3ver_wsid',
4899
                            $queryBuilder->createNamedParameter($currentUserWorkspace, \PDO::PARAM_INT)
4900
                        )
4901
                    );
4902
                }
4903
4904
                $statement = $queryBuilder->execute();
4905
4906
                while ($row = $statement->fetch()) {
4907
                    // Delete any further workspace overlays of the record in question, then delete the record.
4908
                    $this->discardWorkspaceVersionsOfRecord($table, $row['uid']);
4909
                    $this->deleteRecord($table, $row['uid'], true, $forceHardDelete);
4910
                }
4911
            }
4912
        }
4913
4914
        // Delete any further workspace overlays of the record in question, then delete the record.
4915
        $this->discardWorkspaceVersionsOfRecord('pages', $uid);
4916
        $this->deleteRecord('pages', $uid, true, $forceHardDelete);
4917
    }
4918
4919
    /**
4920
     * Used to evaluate if a page can be deleted
4921
     *
4922
     * @param int $uid Page id
4923
     * @return int[]|string If array: List of page uids to traverse and delete (means OK), if string: error message.
4924
     * @internal should only be used from within DataHandler
4925
     */
4926
    public function canDeletePage($uid)
4927
    {
4928
        $uid = (int)$uid;
4929
        $isTranslatedPage = null;
4930
4931
        // If we may at all delete this page
4932
        // If this is a page translation, do the check against the perms_* of the default page
4933
        // Because it is currently only deleting the translation
4934
        $defaultLanguagePageId = $this->getDefaultLanguagePageId($uid);
4935
        if ($defaultLanguagePageId !== $uid) {
4936
            if ($this->doesRecordExist('pages', (int)$defaultLanguagePageId, Permission::PAGE_DELETE)) {
4937
                $isTranslatedPage = true;
4938
            } else {
4939
                return 'Attempt to delete page without permissions';
4940
            }
4941
        } elseif (!$this->doesRecordExist('pages', $uid, Permission::PAGE_DELETE)) {
4942
            return 'Attempt to delete page without permissions';
4943
        }
4944
4945
        $pageIdsInBranch = $this->doesBranchExist('', $uid, Permission::PAGE_DELETE, true);
4946
4947
        if ($pageIdsInBranch === -1) {
4948
            return 'Attempt to delete pages in branch without permissions';
4949
        }
4950
4951
        $pagesInBranch = GeneralUtility::intExplode(',', $pageIdsInBranch . $uid, true);
4952
4953
        if ($disallowedTables = $this->checkForRecordsFromDisallowedTables($pagesInBranch)) {
4954
            return 'Attempt to delete records from disallowed tables (' . implode(', ', $disallowedTables) . ')';
4955
        }
4956
4957
        foreach ($pagesInBranch as $pageInBranch) {
4958
            if (!$this->BE_USER->recordEditAccessInternals('pages', $pageInBranch, false, false, $isTranslatedPage ? false : true)) {
4959
                return 'Attempt to delete page which has prohibited localizations.';
4960
            }
4961
        }
4962
        return $pagesInBranch;
4963
    }
4964
4965
    /**
4966
     * Returns TRUE if record CANNOT be deleted, otherwise FALSE. Used to check before the versioning API allows a record to be marked for deletion.
4967
     *
4968
     * @param string $table Record Table
4969
     * @param int $id Record UID
4970
     * @return string Returns a string IF there is an error (error string explaining). FALSE means record can be deleted
4971
     * @internal should only be used from within DataHandler
4972
     */
4973
    public function cannotDeleteRecord($table, $id)
4974
    {
4975
        if ($table === 'pages') {
4976
            $res = $this->canDeletePage($id);
4977
            return is_array($res) ? false : $res;
0 ignored issues
show
Bug Best Practice introduced by
The expression return is_array($res) ? false : $res could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
4978
        }
4979
        if ($table === 'sys_file_reference' && array_key_exists('pages', $this->datamap)) {
4980
            // @todo: find a more generic way to handle content relations of a page (without needing content editing access to that page)
4981
            $perms = Permission::PAGE_EDIT;
4982
        } else {
4983
            $perms = Permission::CONTENT_EDIT;
4984
        }
4985
        return $this->doesRecordExist($table, $id, $perms) ? false : 'No permission to delete record';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->doesRecord...ssion to delete record' could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
4986
    }
4987
4988
    /**
4989
     * Before a record is deleted, check if it has references such as inline type or MM references.
4990
     * If so, set these child records also to be deleted.
4991
     *
4992
     * @param string $table Record Table
4993
     * @param string $uid Record UID
4994
     * @see deleteRecord()
4995
     * @internal should only be used from within DataHandler
4996
     */
4997
    public function deleteRecord_procFields($table, $uid)
4998
    {
4999
        $conf = $GLOBALS['TCA'][$table]['columns'];
5000
        $row = BackendUtility::getRecord($table, $uid, '*', '', false);
0 ignored issues
show
Bug introduced by
$uid of type string is incompatible with the type integer expected by parameter $uid of TYPO3\CMS\Backend\Utilit...endUtility::getRecord(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

5000
        $row = BackendUtility::getRecord($table, /** @scrutinizer ignore-type */ $uid, '*', '', false);
Loading history...
5001
        if (empty($row)) {
5002
            return;
5003
        }
5004
        foreach ($row as $field => $value) {
5005
            $this->deleteRecord_procBasedOnFieldType($table, $uid, $field, $value, $conf[$field]['config']);
5006
        }
5007
    }
5008
5009
    /**
5010
     * Process fields of a record to be deleted and search for special handling, like
5011
     * inline type, MM records, etc.
5012
     *
5013
     * @param string $table Record Table
5014
     * @param string $uid Record UID
5015
     * @param string $field Record field
5016
     * @param string $value Record field value
5017
     * @param array $conf TCA configuration of current field
5018
     * @see deleteRecord()
5019
     * @internal should only be used from within DataHandler
5020
     */
5021
    public function deleteRecord_procBasedOnFieldType($table, $uid, $field, $value, $conf)
5022
    {
5023
        if ($conf['type'] === 'inline') {
5024
            $foreign_table = $conf['foreign_table'];
5025
            if ($foreign_table) {
5026
                $inlineType = $this->getInlineFieldType($conf);
5027
                if ($inlineType === 'list' || $inlineType === 'field') {
5028
                    /** @var RelationHandler $dbAnalysis */
5029
                    $dbAnalysis = $this->createRelationHandlerInstance();
5030
                    $dbAnalysis->start($value, $conf['foreign_table'], '', $uid, $table, $conf);
0 ignored issues
show
Bug introduced by
$uid of type string is incompatible with the type integer expected by parameter $MMuid of TYPO3\CMS\Core\Database\RelationHandler::start(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

5030
                    $dbAnalysis->start($value, $conf['foreign_table'], '', /** @scrutinizer ignore-type */ $uid, $table, $conf);
Loading history...
5031
                    $dbAnalysis->undeleteRecord = true;
5032
5033
                    $enableCascadingDelete = true;
5034
                    // non type save comparison is intended!
5035
                    if (isset($conf['behaviour']['enableCascadingDelete']) && $conf['behaviour']['enableCascadingDelete'] == false) {
5036
                        $enableCascadingDelete = false;
5037
                    }
5038
5039
                    // Walk through the items and remove them
5040
                    foreach ($dbAnalysis->itemArray as $v) {
5041
                        if ($enableCascadingDelete) {
5042
                            $this->deleteAction($v['table'], $v['id']);
5043
                        }
5044
                    }
5045
                }
5046
            }
5047
        } elseif ($this->isReferenceField($conf)) {
5048
            $allowedTables = $conf['type'] === 'group' ? $conf['allowed'] : $conf['foreign_table'];
5049
            $dbAnalysis = $this->createRelationHandlerInstance();
5050
            $dbAnalysis->start($value, $allowedTables, $conf['MM'], $uid, $table, $conf);
5051
            foreach ($dbAnalysis->itemArray as $v) {
5052
                $this->updateRefIndex($v['table'], $v['id']);
5053
            }
5054
        }
5055
    }
5056
5057
    /**
5058
     * Find l10n-overlay records and perform the requested delete action for these records.
5059
     *
5060
     * @param string $table Record Table
5061
     * @param string $uid Record UID
5062
     * @internal should only be used from within DataHandler
5063
     */
5064
    public function deleteL10nOverlayRecords($table, $uid)
5065
    {
5066
        // Check whether table can be localized
5067
        if (!BackendUtility::isTableLocalizable($table)) {
5068
            return;
5069
        }
5070
5071
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
5072
        $queryBuilder->getRestrictions()
5073
            ->removeAll()
5074
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
5075
            ->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, (int)$this->BE_USER->workspace));
5076
5077
        $queryBuilder->select('*')
5078
            ->from($table)
5079
            ->where(
5080
                $queryBuilder->expr()->eq(
5081
                    $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'],
5082
                    $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
5083
                )
5084
            );
5085
5086
        $result = $queryBuilder->execute();
5087
        while ($record = $result->fetch()) {
5088
            // Ignore workspace delete placeholders. Those records have been marked for
5089
            // deletion before - deleting them again in a workspace would revert that state.
5090
            if ((int)$this->BE_USER->workspace > 0 && BackendUtility::isTableWorkspaceEnabled($table)) {
5091
                BackendUtility::workspaceOL($table, $record, $this->BE_USER->workspace);
5092
                if (VersionState::cast($record['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)) {
5093
                    continue;
5094
                }
5095
            }
5096
            $this->deleteAction($table, (int)$record['t3ver_oid'] > 0 ? (int)$record['t3ver_oid'] : (int)$record['uid']);
5097
        }
5098
    }
5099
5100
    /*********************************************
5101
     *
5102
     * Cmd: undelete / restore
5103
     *
5104
     ********************************************/
5105
5106
    /**
5107
     * Restore live records by setting soft-delete flag to 0.
5108
     *
5109
     * Usually only used by ext:recycler.
5110
     * Connected relations (eg. inline) are restored, too.
5111
     * Additional existing localizations are not restored.
5112
     *
5113
     * @param string $table Record table name
5114
     * @param int $uid Record uid
5115
     */
5116
    protected function undeleteRecord(string $table, int $uid): void
5117
    {
5118
        $record = BackendUtility::getRecord($table, $uid, '*', '', false);
5119
        $deleteField = (string)($GLOBALS['TCA'][$table]['ctrl']['delete'] ?? '');
5120
        $timestampField = (string)($GLOBALS['TCA'][$table]['ctrl']['tstamp'] ?? '');
5121
5122
        if ($record === null
5123
            || $deleteField === ''
5124
            || !isset($record[$deleteField])
5125
            || (bool)$record[$deleteField] === false
5126
            || ($timestampField !== '' && !isset($record[$timestampField]))
5127
            || (int)$this->BE_USER->workspace > 0
5128
            || (BackendUtility::isTableWorkspaceEnabled($table) && (int)($record['t3ver_wsid'] ?? 0) > 0)
5129
        ) {
5130
            // Return early and silently, if:
5131
            // * Record not found
5132
            // * Table is not soft-delete aware
5133
            // * Record does not have deleted field - db analyzer not up-to-date?
5134
            // * Record is not deleted - may eventually happen via recursion with self referencing records?
5135
            // * Table is tstamp aware, but field does not exist - db analyzer not up-to-date?
5136
            // * User is in a workspace - does not make sense
5137
            // * Record is in a workspace - workspace records are not soft-delete aware
5138
            return;
5139
        }
5140
5141
        $recordPid = (int)($record['pid'] ?? 0);
5142
        if ($recordPid > 0) {
5143
            // Record is not on root level. Parent page record must exist and must not be deleted itself.
5144
            $page = BackendUtility::getRecord('pages', $recordPid, 'deleted', '', false);
5145
            if ($page === null || !isset($page['deleted']) || (bool)$page['deleted'] === true) {
5146
                $this->log(
5147
                    $table,
5148
                    $uid,
5149
                    SystemLogDatabaseAction::DELETE,
5150
                    0,
5151
                    SystemLogErrorClassification::USER_ERROR,
5152
                    sprintf('Record "%s:%s" can\'t be restored: The page:%s containing it does not exist or is soft-deleted.', $table, $uid, $recordPid),
5153
                    0,
5154
                    [],
5155
                    $recordPid
5156
                );
5157
                return;
5158
            }
5159
        }
5160
5161
        // @todo: When restoring a not-default language record, it should be verified the default language
5162
        // @todo: record is *not* set to deleted. Maybe even verify a possible l10n_source chain is not deleted?
5163
5164
        if (!$this->BE_USER->recordEditAccessInternals($table, $record, false, true)) {
5165
            // User misses access permissions to record
5166
            $this->log(
5167
                $table,
5168
                $uid,
5169
                SystemLogDatabaseAction::DELETE,
5170
                0,
5171
                SystemLogErrorClassification::USER_ERROR,
5172
                sprintf('Record "%s:%s" can\'t be restored: Insufficient user permissions.', $table, $uid),
5173
                0,
5174
                [],
5175
                $recordPid
5176
            );
5177
            return;
5178
        }
5179
5180
        // Restore referenced child records
5181
        $this->undeleteRecordRelations($table, $uid, $record);
5182
5183
        // Restore record
5184
        $updateFields[$deleteField] = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$updateFields was never initialized. Although not strictly required by PHP, it is generally a good practice to add $updateFields = array(); before regardless.
Loading history...
5185
        if ($timestampField !== '') {
5186
            $updateFields[$timestampField] = $GLOBALS['EXEC_TIME'];
5187
        }
5188
        GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table)
5189
            ->update(
5190
                $table,
5191
                $updateFields,
5192
                ['uid' => $uid]
5193
            );
5194
5195
        if ($this->enableLogging) {
5196
            $this->log(
5197
                $table,
5198
                $uid,
5199
                SystemLogDatabaseAction::INSERT,
5200
                0,
5201
                SystemLogErrorClassification::MESSAGE,
5202
                sprintf('Record "%s:%s" was restored on page:%s', $table, $uid, $recordPid),
5203
                0,
5204
                [],
5205
                $recordPid
5206
            );
5207
        }
5208
5209
        // Register cache clearing of page, or parent page if a page is restored.
5210
        $this->registerRecordIdForPageCacheClearing($table, $uid, $recordPid);
5211
        // Add history entry
5212
        $this->getRecordHistoryStore()->undeleteRecord($table, $uid, $this->correlationId);
5213
        // Update reference index with table/uid on left side (recuid)
5214
        $this->updateRefIndex($table, $uid);
5215
        // Update reference index with table/uid on right side (ref_uid). Important if children of a relation were restored.
5216
        $this->referenceIndexUpdater->registerUpdateForReferencesToItem($table, $uid, 0);
5217
    }
5218
5219
    /**
5220
     * Check if a to-restore record has inline references and restore them.
5221
     *
5222
     * @param string $table Record table name
5223
     * @param int $uid Record uid
5224
     * @param array $record Record row
5225
     * @todo: Add functional test undelete coverage to verify details, some details seem to be missing.
5226
     */
5227
    protected function undeleteRecordRelations(string $table, int $uid, array $record): void
5228
    {
5229
        foreach ($record as $fieldName => $value) {
5230
            $fieldConfig = $GLOBALS['TCA'][$table]['columns'][$fieldName]['config'] ?? [];
5231
            $fieldType = (string)($fieldConfig['type'] ?? '');
5232
            if (empty($fieldConfig) || !is_array($fieldConfig) || $fieldType === '') {
5233
                continue;
5234
            }
5235
            $foreignTable = (string)($fieldConfig['foreign_table'] ?? '');
5236
            if ($fieldType === 'inline') {
5237
                // @todo: Inline MM not handled here, and what about group / select?
5238
                if ($foreignTable === ''
5239
                    || !in_array($this->getInlineFieldType($fieldConfig), ['list', 'field'], true)
5240
                ) {
5241
                    continue;
5242
                }
5243
                $relationHandler = $this->createRelationHandlerInstance();
5244
                $relationHandler->start($value, $foreignTable, '', $uid, $table, $fieldConfig);
5245
                $relationHandler->undeleteRecord = true;
5246
                foreach ($relationHandler->itemArray as $reference) {
5247
                    $this->undeleteRecord($reference['table'], (int)$reference['id']);
5248
                }
5249
            } elseif ($this->isReferenceField($fieldConfig)) {
5250
                $allowedTables = $fieldType === 'group' ? ($fieldConfig['allowed'] ?? '') : $foreignTable;
5251
                $relationHandler = $this->createRelationHandlerInstance();
5252
                $relationHandler->start($value, $allowedTables, $fieldConfig['MM'] ?? '', $uid, $table, $fieldConfig);
5253
                foreach ($relationHandler->itemArray as $reference) {
5254
                    // @todo: Unsure if this is ok / enough. Needs coverage.
5255
                    $this->updateRefIndex($reference['table'], $reference['id']);
5256
                }
5257
            }
5258
        }
5259
    }
5260
5261
    /*********************************************
5262
     *
5263
     * Cmd: Workspace discard & flush
5264
     *
5265
     ********************************************/
5266
5267
    /**
5268
     * Discard a versioned record from this workspace. This deletes records from the database - no soft delete.
5269
     * This main entry method is called recursive for sub pages, localizations, relations and records on a page.
5270
     * The method checks user access and gathers facts about this record to hand the deletion over to detail methods.
5271
     *
5272
     * The incoming $uid or $row can be anything: The workspace of current user is respected and only records
5273
     * of current user workspace are discarded. If giving a live record uid, the versioned overly will be fetched.
5274
     *
5275
     * @param string $table Database table name
5276
     * @param int|null $uid Uid of live or versioned record to be discarded, or null if $record is given
5277
     * @param array|null $record Record row that should be discarded. Used instead of $uid within recursion.
5278
     * @internal should only be used from within DataHandler
5279
     */
5280
    public function discard(string $table, ?int $uid, array $record = null): void
5281
    {
5282
        if ($uid === null && $record === null) {
5283
            throw new \RuntimeException('Either record $uid or $record row must be given', 1600373491);
5284
        }
5285
5286
        // Fetch record we are dealing with if not given
5287
        if ($record === null) {
5288
            $record = BackendUtility::getRecord($table, $uid);
5289
        }
5290
        if (!is_array($record)) {
5291
            return;
5292
        }
5293
        $uid = (int)$record['uid'];
5294
5295
        // Call hook and return if hook took care of the element
5296
        $recordWasDiscarded = false;
5297
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'] ?? [] as $className) {
5298
            $hookObj = GeneralUtility::makeInstance($className);
5299
            if (method_exists($hookObj, 'processCmdmap_discardAction')) {
5300
                $hookObj->processCmdmap_discardAction($table, $uid, $record, $recordWasDiscarded);
5301
            }
5302
        }
5303
5304
        $userWorkspace = (int)$this->BE_USER->workspace;
5305
        if ($recordWasDiscarded
5306
            || $userWorkspace === 0
5307
            || !BackendUtility::isTableWorkspaceEnabled($table)
5308
            || $this->hasDeletedRecord($table, $uid)
5309
        ) {
5310
            return;
5311
        }
5312
5313
        // Gather versioned record
5314
        $versionRecord = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $versionRecord is dead and can be removed.
Loading history...
5315
        if ((int)$record['t3ver_wsid'] === 0) {
5316
            $record = BackendUtility::getWorkspaceVersionOfRecord($userWorkspace, $table, $uid);
5317
        }
5318
        if (!is_array($record)) {
5319
            return;
5320
        }
5321
        $versionRecord = $record;
5322
5323
        // User access checks
5324
        if ($userWorkspace !== (int)$versionRecord['t3ver_wsid']) {
5325
            $this->newlog('Attempt to discard workspace record ' . $table . ':' . $versionRecord['uid'] . ' failed: Different workspace', SystemLogErrorClassification::USER_ERROR);
5326
            return;
5327
        }
5328
        if ($errorCode = $this->BE_USER->workspaceCannotEditOfflineVersion($table, $versionRecord['uid'])) {
5329
            $this->newlog('Attempt to discard workspace record ' . $table . ':' . $versionRecord['uid'] . ' failed: ' . $errorCode, SystemLogErrorClassification::USER_ERROR);
5330
            return;
5331
        }
5332
        if (!$this->checkRecordUpdateAccess($table, $versionRecord['uid'])) {
5333
            $this->newlog('Attempt to discard workspace record ' . $table . ':' . $versionRecord['uid'] . ' failed: User has no edit access', SystemLogErrorClassification::USER_ERROR);
5334
            return;
5335
        }
5336
        $fullLanguageAccessCheck = !($table === 'pages' && (int)$versionRecord[$GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField']] !== 0);
5337
        if (!$this->BE_USER->recordEditAccessInternals($table, $versionRecord, false, true, $fullLanguageAccessCheck)) {
5338
            $this->newlog('Attempt to discard workspace record ' . $table . ':' . $versionRecord['uid'] . ' failed: User has no delete access', SystemLogErrorClassification::USER_ERROR);
5339
            return;
5340
        }
5341
5342
        // Perform discard operations
5343
        $versionState = VersionState::cast($versionRecord['t3ver_state']);
5344
        if ($table === 'pages' && $versionState->equals(VersionState::NEW_PLACEHOLDER)) {
5345
            // When discarding a new page, there can be new sub pages and new records.
5346
            // Those need to be discarded, otherwise they'd end up as records without parent page.
5347
            $this->discardSubPagesAndRecordsOnPage($versionRecord);
5348
        }
5349
5350
        $this->discardLocalizationOverlayRecords($table, $versionRecord);
5351
        $this->discardRecordRelations($table, $versionRecord);
5352
        $this->hardDeleteSingleRecord($table, (int)$versionRecord['uid']);
5353
        $this->deletedRecords[$table][] = (int)$versionRecord['uid'];
5354
        $this->registerReferenceIndexRowsForDrop($table, (int)$versionRecord['uid'], $userWorkspace);
5355
        $this->getRecordHistoryStore()->deleteRecord($table, (int)$versionRecord['uid'], $this->correlationId);
5356
        $this->log(
5357
            $table,
5358
            (int)$versionRecord['uid'],
5359
            SystemLogDatabaseAction::DELETE,
5360
            0,
5361
            SystemLogErrorClassification::MESSAGE,
5362
            'Record ' . $table . ':' . $versionRecord['uid'] . ' was deleted unrecoverable from page ' . $versionRecord['pid'],
5363
            0,
5364
            [],
5365
            (int)$versionRecord['pid']
5366
        );
5367
    }
5368
5369
    /**
5370
     * Also discard any sub pages and records of a new parent page if this page is discarded.
5371
     * Discarding only in specific localization, if needed.
5372
     *
5373
     * @param array $page Page record row
5374
     */
5375
    protected function discardSubPagesAndRecordsOnPage(array $page): void
5376
    {
5377
        $isLocalizedPage = false;
5378
        $sysLanguageId = (int)$page[$GLOBALS['TCA']['pages']['ctrl']['languageField']];
5379
        $versionState = VersionState::cast($page['t3ver_state']);
5380
        if ($sysLanguageId > 0) {
5381
            // New or moved localized page.
5382
            // Discard records on this page localization, but no sub pages.
5383
            // Records of a translated page have the pid set to the default language page uid. Found in l10n_parent.
5384
            // @todo: Discard other page translations that inherit from this?! (l10n_source field)
5385
            $isLocalizedPage = true;
5386
            $pid = (int)$page[$GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField']];
5387
        } elseif ($versionState->equals(VersionState::NEW_PLACEHOLDER)) {
5388
            // New default language page.
5389
            // Discard any sub pages and all other records of this page, including any page localizations.
5390
            // The t3ver_state=1 record is incoming here. Records on this page have their pid field set to the uid
5391
            // of this record. So, since t3ver_state=1 does not have an online counter-part, the actual UID is used here.
5392
            $pid = (int)$page['uid'];
5393
        } else {
5394
            // Moved default language page.
5395
            // Discard any sub pages and all other records of this page, including any page localizations.
5396
            $pid = (int)$page['t3ver_oid'];
5397
        }
5398
        $tables = $this->compileAdminTables();
5399
        foreach ($tables as $table) {
5400
            if (($isLocalizedPage && $table === 'pages')
5401
                || ($isLocalizedPage && !BackendUtility::isTableLocalizable($table))
5402
                || !BackendUtility::isTableWorkspaceEnabled($table)
5403
            ) {
5404
                continue;
5405
            }
5406
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
5407
            $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
5408
            $queryBuilder->select('*')
5409
                ->from($table)
5410
                ->where(
5411
                    $queryBuilder->expr()->eq(
5412
                        'pid',
5413
                        $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)
5414
                    ),
5415
                    $queryBuilder->expr()->eq(
5416
                        't3ver_wsid',
5417
                        $queryBuilder->createNamedParameter((int)$this->BE_USER->workspace, \PDO::PARAM_INT)
5418
                    )
5419
                );
5420
            if ($isLocalizedPage) {
5421
                // Add sys_language_uid = x restriction if discarding a localized page
5422
                $queryBuilder->andWhere(
5423
                    $queryBuilder->expr()->eq(
5424
                        $GLOBALS['TCA'][$table]['ctrl']['languageField'],
5425
                        $queryBuilder->createNamedParameter($sysLanguageId, \PDO::PARAM_INT)
5426
                    )
5427
                );
5428
            }
5429
            $statement = $queryBuilder->execute();
5430
            while ($row = $statement->fetch()) {
5431
                $this->discard($table, null, $row);
5432
            }
5433
        }
5434
    }
5435
5436
    /**
5437
     * Discard record relations like inline and MM of a record.
5438
     *
5439
     * @param string $table Table name of this record
5440
     * @param array $record The record row to handle
5441
     */
5442
    protected function discardRecordRelations(string $table, array $record): void
5443
    {
5444
        foreach ($record as $field => $value) {
5445
            $fieldConfig = $GLOBALS['TCA'][$table]['columns'][$field]['config'] ?? null;
5446
            if (!isset($fieldConfig['type'])) {
5447
                continue;
5448
            }
5449
            if ($fieldConfig['type'] === 'inline') {
5450
                $foreignTable = $fieldConfig['foreign_table'] ?? null;
5451
                if (!$foreignTable
5452
                     || (isset($fieldConfig['behaviour']['enableCascadingDelete'])
5453
                        && (bool)$fieldConfig['behaviour']['enableCascadingDelete'] === false)
5454
                ) {
5455
                    continue;
5456
                }
5457
                $inlineType = $this->getInlineFieldType($fieldConfig);
5458
                if ($inlineType === 'list' || $inlineType === 'field') {
5459
                    $dbAnalysis = $this->createRelationHandlerInstance();
5460
                    $dbAnalysis->start($value, $fieldConfig['foreign_table'], '', (int)$record['uid'], $table, $fieldConfig);
5461
                    $dbAnalysis->undeleteRecord = true;
5462
                    foreach ($dbAnalysis->itemArray as $relationRecord) {
5463
                        $this->discard($relationRecord['table'], (int)$relationRecord['id']);
5464
                    }
5465
                }
5466
            } elseif ($this->isReferenceField($fieldConfig) && !empty($fieldConfig['MM'])) {
5467
                $this->discardMmRelations($fieldConfig, $record);
5468
            }
5469
            // @todo not inline and not mm - probably not handled correctly and has no proper test coverage yet
5470
        }
5471
    }
5472
5473
    /**
5474
     * When a workspace record row is discarded that has mm relations, existing mm table rows need
5475
     * to be deleted. The method performs the delete operation depending on TCA field configuration.
5476
     *
5477
     * @param array $fieldConfig TCA configuration of this field
5478
     * @param array $record The full record of a left- or ride-side relation
5479
     */
5480
    protected function discardMmRelations(array $fieldConfig, array $record): void
5481
    {
5482
        $recordUid = (int)$record['uid'];
5483
        $mmTableName = $fieldConfig['MM'];
5484
        // left - non foreign - uid_local vs. right - foreign - uid_foreign decision
5485
        $relationUidFieldName = isset($fieldConfig['MM_opposite_field']) ? 'uid_foreign' : 'uid_local';
5486
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($mmTableName);
5487
        $queryBuilder->delete($mmTableName)->where(
5488
            // uid_local = given uid OR uid_foreign = given uid
5489
            $queryBuilder->expr()->eq($relationUidFieldName, $queryBuilder->createNamedParameter($recordUid, \PDO::PARAM_INT))
5490
        );
5491
        if (!empty($fieldConfig['MM_table_where']) && is_string($fieldConfig['MM_table_where'])) {
5492
            $queryBuilder->andWhere(
5493
                QueryHelper::stripLogicalOperatorPrefix(str_replace('###THIS_UID###', (string)$recordUid, $fieldConfig['MM_table_where']))
5494
            );
5495
        }
5496
        $mmMatchFields = $fieldConfig['MM_match_fields'] ?? [];
5497
        foreach ($mmMatchFields as $fieldName => $fieldValue) {
5498
            $queryBuilder->andWhere(
5499
                $queryBuilder->expr()->eq($fieldName, $queryBuilder->createNamedParameter($fieldValue, \PDO::PARAM_STR))
5500
            );
5501
        }
5502
        $queryBuilder->execute();
5503
    }
5504
5505
    /**
5506
     * Find localization overlays of a record and discard them.
5507
     *
5508
     * @param string $table Table of this record
5509
     * @param array $record Record row
5510
     */
5511
    protected function discardLocalizationOverlayRecords(string $table, array $record): void
5512
    {
5513
        if (!BackendUtility::isTableLocalizable($table)) {
5514
            return;
5515
        }
5516
        $uid = (int)$record['uid'];
5517
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
5518
        $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
5519
        $statement = $queryBuilder->select('*')
5520
            ->from($table)
5521
            ->where(
5522
                $queryBuilder->expr()->eq(
5523
                    $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'],
5524
                    $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
5525
                ),
5526
                $queryBuilder->expr()->eq(
5527
                    't3ver_wsid',
5528
                    $queryBuilder->createNamedParameter((int)$this->BE_USER->workspace, \PDO::PARAM_INT)
5529
                )
5530
            )
5531
            ->execute();
5532
        while ($record = $statement->fetch()) {
5533
            $this->discard($table, null, $record);
5534
        }
5535
    }
5536
5537
    /*********************************************
5538
     *
5539
     * Cmd: Versioning
5540
     *
5541
     ********************************************/
5542
    /**
5543
     * Creates a new version of a record
5544
     * (Requires support in the table)
5545
     *
5546
     * @param string $table Table name
5547
     * @param int $id Record uid to versionize
5548
     * @param string $label Version label
5549
     * @param bool $delete If TRUE, the version is created to delete the record.
5550
     * @return int|null Returns the id of the new version (if any)
5551
     * @see copyRecord()
5552
     * @internal should only be used from within DataHandler
5553
     */
5554
    public function versionizeRecord($table, $id, $label, $delete = false)
5555
    {
5556
        $id = (int)$id;
5557
        // Stop any actions if the record is marked to be deleted:
5558
        // (this can occur if IRRE elements are versionized and child elements are removed)
5559
        if ($this->isElementToBeDeleted($table, $id)) {
5560
            return null;
5561
        }
5562
        if (!BackendUtility::isTableWorkspaceEnabled($table) || $id <= 0) {
5563
            $this->newlog('Versioning is not supported for this table "' . $table . '" / ' . $id, SystemLogErrorClassification::USER_ERROR);
5564
            return null;
5565
        }
5566
5567
        // Fetch record with permission check
5568
        $row = $this->recordInfoWithPermissionCheck($table, $id, Permission::PAGE_SHOW);
5569
5570
        // This checks if the record can be selected which is all that a copy action requires.
5571
        if ($row === false) {
5572
            $this->newlog(
5573
                'The record does not exist or you don\'t have correct permissions to make a new version (copy) of this record "' . $table . ':' . $id . '"',
5574
                SystemLogErrorClassification::USER_ERROR
5575
            );
5576
            return null;
5577
        }
5578
5579
        // Record must be online record, otherwise we would create a version of a version
5580
        if (($row['t3ver_oid'] ?? 0) > 0) {
5581
            $this->newlog('Record "' . $table . ':' . $id . '" you wanted to versionize was already a version in archive (record has an online ID)!', SystemLogErrorClassification::USER_ERROR);
5582
            return null;
5583
        }
5584
5585
        if ($delete && $this->cannotDeleteRecord($table, $id)) {
5586
            $this->newlog('Record cannot be deleted: ' . $this->cannotDeleteRecord($table, $id), SystemLogErrorClassification::USER_ERROR);
5587
            return null;
5588
        }
5589
5590
        // Set up the values to override when making a raw-copy:
5591
        $overrideArray = [
5592
            't3ver_oid' => $id,
5593
            't3ver_wsid' => $this->BE_USER->workspace,
5594
            't3ver_state' => (string)($delete ? new VersionState(VersionState::DELETE_PLACEHOLDER) : new VersionState(VersionState::DEFAULT_STATE)),
5595
            't3ver_stage' => 0,
5596
        ];
5597
        if ($GLOBALS['TCA'][$table]['ctrl']['editlock']) {
5598
            $overrideArray[$GLOBALS['TCA'][$table]['ctrl']['editlock']] = 0;
5599
        }
5600
        // Checking if the record already has a version in the current workspace of the backend user
5601
        $versionRecord = ['uid' => null];
5602
        if ($this->BE_USER->workspace !== 0) {
5603
            // Look for version already in workspace:
5604
            $versionRecord = BackendUtility::getWorkspaceVersionOfRecord($this->BE_USER->workspace, $table, $id, 'uid');
5605
        }
5606
        // Create new version of the record and return the new uid
5607
        if (empty($versionRecord['uid'])) {
5608
            // Create raw-copy and return result:
5609
            // The information of the label to be used for the workspace record
5610
            // as well as the information whether the record shall be removed
5611
            // must be forwarded (creating delete placeholders on a workspace are
5612
            // done by copying the record and override several fields).
5613
            $workspaceOptions = [
5614
                'delete' => $delete,
5615
                'label' => $label,
5616
            ];
5617
            return $this->copyRecord_raw($table, $id, (int)$row['pid'], $overrideArray, $workspaceOptions);
5618
        }
5619
        // Reuse the existing record and return its uid
5620
        // (prior to TYPO3 CMS 6.2, an error was thrown here, which
5621
        // did not make much sense since the information is available)
5622
        return $versionRecord['uid'];
5623
    }
5624
5625
    /**
5626
     * Swaps MM-relations for current/swap record, see version_swap()
5627
     *
5628
     * @param string $table Table for the two input records
5629
     * @param int $id Current record (about to go offline)
5630
     * @param int $swapWith Swap record (about to go online)
5631
     * @see version_swap()
5632
     * @internal should only be used from within DataHandler
5633
     */
5634
    public function version_remapMMForVersionSwap($table, $id, $swapWith)
5635
    {
5636
        // Actually, selecting the records fully is only need if flexforms are found inside... This could be optimized ...
5637
        $currentRec = BackendUtility::getRecord($table, $id);
5638
        $swapRec = BackendUtility::getRecord($table, $swapWith);
5639
        $this->version_remapMMForVersionSwap_reg = [];
5640
        $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
5641
        foreach ($GLOBALS['TCA'][$table]['columns'] as $field => $fConf) {
5642
            $conf = $fConf['config'];
5643
            if ($this->isReferenceField($conf)) {
5644
                $allowedTables = $conf['type'] === 'group' ? $conf['allowed'] : $conf['foreign_table'];
5645
                $prependName = $conf['type'] === 'group' ? $conf['prepend_tname'] : '';
5646
                if ($conf['MM']) {
5647
                    /** @var RelationHandler $dbAnalysis */
5648
                    $dbAnalysis = $this->createRelationHandlerInstance();
5649
                    $dbAnalysis->start('', $allowedTables, $conf['MM'], $id, $table, $conf);
5650
                    if (!empty($dbAnalysis->getValueArray($prependName))) {
0 ignored issues
show
Bug introduced by
It seems like $prependName can also be of type string; however, parameter $prependTableName of TYPO3\CMS\Core\Database\...andler::getValueArray() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

5650
                    if (!empty($dbAnalysis->getValueArray(/** @scrutinizer ignore-type */ $prependName))) {
Loading history...
5651
                        $this->version_remapMMForVersionSwap_reg[$id][$field] = [$dbAnalysis, $conf['MM'], $prependName];
5652
                    }
5653
                    /** @var RelationHandler $dbAnalysis */
5654
                    $dbAnalysis = $this->createRelationHandlerInstance();
5655
                    $dbAnalysis->start('', $allowedTables, $conf['MM'], $swapWith, $table, $conf);
5656
                    if (!empty($dbAnalysis->getValueArray($prependName))) {
5657
                        $this->version_remapMMForVersionSwap_reg[$swapWith][$field] = [$dbAnalysis, $conf['MM'], $prependName];
5658
                    }
5659
                }
5660
            } elseif ($conf['type'] === 'flex') {
5661
                // Current record
5662
                $dataStructureIdentifier = $flexFormTools->getDataStructureIdentifier(
5663
                    $fConf,
5664
                    $table,
5665
                    $field,
5666
                    $currentRec
5667
                );
5668
                $dataStructureArray = $flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
5669
                $currentValueArray = GeneralUtility::xml2array($currentRec[$field]);
5670
                if (is_array($currentValueArray)) {
5671
                    $this->checkValue_flex_procInData($currentValueArray['data'], [], [], $dataStructureArray, [$table, $id, $field], 'version_remapMMForVersionSwap_flexFormCallBack');
5672
                }
5673
                // Swap record
5674
                $dataStructureIdentifier = $flexFormTools->getDataStructureIdentifier(
5675
                    $fConf,
5676
                    $table,
5677
                    $field,
5678
                    $swapRec
5679
                );
5680
                $dataStructureArray = $flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
5681
                $currentValueArray = GeneralUtility::xml2array($swapRec[$field]);
5682
                if (is_array($currentValueArray)) {
5683
                    $this->checkValue_flex_procInData($currentValueArray['data'], [], [], $dataStructureArray, [$table, $swapWith, $field], 'version_remapMMForVersionSwap_flexFormCallBack');
5684
                }
5685
            }
5686
        }
5687
        // Execute:
5688
        $this->version_remapMMForVersionSwap_execSwap($table, $id, $swapWith);
5689
    }
5690
5691
    /**
5692
     * Callback function for traversing the FlexForm structure in relation to ...
5693
     *
5694
     * @param array $pParams Array of parameters in num-indexes: table, uid, field
5695
     * @param array $dsConf TCA field configuration (from Data Structure XML)
5696
     * @param string $dataValue The value of the flexForm field
5697
     * @param string $dataValue_ext1 Not used.
5698
     * @param string $dataValue_ext2 Not used.
5699
     * @param string $path Path in flexforms
5700
     * @see version_remapMMForVersionSwap()
5701
     * @see checkValue_flex_procInData_travDS()
5702
     * @internal should only be used from within DataHandler
5703
     */
5704
    public function version_remapMMForVersionSwap_flexFormCallBack($pParams, $dsConf, $dataValue, $dataValue_ext1, $dataValue_ext2, $path)
5705
    {
5706
        // Extract parameters:
5707
        [$table, $uid, $field] = $pParams;
5708
        if ($this->isReferenceField($dsConf)) {
5709
            $allowedTables = $dsConf['type'] === 'group' ? $dsConf['allowed'] : $dsConf['foreign_table'];
5710
            $prependName = $dsConf['type'] === 'group' ? $dsConf['prepend_tname'] : '';
5711
            if ($dsConf['MM']) {
5712
                /** @var RelationHandler $dbAnalysis */
5713
                $dbAnalysis = $this->createRelationHandlerInstance();
5714
                $dbAnalysis->start('', $allowedTables, $dsConf['MM'], $uid, $table, $dsConf);
5715
                $this->version_remapMMForVersionSwap_reg[$uid][$field . '/' . $path] = [$dbAnalysis, $dsConf['MM'], $prependName];
5716
            }
5717
        }
5718
    }
5719
5720
    /**
5721
     * Performing the remapping operations found necessary in version_remapMMForVersionSwap()
5722
     * It must be done in three steps with an intermediate "fake" uid. The UID can be something else than -$id (fx. 9999999+$id if you dare... :-)- as long as it is unique.
5723
     *
5724
     * @param string $table Table for the two input records
5725
     * @param int $id Current record (about to go offline)
5726
     * @param int $swapWith Swap record (about to go online)
5727
     * @see version_remapMMForVersionSwap()
5728
     * @internal should only be used from within DataHandler
5729
     */
5730
    public function version_remapMMForVersionSwap_execSwap($table, $id, $swapWith)
5731
    {
5732
        if (is_array($this->version_remapMMForVersionSwap_reg[$id])) {
5733
            foreach ($this->version_remapMMForVersionSwap_reg[$id] as $field => $str) {
5734
                $str[0]->remapMM($str[1], $id, -$id, $str[2]);
5735
            }
5736
        }
5737
        if (is_array($this->version_remapMMForVersionSwap_reg[$swapWith])) {
5738
            foreach ($this->version_remapMMForVersionSwap_reg[$swapWith] as $field => $str) {
5739
                $str[0]->remapMM($str[1], $swapWith, $id, $str[2]);
5740
            }
5741
        }
5742
        if (is_array($this->version_remapMMForVersionSwap_reg[$id])) {
5743
            foreach ($this->version_remapMMForVersionSwap_reg[$id] as $field => $str) {
5744
                $str[0]->remapMM($str[1], -$id, $swapWith, $str[2]);
5745
            }
5746
        }
5747
    }
5748
5749
    /*********************************************
5750
     *
5751
     * Cmd: Helper functions
5752
     *
5753
     ********************************************/
5754
5755
    /**
5756
     * Returns an instance of DataHandler for handling local datamaps/cmdmaps
5757
     *
5758
     * @return DataHandler
5759
     */
5760
    protected function getLocalTCE()
5761
    {
5762
        $copyTCE = GeneralUtility::makeInstance(DataHandler::class, $this->referenceIndexUpdater);
5763
        $copyTCE->copyTree = $this->copyTree;
5764
        $copyTCE->enableLogging = $this->enableLogging;
5765
        // Transformations should NOT be carried out during copy
5766
        $copyTCE->dontProcessTransformations = true;
5767
        // make sure the isImporting flag is transferred, so all hooks know if
5768
        // the current process is an import process
5769
        $copyTCE->isImporting = $this->isImporting;
5770
        $copyTCE->bypassAccessCheckForRecords = $this->bypassAccessCheckForRecords;
5771
        $copyTCE->bypassWorkspaceRestrictions = $this->bypassWorkspaceRestrictions;
5772
        return $copyTCE;
5773
    }
5774
5775
    /**
5776
     * Processes the fields with references as registered during the copy process. This includes all FlexForm fields which had references.
5777
     * @internal should only be used from within DataHandler
5778
     */
5779
    public function remapListedDBRecords()
5780
    {
5781
        if (!empty($this->registerDBList)) {
5782
            $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
5783
            foreach ($this->registerDBList as $table => $records) {
5784
                foreach ($records as $uid => $fields) {
5785
                    $newData = [];
5786
                    $theUidToUpdate = $this->copyMappingArray_merged[$table][$uid];
5787
                    $theUidToUpdate_saveTo = BackendUtility::wsMapId($table, $theUidToUpdate);
5788
                    foreach ($fields as $fieldName => $value) {
5789
                        $conf = $GLOBALS['TCA'][$table]['columns'][$fieldName]['config'];
5790
                        switch ($conf['type']) {
5791
                            case 'group':
5792
                            case 'select':
5793
                                $vArray = $this->remapListedDBRecords_procDBRefs($conf, $value, $theUidToUpdate, $table);
5794
                                if (is_array($vArray)) {
5795
                                    $newData[$fieldName] = implode(',', $vArray);
5796
                                }
5797
                                break;
5798
                            case 'flex':
5799
                                if ($value === 'FlexForm_reference') {
5800
                                    // This will fetch the new row for the element
5801
                                    $origRecordRow = $this->recordInfo($table, $theUidToUpdate, '*');
5802
                                    if (is_array($origRecordRow)) {
5803
                                        BackendUtility::workspaceOL($table, $origRecordRow);
5804
                                        // Get current data structure and value array:
5805
                                        $dataStructureIdentifier = $flexFormTools->getDataStructureIdentifier(
5806
                                            ['config' => $conf],
5807
                                            $table,
5808
                                            $fieldName,
5809
                                            $origRecordRow
5810
                                        );
5811
                                        $dataStructureArray = $flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
5812
                                        $currentValueArray = GeneralUtility::xml2array($origRecordRow[$fieldName]);
5813
                                        // Do recursive processing of the XML data:
5814
                                        $currentValueArray['data'] = $this->checkValue_flex_procInData($currentValueArray['data'], [], [], $dataStructureArray, [$table, $theUidToUpdate, $fieldName], 'remapListedDBRecords_flexFormCallBack');
5815
                                        // The return value should be compiled back into XML, ready to insert directly in the field (as we call updateDB() directly later):
5816
                                        if (is_array($currentValueArray['data'])) {
5817
                                            $newData[$fieldName] = $this->checkValue_flexArray2Xml($currentValueArray, true);
5818
                                        }
5819
                                    }
5820
                                }
5821
                                break;
5822
                            case 'inline':
5823
                                $this->remapListedDBRecords_procInline($conf, $value, $uid, $table);
5824
                                break;
5825
                            default:
5826
                                $this->logger->debug('Field type should not appear here: ' . $conf['type']);
5827
                        }
5828
                    }
5829
                    // If any fields were changed, those fields are updated!
5830
                    if (!empty($newData)) {
5831
                        $this->updateDB($table, $theUidToUpdate_saveTo, $newData);
5832
                    }
5833
                }
5834
            }
5835
        }
5836
    }
5837
5838
    /**
5839
     * Callback function for traversing the FlexForm structure in relation to creating copied files of file relations inside of flex form structures.
5840
     *
5841
     * @param array $pParams Set of parameters in numeric array: table, uid, field
5842
     * @param array $dsConf TCA config for field (from Data Structure of course)
5843
     * @param string $dataValue Field value (from FlexForm XML)
5844
     * @param string $dataValue_ext1 Not used
5845
     * @param string $dataValue_ext2 Not used
5846
     * @return array Array where the "value" key carries the value.
5847
     * @see checkValue_flex_procInData_travDS()
5848
     * @see remapListedDBRecords()
5849
     * @internal should only be used from within DataHandler
5850
     */
5851
    public function remapListedDBRecords_flexFormCallBack($pParams, $dsConf, $dataValue, $dataValue_ext1, $dataValue_ext2)
5852
    {
5853
        // Extract parameters:
5854
        [$table, $uid, $field] = $pParams;
5855
        // If references are set for this field, set flag so they can be corrected later:
5856
        if ($this->isReferenceField($dsConf) && (string)$dataValue !== '') {
5857
            $vArray = $this->remapListedDBRecords_procDBRefs($dsConf, $dataValue, $uid, $table);
5858
            if (is_array($vArray)) {
5859
                $dataValue = implode(',', $vArray);
5860
            }
5861
        }
5862
        // Return
5863
        return ['value' => $dataValue];
5864
    }
5865
5866
    /**
5867
     * Performs remapping of old UID values to NEW uid values for a DB reference field.
5868
     *
5869
     * @param array $conf TCA field config
5870
     * @param string $value Field value
5871
     * @param int $MM_localUid UID of local record (for MM relations - might need to change if support for FlexForms should be done!)
5872
     * @param string $table Table name
5873
     * @return array|null Returns array of items ready to implode for field content.
5874
     * @see remapListedDBRecords()
5875
     * @internal should only be used from within DataHandler
5876
     */
5877
    public function remapListedDBRecords_procDBRefs($conf, $value, $MM_localUid, $table)
5878
    {
5879
        // Initialize variables
5880
        // Will be set TRUE if an upgrade should be done...
5881
        $set = false;
5882
        // Allowed tables for references.
5883
        $allowedTables = $conf['type'] === 'group' ? $conf['allowed'] : $conf['foreign_table'];
5884
        // Table name to prepend the UID
5885
        $prependName = $conf['type'] === 'group' ? $conf['prepend_tname'] : '';
5886
        // Which tables that should possibly not be remapped
5887
        $dontRemapTables = GeneralUtility::trimExplode(',', $conf['dontRemapTablesOnCopy'], true);
5888
        // Convert value to list of references:
5889
        $dbAnalysis = $this->createRelationHandlerInstance();
5890
        $dbAnalysis->registerNonTableValues = $conf['type'] === 'select' && $conf['allowNonIdValues'];
5891
        $dbAnalysis->start($value, $allowedTables, $conf['MM'], $MM_localUid, $table, $conf);
5892
        // Traverse those references and map IDs:
5893
        foreach ($dbAnalysis->itemArray as $k => $v) {
5894
            $mapID = $this->copyMappingArray_merged[$v['table']][$v['id']];
5895
            if ($mapID && !in_array($v['table'], $dontRemapTables, true)) {
5896
                $dbAnalysis->itemArray[$k]['id'] = $mapID;
5897
                $set = true;
5898
            }
5899
        }
5900
        if (!empty($conf['MM'])) {
5901
            // Purge invalid items (live/version)
5902
            $dbAnalysis->purgeItemArray();
5903
            if ($dbAnalysis->isPurged()) {
5904
                $set = true;
5905
            }
5906
5907
            // If record has been versioned/copied in this process, handle invalid relations of the live record
5908
            $liveId = BackendUtility::getLiveVersionIdOfRecord($table, $MM_localUid);
5909
            $originalId = 0;
5910
            if (!empty($this->copyMappingArray_merged[$table])) {
5911
                $originalId = array_search($MM_localUid, $this->copyMappingArray_merged[$table]);
5912
            }
5913
            if (!empty($liveId) && !empty($originalId) && (int)$liveId === (int)$originalId) {
5914
                $liveRelations = $this->createRelationHandlerInstance();
5915
                $liveRelations->setWorkspaceId(0);
5916
                $liveRelations->start('', $allowedTables, $conf['MM'], $liveId, $table, $conf);
5917
                // Purge invalid relations in the live workspace ("0")
5918
                $liveRelations->purgeItemArray(0);
5919
                if ($liveRelations->isPurged()) {
5920
                    $liveRelations->writeMM($conf['MM'], $liveId, $prependName);
0 ignored issues
show
Bug introduced by
It seems like $prependName can also be of type string; however, parameter $prependTableName of TYPO3\CMS\Core\Database\RelationHandler::writeMM() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

5920
                    $liveRelations->writeMM($conf['MM'], $liveId, /** @scrutinizer ignore-type */ $prependName);
Loading history...
5921
                }
5922
            }
5923
        }
5924
        // If a change has been done, set the new value(s)
5925
        if ($set) {
5926
            if ($conf['MM']) {
5927
                $dbAnalysis->writeMM($conf['MM'], $MM_localUid, $prependName);
5928
            } else {
5929
                return $dbAnalysis->getValueArray($prependName);
0 ignored issues
show
Bug introduced by
It seems like $prependName can also be of type string; however, parameter $prependTableName of TYPO3\CMS\Core\Database\...andler::getValueArray() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

5929
                return $dbAnalysis->getValueArray(/** @scrutinizer ignore-type */ $prependName);
Loading history...
5930
            }
5931
        }
5932
        return null;
5933
    }
5934
5935
    /**
5936
     * Performs remapping of old UID values to NEW uid values for an inline field.
5937
     *
5938
     * @param array $conf TCA field config
5939
     * @param string $value Field value
5940
     * @param int $uid The uid of the ORIGINAL record
5941
     * @param string $table Table name
5942
     * @internal should only be used from within DataHandler
5943
     */
5944
    public function remapListedDBRecords_procInline($conf, $value, $uid, $table)
5945
    {
5946
        $theUidToUpdate = $this->copyMappingArray_merged[$table][$uid];
5947
        if ($conf['foreign_table']) {
5948
            $inlineType = $this->getInlineFieldType($conf);
5949
            if ($inlineType === 'mm') {
5950
                $this->remapListedDBRecords_procDBRefs($conf, $value, $theUidToUpdate, $table);
5951
            } elseif ($inlineType !== false) {
5952
                /** @var RelationHandler $dbAnalysis */
5953
                $dbAnalysis = $this->createRelationHandlerInstance();
5954
                $dbAnalysis->start($value, $conf['foreign_table'], '', 0, $table, $conf);
5955
5956
                $updatePidForRecords = [];
5957
                // Update values for specific versioned records
5958
                foreach ($dbAnalysis->itemArray as &$item) {
5959
                    $updatePidForRecords[$item['table']][] = $item['id'];
5960
                    $versionedId = $this->getAutoVersionId($item['table'], $item['id']);
5961
                    if ($versionedId !== null) {
5962
                        $updatePidForRecords[$item['table']][] = $versionedId;
5963
                        $item['id'] = $versionedId;
5964
                    }
5965
                }
5966
5967
                // Update child records if using pointer fields ('foreign_field'):
5968
                if ($inlineType === 'field') {
5969
                    $dbAnalysis->writeForeignField($conf, $uid, $theUidToUpdate);
5970
                }
5971
                $thePidToUpdate = null;
5972
                // If the current field is set on a page record, update the pid of related child records:
5973
                if ($table === 'pages') {
5974
                    $thePidToUpdate = $theUidToUpdate;
5975
                } elseif (isset($this->registerDBPids[$table][$uid])) {
5976
                    $thePidToUpdate = $this->registerDBPids[$table][$uid];
5977
                    $thePidToUpdate = $this->copyMappingArray_merged['pages'][$thePidToUpdate];
5978
                }
5979
5980
                // Update child records if change to pid is required
5981
                if ($thePidToUpdate && !empty($updatePidForRecords)) {
5982
                    // Ensure that only the default language page is used as PID
5983
                    $thePidToUpdate = $this->getDefaultLanguagePageId($thePidToUpdate);
5984
                    // @todo: this can probably go away
5985
                    // ensure, only live page ids are used as 'pid' values
5986
                    $liveId = BackendUtility::getLiveVersionIdOfRecord('pages', $theUidToUpdate);
5987
                    if ($liveId !== null) {
5988
                        $thePidToUpdate = $liveId;
5989
                    }
5990
                    $updateValues = ['pid' => $thePidToUpdate];
5991
                    foreach ($updatePidForRecords as $tableName => $uids) {
5992
                        if (empty($tableName) || empty($uids)) {
5993
                            continue;
5994
                        }
5995
                        $conn = GeneralUtility::makeInstance(ConnectionPool::class)
5996
                            ->getConnectionForTable($tableName);
5997
                        foreach ($uids as $updateUid) {
5998
                            $conn->update($tableName, $updateValues, ['uid' => $updateUid]);
5999
                        }
6000
                    }
6001
                }
6002
            }
6003
        }
6004
    }
6005
6006
    /**
6007
     * Processes the $this->remapStack at the end of copying, inserting, etc. actions.
6008
     * The remapStack takes care about the correct mapping of new and old uids in case of relational data.
6009
     * @internal should only be used from within DataHandler
6010
     */
6011
    public function processRemapStack()
6012
    {
6013
        // Processes the remap stack:
6014
        if (is_array($this->remapStack)) {
0 ignored issues
show
introduced by
The condition is_array($this->remapStack) is always true.
Loading history...
6015
            $remapFlexForms = [];
6016
            $hookPayload = [];
6017
6018
            $newValue = null;
6019
            foreach ($this->remapStack as $remapAction) {
6020
                // If no position index for the arguments was set, skip this remap action:
6021
                if (!is_array($remapAction['pos'])) {
6022
                    continue;
6023
                }
6024
                // Load values from the argument array in remapAction:
6025
                $field = $remapAction['field'];
6026
                $id = $remapAction['args'][$remapAction['pos']['id']];
6027
                $rawId = $id;
6028
                $table = $remapAction['args'][$remapAction['pos']['table']];
6029
                $valueArray = $remapAction['args'][$remapAction['pos']['valueArray']];
6030
                $tcaFieldConf = $remapAction['args'][$remapAction['pos']['tcaFieldConf']];
6031
                $additionalData = $remapAction['additionalData'];
6032
                // The record is new and has one or more new ids (in case of versioning/workspaces):
6033
                if (strpos($id, 'NEW') !== false) {
6034
                    // Replace NEW...-ID with real uid:
6035
                    $id = $this->substNEWwithIDs[$id];
6036
                    // If the new parent record is on a non-live workspace or versionized, it has another new id:
6037
                    if (isset($this->autoVersionIdMap[$table][$id])) {
6038
                        $id = $this->autoVersionIdMap[$table][$id];
6039
                    }
6040
                    $remapAction['args'][$remapAction['pos']['id']] = $id;
6041
                }
6042
                // Replace relations to NEW...-IDs in field value (uids of child records):
6043
                if (is_array($valueArray)) {
6044
                    foreach ($valueArray as $key => $value) {
6045
                        if (strpos($value, 'NEW') !== false) {
6046
                            if (strpos($value, '_') === false) {
6047
                                $affectedTable = $tcaFieldConf['foreign_table'];
6048
                                $prependTable = false;
6049
                            } else {
6050
                                $parts = explode('_', $value);
6051
                                $value = array_pop($parts);
6052
                                $affectedTable = implode('_', $parts);
6053
                                $prependTable = true;
6054
                            }
6055
                            $value = $this->substNEWwithIDs[$value];
6056
                            // The record is new, but was also auto-versionized and has another new id:
6057
                            if (isset($this->autoVersionIdMap[$affectedTable][$value])) {
6058
                                $value = $this->autoVersionIdMap[$affectedTable][$value];
6059
                            }
6060
                            if ($prependTable) {
6061
                                $value = $affectedTable . '_' . $value;
6062
                            }
6063
                            // Set a hint that this was a new child record:
6064
                            $this->newRelatedIDs[$affectedTable][] = $value;
6065
                            $valueArray[$key] = $value;
6066
                        }
6067
                    }
6068
                    $remapAction['args'][$remapAction['pos']['valueArray']] = $valueArray;
6069
                }
6070
                // Process the arguments with the defined function:
6071
                if (!empty($remapAction['func'])) {
6072
                    $newValue = call_user_func_array([$this, $remapAction['func']], $remapAction['args']);
6073
                }
6074
                // If array is returned, check for maxitems condition, if string is returned this was already done:
6075
                if (is_array($newValue)) {
6076
                    $newValue = implode(',', $this->checkValue_checkMax($tcaFieldConf, $newValue));
6077
                    // The reference casting is only required if
6078
                    // checkValue_group_select_processDBdata() returns an array
6079
                    $newValue = $this->castReferenceValue($newValue, $tcaFieldConf);
6080
                }
6081
                // Update in database (list of children (csv) or number of relations (foreign_field)):
6082
                if (!empty($field)) {
6083
                    $fieldArray = [$field => $newValue];
6084
                    if ($GLOBALS['TCA'][$table]['ctrl']['tstamp']) {
6085
                        $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['tstamp']] = $GLOBALS['EXEC_TIME'];
6086
                    }
6087
                    $this->updateDB($table, $id, $fieldArray);
6088
                } elseif (!empty($additionalData['flexFormId']) && !empty($additionalData['flexFormPath'])) {
6089
                    // Collect data to update FlexForms
6090
                    $flexFormId = $additionalData['flexFormId'];
6091
                    $flexFormPath = $additionalData['flexFormPath'];
6092
6093
                    if (!isset($remapFlexForms[$flexFormId])) {
6094
                        $remapFlexForms[$flexFormId] = [];
6095
                    }
6096
6097
                    $remapFlexForms[$flexFormId][$flexFormPath] = $newValue;
6098
                }
6099
6100
                // Collect elements that shall trigger processDatamap_afterDatabaseOperations
6101
                if (isset($this->remapStackRecords[$table][$rawId]['processDatamap_afterDatabaseOperations'])) {
6102
                    $hookArgs = $this->remapStackRecords[$table][$rawId]['processDatamap_afterDatabaseOperations'];
6103
                    if (!isset($hookPayload[$table][$rawId])) {
6104
                        $hookPayload[$table][$rawId] = [
6105
                            'status' => $hookArgs['status'],
6106
                            'fieldArray' => $hookArgs['fieldArray'],
6107
                            'hookObjects' => $hookArgs['hookObjectsArr'],
6108
                        ];
6109
                    }
6110
                    $hookPayload[$table][$rawId]['fieldArray'][$field] = $newValue;
6111
                }
6112
            }
6113
6114
            if ($remapFlexForms) {
6115
                foreach ($remapFlexForms as $flexFormId => $modifications) {
6116
                    $this->updateFlexFormData($flexFormId, $modifications);
6117
                }
6118
            }
6119
6120
            foreach ($hookPayload as $tableName => $rawIdPayload) {
6121
                foreach ($rawIdPayload as $rawId => $payload) {
6122
                    foreach ($payload['hookObjects'] as $hookObject) {
6123
                        if (!method_exists($hookObject, 'processDatamap_afterDatabaseOperations')) {
6124
                            continue;
6125
                        }
6126
                        $hookObject->processDatamap_afterDatabaseOperations(
6127
                            $payload['status'],
6128
                            $tableName,
6129
                            $rawId,
6130
                            $payload['fieldArray'],
6131
                            $this
6132
                        );
6133
                    }
6134
                }
6135
            }
6136
        }
6137
        // Processes the remap stack actions:
6138
        if ($this->remapStackActions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->remapStackActions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
6139
            foreach ($this->remapStackActions as $action) {
6140
                if (isset($action['callback'], $action['arguments'])) {
6141
                    call_user_func_array($action['callback'], $action['arguments']);
6142
                }
6143
            }
6144
        }
6145
        // Reset:
6146
        $this->remapStack = [];
6147
        $this->remapStackRecords = [];
6148
        $this->remapStackActions = [];
6149
    }
6150
6151
    /**
6152
     * Updates FlexForm data.
6153
     *
6154
     * @param string $flexFormId e.g. <table>:<uid>:<field>
6155
     * @param array $modifications Modifications with paths and values (e.g. 'sDEF/lDEV/field/vDEF' => 'TYPO3')
6156
     */
6157
    protected function updateFlexFormData($flexFormId, array $modifications)
6158
    {
6159
        [$table, $uid, $field] = explode(':', $flexFormId, 3);
6160
6161
        if (!MathUtility::canBeInterpretedAsInteger($uid) && !empty($this->substNEWwithIDs[$uid])) {
6162
            $uid = $this->substNEWwithIDs[$uid];
6163
        }
6164
6165
        $record = $this->recordInfo($table, $uid, '*');
6166
6167
        if (!$table || !$uid || !$field || !is_array($record)) {
6168
            return;
6169
        }
6170
6171
        BackendUtility::workspaceOL($table, $record);
6172
6173
        // Get current data structure and value array:
6174
        $valueStructure = GeneralUtility::xml2array($record[$field]);
6175
6176
        // Do recursive processing of the XML data:
6177
        foreach ($modifications as $path => $value) {
6178
            $valueStructure['data'] = ArrayUtility::setValueByPath(
6179
                $valueStructure['data'],
6180
                $path,
6181
                $value
6182
            );
6183
        }
6184
6185
        if (is_array($valueStructure['data'])) {
6186
            // The return value should be compiled back into XML
6187
            $values = [
6188
                $field => $this->checkValue_flexArray2Xml($valueStructure, true),
6189
            ];
6190
6191
            $this->updateDB($table, $uid, $values);
6192
        }
6193
    }
6194
6195
    /**
6196
     * Adds an instruction to the remap action stack (used with IRRE).
6197
     *
6198
     * @param string $table The affected table
6199
     * @param int $id The affected ID
6200
     * @param array $callback The callback information (object and method)
6201
     * @param array $arguments The arguments to be used with the callback
6202
     * @internal should only be used from within DataHandler
6203
     */
6204
    public function addRemapAction($table, $id, array $callback, array $arguments)
6205
    {
6206
        $this->remapStackActions[] = [
6207
            'affects' => [
6208
                'table' => $table,
6209
                'id' => $id
6210
            ],
6211
            'callback' => $callback,
6212
            'arguments' => $arguments
6213
        ];
6214
    }
6215
6216
    /**
6217
     * If a parent record was versionized on a workspace in $this->process_datamap,
6218
     * it might be possible, that child records (e.g. on using IRRE) were affected.
6219
     * This function finds these relations and updates their uids in the $incomingFieldArray.
6220
     * The $incomingFieldArray is updated by reference!
6221
     *
6222
     * @param string $table Table name of the parent record
6223
     * @param int $id Uid of the parent record
6224
     * @param array $incomingFieldArray Reference to the incomingFieldArray of process_datamap
6225
     * @param array $registerDBList Reference to the $registerDBList array that was created/updated by versionizing calls to DataHandler in process_datamap.
6226
     * @internal should only be used from within DataHandler
6227
     */
6228
    public function getVersionizedIncomingFieldArray($table, $id, &$incomingFieldArray, &$registerDBList)
6229
    {
6230
        if (is_array($registerDBList[$table][$id])) {
6231
            foreach ($incomingFieldArray as $field => $value) {
6232
                $fieldConf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
6233
                if ($registerDBList[$table][$id][$field] && ($foreignTable = $fieldConf['foreign_table'])) {
6234
                    $newValueArray = [];
6235
                    $origValueArray = is_array($value) ? $value : explode(',', $value);
6236
                    // Update the uids of the copied records, but also take care about new records:
6237
                    foreach ($origValueArray as $childId) {
6238
                        $newValueArray[] = $this->autoVersionIdMap[$foreignTable][$childId] ?: $childId;
6239
                    }
6240
                    // Set the changed value to the $incomingFieldArray
6241
                    $incomingFieldArray[$field] = implode(',', $newValueArray);
6242
                }
6243
            }
6244
            // Clean up the $registerDBList array:
6245
            unset($registerDBList[$table][$id]);
6246
            if (empty($registerDBList[$table])) {
6247
                unset($registerDBList[$table]);
6248
            }
6249
        }
6250
    }
6251
6252
    /**
6253
     * Simple helper method to hard delete one row from table ignoring delete TCA field
6254
     *
6255
     * @param string $table A row from this table should be deleted
6256
     * @param int $uid Uid of row to be deleted
6257
     */
6258
    protected function hardDeleteSingleRecord(string $table, int $uid): void
6259
    {
6260
        GeneralUtility::makeInstance(ConnectionPool::class)
6261
            ->getConnectionForTable($table)
6262
            ->delete($table, ['uid' => $uid], [\PDO::PARAM_INT]);
6263
    }
6264
6265
    /*****************************
6266
     *
6267
     * Access control / Checking functions
6268
     *
6269
     *****************************/
6270
    /**
6271
     * Checking group modify_table access list
6272
     *
6273
     * @param string $table Table name
6274
     * @return bool Returns TRUE if the user has general access to modify the $table
6275
     * @internal should only be used from within DataHandler
6276
     */
6277
    public function checkModifyAccessList($table)
6278
    {
6279
        $res = $this->admin || (!$this->tableAdminOnly($table) && isset($this->BE_USER->groupData['tables_modify']) && GeneralUtility::inList($this->BE_USER->groupData['tables_modify'], $table));
6280
        // Hook 'checkModifyAccessList': Post-processing of the state of access
6281
        foreach ($this->getCheckModifyAccessListHookObjects() as $hookObject) {
6282
            /** @var DataHandlerCheckModifyAccessListHookInterface $hookObject */
6283
            $hookObject->checkModifyAccessList($res, $table, $this);
6284
        }
6285
        return $res;
6286
    }
6287
6288
    /**
6289
     * Checking if a record with uid $id from $table is in the BE_USERS webmounts which is required for editing etc.
6290
     *
6291
     * @param string $table Table name
6292
     * @param int $id UID of record
6293
     * @return bool Returns TRUE if OK. Cached results.
6294
     * @internal should only be used from within DataHandler
6295
     */
6296
    public function isRecordInWebMount($table, $id)
6297
    {
6298
        if (!isset($this->isRecordInWebMount_Cache[$table . ':' . $id])) {
6299
            $recP = $this->getRecordProperties($table, $id);
6300
            $this->isRecordInWebMount_Cache[$table . ':' . $id] = $this->isInWebMount($recP['event_pid']);
6301
        }
6302
        return $this->isRecordInWebMount_Cache[$table . ':' . $id];
6303
    }
6304
6305
    /**
6306
     * Checks if the input page ID is in the BE_USER webmounts
6307
     *
6308
     * @param int $pid Page ID to check
6309
     * @return bool TRUE if OK. Cached results.
6310
     * @internal should only be used from within DataHandler
6311
     */
6312
    public function isInWebMount($pid)
6313
    {
6314
        if (!isset($this->isInWebMount_Cache[$pid])) {
6315
            $this->isInWebMount_Cache[$pid] = $this->BE_USER->isInWebMount($pid);
6316
        }
6317
        return $this->isInWebMount_Cache[$pid];
6318
    }
6319
6320
    /**
6321
     * Checks if user may update a record with uid=$id from $table
6322
     *
6323
     * @param string $table Record table
6324
     * @param int $id Record UID
6325
     * @param array|bool $data Record data
6326
     * @param array $hookObjectsArr Hook objects
6327
     * @return bool Returns TRUE if the user may update the record given by $table and $id
6328
     * @internal should only be used from within DataHandler
6329
     */
6330
    public function checkRecordUpdateAccess($table, $id, $data = false, $hookObjectsArr = null)
6331
    {
6332
        $res = null;
6333
        if (is_array($hookObjectsArr)) {
6334
            foreach ($hookObjectsArr as $hookObj) {
6335
                if (method_exists($hookObj, 'checkRecordUpdateAccess')) {
6336
                    $res = $hookObj->checkRecordUpdateAccess($table, $id, $data, $res, $this);
6337
                }
6338
            }
6339
            if (isset($res)) {
6340
                return (bool)$res;
6341
            }
6342
        }
6343
        $res = false;
6344
6345
        if ($GLOBALS['TCA'][$table] && (int)$id > 0) {
6346
            $cacheId = 'checkRecordUpdateAccess_' . $table . '_' . $id;
6347
6348
            // If information is cached, return it
6349
            $cachedValue = $this->runtimeCache->get($cacheId);
6350
            if (!empty($cachedValue)) {
6351
                return $cachedValue;
6352
            }
6353
6354
            if ($table === 'pages' || ($table === 'sys_file_reference' && array_key_exists('pages', $this->datamap))) {
6355
                // @todo: find a more generic way to handle content relations of a page (without needing content editing access to that page)
6356
                $perms = Permission::PAGE_EDIT;
6357
            } else {
6358
                $perms = Permission::CONTENT_EDIT;
6359
            }
6360
            if ($this->doesRecordExist($table, $id, $perms)) {
6361
                $res = 1;
6362
            }
6363
            // Cache the result
6364
            $this->runtimeCache->set($cacheId, $res);
6365
        }
6366
        return $res;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $res also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
6367
    }
6368
6369
    /**
6370
     * Checks if user may insert a record from $insertTable on $pid
6371
     *
6372
     * @param string $insertTable Tablename to check
6373
     * @param int $pid Integer PID
6374
     * @param int $action For logging: Action number.
6375
     * @return bool Returns TRUE if the user may insert a record from table $insertTable on page $pid
6376
     * @internal should only be used from within DataHandler
6377
     */
6378
    public function checkRecordInsertAccess($insertTable, $pid, $action = SystemLogDatabaseAction::INSERT)
6379
    {
6380
        $pid = (int)$pid;
6381
        if ($pid < 0) {
6382
            return false;
6383
        }
6384
        // If information is cached, return it
6385
        if (isset($this->recInsertAccessCache[$insertTable][$pid])) {
6386
            return $this->recInsertAccessCache[$insertTable][$pid];
6387
        }
6388
6389
        $res = false;
6390
        if ($insertTable === 'pages') {
6391
            $perms = Permission::PAGE_NEW;
6392
        } elseif (($insertTable === 'sys_file_reference') && array_key_exists('pages', $this->datamap)) {
6393
            // @todo: find a more generic way to handle content relations of a page (without needing content editing access to that page)
6394
            $perms = Permission::PAGE_EDIT;
6395
        } else {
6396
            $perms = Permission::CONTENT_EDIT;
6397
        }
6398
        $pageExists = (bool)$this->doesRecordExist('pages', $pid, $perms);
6399
        // If either admin and root-level or if page record exists and 1) if 'pages' you may create new ones 2) if page-content, new content items may be inserted on the $pid page
6400
        if ($pageExists || $pid === 0 && ($this->admin || BackendUtility::isRootLevelRestrictionIgnored($insertTable))) {
6401
            // Check permissions
6402
            if ($this->isTableAllowedForThisPage($pid, $insertTable)) {
6403
                $res = true;
6404
                // Cache the result
6405
                $this->recInsertAccessCache[$insertTable][$pid] = $res;
6406
            } elseif ($this->enableLogging) {
6407
                $propArr = $this->getRecordProperties('pages', $pid);
6408
                $this->log($insertTable, $pid, $action, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to insert record on page \'%s\' (%s) where this table, %s, is not allowed', 11, [$propArr['header'], $pid, $insertTable], $propArr['event_pid']);
6409
            }
6410
        } elseif ($this->enableLogging) {
6411
            $propArr = $this->getRecordProperties('pages', $pid);
6412
            $this->log($insertTable, $pid, $action, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to insert a record on page \'%s\' (%s) from table \'%s\' without permissions. Or non-existing page.', 12, [$propArr['header'], $pid, $insertTable], $propArr['event_pid']);
6413
        }
6414
        return $res;
6415
    }
6416
6417
    /**
6418
     * Checks if a table is allowed on a certain page id according to allowed tables set for the page "doktype" and its [ctrl][rootLevel]-settings if any.
6419
     *
6420
     * @param int $page_uid Page id for which to check, including 0 (zero) if checking for page tree root.
6421
     * @param string $checkTable Table name to check
6422
     * @return bool TRUE if OK
6423
     * @internal should only be used from within DataHandler
6424
     */
6425
    public function isTableAllowedForThisPage($page_uid, $checkTable)
6426
    {
6427
        $page_uid = (int)$page_uid;
6428
        $rootLevelSetting = (int)$GLOBALS['TCA'][$checkTable]['ctrl']['rootLevel'];
6429
        // Check if rootLevel flag is set and we're trying to insert on rootLevel - and reversed - and that the table is not "pages" which are allowed anywhere.
6430
        if ($checkTable !== 'pages' && $rootLevelSetting !== -1 && ($rootLevelSetting xor !$page_uid)) {
6431
            return false;
6432
        }
6433
        $allowed = false;
6434
        // Check root-level
6435
        if (!$page_uid) {
6436
            if ($this->admin || BackendUtility::isRootLevelRestrictionIgnored($checkTable)) {
6437
                $allowed = true;
6438
            }
6439
        } else {
6440
            // Check non-root-level
6441
            $doktype = $this->pageInfo($page_uid, 'doktype');
6442
            $allowedTableList = $GLOBALS['PAGES_TYPES'][$doktype]['allowedTables'] ?? $GLOBALS['PAGES_TYPES']['default']['allowedTables'];
6443
            $allowedArray = GeneralUtility::trimExplode(',', $allowedTableList, true);
6444
            // If all tables or the table is listed as an allowed type, return TRUE
6445
            if (strpos($allowedTableList, '*') !== false || in_array($checkTable, $allowedArray, true)) {
6446
                $allowed = true;
6447
            }
6448
        }
6449
        return $allowed;
6450
    }
6451
6452
    /**
6453
     * Checks if record can be selected based on given permission criteria
6454
     *
6455
     * @param string $table Record table name
6456
     * @param int $id Record UID
6457
     * @param int $perms Permission restrictions to observe: integer that will be bitwise AND'ed.
6458
     * @return bool Returns TRUE if the record given by $table, $id and $perms can be selected
6459
     *
6460
     * @throws \RuntimeException
6461
     * @internal should only be used from within DataHandler
6462
     */
6463
    public function doesRecordExist($table, $id, int $perms)
6464
    {
6465
        return $this->recordInfoWithPermissionCheck($table, $id, $perms, 'uid, pid') !== false;
6466
    }
6467
6468
    /**
6469
     * Looks up a page based on permissions.
6470
     *
6471
     * @param int $id Page id
6472
     * @param int $perms Permission integer
6473
     * @param array $columns Columns to select
6474
     * @return bool|array
6475
     * @internal
6476
     * @see doesRecordExist()
6477
     */
6478
    protected function doesRecordExist_pageLookUp($id, $perms, $columns = ['uid'])
6479
    {
6480
        $permission = new Permission($perms);
6481
        $cacheId = md5('doesRecordExist_pageLookUp_' . $id . '_' . $perms . '_' . implode(
6482
            '_',
6483
            $columns
6484
        ) . '_' . (string)$this->admin);
6485
6486
        // If result is cached, return it
6487
        $cachedResult = $this->runtimeCache->get($cacheId);
6488
        if (!empty($cachedResult)) {
6489
            return $cachedResult;
6490
        }
6491
6492
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
6493
        $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
6494
        $queryBuilder
6495
            ->select(...$columns)
6496
            ->from('pages')
6497
            ->where($queryBuilder->expr()->eq(
6498
                'uid',
6499
                $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)
6500
            ));
6501
        if (!$permission->nothingIsGranted() && !$this->admin) {
6502
            $queryBuilder->andWhere($this->BE_USER->getPagePermsClause($perms));
6503
        }
6504
        if (!$this->admin && $GLOBALS['TCA']['pages']['ctrl']['editlock'] &&
6505
            ($permission->editPagePermissionIsGranted() || $permission->deletePagePermissionIsGranted() || $permission->editContentPermissionIsGranted())
6506
        ) {
6507
            $queryBuilder->andWhere($queryBuilder->expr()->eq(
6508
                $GLOBALS['TCA']['pages']['ctrl']['editlock'],
6509
                $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
6510
            ));
6511
        }
6512
6513
        $row = $queryBuilder->execute()->fetch();
6514
        $this->runtimeCache->set($cacheId, $row);
6515
6516
        return $row;
6517
    }
6518
6519
    /**
6520
     * Checks if a whole branch of pages exists
6521
     *
6522
     * Tests the branch under $pid like doesRecordExist(), but it doesn't test the page with $pid as uid - use doesRecordExist() for this purpose.
6523
     * If $recurse is set, the function will follow subpages. This MUST be set, if we need the id-list for deleting pages or else we get an incomplete list
6524
     *
6525
     * @param string $inList List of page uids, this is added to and returned in the end
6526
     * @param int $pid Page ID to select subpages from.
6527
     * @param int $perms Perms integer to check each page record for.
6528
     * @param bool $recurse Recursion flag: If set, it will go out through the branch.
6529
     * @return string|int List of page IDs in branch, if there are subpages, empty string if there are none or -1 if no permission
6530
     * @internal should only be used from within DataHandler
6531
     */
6532
    public function doesBranchExist($inList, $pid, $perms, $recurse)
6533
    {
6534
        $pid = (int)$pid;
6535
        $perms = (int)$perms;
6536
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
6537
        $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
6538
        $result = $queryBuilder
6539
            ->select('uid', 'perms_userid', 'perms_groupid', 'perms_user', 'perms_group', 'perms_everybody')
6540
            ->from('pages')
6541
            ->where($queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)))
6542
            ->orderBy('sorting')
6543
            ->execute();
6544
        while ($row = $result->fetch()) {
6545
            // IF admin, then it's OK
6546
            if ($this->admin || $this->BE_USER->doesUserHaveAccess($row, $perms)) {
6547
                $inList .= $row['uid'] . ',';
6548
                if ($recurse) {
6549
                    // Follow the subpages recursively...
6550
                    $inList = $this->doesBranchExist($inList, $row['uid'], $perms, $recurse);
6551
                    if ($inList === -1) {
6552
                        return -1;
6553
                    }
6554
                }
6555
            } else {
6556
                // No permissions
6557
                return -1;
6558
            }
6559
        }
6560
        return $inList;
6561
    }
6562
6563
    /**
6564
     * Checks if the $table is readOnly
6565
     *
6566
     * @param string $table Table name
6567
     * @return bool TRUE, if readonly
6568
     * @internal should only be used from within DataHandler
6569
     */
6570
    public function tableReadOnly($table)
6571
    {
6572
        // Returns TRUE if table is readonly
6573
        return (bool)$GLOBALS['TCA'][$table]['ctrl']['readOnly'];
6574
    }
6575
6576
    /**
6577
     * Checks if the $table is only editable by admin-users
6578
     *
6579
     * @param string $table Table name
6580
     * @return bool TRUE, if readonly
6581
     * @internal should only be used from within DataHandler
6582
     */
6583
    public function tableAdminOnly($table)
6584
    {
6585
        // Returns TRUE if table is admin-only
6586
        return !empty($GLOBALS['TCA'][$table]['ctrl']['adminOnly']);
6587
    }
6588
6589
    /**
6590
     * Checks if page $id is a uid in the rootline of page id $destinationId
6591
     * Used when moving a page
6592
     *
6593
     * @param int $destinationId Destination Page ID to test
6594
     * @param int $id Page ID to test for presence inside Destination
6595
     * @return bool Returns FALSE if ID is inside destination (including equal to)
6596
     * @internal should only be used from within DataHandler
6597
     */
6598
    public function destNotInsideSelf($destinationId, $id)
6599
    {
6600
        $loopCheck = 100;
6601
        $destinationId = (int)$destinationId;
6602
        $id = (int)$id;
6603
        if ($destinationId === $id) {
6604
            return false;
6605
        }
6606
        while ($destinationId !== 0 && $loopCheck > 0) {
6607
            $loopCheck--;
6608
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
6609
            $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
6610
            $result = $queryBuilder
6611
                ->select('pid', 'uid', 't3ver_oid', 't3ver_wsid')
6612
                ->from('pages')
6613
                ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($destinationId, \PDO::PARAM_INT)))
6614
                ->execute();
6615
            if ($row = $result->fetch()) {
6616
                // Ensure that the moved location is used as the PID value
6617
                BackendUtility::workspaceOL('pages', $row, $this->BE_USER->workspace);
6618
                if ($row['pid'] == $id) {
6619
                    return false;
6620
                }
6621
                $destinationId = (int)$row['pid'];
6622
            } else {
6623
                return false;
6624
            }
6625
        }
6626
        return true;
6627
    }
6628
6629
    /**
6630
     * Generate an array of fields to be excluded from editing for the user. Based on "exclude"-field in TCA and a look up in non_exclude_fields
6631
     * Will also generate this list for admin-users so they must be check for before calling the function
6632
     *
6633
     * @return array Array of [table]-[field] pairs to exclude from editing.
6634
     * @internal should only be used from within DataHandler
6635
     */
6636
    public function getExcludeListArray()
6637
    {
6638
        $list = [];
6639
        if (isset($this->BE_USER->groupData['non_exclude_fields'])) {
6640
            $nonExcludeFieldsArray = array_flip(GeneralUtility::trimExplode(',', $this->BE_USER->groupData['non_exclude_fields']));
6641
            foreach ($GLOBALS['TCA'] as $table => $tableConfiguration) {
6642
                if (isset($tableConfiguration['columns'])) {
6643
                    foreach ($tableConfiguration['columns'] as $field => $config) {
6644
                        $isExcludeField = ($config['exclude'] ?? false);
6645
                        $isOnlyVisibleForAdmins = ($GLOBALS['TCA'][$table]['columns'][$field]['displayCond'] ?? '') === 'HIDE_FOR_NON_ADMINS';
6646
                        $editorHasPermissionForThisField = isset($nonExcludeFieldsArray[$table . ':' . $field]);
6647
                        if ($isOnlyVisibleForAdmins || ($isExcludeField && !$editorHasPermissionForThisField)) {
6648
                            $list[] = $table . '-' . $field;
6649
                        }
6650
                    }
6651
                }
6652
            }
6653
        }
6654
6655
        return $list;
6656
    }
6657
6658
    /**
6659
     * Checks if there are records on a page from tables that are not allowed
6660
     *
6661
     * @param int $page_uid Page ID
6662
     * @param int $doktype Page doktype
6663
     * @return bool|array Returns a list of the tables that are 'present' on the page but not allowed with the page_uid/doktype
6664
     * @internal should only be used from within DataHandler
6665
     */
6666
    public function doesPageHaveUnallowedTables($page_uid, $doktype)
6667
    {
6668
        $page_uid = (int)$page_uid;
6669
        if (!$page_uid) {
6670
            // Not a number. Probably a new page
6671
            return false;
6672
        }
6673
        $allowedTableList = $GLOBALS['PAGES_TYPES'][$doktype]['allowedTables'] ?? $GLOBALS['PAGES_TYPES']['default']['allowedTables'];
6674
        // If all tables are allowed, return early
6675
        if (strpos($allowedTableList, '*') !== false) {
6676
            return false;
6677
        }
6678
        $allowedArray = GeneralUtility::trimExplode(',', $allowedTableList, true);
6679
        $tableList = [];
6680
        $allTableNames = $this->compileAdminTables();
6681
        foreach ($allTableNames as $table) {
6682
            // If the table is not in the allowed list, check if there are records...
6683
            if (in_array($table, $allowedArray, true)) {
6684
                continue;
6685
            }
6686
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
6687
            $queryBuilder->getRestrictions()->removeAll();
6688
            $count = $queryBuilder
6689
                ->count('uid')
6690
                ->from($table)
6691
                ->where($queryBuilder->expr()->eq(
6692
                    'pid',
6693
                    $queryBuilder->createNamedParameter($page_uid, \PDO::PARAM_INT)
6694
                ))
6695
                ->execute()
6696
                ->fetchColumn(0);
6697
            if ($count) {
6698
                $tableList[] = $table;
6699
            }
6700
        }
6701
        return implode(',', $tableList);
0 ignored issues
show
Bug Best Practice introduced by
The expression return implode(',', $tableList) returns the type string which is incompatible with the documented return type array|boolean.
Loading history...
6702
    }
6703
6704
    /*****************************
6705
     *
6706
     * Information lookup
6707
     *
6708
     *****************************/
6709
    /**
6710
     * Returns the value of the $field from page $id
6711
     * NOTICE; the function caches the result for faster delivery next time. You can use this function repeatedly without performance loss since it doesn't look up the same record twice!
6712
     *
6713
     * @param int $id Page uid
6714
     * @param string $field Field name for which to return value
6715
     * @return string Value of the field. Result is cached in $this->pageCache[$id][$field] and returned from there next time!
6716
     * @internal should only be used from within DataHandler
6717
     */
6718
    public function pageInfo($id, $field)
6719
    {
6720
        if (!isset($this->pageCache[$id])) {
6721
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
6722
            $queryBuilder->getRestrictions()->removeAll();
6723
            $row = $queryBuilder
6724
                ->select('*')
6725
                ->from('pages')
6726
                ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)))
6727
                ->execute()
6728
                ->fetch();
6729
            if ($row) {
6730
                $this->pageCache[$id] = $row;
6731
            }
6732
        }
6733
        return $this->pageCache[$id][$field];
6734
    }
6735
6736
    /**
6737
     * Returns the row of a record given by $table and $id and $fieldList (list of fields, may be '*')
6738
     * NOTICE: No check for deleted or access!
6739
     *
6740
     * @param string $table Table name
6741
     * @param int $id UID of the record from $table
6742
     * @param string $fieldList Field list for the SELECT query, eg. "*" or "uid,pid,...
6743
     * @return array|null Returns the selected record on success, otherwise NULL.
6744
     * @internal should only be used from within DataHandler
6745
     */
6746
    public function recordInfo($table, $id, $fieldList)
6747
    {
6748
        // Skip, if searching for NEW records or there's no TCA table definition
6749
        if ((int)$id === 0 || !isset($GLOBALS['TCA'][$table])) {
6750
            return null;
6751
        }
6752
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
6753
        $queryBuilder->getRestrictions()->removeAll();
6754
        $result = $queryBuilder
6755
            ->select(...GeneralUtility::trimExplode(',', $fieldList))
6756
            ->from($table)
6757
            ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)))
6758
            ->execute()
6759
            ->fetch();
6760
        return $result ?: null;
6761
    }
6762
6763
    /**
6764
     * Checks if record exists with and without permission check and returns that row
6765
     *
6766
     * @param string $table Record table name
6767
     * @param int $id Record UID
6768
     * @param int $perms Permission restrictions to observe: An integer that will be bitwise AND'ed.
6769
     * @param string $fieldList - fields - default is '*'
6770
     * @throws \RuntimeException
6771
     * @return array|bool Row if exists and accessible, false otherwise
6772
     */
6773
    protected function recordInfoWithPermissionCheck(string $table, int $id, int $perms, string $fieldList = '*')
6774
    {
6775
        if ($this->bypassAccessCheckForRecords) {
6776
            $columns = GeneralUtility::trimExplode(',', $fieldList, true);
6777
6778
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
6779
            $queryBuilder->getRestrictions()->removeAll();
6780
6781
            $record = $queryBuilder->select(...$columns)
6782
                ->from($table)
6783
                ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)))
6784
                ->execute()
6785
                ->fetch();
6786
6787
            return $record ?: false;
6788
        }
6789
        if (!$perms) {
6790
            throw new \RuntimeException('Internal ERROR: no permissions to check for non-admin user', 1270853920);
6791
        }
6792
        // For all tables: Check if record exists:
6793
        $isWebMountRestrictionIgnored = BackendUtility::isWebMountRestrictionIgnored($table);
6794
        if (is_array($GLOBALS['TCA'][$table]) && $id > 0 && ($this->admin || $isWebMountRestrictionIgnored || $this->isRecordInWebMount($table, $id))) {
6795
            $columns = GeneralUtility::trimExplode(',', $fieldList, true);
6796
            if ($table !== 'pages') {
6797
                // Find record without checking page
6798
                // @todo: This should probably check for editlock
6799
                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
6800
                $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
6801
                $output = $queryBuilder
6802
                    ->select(...$columns)
6803
                    ->from($table)
6804
                    ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)))
6805
                    ->execute()
6806
                    ->fetch();
6807
                // If record found, check page as well:
6808
                if (is_array($output)) {
6809
                    // Looking up the page for record:
6810
                    $pageRec = $this->doesRecordExist_pageLookUp($output['pid'], $perms);
6811
                    // Return TRUE if either a page was found OR if the PID is zero AND the user is ADMIN (in which case the record is at root-level):
6812
                    $isRootLevelRestrictionIgnored = BackendUtility::isRootLevelRestrictionIgnored($table);
6813
                    if (is_array($pageRec) || !$output['pid'] && ($this->admin || $isRootLevelRestrictionIgnored)) {
6814
                        return $output;
6815
                    }
6816
                }
6817
                return false;
6818
            }
6819
            return $this->doesRecordExist_pageLookUp($id, $perms, $columns);
6820
        }
6821
        return false;
6822
    }
6823
6824
    /**
6825
     * Returns an array with record properties, like header and pid
6826
     * No check for deleted or access is done!
6827
     * For versionized records, pid is resolved to its live versions pid.
6828
     * Used for logging
6829
     *
6830
     * @param string $table Table name
6831
     * @param int $id Uid of record
6832
     * @param bool $noWSOL If set, no workspace overlay is performed
6833
     * @return array Properties of record
6834
     * @internal should only be used from within DataHandler
6835
     */
6836
    public function getRecordProperties($table, $id, $noWSOL = false)
6837
    {
6838
        $row = $table === 'pages' && !$id ? ['title' => '[root-level]', 'uid' => 0, 'pid' => 0] : $this->recordInfo($table, $id, '*');
6839
        if (!$noWSOL) {
6840
            BackendUtility::workspaceOL($table, $row);
6841
        }
6842
        return $this->getRecordPropertiesFromRow($table, $row);
6843
    }
6844
6845
    /**
6846
     * Returns an array with record properties, like header and pid, based on the row
6847
     *
6848
     * @param string $table Table name
6849
     * @param array $row Input row
6850
     * @return array|null Output array
6851
     * @internal should only be used from within DataHandler
6852
     */
6853
    public function getRecordPropertiesFromRow($table, $row)
6854
    {
6855
        if ($GLOBALS['TCA'][$table]) {
6856
            $liveUid = ($row['t3ver_oid'] ?? null) ? $row['t3ver_oid'] : $row['uid'];
6857
            return [
6858
                'header' => BackendUtility::getRecordTitle($table, $row),
6859
                'pid' => $row['pid'],
6860
                'event_pid' => $this->eventPid($table, (int)$liveUid, $row['pid']),
6861
                't3ver_state' => BackendUtility::isTableWorkspaceEnabled($table) ? $row['t3ver_state'] : ''
6862
            ];
6863
        }
6864
        return null;
6865
    }
6866
6867
    /**
6868
     * @param string $table
6869
     * @param int $uid
6870
     * @param int $pid
6871
     * @return int
6872
     * @internal should only be used from within DataHandler
6873
     */
6874
    public function eventPid($table, $uid, $pid)
6875
    {
6876
        return $table === 'pages' ? $uid : $pid;
6877
    }
6878
6879
    /*********************************************
6880
     *
6881
     * Storing data to Database Layer
6882
     *
6883
     ********************************************/
6884
    /**
6885
     * Update database record
6886
     * Does not check permissions but expects them to be verified on beforehand
6887
     *
6888
     * @param string $table Record table name
6889
     * @param int $id Record uid
6890
     * @param array $fieldArray Array of field=>value pairs to insert. FIELDS MUST MATCH the database FIELDS. No check is done.
6891
     * @internal should only be used from within DataHandler
6892
     */
6893
    public function updateDB($table, $id, $fieldArray)
6894
    {
6895
        if (is_array($fieldArray) && is_array($GLOBALS['TCA'][$table]) && (int)$id) {
6896
            // Do NOT update the UID field, ever!
6897
            unset($fieldArray['uid']);
6898
            if (!empty($fieldArray)) {
6899
                $fieldArray = $this->insertUpdateDB_preprocessBasedOnFieldType($table, $fieldArray);
6900
6901
                $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
6902
6903
                $types = [];
6904
                $platform = $connection->getDatabasePlatform();
6905
                if ($platform instanceof SQLServerPlatform) {
6906
                    // mssql needs to set proper PARAM_LOB and others to update fields
6907
                    $tableDetails = $connection->getSchemaManager()->listTableDetails($table);
6908
                    foreach ($fieldArray as $columnName => $columnValue) {
6909
                        $types[$columnName] = $tableDetails->getColumn($columnName)->getType()->getBindingType();
6910
                    }
6911
                }
6912
6913
                // Execute the UPDATE query:
6914
                $updateErrorMessage = '';
6915
                try {
6916
                    $connection->update($table, $fieldArray, ['uid' => (int)$id], $types);
6917
                } catch (DBALException $e) {
6918
                    $updateErrorMessage = $e->getPrevious()->getMessage();
6919
                }
6920
                // If succeeds, do...:
6921
                if ($updateErrorMessage === '') {
6922
                    // Update reference index:
6923
                    $this->updateRefIndex($table, $id);
6924
                    // Set History data
6925
                    $historyEntryId = 0;
6926
                    if (isset($this->historyRecords[$table . ':' . $id])) {
6927
                        $historyEntryId = $this->getRecordHistoryStore()->modifyRecord($table, $id, $this->historyRecords[$table . ':' . $id], $this->correlationId);
6928
                    }
6929
                    if ($this->enableLogging) {
6930
                        if ($this->checkStoredRecords) {
6931
                            $newRow = $this->checkStoredRecord($table, $id, $fieldArray, SystemLogDatabaseAction::UPDATE);
6932
                        } else {
6933
                            $newRow = $fieldArray;
6934
                            $newRow['uid'] = $id;
6935
                        }
6936
                        // Set log entry:
6937
                        $propArr = $this->getRecordPropertiesFromRow($table, $newRow);
6938
                        $isOfflineVersion = (bool)($newRow['t3ver_oid'] ?? 0);
6939
                        $this->log($table, $id, SystemLogDatabaseAction::UPDATE, $propArr['pid'], SystemLogErrorClassification::MESSAGE, 'Record \'%s\' (%s) was updated.' . ($isOfflineVersion ? ' (Offline version).' : ' (Online).'), 10, [$propArr['header'], $table . ':' . $id, 'history' => $historyEntryId], $propArr['event_pid']);
6940
                    }
6941
                    // Clear cache for relevant pages:
6942
                    $this->registerRecordIdForPageCacheClearing($table, $id);
6943
                    // Unset the pageCache for the id if table was page.
6944
                    if ($table === 'pages') {
6945
                        unset($this->pageCache[$id]);
6946
                    }
6947
                } else {
6948
                    $this->log($table, $id, SystemLogDatabaseAction::UPDATE, 0, SystemLogErrorClassification::SYSTEM_ERROR, 'SQL error: \'%s\' (%s)', 12, [$updateErrorMessage, $table . ':' . $id]);
6949
                }
6950
            }
6951
        }
6952
    }
6953
6954
    /**
6955
     * Insert into database
6956
     * Does not check permissions but expects them to be verified on beforehand
6957
     *
6958
     * @param string $table Record table name
6959
     * @param string $id "NEW...." uid string
6960
     * @param array $fieldArray Array of field=>value pairs to insert. FIELDS MUST MATCH the database FIELDS. No check is done. "pid" must point to the destination of the record!
6961
     * @param bool $newVersion Set to TRUE if new version is created.
6962
     * @param int $suggestedUid Suggested UID value for the inserted record. See the array $this->suggestedInsertUids; Admin-only feature
6963
     * @param bool $dontSetNewIdIndex If TRUE, the ->substNEWwithIDs array is not updated. Only useful in very rare circumstances!
6964
     * @return int|null Returns ID on success.
6965
     * @internal should only be used from within DataHandler
6966
     */
6967
    public function insertDB($table, $id, $fieldArray, $newVersion = false, $suggestedUid = 0, $dontSetNewIdIndex = false)
6968
    {
6969
        if (is_array($fieldArray) && is_array($GLOBALS['TCA'][$table]) && isset($fieldArray['pid'])) {
6970
            // Do NOT insert the UID field, ever!
6971
            unset($fieldArray['uid']);
6972
            if (!empty($fieldArray)) {
6973
                // Check for "suggestedUid".
6974
                // This feature is used by the import functionality to force a new record to have a certain UID value.
6975
                // This is only recommended for use when the destination server is a passive mirror of another server.
6976
                // As a security measure this feature is available only for Admin Users (for now)
6977
                $suggestedUid = (int)$suggestedUid;
6978
                if ($this->BE_USER->isAdmin() && $suggestedUid && $this->suggestedInsertUids[$table . ':' . $suggestedUid]) {
6979
                    // When the value of ->suggestedInsertUids[...] is "DELETE" it will try to remove the previous record
6980
                    if ($this->suggestedInsertUids[$table . ':' . $suggestedUid] === 'DELETE') {
6981
                        $this->hardDeleteSingleRecord($table, (int)$suggestedUid);
6982
                    }
6983
                    $fieldArray['uid'] = $suggestedUid;
6984
                }
6985
                $fieldArray = $this->insertUpdateDB_preprocessBasedOnFieldType($table, $fieldArray);
6986
                $typeArray = [];
6987
                if (!empty($GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField'])
6988
                    && array_key_exists($GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField'], $fieldArray)
6989
                ) {
6990
                    $typeArray[$GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField']] = Connection::PARAM_LOB;
6991
                }
6992
                $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
6993
                $insertErrorMessage = '';
6994
                try {
6995
                    // Execute the INSERT query:
6996
                    $connection->insert(
6997
                        $table,
6998
                        $fieldArray,
6999
                        $typeArray
7000
                    );
7001
                } catch (DBALException $e) {
7002
                    $insertErrorMessage = $e->getPrevious()->getMessage();
7003
                }
7004
                // If succees, do...:
7005
                if ($insertErrorMessage === '') {
7006
                    // Set mapping for NEW... -> real uid:
7007
                    // the NEW_id now holds the 'NEW....' -id
7008
                    $NEW_id = $id;
7009
                    $id = $this->postProcessDatabaseInsert($connection, $table, $suggestedUid);
7010
7011
                    if (!$dontSetNewIdIndex) {
7012
                        $this->substNEWwithIDs[$NEW_id] = $id;
7013
                        $this->substNEWwithIDs_table[$NEW_id] = $table;
7014
                    }
7015
                    $newRow = [];
7016
                    if ($this->enableLogging) {
7017
                        // Checking the record is properly saved if configured
7018
                        if ($this->checkStoredRecords) {
7019
                            $newRow = $this->checkStoredRecord($table, $id, $fieldArray, SystemLogDatabaseAction::INSERT);
7020
                        } else {
7021
                            $newRow = $fieldArray;
7022
                            $newRow['uid'] = $id;
7023
                        }
7024
                    }
7025
                    // Update reference index:
7026
                    $this->updateRefIndex($table, $id);
7027
7028
                    // Store in history
7029
                    $this->getRecordHistoryStore()->addRecord($table, $id, $newRow, $this->correlationId);
0 ignored issues
show
Bug introduced by
It seems like $newRow can also be of type null; however, parameter $payload of TYPO3\CMS\Core\DataHandl...storyStore::addRecord() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

7029
                    $this->getRecordHistoryStore()->addRecord($table, $id, /** @scrutinizer ignore-type */ $newRow, $this->correlationId);
Loading history...
7030
7031
                    if ($newVersion) {
7032
                        if ($this->enableLogging) {
7033
                            $propArr = $this->getRecordPropertiesFromRow($table, $newRow);
7034
                            $this->log($table, $id, SystemLogDatabaseAction::INSERT, 0, SystemLogErrorClassification::MESSAGE, 'New version created of table \'%s\', uid \'%s\'. UID of new version is \'%s\'', 10, [$table, $fieldArray['t3ver_oid'], $id], $propArr['event_pid'], $NEW_id);
7035
                        }
7036
                    } else {
7037
                        if ($this->enableLogging) {
7038
                            $propArr = $this->getRecordPropertiesFromRow($table, $newRow);
7039
                            $page_propArr = $this->getRecordProperties('pages', $propArr['pid']);
7040
                            $this->log($table, $id, SystemLogDatabaseAction::INSERT, 0, SystemLogErrorClassification::MESSAGE, 'Record \'%s\' (%s) was inserted on page \'%s\' (%s)', 10, [$propArr['header'], $table . ':' . $id, $page_propArr['header'], $newRow['pid']], $newRow['pid'], $NEW_id);
7041
                        }
7042
                        // Clear cache for relevant pages:
7043
                        $this->registerRecordIdForPageCacheClearing($table, $id);
7044
                    }
7045
                    return $id;
7046
                }
7047
                if ($this->enableLogging) {
7048
                    $this->log($table, $id, SystemLogDatabaseAction::INSERT, 0, SystemLogErrorClassification::SYSTEM_ERROR, 'SQL error: \'%s\' (%s)', 12, [$insertErrorMessage, $table . ':' . $id]);
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $recuid of TYPO3\CMS\Core\DataHandling\DataHandler::log(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

7048
                    $this->log($table, /** @scrutinizer ignore-type */ $id, SystemLogDatabaseAction::INSERT, 0, SystemLogErrorClassification::SYSTEM_ERROR, 'SQL error: \'%s\' (%s)', 12, [$insertErrorMessage, $table . ':' . $id]);
Loading history...
7049
                }
7050
            }
7051
        }
7052
        return null;
7053
    }
7054
7055
    /**
7056
     * Checking stored record to see if the written values are properly updated.
7057
     *
7058
     * @param string $table Record table name
7059
     * @param int $id Record uid
7060
     * @param array $fieldArray Array of field=>value pairs to insert/update
7061
     * @param string $action Action, for logging only.
7062
     * @return array|null Selected row
7063
     * @see insertDB()
7064
     * @see updateDB()
7065
     * @internal should only be used from within DataHandler
7066
     */
7067
    public function checkStoredRecord($table, $id, $fieldArray, $action)
7068
    {
7069
        $id = (int)$id;
7070
        if (is_array($GLOBALS['TCA'][$table]) && $id) {
7071
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
7072
            $queryBuilder->getRestrictions()->removeAll();
7073
7074
            $row = $queryBuilder
7075
                ->select('*')
7076
                ->from($table)
7077
                ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)))
7078
                ->execute()
7079
                ->fetch();
7080
7081
            if (!empty($row)) {
7082
                // Traverse array of values that was inserted into the database and compare with the actually stored value:
7083
                $errors = [];
7084
                foreach ($fieldArray as $key => $value) {
7085
                    if (!$this->checkStoredRecords_loose || $value || $row[$key]) {
7086
                        if (is_float($row[$key])) {
7087
                            // if the database returns the value as double, compare it as double
7088
                            if ((double)$value !== (double)$row[$key]) {
7089
                                $errors[] = $key;
7090
                            }
7091
                        } else {
7092
                            $dbType = $GLOBALS['TCA'][$table]['columns'][$key]['config']['dbType'] ?? false;
7093
                            if ($dbType === 'datetime' || $dbType === 'time') {
7094
                                $row[$key] = $this->normalizeTimeFormat($table, $row[$key], $dbType);
7095
                            }
7096
                            if ((string)$value !== (string)$row[$key]) {
7097
                                // The is_numeric check catches cases where we want to store a float/double value
7098
                                // and database returns the field as a string with the least required amount of
7099
                                // significant digits, i.e. "0.00" being saved and "0" being read back.
7100
                                if (is_numeric($value) && is_numeric($row[$key])) {
7101
                                    if ((double)$value === (double)$row[$key]) {
7102
                                        continue;
7103
                                    }
7104
                                }
7105
                                $errors[] = $key;
7106
                            }
7107
                        }
7108
                    }
7109
                }
7110
                // Set log message if there were fields with unmatching values:
7111
                if (!empty($errors)) {
7112
                    $message = sprintf(
7113
                        'These fields of record %d in table "%s" have not been saved correctly: %s! The values might have changed due to type casting of the database.',
7114
                        $id,
7115
                        $table,
7116
                        implode(', ', $errors)
7117
                    );
7118
                    $this->log($table, $id, $action, 0, SystemLogErrorClassification::USER_ERROR, $message);
0 ignored issues
show
Bug introduced by
$action of type string is incompatible with the type integer expected by parameter $action of TYPO3\CMS\Core\DataHandling\DataHandler::log(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

7118
                    $this->log($table, $id, /** @scrutinizer ignore-type */ $action, 0, SystemLogErrorClassification::USER_ERROR, $message);
Loading history...
7119
                }
7120
                // Return selected rows:
7121
                return $row;
7122
            }
7123
        }
7124
        return null;
7125
    }
7126
7127
    /**
7128
     * Setting sys_history record, based on content previously set in $this->historyRecords[$table . ':' . $id] (by compareFieldArrayWithCurrentAndUnset())
7129
     *
7130
     * This functionality is now moved into the RecordHistoryStore and can be used instead.
7131
     *
7132
     * @param string $table Table name
7133
     * @param int $id Record ID
7134
     * @internal should only be used from within DataHandler
7135
     */
7136
    public function setHistory($table, $id)
7137
    {
7138
        if (isset($this->historyRecords[$table . ':' . $id])) {
7139
            $this->getRecordHistoryStore()->modifyRecord(
7140
                $table,
7141
                $id,
7142
                $this->historyRecords[$table . ':' . $id],
7143
                $this->correlationId
7144
            );
7145
        }
7146
    }
7147
7148
    /**
7149
     * @return RecordHistoryStore
7150
     */
7151
    protected function getRecordHistoryStore(): RecordHistoryStore
7152
    {
7153
        return GeneralUtility::makeInstance(
7154
            RecordHistoryStore::class,
7155
            RecordHistoryStore::USER_BACKEND,
7156
            $this->BE_USER->user['uid'],
7157
            (int)$this->BE_USER->getOriginalUserIdWhenInSwitchUserMode(),
7158
            $GLOBALS['EXEC_TIME'],
7159
            $this->BE_USER->workspace
7160
        );
7161
    }
7162
7163
    /**
7164
     * Register a table/uid combination in current user workspace for reference updating.
7165
     * Should be called on almost any update to a record which could affect references inside the record.
7166
     *
7167
     * @param string $table Table name
7168
     * @param int $uid Record UID
7169
     * @param int $workspace Workspace the record lives in
7170
     * @internal should only be used from within DataHandler
7171
     */
7172
    public function updateRefIndex($table, $uid, int $workspace = null): void
7173
    {
7174
        if ($workspace === null) {
7175
            $workspace = (int)$this->BE_USER->workspace;
7176
        }
7177
        $this->referenceIndexUpdater->registerForUpdate((string)$table, (int)$uid, $workspace);
7178
    }
7179
7180
    /**
7181
     * Delete rows from sys_refindex a table / uid combination is involved in:
7182
     * Either on left side (tablename + recuid) OR right side (ref_table + ref_uid).
7183
     * Useful in scenarios like workspace-discard where parents or children are hard deleted: The
7184
     * expensive updateRefIndex() does not need to be called since we can just drop straight ahead.
7185
     *
7186
     * @param string $table Table name, used as tablename and ref_table
7187
     * @param int $uid Record uid, used as recuid and ref_uid
7188
     * @param int $workspace Workspace the record lives in
7189
     */
7190
    public function registerReferenceIndexRowsForDrop(string $table, int $uid, int $workspace): void
7191
    {
7192
        $this->referenceIndexUpdater->registerForDrop($table, $uid, $workspace);
7193
    }
7194
7195
    /*********************************************
7196
     *
7197
     * Misc functions
7198
     *
7199
     ********************************************/
7200
    /**
7201
     * Returning sorting number for tables with a "sortby" column
7202
     * Using when new records are created and existing records are moved around.
7203
     *
7204
     * The strategy is:
7205
     *  - if no record exists: set interval as sorting number
7206
     *  - if inserted before an element: put in the middle of the existing elements
7207
     *  - if inserted behind the last element: add interval to last sorting number
7208
     *  - if collision: move all subsequent records by 2 * interval, insert new record with collision + interval
7209
     *
7210
     * How to calculate the maximum possible inserts for the worst case of adding all records to the top,
7211
     * such that the sorting number stays within INT_MAX
7212
     *
7213
     * i = interval (currently 256)
7214
     * c = number of inserts until collision
7215
     * s = max sorting number to reach (INT_MAX - 32bit)
7216
     * n = number of records (~83 million)
7217
     *
7218
     * c = 2 * g
7219
     * g = log2(i) / 2 + 1
7220
     * n = g * s / i - g + 1
7221
     *
7222
     * The algorithm can be tuned by adjusting the interval value.
7223
     * Higher value means less collisions, but also less inserts are possible to stay within INT_MAX.
7224
     *
7225
     * @param string $table Table name
7226
     * @param int $uid Uid of record to find sorting number for. May be zero in case of new.
7227
     * @param int $pid Positioning PID, either >=0 (pointing to page in which case we find sorting number for first record in page) or <0 (pointing to record in which case to find next sorting number after this record)
7228
     * @return int|array|bool|null Returns integer if PID is >=0, otherwise an array with PID and sorting number. Possibly FALSE in case of error.
7229
     * @internal should only be used from within DataHandler
7230
     */
7231
    public function getSortNumber($table, $uid, $pid)
7232
    {
7233
        $sortColumn = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ?? '';
7234
        if (!$sortColumn) {
7235
            return null;
7236
        }
7237
7238
        $considerWorkspaces = BackendUtility::isTableWorkspaceEnabled($table);
7239
        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
7240
        $queryBuilder = $connectionPool->getQueryBuilderForTable($table);
7241
        $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
7242
7243
        $queryBuilder
7244
            ->select($sortColumn, 'pid', 'uid')
7245
            ->from($table);
7246
        if ($considerWorkspaces) {
7247
            $queryBuilder->addSelect('t3ver_state');
7248
        }
7249
7250
        // find and return the sorting value for the first record on that pid
7251
        if ($pid >= 0) {
7252
            // Fetches the first record (lowest sorting) under this pid
7253
            $queryBuilder
7254
                ->where($queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)));
7255
7256
            if ($considerWorkspaces) {
7257
                $queryBuilder->andWhere(
7258
                    $queryBuilder->expr()->orX(
7259
                        $queryBuilder->expr()->eq('t3ver_oid', 0),
7260
                        $queryBuilder->expr()->eq('t3ver_state', VersionState::MOVE_POINTER)
7261
                    )
7262
                );
7263
            }
7264
            $row = $queryBuilder
7265
                ->orderBy($sortColumn, 'ASC')
7266
                ->addOrderBy('uid', 'ASC')
7267
                ->setMaxResults(1)
7268
                ->execute()
7269
                ->fetch();
7270
7271
            if (!empty($row)) {
7272
                // The top record was the record itself, so we return its current sorting value
7273
                if ($row['uid'] == $uid) {
7274
                    return $row[$sortColumn];
7275
                }
7276
                // If the record sorting value < 1 we must resort all the records under this pid
7277
                if ($row[$sortColumn] < 1) {
7278
                    $this->increaseSortingOfFollowingRecords($table, (int)$pid);
7279
                    // Lowest sorting value after full resorting is $sortIntervals
7280
                    return $this->sortIntervals;
7281
                }
7282
                // Sorting number between current top element and zero
7283
                return floor($row[$sortColumn] / 2);
7284
            }
7285
            // No records, so we choose the default value as sorting-number
7286
            return $this->sortIntervals;
7287
        }
7288
7289
        // Find and return first possible sorting value AFTER record with given uid ($pid)
7290
        // Fetches the record which is supposed to be the prev record
7291
        $row = $queryBuilder
7292
                ->where($queryBuilder->expr()->eq(
7293
                    'uid',
7294
                    $queryBuilder->createNamedParameter(abs($pid), \PDO::PARAM_INT)
7295
                ))
7296
                ->execute()
7297
                ->fetch();
7298
7299
        // There is a previous record
7300
        if (!empty($row)) {
7301
            // Look if the record UID happens to be a versioned record. If so, find its live version.
7302
            // If this is already a moved record in workspace, this is not needed
7303
            if ((int)$row['t3ver_state'] !== VersionState::MOVE_POINTER && $lookForLiveVersion = BackendUtility::getLiveVersionOfRecord($table, $row['uid'], $sortColumn . ',pid,uid')) {
7304
                $row = $lookForLiveVersion;
7305
            } elseif ($considerWorkspaces && $this->BE_USER->workspace > 0) {
7306
                // In case the previous record is moved in the workspace, we need to fetch the information from this specific record
7307
                $versionedRecord = BackendUtility::getWorkspaceVersionOfRecord($this->BE_USER->workspace, $table, $row['uid'], $sortColumn . ',pid,uid,t3ver_state');
7308
                if (is_array($versionedRecord) && (int)$versionedRecord['t3ver_state'] === VersionState::MOVE_POINTER) {
7309
                    $row = $versionedRecord;
7310
                }
7311
            }
7312
            // If the record should be inserted after itself, keep the current sorting information:
7313
            if ((int)$row['uid'] === (int)$uid) {
7314
                $sortNumber = $row[$sortColumn];
7315
            } else {
7316
                $queryBuilder = $connectionPool->getQueryBuilderForTable($table);
7317
                $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
7318
7319
                $queryBuilder
7320
                        ->select($sortColumn, 'pid', 'uid')
7321
                        ->from($table)
7322
                        ->where(
7323
                            $queryBuilder->expr()->eq(
7324
                                'pid',
7325
                                $queryBuilder->createNamedParameter($row['pid'], \PDO::PARAM_INT)
7326
                            ),
7327
                            $queryBuilder->expr()->gte(
7328
                                $sortColumn,
7329
                                $queryBuilder->createNamedParameter($row[$sortColumn], \PDO::PARAM_INT)
7330
                            )
7331
                        )
7332
                        ->orderBy($sortColumn, 'ASC')
7333
                        ->addOrderBy('uid', 'DESC')
7334
                        ->setMaxResults(2);
7335
7336
                if ($considerWorkspaces) {
7337
                    $queryBuilder->andWhere(
7338
                        $queryBuilder->expr()->orX(
7339
                            $queryBuilder->expr()->eq('t3ver_oid', 0),
7340
                            $queryBuilder->expr()->eq('t3ver_state', VersionState::MOVE_POINTER)
7341
                        )
7342
                    );
7343
                }
7344
7345
                $subResults = $queryBuilder
7346
                    ->execute()
7347
                    ->fetchAll();
7348
                // Fetches the next record in order to calculate the in-between sortNumber
7349
                // There was a record afterwards
7350
                if (count($subResults) === 2) {
7351
                    // There was a record afterwards, fetch that
7352
                    $subrow = array_pop($subResults);
7353
                    // The sortNumber is found in between these values
7354
                    $sortNumber = $row[$sortColumn] + floor(($subrow[$sortColumn] - $row[$sortColumn]) / 2);
7355
                    // The sortNumber happened NOT to be between the two surrounding numbers, so we'll have to resort the list
7356
                    if ($sortNumber <= $row[$sortColumn] || $sortNumber >= $subrow[$sortColumn]) {
7357
                        $this->increaseSortingOfFollowingRecords($table, (int)$row['pid'], (int)$row[$sortColumn]);
7358
                        $sortNumber = $row[$sortColumn] + $this->sortIntervals;
7359
                    }
7360
                } else {
7361
                    // If after the last record in the list, we just add the sortInterval to the last sortvalue
7362
                    $sortNumber = $row[$sortColumn] + $this->sortIntervals;
7363
                }
7364
            }
7365
            return ['pid' => $row['pid'], 'sortNumber' => $sortNumber];
7366
        }
7367
        if ($this->enableLogging) {
7368
            $propArr = $this->getRecordProperties($table, $uid);
7369
            // OK, don't insert $propArr['event_pid'] here...
7370
            $this->log($table, $uid, SystemLogDatabaseAction::MOVE, 0, SystemLogErrorClassification::USER_ERROR, 'Attempt to move record \'%s\' (%s) to after a non-existing record (uid=%s)', 1, [$propArr['header'], $table . ':' . $uid, abs($pid)], $propArr['pid']);
7371
        }
7372
        // There MUST be a previous record or else this cannot work
7373
        return false;
7374
    }
7375
7376
    /**
7377
     * Increases sorting field value of all records with sorting higher than $sortingValue
7378
     *
7379
     * Used internally by getSortNumber() to "make space" in sorting values when inserting new record
7380
     *
7381
     * @param string $table Table name
7382
     * @param int $pid Page Uid in which to resort records
7383
     * @param int $sortingValue All sorting numbers larger than this number will be shifted
7384
     * @see getSortNumber()
7385
     */
7386
    protected function increaseSortingOfFollowingRecords(string $table, int $pid, int $sortingValue = null): void
7387
    {
7388
        $sortBy = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ?? '';
7389
        if ($sortBy) {
7390
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
7391
7392
            $queryBuilder
7393
                ->update($table)
7394
                ->where($queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)))
7395
                ->set($sortBy, $queryBuilder->quoteIdentifier($sortBy) . ' + ' . $this->sortIntervals . ' + ' . $this->sortIntervals, false);
7396
            if ($sortingValue !== null) {
7397
                $queryBuilder->andWhere($queryBuilder->expr()->gt($sortBy, $sortingValue));
7398
            }
7399
            if (BackendUtility::isTableWorkspaceEnabled($table)) {
7400
                $queryBuilder
7401
                    ->andWhere(
7402
                        $queryBuilder->expr()->eq('t3ver_oid', 0)
7403
                    );
7404
            }
7405
7406
            $deleteColumn = $GLOBALS['TCA'][$table]['ctrl']['delete'] ?? '';
7407
            if ($deleteColumn) {
7408
                $queryBuilder->andWhere($queryBuilder->expr()->eq($deleteColumn, 0));
7409
            }
7410
7411
            $queryBuilder->execute();
7412
        }
7413
    }
7414
7415
    /**
7416
     * Returning uid of previous localized record, if any, for tables with a "sortby" column
7417
     * Used when new localized records are created so that localized records are sorted in the same order as the default language records
7418
     *
7419
     * For a given record (A) uid (record we're translating) it finds first default language record (from the same colpos)
7420
     * with sorting smaller than given record (B).
7421
     * Then it fetches a translated version of record B and returns it's uid.
7422
     *
7423
     * If there is no record B, or it has no translation in given language, the record A uid is returned.
7424
     * The localized record will be placed the after record which uid is returned.
7425
     *
7426
     * @param string $table Table name
7427
     * @param int $uid Uid of default language record
7428
     * @param int $pid Pid of default language record
7429
     * @param int $language Language of localization
7430
     * @return int uid of record after which the localized record should be inserted
7431
     */
7432
    protected function getPreviousLocalizedRecordUid($table, $uid, $pid, $language)
7433
    {
7434
        $previousLocalizedRecordUid = $uid;
7435
        $sortColumn = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ?? '';
7436
        if ($sortColumn) {
7437
            $select = [$sortColumn, 'pid', 'uid'];
7438
            // For content elements, we also need the colPos
7439
            if ($table === 'tt_content') {
7440
                $select[] = 'colPos';
7441
            }
7442
            // Get the sort value of the default language record
7443
            $row = BackendUtility::getRecord($table, $uid, implode(',', $select));
7444
            if (is_array($row)) {
7445
                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
7446
                $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
7447
7448
                $queryBuilder
7449
                    ->select(...$select)
7450
                    ->from($table)
7451
                    ->where(
7452
                        $queryBuilder->expr()->eq(
7453
                            'pid',
7454
                            $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)
7455
                        ),
7456
                        $queryBuilder->expr()->eq(
7457
                            $GLOBALS['TCA'][$table]['ctrl']['languageField'],
7458
                            $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
7459
                        ),
7460
                        $queryBuilder->expr()->lt(
7461
                            $sortColumn,
7462
                            $queryBuilder->createNamedParameter($row[$sortColumn], \PDO::PARAM_INT)
7463
                        )
7464
                    )
7465
                    ->orderBy($sortColumn, 'DESC')
7466
                    ->addOrderBy('uid', 'DESC')
7467
                    ->setMaxResults(1);
7468
                if ($table === 'tt_content') {
7469
                    $queryBuilder
7470
                        ->andWhere(
7471
                            $queryBuilder->expr()->eq(
7472
                                'colPos',
7473
                                $queryBuilder->createNamedParameter($row['colPos'], \PDO::PARAM_INT)
7474
                            )
7475
                        );
7476
                }
7477
                // If there is an element, find its localized record in specified localization language
7478
                if ($previousRow = $queryBuilder->execute()->fetch()) {
7479
                    $previousLocalizedRecord = BackendUtility::getRecordLocalization($table, $previousRow['uid'], $language);
7480
                    if (is_array($previousLocalizedRecord[0])) {
7481
                        $previousLocalizedRecordUid = $previousLocalizedRecord[0]['uid'];
7482
                    }
7483
                }
7484
            }
7485
        }
7486
        return $previousLocalizedRecordUid;
7487
    }
7488
7489
    /**
7490
     * Returns a fieldArray with default values. Values will be picked up from the TCA array looking at the config key "default" for each column. If values are set in ->defaultValues they will overrule though.
7491
     * Used for new records and during copy operations for defaults
7492
     *
7493
     * @param string $table Table name for which to set default values.
7494
     * @return array Array with default values.
7495
     * @internal should only be used from within DataHandler
7496
     */
7497
    public function newFieldArray($table)
7498
    {
7499
        $fieldArray = [];
7500
        if (is_array($GLOBALS['TCA'][$table]['columns'])) {
7501
            foreach ($GLOBALS['TCA'][$table]['columns'] as $field => $content) {
7502
                if (isset($this->defaultValues[$table][$field])) {
7503
                    $fieldArray[$field] = $this->defaultValues[$table][$field];
7504
                } elseif (isset($content['config']['default'])) {
7505
                    $fieldArray[$field] = $content['config']['default'];
7506
                }
7507
            }
7508
        }
7509
        return $fieldArray;
7510
    }
7511
7512
    /**
7513
     * If a "languageField" is specified for $table this function will add a possible value to the incoming array if none is found in there already.
7514
     *
7515
     * @param string $table Table name
7516
     * @param array $incomingFieldArray Incoming array (passed by reference)
7517
     * @internal should only be used from within DataHandler
7518
     */
7519
    public function addDefaultPermittedLanguageIfNotSet($table, &$incomingFieldArray)
7520
    {
7521
        // Checking languages:
7522
        if ($GLOBALS['TCA'][$table]['ctrl']['languageField']) {
7523
            if (!isset($incomingFieldArray[$GLOBALS['TCA'][$table]['ctrl']['languageField']])) {
7524
                // Language field must be found in input row - otherwise it does not make sense.
7525
                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
7526
                    ->getQueryBuilderForTable('sys_language');
7527
                $queryBuilder->getRestrictions()
7528
                    ->removeAll()
7529
                    ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
7530
                $queryBuilder
7531
                    ->select('uid')
7532
                    ->from('sys_language')
7533
                    ->where($queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)));
7534
                $rows = array_merge([['uid' => 0]], $queryBuilder->execute()->fetchAll(), [['uid' => -1]]);
7535
                foreach ($rows as $r) {
7536
                    if ($this->BE_USER->checkLanguageAccess($r['uid'])) {
7537
                        $incomingFieldArray[$GLOBALS['TCA'][$table]['ctrl']['languageField']] = $r['uid'];
7538
                        break;
7539
                    }
7540
                }
7541
            }
7542
        }
7543
    }
7544
7545
    /**
7546
     * Returns the $data array from $table overridden in the fields defined in ->overrideValues.
7547
     *
7548
     * @param string $table Table name
7549
     * @param array $data Data array with fields from table. These will be overlaid with values in $this->overrideValues[$table]
7550
     * @return array Data array, processed.
7551
     * @internal should only be used from within DataHandler
7552
     */
7553
    public function overrideFieldArray($table, $data)
7554
    {
7555
        if (is_array($this->overrideValues[$table])) {
7556
            $data = array_merge($data, $this->overrideValues[$table]);
7557
        }
7558
        return $data;
7559
    }
7560
7561
    /**
7562
     * Compares the incoming field array with the current record and unsets all fields which are the same.
7563
     * Used for existing records being updated
7564
     *
7565
     * @param string $table Record table name
7566
     * @param int $id Record uid
7567
     * @param array $fieldArray Array of field=>value pairs intended to be inserted into the database. All keys with values matching exactly the current value will be unset!
7568
     * @return array Returns $fieldArray. If the returned array is empty, then the record should not be updated!
7569
     * @internal should only be used from within DataHandler
7570
     */
7571
    public function compareFieldArrayWithCurrentAndUnset($table, $id, $fieldArray)
7572
    {
7573
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
7574
        $queryBuilder = $connection->createQueryBuilder();
7575
        $queryBuilder->getRestrictions()->removeAll();
7576
        $currentRecord = $queryBuilder->select('*')
7577
            ->from($table)
7578
            ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)))
7579
            ->execute()
7580
            ->fetch();
7581
        // If the current record exists (which it should...), begin comparison:
7582
        if (is_array($currentRecord)) {
7583
            $tableDetails = $connection->getSchemaManager()->listTableDetails($table);
7584
            $columnRecordTypes = [];
7585
            foreach ($currentRecord as $columnName => $_) {
7586
                $columnRecordTypes[$columnName] = '';
7587
                $type = $tableDetails->getColumn($columnName)->getType();
7588
                if ($type instanceof IntegerType) {
7589
                    $columnRecordTypes[$columnName] = 'int';
7590
                }
7591
            }
7592
            // Unset the fields which are similar:
7593
            foreach ($fieldArray as $col => $val) {
7594
                $fieldConfiguration = $GLOBALS['TCA'][$table]['columns'][$col]['config'];
7595
                $isNullField = (!empty($fieldConfiguration['eval']) && GeneralUtility::inList($fieldConfiguration['eval'], 'null'));
7596
7597
                // Unset fields if stored and submitted values are equal - except the current field holds MM relations.
7598
                // In general this avoids to store superfluous data which also will be visualized in the editing history.
7599
                if (!$fieldConfiguration['MM'] && $this->isSubmittedValueEqualToStoredValue($val, $currentRecord[$col], $columnRecordTypes[$col], $isNullField)) {
7600
                    unset($fieldArray[$col]);
7601
                } else {
7602
                    if (!isset($this->mmHistoryRecords[$table . ':' . $id]['oldRecord'][$col])) {
7603
                        $this->historyRecords[$table . ':' . $id]['oldRecord'][$col] = $currentRecord[$col];
7604
                    } elseif ($this->mmHistoryRecords[$table . ':' . $id]['oldRecord'][$col] != $this->mmHistoryRecords[$table . ':' . $id]['newRecord'][$col]) {
7605
                        $this->historyRecords[$table . ':' . $id]['oldRecord'][$col] = $this->mmHistoryRecords[$table . ':' . $id]['oldRecord'][$col];
7606
                    }
7607
                    if (!isset($this->mmHistoryRecords[$table . ':' . $id]['newRecord'][$col])) {
7608
                        $this->historyRecords[$table . ':' . $id]['newRecord'][$col] = $fieldArray[$col];
7609
                    } elseif ($this->mmHistoryRecords[$table . ':' . $id]['newRecord'][$col] != $this->mmHistoryRecords[$table . ':' . $id]['oldRecord'][$col]) {
7610
                        $this->historyRecords[$table . ':' . $id]['newRecord'][$col] = $this->mmHistoryRecords[$table . ':' . $id]['newRecord'][$col];
7611
                    }
7612
                }
7613
            }
7614
        } else {
7615
            // If the current record does not exist this is an error anyways and we just return an empty array here.
7616
            $fieldArray = [];
7617
        }
7618
        return $fieldArray;
7619
    }
7620
7621
    /**
7622
     * Determines whether submitted values and stored values are equal.
7623
     * This prevents from adding superfluous field changes which would be shown in the record history as well.
7624
     * For NULL fields (see accordant TCA definition 'eval' = 'null'), a special handling is required since
7625
     * (!strcmp(NULL, '')) would be a false-positive.
7626
     *
7627
     * @param mixed $submittedValue Value that has submitted (e.g. from a backend form)
7628
     * @param mixed $storedValue Value that is currently stored in the database
7629
     * @param string $storedType SQL type of the stored value column (see mysql_field_type(), e.g 'int', 'string',  ...)
7630
     * @param bool $allowNull Whether NULL values are allowed by accordant TCA definition ('eval' = 'null')
7631
     * @return bool Whether both values are considered to be equal
7632
     */
7633
    protected function isSubmittedValueEqualToStoredValue($submittedValue, $storedValue, $storedType, $allowNull = false)
7634
    {
7635
        // No NULL values are allowed, this is the regular behaviour.
7636
        // Thus, check whether strings are the same or whether integer values are empty ("0" or "").
7637
        if (!$allowNull) {
7638
            $result = (string)$submittedValue === (string)$storedValue || $storedType === 'int' && (int)$storedValue === (int)$submittedValue;
7639
        // Null values are allowed, but currently there's a real (not NULL) value.
7640
        // Thus, ensure no NULL value was submitted and fallback to the regular behaviour.
7641
        } elseif ($storedValue !== null) {
7642
            $result = (
7643
                $submittedValue !== null
7644
                && $this->isSubmittedValueEqualToStoredValue($submittedValue, $storedValue, $storedType, false)
7645
            );
7646
        // Null values are allowed, and currently there's a NULL value.
7647
        // Thus, check whether a NULL value was submitted.
7648
        } else {
7649
            $result = ($submittedValue === null);
7650
        }
7651
7652
        return $result;
7653
    }
7654
7655
    /**
7656
     * Converts a HTML entity (like &#123;) to the character '123'
7657
     *
7658
     * @param string $input Input string
7659
     * @return string Output string
7660
     * @internal should only be used from within DataHandler
7661
     */
7662
    public function convNumEntityToByteValue($input)
7663
    {
7664
        $token = md5(microtime());
7665
        $parts = explode($token, preg_replace('/(&#([0-9]+);)/', $token . '\\2' . $token, $input));
7666
        foreach ($parts as $k => $v) {
7667
            if ($k % 2) {
7668
                $v = (int)$v;
7669
                // Just to make sure that control bytes are not converted.
7670
                if ($v > 32) {
7671
                    $parts[$k] = chr($v);
7672
                }
7673
            }
7674
        }
7675
        return implode('', $parts);
7676
    }
7677
7678
    /**
7679
     * Disables the delete clause for fetching records.
7680
     * In general only undeleted records will be used. If the delete
7681
     * clause is disabled, also deleted records are taken into account.
7682
     */
7683
    public function disableDeleteClause()
7684
    {
7685
        $this->disableDeleteClause = true;
7686
    }
7687
7688
    /**
7689
     * Returns delete-clause for the $table
7690
     *
7691
     * @param string $table Table name
7692
     * @return string Delete clause
7693
     * @internal should only be used from within DataHandler
7694
     */
7695
    public function deleteClause($table)
7696
    {
7697
        // Returns the proper delete-clause if any for a table from TCA
7698
        if (!$this->disableDeleteClause && $GLOBALS['TCA'][$table]['ctrl']['delete']) {
7699
            return ' AND ' . $table . '.' . $GLOBALS['TCA'][$table]['ctrl']['delete'] . '=0';
7700
        }
7701
        return '';
7702
    }
7703
7704
    /**
7705
     * Add delete restriction if not disabled
7706
     *
7707
     * @param QueryRestrictionContainerInterface $restrictions
7708
     */
7709
    protected function addDeleteRestriction(QueryRestrictionContainerInterface $restrictions)
7710
    {
7711
        if (!$this->disableDeleteClause) {
7712
            $restrictions->add(GeneralUtility::makeInstance(DeletedRestriction::class));
7713
        }
7714
    }
7715
7716
    /**
7717
     * Gets UID of parent record. If record is deleted it will be looked up in
7718
     * an array built before the record was deleted
7719
     *
7720
     * @param string $table Table where record lives/lived
7721
     * @param int $uid Record UID
7722
     * @return int[] Parent UIDs
7723
     */
7724
    protected function getOriginalParentOfRecord($table, $uid)
7725
    {
7726
        if (isset(self::$recordPidsForDeletedRecords[$table][$uid])) {
7727
            return self::$recordPidsForDeletedRecords[$table][$uid];
7728
        }
7729
        [$parentUid] = BackendUtility::getTSCpid($table, $uid, '');
7730
        return [$parentUid];
7731
    }
7732
7733
    /**
7734
     * Extract entries from TSconfig for a specific table. This will merge specific and default configuration together.
7735
     *
7736
     * @param string $table Table name
7737
     * @param array $TSconfig TSconfig for page
7738
     * @return array TSconfig merged
7739
     * @internal should only be used from within DataHandler
7740
     */
7741
    public function getTableEntries($table, $TSconfig)
7742
    {
7743
        $tA = is_array($TSconfig['table.'][$table . '.']) ? $TSconfig['table.'][$table . '.'] : [];
7744
        $dA = is_array($TSconfig['default.']) ? $TSconfig['default.'] : [];
7745
        ArrayUtility::mergeRecursiveWithOverrule($dA, $tA);
7746
        return $dA;
7747
    }
7748
7749
    /**
7750
     * Returns the pid of a record from $table with $uid
7751
     *
7752
     * @param string $table Table name
7753
     * @param int $uid Record uid
7754
     * @return int|false PID value (unless the record did not exist in which case FALSE is returned)
7755
     * @internal should only be used from within DataHandler
7756
     */
7757
    public function getPID($table, $uid)
7758
    {
7759
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
7760
        $queryBuilder->getRestrictions()
7761
            ->removeAll();
7762
        $queryBuilder->select('pid')
7763
            ->from($table)
7764
            ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)));
7765
        if ($row = $queryBuilder->execute()->fetch()) {
7766
            return $row['pid'];
7767
        }
7768
        return false;
7769
    }
7770
7771
    /**
7772
     * Executing dbAnalysisStore
7773
     * This will save MM relations for new records but is executed after records are created because we need to know the ID of them
7774
     * @internal should only be used from within DataHandler
7775
     */
7776
    public function dbAnalysisStoreExec()
7777
    {
7778
        foreach ($this->dbAnalysisStore as $action) {
7779
            $id = BackendUtility::wsMapId($action[4], MathUtility::canBeInterpretedAsInteger($action[2]) ? $action[2] : $this->substNEWwithIDs[$action[2]]);
7780
            if ($id) {
7781
                $action[0]->writeMM($action[1], $id, $action[3]);
7782
            }
7783
        }
7784
    }
7785
7786
    /**
7787
     * Returns array, $CPtable, of pages under the $pid going down to $counter levels.
7788
     * Selecting ONLY pages which the user has read-access to!
7789
     *
7790
     * @param array $CPtable Accumulation of page uid=>pid pairs in branch of $pid
7791
     * @param int $pid Page ID for which to find subpages
7792
     * @param int $counter Number of levels to go down.
7793
     * @param int $rootID ID of root point for new copied branch: The idea seems to be that a copy is not made of the already new page!
7794
     * @return array Return array.
7795
     * @internal should only be used from within DataHandler
7796
     */
7797
    public function int_pageTreeInfo($CPtable, $pid, $counter, $rootID)
7798
    {
7799
        if ($counter) {
7800
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
7801
            $restrictions = $queryBuilder->getRestrictions()->removeAll();
7802
            $this->addDeleteRestriction($restrictions);
7803
            $queryBuilder
7804
                ->select('uid')
7805
                ->from('pages')
7806
                ->where($queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)))
7807
                ->orderBy('sorting', 'DESC');
7808
            if (!$this->admin) {
7809
                $queryBuilder->andWhere($this->BE_USER->getPagePermsClause(Permission::PAGE_SHOW));
7810
            }
7811
            if ((int)$this->BE_USER->workspace === 0) {
7812
                $queryBuilder->andWhere(
7813
                    $queryBuilder->expr()->eq('t3ver_wsid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
7814
                );
7815
            } else {
7816
                $queryBuilder->andWhere($queryBuilder->expr()->in(
7817
                    't3ver_wsid',
7818
                    $queryBuilder->createNamedParameter([0, $this->BE_USER->workspace], Connection::PARAM_INT_ARRAY)
7819
                ));
7820
            }
7821
            $result = $queryBuilder->execute();
7822
7823
            $pages = [];
7824
            while ($row = $result->fetch()) {
7825
                $pages[$row['uid']] = $row;
7826
            }
7827
7828
            // Resolve placeholders of workspace versions
7829
            if (!empty($pages) && (int)$this->BE_USER->workspace !== 0) {
7830
                $pages = array_reverse(
7831
                    $this->resolveVersionedRecords(
7832
                        'pages',
7833
                        'uid',
7834
                        'sorting',
7835
                        array_keys($pages)
7836
                    ),
7837
                    true
7838
                );
7839
            }
7840
7841
            foreach ($pages as $page) {
7842
                if ($page['uid'] != $rootID) {
7843
                    $CPtable[$page['uid']] = $pid;
7844
                    // If the uid is NOT the rootID of the copyaction and if we are supposed to walk further down
7845
                    if ($counter - 1) {
7846
                        $CPtable = $this->int_pageTreeInfo($CPtable, $page['uid'], $counter - 1, $rootID);
7847
                    }
7848
                }
7849
            }
7850
        }
7851
        return $CPtable;
7852
    }
7853
7854
    /**
7855
     * List of all tables (those administrators has access to = array_keys of $GLOBALS['TCA'])
7856
     *
7857
     * @return array Array of all TCA table names
7858
     * @internal should only be used from within DataHandler
7859
     */
7860
    public function compileAdminTables()
7861
    {
7862
        return array_keys($GLOBALS['TCA']);
7863
    }
7864
7865
    /**
7866
     * Checks if any uniqueInPid eval input fields are in the record and if so, they are re-written to be correct.
7867
     *
7868
     * @param string $table Table name
7869
     * @param int $uid Record UID
7870
     * @internal should only be used from within DataHandler
7871
     */
7872
    public function fixUniqueInPid($table, $uid)
7873
    {
7874
        if (empty($GLOBALS['TCA'][$table])) {
7875
            return;
7876
        }
7877
7878
        $curData = $this->recordInfo($table, $uid, '*');
7879
        $newData = [];
7880
        foreach ($GLOBALS['TCA'][$table]['columns'] as $field => $conf) {
7881
            if ($conf['config']['type'] === 'input' && (string)$curData[$field] !== '') {
7882
                $evalCodesArray = GeneralUtility::trimExplode(',', $conf['config']['eval'], true);
7883
                if (in_array('uniqueInPid', $evalCodesArray, true)) {
7884
                    $newV = $this->getUnique($table, $field, $curData[$field], $uid, $curData['pid']);
7885
                    if ((string)$newV !== (string)$curData[$field]) {
7886
                        $newData[$field] = $newV;
7887
                    }
7888
                }
7889
            }
7890
        }
7891
        // IF there are changed fields, then update the database
7892
        if (!empty($newData)) {
7893
            $this->updateDB($table, $uid, $newData);
7894
        }
7895
    }
7896
7897
    /**
7898
     * Checks if any uniqueInSite eval fields are in the record and if so, they are re-written to be correct.
7899
     *
7900
     * @param string $table Table name
7901
     * @param int $uid Record UID
7902
     * @return bool whether the record had to be fixed or not
7903
     */
7904
    protected function fixUniqueInSite(string $table, int $uid): bool
7905
    {
7906
        $curData = $this->recordInfo($table, $uid, '*');
7907
        $workspaceId = $this->BE_USER->workspace;
7908
        $newData = [];
7909
        foreach ($GLOBALS['TCA'][$table]['columns'] as $field => $conf) {
7910
            if ($conf['config']['type'] === 'slug' && (string)$curData[$field] !== '') {
7911
                $evalCodesArray = GeneralUtility::trimExplode(',', $conf['config']['eval'], true);
7912
                if (in_array('uniqueInSite', $evalCodesArray, true)) {
7913
                    $helper = GeneralUtility::makeInstance(SlugHelper::class, $table, $field, $conf['config'], $workspaceId);
7914
                    $state = RecordStateFactory::forName($table)->fromArray($curData);
0 ignored issues
show
Bug introduced by
It seems like $curData can also be of type null; however, parameter $data of TYPO3\CMS\Core\DataHandl...ateFactory::fromArray() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

7914
                    $state = RecordStateFactory::forName($table)->fromArray(/** @scrutinizer ignore-type */ $curData);
Loading history...
7915
                    $newValue = $helper->buildSlugForUniqueInSite($curData[$field], $state);
7916
                    if ((string)$newValue !== (string)$curData[$field]) {
7917
                        $newData[$field] = $newValue;
7918
                    }
7919
                }
7920
            }
7921
        }
7922
        // IF there are changed fields, then update the database
7923
        if (!empty($newData)) {
7924
            $this->updateDB($table, $uid, $newData);
7925
            return true;
7926
        }
7927
        return false;
7928
    }
7929
7930
    /**
7931
     * Check if there are subpages that need an adoption as well
7932
     * @param int $pageId
7933
     */
7934
    protected function fixUniqueInSiteForSubpages(int $pageId)
7935
    {
7936
        // Get ALL subpages to update - read-permissions are respected
7937
        $subPages = $this->int_pageTreeInfo([], $pageId, 99, $pageId);
7938
        // Now fix uniqueInSite for subpages
7939
        foreach ($subPages as $thePageUid => $thePagePid) {
7940
            $recordWasModified = $this->fixUniqueInSite('pages', $thePageUid);
7941
            if ($recordWasModified) {
7942
                // @todo: Add logging and history - but how? we don't know the data that was in the system before
7943
            }
7944
        }
7945
    }
7946
7947
    /**
7948
     * When er record is copied you can specify fields from the previous record which should be copied into the new one
7949
     * This function is also called with new elements. But then $update must be set to zero and $newData containing the data array. In that case data in the incoming array is NOT overridden. (250202)
7950
     *
7951
     * @param string $table Table name
7952
     * @param int $uid Record UID
7953
     * @param int $prevUid UID of previous record
7954
     * @param bool $update If set, updates the record
7955
     * @param array $newData Input array. If fields are already specified AND $update is not set, values are not set in output array.
7956
     * @return array Output array (For when the copying operation needs to get the information instead of updating the info)
7957
     * @internal should only be used from within DataHandler
7958
     */
7959
    public function fixCopyAfterDuplFields($table, $uid, $prevUid, $update, $newData = [])
7960
    {
7961
        if ($GLOBALS['TCA'][$table] && $GLOBALS['TCA'][$table]['ctrl']['copyAfterDuplFields']) {
7962
            $prevData = $this->recordInfo($table, $prevUid, '*');
7963
            $theFields = GeneralUtility::trimExplode(',', $GLOBALS['TCA'][$table]['ctrl']['copyAfterDuplFields'], true);
7964
            foreach ($theFields as $field) {
7965
                if ($GLOBALS['TCA'][$table]['columns'][$field] && ($update || !isset($newData[$field]))) {
7966
                    $newData[$field] = $prevData[$field];
7967
                }
7968
            }
7969
            if ($update && !empty($newData)) {
7970
                $this->updateDB($table, $uid, $newData);
7971
            }
7972
        }
7973
        return $newData;
7974
    }
7975
7976
    /**
7977
     * Casts a reference value. In case MM relations or foreign_field
7978
     * references are used. All other configurations, as well as
7979
     * foreign_table(!) could be stored as comma-separated-values
7980
     * as well. Since the system is not able to determine the default
7981
     * value automatically then, the TCA default value is used if
7982
     * it has been defined.
7983
     *
7984
     * @param int|string $value The value to be casted (e.g. '', '0', '1,2,3')
7985
     * @param array $configuration The TCA configuration of the accordant field
7986
     * @return int|string
7987
     */
7988
    protected function castReferenceValue($value, array $configuration)
7989
    {
7990
        if ((string)$value !== '') {
7991
            return $value;
7992
        }
7993
7994
        if (!empty($configuration['MM']) || !empty($configuration['foreign_field'])) {
7995
            return 0;
7996
        }
7997
7998
        if (array_key_exists('default', $configuration)) {
7999
            return $configuration['default'];
8000
        }
8001
8002
        return $value;
8003
    }
8004
8005
    /**
8006
     * Returns TRUE if the TCA/columns field type is a DB reference field
8007
     *
8008
     * @param array $conf Config array for TCA/columns field
8009
     * @return bool TRUE if DB reference field (group/db or select with foreign-table)
8010
     * @internal should only be used from within DataHandler
8011
     */
8012
    public function isReferenceField($conf)
8013
    {
8014
        return $conf['type'] === 'group' && $conf['internal_type'] === 'db' || $conf['type'] === 'select' && $conf['foreign_table'];
8015
    }
8016
8017
    /**
8018
     * Returns the subtype as a string of an inline field.
8019
     * If it's not an inline field at all, it returns FALSE.
8020
     *
8021
     * @param array $conf Config array for TCA/columns field
8022
     * @return string|bool string Inline subtype (field|mm|list), boolean: FALSE
8023
     * @internal should only be used from within DataHandler
8024
     */
8025
    public function getInlineFieldType($conf)
8026
    {
8027
        if ($conf['type'] !== 'inline' || !$conf['foreign_table']) {
8028
            return false;
8029
        }
8030
        if ($conf['foreign_field']) {
8031
            // The reference to the parent is stored in a pointer field in the child record
8032
            return 'field';
8033
        }
8034
        if ($conf['MM']) {
8035
            // Regular MM intermediate table is used to store data
8036
            return 'mm';
8037
        }
8038
        // An item list (separated by comma) is stored (like select type is doing)
8039
        return 'list';
8040
    }
8041
8042
    /**
8043
     * Get modified header for a copied record
8044
     *
8045
     * @param string $table Table name
8046
     * @param int $pid PID value in which other records to test might be
8047
     * @param string $field Field name to get header value for.
8048
     * @param string $value Current field value
8049
     * @param int $count Counter (number of recursions)
8050
     * @param string $prevTitle Previous title we checked for (in previous recursion)
8051
     * @return string The field value, possibly appended with a "copy label
8052
     * @internal should only be used from within DataHandler
8053
     */
8054
    public function getCopyHeader($table, $pid, $field, $value, $count, $prevTitle = '')
8055
    {
8056
        // Set title value to check for:
8057
        $checkTitle = $value;
8058
        if ($count > 0) {
8059
            $checkTitle = $value . rtrim(' ' . sprintf($this->prependLabel($table), $count));
8060
        }
8061
        // Do check:
8062
        if ($prevTitle != $checkTitle || $count < 100) {
8063
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
8064
            $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
8065
            $rowCount = $queryBuilder
8066
                ->count('uid')
8067
                ->from($table)
8068
                ->where(
8069
                    $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)),
8070
                    $queryBuilder->expr()->eq($field, $queryBuilder->createNamedParameter($checkTitle, \PDO::PARAM_STR))
8071
                )
8072
                ->execute()
8073
                ->fetchColumn(0);
8074
            if ($rowCount) {
8075
                return $this->getCopyHeader($table, $pid, $field, $value, $count + 1, $checkTitle);
8076
            }
8077
        }
8078
        // Default is to just return the current input title if no other was returned before:
8079
        return $checkTitle;
8080
    }
8081
8082
    /**
8083
     * Return "copy" label for a table. Although the name is "prepend" it actually APPENDs the label (after ...)
8084
     *
8085
     * @param string $table Table name
8086
     * @return string Label to append, containing "%s" for the number
8087
     * @see getCopyHeader()
8088
     * @internal should only be used from within DataHandler
8089
     */
8090
    public function prependLabel($table)
8091
    {
8092
        return $this->getLanguageService()->sL($GLOBALS['TCA'][$table]['ctrl']['prependAtCopy']);
8093
    }
8094
8095
    /**
8096
     * Get the final pid based on $table and $pid ($destPid type... pos/neg)
8097
     *
8098
     * @param string $table Table name
8099
     * @param int $pid "Destination pid" : If the value is >= 0 it's just returned directly (through (int)though) but if the value is <0 then the method looks up the record with the uid equal to abs($pid) (positive number) and returns the PID of that record! The idea is that negative numbers point to the record AFTER WHICH the position is supposed to be!
8100
     * @return int
8101
     * @internal should only be used from within DataHandler
8102
     */
8103
    public function resolvePid($table, $pid)
8104
    {
8105
        $pid = (int)$pid;
8106
        if ($pid < 0) {
8107
            $query = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
8108
            $query->getRestrictions()
8109
                ->removeAll();
8110
            $row = $query
8111
                ->select('pid')
8112
                ->from($table)
8113
                ->where($query->expr()->eq('uid', $query->createNamedParameter(abs($pid), \PDO::PARAM_INT)))
8114
                ->execute()
8115
                ->fetch();
8116
            $pid = (int)$row['pid'];
8117
        }
8118
        return $pid;
8119
    }
8120
8121
    /**
8122
     * Removes the prependAtCopy prefix on values
8123
     *
8124
     * @param string $table Table name
8125
     * @param string $value The value to fix
8126
     * @return string Clean name
8127
     * @internal should only be used from within DataHandler
8128
     */
8129
    public function clearPrefixFromValue($table, $value)
8130
    {
8131
        $regex = '/\s' . sprintf(preg_quote($this->prependLabel($table)), '[0-9]*') . '$/';
8132
        return @preg_replace($regex, '', $value);
8133
    }
8134
8135
    /**
8136
     * Check if there are records from tables on the pages to be deleted which the current user is not allowed to
8137
     *
8138
     * @param int[] $pageIds IDs of pages which should be checked
8139
     * @return string[]|null Return null, if permission granted, otherwise an array with the tables that are not allowed to be deleted
8140
     * @see canDeletePage()
8141
     */
8142
    protected function checkForRecordsFromDisallowedTables(array $pageIds): ?array
8143
    {
8144
        if ($this->admin) {
8145
            return null;
8146
        }
8147
8148
        $disallowedTables = [];
8149
        if (!empty($pageIds)) {
8150
            $tableNames = $this->compileAdminTables();
8151
            foreach ($tableNames as $table) {
8152
                $query = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
8153
                $query->getRestrictions()
8154
                    ->removeAll()
8155
                    ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
8156
                $count = $query->count('uid')
8157
                    ->from($table)
8158
                    ->where($query->expr()->in(
8159
                        'pid',
8160
                        $query->createNamedParameter($pageIds, Connection::PARAM_INT_ARRAY)
8161
                    ))
8162
                    ->execute()
8163
                    ->fetchColumn(0);
8164
                if ($count && ($this->tableReadOnly($table) || !$this->checkModifyAccessList($table))) {
8165
                    $disallowedTables[] = $table;
8166
                }
8167
            }
8168
        }
8169
        return !empty($disallowedTables) ? $disallowedTables : null;
8170
    }
8171
8172
    /**
8173
     * Determine if a record was copied or if a record is the result of a copy action.
8174
     *
8175
     * @param string $table The tablename of the record
8176
     * @param int $uid The uid of the record
8177
     * @return bool Returns TRUE if the record is copied or is the result of a copy action
8178
     * @internal should only be used from within DataHandler
8179
     */
8180
    public function isRecordCopied($table, $uid)
8181
    {
8182
        // If the record was copied:
8183
        if (isset($this->copyMappingArray[$table][$uid])) {
8184
            return true;
8185
        }
8186
        if (isset($this->copyMappingArray[$table]) && in_array($uid, array_values($this->copyMappingArray[$table]))) {
8187
            return true;
8188
        }
8189
        return false;
8190
    }
8191
8192
    /******************************
8193
     *
8194
     * Clearing cache
8195
     *
8196
     ******************************/
8197
8198
    /**
8199
     * Clearing the cache based on a page being updated
8200
     * If the $table is 'pages' then cache is cleared for all pages on the same level (and subsequent?)
8201
     * Else just clear the cache for the parent page of the record.
8202
     *
8203
     * @param string $table Table name of record that was just updated.
8204
     * @param int $uid UID of updated / inserted record
8205
     * @param int $pid REAL PID of page of a deleted/moved record to get TSconfig in ClearCache.
8206
     * @internal This method is not meant to be called directly but only from the core itself or from hooks
8207
     */
8208
    public function registerRecordIdForPageCacheClearing($table, $uid, $pid = null)
8209
    {
8210
        if (!is_array(static::$recordsToClearCacheFor[$table])) {
8211
            static::$recordsToClearCacheFor[$table] = [];
8212
        }
8213
        static::$recordsToClearCacheFor[$table][] = (int)$uid;
8214
        if ($pid !== null) {
8215
            if (!is_array(static::$recordPidsForDeletedRecords[$table])) {
8216
                static::$recordPidsForDeletedRecords[$table] = [];
8217
            }
8218
            static::$recordPidsForDeletedRecords[$table][$uid][] = (int)$pid;
8219
        }
8220
    }
8221
8222
    /**
8223
     * Do the actual clear cache
8224
     */
8225
    protected function processClearCacheQueue()
8226
    {
8227
        $tagsToClear = [];
8228
        $clearCacheCommands = [];
8229
8230
        foreach (static::$recordsToClearCacheFor as $table => $uids) {
8231
            foreach (array_unique($uids) as $uid) {
8232
                if (!isset($GLOBALS['TCA'][$table]) || $uid <= 0) {
8233
                    return;
8234
                }
8235
                // For move commands we may get more then 1 parent.
8236
                $pageUids = $this->getOriginalParentOfRecord($table, $uid);
8237
                foreach ($pageUids as $originalParent) {
8238
                    [$tagsToClearFromPrepare, $clearCacheCommandsFromPrepare]
8239
                        = $this->prepareCacheFlush($table, $uid, $originalParent);
8240
                    $tagsToClear = array_merge($tagsToClear, $tagsToClearFromPrepare);
8241
                    $clearCacheCommands = array_merge($clearCacheCommands, $clearCacheCommandsFromPrepare);
8242
                }
8243
            }
8244
        }
8245
8246
        /** @var CacheManager $cacheManager */
8247
        $cacheManager = $this->getCacheManager();
8248
        $cacheManager->flushCachesInGroupByTags('pages', array_keys($tagsToClear));
8249
8250
        // Filter duplicate cache commands from cacheQueue
8251
        $clearCacheCommands = array_unique($clearCacheCommands);
8252
        // Execute collected clear cache commands from page TSConfig
8253
        foreach ($clearCacheCommands as $command) {
8254
            $this->clear_cacheCmd($command);
8255
        }
8256
8257
        // Reset the cache clearing array
8258
        static::$recordsToClearCacheFor = [];
8259
8260
        // Reset the original pid array
8261
        static::$recordPidsForDeletedRecords = [];
8262
    }
8263
8264
    /**
8265
     * Prepare the cache clearing
8266
     *
8267
     * @param string $table Table name of record that needs to be cleared
8268
     * @param int $uid UID of record for which the cache needs to be cleared
8269
     * @param int $pid Original pid of the page of the record which the cache needs to be cleared
8270
     * @return array Array with tagsToClear and clearCacheCommands
8271
     * @internal This function is internal only it may be changed/removed also in minor version numbers.
8272
     */
8273
    protected function prepareCacheFlush($table, $uid, $pid)
8274
    {
8275
        $tagsToClear = [];
8276
        $clearCacheCommands = [];
8277
        $pageUid = 0;
8278
        // Get Page TSconfig relevant:
8279
        $TSConfig = BackendUtility::getPagesTSconfig($pid)['TCEMAIN.'] ?? [];
8280
        if (empty($TSConfig['clearCache_disable']) && $this->BE_USER->workspace === 0) {
8281
            $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
8282
            // If table is "pages":
8283
            $pageIdsThatNeedCacheFlush = [];
8284
            if ($table === 'pages') {
8285
                // Find out if the record is a get the original page
8286
                $pageUid = $this->getDefaultLanguagePageId($uid);
8287
8288
                // Builds list of pages on the SAME level as this page (siblings)
8289
                $queryBuilder = $connectionPool->getQueryBuilderForTable('pages');
8290
                $queryBuilder->getRestrictions()
8291
                    ->removeAll()
8292
                    ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
8293
                $siblings = $queryBuilder
8294
                    ->select('A.pid AS pid', 'B.uid AS uid')
8295
                    ->from('pages', 'A')
8296
                    ->from('pages', 'B')
8297
                    ->where(
8298
                        $queryBuilder->expr()->eq('A.uid', $queryBuilder->createNamedParameter($pageUid, \PDO::PARAM_INT)),
8299
                        $queryBuilder->expr()->eq('B.pid', $queryBuilder->quoteIdentifier('A.pid')),
8300
                        $queryBuilder->expr()->gte('A.pid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
8301
                    )
8302
                    ->execute();
8303
8304
                $parentPageId = 0;
8305
                while ($row_tmp = $siblings->fetch()) {
8306
                    $pageIdsThatNeedCacheFlush[] = (int)$row_tmp['uid'];
8307
                    $parentPageId = (int)$row_tmp['pid'];
8308
                    // Add children as well:
8309
                    if ($TSConfig['clearCache_pageSiblingChildren']) {
8310
                        $siblingChildrenQuery = $connectionPool->getQueryBuilderForTable('pages');
8311
                        $siblingChildrenQuery->getRestrictions()
8312
                            ->removeAll()
8313
                            ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
8314
                        $siblingChildren = $siblingChildrenQuery
8315
                            ->select('uid')
8316
                            ->from('pages')
8317
                            ->where($siblingChildrenQuery->expr()->eq(
8318
                                'pid',
8319
                                $siblingChildrenQuery->createNamedParameter($row_tmp['uid'], \PDO::PARAM_INT)
8320
                            ))
8321
                            ->execute();
8322
                        while ($row_tmp2 = $siblingChildren->fetch()) {
8323
                            $pageIdsThatNeedCacheFlush[] = (int)$row_tmp2['uid'];
8324
                        }
8325
                    }
8326
                }
8327
                // Finally, add the parent page as well when clearing a specific page
8328
                if ($parentPageId > 0) {
8329
                    $pageIdsThatNeedCacheFlush[] = $parentPageId;
8330
                }
8331
                // Add grand-parent as well if configured
8332
                if ($TSConfig['clearCache_pageGrandParent']) {
8333
                    $parentQuery = $connectionPool->getQueryBuilderForTable('pages');
8334
                    $parentQuery->getRestrictions()
8335
                        ->removeAll()
8336
                        ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
8337
                    $row_tmp = $parentQuery
8338
                        ->select('pid')
8339
                        ->from('pages')
8340
                        ->where($parentQuery->expr()->eq(
8341
                            'uid',
8342
                            $parentQuery->createNamedParameter($parentPageId, \PDO::PARAM_INT)
8343
                        ))
8344
                        ->execute()
8345
                        ->fetch();
8346
                    if (!empty($row_tmp)) {
8347
                        $pageIdsThatNeedCacheFlush[] = (int)$row_tmp['pid'];
8348
                    }
8349
                }
8350
            } else {
8351
                // For other tables than "pages", delete cache for the records "parent page".
8352
                $pageIdsThatNeedCacheFlush[] = $pageUid = (int)$this->getPID($table, $uid);
8353
                // Add the parent page as well
8354
                if ($TSConfig['clearCache_pageGrandParent']) {
8355
                    $parentQuery = $connectionPool->getQueryBuilderForTable('pages');
8356
                    $parentQuery->getRestrictions()
8357
                        ->removeAll()
8358
                        ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
8359
                    $parentPageRecord = $parentQuery
8360
                        ->select('pid')
8361
                        ->from('pages')
8362
                        ->where($parentQuery->expr()->eq(
8363
                            'uid',
8364
                            $parentQuery->createNamedParameter($pageUid, \PDO::PARAM_INT)
8365
                        ))
8366
                        ->execute()
8367
                        ->fetch();
8368
                    if (!empty($parentPageRecord)) {
8369
                        $pageIdsThatNeedCacheFlush[] = (int)$parentPageRecord['pid'];
8370
                    }
8371
                }
8372
            }
8373
            // Call pre-processing function for clearing of cache for page ids:
8374
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['clearPageCacheEval'] ?? [] as $funcName) {
8375
                $_params = ['pageIdArray' => &$pageIdsThatNeedCacheFlush, 'table' => $table, 'uid' => $uid, 'functionID' => 'clear_cache()'];
8376
                // Returns the array of ids to clear, FALSE if nothing should be cleared! Never an empty array!
8377
                GeneralUtility::callUserFunction($funcName, $_params, $this);
8378
            }
8379
            // Delete cache for selected pages:
8380
            foreach ($pageIdsThatNeedCacheFlush as $pageId) {
8381
                $tagsToClear['pageId_' . $pageId] = true;
8382
            }
8383
            // Queue delete cache for current table and record
8384
            $tagsToClear[$table] = true;
8385
            $tagsToClear[$table . '_' . $uid] = true;
8386
        }
8387
        // Clear cache for pages entered in TSconfig:
8388
        if (!empty($TSConfig['clearCacheCmd'])) {
8389
            $commands = GeneralUtility::trimExplode(',', $TSConfig['clearCacheCmd'], true);
8390
            $clearCacheCommands = array_unique($commands);
8391
        }
8392
        // Call post processing function for clear-cache:
8393
        $_params = ['table' => $table, 'uid' => $uid, 'uid_page' => $pageUid, 'TSConfig' => $TSConfig, 'tags' => $tagsToClear];
8394
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['clearCachePostProc'] ?? [] as $_funcRef) {
8395
            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
8396
        }
8397
        return [
8398
            $tagsToClear,
8399
            $clearCacheCommands
8400
        ];
8401
    }
8402
8403
    /**
8404
     * Clears the cache based on the command $cacheCmd.
8405
     *
8406
     * $cacheCmd='pages'
8407
     * Clears cache for all pages and page-based caches inside the cache manager.
8408
     * Requires admin-flag to be set for BE_USER.
8409
     *
8410
     * $cacheCmd='all'
8411
     * Clears all cache_tables. This is necessary if templates are updated.
8412
     * Requires admin-flag to be set for BE_USER.
8413
     *
8414
     * The following cache_* are intentionally not cleared by 'all'
8415
     *
8416
     * - imagesizes:	Clearing this table would cause a lot of unneeded
8417
     * Imagemagick calls because the size information has
8418
     * to be fetched again after clearing.
8419
     * - all caches inside the cache manager that are inside the group "system"
8420
     * - they are only needed to build up the core system and templates.
8421
     *   If the group of system caches needs to be deleted explicitly, use
8422
     *   flushCachesInGroup('system') of CacheManager directly.
8423
     *
8424
     * $cacheCmd=[integer]
8425
     * Clears cache for the page pointed to by $cacheCmd (an integer).
8426
     *
8427
     * $cacheCmd='cacheTag:[string]'
8428
     * Flush page and pagesection cache by given tag
8429
     *
8430
     * $cacheCmd='cacheId:[string]'
8431
     * Removes cache identifier from page and page section cache
8432
     *
8433
     * Can call a list of post processing functions as defined in
8434
     * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['clearCachePostProc']
8435
     * (numeric array with values being the function references, called by
8436
     * GeneralUtility::callUserFunction()).
8437
     *
8438
     *
8439
     * @param string $cacheCmd The cache command, see above description
8440
     */
8441
    public function clear_cacheCmd($cacheCmd)
8442
    {
8443
        if (is_object($this->BE_USER)) {
8444
            $this->BE_USER->writeLog(SystemLogType::CACHE, SystemLogCacheAction::CLEAR, SystemLogErrorClassification::MESSAGE, 0, 'User %s has cleared the cache (cacheCmd=%s)', [$this->BE_USER->user['username'], $cacheCmd]);
8445
        }
8446
        $userTsConfig = $this->BE_USER->getTSConfig();
8447
        switch (strtolower($cacheCmd)) {
8448
            case 'pages':
8449
                if ($this->admin || ($userTsConfig['options.']['clearCache.']['pages'] ?? false)) {
8450
                    $this->getCacheManager()->flushCachesInGroup('pages');
8451
                }
8452
                break;
8453
            case 'all':
8454
                // allow to clear all caches if the TS config option is enabled or the option is not explicitly
8455
                // disabled for admins (which could clear all caches by default). The latter option is useful
8456
                // for big production sites where it should be possible to restrict the cache clearing for some admins.
8457
                if (($userTsConfig['options.']['clearCache.']['all'] ?? false)
8458
                    || ($this->admin && (bool)($userTsConfig['options.']['clearCache.']['all'] ?? true))
8459
                ) {
8460
                    $this->getCacheManager()->flushCaches();
8461
                    GeneralUtility::makeInstance(ConnectionPool::class)
8462
                        ->getConnectionForTable('cache_treelist')
8463
                        ->truncate('cache_treelist');
8464
8465
                    // Delete Opcode Cache
8466
                    GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive();
8467
                }
8468
                break;
8469
        }
8470
8471
        $tagsToFlush = [];
8472
        // Clear cache for a page ID!
8473
        if (MathUtility::canBeInterpretedAsInteger($cacheCmd)) {
8474
            $list_cache = [$cacheCmd];
8475
            // Call pre-processing function for clearing of cache for page ids:
8476
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['clearPageCacheEval'] ?? [] as $funcName) {
8477
                $_params = ['pageIdArray' => &$list_cache, 'cacheCmd' => $cacheCmd, 'functionID' => 'clear_cacheCmd()'];
8478
                // Returns the array of ids to clear, FALSE if nothing should be cleared! Never an empty array!
8479
                GeneralUtility::callUserFunction($funcName, $_params, $this);
8480
            }
8481
            // Delete cache for selected pages:
8482
            if (is_array($list_cache)) {
0 ignored issues
show
introduced by
The condition is_array($list_cache) is always true.
Loading history...
8483
                foreach ($list_cache as $pageId) {
8484
                    $tagsToFlush[] = 'pageId_' . (int)$pageId;
8485
                }
8486
            }
8487
        }
8488
        // flush cache by tag
8489
        if (GeneralUtility::isFirstPartOfStr(strtolower($cacheCmd), 'cachetag:')) {
8490
            $cacheTag = substr($cacheCmd, 9);
8491
            $tagsToFlush[] = $cacheTag;
8492
        }
8493
        // process caching framework operations
8494
        if (!empty($tagsToFlush)) {
8495
            $this->getCacheManager()->flushCachesInGroupByTags('pages', $tagsToFlush);
0 ignored issues
show
Bug introduced by
The method getCacheManager() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

8495
            $this->/** @scrutinizer ignore-call */ 
8496
                   getCacheManager()->flushCachesInGroupByTags('pages', $tagsToFlush);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
8496
        }
8497
8498
        // Call post processing function for clear-cache:
8499
        $_params = ['cacheCmd' => strtolower($cacheCmd), 'tags' => $tagsToFlush];
8500
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['clearCachePostProc'] ?? [] as $_funcRef) {
8501
            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
8502
        }
8503
    }
8504
8505
    /*****************************
8506
     *
8507
     * Logging
8508
     *
8509
     *****************************/
8510
    /**
8511
     * Logging actions from DataHandler
8512
     *
8513
     * @param string $table Table name the log entry is concerned with. Blank if NA
8514
     * @param int $recuid Record UID. Zero if NA
8515
     * @param int $action Action number: 0=No category, 1=new record, 2=update record, 3= delete record, 4= move record, 5= Check/evaluate
8516
     * @param int $recpid Normally 0 (zero). If set, it indicates that this log-entry is used to notify the backend of a record which is moved to another location
8517
     * @param int $error The severity: 0 = message, 1 = error, 2 = System Error, 3 = security notice (admin), 4 warning
8518
     * @param string $details Default error message in english
8519
     * @param int $details_nr This number is unique for every combination of $type and $action. This is the error-message number, which can later be used to translate error messages. 0 if not categorized, -1 if temporary
8520
     * @param array $data Array with special information that may go into $details by '%s' marks / sprintf() when the log is shown
8521
     * @param int $event_pid The page_uid (pid) where the event occurred. Used to select log-content for specific pages.
8522
     * @param string $NEWid NEW id for new records
8523
     * @return int Log entry UID (0 if no log entry was written or logging is disabled)
8524
     * @see \TYPO3\CMS\Core\SysLog\Action\Database for all available values of argument $action
8525
     * @see \TYPO3\CMS\Core\SysLog\Error for all available values of argument $error
8526
     * @internal should only be used from within TYPO3 Core
8527
     */
8528
    public function log($table, $recuid, $action, $recpid, $error, $details, $details_nr = -1, $data = [], $event_pid = -1, $NEWid = '')
8529
    {
8530
        if (!$this->enableLogging) {
8531
            return 0;
8532
        }
8533
        // Type value for DataHandler
8534
        if (!$this->storeLogMessages) {
8535
            $details = '';
8536
        }
8537
        if ($error > 0) {
8538
            $detailMessage = $details;
8539
            if (is_array($data)) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
8540
                $detailMessage = vsprintf($details, $data);
8541
            }
8542
            $this->errorLog[] = '[' . SystemLogType::DB . '.' . $action . '.' . $details_nr . ']: ' . $detailMessage;
8543
        }
8544
        return $this->BE_USER->writelog(SystemLogType::DB, $action, $error, $details_nr, $details, $data, $table, $recuid, $recpid, $event_pid, $NEWid);
8545
    }
8546
8547
    /**
8548
     * Simple logging function meant to be used when logging messages is not yet fixed.
8549
     *
8550
     * @param string $message Message string
8551
     * @param int $error Error code, see log()
8552
     * @return int Log entry UID
8553
     * @see log()
8554
     * @internal should only be used from within TYPO3 Core
8555
     */
8556
    public function newlog($message, $error = SystemLogErrorClassification::MESSAGE)
8557
    {
8558
        return $this->log('', 0, SystemLogGenericAction::UNDEFINED, 0, $error, $message, -1);
8559
    }
8560
8561
    /**
8562
     * Print log error messages from the operations of this script instance
8563
     * @internal should only be used from within TYPO3 Core
8564
     */
8565
    public function printLogErrorMessages()
8566
    {
8567
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_log');
8568
        $queryBuilder->getRestrictions()->removeAll();
8569
        $result = $queryBuilder
8570
            ->select('*')
8571
            ->from('sys_log')
8572
            ->where(
8573
                $queryBuilder->expr()->eq('type', $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT)),
8574
                $queryBuilder->expr()->lt('action', $queryBuilder->createNamedParameter(256, \PDO::PARAM_INT)),
8575
                $queryBuilder->expr()->eq(
8576
                    'userid',
8577
                    $queryBuilder->createNamedParameter($this->BE_USER->user['uid'], \PDO::PARAM_INT)
8578
                ),
8579
                $queryBuilder->expr()->eq(
8580
                    'tstamp',
8581
                    $queryBuilder->createNamedParameter($GLOBALS['EXEC_TIME'], \PDO::PARAM_INT)
8582
                ),
8583
                $queryBuilder->expr()->neq('error', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
8584
            )
8585
            ->execute();
8586
8587
        while ($row = $result->fetch()) {
8588
            $log_data = unserialize($row['log_data']);
8589
            $msg = $row['error'] . ': ' . sprintf($row['details'], $log_data[0], $log_data[1], $log_data[2], $log_data[3], $log_data[4]);
8590
            /** @var FlashMessage $flashMessage */
8591
            $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $msg, '', $row['error'] === 4 ? FlashMessage::WARNING : FlashMessage::ERROR, true);
8592
            /** @var FlashMessageService $flashMessageService */
8593
            $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
8594
            $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
8595
            $defaultFlashMessageQueue->enqueue($flashMessage);
8596
        }
8597
    }
8598
8599
    /*****************************
8600
     *
8601
     * Internal (do not use outside Core!)
8602
     *
8603
     *****************************/
8604
8605
    /**
8606
     * Find out if the record is a localization. If so, get the uid of the default language page.
8607
     * Always returns the uid of the workspace live record: No explicit workspace overlay is applied.
8608
     *
8609
     * @param int $pageId Page UID, can be the default page record, or a page translation record ID
8610
     * @return int UID of the default page record in live workspace
8611
     */
8612
    protected function getDefaultLanguagePageId(int $pageId): int
8613
    {
8614
        $localizationParentFieldName = $GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'];
8615
        $row = $this->recordInfo('pages', $pageId, $localizationParentFieldName);
8616
        $localizationParent = (int)$row[$localizationParentFieldName];
8617
        if ($localizationParent > 0) {
8618
            return $localizationParent;
8619
        }
8620
        return $pageId;
8621
    }
8622
8623
    /**
8624
     * Preprocesses field array based on field type. Some fields must be adjusted
8625
     * before going to database. This is done on the copy of the field array because
8626
     * original values are used in remap action later.
8627
     *
8628
     * @param string $table	Table name
8629
     * @param array $fieldArray	Field array to check
8630
     * @return array Updated field array
8631
     * @internal should only be used from within TYPO3 Core
8632
     */
8633
    public function insertUpdateDB_preprocessBasedOnFieldType($table, $fieldArray)
8634
    {
8635
        $result = $fieldArray;
8636
        foreach ($fieldArray as $field => $value) {
8637
            if (!MathUtility::canBeInterpretedAsInteger($value)
8638
                && $GLOBALS['TCA'][$table]['columns'][$field]['config']['type'] === 'inline'
8639
                && $GLOBALS['TCA'][$table]['columns'][$field]['config']['foreign_field']) {
8640
                $result[$field] = count(GeneralUtility::trimExplode(',', $value, true));
8641
            }
8642
        }
8643
        return $result;
8644
    }
8645
8646
    /**
8647
     * Determines whether a particular record has been deleted
8648
     * using DataHandler::deleteRecord() in this instance.
8649
     *
8650
     * @param string $tableName
8651
     * @param string $uid
8652
     * @return bool
8653
     * @internal should only be used from within TYPO3 Core
8654
     */
8655
    public function hasDeletedRecord($tableName, $uid)
8656
    {
8657
        return
8658
            !empty($this->deletedRecords[$tableName])
8659
            && in_array($uid, $this->deletedRecords[$tableName])
8660
        ;
8661
    }
8662
8663
    /**
8664
     * Gets the automatically versionized id of a record.
8665
     *
8666
     * @param string $table Name of the table
8667
     * @param int $id Uid of the record
8668
     * @return int|null
8669
     * @internal should only be used from within TYPO3 Core
8670
     */
8671
    public function getAutoVersionId($table, $id): ?int
8672
    {
8673
        $result = null;
8674
        if (isset($this->autoVersionIdMap[$table][$id])) {
8675
            $result = (int)trim($this->autoVersionIdMap[$table][$id]);
8676
        }
8677
        return $result;
8678
    }
8679
8680
    /**
8681
     * Overlays the automatically versionized id of a record.
8682
     *
8683
     * @param string $table Name of the table
8684
     * @param int $id Uid of the record
8685
     * @return int
8686
     */
8687
    protected function overlayAutoVersionId($table, $id)
8688
    {
8689
        $autoVersionId = $this->getAutoVersionId($table, $id);
8690
        if ($autoVersionId !== null) {
8691
            $id = $autoVersionId;
8692
        }
8693
        return $id;
8694
    }
8695
8696
    /**
8697
     * Adds new values to the remapStackChildIds array.
8698
     *
8699
     * @param array $idValues uid values
8700
     */
8701
    protected function addNewValuesToRemapStackChildIds(array $idValues)
8702
    {
8703
        foreach ($idValues as $idValue) {
8704
            if (strpos($idValue, 'NEW') === 0) {
8705
                $this->remapStackChildIds[$idValue] = true;
8706
            }
8707
        }
8708
    }
8709
8710
    /**
8711
     * Resolves versioned records for the current workspace scope.
8712
     * Delete placeholders are substituted and removed.
8713
     *
8714
     * @param string $tableName Name of the table to be processed
8715
     * @param string $fieldNames List of the field names to be fetched
8716
     * @param string $sortingField Name of the sorting field to be used
8717
     * @param array $liveIds Flat array of (live) record ids
8718
     * @return array
8719
     */
8720
    protected function resolveVersionedRecords($tableName, $fieldNames, $sortingField, array $liveIds)
8721
    {
8722
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)
8723
            ->getConnectionForTable($tableName);
8724
        $sortingStatement = !empty($sortingField)
8725
            ? [$connection->quoteIdentifier($sortingField)]
8726
            : null;
8727
        /** @var PlainDataResolver $resolver */
8728
        $resolver = GeneralUtility::makeInstance(
8729
            PlainDataResolver::class,
8730
            $tableName,
8731
            $liveIds,
8732
            $sortingStatement
8733
        );
8734
8735
        $resolver->setWorkspaceId($this->BE_USER->workspace);
8736
        $resolver->setKeepDeletePlaceholder(false);
8737
        $resolver->setKeepMovePlaceholder(false);
8738
        $resolver->setKeepLiveIds(true);
8739
        $recordIds = $resolver->get();
8740
8741
        $records = [];
8742
        foreach ($recordIds as $recordId) {
8743
            $records[$recordId] = BackendUtility::getRecord($tableName, $recordId, $fieldNames);
8744
        }
8745
8746
        return $records;
8747
    }
8748
8749
    /**
8750
     * Gets the outer most instance of \TYPO3\CMS\Core\DataHandling\DataHandler
8751
     * Since \TYPO3\CMS\Core\DataHandling\DataHandler can create nested objects of itself,
8752
     * this method helps to determine the first (= outer most) one.
8753
     *
8754
     * @return DataHandler
8755
     */
8756
    protected function getOuterMostInstance()
8757
    {
8758
        if (!isset($this->outerMostInstance)) {
8759
            $stack = array_reverse(debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS));
8760
            foreach ($stack as $stackItem) {
8761
                if (isset($stackItem['object']) && $stackItem['object'] instanceof self) {
8762
                    $this->outerMostInstance = $stackItem['object'];
8763
                    break;
8764
                }
8765
            }
8766
        }
8767
        return $this->outerMostInstance;
8768
    }
8769
8770
    /**
8771
     * Determines whether the this object is the outer most instance of itself
8772
     * Since DataHandler can create nested objects of itself,
8773
     * this method helps to determine the first (= outer most) one.
8774
     *
8775
     * @return bool
8776
     */
8777
    public function isOuterMostInstance()
8778
    {
8779
        return $this->getOuterMostInstance() === $this;
8780
    }
8781
8782
    /**
8783
     * Gets an instance of the runtime cache.
8784
     *
8785
     * @return FrontendInterface
8786
     */
8787
    protected function getRuntimeCache()
8788
    {
8789
        return $this->getCacheManager()->getCache('runtime');
8790
    }
8791
8792
    /**
8793
     * Determines nested element calls.
8794
     *
8795
     * @param string $table Name of the table
8796
     * @param int $id Uid of the record
8797
     * @param string $identifier Name of the action to be checked
8798
     * @return bool
8799
     */
8800
    protected function isNestedElementCallRegistered($table, $id, $identifier)
8801
    {
8802
        $nestedElementCalls = (array)$this->runtimeCache->get($this->cachePrefixNestedElementCalls);
8803
        return isset($nestedElementCalls[$identifier][$table][$id]);
8804
    }
8805
8806
    /**
8807
     * Registers nested elements calls.
8808
     * This is used to track nested calls (e.g. for following m:n relations).
8809
     *
8810
     * @param string $table Name of the table
8811
     * @param int $id Uid of the record
8812
     * @param string $identifier Name of the action to be tracked
8813
     */
8814
    protected function registerNestedElementCall($table, $id, $identifier)
8815
    {
8816
        $nestedElementCalls = (array)$this->runtimeCache->get($this->cachePrefixNestedElementCalls);
8817
        $nestedElementCalls[$identifier][$table][$id] = true;
8818
        $this->runtimeCache->set($this->cachePrefixNestedElementCalls, $nestedElementCalls);
8819
    }
8820
8821
    /**
8822
     * Resets the nested element calls.
8823
     */
8824
    protected function resetNestedElementCalls()
8825
    {
8826
        $this->runtimeCache->remove($this->cachePrefixNestedElementCalls);
8827
    }
8828
8829
    /**
8830
     * Determines whether an element was registered to be deleted in the registry.
8831
     *
8832
     * @param string $table Name of the table
8833
     * @param int $id Uid of the record
8834
     * @return bool
8835
     * @see registerElementsToBeDeleted
8836
     * @see resetElementsToBeDeleted
8837
     * @see copyRecord_raw
8838
     * @see versionizeRecord
8839
     */
8840
    protected function isElementToBeDeleted($table, $id)
8841
    {
8842
        $elementsToBeDeleted = (array)$this->runtimeCache->get('core-datahandler-elementsToBeDeleted');
8843
        return isset($elementsToBeDeleted[$table][$id]);
8844
    }
8845
8846
    /**
8847
     * Registers elements to be deleted in the registry.
8848
     *
8849
     * @see process_datamap
8850
     */
8851
    protected function registerElementsToBeDeleted()
8852
    {
8853
        $elementsToBeDeleted = (array)$this->runtimeCache->get('core-datahandler-elementsToBeDeleted');
8854
        $this->runtimeCache->set('core-datahandler-elementsToBeDeleted', array_merge($elementsToBeDeleted, $this->getCommandMapElements('delete')));
8855
    }
8856
8857
    /**
8858
     * Resets the elements to be deleted in the registry.
8859
     *
8860
     * @see process_datamap
8861
     */
8862
    protected function resetElementsToBeDeleted()
8863
    {
8864
        $this->runtimeCache->remove('core-datahandler-elementsToBeDeleted');
8865
    }
8866
8867
    /**
8868
     * Unsets elements (e.g. of the data map) that shall be deleted.
8869
     * This avoids to modify records that will be deleted later on.
8870
     *
8871
     * @param array $elements Elements to be modified
8872
     * @return array
8873
     */
8874
    protected function unsetElementsToBeDeleted(array $elements)
8875
    {
8876
        $elements = ArrayUtility::arrayDiffAssocRecursive($elements, $this->getCommandMapElements('delete'));
8877
        foreach ($elements as $key => $value) {
8878
            if (empty($value)) {
8879
                unset($elements[$key]);
8880
            }
8881
        }
8882
        return $elements;
8883
    }
8884
8885
    /**
8886
     * Gets elements of the command map that match a particular command.
8887
     *
8888
     * @param string $needle The command to be matched
8889
     * @return array
8890
     */
8891
    protected function getCommandMapElements($needle)
8892
    {
8893
        $elements = [];
8894
        foreach ($this->cmdmap as $tableName => $idArray) {
8895
            foreach ($idArray as $id => $commandArray) {
8896
                foreach ($commandArray as $command => $value) {
8897
                    if ($value && $command == $needle) {
8898
                        $elements[$tableName][$id] = true;
8899
                    }
8900
                }
8901
            }
8902
        }
8903
        return $elements;
8904
    }
8905
8906
    /**
8907
     * Controls active elements and sets NULL values if not active.
8908
     * Datamap is modified accordant to submitted control values.
8909
     */
8910
    protected function controlActiveElements()
8911
    {
8912
        if (!empty($this->control['active'])) {
8913
            $this->setNullValues(
8914
                $this->control['active'],
8915
                $this->datamap
8916
            );
8917
        }
8918
    }
8919
8920
    /**
8921
     * Sets NULL values in haystack array.
8922
     * The general behaviour in the user interface is to enable/activate fields.
8923
     * Thus, this method uses NULL as value to be stored if a field is not active.
8924
     *
8925
     * @param array $active hierarchical array with active elements
8926
     * @param array $haystack hierarchical array with haystack to be modified
8927
     */
8928
    protected function setNullValues(array $active, array &$haystack)
8929
    {
8930
        foreach ($active as $key => $value) {
8931
            // Nested data is processes recursively
8932
            if (is_array($value)) {
8933
                $this->setNullValues(
8934
                    $value,
8935
                    $haystack[$key]
8936
                );
8937
            } elseif ($value == 0) {
8938
                // Field has not been activated in the user interface,
8939
                // thus a NULL value shall be stored in the database
8940
                $haystack[$key] = null;
8941
            }
8942
        }
8943
    }
8944
8945
    /**
8946
     * @param CorrelationId $correlationId
8947
     */
8948
    public function setCorrelationId(CorrelationId $correlationId): void
8949
    {
8950
        $this->correlationId = $correlationId;
8951
    }
8952
8953
    /**
8954
     * @return CorrelationId|null
8955
     */
8956
    public function getCorrelationId(): ?CorrelationId
8957
    {
8958
        return $this->correlationId;
8959
    }
8960
8961
    /**
8962
     * Entry point to post process a database insert. Currently bails early unless a UID has been forced
8963
     * and the database platform is not MySQL.
8964
     *
8965
     * @param \TYPO3\CMS\Core\Database\Connection $connection
8966
     * @param string $tableName
8967
     * @param int $suggestedUid
8968
     * @return int
8969
     */
8970
    protected function postProcessDatabaseInsert(Connection $connection, string $tableName, int $suggestedUid): int
8971
    {
8972
        if ($suggestedUid !== 0 && $connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
8973
            $this->postProcessPostgresqlInsert($connection, $tableName);
8974
            // The last inserted id on postgresql is actually the last value generated by the sequence.
8975
            // On a forced UID insert this might not be the actual value or the sequence might not even
8976
            // have generated a value yet.
8977
            // Return the actual ID we forced on insert as a surrogate.
8978
            return $suggestedUid;
8979
        }
8980
        if ($connection->getDatabasePlatform() instanceof SQLServerPlatform) {
8981
            return $this->postProcessSqlServerInsert($connection, $tableName);
8982
        }
8983
        $id = $connection->lastInsertId($tableName);
8984
        return (int)$id;
8985
    }
8986
8987
    /**
8988
     * Get the last insert ID from sql server
8989
     *
8990
     * - first checks whether doctrine might be able to fetch the ID from the
8991
     * sequence table
8992
     * - if that does not succeed it manually selects the current IDENTITY value
8993
     * from a table
8994
     * - returns 0 if both fail
8995
     *
8996
     * @param \TYPO3\CMS\Core\Database\Connection $connection
8997
     * @param string $tableName
8998
     * @return int
8999
     * @throws \Doctrine\DBAL\Exception
9000
     */
9001
    protected function postProcessSqlServerInsert(Connection $connection, string $tableName): int
9002
    {
9003
        $id = $connection->lastInsertId($tableName);
9004
        if (!((int)$id > 0)) {
9005
            $table = $connection->quoteIdentifier($tableName);
9006
            $result = $connection->executeQuery('SELECT IDENT_CURRENT(\'' . $table . '\') AS id')->fetch();
9007
            if (isset($result['id']) && $result['id'] > 0) {
9008
                $id = $result['id'];
9009
            }
9010
        }
9011
        return (int)$id;
9012
    }
9013
9014
    /**
9015
     * PostgreSQL works with sequences for auto increment columns. A sequence is not updated when a value is
9016
     * written to such a column. To avoid clashes when the sequence returns an existing ID this helper will
9017
     * update the sequence to the current max value of the column.
9018
     *
9019
     * @param \TYPO3\CMS\Core\Database\Connection $connection
9020
     * @param string $tableName
9021
     */
9022
    protected function postProcessPostgresqlInsert(Connection $connection, string $tableName)
9023
    {
9024
        $queryBuilder = $connection->createQueryBuilder();
9025
        $queryBuilder->getRestrictions()->removeAll();
9026
        $row = $queryBuilder->select('PGT.schemaname', 'S.relname', 'C.attname', 'T.relname AS tablename')
9027
            ->from('pg_class', 'S')
9028
            ->from('pg_depend', 'D')
9029
            ->from('pg_class', 'T')
9030
            ->from('pg_attribute', 'C')
9031
            ->from('pg_tables', 'PGT')
9032
            ->where(
9033
                $queryBuilder->expr()->eq('S.relkind', $queryBuilder->quote('S')),
9034
                $queryBuilder->expr()->eq('S.oid', $queryBuilder->quoteIdentifier('D.objid')),
9035
                $queryBuilder->expr()->eq('D.refobjid', $queryBuilder->quoteIdentifier('T.oid')),
9036
                $queryBuilder->expr()->eq('D.refobjid', $queryBuilder->quoteIdentifier('C.attrelid')),
9037
                $queryBuilder->expr()->eq('D.refobjsubid', $queryBuilder->quoteIdentifier('C.attnum')),
9038
                $queryBuilder->expr()->eq('T.relname', $queryBuilder->quoteIdentifier('PGT.tablename')),
9039
                $queryBuilder->expr()->eq('PGT.tablename', $queryBuilder->quote($tableName))
9040
            )
9041
            ->setMaxResults(1)
9042
            ->execute()
9043
            ->fetch();
9044
9045
        if ($row !== false) {
9046
            $connection->exec(
9047
                sprintf(
9048
                    'SELECT SETVAL(%s, COALESCE(MAX(%s), 0)+1, FALSE) FROM %s',
9049
                    $connection->quote($row['schemaname'] . '.' . $row['relname']),
9050
                    $connection->quoteIdentifier($row['attname']),
9051
                    $connection->quoteIdentifier($row['schemaname'] . '.' . $row['tablename'])
9052
                )
9053
            );
9054
        }
9055
    }
9056
9057
    /**
9058
     * Return the cache entry identifier for field evals
9059
     *
9060
     * @param string $additionalIdentifier
9061
     * @return string
9062
     */
9063
    protected function getFieldEvalCacheIdentifier($additionalIdentifier)
9064
    {
9065
        return 'core-datahandler-eval-' . md5($additionalIdentifier);
9066
    }
9067
9068
    /**
9069
     * @return RelationHandler
9070
     */
9071
    protected function createRelationHandlerInstance()
9072
    {
9073
        $isWorkspacesLoaded = ExtensionManagementUtility::isLoaded('workspaces');
9074
        $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
9075
        $relationHandler->setWorkspaceId($this->BE_USER->workspace);
9076
        $relationHandler->setUseLiveReferenceIds($isWorkspacesLoaded);
9077
        $relationHandler->setUseLiveParentIds($isWorkspacesLoaded);
9078
        $relationHandler->setReferenceIndexUpdater($this->referenceIndexUpdater);
9079
        return $relationHandler;
9080
    }
9081
9082
    /**
9083
     * Create and returns an instance of the CacheManager
9084
     *
9085
     * @return CacheManager
9086
     */
9087
    protected function getCacheManager()
9088
    {
9089
        return GeneralUtility::makeInstance(CacheManager::class);
9090
    }
9091
9092
    /**
9093
     * Gets the resourceFactory
9094
     *
9095
     * @return ResourceFactory
9096
     */
9097
    protected function getResourceFactory()
9098
    {
9099
        return GeneralUtility::makeInstance(ResourceFactory::class);
9100
    }
9101
9102
    /**
9103
     * @return LanguageService
9104
     */
9105
    protected function getLanguageService()
9106
    {
9107
        return $GLOBALS['LANG'];
9108
    }
9109
9110
    /**
9111
     * @internal should only be used from within TYPO3 Core
9112
     * @return array
9113
     */
9114
    public function getHistoryRecords(): array
9115
    {
9116
        return $this->historyRecords;
9117
    }
9118
}
9119