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