[TASK] Update Microsoft SQL server docker images for tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / DataHandling / DataHandler.php
1 <?php
2 namespace TYPO3\CMS\Core\DataHandling;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Doctrine\DBAL\DBALException;
18 use Doctrine\DBAL\Driver\Statement;
19 use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
20 use Doctrine\DBAL\Platforms\SQLServerPlatform;
21 use Doctrine\DBAL\Types\IntegerType;
22 use Psr\Log\LoggerAwareInterface;
23 use Psr\Log\LoggerAwareTrait;
24 use TYPO3\CMS\Backend\Utility\BackendUtility;
25 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
26 use TYPO3\CMS\Core\Cache\CacheManager;
27 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
28 use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidIdentifierException;
29 use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidParentRowException;
30 use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidParentRowLoopException;
31 use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidParentRowRootException;
32 use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidPointerFieldValueException;
33 use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
34 use TYPO3\CMS\Core\Configuration\Richtext;
35 use TYPO3\CMS\Core\Crypto\PasswordHashing\InvalidPasswordHashException;
36 use TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory;
37 use TYPO3\CMS\Core\Database\Connection;
38 use TYPO3\CMS\Core\Database\ConnectionPool;
39 use TYPO3\CMS\Core\Database\Query\QueryHelper;
40 use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
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\ReferenceIndex;
45 use TYPO3\CMS\Core\Database\RelationHandler;
46 use TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore;
47 use TYPO3\CMS\Core\DataHandling\Localization\DataMapProcessor;
48 use TYPO3\CMS\Core\DataHandling\Model\CorrelationId;
49 use TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory;
50 use TYPO3\CMS\Core\Html\RteHtmlParser;
51 use TYPO3\CMS\Core\Localization\LanguageService;
52 use TYPO3\CMS\Core\Messaging\FlashMessage;
53 use TYPO3\CMS\Core\Messaging\FlashMessageService;
54 use TYPO3\CMS\Core\Resource\ResourceFactory;
55 use TYPO3\CMS\Core\Service\OpcodeCacheService;
56 use TYPO3\CMS\Core\Type\Bitmask\Permission;
57 use TYPO3\CMS\Core\Utility\ArrayUtility;
58 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
59 use TYPO3\CMS\Core\Utility\GeneralUtility;
60 use TYPO3\CMS\Core\Utility\HttpUtility;
61 use TYPO3\CMS\Core\Utility\MathUtility;
62 use TYPO3\CMS\Core\Utility\StringUtility;
63 use TYPO3\CMS\Core\Versioning\VersionState;
64
65 /**
66 * The main data handler class which takes care of correctly updating and inserting records.
67 * This class was formerly known as TCEmain.
68 *
69 * This is the TYPO3 Core Engine class for manipulation of the database
70 * This class is used by eg. the tce_db BE route (SimpleDataHandlerController) which provides an interface for POST forms to this class.
71 *
72 * Dependencies:
73 * - $GLOBALS['TCA'] must exist
74 * - $GLOBALS['LANG'] must exist
75 *
76 * Also see document 'TYPO3 Core API' for details.
77 */
78 class DataHandler implements LoggerAwareInterface
79 {
80 use LoggerAwareTrait;
81
82 // *********************
83 // Public variables you can configure before using the class:
84 // *********************
85 /**
86 * If TRUE, the default log-messages will be stored. This should not be necessary if the locallang-file for the
87 * log-display is properly configured. So disabling this will just save some database-space as the default messages are not saved.
88 *
89 * @var bool
90 */
91 public $storeLogMessages = true;
92
93 /**
94 * If TRUE, actions are logged to sys_log.
95 *
96 * @var bool
97 */
98 public $enableLogging = true;
99
100 /**
101 * If TRUE, the datamap array is reversed in the order, which is a nice thing if you're creating a whole new
102 * bunch of records.
103 *
104 * @var bool
105 */
106 public $reverseOrder = false;
107
108 /**
109 * If TRUE, only fields which are different from the database values are saved! In fact, if a whole input array
110 * is similar, it's not saved then.
111 *
112 * @var bool
113 */
114 public $checkSimilar = true;
115
116 /**
117 * This will read the record after having updated or inserted it. If anything is not properly submitted an error
118 * is written to the log. This feature consumes extra time by selecting records
119 *
120 * @var bool
121 */
122 public $checkStoredRecords = true;
123
124 /**
125 * If set, values '' and 0 will equal each other when the stored records are checked.
126 *
127 * @var bool
128 */
129 public $checkStoredRecords_loose = true;
130
131 /**
132 * If this is set, then a page is deleted by deleting the whole branch under it (user must have
133 * delete permissions to it all). If not set, then the page is deleted ONLY if it has no branch.
134 *
135 * @var bool
136 */
137 public $deleteTree = false;
138
139 /**
140 * If set, then the 'hideAtCopy' flag for tables will be ignored.
141 *
142 * @var bool
143 */
144 public $neverHideAtCopy = false;
145
146 /**
147 * If set, then the TCE class has been instantiated during an import action of a T3D
148 *
149 * @var bool
150 */
151 public $isImporting = false;
152
153 /**
154 * If set, then transformations are NOT performed on the input.
155 *
156 * @var bool
157 */
158 public $dontProcessTransformations = false;
159
160 /**
161 * Will distinguish between translations (with parent) and localizations (without parent) while still using the same methods to copy the records
162 * TRUE: translation of a record connected to the default language
163 * FALSE: localization of a record without connection to the default language
164 *
165 * @var bool
166 */
167 protected $useTransOrigPointerField = true;
168
169 /**
170 * If TRUE, workspace restrictions are bypassed on edit and create actions (process_datamap()).
171 * YOU MUST KNOW what you do if you use this feature!
172 *
173 * @var bool
174 */
175 public $bypassWorkspaceRestrictions = false;
176
177 /**
178 * If TRUE, access check, check for deleted etc. for records is bypassed.
179 * YOU MUST KNOW what you are doing if you use this feature!
180 *
181 * @var bool
182 */
183 public $bypassAccessCheckForRecords = false;
184
185 /**
186 * Comma-separated list. This list of tables decides which tables will be copied. If empty then none will.
187 * If '*' then all will (that the user has permission to of course)
188 *
189 * @var string
190 */
191 public $copyWhichTables = '*';
192
193 /**
194 * If 0 then branch is NOT copied.
195 * If 1 then pages on the 1st level is copied.
196 * If 2 then pages on the second level is copied ... and so on
197 *
198 * @var int
199 */
200 public $copyTree = 0;
201
202 /**
203 * [table][fields]=value: New records are created with default values and you can set this array on the
204 * form $defaultValues[$table][$field] = $value to override the default values fetched from TCA.
205 * If ->setDefaultsFromUserTS is called UserTSconfig default values will overrule existing values in this array
206 * (thus UserTSconfig overrules externally set defaults which overrules TCA defaults)
207 *
208 * @var array
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 */
219 public $overrideValues = [];
220
221 /**
222 * If entries are set in this array corresponding to fields for update, they are ignored and thus NOT updated.
223 * You could set this array from a series of checkboxes with value=0 and hidden fields before the checkbox with 1.
224 * Then an empty checkbox will disable the field.
225 *
226 * @var array
227 */
228 public $data_disableFields = [];
229
230 /**
231 * Use this array to validate suggested uids for tables by setting [table]:[uid]. This is a dangerous option
232 * since it will force the inserted record to have a certain UID. The value just have to be TRUE, but if you set
233 * it to "DELETE" it will make sure any record with that UID will be deleted first (raw delete).
234 * The option is used for import of T3D files when synchronizing between two mirrored servers.
235 * As a security measure this feature is available only for Admin Users (for now)
236 *
237 * @var array
238 */
239 public $suggestedInsertUids = [];
240
241 /**
242 * Object. Call back object for FlexForm traversal. Useful when external classes wants to use the
243 * iteration functions inside DataHandler for traversing a FlexForm structure.
244 *
245 * @var object
246 */
247 public $callBackObj;
248
249 /**
250 * A string which can be used as correlationId for RecordHistory entries.
251 * The string can later be used to rollback multiple changes at once.
252 *
253 * @var CorrelationId|null
254 */
255 protected $correlationId;
256
257 // *********************
258 // Internal variables (mapping arrays) which can be used (read-only) from outside
259 // *********************
260 /**
261 * Contains mapping of auto-versionized records.
262 *
263 * @var array
264 */
265 public $autoVersionIdMap = [];
266
267 /**
268 * 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
269 *
270 * @var array
271 */
272 public $substNEWwithIDs = [];
273
274 /**
275 * Like $substNEWwithIDs, but where each old "NEW..." id is mapped to the table it was from.
276 *
277 * @var array
278 */
279 public $substNEWwithIDs_table = [];
280
281 /**
282 * Holds the tables and there the ids of newly created child records from IRRE
283 *
284 * @var array
285 */
286 public $newRelatedIDs = [];
287
288 /**
289 * This array is the sum of all copying operations in this class. May be READ from outside, thus partly public.
290 *
291 * @var array
292 */
293 public $copyMappingArray_merged = [];
294
295 /**
296 * Per-table array with UIDs that have been deleted.
297 *
298 * @var array
299 */
300 protected $deletedRecords = [];
301
302 /**
303 * Errors are collected in this variable.
304 *
305 * @var array
306 */
307 public $errorLog = [];
308
309 /**
310 * Fields from the pages-table for which changes will trigger a pagetree refresh
311 *
312 * @var array
313 */
314 public $pagetreeRefreshFieldsFromPages = ['pid', 'sorting', 'deleted', 'hidden', 'title', 'doktype', 'is_siteroot', 'fe_group', 'nav_hide', 'nav_title', 'module', 'starttime', 'endtime', 'content_from_pid', 'extendToSubpages'];
315
316 /**
317 * Indicates whether the pagetree needs a refresh because of important changes
318 *
319 * @var bool
320 */
321 public $pagetreeNeedsRefresh = false;
322
323 // *********************
324 // Internal Variables, do not touch.
325 // *********************
326
327 // Variables set in init() function:
328
329 /**
330 * The user-object the script uses. If not set from outside, this is set to the current global $BE_USER.
331 *
332 * @var BackendUserAuthentication
333 */
334 public $BE_USER;
335
336 /**
337 * Will be set to uid of be_user executing this script
338 *
339 * @var int
340 */
341 public $userid;
342
343 /**
344 * Will be set to username of be_user executing this script
345 *
346 * @var string
347 */
348 public $username;
349
350 /**
351 * Will be set if user is admin
352 *
353 * @var bool
354 */
355 public $admin;
356
357 /**
358 * Can be overridden from $GLOBALS['TYPO3_CONF_VARS']
359 *
360 * @var array
361 */
362 public $defaultPermissions = [
363 'user' => 'show,edit,delete,new,editcontent',
364 'group' => 'show,edit,new,editcontent',
365 'everybody' => ''
366 ];
367
368 /**
369 * 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.
370 *
371 * @var array
372 */
373 protected $excludedTablesAndFields = [];
374
375 /**
376 * Data submitted from the form view, used to control behaviours,
377 * e.g. this is used to activate/deactivate fields and thus store NULL values
378 *
379 * @var array
380 */
381 protected $control = [];
382
383 /**
384 * Set with incoming data array
385 *
386 * @var array
387 */
388 public $datamap = [];
389
390 /**
391 * Set with incoming cmd array
392 *
393 * @var array
394 */
395 public $cmdmap = [];
396
397 /**
398 * List of changed old record ids to new records ids
399 *
400 * @var array
401 */
402 protected $mmHistoryRecords = [];
403
404 /**
405 * List of changed old record ids to new records ids
406 *
407 * @var array
408 */
409 protected $historyRecords = [];
410
411 // Internal static:
412 /**
413 * Permission mapping
414 *
415 * @var array
416 */
417 public $pMap = [
418 'show' => 1,
419 // 1st bit
420 'edit' => 2,
421 // 2nd bit
422 'delete' => 4,
423 // 3rd bit
424 'new' => 8,
425 // 4th bit
426 'editcontent' => 16
427 ];
428
429 /**
430 * The interval between sorting numbers used with tables with a 'sorting' field defined.
431 *
432 * Min 1, should be power of 2
433 *
434 * @var int
435 */
436 public $sortIntervals = 256;
437
438 // Internal caching arrays
439 /**
440 * Used by function checkRecordUpdateAccess() to store whether a record is updatable or not.
441 *
442 * @var array
443 */
444 protected $recUpdateAccessCache = [];
445
446 /**
447 * User by function checkRecordInsertAccess() to store whether a record can be inserted on a page id
448 *
449 * @var array
450 */
451 protected $recInsertAccessCache = [];
452
453 /**
454 * Caching array for check of whether records are in a webmount
455 *
456 * @var array
457 */
458 protected $isRecordInWebMount_Cache = [];
459
460 /**
461 * Caching array for page ids in webmounts
462 *
463 * @var array
464 */
465 protected $isInWebMount_Cache = [];
466
467 /**
468 * Used for caching page records in pageInfo()
469 *
470 * @var array
471 */
472 protected $pageCache = [];
473
474 // Other arrays:
475 /**
476 * For accumulation of MM relations that must be written after new records are created.
477 *
478 * @var array
479 */
480 public $dbAnalysisStore = [];
481
482 /**
483 * Used for tracking references that might need correction after operations
484 *
485 * @var array
486 */
487 public $registerDBList = [];
488
489 /**
490 * Used for tracking references that might need correction in pid field after operations (e.g. IRRE)
491 *
492 * @var array
493 */
494 public $registerDBPids = [];
495
496 /**
497 * Used by the copy action to track the ids of new pages so subpages are correctly inserted!
498 * THIS is internally cleared for each executed copy operation! DO NOT USE THIS FROM OUTSIDE!
499 * Read from copyMappingArray_merged instead which is accumulating this information.
500 *
501 * NOTE: This is used by some outside scripts (e.g. hooks), as the results in $copyMappingArray_merged
502 * are only available after an action has been completed.
503 *
504 * @var array
505 */
506 public $copyMappingArray = [];
507
508 /**
509 * Array used for remapping uids and values at the end of process_datamap
510 *
511 * @var array
512 */
513 public $remapStack = [];
514
515 /**
516 * Array used for remapping uids and values at the end of process_datamap
517 * (e.g. $remapStackRecords[<table>][<uid>] = <index in $remapStack>)
518 *
519 * @var array
520 */
521 public $remapStackRecords = [];
522
523 /**
524 * Array used for checking whether new children need to be remapped
525 *
526 * @var array
527 */
528 protected $remapStackChildIds = [];
529
530 /**
531 * Array used for executing addition actions after remapping happened (set processRemapStack())
532 *
533 * @var array
534 */
535 protected $remapStackActions = [];
536
537 /**
538 * Array used for executing post-processing on the reference index
539 *
540 * @var array
541 */
542 protected $remapStackRefIndex = [];
543
544 /**
545 * Array used for additional calls to $this->updateRefIndex
546 *
547 * @var array
548 */
549 public $updateRefIndexStack = [];
550
551 /**
552 * Tells, that this DataHandler instance was called from \TYPO3\CMS\Impext\ImportExport.
553 * This variable is set by \TYPO3\CMS\Impext\ImportExport
554 *
555 * @var array
556 */
557 public $callFromImpExp = false;
558
559 // Various
560
561 /**
562 * Set to "currentRecord" during checking of values.
563 *
564 * @var array
565 */
566 public $checkValue_currentRecord = [];
567
568 /**
569 * Disable delete clause
570 *
571 * @var bool
572 */
573 protected $disableDeleteClause = false;
574
575 /**
576 * @var array
577 */
578 protected $checkModifyAccessListHookObjects;
579
580 /**
581 * @var array
582 */
583 protected $version_remapMMForVersionSwap_reg;
584
585 /**
586 * The outer most instance of \TYPO3\CMS\Core\DataHandling\DataHandler:
587 * This object instantiates itself on versioning and localization ...
588 *
589 * @var \TYPO3\CMS\Core\DataHandling\DataHandler
590 */
591 protected $outerMostInstance;
592
593 /**
594 * Internal cache for collecting records that should trigger cache clearing
595 *
596 * @var array
597 */
598 protected static $recordsToClearCacheFor = [];
599
600 /**
601 * Internal cache for pids of records which were deleted. It's not possible
602 * to retrieve the parent folder/page at a later stage
603 *
604 * @var array
605 */
606 protected static $recordPidsForDeletedRecords = [];
607
608 /**
609 * Runtime Cache to store and retrieve data computed for a single request
610 *
611 * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
612 */
613 protected $runtimeCache;
614
615 /**
616 * Prefix for the cache entries of nested element calls since the runtimeCache has a global scope.
617 *
618 * @var string
619 */
620 protected $cachePrefixNestedElementCalls = 'core-datahandler-nestedElementCalls-';
621
622 /**
623 * Sets up the data handler cache and some additional options, the main logic is done in the start() method.
624 */
625 public function __construct()
626 {
627 $this->checkStoredRecords = (bool)$GLOBALS['TYPO3_CONF_VARS']['BE']['checkStoredRecords'];
628 $this->checkStoredRecords_loose = (bool)$GLOBALS['TYPO3_CONF_VARS']['BE']['checkStoredRecordsLoose'];
629 $this->runtimeCache = $this->getRuntimeCache();
630 }
631
632 /**
633 * @param array $control
634 */
635 public function setControl(array $control)
636 {
637 $this->control = $control;
638 }
639
640 /**
641 * Initializing.
642 * For details, see 'TYPO3 Core API' document.
643 * This function does not start the processing of data, but merely initializes the object
644 *
645 * @param array $data Data to be modified or inserted in the database
646 * @param array $cmd Commands to copy, move, delete, localize, versionize records.
647 * @param BackendUserAuthentication|null $altUserObject An alternative userobject you can set instead of the default, which is $GLOBALS['BE_USER']
648 */
649 public function start($data, $cmd, $altUserObject = null)
650 {
651 // Initializing BE_USER
652 $this->BE_USER = is_object($altUserObject) ? $altUserObject : $GLOBALS['BE_USER'];
653 $this->userid = $this->BE_USER->user['uid'];
654 $this->username = $this->BE_USER->user['username'];
655 $this->admin = $this->BE_USER->user['admin'];
656 if ($this->BE_USER->uc['recursiveDelete']) {
657 $this->deleteTree = 1;
658 }
659
660 // set correlation id for each new set of data or commands
661 $this->correlationId = CorrelationId::forScope(
662 md5(StringUtility::getUniqueId(self::class))
663 );
664
665 // Get default values from user TSconfig
666 $tcaDefaultOverride = $this->BE_USER->getTSConfig()['TCAdefaults.'] ?? null;
667 if (is_array($tcaDefaultOverride)) {
668 $this->setDefaultsFromUserTS($tcaDefaultOverride);
669 }
670
671 // Initializing default permissions for pages
672 $defaultPermissions = $GLOBALS['TYPO3_CONF_VARS']['BE']['defaultPermissions'];
673 if (isset($defaultPermissions['user'])) {
674 $this->defaultPermissions['user'] = $defaultPermissions['user'];
675 }
676 if (isset($defaultPermissions['group'])) {
677 $this->defaultPermissions['group'] = $defaultPermissions['group'];
678 }
679 if (isset($defaultPermissions['everybody'])) {
680 $this->defaultPermissions['everybody'] = $defaultPermissions['everybody'];
681 }
682 // generates the excludelist, based on TCA/exclude-flag and non_exclude_fields for the user:
683 if (!$this->admin) {
684 $this->excludedTablesAndFields = array_flip($this->getExcludeListArray());
685 }
686 // Setting the data and cmd arrays
687 if (is_array($data)) {
688 reset($data);
689 $this->datamap = $data;
690 }
691 if (is_array($cmd)) {
692 reset($cmd);
693 $this->cmdmap = $cmd;
694 }
695 }
696
697 /**
698 * Function that can mirror input values in datamap-array to other uid numbers.
699 * 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]
700 *
701 * @param array $mirror This array has the syntax $mirror[table_name][uid] = [list of uids to copy data-value TO!]
702 */
703 public function setMirror($mirror)
704 {
705 if (!is_array($mirror)) {
706 return;
707 }
708
709 foreach ($mirror as $table => $uid_array) {
710 if (!isset($this->datamap[$table])) {
711 continue;
712 }
713
714 foreach ($uid_array as $id => $uidList) {
715 if (!isset($this->datamap[$table][$id])) {
716 continue;
717 }
718
719 $theIdsInArray = GeneralUtility::trimExplode(',', $uidList, true);
720 foreach ($theIdsInArray as $copyToUid) {
721 $this->datamap[$table][$copyToUid] = $this->datamap[$table][$id];
722 }
723 }
724 }
725 }
726
727 /**
728 * Initializes default values coming from User TSconfig
729 *
730 * @param array $userTS User TSconfig array
731 */
732 public function setDefaultsFromUserTS($userTS)
733 {
734 if (!is_array($userTS)) {
735 return;
736 }
737
738 foreach ($userTS as $k => $v) {
739 $k = mb_substr($k, 0, -1);
740 if (!$k || !is_array($v) || !isset($GLOBALS['TCA'][$k])) {
741 continue;
742 }
743
744 if (is_array($this->defaultValues[$k])) {
745 $this->defaultValues[$k] = array_merge($this->defaultValues[$k], $v);
746 } else {
747 $this->defaultValues[$k] = $v;
748 }
749 }
750 }
751
752 /**
753 * Dummy method formerly used for file handling.
754 *
755 * @deprecated since TYPO3 v10.0, will be removed in TYPO3 v11.0.
756 */
757 public function process_uploads()
758 {
759 trigger_error('DataHandler->process_uploads() will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
760 }
761
762 /*********************************************
763 *
764 * HOOKS
765 *
766 *********************************************/
767 /**
768 * Hook: processDatamap_afterDatabaseOperations
769 * (calls $hookObj->processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $this);)
770 *
771 * Note: When using the hook after INSERT operations, you will only get the temporary NEW... id passed to your hook as $id,
772 * but you can easily translate it to the real uid of the inserted record using the $this->substNEWwithIDs array.
773 *
774 * @param array $hookObjectsArr (reference) Array with hook objects
775 * @param string $status (reference) Status of the current operation, 'new' or 'update
776 * @param string $table (reference) The table currently processing data for
777 * @param string $id (reference) The record uid currently processing data for, [integer] or [string] (like 'NEW...')
778 * @param array $fieldArray (reference) The field array of a record
779 */
780 public function hook_processDatamap_afterDatabaseOperations(&$hookObjectsArr, &$status, &$table, &$id, &$fieldArray)
781 {
782 // Process hook directly:
783 if (!isset($this->remapStackRecords[$table][$id])) {
784 foreach ($hookObjectsArr as $hookObj) {
785 if (method_exists($hookObj, 'processDatamap_afterDatabaseOperations')) {
786 $hookObj->processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $this);
787 }
788 }
789 } else {
790 $this->remapStackRecords[$table][$id]['processDatamap_afterDatabaseOperations'] = [
791 'status' => $status,
792 'fieldArray' => $fieldArray,
793 'hookObjectsArr' => $hookObjectsArr
794 ];
795 }
796 }
797
798 /**
799 * Gets the 'checkModifyAccessList' hook objects.
800 * The first call initializes the accordant objects.
801 *
802 * @return array The 'checkModifyAccessList' hook objects (if any)
803 * @throws \UnexpectedValueException
804 */
805 protected function getCheckModifyAccessListHookObjects()
806 {
807 if (!isset($this->checkModifyAccessListHookObjects)) {
808 $this->checkModifyAccessListHookObjects = [];
809 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList'] ?? [] as $className) {
810 $hookObject = GeneralUtility::makeInstance($className);
811 if (!$hookObject instanceof DataHandlerCheckModifyAccessListHookInterface) {
812 throw new \UnexpectedValueException($className . ' must implement interface ' . DataHandlerCheckModifyAccessListHookInterface::class, 1251892472);
813 }
814 $this->checkModifyAccessListHookObjects[] = $hookObject;
815 }
816 }
817 return $this->checkModifyAccessListHookObjects;
818 }
819
820 /*********************************************
821 *
822 * PROCESSING DATA
823 *
824 *********************************************/
825 /**
826 * Processing the data-array
827 * Call this function to process the data-array set by start()
828 *
829 * @return bool|void
830 */
831 public function process_datamap()
832 {
833 $this->controlActiveElements();
834
835 // Keep versionized(!) relations here locally:
836 $registerDBList = [];
837 $this->registerElementsToBeDeleted();
838 $this->datamap = $this->unsetElementsToBeDeleted($this->datamap);
839 // Editing frozen:
840 if ($this->BE_USER->workspace !== 0 && $this->BE_USER->workspaceRec['freeze']) {
841 $this->newlog('All editing in this workspace has been frozen!', 1);
842 return false;
843 }
844 // First prepare user defined objects (if any) for hooks which extend this function:
845 $hookObjectsArr = [];
846 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'] ?? [] as $className) {
847 $hookObject = GeneralUtility::makeInstance($className);
848 if (method_exists($hookObject, 'processDatamap_beforeStart')) {
849 $hookObject->processDatamap_beforeStart($this);
850 }
851 $hookObjectsArr[] = $hookObject;
852 }
853 // Pre-process data-map and synchronize localization states
854 $this->datamap = GeneralUtility::makeInstance(SlugEnricher::class)->enrichDataMap($this->datamap);
855 $this->datamap = DataMapProcessor::instance($this->datamap, $this->BE_USER)->process();
856 // 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.
857 $orderOfTables = [];
858 // Set pages first.
859 if (isset($this->datamap['pages'])) {
860 $orderOfTables[] = 'pages';
861 }
862 $orderOfTables = array_unique(array_merge($orderOfTables, array_keys($this->datamap)));
863 // Process the tables...
864 foreach ($orderOfTables as $table) {
865 // Check if
866 // - table is set in $GLOBALS['TCA'],
867 // - table is NOT readOnly
868 // - the table is set with content in the data-array (if not, there's nothing to process...)
869 // - permissions for tableaccess OK
870 $modifyAccessList = $this->checkModifyAccessList($table);
871 if (!$modifyAccessList) {
872 $this->log($table, 0, 2, 0, 1, 'Attempt to modify table \'%s\' without permission', 1, [$table]);
873 }
874 if (!isset($GLOBALS['TCA'][$table]) || $this->tableReadOnly($table) || !is_array($this->datamap[$table]) || !$modifyAccessList) {
875 continue;
876 }
877
878 if ($this->reverseOrder) {
879 $this->datamap[$table] = array_reverse($this->datamap[$table], 1);
880 }
881 // For each record from the table, do:
882 // $id is the record uid, may be a string if new records...
883 // $incomingFieldArray is the array of fields
884 foreach ($this->datamap[$table] as $id => $incomingFieldArray) {
885 if (!is_array($incomingFieldArray)) {
886 continue;
887 }
888 $theRealPid = null;
889
890 // Hook: processDatamap_preProcessFieldArray
891 foreach ($hookObjectsArr as $hookObj) {
892 if (method_exists($hookObj, 'processDatamap_preProcessFieldArray')) {
893 $hookObj->processDatamap_preProcessFieldArray($incomingFieldArray, $table, $id, $this);
894 }
895 }
896 // ******************************
897 // Checking access to the record
898 // ******************************
899 $createNewVersion = false;
900 $recordAccess = false;
901 $old_pid_value = '';
902 // Is it a new record? (Then Id is a string)
903 if (!MathUtility::canBeInterpretedAsInteger($id)) {
904 // Get a fieldArray with tca default values
905 $fieldArray = $this->newFieldArray($table);
906 // A pid must be set for new records.
907 if (isset($incomingFieldArray['pid'])) {
908 $pid_value = $incomingFieldArray['pid'];
909 // Checking and finding numerical pid, it may be a string-reference to another value
910 $canProceed = true;
911 // If a NEW... id
912 if (strpos($pid_value, 'NEW') !== false) {
913 if ($pid_value[0] === '-') {
914 $negFlag = -1;
915 $pid_value = substr($pid_value, 1);
916 } else {
917 $negFlag = 1;
918 }
919 // Trying to find the correct numerical value as it should be mapped by earlier processing of another new record.
920 if (isset($this->substNEWwithIDs[$pid_value])) {
921 if ($negFlag === 1) {
922 $old_pid_value = $this->substNEWwithIDs[$pid_value];
923 }
924 $pid_value = (int)($negFlag * $this->substNEWwithIDs[$pid_value]);
925 } else {
926 $canProceed = false;
927 }
928 }
929 $pid_value = (int)$pid_value;
930 if ($canProceed) {
931 $fieldArray = $this->resolveSortingAndPidForNewRecord($table, $pid_value, $fieldArray);
932 }
933 }
934 $theRealPid = $fieldArray['pid'];
935 // Now, check if we may insert records on this pid.
936 if ($theRealPid >= 0) {
937 // Checks if records can be inserted on this $pid.
938 // If this is a page translation, the check needs to be done for the l10n_parent record
939 if ($table === 'pages' && $incomingFieldArray[$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0 && $incomingFieldArray[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] > 0) {
940 $recordAccess = $this->checkRecordInsertAccess($table, $incomingFieldArray[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']]);
941 } else {
942 $recordAccess = $this->checkRecordInsertAccess($table, $theRealPid);
943 }
944 if ($recordAccess) {
945 $this->addDefaultPermittedLanguageIfNotSet($table, $incomingFieldArray);
946 $recordAccess = $this->BE_USER->recordEditAccessInternals($table, $incomingFieldArray, true);
947 if (!$recordAccess) {
948 $this->newlog('recordEditAccessInternals() check failed. [' . $this->BE_USER->errorMsg . ']', 1);
949 } elseif (!$this->bypassWorkspaceRestrictions) {
950 // Workspace related processing:
951 // If LIVE records cannot be created due to workspace restrictions, prepare creation of placeholder-record
952 if (!$this->BE_USER->workspaceAllowsLiveEditingInTable($table)) {
953 if (BackendUtility::isTableWorkspaceEnabled($table)) {
954 $createNewVersion = true;
955 } else {
956 $recordAccess = false;
957 $this->newlog('Record could not be created in this workspace in this branch', 1);
958 }
959 }
960 }
961 }
962 } else {
963 $this->logger->debug('Internal ERROR: pid should not be less than zero!');
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, 2, 0, 1, '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 . ']', 1);
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'];
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__);
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!', 1);
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!', 1);
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 list($tscPID) = BackendUtility::getTSCpid($table, $id, $old_pid_value ? $old_pid_value : $fieldArray['pid']);
1053 if ($status === 'new' && $table === 'pages') {
1054 $TSConfig = BackendUtility::getPagesTSconfig($tscPID)['TCEMAIN.'] ?? [];
1055 if (isset($TSConfig['permissions.']) && is_array($TSConfig['permissions.'])) {
1056 $fieldArray = $this->setTSconfigPermissions($fieldArray, $TSConfig['permissions.']);
1057 }
1058 }
1059 // Processing of all fields in incomingFieldArray and setting them in $fieldArray
1060 $fieldArray = $this->fillInFieldArray($table, $id, $fieldArray, $incomingFieldArray, $theRealPid, $status, $tscPID);
1061 $newVersion_placeholderFieldArray = [];
1062 if ($createNewVersion) {
1063 // create a placeholder array with already processed field content
1064 $newVersion_placeholderFieldArray = $fieldArray;
1065 }
1066 // NOTICE! All manipulation beyond this point bypasses both "excludeFields" AND possible "MM" relations to field!
1067 // Forcing some values unto field array:
1068 // NOTICE: This overriding is potentially dangerous; permissions per field is not checked!!!
1069 $fieldArray = $this->overrideFieldArray($table, $fieldArray);
1070 if ($createNewVersion) {
1071 $newVersion_placeholderFieldArray = $this->overrideFieldArray($table, $newVersion_placeholderFieldArray);
1072 }
1073 // Setting system fields
1074 if ($status === 'new') {
1075 if ($GLOBALS['TCA'][$table]['ctrl']['crdate']) {
1076 $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['crdate']] = $GLOBALS['EXEC_TIME'];
1077 if ($createNewVersion) {
1078 $newVersion_placeholderFieldArray[$GLOBALS['TCA'][$table]['ctrl']['crdate']] = $GLOBALS['EXEC_TIME'];
1079 }
1080 }
1081 if ($GLOBALS['TCA'][$table]['ctrl']['cruser_id']) {
1082 $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['cruser_id']] = $this->userid;
1083 if ($createNewVersion) {
1084 $newVersion_placeholderFieldArray[$GLOBALS['TCA'][$table]['ctrl']['cruser_id']] = $this->userid;
1085 }
1086 }
1087 } elseif ($this->checkSimilar) {
1088 // Removing fields which are equal to the current value:
1089 $fieldArray = $this->compareFieldArrayWithCurrentAndUnset($table, $id, $fieldArray);
1090 }
1091 if ($GLOBALS['TCA'][$table]['ctrl']['tstamp'] && !empty($fieldArray)) {
1092 $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['tstamp']] = $GLOBALS['EXEC_TIME'];
1093 if ($createNewVersion) {
1094 $newVersion_placeholderFieldArray[$GLOBALS['TCA'][$table]['ctrl']['tstamp']] = $GLOBALS['EXEC_TIME'];
1095 }
1096 }
1097 // Set stage to "Editing" to make sure we restart the workflow
1098 if (BackendUtility::isTableWorkspaceEnabled($table)) {
1099 $fieldArray['t3ver_stage'] = 0;
1100 }
1101 // Hook: processDatamap_postProcessFieldArray
1102 foreach ($hookObjectsArr as $hookObj) {
1103 if (method_exists($hookObj, 'processDatamap_postProcessFieldArray')) {
1104 $hookObj->processDatamap_postProcessFieldArray($status, $table, $id, $fieldArray, $this);
1105 }
1106 }
1107 // Performing insert/update. If fieldArray has been unset by some userfunction (see hook above), don't do anything
1108 // Kasper: Unsetting the fieldArray is dangerous; MM relations might be saved already
1109 if (is_array($fieldArray)) {
1110 if ($status === 'new') {
1111 if ($table === 'pages') {
1112 // for new pages always a refresh is needed
1113 $this->pagetreeNeedsRefresh = true;
1114 }
1115
1116 // This creates a new version of the record with online placeholder and offline version
1117 if ($createNewVersion) {
1118 // new record created in a workspace - so always refresh pagetree to indicate there is a change in the workspace
1119 $this->pagetreeNeedsRefresh = true;
1120
1121 // Setting placeholder state value for temporary record
1122 $newVersion_placeholderFieldArray['t3ver_state'] = (string)new VersionState(VersionState::NEW_PLACEHOLDER);
1123 // Setting workspace - only so display of placeholders can filter out those from other workspaces.
1124 $newVersion_placeholderFieldArray['t3ver_wsid'] = $this->BE_USER->workspace;
1125 $newVersion_placeholderFieldArray[$GLOBALS['TCA'][$table]['ctrl']['label']] = $this->getPlaceholderTitleForTableLabel($table);
1126 // Saving placeholder as 'original'
1127 $this->insertDB($table, $id, $newVersion_placeholderFieldArray, false, (int)($incomingFieldArray['uid'] ?? 0));
1128 // For the actual new offline version, set versioning values to point to placeholder:
1129 $fieldArray['pid'] = -1;
1130 $fieldArray['t3ver_oid'] = $this->substNEWwithIDs[$id];
1131 // Setting placeholder state value for version (so it can know it is currently a new version...)
1132 $fieldArray['t3ver_state'] = (string)new VersionState(VersionState::NEW_PLACEHOLDER_VERSION);
1133 $fieldArray['t3ver_wsid'] = $this->BE_USER->workspace;
1134 // When inserted, $this->substNEWwithIDs[$id] will be changed to the uid of THIS version and so the interface will pick it up just nice!
1135 $phShadowId = $this->insertDB($table, $id, $fieldArray, true, 0, true);
1136 if ($phShadowId) {
1137 // Processes fields of the placeholder record:
1138 $this->triggerRemapAction($table, $id, [$this, 'placeholderShadowing'], [$table, $phShadowId]);
1139 // Hold auto-versionized ids of placeholders:
1140 $this->autoVersionIdMap[$table][$this->substNEWwithIDs[$id]] = $phShadowId;
1141 }
1142 } else {
1143 $this->insertDB($table, $id, $fieldArray, false, (int)($incomingFieldArray['uid'] ?? 0));
1144 }
1145 } else {
1146 if ($table === 'pages') {
1147 // only a certain number of fields needs to be checked for updates
1148 // if $this->checkSimilar is TRUE, fields with unchanged values are already removed here
1149 $fieldsToCheck = array_intersect($this->pagetreeRefreshFieldsFromPages, array_keys($fieldArray));
1150 if (!empty($fieldsToCheck)) {
1151 $this->pagetreeNeedsRefresh = true;
1152 }
1153 }
1154 $this->updateDB($table, $id, $fieldArray);
1155 $this->placeholderShadowing($table, $id);
1156 }
1157 }
1158 // Hook: processDatamap_afterDatabaseOperations
1159 // Note: When using the hook after INSERT operations, you will only get the temporary NEW... id passed to your hook as $id,
1160 // but you can easily translate it to the real uid of the inserted record using the $this->substNEWwithIDs array.
1161 $this->hook_processDatamap_afterDatabaseOperations($hookObjectsArr, $status, $table, $id, $fieldArray);
1162 }
1163 }
1164 // Process the stack of relations to remap/correct
1165 $this->processRemapStack();
1166 $this->dbAnalysisStoreExec();
1167 // Hook: processDatamap_afterAllOperations
1168 // Note: When this hook gets called, all operations on the submitted data have been finished.
1169 foreach ($hookObjectsArr as $hookObj) {
1170 if (method_exists($hookObj, 'processDatamap_afterAllOperations')) {
1171 $hookObj->processDatamap_afterAllOperations($this);
1172 }
1173 }
1174 if ($this->isOuterMostInstance()) {
1175 $this->processClearCacheQueue();
1176 $this->resetElementsToBeDeleted();
1177 }
1178 }
1179
1180 /**
1181 * @param string $table
1182 * @param string $value
1183 * @param string $dbType
1184 * @return string
1185 */
1186 protected function normalizeTimeFormat(string $table, string $value, string $dbType): string
1187 {
1188 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
1189 $platform = $connection->getDatabasePlatform();
1190 if ($platform instanceof SQLServerPlatform) {
1191 $defaultLength = QueryHelper::getDateTimeFormats()[$dbType]['empty'];
1192 $value = substr(
1193 $value,
1194 0,
1195 strlen($defaultLength)
1196 );
1197 }
1198 return $value;
1199 }
1200
1201 /**
1202 * Sets the "sorting" DB field and the "pid" field of an incoming record that should be added (NEW1234)
1203 * depending on the record that should be added or where it should be added.
1204 *
1205 * This method is called from process_datamap()
1206 *
1207 * @param string $table the table name of the record to insert
1208 * @param int $pid the real PID (numeric) where the record should be
1209 * @param array $fieldArray field+value pairs to add
1210 * @return array the modified field array
1211 */
1212 protected function resolveSortingAndPidForNewRecord(string $table, int $pid, array $fieldArray): array
1213 {
1214 $sortColumn = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ?? '';
1215 // Points to a page on which to insert the element, possibly in the top of the page
1216 if ($pid >= 0) {
1217 // Ensure that the "pid" is not a translated page ID, but the default page ID
1218 $pid = $this->getDefaultLanguagePageId($pid);
1219 // The numerical pid is inserted in the data array
1220 $fieldArray['pid'] = $pid;
1221 // If this table is sorted we better find the top sorting number
1222 if ($sortColumn) {
1223 $fieldArray[$sortColumn] = $this->getSortNumber($table, 0, $pid);
1224 }
1225 } elseif ($sortColumn) {
1226 // Points to another record before itself
1227 // If this table is sorted we better find the top sorting number
1228 // Because $pid is < 0, getSortNumber() returns an array
1229 $sortingInfo = $this->getSortNumber($table, 0, $pid);
1230 $fieldArray['pid'] = $sortingInfo['pid'];
1231 $fieldArray[$sortColumn] = $sortingInfo['sortNumber'];
1232 } else {
1233 // Here we fetch the PID of the record that we point to
1234 $record = $this->recordInfo($table, abs($pid), 'pid');
1235 // Ensure that the "pid" is not a translated page ID, but the default page ID
1236 $fieldArray['pid'] = $this->getDefaultLanguagePageId($record['pid']);
1237 }
1238 return $fieldArray;
1239 }
1240
1241 /**
1242 * Fix shadowing of data in case we are editing an offline version of a live "New" placeholder record:
1243 *
1244 * @param string $table Table name
1245 * @param int $id Record uid
1246 */
1247 public function placeholderShadowing($table, $id)
1248 {
1249 if ($liveRec = BackendUtility::getLiveVersionOfRecord($table, $id, '*')) {
1250 if (VersionState::cast($liveRec['t3ver_state'])->indicatesPlaceholder()) {
1251 $justStoredRecord = BackendUtility::getRecord($table, $id);
1252 $newRecord = [];
1253 $shadowCols = $GLOBALS['TCA'][$table]['ctrl']['shadowColumnsForNewPlaceholders'];
1254 $shadowCols .= ',' . $GLOBALS['TCA'][$table]['ctrl']['languageField'];
1255 $shadowCols .= ',' . $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'];
1256 if (isset($GLOBALS['TCA'][$table]['ctrl']['translationSource'])) {
1257 $shadowCols .= ',' . $GLOBALS['TCA'][$table]['ctrl']['translationSource'];
1258 }
1259 $shadowCols .= ',' . $GLOBALS['TCA'][$table]['ctrl']['type'];
1260 $shadowCols .= ',' . $GLOBALS['TCA'][$table]['ctrl']['label'];
1261 $shadowCols .= ',' . implode(',', GeneralUtility::makeInstance(SlugEnricher::class)->resolveSlugFieldNames($table));
1262 $shadowColumns = array_unique(GeneralUtility::trimExplode(',', $shadowCols, true));
1263 foreach ($shadowColumns as $fieldName) {
1264 if ((string)$justStoredRecord[$fieldName] !== (string)$liveRec[$fieldName] && isset($GLOBALS['TCA'][$table]['columns'][$fieldName]) && $fieldName !== 'uid' && $fieldName !== 'pid') {
1265 $newRecord[$fieldName] = $justStoredRecord[$fieldName];
1266 }
1267 }
1268 if (!empty($newRecord)) {
1269 if ($this->enableLogging) {
1270 $this->log($table, $liveRec['uid'], 0, 0, 0, 'Shadowing done on fields <i>' . implode(',', array_keys($newRecord)) . '</i> in placeholder record ' . $table . ':' . $liveRec['uid'] . ' (offline version UID=' . $id . ')', -1, [], $this->eventPid($table, $liveRec['uid'], $liveRec['pid']));
1271 }
1272 $this->updateDB($table, $liveRec['uid'], $newRecord);
1273 }
1274 }
1275 }
1276 }
1277
1278 /**
1279 * Create a placeholder title for the label field that does match the field requirements
1280 *
1281 * @param string $table The table name
1282 * @param string $placeholderContent Placeholder content to be used
1283 * @return string placeholder value
1284 */
1285 public function getPlaceholderTitleForTableLabel($table, $placeholderContent = null)
1286 {
1287 if ($placeholderContent === null) {
1288 $placeholderContent = 'PLACEHOLDER';
1289 }
1290
1291 $labelPlaceholder = '[' . $placeholderContent . ', WS#' . $this->BE_USER->workspace . ']';
1292 $labelField = $GLOBALS['TCA'][$table]['ctrl']['label'];
1293 if (!isset($GLOBALS['TCA'][$table]['columns'][$labelField]['config']['eval'])) {
1294 return $labelPlaceholder;
1295 }
1296 $evalCodesArray = GeneralUtility::trimExplode(',', $GLOBALS['TCA'][$table]['columns'][$labelField]['config']['eval'], true);
1297 $transformedLabel = $this->checkValue_input_Eval($labelPlaceholder, $evalCodesArray, '', $table);
1298 return $transformedLabel['value'] ?? $labelPlaceholder;
1299 }
1300
1301 /**
1302 * Filling in the field array
1303 * $this->excludedTablesAndFields is used to filter fields if needed.
1304 *
1305 * @param string $table Table name
1306 * @param int $id Record ID
1307 * @param array $fieldArray Default values, Preset $fieldArray with 'pid' maybe (pid and uid will be not be overridden anyway)
1308 * @param array $incomingFieldArray Is which fields/values you want to set. There are processed and put into $fieldArray if OK
1309 * @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.
1310 * @param string $status Is 'new' or 'update'
1311 * @param int $tscPID TSconfig PID
1312 * @return array Field Array
1313 */
1314 public function fillInFieldArray($table, $id, $fieldArray, $incomingFieldArray, $realPid, $status, $tscPID)
1315 {
1316 // Initialize:
1317 $originalLanguageRecord = null;
1318 $originalLanguage_diffStorage = null;
1319 $diffStorageFlag = false;
1320 // Setting 'currentRecord' and 'checkValueRecord':
1321 if (strpos($id, 'NEW') !== false) {
1322 // Must have the 'current' array - not the values after processing below...
1323 $checkValueRecord = $fieldArray;
1324 // IF $incomingFieldArray is an array, overlay it.
1325 // 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...
1326 if (is_array($incomingFieldArray) && is_array($checkValueRecord)) {
1327 ArrayUtility::mergeRecursiveWithOverrule($checkValueRecord, $incomingFieldArray);
1328 }
1329 $currentRecord = $checkValueRecord;
1330 } else {
1331 // We must use the current values as basis for this!
1332 $currentRecord = ($checkValueRecord = $this->recordInfo($table, $id, '*'));
1333 // This is done to make the pid positive for offline versions; Necessary to have diff-view for page translations in workspaces.
1334 BackendUtility::fixVersioningPid($table, $currentRecord);
1335 }
1336
1337 // Get original language record if available:
1338 if (is_array($currentRecord)
1339 && $GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField']
1340 && $GLOBALS['TCA'][$table]['ctrl']['languageField']
1341 && $currentRecord[$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0
1342 && $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']
1343 && (int)$currentRecord[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] > 0
1344 ) {
1345 $originalLanguageRecord = $this->recordInfo($table, $currentRecord[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']], '*');
1346 BackendUtility::workspaceOL($table, $originalLanguageRecord);
1347 $originalLanguage_diffStorage = unserialize(
1348 $currentRecord[$GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField']],
1349 ['allowed_classes' => false]
1350 );
1351 }
1352
1353 $this->checkValue_currentRecord = $checkValueRecord;
1354 // In the following all incoming value-fields are tested:
1355 // - Are the user allowed to change the field?
1356 // - Is the field uid/pid (which are already set)
1357 // - perms-fields for pages-table, then do special things...
1358 // - If the field is nothing of the above and the field is configured in TCA, the fieldvalues are evaluated by ->checkValue
1359 // If everything is OK, the field is entered into $fieldArray[]
1360 foreach ($incomingFieldArray as $field => $fieldValue) {
1361 if (isset($this->excludedTablesAndFields[$table . '-' . $field]) || $this->data_disableFields[$table][$id][$field]) {
1362 continue;
1363 }
1364
1365 // The field must be editable.
1366 // Checking if a value for language can be changed:
1367 $languageDeny = $GLOBALS['TCA'][$table]['ctrl']['languageField'] && (string)$GLOBALS['TCA'][$table]['ctrl']['languageField'] === (string)$field && !$this->BE_USER->checkLanguageAccess($fieldValue);
1368 if ($languageDeny) {
1369 continue;
1370 }
1371
1372 switch ($field) {
1373 case 'uid':
1374 case 'pid':
1375 // Nothing happens, already set
1376 break;
1377 case 'perms_userid':
1378 case 'perms_groupid':
1379 case 'perms_user':
1380 case 'perms_group':
1381 case 'perms_everybody':
1382 // Permissions can be edited by the owner or the administrator
1383 if ($table === 'pages' && ($this->admin || $status === 'new' || $this->pageInfo($id, 'perms_userid') == $this->userid)) {
1384 $value = (int)$fieldValue;
1385 switch ($field) {
1386 case 'perms_userid':
1387 case 'perms_groupid':
1388 $fieldArray[$field] = $value;
1389 break;
1390 default:
1391 if ($value >= 0 && $value < (2 ** 5)) {
1392 $fieldArray[$field] = $value;
1393 }
1394 }
1395 }
1396 break;
1397 case 't3ver_oid':
1398 case 't3ver_wsid':
1399 case 't3ver_state':
1400 case 't3ver_count':
1401 case 't3ver_stage':
1402 case 't3ver_tstamp':
1403 break;
1404 case 'l10n_state':
1405 $fieldArray[$field] = $fieldValue;
1406 break;
1407 default:
1408 if (isset($GLOBALS['TCA'][$table]['columns'][$field])) {
1409 // Evaluating the value
1410 $res = $this->checkValue($table, $field, $fieldValue, $id, $status, $realPid, $tscPID, $incomingFieldArray);
1411 if (array_key_exists('value', $res)) {
1412 $fieldArray[$field] = $res['value'];
1413 }
1414 // Add the value of the original record to the diff-storage content:
1415 if ($GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField']) {
1416 $originalLanguage_diffStorage[$field] = $originalLanguageRecord[$field];
1417 $diffStorageFlag = true;
1418 }
1419 } elseif ($GLOBALS['TCA'][$table]['ctrl']['origUid'] === $field) {
1420 // Allow value for original UID to pass by...
1421 $fieldArray[$field] = $fieldValue;
1422 }
1423 }
1424 }
1425
1426 // Dealing with a page translation, setting "sorting", "pid", "perms_*" to the same values as the original record
1427 if ($table === 'pages' && is_array($originalLanguageRecord)) {
1428 $fieldArray['sorting'] = $originalLanguageRecord['sorting'];
1429 $fieldArray['perms_userid'] = $originalLanguageRecord['perms_userid'];
1430 $fieldArray['perms_groupid'] = $originalLanguageRecord['perms_groupid'];
1431 $fieldArray['perms_user'] = $originalLanguageRecord['perms_user'];
1432 $fieldArray['perms_group'] = $originalLanguageRecord['perms_group'];
1433 $fieldArray['perms_everybody'] = $originalLanguageRecord['perms_everybody'];
1434 }
1435
1436 // Add diff-storage information:
1437 if ($diffStorageFlag
1438 && !array_key_exists($GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField'], $fieldArray)
1439 ) {
1440 // 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...
1441 $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField']] = serialize($originalLanguage_diffStorage);
1442 }
1443 // Return fieldArray
1444 return $fieldArray;
1445 }
1446
1447 /*********************************************
1448 *
1449 * Evaluation of input values
1450 *
1451 ********************************************/
1452 /**
1453 * Evaluates a value according to $table/$field settings.
1454 * This function is for real database fields - NOT FlexForm "pseudo" fields.
1455 * NOTICE: Calling this function expects this: 1) That the data is saved!
1456 *
1457 * @param string $table Table name
1458 * @param string $field Field name
1459 * @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.
1460 * @param string $id The record-uid, mainly - but not exclusively - used for logging
1461 * @param string $status 'update' or 'new' flag
1462 * @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. If $realPid is -1 it means that a new version of the record is being inserted.
1463 * @param int $tscPID TSconfig PID
1464 * @param array $incomingFieldArray the fields being explicitly set by the outside (unlike $fieldArray)
1465 * @return array Returns the evaluated $value as key "value" in this array. Can be checked with isset($res['value']) ...
1466 */
1467 public function checkValue($table, $field, $value, $id, $status, $realPid, $tscPID, $incomingFieldArray = [])
1468 {
1469 // Result array
1470 $res = [];
1471
1472 // Processing special case of field pages.doktype
1473 if ($table === 'pages' && $field === 'doktype') {
1474 // If the user may not use this specific doktype, we issue a warning
1475 if (!($this->admin || GeneralUtility::inList($this->BE_USER->groupData['pagetypes_select'], $value))) {
1476 if ($this->enableLogging) {
1477 $propArr = $this->getRecordProperties($table, $id);
1478 $this->log($table, $id, 5, 0, 1, 'You cannot change the \'doktype\' of page \'%s\' to the desired value.', 1, [$propArr['header']], $propArr['event_pid']);
1479 }
1480 return $res;
1481 }
1482 if ($status === 'update') {
1483 // This checks 1) if we should check for disallowed tables and 2) if there are records from disallowed tables on the current page
1484 $onlyAllowedTables = $GLOBALS['PAGES_TYPES'][$value]['onlyAllowedTables'] ?? $GLOBALS['PAGES_TYPES']['default']['onlyAllowedTables'];
1485 if ($onlyAllowedTables) {
1486 // use the real page id (default language)
1487 $recordId = $this->getDefaultLanguagePageId($id);
1488 $theWrongTables = $this->doesPageHaveUnallowedTables($recordId, $value);
1489 if ($theWrongTables) {
1490 if ($this->enableLogging) {
1491 $propArr = $this->getRecordProperties($table, $id);
1492 $this->log($table, $id, 5, 0, 1, '\'doktype\' of page \'%s\' could not be changed because the page contains records from disallowed tables; %s', 2, [$propArr['header'], $theWrongTables], $propArr['event_pid']);
1493 }
1494 return $res;
1495 }
1496 }
1497 }
1498 }
1499
1500 $curValue = null;
1501 if ((int)$id !== 0) {
1502 // Get current value:
1503 $curValueRec = $this->recordInfo($table, $id, $field);
1504 // isset() won't work here, since values can be NULL
1505 if ($curValueRec !== null && array_key_exists($field, $curValueRec)) {
1506 $curValue = $curValueRec[$field];
1507 }
1508 }
1509
1510 if ($table === 'be_users'
1511 && ($field === 'admin' || $field === 'password')
1512 && $status === 'update'
1513 ) {
1514 // Do not allow a non system maintainer admin to change admin flag and password of system maintainers
1515 $systemMaintainers = array_map('intval', $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemMaintainers'] ?? []);
1516 // False if current user is not in system maintainer list or if switch to user mode is active
1517 $isCurrentUserSystemMaintainer = $this->BE_USER->isSystemMaintainer();
1518 $isTargetUserInSystemMaintainerList = in_array((int)$id, $systemMaintainers, true);
1519 if ($field === 'admin') {
1520 $isFieldChanged = (int)$curValueRec[$field] !== (int)$value;
1521 } else {
1522 $isFieldChanged = $curValueRec[$field] !== $value;
1523 }
1524 if (!$isCurrentUserSystemMaintainer && $isTargetUserInSystemMaintainerList && $isFieldChanged) {
1525 $value = $curValueRec[$field];
1526 $message = GeneralUtility::makeInstance(
1527 FlashMessage::class,
1528 $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.adminCanNotChangeSystemMaintainer'),
1529 '',
1530 FlashMessage::ERROR,
1531 true
1532 );
1533 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
1534 $flashMessageService->getMessageQueueByIdentifier()->enqueue($message);
1535 }
1536 }
1537
1538 // Getting config for the field
1539 $tcaFieldConf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
1540
1541 // Create $recFID only for those types that need it
1542 if ($tcaFieldConf['type'] === 'flex') {
1543 $recFID = $table . ':' . $id . ':' . $field;
1544 } else {
1545 $recFID = null;
1546 }
1547
1548 // Perform processing:
1549 $res = $this->checkValue_SW($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $realPid, $recFID, $field, [], $tscPID, ['incomingFieldArray' => $incomingFieldArray]);
1550 return $res;
1551 }
1552
1553 /**
1554 * Branches out evaluation of a field value based on its type as configured in $GLOBALS['TCA']
1555 * Can be called for FlexForm pseudo fields as well, BUT must not have $field set if so.
1556 *
1557 * @param array $res The result array. The processed value (if any!) is set in the "value" key.
1558 * @param string $value The value to set.
1559 * @param array $tcaFieldConf Field configuration from $GLOBALS['TCA']
1560 * @param string $table Table name
1561 * @param int $id UID of record
1562 * @param mixed $curValue Current value of the field
1563 * @param string $status 'update' or 'new' flag
1564 * @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. If $realPid is -1 it means that a new version of the record is being inserted.
1565 * @param string $recFID Field identifier [table:uid:field] for flexforms
1566 * @param string $field Field name. Must NOT be set if the call is for a flexform field (since flexforms are not allowed within flexforms).
1567 * @param array $uploadedFiles
1568 * @param int $tscPID TSconfig PID
1569 * @param array $additionalData Additional data to be forwarded to sub-processors
1570 * @return array Returns the evaluated $value as key "value" in this array.
1571 */
1572 public function checkValue_SW($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $realPid, $recFID, $field, $uploadedFiles, $tscPID, array $additionalData = null)
1573 {
1574 // Convert to NULL value if defined in TCA
1575 if ($value === null && !empty($tcaFieldConf['eval']) && GeneralUtility::inList($tcaFieldConf['eval'], 'null')) {
1576 $res = ['value' => null];
1577 return $res;
1578 }
1579
1580 switch ($tcaFieldConf['type']) {
1581 case 'text':
1582 $res = $this->checkValueForText($value, $tcaFieldConf, $table, $id, $realPid, $field);
1583 break;
1584 case 'passthrough':
1585 case 'imageManipulation':
1586 case 'user':
1587 $res['value'] = $value;
1588 break;
1589 case 'input':
1590 $res = $this->checkValueForInput($value, $tcaFieldConf, $table, $id, $realPid, $field);
1591 break;
1592 case 'slug':
1593 $res = $this->checkValueForSlug((string)$value, $tcaFieldConf, $table, $id, (int)$realPid, $field, $additionalData['incomingFieldArray'] ?? []);
1594 break;
1595 case 'check':
1596 $res = $this->checkValueForCheck($res, $value, $tcaFieldConf, $table, $id, $realPid, $field);
1597 break;
1598 case 'radio':
1599 $res = $this->checkValueForRadio($res, $value, $tcaFieldConf, $table, $id, $realPid, $field);
1600 break;
1601 case 'group':
1602 case 'select':
1603 $res = $this->checkValueForGroupSelect($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $recFID, $uploadedFiles, $field);
1604 break;
1605 case 'inline':
1606 $res = $this->checkValueForInline($res, $value, $tcaFieldConf, $table, $id, $status, $field, $additionalData);
1607 break;
1608 case 'flex':
1609 // FlexForms are only allowed for real fields.
1610 if ($field) {
1611 $res = $this->checkValueForFlex($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $realPid, $recFID, $tscPID, $uploadedFiles, $field);
1612 }
1613 break;
1614 default:
1615 // Do nothing
1616 }
1617 $res = $this->checkValueForInternalReferences($res, $value, $tcaFieldConf, $table, $id, $field);
1618 return $res;
1619 }
1620
1621 /**
1622 * Checks values that are used for internal references. If the provided $value
1623 * is a NEW-identifier, the direct processing is stopped. Instead, the value is
1624 * forwarded to the remap-stack to be post-processed and resolved into a proper
1625 * UID after all data has been resolved.
1626 *
1627 * This method considers TCA types that cannot handle and resolve these internal
1628 * values directly, like 'passthrough', 'none' or 'user'. Values are only modified
1629 * here if the $field is used as 'transOrigPointerField' or 'translationSource'.
1630 *
1631 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
1632 * @param string $value The value to set.
1633 * @param array $tcaFieldConf Field configuration from TCA
1634 * @param string $table Table name
1635 * @param int $id UID of record
1636 * @param string $field The field name
1637 * @return array The result array. The processed value (if any!) is set in the "value" key.
1638 */
1639 protected function checkValueForInternalReferences(array $res, $value, $tcaFieldConf, $table, $id, $field)
1640 {
1641 $relevantFieldNames = [
1642 $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] ?? null,
1643 $GLOBALS['TCA'][$table]['ctrl']['translationSource'] ?? null,
1644 ];
1645
1646 if (
1647 // in case field is empty
1648 empty($field)
1649 // in case the field is not relevant
1650 || !in_array($field, $relevantFieldNames)
1651 // in case the 'value' index has been unset already
1652 || !array_key_exists('value', $res)
1653 // in case it's not a NEW-identifier
1654 || strpos($value, 'NEW') === false
1655 ) {
1656 return $res;
1657 }
1658
1659 $valueArray = [$value];
1660 $this->remapStackRecords[$table][$id] = ['remapStackIndex' => count($this->remapStack)];
1661 $this->addNewValuesToRemapStackChildIds($valueArray);
1662 $this->remapStack[] = [
1663 'args' => [$valueArray, $tcaFieldConf, $id, $table, $field],
1664 'pos' => ['valueArray' => 0, 'tcaFieldConf' => 1, 'id' => 2, 'table' => 3],
1665 'field' => $field
1666 ];
1667 unset($res['value']);
1668
1669 return $res;
1670 }
1671
1672 /**
1673 * Evaluate "text" type values.
1674 *
1675 * @param string $value The value to set.
1676 * @param array $tcaFieldConf Field configuration from TCA
1677 * @param string $table Table name
1678 * @param int $id UID of record
1679 * @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. If $realPid is -1 it means that a new version of the record is being inserted.
1680 * @param string $field Field name
1681 * @return array $res The result array. The processed value (if any!) is set in the "value" key.
1682 */
1683 protected function checkValueForText($value, $tcaFieldConf, $table, $id, $realPid, $field)
1684 {
1685 if (isset($tcaFieldConf['eval']) && $tcaFieldConf['eval'] !== '') {
1686 $cacheId = $this->getFieldEvalCacheIdentifier($tcaFieldConf['eval']);
1687 $evalCodesArray = $this->runtimeCache->get($cacheId);
1688 if (!is_array($evalCodesArray)) {
1689 $evalCodesArray = GeneralUtility::trimExplode(',', $tcaFieldConf['eval'], true);
1690 $this->runtimeCache->set($cacheId, $evalCodesArray);
1691 }
1692 $valueArray = $this->checkValue_text_Eval($value, $evalCodesArray, $tcaFieldConf['is_in']);
1693 } else {
1694 $valueArray = ['value' => $value];
1695 }
1696
1697 // Handle richtext transformations
1698 if ($this->dontProcessTransformations) {
1699 return $valueArray;
1700 }
1701 $recordType = BackendUtility::getTCAtypeValue($table, $this->checkValue_currentRecord);
1702 $columnsOverridesConfigOfField = $GLOBALS['TCA'][$table]['types'][$recordType]['columnsOverrides'][$field]['config'] ?? null;
1703 if ($columnsOverridesConfigOfField) {
1704 ArrayUtility::mergeRecursiveWithOverrule($tcaFieldConf, $columnsOverridesConfigOfField);
1705 }
1706 if (isset($tcaFieldConf['enableRichtext']) && (bool)$tcaFieldConf['enableRichtext'] === true) {
1707 $richtextConfigurationProvider = GeneralUtility::makeInstance(Richtext::class);
1708 $richtextConfiguration = $richtextConfigurationProvider->getConfiguration($table, $field, $realPid, $recordType, $tcaFieldConf);
1709 $parseHTML = GeneralUtility::makeInstance(RteHtmlParser::class);
1710 $parseHTML->init($table . ':' . $field, $realPid);
1711 $valueArray['value'] = $parseHTML->RTE_transform($value, [], 'db', $richtextConfiguration);
1712 }
1713
1714 return $valueArray;
1715 }
1716
1717 /**
1718 * Evaluate "input" type values.
1719 *
1720 * @param string $value The value to set.
1721 * @param array $tcaFieldConf Field configuration from TCA
1722 * @param string $table Table name
1723 * @param int $id UID of record
1724 * @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. If $realPid is -1 it means that a new version of the record is being inserted.
1725 * @param string $field Field name
1726 * @return array $res The result array. The processed value (if any!) is set in the "value" key.
1727 */
1728 protected function checkValueForInput($value, $tcaFieldConf, $table, $id, $realPid, $field)
1729 {
1730 // Handle native date/time fields
1731 $isDateOrDateTimeField = false;
1732 $format = '';
1733 $emptyValue = '';
1734 $dateTimeTypes = QueryHelper::getDateTimeTypes();
1735 // normal integer "date" fields (timestamps) are handled in checkValue_input_Eval
1736 if (isset($tcaFieldConf['dbType']) && in_array($tcaFieldConf['dbType'], $dateTimeTypes, true)) {
1737 if (empty($value)) {
1738 $value = null;
1739 } else {
1740 $isDateOrDateTimeField = true;
1741 $dateTimeFormats = QueryHelper::getDateTimeFormats();
1742 $format = $dateTimeFormats[$tcaFieldConf['dbType']]['format'];
1743
1744 // Convert the date/time into a timestamp for the sake of the checks
1745 $emptyValue = $dateTimeFormats[$tcaFieldConf['dbType']]['empty'];
1746 // We store UTC timestamps in the database, which is what getTimestamp() returns.
1747 $dateTime = new \DateTime($value);
1748 $value = $value === $emptyValue ? null : $dateTime->getTimestamp();
1749 }
1750 }
1751 // Secures the string-length to be less than max.
1752 if (isset($tcaFieldConf['max']) && (int)$tcaFieldConf['max'] > 0) {
1753 $value = mb_substr((string)$value, 0, (int)$tcaFieldConf['max'], 'utf-8');
1754 }
1755
1756 if (empty($tcaFieldConf['eval'])) {
1757 $res = ['value' => $value];
1758 } else {
1759 // Process evaluation settings:
1760 $cacheId = $this->getFieldEvalCacheIdentifier($tcaFieldConf['eval']);
1761 $evalCodesArray = $this->runtimeCache->get($cacheId);
1762 if (!is_array($evalCodesArray)) {
1763 $evalCodesArray = GeneralUtility::trimExplode(',', $tcaFieldConf['eval'], true);
1764 $this->runtimeCache->set($cacheId, $evalCodesArray);
1765 }
1766
1767 $res = $this->checkValue_input_Eval($value, $evalCodesArray, $tcaFieldConf['is_in'] ?? '', $table);
1768 if (isset($tcaFieldConf['dbType']) && isset($res['value']) && !$res['value']) {
1769 // set the value to null if we have an empty value for a native field
1770 $res['value'] = null;
1771 }
1772
1773 // Process UNIQUE settings:
1774 // 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 and if PID is -1 ($realPid<0) then versioning is happening...
1775 if ($field && $realPid >= 0 && !empty($res['value'])) {
1776 if (in_array('uniqueInPid', $evalCodesArray, true)) {
1777 $res['value'] = $this->getUnique($table, $field, $res['value'], $id, $realPid);
1778 }
1779 if ($res['value'] && in_array('unique', $evalCodesArray, true)) {
1780 $res['value'] = $this->getUnique($table, $field, $res['value'], $id);
1781 }
1782 }
1783 }
1784
1785 // Checking range of value:
1786 // @todo: The "checkbox" option was removed for type=input, this check could be probably relaxed?
1787 if (
1788 isset($tcaFieldConf['range']) && $tcaFieldConf['range']
1789 && (!isset($tcaFieldConf['checkbox']) || $res['value'] != $tcaFieldConf['checkbox'])
1790 && (!isset($tcaFieldConf['default']) || (int)$res['value'] !== (int)$tcaFieldConf['default'])
1791 ) {
1792 if (isset($tcaFieldConf['range']['upper']) && (int)$res['value'] > (int)$tcaFieldConf['range']['upper']) {
1793 $res['value'] = (int)$tcaFieldConf['range']['upper'];
1794 }
1795 if (isset($tcaFieldConf['range']['lower']) && (int)$res['value'] < (int)$tcaFieldConf['range']['lower']) {
1796 $res['value'] = (int)$tcaFieldConf['range']['lower'];
1797 }
1798 }
1799
1800 // Handle native date/time fields
1801 if ($isDateOrDateTimeField) {
1802 // Convert the timestamp back to a date/time
1803 $res['value'] = $res['value'] ? gmdate($format, $res['value']) : $emptyValue;
1804 }
1805 return $res;
1806 }
1807
1808 /**
1809 * Evaluate "slug" type values.
1810 *
1811 * @param string $value The value to set.
1812 * @param array $tcaFieldConf Field configuration from TCA
1813 * @param string $table Table name
1814 * @param int $id UID of record
1815 * @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. If $realPid is -1 it means that a new version of the record is being inserted.
1816 * @param string $field Field name
1817 * @param array $incomingFieldArray the fields being explicitly set by the outside (unlike $fieldArray) for the record
1818 * @return array $res The result array. The processed value (if any!) is set in the "value" key.
1819 * @see SlugEnricher
1820 * @see SlugHelper
1821 */
1822 protected function checkValueForSlug(string $value, array $tcaFieldConf, string $table, $id, int $realPid, string $field, array $incomingFieldArray = []): array
1823 {
1824 $workspaceId = $this->BE_USER->workspace;
1825 $helper = GeneralUtility::makeInstance(SlugHelper::class, $table, $field, $tcaFieldConf, $workspaceId);
1826 $fullRecord = array_replace_recursive($this->checkValue_currentRecord, $incomingFieldArray ?? []);
1827 // Generate a value if there is none, otherwise ensure that all characters are cleaned up
1828 if ($value === '') {
1829 $value = $helper->generate($fullRecord, $realPid);
1830 } else {
1831 $value = $helper->sanitize($value);
1832 }
1833
1834 // In case a workspace is given, and the $realPid(!) still is negative
1835 // this is most probably triggered by versionizeRecord() and a raw record
1836 // copy - thus, uniqueness cannot be determined without having the
1837 // real information
1838 // @todo This is still not explicit, but probably should be
1839 if ($workspaceId > 0 && $realPid === -1
1840 && !MathUtility::canBeInterpretedAsInteger($id)
1841 ) {
1842 return ['value' => $value];
1843 }
1844
1845 // Return directly in case no evaluations are defined
1846 if (empty($tcaFieldConf['eval'])) {
1847 return ['value' => $value];
1848 }
1849
1850 $state = RecordStateFactory::forName($table)
1851 ->fromArray($fullRecord, $realPid, $id);
1852 $evalCodesArray = GeneralUtility::trimExplode(',', $tcaFieldConf['eval'], true);
1853 if (in_array('uniqueInSite', $evalCodesArray, true)) {
1854 $value = $helper->buildSlugForUniqueInSite($value, $state);
1855 }
1856 if (in_array('uniqueInPid', $evalCodesArray, true)) {
1857 $value = $helper->buildSlugForUniqueInPid($value, $state);
1858 }
1859
1860 return ['value' => $value];
1861 }
1862
1863 /**
1864 * Evaluates 'check' type values.
1865 *
1866 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
1867 * @param string $value The value to set.
1868 * @param array $tcaFieldConf Field configuration from TCA
1869 * @param string $table Table name
1870 * @param int $id UID of record
1871 * @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. If $realPid is -1 it means that a new version of the record is being inserted.
1872 * @param string $field Field name
1873 * @return array Modified $res array
1874 */
1875 protected function checkValueForCheck($res, $value, $tcaFieldConf, $table, $id, $realPid, $field)
1876 {
1877 $items = $tcaFieldConf['items'];
1878 if (!empty($tcaFieldConf['itemsProcFunc'])) {
1879 /** @var ItemProcessingService $processingService */
1880 $processingService = GeneralUtility::makeInstance(ItemProcessingService::class);
1881 $items = $processingService->getProcessingItems(
1882 $table,
1883 $realPid,
1884 $field,
1885 $this->checkValue_currentRecord,
1886 $tcaFieldConf,
1887 $tcaFieldConf['items']
1888 );
1889 }
1890
1891 $itemC = 0;
1892 if ($items !== null) {
1893 $itemC = count($items);
1894 }
1895 if (!$itemC) {
1896 $itemC = 1;
1897 }
1898 $maxV = (2 ** $itemC) - 1;
1899 if ($value < 0) {
1900 // @todo: throw LogicException here? Negative values for checkbox items do not make sense and indicate a coding error.
1901 $value = 0;
1902 }
1903 if ($value > $maxV) {
1904 // @todo: This case is pretty ugly: If there is an itemsProcFunc registered, and if it returns a dynamic,
1905 // @todo: changing list of items, then it may happen that a value is transformed and vanished checkboxes
1906 // @todo: are permanently removed from the value.
1907 // @todo: Suggestion: Throw an exception instead? Maybe a specific, catchable exception that generates a
1908 // @todo: error message to the user - dynamic item sets via itemProcFunc on check would be a bad idea anyway.
1909 $value = $value & $maxV;
1910 }
1911 if ($field && $realPid >= 0 && $value > 0 && !empty($tcaFieldConf['eval'])) {
1912 $evalCodesArray = GeneralUtility::trimExplode(',', $tcaFieldConf['eval'], true);
1913 $otherRecordsWithSameValue = [];
1914 $maxCheckedRecords = 0;
1915 if (in_array('maximumRecordsCheckedInPid', $evalCodesArray, true)) {
1916 $otherRecordsWithSameValue = $this->getRecordsWithSameValue($table, $id, $field, $value, $realPid);
1917 $maxCheckedRecords = (int)$tcaFieldConf['validation']['maximumRecordsCheckedInPid'];
1918 }
1919 if (in_array('maximumRecordsChecked', $evalCodesArray, true)) {
1920 $otherRecordsWithSameValue = $this->getRecordsWithSameValue($table, $id, $field, $value);
1921 $maxCheckedRecords = (int)$tcaFieldConf['validation']['maximumRecordsChecked'];
1922 }
1923
1924 // there are more than enough records with value "1" in the DB
1925 // if so, set this value to "0" again
1926 if ($maxCheckedRecords && count($otherRecordsWithSameValue) >= $maxCheckedRecords) {
1927 $value = 0;
1928 $this->log($table, $id, 5, 0, 1, '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]);
1929 }
1930 }
1931 $res['value'] = $value;
1932 return $res;
1933 }
1934
1935 /**
1936 * Evaluates 'radio' type values.
1937 *
1938 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
1939 * @param string $value The value to set.
1940 * @param array $tcaFieldConf Field configuration from TCA
1941 * @param string $table The table of the record
1942 * @param int $id The id of the record
1943 * @param int $pid The pid of the record
1944 * @param string $field The field to check
1945 * @return array Modified $res array
1946 */
1947 protected function checkValueForRadio($res, $value, $tcaFieldConf, $table, $id, $pid, $field)
1948 {
1949 if (is_array($tcaFieldConf['items'])) {
1950 foreach ($tcaFieldConf['items'] as $set) {
1951 if ((string)$set[1] === (string)$value) {
1952 $res['value'] = $value;
1953 break;
1954 }
1955 }
1956 }
1957
1958 // if no value was found and an itemsProcFunc is defined, check that for the value
1959 if ($tcaFieldConf['itemsProcFunc'] && empty($res['value'])) {
1960 $processingService = GeneralUtility::makeInstance(ItemProcessingService::class);
1961 $processedItems = $processingService->getProcessingItems(
1962 $table,
1963 $pid,
1964 $field,
1965 $this->checkValue_currentRecord,
1966 $tcaFieldConf,
1967 $tcaFieldConf['items']
1968 );
1969
1970 foreach ($processedItems as $set) {
1971 if ((string)$set[1] === (string)$value) {
1972 $res['value'] = $value;
1973 break;
1974 }
1975 }
1976 }
1977
1978 return $res;
1979 }
1980
1981 /**
1982 * Evaluates 'group' or 'select' type values.
1983 *
1984 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
1985 * @param string|array $value The value to set.
1986 * @param array $tcaFieldConf Field configuration from TCA
1987 * @param string $table Table name
1988 * @param int $id UID of record
1989 * @param mixed $curValue Current value of the field
1990 * @param string $status 'update' or 'new' flag
1991 * @param string $recFID Field identifier [table:uid:field] for flexforms
1992 * @param array $uploadedFiles
1993 * @param string $field Field name
1994 * @return array Modified $res array
1995 */
1996 protected function checkValueForGroupSelect($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $recFID, $uploadedFiles, $field)
1997 {
1998 // Detecting if value sent is an array and if so, implode it around a comma:
1999 if (is_array($value)) {
2000 $value = implode(',', $value);
2001 }
2002 // 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.
2003 // Anyway, this should NOT disturb anything else:
2004 $value = $this->convNumEntityToByteValue($value);
2005 // When values are sent as group or select they come as comma-separated values which are exploded by this function:
2006 $valueArray = $this->checkValue_group_select_explodeSelectGroupValue($value);
2007 // If multiple is not set, remove duplicates:
2008 if (!$tcaFieldConf['multiple']) {
2009 $valueArray = array_unique($valueArray);
2010 }
2011 // If an exclusive key is found, discard all others:
2012 if ($tcaFieldConf['type'] === 'select' && $tcaFieldConf['exclusiveKeys']) {
2013 $exclusiveKeys = GeneralUtility::trimExplode(',', $tcaFieldConf['exclusiveKeys']);
2014 foreach ($valueArray as $index => $key) {
2015 if (in_array($key, $exclusiveKeys, true)) {
2016 $valueArray = [$index => $key];
2017 break;
2018 }
2019 }
2020 }
2021 // 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?)
2022 // 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!!
2023 $valueArray = $this->applyFiltersToValues($tcaFieldConf, $valueArray);
2024 // Checking for select / authMode, removing elements from $valueArray if any of them is not allowed!
2025 if ($tcaFieldConf['type'] === 'select' && $tcaFieldConf['authMode']) {
2026 $preCount = count($valueArray);
2027 foreach ($valueArray as $index => $key) {
2028 if (!$this->BE_USER->checkAuthMode($table, $field, $key, $tcaFieldConf['authMode'])) {
2029 unset($valueArray[$index]);
2030 }
2031 }
2032 // 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.
2033 if ($preCount && empty($valueArray)) {
2034 return [];
2035 }
2036 }
2037 // For select types which has a foreign table attached:
2038 $unsetResult = false;
2039 if (
2040 $tcaFieldConf['type'] === 'group' && $tcaFieldConf['internal_type'] === 'db'
2041 || $tcaFieldConf['type'] === 'select' && ($tcaFieldConf['foreign_table'] || isset($tcaFieldConf['special']) && $tcaFieldConf['special'] === 'languages')
2042 ) {
2043 // check, if there is a NEW... id in the value, that should be substituted later
2044 if (strpos($value, 'NEW') !== false) {
2045 $this->remapStackRecords[$table][$id] = ['remapStackIndex' => count($this->remapStack)];
2046 $this->addNewValuesToRemapStackChildIds($valueArray);
2047 $this->remapStack[] = [
2048 'func' => 'checkValue_group_select_processDBdata',
2049 'args' => [$valueArray, $tcaFieldConf, $id, $status, $tcaFieldConf['type'], $table, $field],
2050 'pos' => ['valueArray' => 0, 'tcaFieldConf' => 1, 'id' => 2, 'table' => 5],
2051 'field' => $field
2052 ];
2053 $unsetResult = true;
2054 } else {
2055 $valueArray = $this->checkValue_group_select_processDBdata($valueArray, $tcaFieldConf, $id, $status, $tcaFieldConf['type'], $table, $field);
2056 }
2057 }
2058 if (!$unsetResult) {
2059 $newVal = $this->checkValue_checkMax($tcaFieldConf, $valueArray);
2060 $res['value'] = $this->castReferenceValue(implode(',', $newVal), $tcaFieldConf);
2061 } else {
2062 unset($res['value']);
2063 }
2064 return $res;
2065 }
2066
2067 /**
2068 * Applies the filter methods from a column's TCA configuration to a value array.
2069 *
2070 * @param array $tcaFieldConfiguration
2071 * @param array $values
2072 * @return array|mixed
2073 * @throws \RuntimeException
2074 */
2075 protected function applyFiltersToValues(array $tcaFieldConfiguration, array $values)
2076 {
2077 if (empty($tcaFieldConfiguration['filter']) || !is_array($tcaFieldConfiguration['filter'])) {
2078 return $values;
2079 }
2080 foreach ($tcaFieldConfiguration['filter'] as $filter) {
2081 if (empty($filter['userFunc'])) {
2082 continue;
2083 }
2084 $parameters = $filter['parameters'] ?: [];
2085 $parameters['values'] = $values;
2086 $parameters['tcaFieldConfig'] = $tcaFieldConfiguration;
2087 $values = GeneralUtility::callUserFunction($filter['userFunc'], $parameters, $this);
2088 if (!is_array($values)) {
2089 throw new \RuntimeException('Failed calling filter userFunc.', 1336051942);
2090 }
2091 }
2092 return $values;
2093 }
2094
2095 /**
2096 * Evaluates 'flex' type values.
2097 *
2098 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
2099 * @param string|array $value The value to set.
2100 * @param array $tcaFieldConf Field configuration from TCA
2101 * @param string $table Table name
2102 * @param int $id UID of record
2103 * @param mixed $curValue Current value of the field
2104 * @param string $status 'update' or 'new' flag
2105 * @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. If $realPid is -1 it means that a new version of the record is being inserted.
2106 * @param string $recFID Field identifier [table:uid:field] for flexforms
2107 * @param int $tscPID TSconfig PID
2108 * @param array $uploadedFiles Uploaded files for the field
2109 * @param string $field Field name
2110 * @return array Modified $res array
2111 */
2112 protected function checkValueForFlex($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $realPid, $recFID, $tscPID, $uploadedFiles, $field)
2113 {
2114 if (is_array($value)) {
2115 // This value is necessary for flex form processing to happen on flexform fields in page records when they are copied.
2116 // Problem: when copying a page, flexform XML comes along in the array for the new record - but since $this->checkValue_currentRecord
2117 // does not have a uid or pid for that sake, the FlexFormTools->getDataStructureIdentifier() function returns no good DS. For new
2118 // records we do know the expected PID so therefore we send that with this special parameter. Only active when larger than zero.
2119 $row = $this->checkValue_currentRecord;
2120 if ($status === 'new') {
2121 $row['pid'] = $realPid;
2122 }
2123
2124 $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
2125
2126 // Get data structure. The methods may throw various exceptions, with some of them being
2127 // ok in certain scenarios, for instance on new record rows. Those are ok to "eat" here
2128 // and substitute with a dummy DS.
2129 $dataStructureArray = ['sheets' => ['sDEF' => []]];
2130 try {
2131 $dataStructureIdentifier = $flexFormTools->getDataStructureIdentifier(
2132 ['config' => $tcaFieldConf],
2133 $table,
2134 $field,
2135 $row
2136 );
2137
2138 $dataStructureArray = $flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
2139 } catch (InvalidParentRowException $e) {
2140 } catch (InvalidParentRowLoopException $e) {
2141 } catch (InvalidParentRowRootException $e) {
2142 } catch (InvalidPointerFieldValueException $e) {
2143 } catch (InvalidIdentifierException $e) {
2144 }
2145
2146 // Get current value array:
2147 $currentValueArray = (string)$curValue !== '' ? GeneralUtility::xml2array($curValue) : [];
2148 if (!is_array($currentValueArray)) {
2149 $currentValueArray = [];
2150 }
2151 // Remove all old meta for languages...
2152 // Evaluation of input values:
2153 $value['data'] = $this->checkValue_flex_procInData($value['data'] ?? [], $currentValueArray['data'] ?? [], $uploadedFiles['data'] ?? [], $dataStructureArray, [$table, $id, $curValue, $status, $realPid, $recFID, $tscPID]);
2154 // Create XML from input value:
2155 $xmlValue = $this->checkValue_flexArray2Xml($value, true);
2156
2157 // 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
2158 // (provided that the current value was already stored IN the charset that the new value is converted to).
2159 $arrValue = GeneralUtility::xml2array($xmlValue);
2160
2161 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkFlexFormValue'] ?? [] as $className) {
2162 $hookObject = GeneralUtility::makeInstance($className);
2163 if (method_exists($hookObject, 'checkFlexFormValue_beforeMerge')) {
2164 $hookObject->checkFlexFormValue_beforeMerge($this, $currentValueArray, $arrValue);
2165 }
2166 }
2167
2168 ArrayUtility::mergeRecursiveWithOverrule($currentValueArray, $arrValue);
2169 $xmlValue = $this->checkValue_flexArray2Xml($currentValueArray, true);
2170
2171 // Action commands (sorting order and removals of elements) for flexform sections,
2172 // see FormEngine for the use of this GP parameter
2173 $actionCMDs = GeneralUtility::_GP('_ACTION_FLEX_FORMdata');
2174 if (is_array($actionCMDs[$table][$id][$field]['data'])) {
2175 $arrValue = GeneralUtility::xml2array($xmlValue);
2176 $this->_ACTION_FLEX_FORMdata($arrValue['data'], $actionCMDs[$table][$id][$field]['data']);
2177 $xmlValue = $this->checkValue_flexArray2Xml($arrValue, true);
2178 }
2179 // Create the value XML:
2180 $res['value'] = '';
2181 $res['value'] .= $xmlValue;
2182 } else {
2183 // Passthrough...:
2184 $res['value'] = $value;
2185 }
2186
2187 return $res;
2188 }
2189
2190 /**
2191 * Converts an array to FlexForm XML
2192 *
2193 * @param array $array Array with FlexForm data
2194 * @param bool $addPrologue If set, the XML prologue is returned as well.
2195 * @return string Input array converted to XML
2196 */
2197 public function checkValue_flexArray2Xml($array, $addPrologue = false)
2198 {
2199 /** @var FlexFormTools $flexObj */
2200 $flexObj = GeneralUtility::makeInstance(FlexFormTools::class);
2201 return $flexObj->flexArray2Xml($array, $addPrologue);
2202 }
2203
2204 /**
2205 * Actions for flex form element (move, delete)
2206 * allows to remove and move flexform sections
2207 *
2208 * @param array $valueArray by reference
2209 * @param array $actionCMDs
2210 */
2211 protected function _ACTION_FLEX_FORMdata(&$valueArray, $actionCMDs)
2212 {
2213 if (!is_array($valueArray) || !is_array($actionCMDs)) {
2214 return;
2215 }
2216
2217 foreach ($actionCMDs as $key => $value) {
2218 if ($key === '_ACTION') {
2219 // First, check if there are "commands":
2220 if (current($actionCMDs[$key]) === '') {
2221 continue;
2222 }
2223
2224 asort($actionCMDs[$key]);
2225 $newValueArray = [];
2226 foreach ($actionCMDs[$key] as $idx => $order) {
2227 // Just one reflection here: It is clear that when removing elements from a flexform, then we will get lost
2228 // files unless we act on this delete operation by traversing and deleting files that were referred to.
2229 if ($order !== 'DELETE') {
2230 $newValueArray[$idx] = $valueArray[$idx];
2231 }
2232 unset($valueArray[$idx]);
2233 }
2234 $valueArray += $newValueArray;
2235 } elseif (is_array($actionCMDs[$key]) && isset($valueArray[$key])) {
2236 $this->_ACTION_FLEX_FORMdata($valueArray[$key], $actionCMDs[$key]);
2237 }
2238 }
2239 }
2240
2241 /**
2242 * Evaluates 'inline' type values.
2243 * (partly copied from the select_group function on this issue)
2244 *
2245 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
2246 * @param string $value The value to set.
2247 * @param array $tcaFieldConf Field configuration from TCA
2248 * @param array $PP Additional parameters in a numeric array: $table,$id,$curValue,$status,$realPid,$recFID
2249 * @param string $field Field name
2250 * @param array $additionalData Additional data to be forwarded to sub-processors
2251 */
2252 public function checkValue_inline($res, $value, $tcaFieldConf, $PP, $field, array $additionalData = null)
2253 {
2254 list($table, $id, , $status) = $PP;
2255 $this->checkValueForInline($res, $value, $tcaFieldConf, $table, $id, $status, $field, $additionalData);
2256 }
2257
2258 /**
2259 * Evaluates 'inline' type values.
2260 * (partly copied from the select_group function on this issue)
2261 *
2262 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
2263 * @param string $value The value to set.
2264 * @param array $tcaFieldConf Field configuration from TCA
2265 * @param string $table Table name
2266 * @param int $id UID of record
2267 * @param string $status 'update' or 'new' flag
2268 * @param string $field Field name
2269 * @param array $additionalData Additional data to be forwarded to sub-processors
2270 * @return array|bool Modified $res array
2271 */
2272 public function checkValueForInline($res, $value, $tcaFieldConf, $table, $id, $status, $field, array $additionalData = null)
2273 {
2274 if (!$tcaFieldConf['foreign_table']) {
2275 // Fatal error, inline fields should always have a foreign_table defined
2276 return false;
2277 }
2278 // When values are sent they come as comma-separated values which are exploded by this function:
2279 $valueArray = GeneralUtility::trimExplode(',', $value);
2280 // Remove duplicates: (should not be needed)
2281 $valueArray = array_unique($valueArray);
2282 // Example for received data:
2283 // $value = 45,NEW4555fdf59d154,12,123
2284 // We need to decide whether we use the stack or can save the relation directly.
2285 if (!empty($value) && (strpos($value, 'NEW') !== false || !MathUtility::canBeInterpretedAsInteger($id))) {
2286 $this->remapStackRecords[$table][$id] = ['remapStackIndex' => count($this->remapStack)];
2287 $this->addNewValuesToRemapStackChildIds($valueArray);
2288 $this->remapStack[] = [
2289 'func' => 'checkValue_inline_processDBdata',
2290 'args' => [$valueArray, $tcaFieldConf, $id, $status, $table, $field, $additionalData],
2291 'pos' => ['valueArray' => 0, 'tcaFieldConf' => 1, 'id' => 2, 'table' => 4],
2292 'additionalData' => $additionalData,
2293 'field' => $field,
2294 ];
2295 unset($res['value']);
2296 } elseif ($value || MathUtility::canBeInterpretedAsInteger($id)) {
2297 $res['value'] = $this->checkValue_inline_processDBdata($valueArray, $tcaFieldConf, $id, $status, $table, $field, $additionalData);
2298 }
2299 return $res;
2300 }
2301
2302 /**
2303 * Checks if a fields has more items than defined via TCA in maxitems.
2304 * If there are more items than allowed, the item list is truncated to the defined number.
2305 *
2306 * @param array $tcaFieldConf Field configuration from TCA
2307 * @param array $valueArray Current value array of items
2308 * @return array The truncated value array of items
2309 */
2310 public function checkValue_checkMax($tcaFieldConf, $valueArray)
2311 {
2312 // BTW, checking for min and max items here does NOT make any sense when MM is used because the above function
2313 // calls will just return an array with a single item (the count) if MM is used... Why didn't I perform the check
2314 // before? Probably because we could not evaluate the validity of record uids etc... Hmm...
2315 // NOTE to the comment: It's not really possible to check for too few items, because you must then determine first,
2316 // if the field is actual used regarding the CType.
2317 $maxitems = isset($tcaFieldConf['maxitems']) ? (int)$tcaFieldConf['maxitems'] : 99999;
2318 return array_slice($valueArray, 0, $maxitems);
2319 }
2320
2321 /*********************************************
2322 *
2323 * Helper functions for evaluation functions.
2324 *
2325 ********************************************/
2326 /**
2327 * Gets a unique value for $table/$id/$field based on $value
2328 *
2329 * @param string $table Table name
2330 * @param string $field Field name for which $value must be unique
2331 * @param string $value Value string.
2332 * @param int $id UID to filter out in the lookup (the record itself...)
2333 * @param int $newPid If set, the value will be unique for this PID
2334 * @return string Modified value (if not-unique). Will be the value appended with a number (until 100, then the function just breaks).
2335 */
2336 public function getUnique($table, $field, $value, $id, $newPid = 0)
2337 {
2338 if (!is_array($GLOBALS['TCA'][$table]) || !is_array($GLOBALS['TCA'][$table]['columns'][$field])) {
2339 // Field is not configured in TCA
2340 return $value;
2341 }
2342
2343 if ((string)$GLOBALS['TCA'][$table]['columns'][$field]['l10n_mode'] === 'exclude') {
2344 $transOrigPointerField = $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'];
2345 $l10nParent = (int)$this->checkValue_currentRecord[$transOrigPointerField];
2346 if ($l10nParent > 0) {
2347 // Current record is a translation and l10n_mode "exclude" just copies the value from source language
2348 return $value;
2349 }
2350 }
2351
2352 $newValue = $originalValue = $value;
2353 $statement = $this->getUniqueCountStatement($newValue, $table, $field, (int)$id, (int)$newPid);
2354 // For as long as records with the test-value existing, try again (with incremented numbers appended)
2355 if ($statement->fetchColumn()) {
2356 for ($counter = 0; $counter <= 100; $counter++) {
2357 $newValue = $value . $counter;
2358 $statement->bindValue(1, $newValue);
2359 $statement->execute();
2360 if (!$statement->fetchColumn()) {
2361 break;
2362 }
2363 }
2364 }
2365
2366 if ($originalValue !== $newValue) {
2367 $this->log($table, $id, 5, 0, 4, '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);
2368 }
2369
2370 return $newValue;
2371 }
2372
2373 /**
2374 * Gets the count of records for a unique field
2375 *
2376 * @param string $value The string value which should be unique
2377 * @param string $table Table name
2378 * @param string $field Field name for which $value must be unique
2379 * @param int $uid UID to filter out in the lookup (the record itself...)
2380 * @param int $pid If set, the value will be unique for this PID
2381 * @return \Doctrine\DBAL\Driver\Statement|int Return the prepared statement to check uniqueness
2382 */
2383 protected function getUniqueCountStatement(
2384 string $value,
2385 string $table,
2386 string $field,
2387 int $uid,
2388 int $pid
2389 ): Statement {
2390 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
2391 $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
2392 $queryBuilder
2393 ->count('uid')
2394 ->from($table)
2395 ->where(
2396 $queryBuilder->expr()->eq($field, $queryBuilder->createPositionalParameter($value, \PDO::PARAM_STR)),
2397 $queryBuilder->expr()->neq('uid', $queryBuilder->createPositionalParameter($uid, \PDO::PARAM_INT))
2398 );
2399 if ($pid !== 0) {
2400 $queryBuilder->andWhere(
2401 $queryBuilder->expr()->eq('pid', $queryBuilder->createPositionalParameter($pid, \PDO::PARAM_INT))
2402 );
2403 } else {
2404 // pid>=0 for versioning
2405 $queryBuilder->andWhere(
2406 $queryBuilder->expr()->gte('pid', $queryBuilder->createPositionalParameter(0, \PDO::PARAM_INT))
2407 );
2408 }
2409
2410 return $queryBuilder->execute();
2411 }
2412
2413 /**
2414 * gets all records that have the same value in a field
2415 * excluding the given uid
2416 *
2417 * @param string $tableName Table name
2418 * @param int $uid UID to filter out in the lookup (the record itself...)
2419 * @param string $fieldName Field name for which $value must be unique
2420 * @param string $value Value string.
2421 * @param int $pageId If set, the value will be unique for this PID
2422 * @return array
2423 */
2424 public function getRecordsWithSameValue($tableName, $uid, $fieldName, $value, $pageId = 0)
2425 {
2426 $result = [];
2427 if (empty($GLOBALS['TCA'][$tableName]['columns'][$fieldName])) {
2428 return $result;
2429 }
2430
2431 $uid = (int)$uid;
2432 $pageId = (int)$pageId;
2433
2434 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
2435 $queryBuilder->getRestrictions()
2436 ->removeAll()
2437 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
2438 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
2439
2440 $queryBuilder->select('*')
2441 ->from($tableName)
2442 ->where(
2443 $queryBuilder->expr()->eq(
2444 $fieldName,
2445 $queryBuilder->createNamedParameter($value, \PDO::PARAM_STR)
2446 ),
2447 $queryBuilder->expr()->neq(
2448 'uid',
2449 $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
2450 )
2451 );
2452
2453 if ($pageId) {
2454 $queryBuilder->andWhere(
2455 $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT))
2456 );
2457 } else {
2458 $queryBuilder->andWhere(
2459 $queryBuilder->expr()->gte('pid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
2460 );
2461 }
2462
2463 $result = $queryBuilder->execute()->fetchAll();
2464
2465 return $result;
2466 }
2467
2468 /**
2469 * @param string $value The field value to be evaluated
2470 * @param array $evalArray Array of evaluations to traverse.
2471 * @param string $is_in The "is_in" value of the field configuration from TCA
2472 * @return array
2473 */
2474 public function checkValue_text_Eval($value, $evalArray, $is_in)
2475 {
2476 $res = [];
2477 $set = true;
2478 foreach ($evalArray as $func) {
2479 switch ($func) {
2480 case 'trim':
2481 $value = trim($value);
2482 break;
2483 case 'required':
2484 if (!$value) {
2485 $set = false;
2486 }
2487 break;
2488 default:
2489 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
2490 if (class_exists($func)) {
2491 $evalObj = GeneralUtility::makeInstance($func);
2492 if (method_exists($evalObj, 'evaluateFieldValue')) {
2493 $value = $evalObj->evaluateFieldValue($value, $is_in, $set);
2494 }
2495 }
2496 }
2497 }
2498 }
2499 if ($set) {
2500 $res['value'] = $value;
2501 }
2502 return $res;
2503 }
2504
2505 /**
2506 * Evaluation of 'input'-type values based on 'eval' list
2507 *
2508 * @param string $value Value to evaluate
2509 * @param array $evalArray Array of evaluations to traverse.
2510 * @param string $is_in Is-in string for 'is_in' evaluation
2511 * @param string $table Table name the eval is evaluated on
2512 * @return array Modified $value in key 'value' or empty array
2513 */
2514 public function checkValue_input_Eval($value, $evalArray, $is_in, string $table = ''): array
2515 {
2516 $res = [];
2517 $set = true;
2518 foreach ($evalArray as $func) {
2519 switch ($func) {
2520 case 'int':
2521 case 'year':
2522 $value = (int)$value;
2523 break;
2524 case 'time':
2525 case 'timesec':
2526 // If $value is a pure integer we have the number of seconds, we can store that directly
2527 if ($value !== '' && !MathUtility::canBeInterpretedAsInteger($value)) {
2528 // $value is an ISO 8601 date
2529 $value = (new \DateTime($value))->getTimestamp();
2530 }
2531 break;
2532 case 'date':
2533 case 'datetime':
2534 // If $value is a pure integer we have the number of seconds, we can store that directly
2535 if ($value !== null && $value !== '' && !MathUtility::canBeInterpretedAsInteger($value)) {
2536 // The value we receive from JS is an ISO 8601 date, which is always in UTC. (the JS code works like that, on purpose!)
2537 // For instance "1999-11-11T11:11:11Z"
2538 // Since the user actually specifies the time in the server's local time, we need to mangle this
2539 // to reflect the server TZ. So we make this 1999-11-11T11:11:11+0200 (assuming Europe/Vienna here)
2540 // In the database we store the date in UTC (1999-11-11T09:11:11Z), hence we take the timestamp of this converted value.
2541 // For achieving this we work with timestamps only (which are UTC) and simply adjust it for the
2542 // TZ difference.
2543 try {
2544 // Make the date from JS a timestamp
2545 $value = (new \DateTime($value))->getTimestamp();
2546 } catch (\Exception $e) {
2547 // set the default timezone value to achieve the value of 0 as a result
2548 $value = (int)date('Z', 0);
2549 }
2550
2551 // @todo this hacky part is problematic when it comes to times around DST switch! Add test to prove that this is broken.
2552 $value -= date('Z', $value);
2553 }
2554 break;
2555 case 'double2':
2556 $value = preg_replace('/[^0-9,\\.-]/', '', $value);
2557 $negative = $value[0] === '-';
2558 $value = strtr($value, [',' => '.', '-' => '']);
2559 if (strpos($value, '.') === false) {
2560 $value .= '.0';
2561 }
2562 $valueArray = explode('.', $value);
2563 $dec = array_pop($valueArray);
2564 $value = implode('', $valueArray) . '.' . $dec;
2565 if ($negative) {
2566 $value *= -1;
2567 }
2568 $value = number_format($value, 2, '.', '');
2569 break;
2570 case 'md5':
2571 if (strlen($value) !== 32) {
2572 $set = false;
2573 }
2574 break;
2575 case 'trim':
2576 $value = trim($value);
2577 break;
2578 case 'upper':
2579 $value = mb_strtoupper($value, 'utf-8');
2580 break;
2581 case 'lower':
2582 $value = mb_strtolower($value, 'utf-8');
2583 break;
2584 case 'required':
2585 if (!isset($value) || $value === '') {
2586 $set = false;
2587 }
2588 break;
2589 case 'is_in':
2590 $c = mb_strlen($value);
2591 if ($c) {
2592 $newVal = '';
2593 for ($a = 0; $a < $c; $a++) {
2594 $char = mb_substr($value, $a, 1);
2595 if (mb_strpos($is_in, $char) !== false) {
2596 $newVal .= $char;
2597 }
2598 }
2599 $value = $newVal;
2600 }
2601 break;
2602 case 'nospace':
2603 $value = str_replace(' ', '', $value);
2604 break;
2605 case 'alpha':
2606 $value = preg_replace('/[^a-zA-Z]/', '', $value);
2607 break;
2608 case 'num':
2609 $value = preg_replace('/[^0-9]/', '', $value);
2610 break;
2611 case 'alphanum':
2612 $value = preg_replace('/[^a-zA-Z0-9]/', '', $value);
2613 break;
2614 case 'alphanum_x':
2615 $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value);
2616 break;
2617 case 'domainname':
2618 if (!preg_match('/^[a-z0-9.\\-]*$/i', $value)) {
2619 $value = (string)HttpUtility::idn_to_ascii($value);
2620 }
2621 break;
2622 case 'email':
2623 if ((string)$value !== '') {
2624 $this->checkValue_input_ValidateEmail($value, $set);
2625 }
2626 break;
2627 case 'saltedPassword':
2628 // An incoming value is either the salted password if the user did not change existing password
2629 // when submitting the form, or a plaintext new password that needs to be turned into a salted password now.
2630 // The strategy is to see if a salt instance can be created from the incoming value. If so,
2631 // no new password was submitted and we keep the value. If no salting instance can be created,
2632 // incoming value must be a new plain text value that needs to be hashed.
2633 $hashFactory = GeneralUtility::makeInstance(PasswordHashFactory::class);
2634 $mode = $table === 'fe_users' ? 'FE' : 'BE';
2635 try {
2636 $hashFactory->get($value, $mode);
2637 } catch (InvalidPasswordHashException $e) {
2638 // We got no salted password instance, incoming value must be a new plaintext password
2639 // Get an instance of the current configured salted password strategy and hash the value
2640 $newHashInstance = $hashFactory->getDefaultHashInstance($mode);
2641 $value = $newHashInstance->getHashedPassword($value);
2642 }
2643 break;
2644 default:
2645 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
2646 if (class_exists($func)) {
2647 $evalObj = GeneralUtility::makeInstance($func);
2648 if (method_exists($evalObj, 'evaluateFieldValue')) {
2649 $value = $evalObj->evaluateFieldValue($value, $is_in, $set);
2650 }
2651 }
2652 }
2653 }
2654 }
2655 if ($set) {
2656 $res['value'] = $value;
2657 }
2658 return $res;
2659 }
2660
2661 /**
2662 * If $value is not a valid e-mail address,
2663 * $set will be set to false and a flash error
2664 * message will be added
2665 *
2666 * @param string $value Value to evaluate
2667 * @param bool $set TRUE if an update should be done
2668 * @throws \InvalidArgumentException
2669 * @throws \TYPO3\CMS\Core\Exception
2670 */
2671 protected function checkValue_input_ValidateEmail($value, &$set)
2672 {
2673 if (GeneralUtility::validEmail($value)) {
2674 return;
2675 }
2676
2677 $set = false;
2678 /** @var FlashMessage $message */
2679 $message = GeneralUtility::makeInstance(
2680 FlashMessage::class,
2681 sprintf($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.invalidEmail'), $value),
2682 '', // header is optional
2683 FlashMessage::ERROR,
2684 true // whether message should be stored in session
2685 );
2686 /** @var FlashMessageService $flashMessageService */
2687 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
2688 $flashMessageService->getMessageQueueByIdentifier()->enqueue($message);
2689 }
2690
2691 /**
2692 * Returns data for group/db and select fields
2693 *
2694 * @param array $valueArray Current value array
2695 * @param array $tcaFieldConf TCA field config
2696 * @param int $id Record id, used for look-up of MM relations (local_uid)
2697 * @param string $status Status string ('update' or 'new')
2698 * @param string $type The type, either 'select', 'group' or 'inline'
2699 * @param string $currentTable Table name, needs to be passed to \TYPO3\CMS\Core\Database\RelationHandler
2700 * @param string $currentField field name, needs to be set for writing to sys_history
2701 * @return array Modified value array
2702 */
2703 public function checkValue_group_select_processDBdata($valueArray, $tcaFieldConf, $id, $status, $type, $currentTable, $currentField)
2704 {
2705 if ($type === 'group') {
2706 $tables = $tcaFieldConf['allowed'];
2707 } elseif (!empty($tcaFieldConf['special']) && $tcaFieldConf['special'] === 'languages') {
2708 $tables = 'sys_language';
2709 } else {
2710 $tables = $tcaFieldConf['foreign_table'];
2711 }
2712 $prep = $type === 'group' ? $tcaFieldConf['prepend_tname'] : '';
2713 $newRelations = implode(',', $valueArray);
2714 /** @var RelationHandler $dbAnalysis */
2715 $dbAnalysis = $this->createRelationHandlerInstance();
2716 $dbAnalysis->registerNonTableValues = !empty($tcaFieldConf['allowNonIdValues']);
2717 $dbAnalysis->start($newRelations, $tables, '', 0, $currentTable, $tcaFieldConf);
2718 if ($tcaFieldConf['MM']) {
2719 // convert submitted items to use version ids instead of live ids
2720 // (only required for MM relations in a workspace context)
2721 $dbAnalysis->convertItemArray();
2722 if ($status === 'update') {
2723 /** @var RelationHandler $oldRelations_dbAnalysis */
2724 $oldRelations_dbAnalysis = $this->createRelationHandlerInstance();
2725 $oldRelations_dbAnalysis->registerNonTableValues = !empty($tcaFieldConf['allowNonIdValues']);
2726 // Db analysis with $id will initialize with the existing relations
2727 $oldRelations_dbAnalysis->start('', $tables, $tcaFieldConf['MM'], $id, $currentTable, $tcaFieldConf);
2728 $oldRelations = implode(',', $oldRelations_dbAnalysis->getValueArray());
2729 $dbAnalysis->writeMM($tcaFieldConf['MM'], $id, $prep);
2730 if ($oldRelations != $newRelations) {
2731 $this->mmHistoryRecords[$currentTable . ':' . $id]['oldRecord'][$currentField] = $oldRelations;
2732 $this->mmHistoryRecords[$currentTable . ':' . $id]['newRecord'][$currentField] = $newRelations;
2733 } else {
2734 $this->mmHistoryRecords[$currentTable . ':' . $id]['oldRecord'][$currentField] = '';
2735 $this->mmHistoryRecords[$currentTable . ':' . $id]['newRecord'][$currentField] = '';
2736 }
2737 } else {
2738 $this->dbAnalysisStore[] = [$dbAnalysis, $tcaFieldConf['MM'], $id, $prep, $currentTable];
2739 }
2740 $valueArray = $dbAnalysis->countItems();
2741 } else {
2742 $valueArray = $dbAnalysis->getValueArray($prep);
2743 }
2744 // 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.
2745 return $valueArray;
2746 }
2747
2748 /**
2749 * Explodes the $value, which is a list of files/uids (group select)
2750 *
2751 * @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.
2752 * @return array The value array.
2753 */
2754 public function checkValue_group_select_explodeSelectGroupValue($value)
2755 {
2756 $valueArray = GeneralUtility::trimExplode(',', $value, true);
2757 foreach ($valueArray as &$newVal) {
2758 $temp = explode('|', $newVal, 2);
2759 $newVal = str_replace(['|', ','], '', rawurldecode($temp[0]));
2760 }
2761 unset($newVal);
2762 return $valueArray;
2763 }
2764
2765 /**
2766 * Starts the processing the input data for flexforms. This will traverse all sheets / languages and for each it will traverse the sub-structure.
2767 * See checkValue_flex_procInData_travDS() for more details.
2768 * 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
2769 *
2770 * @param array $dataPart The 'data' part of the INPUT flexform data
2771 * @param array $dataPart_current The 'data' part of the CURRENT flexform data
2772 * @param array $uploadedFiles The uploaded files for the 'data' part of the INPUT flexform data
2773 * @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.
2774 * @param array $pParams A set of parameters to pass through for the calling of the evaluation functions
2775 * @param string $callBackFunc Optional call back function, see checkValue_flex_procInData_travDS() DEPRECATED, use \TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools instead for traversal!
2776 * @param array $workspaceOptions
2777 * @return array The modified 'data' part.
2778 * @see checkValue_flex_procInData_travDS()
2779 */
2780 public function checkValue_flex_procInData($dataPart, $dataPart_current, $uploadedFiles, $dataStructure, $pParams, $callBackFunc = '', array $workspaceOptions = [])
2781 {
2782 if (is_array($dataPart)) {
2783 foreach ($dataPart as $sKey => $sheetDef) {
2784 if (isset($dataStructure['sheets'][$sKey]) && is_array($dataStructure['sheets'][$sKey]) && is_array($sheetDef)) {
2785 foreach ($sheetDef as $lKey => $lData) {
2786 $this->checkValue_flex_procInData_travDS(
2787 $dataPart[$sKey][$lKey],
2788 $dataPart_current[$sKey][$lKey],
2789 $uploadedFiles[$sKey][$lKey],
2790 $dataStructure['sheets'][$sKey]['ROOT']['el'],
2791 $pParams,
2792 $callBackFunc,
2793 $sKey . '/' . $lKey . '/',
2794 $workspaceOptions
2795 );
2796 }
2797 }
2798 }
2799 }
2800 return $dataPart;
2801 }
2802
2803 /**
2804 * Processing of the sheet/language data array
2805 * 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.
2806 *
2807 * @param array $dataValues New values (those being processed): Multidimensional Data array for sheet/language, passed by reference!
2808 * @param array $dataValues_current Current values: Multidimensional Data array. May be empty array() if not needed (for callBackFunctions)
2809 * @param array $uploadedFiles Uploaded files array for sheet/language. May be empty array() if not needed (for callBackFunctions)
2810 * @param array $DSelements Data structure which fits the data array
2811 * @param array $pParams A set of parameters to pass through for the calling of the evaluation functions / call back function
2812 * @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.
2813 * @param string $structurePath
2814 * @param array $workspaceOptions
2815 * @see checkValue_flex_procInData()
2816 */
2817 public function checkValue_flex_procInData_travDS(&$dataValues, $dataValues_current, $uploadedFiles, $DSelements, $pParams, $callBackFunc, $structurePath, array $workspaceOptions = [])
2818 {
2819 if (!is_array($DSelements)) {
2820 return;
2821 }
2822
2823 // For each DS element:
2824 foreach ($DSelements as $key => $dsConf) {
2825 // Array/Section:
2826 if ($DSelements[$key]['type'] === 'array') {
2827 if (!is_array($dataValues[$key]['el'])) {
2828 continue;
2829 }
2830
2831 if ($DSelements[$key]['section']) {
2832 foreach ($dataValues[$key]['el'] as $ik => $el) {
2833 if (!is_array($el)) {
2834 continue;
2835 }
2836
2837 if (!is_array($dataValues_current[$key]['el'])) {
2838 $dataValues_current[$key]['el'] = [];
2839 }
2840 $theKey = key($el);
2841 if (!is_array($dataValues[$key]['el'][$ik][$theKey]['el'])) {
2842 continue;
2843 }
2844
2845 $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);
2846 }
2847 } else {
2848 if (!isset($dataValues[$key]['el'])) {
2849 $dataValues[$key]['el'] = [];
2850 }
2851 $this->checkValue_flex_procInData_travDS($dataValues[$key]['el'], $dataValues_current[$key]['el'], $uploadedFiles[$key]['el'], $DSelements[$key]['el'], $pParams, $callBackFunc, $structurePath . $key . '/el/', $workspaceOptions);
2852 }
2853 } else {
2854 // When having no specific sheets, it's "TCEforms.config", when having a sheet, it's just "config"
2855 $fieldConfiguration = $dsConf['TCEforms']['config'] ?? $dsConf['config'] ?? null;
2856 // init with value from config for passthrough fields
2857 if (!empty($fieldConfiguration['type']) && $fieldConfiguration['type'] === 'passthrough') {
2858 if (!empty($dataValues_current[$key]['vDEF'])) {
2859 // If there is existing value, keep it
2860 $dataValues[$key]['vDEF'] = $dataValues_current[$key]['vDEF'];
2861 } elseif (
2862 !empty($fieldConfiguration['default'])
2863 && isset($pParams[1])
2864 && !MathUtility::canBeInterpretedAsInteger($pParams[1])
2865 ) {
2866 // If is new record and a default is specified for field, use it.
2867 $dataValues[$key]['vDEF'] = $fieldConfiguration['default'];
2868 }
2869 }
2870 if (!is_array($fieldConfiguration) || !is_array($dataValues[$key])) {
2871 continue;
2872 }
2873
2874 foreach ($dataValues[$key] as $vKey => $data) {
2875 if ($callBackFunc) {
2876 if (is_object($this->callBackObj)) {
2877 $res = $this->callBackObj->{$callBackFunc}($pParams, $fieldConfiguration, $dataValues[$key][$vKey], $dataValues_current[$key][$vKey], $uploadedFiles[$key][$vKey], $structurePath . $key . '/' . $vKey . '/', $workspaceOptions);
2878 } else {
2879 $res = $this->{$callBackFunc}($pParams, $fieldConfiguration, $dataValues[$key][$vKey], $dataValues_current[$key][$vKey], $uploadedFiles[$key][$vKey], $structurePath . $key . '/' . $vKey . '/', $workspaceOptions);
2880 }
2881 } else {
2882 // Default
2883 list($CVtable, $CVid, $CVcurValue, $CVstatus, $CVrealPid, $CVrecFID, $CVtscPID) = $pParams;
2884
2885 $additionalData = [
2886 'flexFormId' => $CVrecFID,
2887 'flexFormPath' => trim(rtrim($structurePath, '/') . '/' . $key . '/' . $vKey, '/'),
2888 ];
2889
2890 $res = $this->checkValue_SW([], $dataValues[$key][$vKey], $fieldConfiguration, $CVtable, $CVid, $dataValues_current[$key][$vKey], $CVstatus, $CVrealPid, $CVrecFID, '', $uploadedFiles[$key][$vKey], $CVtscPID, $additionalData);
2891 }
2892 // Adding the value:
2893 if (isset($res['value'])) {
2894 $dataValues[$key][$vKey] = $res['value'];
2895 }
2896 // 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.
2897 // 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).
2898 if (mb_substr($vKey, -9) !== '.vDEFbase') {
2899 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['flexFormXMLincludeDiffBase'] && $vKey !== 'vDEF' && ((string)$dataValues[$key][$vKey] !== (string)$dataValues_current[$key][$vKey] || !isset($dataValues_current[$key][$vKey . '.vDEFbase']))) {
2900 // 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:
2901 if (isset($dataValues[$key]['vDEF'])) {
2902 $diffValue = $dataValues[$key]['vDEF'];
2903 } else {
2904 // If not found (for translators with no access to the default language) we use the one from the current-value data set:
2905 $diffValue = $dataValues_current[$key]['vDEF'];
2906 }
2907 // 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.
2908 $dataValues[$key][$vKey . '.vDEFbase'] = $diffValue;
2909 }
2910 }
2911 }
2912 }
2913 }
2914 }
2915
2916 /**
2917 * Returns data for inline fields.
2918 *
2919 * @param array $valueArray Current value array
2920 * @param array $tcaFieldConf TCA field config
2921 * @param int $id Record id
2922 * @param string $status Status string ('update' or 'new')
2923 * @param string $table Table name, needs to be passed to \TYPO3\CMS\Core\Database\RelationHandler
2924 * @param string $field The current field the values are modified for
2925 * @param array $additionalData Additional data to be forwarded to sub-processors
2926 * @return string Modified values
2927 */
2928 protected function checkValue_inline_processDBdata($valueArray, $tcaFieldConf, $id, $status, $table, $field, array $additionalData = null)
2929 {
2930 $foreignTable = $tcaFieldConf['foreign_table'];
2931 $valueArray = $this->applyFiltersToValues($tcaFieldConf, $valueArray);
2932 // Fetch the related child records using \TYPO3\CMS\Core\Database\RelationHandler
2933 /** @var RelationHandler $dbAnalysis */
2934 $dbAnalysis = $this->createRelationHandlerInstance();
2935 $dbAnalysis->start(implode(',', $valueArray), $foreignTable, '', 0, $table, $tcaFieldConf);
2936 // IRRE with a pointer field (database normalization):
2937 if ($tcaFieldConf['foreign_field']) {
2938 // if the record was imported, sorting was also imported, so skip this
2939 $skipSorting = (bool)$this->callFromImpExp;
2940 // update record in intermediate table (sorting & pointer uid to parent record)
2941 $dbAnalysis->writeForeignField($tcaFieldConf, $id, 0, $skipSorting);
2942 $newValue = $dbAnalysis->countItems(false);
2943 } elseif ($this->getInlineFieldType($tcaFieldConf) === 'mm') {
2944 // In order to fully support all the MM stuff, directly call checkValue_group_select_processDBdata instead of repeating the needed code here
2945 $valueArray = $this->checkValue_group_select_processDBdata($valueArray, $tcaFieldConf, $id, $status, 'select', $table, $field);
2946 $newValue = $valueArray[0];
2947 } else {
2948 $valueArray = $dbAnalysis->getValueArray();
2949 // Checking that the number of items is correct:
2950 $valueArray = $this->checkValue_checkMax($tcaFieldConf, $valueArray);
2951 $newValue = $this->castReferenceValue(implode(',', $valueArray), $tcaFieldConf);
2952 }
2953 return $newValue;
2954 }
2955
2956 /*********************************************
2957 *
2958 * PROCESSING COMMANDS
2959 *
2960 ********************************************/
2961 /**
2962 * Processing the cmd-array
2963 * See "TYPO3 Core API" for a description of the options.
2964 *
2965 * @return void|bool
2966 */
2967 public function process_cmdmap()
2968 {
2969 // Editing frozen:
2970 if ($this->BE_USER->workspace !== 0 && $this->BE_USER->workspaceRec['freeze']) {
2971 $this->newlog('All editing in this workspace has been frozen!', 1);
2972 return false;
2973 }
2974 // Hook initialization:
2975 $hookObjectsArr = [];
2976 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'] ?? [] as $className) {
2977 $hookObj = GeneralUtility::makeInstance($className);
2978 if (method_exists($hookObj, 'processCmdmap_beforeStart')) {
2979 $hookObj->processCmdmap_beforeStart($this);
2980 }
2981 $hookObjectsArr[] = $hookObj;
2982 }
2983 $pasteDatamap = [];
2984 // Traverse command map:
2985 foreach ($this->cmdmap as $table => $_) {
2986 // Check if the table may be modified!
2987 $modifyAccessList = $this->checkModifyAccessList($table);
2988 if (!$modifyAccessList) {
2989 $this->log($table, 0, 2, 0, 1, 'Attempt to modify table \'%s\' without permission', 1, [$table]);
2990 }
2991 // Check basic permissions and circumstances:
2992 if (!isset($GLOBALS['TCA'][$table]) || $this->tableReadOnly($table) || !is_array($this->cmdmap[$table]) || !$modifyAccessList) {
2993 continue;
2994 }
2995
2996 // Traverse the command map:
2997 foreach ($this->cmdmap[$table] as $id => $incomingCmdArray) {
2998 if (!is_array($incomingCmdArray)) {
2999 continue;
3000 }
3001
3002 if ($table === 'pages') {
3003 // for commands on pages do a pagetree-refresh
3004 $this->pagetreeNeedsRefresh = true;
3005 }
3006
3007 foreach ($incomingCmdArray as $command => $value) {
3008 $pasteUpdate = false;
3009 if (is_array($value) && isset($value['action']) && $value['action'] === 'paste') {
3010 // Extended paste command: $command is set to "move" or "copy"
3011 // $value['update'] holds field/value pairs which should be updated after copy/move operation
3012 // $value['target'] holds original $value (target of move/copy)
3013 $pasteUpdate = $value['update'];
3014 $value = $value['target'];
3015 }
3016 foreach ($hookObjectsArr as $hookObj) {
3017 if (method_exists($hookObj, 'processCmdmap_preProcess')) {
3018 $hookObj->processCmdmap_preProcess($command, $table, $id, $value, $this, $pasteUpdate);
3019 }
3020 }
3021 // Init copyMapping array:
3022 // Must clear this array before call from here to those functions:
3023 // Contains mapping information between new and old id numbers.
3024 $this->copyMappingArray = [];
3025 // process the command
3026 $commandIsProcessed = false;
3027 foreach ($hookObjectsArr as $hookObj) {
3028 if (method_exists($hookObj, 'processCmdmap')) {
3029 $hookObj->processCmdmap($command, $table, $id, $value, $commandIsProcessed, $this, $pasteUpdate);
3030 }
3031 }
3032 // Only execute default commands if a hook hasn't been processed the command already
3033 if (!$commandIsProcessed) {
3034 $procId = $id;
3035 $backupUseTransOrigPointerField = $this->useTransOrigPointerField;
3036 // Branch, based on command
3037 switch ($command) {
3038 case 'move':
3039 $this->moveRecord($table, $id, $value);
3040 break;
3041 case 'copy':
3042 $target = $value['target'] ?? $value;
3043 $ignoreLocalization = (bool)($value['ignoreLocalization'] ?? false);
3044 if ($table === 'pages') {
3045 $this->copyPages($id, $target);
3046 } else {
3047 $this->copyRecord($table, $id, $target, true, [], '', 0, $ignoreLocalization);
3048 }
3049 $procId = $this->copyMappingArray[$table][$id];
3050 break;
3051 case 'localize':
3052 $this->useTransOrigPointerField = true;
3053 $this->localize($table, $id, $value);
3054 break;
3055 case 'copyToLanguage':
3056 $this->useTransOrigPointerField = false;
3057 $this->localize($table, $id, $value);
3058 break;
3059 case 'inlineLocalizeSynchronize':
3060 $this->inlineLocalizeSynchronize($table, $id, $value);
3061 break;
3062 case 'delete':
3063 $this->deleteAction($table, $id);
3064 break;
3065 case 'undelete':
3066 $this->undeleteRecord($table, $id);
3067 break;
3068 }
3069 $this->useTransOrigPointerField = $backupUseTransOrigPointerField;
3070 if (is_array($pasteUpdate)) {
3071 $pasteDatamap[$table][$procId] = $pasteUpdate;
3072 }
3073 }
3074 foreach ($hookObjectsArr as $hookObj) {
3075 if (method_exists($hookObj, 'processCmdmap_postProcess')) {
3076 $hookObj->processCmdmap_postProcess($command, $table, $id, $value, $this, $pasteUpdate, $pasteDatamap);
3077 }
3078 }
3079 // Merging the copy-array info together for remapping purposes.
3080 ArrayUtility::mergeRecursiveWithOverrule($this->copyMappingArray_merged, $this->copyMappingArray);
3081 }
3082 }
3083 }
3084 /** @var DataHandler $copyTCE */
3085 $copyTCE = $this->getLocalTCE();
3086 $copyTCE->start($pasteDatamap, [], $this->BE_USER);
3087 $copyTCE->process_datamap();
3088 $this->errorLog = array_merge($this->errorLog, $copyTCE->errorLog);
3089 unset($copyTCE);
3090
3091 // Finally, before exit, check if there are ID references to remap.
3092 // This might be the case if versioning or copying has taken place!
3093 $this->remapListedDBRecords();
3094 $this->processRemapStack();
3095 foreach ($hookObjectsArr as $hookObj) {
3096 if (method_exists($hookObj, 'processCmdmap_afterFinish')) {
3097 $hookObj->processCmdmap_afterFinish($this);
3098 }
3099 }
3100 if ($this->isOuterMostInstance()) {
3101 $this->processClearCacheQueue();
3102 $this->resetNestedElementCalls();
3103 }
3104 }
3105
3106 /*********************************************
3107 *
3108 * Cmd: Copying
3109 *
3110 ********************************************/
3111 /**
3112 * Copying a single record
3113 *
3114 * @param string $table Element table
3115 * @param int $uid Element UID
3116 * @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
3117 * @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
3118 * @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!
3119 * @param string $excludeFields Commalist of fields to exclude from the copy process (might get default values)
3120 * @param int $language Language ID (from sys_language table)
3121 * @param bool $ignoreLocalization If TRUE, any localization routine is skipped
3122 * @return int|null ID of new record, if any
3123 */
3124 public function copyRecord($table, $uid, $destPid, $first = false, $overrideValues = [], $excludeFields = '', $language = 0, $ignoreLocalization = false)
3125 {
3126 $uid = ($origUid = (int)$uid);
3127 // Only copy if the table is defined in $GLOBALS['TCA'], a uid is given and the record wasn't copied before:
3128 if (empty($GLOBALS['TCA'][$table]) || $uid === 0) {
3129 return null;
3130 }
3131 if ($this->isRecordCopied($table, $uid)) {
3132 return null;
3133 }
3134
3135 // Fetch record with permission check
3136 $row = $this->recordInfoWithPermissionCheck($table, $uid, 'show');
3137
3138 // This checks if the record can be selected which is all that a copy action requires.
3139 if ($row === false) {
3140 $this->log($table, $uid, 1, 0, 1, 'Attempt to copy record "%s:%s" which does not exist or you do not have permission to read', -1, [$table, $uid]);
3141 return null;
3142 }
3143
3144 // Check if table is allowed on destination page
3145 if ($destPid >= 0 && !$this->isTableAllowedForThisPage($destPid, $table)) {
3146 $this->log($table, $uid, 1, 0, 1, 'Attempt to insert record "%s:%s" on a page (%s) that can\'t store record type.', -1, [$table, $uid, $destPid]);
3147 return null;
3148 }
3149
3150 $fullLanguageCheckNeeded = $table !== 'pages';
3151 // Used to check language and general editing rights
3152 if (!$ignoreLocalization && ($language <= 0 || !$this->BE_USER->checkLanguageAccess($language)) && !$this->BE_USER->recordEditAccessInternals($table, $uid, false, false, $fullLanguageCheckNeeded)) {
3153 $this->log($table, $uid, 1, 0, 1, 'Attempt to copy record "%s:%s" without having permissions to do so. [' . $this->BE_USER->errorMsg . '].', -1, [$table, $uid]);
3154 return null;
3155 }
3156
3157 $data = [];
3158 $nonFields = array_unique(GeneralUtility::trimExplode(',', 'uid,perms_userid,perms_groupid,perms_user,perms_group,perms_everybody,t3ver_oid,t3ver_wsid,t3ver_state,t3ver_count,t3ver_stage,t3ver_tstamp,' . $excludeFields, true));
3159 BackendUtility::workspaceOL($table, $row, -99, false);
3160 $row = BackendUtility::purgeComputedPropertiesFromRecord($row);
3161
3162 // Initializing:
3163 $theNewID = StringUtility::getUniqueId('NEW');
3164 $enableField = isset($GLOBALS['TCA'][$table]['ctrl']['enablecolumns']) ? $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'] : '';
3165 $headerField = $GLOBALS['TCA'][$table]['ctrl']['label'];
3166 // Getting "copy-after" fields if applicable:
3167 $copyAfterFields = $destPid < 0 ? $this->fixCopyAfterDuplFields($table, $uid, abs($destPid), 0) : [];
3168 // Page TSconfig related:
3169 // 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...
3170 $tscPID = BackendUtility::getTSconfig_pidValue($table, $uid, $destPid);
3171 $TSConfig = BackendUtility::getPagesTSconfig($tscPID)['TCEMAIN.'] ?? [];
3172 $tE = $this->getTableEntries($table, $TSConfig);
3173 // Traverse ALL fields of the selected record:
3174 foreach ($row as $field => $value) {
3175 if (!in_array($field, $nonFields, true)) {
3176 // Get TCA configuration for the field:
3177 $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
3178 // Preparation/Processing of the value:
3179 // "pid" is hardcoded of course:
3180 // isset() won't work here, since values can be NULL in each of the arrays
3181 // except setDefaultOnCopyArray, since we exploded that from a string
3182 if ($field === 'pid') {
3183 $value = $destPid;
3184 } elseif (array_key_exists($field, $overrideValues)) {
3185 // Override value...
3186 $value = $overrideValues[$field];
3187 } elseif (array_key_exists($field, $copyAfterFields)) {
3188 // Copy-after value if available:
3189 $value = $copyAfterFields[$field];
3190 } else {
3191 // Hide at copy may override:
3192 if ($first && $field == $enableField && $GLOBALS['TCA'][$table]['ctrl']['hideAtCopy'] && !$this->neverHideAtCopy && !$tE['disableHideAtCopy']) {
3193 $value = 1;
3194 }
3195 // Prepend label on copy:
3196 if ($first && $field == $headerField && $GLOBALS['TCA'][$table]['ctrl']['prependAtCopy'] && !$tE['disablePrependAtCopy']) {
3197 $value = $this->getCopyHeader($table, $this->resolvePid($table, $destPid), $field, $this->clearPrefixFromValue($table, $value), 0);
3198 }
3199 // Processing based on the TCA config field type (files, references, flexforms...)
3200 $value = $this->copyRecord_procBasedOnFieldType($table, $uid, $field, $value, $row, $conf, $tscPID, $language);
3201 }
3202 // Add value to array.
3203 $data[$table][$theNewID][$field] = $value;
3204 }
3205 }
3206 // Overriding values:
3207 if ($GLOBALS['TCA'][$table]['ctrl']['editlock']) {
3208 $data[$table][$theNewID][$GLOBALS['TCA'][$table]['ctrl']['editlock']] = 0;
3209 }
3210 // Setting original UID:
3211 if ($GLOBALS['TCA'][$table]['ctrl']['origUid']) {
3212 $data[$table][$theNewID][$GLOBALS['TCA'][$table]['ctrl']['origUid']] = $uid;
3213 }
3214 // Do the copy by simply submitting the array through DataHandler:
3215 /** @var DataHandler $copyTCE */
3216 $copyTCE = $this->getLocalTCE();
3217 $copyTCE->start($data, [], $this->BE_USER);
3218 $copyTCE->process_datamap();
3219 // Getting the new UID:
3220 $theNewSQLID = $copyTCE->substNEWwithIDs[$theNewID];
3221 if ($theNewSQLID) {
3222 $this->copyMappingArray[$table][$origUid] = $theNewSQLID;
3223 // Keep automatically versionized record information:
3224 if (isset($copyTCE->autoVersionIdMap[$table][$theNewSQLID])) {
3225 $this->autoVersionIdMap[$table][$theNewSQLID] = $copyTCE->autoVersionIdMap[$table][$theNewSQLID];
3226 }
3227 }
3228 $this->errorLog = array_merge($this->errorLog, $copyTCE->errorLog);
3229 unset($copyTCE);
3230 if (!$ignoreLocalization && $language == 0) {
3231 //repointing the new translation records to the parent record we just created
3232 $overrideValues[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'