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