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