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