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