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